kexdh.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* $OpenBSD: kexdh.c,v 1.33 2020/05/08 05:13:14 djm Exp $ */
  2. /*
  3. * Copyright (c) 2019 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. #include "includes.h"
  26. #ifdef WITH_OPENSSL
  27. #include <sys/types.h>
  28. #include <signal.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include "openbsd-compat/openssl-compat.h"
  32. #include <openssl/dh.h>
  33. #include "sshkey.h"
  34. #include "kex.h"
  35. #include "sshbuf.h"
  36. #include "digest.h"
  37. #include "ssherr.h"
  38. #include "dh.h"
  39. #include "log.h"
  40. int
  41. kex_dh_keygen(struct kex *kex)
  42. {
  43. switch (kex->kex_type) {
  44. case KEX_DH_GRP1_SHA1:
  45. kex->dh = dh_new_group1();
  46. break;
  47. case KEX_DH_GRP14_SHA1:
  48. case KEX_DH_GRP14_SHA256:
  49. kex->dh = dh_new_group14();
  50. break;
  51. case KEX_DH_GRP16_SHA512:
  52. kex->dh = dh_new_group16();
  53. break;
  54. case KEX_DH_GRP18_SHA512:
  55. kex->dh = dh_new_group18();
  56. break;
  57. default:
  58. return SSH_ERR_INVALID_ARGUMENT;
  59. }
  60. if (kex->dh == NULL)
  61. return SSH_ERR_ALLOC_FAIL;
  62. return (dh_gen_key(kex->dh, kex->we_need * 8));
  63. }
  64. int
  65. kex_dh_compute_key(struct kex *kex, BIGNUM *dh_pub, struct sshbuf *out)
  66. {
  67. BIGNUM *shared_secret = NULL;
  68. u_char *kbuf = NULL;
  69. size_t klen = 0;
  70. int kout, r;
  71. #ifdef DEBUG_KEXDH
  72. fprintf(stderr, "dh_pub= ");
  73. BN_print_fp(stderr, dh_pub);
  74. fprintf(stderr, "\n");
  75. debug("bits %d", BN_num_bits(dh_pub));
  76. DHparams_print_fp(stderr, kex->dh);
  77. fprintf(stderr, "\n");
  78. #endif
  79. if (!dh_pub_is_valid(kex->dh, dh_pub)) {
  80. r = SSH_ERR_MESSAGE_INCOMPLETE;
  81. goto out;
  82. }
  83. klen = DH_size(kex->dh);
  84. if ((kbuf = malloc(klen)) == NULL ||
  85. (shared_secret = BN_new()) == NULL) {
  86. r = SSH_ERR_ALLOC_FAIL;
  87. goto out;
  88. }
  89. if ((kout = DH_compute_key(kbuf, dh_pub, kex->dh)) < 0 ||
  90. BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
  91. debug("kout = DH_compute_key(kbuf, dh_pub, kex->dh)) < 0 || BN_bin2bn(kbuf, kout, shared_secret) == NULL)");
  92. r = SSH_ERR_LIBCRYPTO_ERROR;
  93. goto out;
  94. }
  95. #ifdef DEBUG_KEXDH
  96. dump_digest("shared secret", kbuf, kout);
  97. #endif
  98. r = sshbuf_put_bignum2(out, shared_secret);
  99. out:
  100. freezero(kbuf, klen);
  101. BN_clear_free(shared_secret);
  102. return r;
  103. }
  104. int
  105. kex_dh_keypair(struct kex *kex)
  106. {
  107. const BIGNUM *pub_key;
  108. struct sshbuf *buf = NULL;
  109. int r;
  110. if ((r = kex_dh_keygen(kex)) != 0)
  111. return r;
  112. DH_get0_key(kex->dh, &pub_key, NULL);
  113. if ((buf = sshbuf_new()) == NULL)
  114. return SSH_ERR_ALLOC_FAIL;
  115. if ((r = sshbuf_put_bignum2(buf, pub_key)) != 0 ||
  116. (r = sshbuf_get_u32(buf, NULL)) != 0)
  117. goto out;
  118. #ifdef DEBUG_KEXDH
  119. DHparams_print_fp(stderr, kex->dh);
  120. fprintf(stderr, "pub= ");
  121. BN_print_fp(stderr, pub_key);
  122. fprintf(stderr, "\n");
  123. #endif
  124. kex->client_pub = buf;
  125. buf = NULL;
  126. out:
  127. sshbuf_free(buf);
  128. return r;
  129. }
  130. int
  131. kex_dh_enc(struct kex *kex, const struct sshbuf *client_blob,
  132. struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
  133. {
  134. const BIGNUM *pub_key;
  135. struct sshbuf *server_blob = NULL;
  136. int r;
  137. *server_blobp = NULL;
  138. *shared_secretp = NULL;
  139. if ((r = kex_dh_keygen(kex)) != 0)
  140. goto out;
  141. DH_get0_key(kex->dh, &pub_key, NULL);
  142. if ((server_blob = sshbuf_new()) == NULL) {
  143. r = SSH_ERR_ALLOC_FAIL;
  144. goto out;
  145. }
  146. if ((r = sshbuf_put_bignum2(server_blob, pub_key)) != 0 ||
  147. (r = sshbuf_get_u32(server_blob, NULL)) != 0)
  148. goto out;
  149. if ((r = kex_dh_dec(kex, client_blob, shared_secretp)) != 0)
  150. goto out;
  151. *server_blobp = server_blob;
  152. server_blob = NULL;
  153. out:
  154. DH_free(kex->dh);
  155. kex->dh = NULL;
  156. sshbuf_free(server_blob);
  157. return r;
  158. }
  159. int
  160. kex_dh_dec(struct kex *kex, const struct sshbuf *dh_blob,
  161. struct sshbuf **shared_secretp)
  162. {
  163. struct sshbuf *buf = NULL;
  164. BIGNUM *dh_pub = NULL;
  165. int r;
  166. *shared_secretp = NULL;
  167. if ((buf = sshbuf_new()) == NULL) {
  168. r = SSH_ERR_ALLOC_FAIL;
  169. goto out;
  170. }
  171. if ((r = sshbuf_put_stringb(buf, dh_blob)) != 0 ||
  172. (r = sshbuf_get_bignum2(buf, &dh_pub)) != 0)
  173. goto out;
  174. sshbuf_reset(buf);
  175. if ((r = kex_dh_compute_key(kex, dh_pub, buf)) != 0)
  176. goto out;
  177. *shared_secretp = buf;
  178. buf = NULL;
  179. out:
  180. BN_free(dh_pub);
  181. DH_free(kex->dh);
  182. kex->dh = NULL;
  183. sshbuf_free(buf);
  184. return r;
  185. }
  186. #endif /* WITH_OPENSSL */