dh_lib.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. return NULL;
  71. ret->lock = CRYPTO_THREAD_lock_new();
  72. if (ret->lock == NULL) {
  73. ERR_raise(ERR_LIB_DH, ERR_R_CRYPTO_LIB);
  74. OPENSSL_free(ret);
  75. return NULL;
  76. }
  77. if (!CRYPTO_NEW_REF(&ret->references, 1)) {
  78. CRYPTO_THREAD_lock_free(ret->lock);
  79. OPENSSL_free(ret);
  80. return NULL;
  81. }
  82. ret->libctx = libctx;
  83. ret->meth = DH_get_default_method();
  84. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  85. ret->flags = ret->meth->flags; /* early default init */
  86. if (engine) {
  87. if (!ENGINE_init(engine)) {
  88. ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
  89. goto err;
  90. }
  91. ret->engine = engine;
  92. } else
  93. ret->engine = ENGINE_get_default_DH();
  94. if (ret->engine) {
  95. ret->meth = ENGINE_get_DH(ret->engine);
  96. if (ret->meth == NULL) {
  97. ERR_raise(ERR_LIB_DH, ERR_R_ENGINE_LIB);
  98. goto err;
  99. }
  100. }
  101. #endif
  102. ret->flags = ret->meth->flags;
  103. #ifndef FIPS_MODULE
  104. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data))
  105. goto err;
  106. #endif /* FIPS_MODULE */
  107. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  108. ERR_raise(ERR_LIB_DH, ERR_R_INIT_FAIL);
  109. goto err;
  110. }
  111. return ret;
  112. err:
  113. DH_free(ret);
  114. return NULL;
  115. }
  116. void DH_free(DH *r)
  117. {
  118. int i;
  119. if (r == NULL)
  120. return;
  121. CRYPTO_DOWN_REF(&r->references, &i);
  122. REF_PRINT_COUNT("DH", r);
  123. if (i > 0)
  124. return;
  125. REF_ASSERT_ISNT(i < 0);
  126. if (r->meth != NULL && r->meth->finish != NULL)
  127. r->meth->finish(r);
  128. #if !defined(FIPS_MODULE)
  129. # if !defined(OPENSSL_NO_ENGINE)
  130. ENGINE_finish(r->engine);
  131. # endif
  132. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
  133. #endif
  134. CRYPTO_THREAD_lock_free(r->lock);
  135. CRYPTO_FREE_REF(&r->references);
  136. ossl_ffc_params_cleanup(&r->params);
  137. BN_clear_free(r->pub_key);
  138. BN_clear_free(r->priv_key);
  139. OPENSSL_free(r);
  140. }
  141. int DH_up_ref(DH *r)
  142. {
  143. int i;
  144. if (CRYPTO_UP_REF(&r->references, &i) <= 0)
  145. return 0;
  146. REF_PRINT_COUNT("DH", r);
  147. REF_ASSERT_ISNT(i < 2);
  148. return ((i > 1) ? 1 : 0);
  149. }
  150. void ossl_dh_set0_libctx(DH *d, OSSL_LIB_CTX *libctx)
  151. {
  152. d->libctx = libctx;
  153. }
  154. #ifndef FIPS_MODULE
  155. int DH_set_ex_data(DH *d, int idx, void *arg)
  156. {
  157. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  158. }
  159. void *DH_get_ex_data(const DH *d, int idx)
  160. {
  161. return CRYPTO_get_ex_data(&d->ex_data, idx);
  162. }
  163. #endif
  164. int DH_bits(const DH *dh)
  165. {
  166. if (dh->params.p != NULL)
  167. return BN_num_bits(dh->params.p);
  168. return -1;
  169. }
  170. int DH_size(const DH *dh)
  171. {
  172. if (dh->params.p != NULL)
  173. return BN_num_bytes(dh->params.p);
  174. return -1;
  175. }
  176. int DH_security_bits(const DH *dh)
  177. {
  178. int N;
  179. if (dh->params.q != NULL)
  180. N = BN_num_bits(dh->params.q);
  181. else if (dh->length)
  182. N = dh->length;
  183. else
  184. N = -1;
  185. if (dh->params.p != NULL)
  186. return BN_security_bits(BN_num_bits(dh->params.p), N);
  187. return -1;
  188. }
  189. void DH_get0_pqg(const DH *dh,
  190. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  191. {
  192. ossl_ffc_params_get0_pqg(&dh->params, p, q, g);
  193. }
  194. int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  195. {
  196. /*
  197. * If the fields p and g in dh are NULL, the corresponding input
  198. * parameters MUST be non-NULL. q may remain NULL.
  199. */
  200. if ((dh->params.p == NULL && p == NULL)
  201. || (dh->params.g == NULL && g == NULL))
  202. return 0;
  203. ossl_ffc_params_set0_pqg(&dh->params, p, q, g);
  204. ossl_dh_cache_named_group(dh);
  205. dh->dirty_cnt++;
  206. return 1;
  207. }
  208. long DH_get_length(const DH *dh)
  209. {
  210. return dh->length;
  211. }
  212. int DH_set_length(DH *dh, long length)
  213. {
  214. dh->length = length;
  215. dh->dirty_cnt++;
  216. return 1;
  217. }
  218. void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  219. {
  220. if (pub_key != NULL)
  221. *pub_key = dh->pub_key;
  222. if (priv_key != NULL)
  223. *priv_key = dh->priv_key;
  224. }
  225. int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
  226. {
  227. if (pub_key != NULL) {
  228. BN_clear_free(dh->pub_key);
  229. dh->pub_key = pub_key;
  230. }
  231. if (priv_key != NULL) {
  232. BN_clear_free(dh->priv_key);
  233. dh->priv_key = priv_key;
  234. }
  235. dh->dirty_cnt++;
  236. return 1;
  237. }
  238. const BIGNUM *DH_get0_p(const DH *dh)
  239. {
  240. return dh->params.p;
  241. }
  242. const BIGNUM *DH_get0_q(const DH *dh)
  243. {
  244. return dh->params.q;
  245. }
  246. const BIGNUM *DH_get0_g(const DH *dh)
  247. {
  248. return dh->params.g;
  249. }
  250. const BIGNUM *DH_get0_priv_key(const DH *dh)
  251. {
  252. return dh->priv_key;
  253. }
  254. const BIGNUM *DH_get0_pub_key(const DH *dh)
  255. {
  256. return dh->pub_key;
  257. }
  258. void DH_clear_flags(DH *dh, int flags)
  259. {
  260. dh->flags &= ~flags;
  261. }
  262. int DH_test_flags(const DH *dh, int flags)
  263. {
  264. return dh->flags & flags;
  265. }
  266. void DH_set_flags(DH *dh, int flags)
  267. {
  268. dh->flags |= flags;
  269. }
  270. #ifndef FIPS_MODULE
  271. ENGINE *DH_get0_engine(DH *dh)
  272. {
  273. return dh->engine;
  274. }
  275. #endif /*FIPS_MODULE */
  276. FFC_PARAMS *ossl_dh_get0_params(DH *dh)
  277. {
  278. return &dh->params;
  279. }
  280. int ossl_dh_get0_nid(const DH *dh)
  281. {
  282. return dh->params.nid;
  283. }