1
0

ssh2transport.h 8.0 KB

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