dh_lib.c 6.4 KB

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