dh_lib.c 6.7 KB

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