ssh2transport.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #define MAXKEXLIST 16
  18. struct kexinit_algorithm {
  19. const char *name;
  20. union {
  21. struct {
  22. const ssh_kex *kex;
  23. bool warn;
  24. } kex;
  25. struct {
  26. const ssh_keyalg *hostkey;
  27. unsigned hkflags;
  28. bool warn;
  29. } hk;
  30. struct {
  31. const ssh_cipheralg *cipher;
  32. bool warn;
  33. } cipher;
  34. struct {
  35. const ssh2_macalg *mac;
  36. bool etm;
  37. } mac;
  38. struct {
  39. const ssh_compression_alg *comp;
  40. bool delayed;
  41. } comp;
  42. } u;
  43. };
  44. #define HOSTKEY_ALGORITHMS(X) \
  45. X(HK_ED25519, ssh_ecdsa_ed25519) \
  46. X(HK_ED448, ssh_ecdsa_ed448) \
  47. X(HK_ECDSA, ssh_ecdsa_nistp256) \
  48. X(HK_ECDSA, ssh_ecdsa_nistp384) \
  49. X(HK_ECDSA, ssh_ecdsa_nistp521) \
  50. /* Changed order to match WinSCP default preference list for SshHostKeyList() */ \
  51. X(HK_RSA, ssh_rsa_sha512) \
  52. X(HK_RSA, ssh_rsa_sha256) \
  53. X(HK_RSA, ssh_rsa) \
  54. X(HK_DSA, ssh_dss) \
  55. /* end of list */
  56. #define COUNT_HOSTKEY_ALGORITHM(type, alg) +1
  57. #define N_HOSTKEY_ALGORITHMS (0 HOSTKEY_ALGORITHMS(COUNT_HOSTKEY_ALGORITHM))
  58. struct ssh_signkey_with_user_pref_id {
  59. const ssh_keyalg *alg;
  60. int id;
  61. };
  62. extern const struct ssh_signkey_with_user_pref_id
  63. ssh2_hostkey_algs[N_HOSTKEY_ALGORITHMS];
  64. /*
  65. * Enumeration of high-level classes of reason why we might need to do
  66. * a repeat key exchange. A full detailed reason in human-readable
  67. * string form for the Event Log is also provided, but this enum type
  68. * is used to discriminate between classes of reason that the code
  69. * needs to treat differently.
  70. *
  71. * RK_NONE == 0 is the value indicating that no rekey is currently
  72. * needed at all. RK_INITIAL indicates that we haven't even done the
  73. * _first_ key exchange yet. RK_SERVER indicates that we're rekeying
  74. * because the server asked for it, not because we decided it
  75. * ourselves. RK_NORMAL is the usual case. RK_GSS_UPDATE indicates
  76. * that we're rekeying because we've just got new GSSAPI credentials
  77. * (hence there's no point in doing a preliminary check for new GSS
  78. * creds, because we already know the answer); RK_POST_USERAUTH
  79. * indicates that _if_ we're going to need a post-userauth immediate
  80. * rekey for any reason, this is the moment to do it.
  81. *
  82. * So RK_POST_USERAUTH only tells the transport layer to _consider_
  83. * rekeying, not to definitely do it. Also, that one enum value is
  84. * special in that the user-readable reason text is passed in to the
  85. * transport layer as NULL, whereas fills in the reason text after it
  86. * decides whether it needs a rekey at all. In the other cases,
  87. * rekey_reason is passed in to the at the same time as rekey_class.
  88. */
  89. typedef enum RekeyClass {
  90. RK_NONE = 0,
  91. RK_INITIAL,
  92. RK_SERVER,
  93. RK_NORMAL,
  94. RK_POST_USERAUTH,
  95. RK_GSS_UPDATE
  96. } RekeyClass;
  97. typedef struct transport_direction {
  98. const ssh_cipheralg *cipher;
  99. const ssh2_macalg *mac;
  100. bool etm_mode;
  101. const ssh_compression_alg *comp;
  102. bool comp_delayed;
  103. int mkkey_adjust;
  104. } transport_direction;
  105. struct ssh2_transport_state {
  106. int crState, crStateKex;
  107. PacketProtocolLayer *higher_layer;
  108. PktInQueue pq_in_higher;
  109. PktOutQueue pq_out_higher;
  110. IdempotentCallback ic_pq_out_higher;
  111. Conf *conf;
  112. char *savedhost;
  113. int savedport;
  114. const char *rekey_reason;
  115. enum RekeyClass rekey_class;
  116. unsigned long max_data_size;
  117. const ssh_kex *kex_alg;
  118. const ssh_keyalg *hostkey_alg;
  119. char *hostkey_str; /* string representation, for easy checking in rekeys */
  120. unsigned char session_id[MAX_HASH_LEN];
  121. int session_id_len;
  122. int dh_min_size, dh_max_size;
  123. bool dh_got_size_bounds;
  124. dh_ctx *dh_ctx;
  125. ssh_hash *exhash;
  126. struct DataTransferStats *stats;
  127. const SshServerConfig *ssc;
  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, *cstrans, *sctrans;
  155. ptrlen hostkeydata, sigdata;
  156. strbuf *hostkeyblob;
  157. char *keystr;
  158. ssh_key *hkey; /* actual host key */
  159. unsigned hkflags; /* signing flags, used in server */
  160. RSAKey *rsa_kex_key; /* for RSA kex */
  161. bool rsa_kex_key_needs_freeing;
  162. ecdh_key *ecdh_key; /* for ECDH kex */
  163. unsigned char exchange_hash[MAX_HASH_LEN];
  164. bool can_gssapi_keyex;
  165. bool need_gss_transient_hostkey;
  166. bool warned_about_no_gss_transient_hostkey;
  167. bool got_session_id;
  168. bool can_send_ext_info, post_newkeys_ext_info;
  169. int dlgret;
  170. bool guessok;
  171. bool ignorepkt;
  172. struct kexinit_algorithm kexlists[NKEXLIST][MAXKEXLIST];
  173. #ifndef NO_GSSAPI
  174. Ssh_gss_buf gss_buf;
  175. Ssh_gss_buf gss_rcvtok, gss_sndtok;
  176. Ssh_gss_stat gss_stat;
  177. Ssh_gss_buf mic;
  178. bool init_token_sent;
  179. bool complete_rcvd;
  180. bool gss_delegate;
  181. #endif
  182. /* List of crypto primitives below the warning threshold that the
  183. * user has already clicked OK to, so that we don't keep asking
  184. * about them again during rekeys. This directly stores pointers
  185. * to the algorithm vtables, compared by pointer value (which is
  186. * not a determinism hazard, because we're only using it as a
  187. * set). */
  188. tree234 *weak_algorithms_consented_to;
  189. /*
  190. * List of host key algorithms for which we _don't_ have a stored
  191. * host key. These are indices into the main hostkey_algs[] array
  192. */
  193. int uncert_hostkeys[N_HOSTKEY_ALGORITHMS];
  194. int n_uncert_hostkeys;
  195. /*
  196. * Indicate that the current rekey is intended to finish with a
  197. * newly cross-certified host key. To double-check that we
  198. * certified the right one, we set this to point to the host key
  199. * algorithm we expect it to be.
  200. */
  201. const ssh_keyalg *cross_certifying;
  202. ssh_key *const *hostkeys;
  203. int nhostkeys;
  204. PacketProtocolLayer ppl;
  205. };
  206. /* Helpers shared between transport and kex */
  207. PktIn *ssh2_transport_pop(struct ssh2_transport_state *s);
  208. void ssh2_transport_dialog_callback(void *, int);
  209. /* Provided by transport for use in kex */
  210. void ssh2transport_finalise_exhash(struct ssh2_transport_state *s);
  211. /* Provided by kex for use in transport. Must set the 'aborted' flag
  212. * if it throws a connection-terminating error, so that the caller
  213. * won't have to check that by looking inside its state parameter
  214. * which might already have been freed. */
  215. void ssh2kex_coroutine(struct ssh2_transport_state *s, bool *aborted);
  216. #endif /* PUTTY_SSH2TRANSPORT_H */