dh_lib.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
  146. {
  147. d->libctx = libctx;
  148. }
  149. #ifndef FIPS_MODULE
  150. int DH_set_ex_data(DH *d, int idx, void *arg)
  151. {
  152. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  153. }
  154. void *DH_get_ex_data(const DH *d, int idx)
  155. {
  156. return CRYPTO_get_ex_data(&d->ex_data, idx);
  157. }
  158. #endif
  159. int DH_bits(const DH *dh)
  160. {
  161. return BN_num_bits(dh->params.p);
  162. }
  163. int DH_size(const DH *dh)
  164. {
  165. return BN_num_bytes(dh->params.p);
  166. }
  167. int DH_security_bits(const DH *dh)
  168. {
  169. int N;
  170. if (dh->params.q != NULL)
  171. N = BN_num_bits(dh->params.q);
  172. else if (dh->length)
  173. N = dh->length;
  174. else
  175. N = -1;
  176. return BN_security_bits(BN_num_bits(dh->params.p), N);
  177. }
  178. void DH_get0_pqg(const DH *dh,
  179. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  180. {
  181. ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
  182. }
  183. int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  184. {
  185. /*
  186. * If the fields p and g in d are NULL, the corresponding input
  187. * parameters MUST be non-NULL. q may remain NULL.
  188. */
  189. if ((dh->params.p == NULL && p == NULL)
  190. || (dh->params.g == NULL && g == NULL))
  191. return 0;
  192. ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
  193. dh_cache_named_group(dh);
  194. dh->dirty_cnt++;
  195. return 1;
  196. }
  197. long DH_get_length(const DH *dh)
  198. {
  199. return dh->length;
  200. }
  201. int DH_set_length(DH *dh, long length)
  202. {
  203. dh->length = length;
  204. dh->dirty_cnt++;
  205. return 1;
  206. }
  207. void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  208. {
  209. if (pub_key != NULL)
  210. *pub_key = dh->pub_key;
  211. if (priv_key != NULL)
  212. *priv_key = dh->priv_key;
  213. }
  214. int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
  215. {
  216. if (pub_key != NULL) {
  217. BN_clear_free(dh->pub_key);
  218. dh->pub_key = pub_key;
  219. }
  220. if (priv_key != NULL) {
  221. BN_clear_free(dh->priv_key);
  222. dh->priv_key = priv_key;
  223. }
  224. dh->dirty_cnt++;
  225. return 1;
  226. }
  227. const BIGNUM *DH_get0_p(const DH *dh)
  228. {
  229. return dh->params.p;
  230. }
  231. const BIGNUM *DH_get0_q(const DH *dh)
  232. {
  233. return dh->params.q;
  234. }
  235. const BIGNUM *DH_get0_g(const DH *dh)
  236. {
  237. return dh->params.g;
  238. }
  239. const BIGNUM *DH_get0_priv_key(const DH *dh)
  240. {
  241. return dh->priv_key;
  242. }
  243. const BIGNUM *DH_get0_pub_key(const DH *dh)
  244. {
  245. return dh->pub_key;
  246. }
  247. void DH_clear_flags(DH *dh, int flags)
  248. {
  249. dh->flags &= ~flags;
  250. }
  251. int DH_test_flags(const DH *dh, int flags)
  252. {
  253. return dh->flags & flags;
  254. }
  255. void DH_set_flags(DH *dh, int flags)
  256. {
  257. dh->flags |= flags;
  258. }
  259. #ifndef FIPS_MODULE
  260. ENGINE *DH_get0_engine(DH *dh)
  261. {
  262. return dh->engine;
  263. }
  264. #endif /*FIPS_MODULE */
  265. FFC_PARAMS *dh_get0_params(DH *dh)
  266. {
  267. return &dh->params;
  268. }
  269. int dh_get0_nid(const DH *dh)
  270. {
  271. return dh->params.nid;
  272. }