transport2.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "gssc.h"
  8. #include "gss.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. struct kexinit_algorithm {
  18. ptrlen name;
  19. union {
  20. struct {
  21. const ssh_kex *kex;
  22. bool warn;
  23. } kex;
  24. struct {
  25. const ssh_keyalg *hostkey;
  26. unsigned hkflags;
  27. bool warn;
  28. } hk;
  29. struct {
  30. const ssh_cipheralg *cipher;
  31. bool warn;
  32. } cipher;
  33. struct {
  34. const ssh2_macalg *mac;
  35. bool etm;
  36. } mac;
  37. struct {
  38. const ssh_compression_alg *comp;
  39. bool delayed;
  40. } comp;
  41. } u;
  42. };
  43. struct kexinit_algorithm_list {
  44. struct kexinit_algorithm *algs;
  45. size_t nalgs, algsize;
  46. };
  47. #define HOSTKEY_ALGORITHMS(X) \
  48. X(HK_ED25519, ssh_ecdsa_ed25519) \
  49. X(HK_ED448, ssh_ecdsa_ed448) \
  50. X(HK_ECDSA, ssh_ecdsa_nistp256) \
  51. X(HK_ECDSA, ssh_ecdsa_nistp384) \
  52. X(HK_ECDSA, ssh_ecdsa_nistp521) \
  53. X(HK_DSA, ssh_dsa) \
  54. X(HK_RSA, ssh_rsa_sha512) \
  55. X(HK_RSA, ssh_rsa_sha256) \
  56. X(HK_RSA, ssh_rsa) \
  57. /* WINSCP */ \
  58. X(HK_ED25519, opensshcert_ssh_ecdsa_ed25519) \
  59. /* OpenSSH defines no certified version of Ed448 */ \
  60. X(HK_ECDSA, opensshcert_ssh_ecdsa_nistp256) \
  61. X(HK_ECDSA, opensshcert_ssh_ecdsa_nistp384) \
  62. X(HK_ECDSA, opensshcert_ssh_ecdsa_nistp521) \
  63. X(HK_DSA, opensshcert_ssh_dsa) \
  64. X(HK_RSA, opensshcert_ssh_rsa_sha512) \
  65. X(HK_RSA, opensshcert_ssh_rsa_sha256) \
  66. X(HK_RSA, opensshcert_ssh_rsa) \
  67. /* end of list */
  68. #define COUNT_HOSTKEY_ALGORITHM(type, alg) +1
  69. #define N_HOSTKEY_ALGORITHMS (0 HOSTKEY_ALGORITHMS(COUNT_HOSTKEY_ALGORITHM))
  70. struct ssh_signkey_with_user_pref_id {
  71. const ssh_keyalg *alg;
  72. int id;
  73. };
  74. extern const struct ssh_signkey_with_user_pref_id
  75. ssh2_hostkey_algs[N_HOSTKEY_ALGORITHMS];
  76. /*
  77. * Enumeration of high-level classes of reason why we might need to do
  78. * a repeat key exchange. A full detailed reason in human-readable
  79. * string form for the Event Log is also provided, but this enum type
  80. * is used to discriminate between classes of reason that the code
  81. * needs to treat differently.
  82. *
  83. * RK_NONE == 0 is the value indicating that no rekey is currently
  84. * needed at all. RK_INITIAL indicates that we haven't even done the
  85. * _first_ key exchange yet. RK_SERVER indicates that we're rekeying
  86. * because the server asked for it, not because we decided it
  87. * ourselves. RK_NORMAL is the usual case. RK_GSS_UPDATE indicates
  88. * that we're rekeying because we've just got new GSSAPI credentials
  89. * (hence there's no point in doing a preliminary check for new GSS
  90. * creds, because we already know the answer); RK_POST_USERAUTH
  91. * indicates that _if_ we're going to need a post-userauth immediate
  92. * rekey for any reason, this is the moment to do it.
  93. *
  94. * So RK_POST_USERAUTH only tells the transport layer to _consider_
  95. * rekeying, not to definitely do it. Also, that one enum value is
  96. * special in that the user-readable reason text is passed in to the
  97. * transport layer as NULL, whereas fills in the reason text after it
  98. * decides whether it needs a rekey at all. In the other cases,
  99. * rekey_reason is passed in to the at the same time as rekey_class.
  100. */
  101. typedef enum RekeyClass {
  102. RK_NONE = 0,
  103. RK_INITIAL,
  104. RK_SERVER,
  105. RK_NORMAL,
  106. RK_POST_USERAUTH,
  107. RK_GSS_UPDATE
  108. } RekeyClass;
  109. typedef struct transport_direction {
  110. const ssh_cipheralg *cipher;
  111. const ssh2_macalg *mac;
  112. bool etm_mode;
  113. const ssh_compression_alg *comp;
  114. bool comp_delayed;
  115. int mkkey_adjust;
  116. } transport_direction;
  117. struct ssh2_transport_state {
  118. int crState, crStateKex;
  119. PacketProtocolLayer *higher_layer;
  120. PktInQueue pq_in_higher;
  121. PktOutQueue pq_out_higher;
  122. IdempotentCallback ic_pq_out_higher;
  123. Conf *conf;
  124. char *savedhost;
  125. int savedport;
  126. const char *rekey_reason;
  127. enum RekeyClass rekey_class;
  128. unsigned long max_data_size;
  129. const ssh_kex *kex_alg;
  130. const ssh_keyalg *hostkey_alg;
  131. unsigned char session_id[MAX_HASH_LEN];
  132. int session_id_len;
  133. int dh_min_size, dh_max_size;
  134. bool dh_got_size_bounds;
  135. dh_ctx *dh_ctx;
  136. ssh_hash *exhash;
  137. struct DataTransferStats *stats;
  138. const SshServerConfig *ssc;
  139. char *client_greeting, *server_greeting;
  140. bool kex_in_progress, kexinit_delayed;
  141. unsigned long next_rekey, last_rekey;
  142. const char *deferred_rekey_reason;
  143. bool higher_layer_ok;
  144. /*
  145. * Fully qualified host name, which we need if doing GSSAPI.
  146. */
  147. char *fullhostname;
  148. /* shgss is outside the ifdef on purpose to keep APIs simple. If
  149. * NO_GSSAPI is not defined, then it's just an opaque structure
  150. * tag and the pointer will be NULL. */
  151. struct ssh_connection_shared_gss_state *shgss;
  152. #ifndef NO_GSSAPI
  153. int gss_status;
  154. time_t gss_cred_expiry; /* Re-delegate if newer */
  155. unsigned long gss_ctxt_lifetime; /* Re-delegate when short */
  156. #endif
  157. ssh_transient_hostkey_cache *thc;
  158. bool gss_kex_used;
  159. tree234 *host_cas;
  160. int nbits, pbits;
  161. bool warn_kex, warn_hk, warn_cscipher, warn_sccipher;
  162. struct {
  163. const char *csvuln, *scvuln;
  164. WeakCryptoReason wcr;
  165. } terrapin;
  166. mp_int *p, *g, *e, *f;
  167. strbuf *ebuf, *fbuf;
  168. strbuf *kex_shared_secret;
  169. strbuf *outgoing_kexinit, *incoming_kexinit;
  170. strbuf *client_kexinit, *server_kexinit; /* aliases to the above */
  171. int kex_init_value, kex_reply_value;
  172. transport_direction in, out, *cstrans, *sctrans;
  173. ptrlen hostkeydata, sigdata;
  174. strbuf *hostkeyblob; /* used in server to construct host key to
  175. * send to client; in client to check in rekeys */
  176. char *keystr;
  177. ssh_key *hkey; /* actual host key */
  178. unsigned hkflags; /* signing flags, used in server */
  179. RSAKey *rsa_kex_key; /* for RSA kex */
  180. bool rsa_kex_key_needs_freeing;
  181. ecdh_key *ecdh_key; /* for ECDH kex */
  182. unsigned char exchange_hash[MAX_HASH_LEN];
  183. bool can_gssapi_keyex;
  184. bool need_gss_transient_hostkey;
  185. bool warned_about_no_gss_transient_hostkey;
  186. bool got_session_id;
  187. bool can_send_ext_info, post_newkeys_ext_info;
  188. bool strict_kex, enabled_outgoing_crypto, enabled_incoming_crypto;
  189. bool seen_non_kexinit;
  190. SeatPromptResult spr;
  191. bool guessok;
  192. bool ignorepkt;
  193. struct kexinit_algorithm_list kexlists[NKEXLIST];
  194. #ifndef NO_GSSAPI
  195. Ssh_gss_buf gss_buf;
  196. Ssh_gss_buf gss_rcvtok, gss_sndtok;
  197. Ssh_gss_stat gss_stat;
  198. Ssh_gss_buf mic;
  199. bool init_token_sent;
  200. bool complete_rcvd;
  201. bool gss_delegate;
  202. #endif
  203. /* List of crypto primitives below the warning threshold that the
  204. * user has already clicked OK to, so that we don't keep asking
  205. * about them again during rekeys. This directly stores pointers
  206. * to the algorithm vtables, compared by pointer value (which is
  207. * not a determinism hazard, because we're only using it as a
  208. * set). */
  209. tree234 *weak_algorithms_consented_to;
  210. /*
  211. * List of host key algorithms for which we _don't_ have a stored
  212. * host key. These are indices into the main hostkey_algs[] array
  213. */
  214. int uncert_hostkeys[N_HOSTKEY_ALGORITHMS];
  215. int n_uncert_hostkeys;
  216. /*
  217. * Indicate that the current rekey is intended to finish with a
  218. * newly cross-certified host key. To double-check that we
  219. * certified the right one, we set this to point to the host key
  220. * algorithm we expect it to be.
  221. */
  222. const ssh_keyalg *cross_certifying;
  223. ssh_key *const *hostkeys;
  224. int nhostkeys;
  225. PacketProtocolLayer ppl;
  226. };
  227. /* Helpers shared between transport and kex */
  228. PktIn *ssh2_transport_pop(struct ssh2_transport_state *s);
  229. void ssh2_transport_dialog_callback(void *, SeatPromptResult);
  230. /* Provided by transport for use in kex */
  231. void ssh2transport_finalise_exhash(struct ssh2_transport_state *s);
  232. /* Provided by kex for use in transport. Must set the 'aborted' flag
  233. * if it throws a connection-terminating error, so that the caller
  234. * won't have to check that by looking inside its state parameter
  235. * which might already have been freed. */
  236. void ssh2kex_coroutine(struct ssh2_transport_state *s, bool *aborted);
  237. #endif /* PUTTY_SSH2TRANSPORT_H */