kex.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* $OpenBSD: kex.h,v 1.109 2019/09/06 05:23:55 djm Exp $ */
  2. /*
  3. * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef KEX_H
  26. #define KEX_H
  27. #include "mac.h"
  28. #include "crypto_api.h"
  29. #ifdef WITH_OPENSSL
  30. # include <openssl/bn.h>
  31. # include <openssl/dh.h>
  32. # include <openssl/ecdsa.h>
  33. # ifdef OPENSSL_HAS_ECC
  34. # include <openssl/ec.h>
  35. # else /* OPENSSL_HAS_ECC */
  36. # define EC_KEY void
  37. # define EC_GROUP void
  38. # define EC_POINT void
  39. # endif /* OPENSSL_HAS_ECC */
  40. #else /* WITH_OPENSSL */
  41. # define DH void
  42. # define BIGNUM void
  43. # define EC_KEY void
  44. # define EC_GROUP void
  45. # define EC_POINT void
  46. #endif /* WITH_OPENSSL */
  47. #define KEX_COOKIE_LEN 16
  48. #define KEX_DH1 "diffie-hellman-group1-sha1"
  49. #define KEX_DH14_SHA1 "diffie-hellman-group14-sha1"
  50. #define KEX_DH14_SHA256 "diffie-hellman-group14-sha256"
  51. #define KEX_DH16_SHA512 "diffie-hellman-group16-sha512"
  52. #define KEX_DH18_SHA512 "diffie-hellman-group18-sha512"
  53. #define KEX_DHGEX_SHA1 "diffie-hellman-group-exchange-sha1"
  54. #define KEX_DHGEX_SHA256 "diffie-hellman-group-exchange-sha256"
  55. #define KEX_ECDH_SHA2_NISTP256 "ecdh-sha2-nistp256"
  56. #define KEX_ECDH_SHA2_NISTP384 "ecdh-sha2-nistp384"
  57. #define KEX_ECDH_SHA2_NISTP521 "ecdh-sha2-nistp521"
  58. #define KEX_CURVE25519_SHA256 "curve25519-sha256"
  59. #define KEX_CURVE25519_SHA256_OLD "curve25519-sha256@libssh.org"
  60. #define KEX_SNTRUP761X25519_SHA512 "sntrup761x25519-sha512@openssh.com"
  61. #define COMP_NONE 0
  62. /* pre-auth compression (COMP_ZLIB) is only supported in the client */
  63. #define COMP_ZLIB 1
  64. #define COMP_DELAYED 2
  65. #define CURVE25519_SIZE 32
  66. enum kex_init_proposals {
  67. PROPOSAL_KEX_ALGS,
  68. PROPOSAL_SERVER_HOST_KEY_ALGS,
  69. PROPOSAL_ENC_ALGS_CTOS,
  70. PROPOSAL_ENC_ALGS_STOC,
  71. PROPOSAL_MAC_ALGS_CTOS,
  72. PROPOSAL_MAC_ALGS_STOC,
  73. PROPOSAL_COMP_ALGS_CTOS,
  74. PROPOSAL_COMP_ALGS_STOC,
  75. PROPOSAL_LANG_CTOS,
  76. PROPOSAL_LANG_STOC,
  77. PROPOSAL_MAX
  78. };
  79. enum kex_modes {
  80. MODE_IN,
  81. MODE_OUT,
  82. MODE_MAX
  83. };
  84. enum kex_exchange {
  85. KEX_DH_GRP1_SHA1,
  86. KEX_DH_GRP14_SHA1,
  87. KEX_DH_GRP14_SHA256,
  88. KEX_DH_GRP16_SHA512,
  89. KEX_DH_GRP18_SHA512,
  90. KEX_DH_GEX_SHA1,
  91. KEX_DH_GEX_SHA256,
  92. KEX_ECDH_SHA2,
  93. KEX_C25519_SHA256,
  94. KEX_KEM_SNTRUP761X25519_SHA512,
  95. KEX_MAX
  96. };
  97. #define KEX_INIT_SENT 0x0001
  98. #define KEX_INITIAL 0x0002
  99. struct sshenc {
  100. char *name;
  101. const struct sshcipher *cipher;
  102. int enabled;
  103. u_int key_len;
  104. u_int iv_len;
  105. u_int block_size;
  106. u_char *key;
  107. u_char *iv;
  108. };
  109. struct sshcomp {
  110. u_int type;
  111. int enabled;
  112. char *name;
  113. };
  114. struct newkeys {
  115. struct sshenc enc;
  116. struct sshmac mac;
  117. struct sshcomp comp;
  118. };
  119. struct ssh;
  120. struct kex {
  121. struct newkeys *newkeys[MODE_MAX];
  122. u_int we_need;
  123. u_int dh_need;
  124. int server;
  125. char *name;
  126. char *hostkey_alg;
  127. int hostkey_type;
  128. int hostkey_nid;
  129. u_int kex_type;
  130. char *server_sig_algs;
  131. int ext_info_c;
  132. struct sshbuf *my;
  133. struct sshbuf *peer;
  134. struct sshbuf *client_version;
  135. struct sshbuf *server_version;
  136. struct sshbuf *session_id;
  137. sig_atomic_t done;
  138. u_int flags;
  139. int hash_alg;
  140. int ec_nid;
  141. char *failed_choice;
  142. int (*verify_host_key)(struct sshkey *, struct ssh *);
  143. struct sshkey *(*load_host_public_key)(int, int, struct ssh *);
  144. struct sshkey *(*load_host_private_key)(int, int, struct ssh *);
  145. int (*host_key_index)(struct sshkey *, int, struct ssh *);
  146. int (*sign)(struct ssh *, struct sshkey *, struct sshkey *,
  147. u_char **, size_t *, const u_char *, size_t, const char *);
  148. int (*kex[KEX_MAX])(struct ssh *);
  149. /* kex specific state */
  150. DH *dh; /* DH */
  151. u_int min, max, nbits; /* GEX */
  152. EC_KEY *ec_client_key; /* ECDH */
  153. const EC_GROUP *ec_group; /* ECDH */
  154. u_char c25519_client_key[CURVE25519_SIZE]; /* 25519 + KEM */
  155. u_char c25519_client_pubkey[CURVE25519_SIZE]; /* 25519 */
  156. u_char sntrup761_client_key[crypto_kem_sntrup761_SECRETKEYBYTES]; /* KEM */
  157. struct sshbuf *client_pub;
  158. };
  159. int kex_names_valid(const char *);
  160. char *kex_alg_list(char);
  161. char *kex_names_cat(const char *, const char *);
  162. int kex_assemble_names(char **, const char *, const char *);
  163. int kex_exchange_identification(struct ssh *, int, const char *);
  164. struct kex *kex_new(void);
  165. int kex_ready(struct ssh *, char *[PROPOSAL_MAX]);
  166. int kex_setup(struct ssh *, char *[PROPOSAL_MAX]);
  167. void kex_free_newkeys(struct newkeys *);
  168. void kex_free(struct kex *);
  169. int kex_buf2prop(struct sshbuf *, int *, char ***);
  170. int kex_prop2buf(struct sshbuf *, char *proposal[PROPOSAL_MAX]);
  171. void kex_prop_free(char **);
  172. int kex_load_hostkey(struct ssh *, struct sshkey **, struct sshkey **);
  173. int kex_verify_host_key(struct ssh *, struct sshkey *);
  174. int kex_send_kexinit(struct ssh *);
  175. int kex_input_kexinit(int, u_int32_t, struct ssh *);
  176. int kex_input_ext_info(int, u_int32_t, struct ssh *);
  177. int kex_protocol_error(int, u_int32_t, struct ssh *);
  178. int kex_derive_keys(struct ssh *, u_char *, u_int, const struct sshbuf *);
  179. int kex_send_newkeys(struct ssh *);
  180. int kex_start_rekex(struct ssh *);
  181. int kexgex_client(struct ssh *);
  182. int kexgex_server(struct ssh *);
  183. int kex_gen_client(struct ssh *);
  184. int kex_gen_server(struct ssh *);
  185. void newkeys_destroy(struct newkeys *newkeys);
  186. int kex_dh_keypair(struct kex *);
  187. int kex_dh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
  188. struct sshbuf **);
  189. int kex_dh_dec(struct kex *, const struct sshbuf *, struct sshbuf **);
  190. int kex_ecdh_keypair(struct kex *);
  191. int kex_ecdh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
  192. struct sshbuf **);
  193. int kex_ecdh_dec(struct kex *, const struct sshbuf *, struct sshbuf **);
  194. int kex_c25519_keypair(struct kex *);
  195. int kex_c25519_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
  196. struct sshbuf **);
  197. int kex_c25519_dec(struct kex *, const struct sshbuf *, struct sshbuf **);
  198. int kex_kem_sntrup761x25519_keypair(struct kex *);
  199. int kex_kem_sntrup761x25519_enc(struct kex *, const struct sshbuf *,
  200. struct sshbuf **, struct sshbuf **);
  201. int kex_kem_sntrup761x25519_dec(struct kex *, const struct sshbuf *,
  202. struct sshbuf **);
  203. int kex_dh_keygen(struct kex *);
  204. int kex_dh_compute_key(struct kex *, BIGNUM *, struct sshbuf *);
  205. int kexgex_hash(int, const struct sshbuf *, const struct sshbuf *,
  206. const struct sshbuf *, const struct sshbuf *, const struct sshbuf *,
  207. int, int, int,
  208. const BIGNUM *, const BIGNUM *, const BIGNUM *,
  209. const BIGNUM *, const u_char *, size_t,
  210. u_char *, size_t *);
  211. void kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
  212. __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
  213. __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
  214. int kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
  215. const u_char pub[CURVE25519_SIZE], struct sshbuf *out)
  216. __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
  217. __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
  218. int kexc25519_shared_key_ext(const u_char key[CURVE25519_SIZE],
  219. const u_char pub[CURVE25519_SIZE], struct sshbuf *out, int)
  220. __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
  221. __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
  222. #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
  223. void dump_digest(const char *, const u_char *, int);
  224. #endif
  225. #if !defined(WITH_OPENSSL) || !defined(OPENSSL_HAS_ECC)
  226. # undef EC_KEY
  227. # undef EC_GROUP
  228. # undef EC_POINT
  229. #endif
  230. #endif