dh_lib.c 6.6 KB

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