ssh2transport.h 7.9 KB

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