dh_lib.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "internal/refcount.h"
  12. #include <openssl/bn.h>
  13. #include "dh_locl.h"
  14. #include <openssl/engine.h>
  15. int DH_set_method(DH *dh, const DH_METHOD *meth)
  16. {
  17. /*
  18. * NB: The caller is specifically setting a method, so it's not up to us
  19. * to deal with which ENGINE it comes from.
  20. */
  21. const DH_METHOD *mtmp;
  22. mtmp = dh->meth;
  23. if (mtmp->finish)
  24. mtmp->finish(dh);
  25. #ifndef OPENSSL_NO_ENGINE
  26. ENGINE_finish(dh->engine);
  27. dh->engine = NULL;
  28. #endif
  29. dh->meth = meth;
  30. if (meth->init)
  31. meth->init(dh);
  32. return 1;
  33. }
  34. DH *DH_new(void)
  35. {
  36. return DH_new_method(NULL);
  37. }
  38. DH *DH_new_method(ENGINE *engine)
  39. {
  40. DH *ret = OPENSSL_zalloc(sizeof(*ret));
  41. if (ret == NULL) {
  42. DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  43. return NULL;
  44. }
  45. ret->references = 1;
  46. ret->lock = CRYPTO_THREAD_lock_new();
  47. if (ret->lock == NULL) {
  48. DHerr(DH_F_DH_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  49. OPENSSL_free(ret);
  50. return NULL;
  51. }
  52. ret->meth = DH_get_default_method();
  53. #ifndef OPENSSL_NO_ENGINE
  54. ret->flags = ret->meth->flags; /* early default init */
  55. if (engine) {
  56. if (!ENGINE_init(engine)) {
  57. DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
  58. goto err;
  59. }
  60. ret->engine = engine;
  61. } else
  62. ret->engine = ENGINE_get_default_DH();
  63. if (ret->engine) {
  64. ret->meth = ENGINE_get_DH(ret->engine);
  65. if (ret->meth == NULL) {
  66. DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
  67. goto err;
  68. }
  69. }
  70. #endif
  71. ret->flags = ret->meth->flags;
  72. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
  73. goto err;
  74. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  75. DHerr(DH_F_DH_NEW_METHOD, ERR_R_INIT_FAIL);
  76. err:
  77. DH_free(ret);
  78. ret = NULL;
  79. }
  80. return ret;
  81. }
  82. void DH_free(DH *r)
  83. {
  84. int i;
  85. if (r == NULL)
  86. return;
  87. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  88. REF_PRINT_COUNT("DH", r);
  89. if (i > 0)
  90. return;
  91. REF_ASSERT_ISNT(i < 0);
  92. if (r->meth->finish)
  93. r->meth->finish(r);
  94. #ifndef OPENSSL_NO_ENGINE
  95. ENGINE_finish(r->engine);
  96. #endif
  97. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
  98. CRYPTO_THREAD_lock_free(r->lock);
  99. BN_clear_free(r->p);
  100. BN_clear_free(r->g);
  101. BN_clear_free(r->q);
  102. BN_clear_free(r->j);
  103. OPENSSL_free(r->seed);
  104. BN_clear_free(r->counter);
  105. BN_clear_free(r->pub_key);
  106. BN_clear_free(r->priv_key);
  107. OPENSSL_free(r);
  108. }
  109. int DH_up_ref(DH *r)
  110. {
  111. int i;
  112. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  113. return 0;
  114. REF_PRINT_COUNT("DH", r);
  115. REF_ASSERT_ISNT(i < 2);
  116. return ((i > 1) ? 1 : 0);
  117. }
  118. int DH_set_ex_data(DH *d, int idx, void *arg)
  119. {
  120. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  121. }
  122. void *DH_get_ex_data(DH *d, int idx)
  123. {
  124. return CRYPTO_get_ex_data(&d->ex_data, idx);
  125. }
  126. int DH_bits(const DH *dh)
  127. {
  128. return BN_num_bits(dh->p);
  129. }
  130. int DH_size(const DH *dh)
  131. {
  132. return BN_num_bytes(dh->p);
  133. }
  134. int DH_security_bits(const DH *dh)
  135. {
  136. int N;
  137. if (dh->q)
  138. N = BN_num_bits(dh->q);
  139. else if (dh->length)
  140. N = dh->length;
  141. else
  142. N = -1;
  143. return BN_security_bits(BN_num_bits(dh->p), N);
  144. }
  145. void DH_get0_pqg(const DH *dh,
  146. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  147. {
  148. if (p != NULL)
  149. *p = dh->p;
  150. if (q != NULL)
  151. *q = dh->q;
  152. if (g != NULL)
  153. *g = dh->g;
  154. }
  155. int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  156. {
  157. /* If the fields p and g in d are NULL, the corresponding input
  158. * parameters MUST be non-NULL. q may remain NULL.
  159. */
  160. if ((dh->p == NULL && p == NULL)
  161. || (dh->g == NULL && g == NULL))
  162. return 0;
  163. if (p != NULL) {
  164. BN_free(dh->p);
  165. dh->p = p;
  166. }
  167. if (q != NULL) {
  168. BN_free(dh->q);
  169. dh->q = q;
  170. }
  171. if (g != NULL) {
  172. BN_free(dh->g);
  173. dh->g = g;
  174. }
  175. if (q != NULL) {
  176. dh->length = BN_num_bits(q);
  177. }
  178. return 1;
  179. }
  180. long DH_get_length(const DH *dh)
  181. {
  182. return dh->length;
  183. }
  184. int DH_set_length(DH *dh, long length)
  185. {
  186. dh->length = length;
  187. return 1;
  188. }
  189. void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  190. {
  191. if (pub_key != NULL)
  192. *pub_key = dh->pub_key;
  193. if (priv_key != NULL)
  194. *priv_key = dh->priv_key;
  195. }
  196. int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
  197. {
  198. if (pub_key != NULL) {
  199. BN_free(dh->pub_key);
  200. dh->pub_key = pub_key;
  201. }
  202. if (priv_key != NULL) {
  203. BN_free(dh->priv_key);
  204. dh->priv_key = priv_key;
  205. }
  206. return 1;
  207. }
  208. const BIGNUM *DH_get0_p(const DH *dh)
  209. {
  210. return dh->p;
  211. }
  212. const BIGNUM *DH_get0_q(const DH *dh)
  213. {
  214. return dh->q;
  215. }
  216. const BIGNUM *DH_get0_g(const DH *dh)
  217. {
  218. return dh->g;
  219. }
  220. const BIGNUM *DH_get0_priv_key(const DH *dh)
  221. {
  222. return dh->priv_key;
  223. }
  224. const BIGNUM *DH_get0_pub_key(const DH *dh)
  225. {
  226. return dh->pub_key;
  227. }
  228. void DH_clear_flags(DH *dh, int flags)
  229. {
  230. dh->flags &= ~flags;
  231. }
  232. int DH_test_flags(const DH *dh, int flags)
  233. {
  234. return dh->flags & flags;
  235. }
  236. void DH_set_flags(DH *dh, int flags)
  237. {
  238. dh->flags |= flags;
  239. }
  240. ENGINE *DH_get0_engine(DH *dh)
  241. {
  242. return dh->engine;
  243. }