1
0

dsa_lib.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/bn.h>
  15. #ifndef FIPS_MODULE
  16. # include <openssl/engine.h>
  17. #endif
  18. #include "internal/cryptlib.h"
  19. #include "internal/refcount.h"
  20. #include "crypto/dsa.h"
  21. #include "crypto/dh.h" /* required by DSA_dup_DH() */
  22. #include "dsa_local.h"
  23. static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
  24. #ifndef FIPS_MODULE
  25. int DSA_set_ex_data(DSA *d, int idx, void *arg)
  26. {
  27. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  28. }
  29. void *DSA_get_ex_data(const DSA *d, int idx)
  30. {
  31. return CRYPTO_get_ex_data(&d->ex_data, idx);
  32. }
  33. # ifndef OPENSSL_NO_DH
  34. DH *DSA_dup_DH(const DSA *r)
  35. {
  36. /*
  37. * DSA has p, q, g, optional pub_key, optional priv_key.
  38. * DH has p, optional length, g, optional pub_key,
  39. * optional priv_key, optional q.
  40. */
  41. DH *ret = NULL;
  42. BIGNUM *pub_key = NULL, *priv_key = NULL;
  43. if (r == NULL)
  44. goto err;
  45. ret = DH_new();
  46. if (ret == NULL)
  47. goto err;
  48. if (!ossl_ffc_params_copy(ossl_dh_get0_params(ret), &r->params))
  49. goto err;
  50. if (r->pub_key != NULL) {
  51. pub_key = BN_dup(r->pub_key);
  52. if (pub_key == NULL)
  53. goto err;
  54. if (r->priv_key != NULL) {
  55. priv_key = BN_dup(r->priv_key);
  56. if (priv_key == NULL)
  57. goto err;
  58. }
  59. if (!DH_set0_key(ret, pub_key, priv_key))
  60. goto err;
  61. } else if (r->priv_key != NULL) {
  62. /* Shouldn't happen */
  63. goto err;
  64. }
  65. return ret;
  66. err:
  67. BN_free(pub_key);
  68. BN_free(priv_key);
  69. DH_free(ret);
  70. return NULL;
  71. }
  72. # endif /* OPENSSL_NO_DH */
  73. void DSA_clear_flags(DSA *d, int flags)
  74. {
  75. d->flags &= ~flags;
  76. }
  77. int DSA_test_flags(const DSA *d, int flags)
  78. {
  79. return d->flags & flags;
  80. }
  81. void DSA_set_flags(DSA *d, int flags)
  82. {
  83. d->flags |= flags;
  84. }
  85. ENGINE *DSA_get0_engine(DSA *d)
  86. {
  87. return d->engine;
  88. }
  89. int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
  90. {
  91. /*
  92. * NB: The caller is specifically setting a method, so it's not up to us
  93. * to deal with which ENGINE it comes from.
  94. */
  95. const DSA_METHOD *mtmp;
  96. mtmp = dsa->meth;
  97. if (mtmp->finish)
  98. mtmp->finish(dsa);
  99. #ifndef OPENSSL_NO_ENGINE
  100. ENGINE_finish(dsa->engine);
  101. dsa->engine = NULL;
  102. #endif
  103. dsa->meth = meth;
  104. if (meth->init)
  105. meth->init(dsa);
  106. return 1;
  107. }
  108. #endif /* FIPS_MODULE */
  109. const DSA_METHOD *DSA_get_method(DSA *d)
  110. {
  111. return d->meth;
  112. }
  113. static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
  114. {
  115. DSA *ret = OPENSSL_zalloc(sizeof(*ret));
  116. if (ret == NULL) {
  117. ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
  118. return NULL;
  119. }
  120. ret->references = 1;
  121. ret->lock = CRYPTO_THREAD_lock_new();
  122. if (ret->lock == NULL) {
  123. ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
  124. OPENSSL_free(ret);
  125. return NULL;
  126. }
  127. ret->libctx = libctx;
  128. ret->meth = DSA_get_default_method();
  129. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  130. ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
  131. if (engine) {
  132. if (!ENGINE_init(engine)) {
  133. ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
  134. goto err;
  135. }
  136. ret->engine = engine;
  137. } else
  138. ret->engine = ENGINE_get_default_DSA();
  139. if (ret->engine) {
  140. ret->meth = ENGINE_get_DSA(ret->engine);
  141. if (ret->meth == NULL) {
  142. ERR_raise(ERR_LIB_DSA, ERR_R_ENGINE_LIB);
  143. goto err;
  144. }
  145. }
  146. #endif
  147. ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
  148. #ifndef FIPS_MODULE
  149. if (!ossl_crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret,
  150. &ret->ex_data))
  151. goto err;
  152. #endif
  153. ossl_ffc_params_init(&ret->params);
  154. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  155. ERR_raise(ERR_LIB_DSA, ERR_R_INIT_FAIL);
  156. goto err;
  157. }
  158. return ret;
  159. err:
  160. DSA_free(ret);
  161. return NULL;
  162. }
  163. DSA *DSA_new_method(ENGINE *engine)
  164. {
  165. return dsa_new_intern(engine, NULL);
  166. }
  167. DSA *ossl_dsa_new(OSSL_LIB_CTX *libctx)
  168. {
  169. return dsa_new_intern(NULL, libctx);
  170. }
  171. #ifndef FIPS_MODULE
  172. DSA *DSA_new(void)
  173. {
  174. return dsa_new_intern(NULL, NULL);
  175. }
  176. #endif
  177. void DSA_free(DSA *r)
  178. {
  179. int i;
  180. if (r == NULL)
  181. return;
  182. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  183. REF_PRINT_COUNT("DSA", r);
  184. if (i > 0)
  185. return;
  186. REF_ASSERT_ISNT(i < 0);
  187. if (r->meth != NULL && r->meth->finish != NULL)
  188. r->meth->finish(r);
  189. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  190. ENGINE_finish(r->engine);
  191. #endif
  192. #ifndef FIPS_MODULE
  193. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
  194. #endif
  195. CRYPTO_THREAD_lock_free(r->lock);
  196. ossl_ffc_params_cleanup(&r->params);
  197. BN_clear_free(r->pub_key);
  198. BN_clear_free(r->priv_key);
  199. OPENSSL_free(r);
  200. }
  201. int DSA_up_ref(DSA *r)
  202. {
  203. int i;
  204. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  205. return 0;
  206. REF_PRINT_COUNT("DSA", r);
  207. REF_ASSERT_ISNT(i < 2);
  208. return ((i > 1) ? 1 : 0);
  209. }
  210. void ossl_dsa_set0_libctx(DSA *d, OSSL_LIB_CTX *libctx)
  211. {
  212. d->libctx = libctx;
  213. }
  214. void DSA_get0_pqg(const DSA *d,
  215. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  216. {
  217. ossl_ffc_params_get0_pqg(&d->params, p, q, g);
  218. }
  219. int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  220. {
  221. /* If the fields p, q and g in d are NULL, the corresponding input
  222. * parameters MUST be non-NULL.
  223. */
  224. if ((d->params.p == NULL && p == NULL)
  225. || (d->params.q == NULL && q == NULL)
  226. || (d->params.g == NULL && g == NULL))
  227. return 0;
  228. ossl_ffc_params_set0_pqg(&d->params, p, q, g);
  229. d->dirty_cnt++;
  230. return 1;
  231. }
  232. const BIGNUM *DSA_get0_p(const DSA *d)
  233. {
  234. return d->params.p;
  235. }
  236. const BIGNUM *DSA_get0_q(const DSA *d)
  237. {
  238. return d->params.q;
  239. }
  240. const BIGNUM *DSA_get0_g(const DSA *d)
  241. {
  242. return d->params.g;
  243. }
  244. const BIGNUM *DSA_get0_pub_key(const DSA *d)
  245. {
  246. return d->pub_key;
  247. }
  248. const BIGNUM *DSA_get0_priv_key(const DSA *d)
  249. {
  250. return d->priv_key;
  251. }
  252. void DSA_get0_key(const DSA *d,
  253. const BIGNUM **pub_key, const BIGNUM **priv_key)
  254. {
  255. if (pub_key != NULL)
  256. *pub_key = d->pub_key;
  257. if (priv_key != NULL)
  258. *priv_key = d->priv_key;
  259. }
  260. int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
  261. {
  262. if (pub_key != NULL) {
  263. BN_free(d->pub_key);
  264. d->pub_key = pub_key;
  265. }
  266. if (priv_key != NULL) {
  267. BN_free(d->priv_key);
  268. d->priv_key = priv_key;
  269. }
  270. d->dirty_cnt++;
  271. return 1;
  272. }
  273. int DSA_security_bits(const DSA *d)
  274. {
  275. if (d->params.p != NULL && d->params.q != NULL)
  276. return BN_security_bits(BN_num_bits(d->params.p),
  277. BN_num_bits(d->params.q));
  278. return -1;
  279. }
  280. int DSA_bits(const DSA *dsa)
  281. {
  282. if (dsa->params.p != NULL)
  283. return BN_num_bits(dsa->params.p);
  284. return -1;
  285. }
  286. FFC_PARAMS *ossl_dsa_get0_params(DSA *dsa)
  287. {
  288. return &dsa->params;
  289. }
  290. int ossl_dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
  291. {
  292. int ret;
  293. FFC_PARAMS *ffc;
  294. if (dsa == NULL)
  295. return 0;
  296. ffc = ossl_dsa_get0_params(dsa);
  297. if (ffc == NULL)
  298. return 0;
  299. ret = ossl_ffc_params_fromdata(ffc, params);
  300. if (ret)
  301. dsa->dirty_cnt++;
  302. return ret;
  303. }