dh_lib.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 1995-2019 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_local.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. goto err;
  77. }
  78. return ret;
  79. err:
  80. DH_free(ret);
  81. return NULL;
  82. }
  83. void DH_free(DH *r)
  84. {
  85. int i;
  86. if (r == NULL)
  87. return;
  88. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  89. REF_PRINT_COUNT("DH", r);
  90. if (i > 0)
  91. return;
  92. REF_ASSERT_ISNT(i < 0);
  93. if (r->meth != NULL && r->meth->finish != NULL)
  94. r->meth->finish(r);
  95. #ifndef OPENSSL_NO_ENGINE
  96. ENGINE_finish(r->engine);
  97. #endif
  98. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
  99. CRYPTO_THREAD_lock_free(r->lock);
  100. BN_clear_free(r->p);
  101. BN_clear_free(r->g);
  102. BN_clear_free(r->q);
  103. BN_clear_free(r->j);
  104. OPENSSL_free(r->seed);
  105. BN_clear_free(r->counter);
  106. BN_clear_free(r->pub_key);
  107. BN_clear_free(r->priv_key);
  108. OPENSSL_free(r);
  109. }
  110. int DH_up_ref(DH *r)
  111. {
  112. int i;
  113. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  114. return 0;
  115. REF_PRINT_COUNT("DH", r);
  116. REF_ASSERT_ISNT(i < 2);
  117. return ((i > 1) ? 1 : 0);
  118. }
  119. int DH_set_ex_data(DH *d, int idx, void *arg)
  120. {
  121. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  122. }
  123. void *DH_get_ex_data(DH *d, int idx)
  124. {
  125. return CRYPTO_get_ex_data(&d->ex_data, idx);
  126. }
  127. int DH_bits(const DH *dh)
  128. {
  129. return BN_num_bits(dh->p);
  130. }
  131. int DH_size(const DH *dh)
  132. {
  133. return BN_num_bytes(dh->p);
  134. }
  135. int DH_security_bits(const DH *dh)
  136. {
  137. int N;
  138. if (dh->q)
  139. N = BN_num_bits(dh->q);
  140. else if (dh->length)
  141. N = dh->length;
  142. else
  143. N = -1;
  144. return BN_security_bits(BN_num_bits(dh->p), N);
  145. }
  146. void DH_get0_pqg(const DH *dh,
  147. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  148. {
  149. if (p != NULL)
  150. *p = dh->p;
  151. if (q != NULL)
  152. *q = dh->q;
  153. if (g != NULL)
  154. *g = dh->g;
  155. }
  156. int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  157. {
  158. /* If the fields p and g in d are NULL, the corresponding input
  159. * parameters MUST be non-NULL. q may remain NULL.
  160. */
  161. if ((dh->p == NULL && p == NULL)
  162. || (dh->g == NULL && g == NULL))
  163. return 0;
  164. if (p != NULL) {
  165. BN_free(dh->p);
  166. dh->p = p;
  167. }
  168. if (q != NULL) {
  169. BN_free(dh->q);
  170. dh->q = q;
  171. }
  172. if (g != NULL) {
  173. BN_free(dh->g);
  174. dh->g = g;
  175. }
  176. if (q != NULL) {
  177. dh->length = BN_num_bits(q);
  178. }
  179. return 1;
  180. }
  181. long DH_get_length(const DH *dh)
  182. {
  183. return dh->length;
  184. }
  185. int DH_set_length(DH *dh, long length)
  186. {
  187. dh->length = length;
  188. return 1;
  189. }
  190. void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  191. {
  192. if (pub_key != NULL)
  193. *pub_key = dh->pub_key;
  194. if (priv_key != NULL)
  195. *priv_key = dh->priv_key;
  196. }
  197. int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
  198. {
  199. if (pub_key != NULL) {
  200. BN_clear_free(dh->pub_key);
  201. dh->pub_key = pub_key;
  202. }
  203. if (priv_key != NULL) {
  204. BN_clear_free(dh->priv_key);
  205. dh->priv_key = priv_key;
  206. }
  207. return 1;
  208. }
  209. const BIGNUM *DH_get0_p(const DH *dh)
  210. {
  211. return dh->p;
  212. }
  213. const BIGNUM *DH_get0_q(const DH *dh)
  214. {
  215. return dh->q;
  216. }
  217. const BIGNUM *DH_get0_g(const DH *dh)
  218. {
  219. return dh->g;
  220. }
  221. const BIGNUM *DH_get0_priv_key(const DH *dh)
  222. {
  223. return dh->priv_key;
  224. }
  225. const BIGNUM *DH_get0_pub_key(const DH *dh)
  226. {
  227. return dh->pub_key;
  228. }
  229. void DH_clear_flags(DH *dh, int flags)
  230. {
  231. dh->flags &= ~flags;
  232. }
  233. int DH_test_flags(const DH *dh, int flags)
  234. {
  235. return dh->flags & flags;
  236. }
  237. void DH_set_flags(DH *dh, int flags)
  238. {
  239. dh->flags |= flags;
  240. }
  241. ENGINE *DH_get0_engine(DH *dh)
  242. {
  243. return dh->engine;
  244. }