dsa_lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright 1995-2020 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 <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include "internal/refcount.h"
  17. #include <openssl/bn.h>
  18. #include <openssl/asn1.h>
  19. #include <openssl/engine.h>
  20. #include <openssl/core_names.h>
  21. #include "dsa_local.h"
  22. #include "crypto/evp.h"
  23. #include "crypto/dsa.h"
  24. #include "crypto/dh.h" /* required by DSA_dup_DH() */
  25. static DSA *dsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx);
  26. #ifndef FIPS_MODULE
  27. int DSA_set_ex_data(DSA *d, int idx, void *arg)
  28. {
  29. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  30. }
  31. void *DSA_get_ex_data(const DSA *d, int idx)
  32. {
  33. return CRYPTO_get_ex_data(&d->ex_data, idx);
  34. }
  35. # ifndef OPENSSL_NO_DH
  36. DH *DSA_dup_DH(const DSA *r)
  37. {
  38. /*
  39. * DSA has p, q, g, optional pub_key, optional priv_key.
  40. * DH has p, optional length, g, optional pub_key,
  41. * optional priv_key, optional q.
  42. */
  43. DH *ret = NULL;
  44. BIGNUM *pub_key = NULL, *priv_key = NULL;
  45. if (r == NULL)
  46. goto err;
  47. ret = DH_new();
  48. if (ret == NULL)
  49. goto err;
  50. if (!ffc_params_copy(dh_get0_params(ret), &r->params))
  51. goto err;
  52. if (r->pub_key != NULL) {
  53. pub_key = BN_dup(r->pub_key);
  54. if (pub_key == NULL)
  55. goto err;
  56. if (r->priv_key != NULL) {
  57. priv_key = BN_dup(r->priv_key);
  58. if (priv_key == NULL)
  59. goto err;
  60. }
  61. if (!DH_set0_key(ret, pub_key, priv_key))
  62. goto err;
  63. } else if (r->priv_key != NULL) {
  64. /* Shouldn't happen */
  65. goto err;
  66. }
  67. return ret;
  68. err:
  69. BN_free(pub_key);
  70. BN_free(priv_key);
  71. DH_free(ret);
  72. return NULL;
  73. }
  74. # endif /* OPENSSL_NO_DH */
  75. void DSA_clear_flags(DSA *d, int flags)
  76. {
  77. d->flags &= ~flags;
  78. }
  79. int DSA_test_flags(const DSA *d, int flags)
  80. {
  81. return d->flags & flags;
  82. }
  83. void DSA_set_flags(DSA *d, int flags)
  84. {
  85. d->flags |= flags;
  86. }
  87. ENGINE *DSA_get0_engine(DSA *d)
  88. {
  89. return d->engine;
  90. }
  91. int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
  92. {
  93. /*
  94. * NB: The caller is specifically setting a method, so it's not up to us
  95. * to deal with which ENGINE it comes from.
  96. */
  97. const DSA_METHOD *mtmp;
  98. mtmp = dsa->meth;
  99. if (mtmp->finish)
  100. mtmp->finish(dsa);
  101. #ifndef OPENSSL_NO_ENGINE
  102. ENGINE_finish(dsa->engine);
  103. dsa->engine = NULL;
  104. #endif
  105. dsa->meth = meth;
  106. if (meth->init)
  107. meth->init(dsa);
  108. return 1;
  109. }
  110. #endif /* FIPS_MODULE */
  111. const DSA_METHOD *DSA_get_method(DSA *d)
  112. {
  113. return d->meth;
  114. }
  115. static DSA *dsa_new_intern(ENGINE *engine, OPENSSL_CTX *libctx)
  116. {
  117. DSA *ret = OPENSSL_zalloc(sizeof(*ret));
  118. if (ret == NULL) {
  119. DSAerr(0, ERR_R_MALLOC_FAILURE);
  120. return NULL;
  121. }
  122. ret->references = 1;
  123. ret->lock = CRYPTO_THREAD_lock_new();
  124. if (ret->lock == NULL) {
  125. DSAerr(0, ERR_R_MALLOC_FAILURE);
  126. OPENSSL_free(ret);
  127. return NULL;
  128. }
  129. ret->libctx = libctx;
  130. ret->meth = DSA_get_default_method();
  131. #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
  132. ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
  133. if (engine) {
  134. if (!ENGINE_init(engine)) {
  135. DSAerr(0, ERR_R_ENGINE_LIB);
  136. goto err;
  137. }
  138. ret->engine = engine;
  139. } else
  140. ret->engine = ENGINE_get_default_DSA();
  141. if (ret->engine) {
  142. ret->meth = ENGINE_get_DSA(ret->engine);
  143. if (ret->meth == NULL) {
  144. DSAerr(0, ERR_R_ENGINE_LIB);
  145. goto err;
  146. }
  147. }
  148. #endif
  149. ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
  150. #ifndef FIPS_MODULE
  151. if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
  152. goto err;
  153. #endif
  154. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  155. DSAerr(0, 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 *dsa_new_with_ctx(OPENSSL_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. 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 DSA_get0_pqg(const DSA *d,
  211. const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  212. {
  213. ffc_params_get0_pqg(&d->params, p, q, g);
  214. }
  215. int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  216. {
  217. /* If the fields p, q and g in d are NULL, the corresponding input
  218. * parameters MUST be non-NULL.
  219. */
  220. if ((d->params.p == NULL && p == NULL)
  221. || (d->params.q == NULL && q == NULL)
  222. || (d->params.g == NULL && g == NULL))
  223. return 0;
  224. ffc_params_set0_pqg(&d->params, p, q, g);
  225. d->dirty_cnt++;
  226. return 1;
  227. }
  228. const BIGNUM *DSA_get0_p(const DSA *d)
  229. {
  230. return d->params.p;
  231. }
  232. const BIGNUM *DSA_get0_q(const DSA *d)
  233. {
  234. return d->params.q;
  235. }
  236. const BIGNUM *DSA_get0_g(const DSA *d)
  237. {
  238. return d->params.g;
  239. }
  240. const BIGNUM *DSA_get0_pub_key(const DSA *d)
  241. {
  242. return d->pub_key;
  243. }
  244. const BIGNUM *DSA_get0_priv_key(const DSA *d)
  245. {
  246. return d->priv_key;
  247. }
  248. void DSA_get0_key(const DSA *d,
  249. const BIGNUM **pub_key, const BIGNUM **priv_key)
  250. {
  251. if (pub_key != NULL)
  252. *pub_key = d->pub_key;
  253. if (priv_key != NULL)
  254. *priv_key = d->priv_key;
  255. }
  256. int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
  257. {
  258. /* If the field pub_key in d is NULL, the corresponding input
  259. * parameters MUST be non-NULL. The priv_key field may
  260. * be left NULL.
  261. */
  262. if (d->pub_key == NULL && pub_key == NULL)
  263. return 0;
  264. if (pub_key != NULL) {
  265. BN_free(d->pub_key);
  266. d->pub_key = pub_key;
  267. }
  268. if (priv_key != NULL) {
  269. BN_free(d->priv_key);
  270. d->priv_key = priv_key;
  271. }
  272. d->dirty_cnt++;
  273. return 1;
  274. }
  275. int DSA_security_bits(const DSA *d)
  276. {
  277. if (d->params.p != NULL && d->params.q != NULL)
  278. return BN_security_bits(BN_num_bits(d->params.p),
  279. BN_num_bits(d->params.q));
  280. return -1;
  281. }
  282. int DSA_bits(const DSA *dsa)
  283. {
  284. return BN_num_bits(dsa->params.p);
  285. }
  286. FFC_PARAMS *dsa_get0_params(DSA *dsa)
  287. {
  288. return &dsa->params;
  289. }
  290. int 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 = dsa_get0_params(dsa);
  297. if (ffc == NULL)
  298. return 0;
  299. ret = ffc_params_fromdata(ffc, params);
  300. if (ret)
  301. dsa->dirty_cnt++;
  302. return ret;
  303. }
  304. static int dsa_paramgen_check(EVP_PKEY_CTX *ctx)
  305. {
  306. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  307. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  308. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  309. return -2;
  310. }
  311. /* If key type not DSA return error */
  312. if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_DSA)
  313. return -1;
  314. return 1;
  315. }
  316. int EVP_PKEY_CTX_set_dsa_paramgen_type(EVP_PKEY_CTX *ctx, const char *name)
  317. {
  318. int ret;
  319. OSSL_PARAM params[2], *p = params;
  320. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  321. return ret;
  322. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
  323. (char *)name, 0);
  324. *p++ = OSSL_PARAM_construct_end();
  325. return EVP_PKEY_CTX_set_params(ctx, params);
  326. }
  327. int EVP_PKEY_CTX_set_dsa_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
  328. {
  329. int ret;
  330. OSSL_PARAM params[2], *p = params;
  331. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  332. return ret;
  333. *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
  334. *p++ = OSSL_PARAM_construct_end();
  335. return EVP_PKEY_CTX_set_params(ctx, params);
  336. }
  337. int EVP_PKEY_CTX_set_dsa_paramgen_seed(EVP_PKEY_CTX *ctx,
  338. const unsigned char *seed,
  339. size_t seedlen)
  340. {
  341. int ret;
  342. OSSL_PARAM params[2], *p = params;
  343. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  344. return ret;
  345. *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
  346. (void *)seed, seedlen);
  347. *p++ = OSSL_PARAM_construct_end();
  348. return EVP_PKEY_CTX_set_params(ctx, params);
  349. }
  350. int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits)
  351. {
  352. int ret;
  353. OSSL_PARAM params[2], *p = params;
  354. size_t bits = nbits;
  355. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  356. return ret;
  357. #if !defined(FIPS_MODULE)
  358. /* TODO(3.0): Remove this eventually when no more legacy */
  359. if (ctx->op.keymgmt.genctx == NULL)
  360. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
  361. EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, nbits, NULL);
  362. #endif
  363. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
  364. *p++ = OSSL_PARAM_construct_end();
  365. return EVP_PKEY_CTX_set_params(ctx, params);
  366. }
  367. int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits)
  368. {
  369. int ret;
  370. OSSL_PARAM params[2], *p = params;
  371. size_t bits2 = qbits;
  372. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  373. return ret;
  374. #if !defined(FIPS_MODULE)
  375. /* TODO(3.0): Remove this eventually when no more legacy */
  376. if (ctx->op.keymgmt.genctx == NULL)
  377. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
  378. EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits, NULL);
  379. #endif
  380. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
  381. *p++ = OSSL_PARAM_construct_end();
  382. return EVP_PKEY_CTX_set_params(ctx, params);
  383. }
  384. int EVP_PKEY_CTX_set_dsa_paramgen_md_props(EVP_PKEY_CTX *ctx,
  385. const char *md_name,
  386. const char *md_properties)
  387. {
  388. int ret;
  389. OSSL_PARAM params[3], *p = params;
  390. if ((ret = dsa_paramgen_check(ctx)) <= 0)
  391. return ret;
  392. #if !defined(FIPS_MODULE)
  393. /* TODO(3.0): Remove this eventually when no more legacy */
  394. if (ctx->op.keymgmt.genctx == NULL) {
  395. const EVP_MD *md = EVP_get_digestbyname(md_name);
  396. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
  397. EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, (void *)(md));
  398. }
  399. #endif
  400. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
  401. (char *)md_name, 0);
  402. if (md_properties != NULL)
  403. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
  404. (char *)md_properties, 0);
  405. *p++ = OSSL_PARAM_construct_end();
  406. return EVP_PKEY_CTX_set_params(ctx, params);
  407. }
  408. #if !defined(FIPS_MODULE)
  409. int EVP_PKEY_CTX_set_dsa_paramgen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  410. {
  411. const char *md_name = (md == NULL) ? "" : EVP_MD_name(md);
  412. return EVP_PKEY_CTX_set_dsa_paramgen_md_props(ctx, md_name, NULL);
  413. }
  414. #endif