ssh2transport.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Header connecting the pieces of the SSH-2 transport layer.
  3. */
  4. #ifndef PUTTY_SSH2TRANSPORT_H
  5. #define PUTTY_SSH2TRANSPORT_H
  6. #ifndef NO_GSSAPI
  7. #include "sshgssc.h"
  8. #include "sshgss.h"
  9. #define MIN_CTXT_LIFETIME 5 /* Avoid rekey with short lifetime (seconds) */
  10. #define GSS_KEX_CAPABLE (1<<0) /* Can do GSS KEX */
  11. #define GSS_CRED_UPDATED (1<<1) /* Cred updated since previous delegation */
  12. #define GSS_CTXT_EXPIRES (1<<2) /* Context expires before next timer */
  13. #define GSS_CTXT_MAYFAIL (1<<3) /* Context may expire during handshake */
  14. #endif
  15. #define DH_MIN_SIZE 1024
  16. #define DH_MAX_SIZE 8192
  17. enum kexlist {
  18. KEXLIST_KEX, KEXLIST_HOSTKEY, KEXLIST_CSCIPHER, KEXLIST_SCCIPHER,
  19. KEXLIST_CSMAC, KEXLIST_SCMAC, KEXLIST_CSCOMP, KEXLIST_SCCOMP,
  20. NKEXLIST
  21. };
  22. #define MAXKEXLIST 16
  23. struct kexinit_algorithm {
  24. const char *name;
  25. union {
  26. struct {
  27. const ssh_kex *kex;
  28. bool warn;
  29. } kex;
  30. struct {
  31. const ssh_keyalg *hostkey;
  32. bool warn;
  33. } hk;
  34. struct {
  35. const ssh_cipheralg *cipher;
  36. bool warn;
  37. } cipher;
  38. struct {
  39. const ssh2_macalg *mac;
  40. bool etm;
  41. } mac;
  42. struct {
  43. const ssh_compression_alg *comp;
  44. bool delayed;
  45. } comp;
  46. } u;
  47. };
  48. #define HOSTKEY_ALGORITHMS(X) \
  49. X(HK_ED25519, ssh_ecdsa_ed25519) \
  50. X(HK_ECDSA, ssh_ecdsa_nistp256) \
  51. X(HK_ECDSA, ssh_ecdsa_nistp384) \
  52. X(HK_ECDSA, ssh_ecdsa_nistp521) \
  53. /* Changed order to match WinSCP default preference list for SshHostKeyList() */ \
  54. X(HK_RSA, ssh_rsa) \
  55. X(HK_DSA, ssh_dss) \
  56. /* end of list */
  57. #define COUNT_HOSTKEY_ALGORITHM(type, alg) +1
  58. #define N_HOSTKEY_ALGORITHMS (0 HOSTKEY_ALGORITHMS(COUNT_HOSTKEY_ALGORITHM))
  59. struct ssh_signkey_with_user_pref_id {
  60. const ssh_keyalg *alg;
  61. int id;
  62. };
  63. extern const struct ssh_signkey_with_user_pref_id
  64. ssh2_hostkey_algs[N_HOSTKEY_ALGORITHMS];
  65. /*
  66. * Enumeration of high-level classes of reason why we might need to do
  67. * a repeat key exchange. A full detailed reason in human-readable
  68. * string form for the Event Log is also provided, but this enum type
  69. * is used to discriminate between classes of reason that the code
  70. * needs to treat differently.
  71. *
  72. * RK_NONE == 0 is the value indicating that no rekey is currently
  73. * needed at all. RK_INITIAL indicates that we haven't even done the
  74. * _first_ key exchange yet. RK_SERVER indicates that we're rekeying
  75. * because the server asked for it, not because we decided it
  76. * ourselves. RK_NORMAL is the usual case. RK_GSS_UPDATE indicates
  77. * that we're rekeying because we've just got new GSSAPI credentials
  78. * (hence there's no point in doing a preliminary check for new GSS
  79. * creds, because we already know the answer); RK_POST_USERAUTH
  80. * indicates that _if_ we're going to need a post-userauth immediate
  81. * rekey for any reason, this is the moment to do it.
  82. *
  83. * So RK_POST_USERAUTH only tells the transport layer to _consider_
  84. * rekeying, not to definitely do it. Also, that one enum value is
  85. * special in that the user-readable reason text is passed in to the
  86. * transport layer as NULL, whereas fills in the reason text after it
  87. * decides whether it needs a rekey at all. In the other cases,
  88. * rekey_reason is passed in to the at the same time as rekey_class.
  89. */
  90. typedef enum RekeyClass {
  91. RK_NONE = 0,
  92. RK_INITIAL,
  93. RK_SERVER,
  94. RK_NORMAL,
  95. RK_POST_USERAUTH,
  96. RK_GSS_UPDATE
  97. } RekeyClass;
  98. typedef struct transport_direction {
  99. const ssh_cipheralg *cipher;
  100. const ssh2_macalg *mac;
  101. bool etm_mode;
  102. const ssh_compression_alg *comp;
  103. bool comp_delayed;
  104. int mkkey_adjust;
  105. } transport_direction;
  106. struct ssh2_transport_state {
  107. int crState, crStateKex;
  108. PacketProtocolLayer *higher_layer;
  109. PktInQueue pq_in_higher;
  110. PktOutQueue pq_out_higher;
  111. IdempotentCallback ic_pq_out_higher;
  112. Conf *conf;
  113. char *savedhost;
  114. int savedport;
  115. const char *rekey_reason;
  116. enum RekeyClass rekey_class;
  117. unsigned long max_data_size;
  118. const ssh_kex *kex_alg;
  119. const ssh_keyalg *hostkey_alg;
  120. char *hostkey_str; /* string representation, for easy checking in rekeys */
  121. unsigned char session_id[MAX_HASH_LEN];
  122. int session_id_len;
  123. int dh_min_size, dh_max_size;
  124. bool dh_got_size_bounds;
  125. dh_ctx *dh_ctx;
  126. ssh_hash *exhash;
  127. struct DataTransferStats *stats;
  128. char *client_greeting, *server_greeting;
  129. bool kex_in_progress;
  130. unsigned long next_rekey, last_rekey;
  131. const char *deferred_rekey_reason;
  132. bool higher_layer_ok;
  133. /*
  134. * Fully qualified host name, which we need if doing GSSAPI.
  135. */
  136. char *fullhostname;
  137. /* shgss is outside the ifdef on purpose to keep APIs simple. If
  138. * NO_GSSAPI is not defined, then it's just an opaque structure
  139. * tag and the pointer will be NULL. */
  140. struct ssh_connection_shared_gss_state *shgss;
  141. #ifndef NO_GSSAPI
  142. int gss_status;
  143. time_t gss_cred_expiry; /* Re-delegate if newer */
  144. unsigned long gss_ctxt_lifetime; /* Re-delegate when short */
  145. #endif
  146. ssh_transient_hostkey_cache *thc;
  147. bool gss_kex_used;
  148. int nbits, pbits;
  149. bool warn_kex, warn_hk, warn_cscipher, warn_sccipher;
  150. mp_int *p, *g, *e, *f, *K;
  151. strbuf *outgoing_kexinit, *incoming_kexinit;
  152. strbuf *client_kexinit, *server_kexinit; /* aliases to the above */
  153. int kex_init_value, kex_reply_value;
  154. transport_direction in, out;
  155. ptrlen hostkeydata, sigdata;
  156. strbuf *hostkeyblob;
  157. char *keystr, *fingerprint;
  158. ssh_key *hkey; /* actual host key */
  159. RSAKey *rsa_kex_key; /* for RSA kex */
  160. ecdh_key *ecdh_key; /* for ECDH kex */
  161. unsigned char exchange_hash[MAX_HASH_LEN];
  162. bool can_gssapi_keyex;
  163. bool need_gss_transient_hostkey;
  164. bool warned_about_no_gss_transient_hostkey;
  165. bool got_session_id;
  166. int dlgret;
  167. bool guessok;
  168. bool ignorepkt;
  169. struct kexinit_algorithm kexlists[NKEXLIST][MAXKEXLIST];
  170. #ifndef NO_GSSAPI
  171. Ssh_gss_buf gss_buf;
  172. Ssh_gss_buf gss_rcvtok, gss_sndtok;
  173. Ssh_gss_stat gss_stat;
  174. Ssh_gss_buf mic;
  175. bool init_token_sent;
  176. bool complete_rcvd;
  177. bool gss_delegate;
  178. #endif
  179. /* List of crypto primitives below the warning threshold that the
  180. * user has already clicked OK to, so that we don't keep asking
  181. * about them again during rekeys. This directly stores pointers
  182. * to the algorithm vtables, compared by pointer value (which is
  183. * not a determinism hazard, because we're only using it as a
  184. * set). */
  185. tree234 *weak_algorithms_consented_to;
  186. /*
  187. * List of host key algorithms for which we _don't_ have a stored
  188. * host key. These are indices into the main hostkey_algs[] array
  189. */
  190. int uncert_hostkeys[N_HOSTKEY_ALGORITHMS];
  191. int n_uncert_hostkeys;
  192. /*
  193. * Indicate that the current rekey is intended to finish with a
  194. * newly cross-certified host key. To double-check that we
  195. * certified the right one, we set this to point to the host key
  196. * algorithm we expect it to be.
  197. */
  198. const ssh_keyalg *cross_certifying;
  199. ssh_key *const *hostkeys;
  200. int nhostkeys;
  201. PacketProtocolLayer ppl;
  202. };
  203. /* Helpers shared between transport and kex */
  204. PktIn *ssh2_transport_pop(struct ssh2_transport_state *s);
  205. void ssh2_transport_dialog_callback(void *, int);
  206. /* Provided by transport for use in kex */
  207. void ssh2transport_finalise_exhash(struct ssh2_transport_state *s);
  208. /* Provided by kex for use in transport. Must set the 'aborted' flag
  209. * if it throws a connection-terminating error, so that the caller
  210. * won't have to check that by looking inside its state parameter
  211. * which might already have been freed. */
  212. void ssh2kex_coroutine(struct ssh2_transport_state *s, bool *aborted);
  213. #endif /* PUTTY_SSH2TRANSPORT_H */