dh_lib.c 6.8 KB

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