dsa_ameth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Copyright 2006-2022 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 <openssl/x509.h>
  16. #include <openssl/asn1.h>
  17. #include <openssl/bn.h>
  18. #include <openssl/core_names.h>
  19. #include <openssl/param_build.h>
  20. #include "internal/cryptlib.h"
  21. #include "crypto/asn1.h"
  22. #include "crypto/dsa.h"
  23. #include "crypto/evp.h"
  24. #include "internal/ffc.h"
  25. #include "dsa_local.h"
  26. static int dsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
  27. {
  28. const unsigned char *p, *pm;
  29. int pklen, pmlen;
  30. int ptype;
  31. const void *pval;
  32. const ASN1_STRING *pstr;
  33. X509_ALGOR *palg;
  34. ASN1_INTEGER *public_key = NULL;
  35. DSA *dsa = NULL;
  36. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  37. return 0;
  38. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  39. if (ptype == V_ASN1_SEQUENCE) {
  40. pstr = pval;
  41. pm = pstr->data;
  42. pmlen = pstr->length;
  43. if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
  44. ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR);
  45. goto err;
  46. }
  47. } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
  48. if ((dsa = DSA_new()) == NULL) {
  49. ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB);
  50. goto err;
  51. }
  52. } else {
  53. ERR_raise(ERR_LIB_DSA, DSA_R_PARAMETER_ENCODING_ERROR);
  54. goto err;
  55. }
  56. if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
  57. ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR);
  58. goto err;
  59. }
  60. if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
  61. ERR_raise(ERR_LIB_DSA, DSA_R_BN_DECODE_ERROR);
  62. goto err;
  63. }
  64. dsa->dirty_cnt++;
  65. ASN1_INTEGER_free(public_key);
  66. EVP_PKEY_assign_DSA(pkey, dsa);
  67. return 1;
  68. err:
  69. ASN1_INTEGER_free(public_key);
  70. DSA_free(dsa);
  71. return 0;
  72. }
  73. static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  74. {
  75. DSA *dsa;
  76. int ptype;
  77. unsigned char *penc = NULL;
  78. int penclen;
  79. ASN1_STRING *str = NULL;
  80. ASN1_INTEGER *pubint = NULL;
  81. ASN1_OBJECT *aobj;
  82. dsa = pkey->pkey.dsa;
  83. if (pkey->save_parameters
  84. && dsa->params.p != NULL
  85. && dsa->params.q != NULL
  86. && dsa->params.g != NULL) {
  87. str = ASN1_STRING_new();
  88. if (str == NULL) {
  89. ERR_raise(ERR_LIB_DSA, ERR_R_ASN1_LIB);
  90. goto err;
  91. }
  92. str->length = i2d_DSAparams(dsa, &str->data);
  93. if (str->length <= 0) {
  94. ERR_raise(ERR_LIB_DSA, ERR_R_ASN1_LIB);
  95. goto err;
  96. }
  97. ptype = V_ASN1_SEQUENCE;
  98. } else
  99. ptype = V_ASN1_UNDEF;
  100. pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
  101. if (pubint == NULL) {
  102. ERR_raise(ERR_LIB_DSA, ERR_R_ASN1_LIB);
  103. goto err;
  104. }
  105. penclen = i2d_ASN1_INTEGER(pubint, &penc);
  106. ASN1_INTEGER_free(pubint);
  107. if (penclen <= 0) {
  108. ERR_raise(ERR_LIB_DSA, ERR_R_ASN1_LIB);
  109. goto err;
  110. }
  111. aobj = OBJ_nid2obj(EVP_PKEY_DSA);
  112. if (aobj == NULL)
  113. goto err;
  114. if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
  115. return 1;
  116. err:
  117. OPENSSL_free(penc);
  118. ASN1_STRING_free(str);
  119. return 0;
  120. }
  121. /*
  122. * In PKCS#8 DSA: you just get a private key integer and parameters in the
  123. * AlgorithmIdentifier the pubkey must be recalculated.
  124. */
  125. static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  126. {
  127. int ret = 0;
  128. DSA *dsa = ossl_dsa_key_from_pkcs8(p8, NULL, NULL);
  129. if (dsa != NULL) {
  130. ret = 1;
  131. EVP_PKEY_assign_DSA(pkey, dsa);
  132. }
  133. return ret;
  134. }
  135. static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  136. {
  137. ASN1_STRING *params = NULL;
  138. ASN1_INTEGER *prkey = NULL;
  139. unsigned char *dp = NULL;
  140. int dplen;
  141. if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) {
  142. ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS);
  143. goto err;
  144. }
  145. params = ASN1_STRING_new();
  146. if (params == NULL) {
  147. ERR_raise(ERR_LIB_DSA, ERR_R_ASN1_LIB);
  148. goto err;
  149. }
  150. params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
  151. if (params->length <= 0) {
  152. ERR_raise(ERR_LIB_DSA, ERR_R_ASN1_LIB);
  153. goto err;
  154. }
  155. params->type = V_ASN1_SEQUENCE;
  156. /* Get private key into integer */
  157. prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
  158. if (prkey == NULL) {
  159. ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
  160. goto err;
  161. }
  162. dplen = i2d_ASN1_INTEGER(prkey, &dp);
  163. ASN1_STRING_clear_free(prkey);
  164. if (dplen <= 0) {
  165. ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
  166. goto err;
  167. }
  168. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
  169. V_ASN1_SEQUENCE, params, dp, dplen)) {
  170. OPENSSL_clear_free(dp, dplen);
  171. goto err;
  172. }
  173. return 1;
  174. err:
  175. ASN1_STRING_free(params);
  176. return 0;
  177. }
  178. static int int_dsa_size(const EVP_PKEY *pkey)
  179. {
  180. return DSA_size(pkey->pkey.dsa);
  181. }
  182. static int dsa_bits(const EVP_PKEY *pkey)
  183. {
  184. return DSA_bits(pkey->pkey.dsa);
  185. }
  186. static int dsa_security_bits(const EVP_PKEY *pkey)
  187. {
  188. return DSA_security_bits(pkey->pkey.dsa);
  189. }
  190. static int dsa_missing_parameters(const EVP_PKEY *pkey)
  191. {
  192. DSA *dsa;
  193. dsa = pkey->pkey.dsa;
  194. return dsa == NULL
  195. || dsa->params.p == NULL
  196. || dsa->params.q == NULL
  197. || dsa->params.g == NULL;
  198. }
  199. static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  200. {
  201. if (to->pkey.dsa == NULL) {
  202. to->pkey.dsa = DSA_new();
  203. if (to->pkey.dsa == NULL)
  204. return 0;
  205. }
  206. if (!ossl_ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params))
  207. return 0;
  208. to->pkey.dsa->dirty_cnt++;
  209. return 1;
  210. }
  211. static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  212. {
  213. return ossl_ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1);
  214. }
  215. static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  216. {
  217. return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
  218. }
  219. static void int_dsa_free(EVP_PKEY *pkey)
  220. {
  221. DSA_free(pkey->pkey.dsa);
  222. }
  223. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
  224. {
  225. int ret = 0;
  226. const char *ktype = NULL;
  227. const BIGNUM *priv_key, *pub_key;
  228. int mod_len = 0;
  229. if (x->params.p != NULL)
  230. mod_len = DSA_bits(x);
  231. if (ptype == 2)
  232. priv_key = x->priv_key;
  233. else
  234. priv_key = NULL;
  235. if (ptype > 0)
  236. pub_key = x->pub_key;
  237. else
  238. pub_key = NULL;
  239. if (ptype == 2)
  240. ktype = "Private-Key";
  241. else if (ptype == 1)
  242. ktype = "Public-Key";
  243. else
  244. ktype = "DSA-Parameters";
  245. if (priv_key != NULL) {
  246. if (!BIO_indent(bp, off, 128))
  247. goto err;
  248. if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0)
  249. goto err;
  250. } else {
  251. if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
  252. goto err;
  253. }
  254. if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
  255. goto err;
  256. if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
  257. goto err;
  258. if (!ossl_ffc_params_print(bp, &x->params, off))
  259. goto err;
  260. ret = 1;
  261. err:
  262. return ret;
  263. }
  264. static int dsa_param_decode(EVP_PKEY *pkey,
  265. const unsigned char **pder, int derlen)
  266. {
  267. DSA *dsa;
  268. if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL)
  269. return 0;
  270. dsa->dirty_cnt++;
  271. EVP_PKEY_assign_DSA(pkey, dsa);
  272. return 1;
  273. }
  274. static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  275. {
  276. return i2d_DSAparams(pkey->pkey.dsa, pder);
  277. }
  278. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  279. ASN1_PCTX *ctx)
  280. {
  281. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  282. }
  283. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  284. ASN1_PCTX *ctx)
  285. {
  286. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  287. }
  288. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  289. ASN1_PCTX *ctx)
  290. {
  291. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  292. }
  293. static int old_dsa_priv_decode(EVP_PKEY *pkey,
  294. const unsigned char **pder, int derlen)
  295. {
  296. DSA *dsa;
  297. if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
  298. ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB);
  299. return 0;
  300. }
  301. dsa->dirty_cnt++;
  302. EVP_PKEY_assign_DSA(pkey, dsa);
  303. return 1;
  304. }
  305. static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  306. {
  307. return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
  308. }
  309. static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  310. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  311. {
  312. DSA_SIG *dsa_sig;
  313. const unsigned char *p;
  314. if (sig == NULL) {
  315. if (BIO_puts(bp, "\n") <= 0)
  316. return 0;
  317. else
  318. return 1;
  319. }
  320. p = sig->data;
  321. dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
  322. if (dsa_sig != NULL) {
  323. int rv = 0;
  324. const BIGNUM *r, *s;
  325. DSA_SIG_get0(dsa_sig, &r, &s);
  326. if (BIO_write(bp, "\n", 1) != 1)
  327. goto err;
  328. if (!ASN1_bn_print(bp, "r: ", r, NULL, indent))
  329. goto err;
  330. if (!ASN1_bn_print(bp, "s: ", s, NULL, indent))
  331. goto err;
  332. rv = 1;
  333. err:
  334. DSA_SIG_free(dsa_sig);
  335. return rv;
  336. }
  337. if (BIO_puts(bp, "\n") <= 0)
  338. return 0;
  339. return X509_signature_dump(bp, sig, indent);
  340. }
  341. static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  342. {
  343. switch (op) {
  344. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  345. *(int *)arg2 = NID_sha256;
  346. return 1;
  347. default:
  348. return -2;
  349. }
  350. }
  351. static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
  352. {
  353. return pkey->pkey.dsa->dirty_cnt;
  354. }
  355. static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  356. OSSL_FUNC_keymgmt_import_fn *importer,
  357. OSSL_LIB_CTX *libctx, const char *propq)
  358. {
  359. DSA *dsa = from->pkey.dsa;
  360. OSSL_PARAM_BLD *tmpl;
  361. const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
  362. const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
  363. const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
  364. OSSL_PARAM *params;
  365. int selection = 0;
  366. int rv = 0;
  367. if (p == NULL || q == NULL || g == NULL)
  368. return 0;
  369. tmpl = OSSL_PARAM_BLD_new();
  370. if (tmpl == NULL)
  371. return 0;
  372. if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
  373. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
  374. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
  375. goto err;
  376. selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  377. if (pub_key != NULL) {
  378. if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
  379. pub_key))
  380. goto err;
  381. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  382. }
  383. if (priv_key != NULL) {
  384. if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
  385. priv_key))
  386. goto err;
  387. selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  388. }
  389. if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
  390. goto err;
  391. /* We export, the provider imports */
  392. rv = importer(to_keydata, selection, params);
  393. OSSL_PARAM_free(params);
  394. err:
  395. OSSL_PARAM_BLD_free(tmpl);
  396. return rv;
  397. }
  398. static int dsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  399. {
  400. EVP_PKEY_CTX *pctx = vpctx;
  401. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  402. DSA *dsa = ossl_dsa_new(pctx->libctx);
  403. if (dsa == NULL) {
  404. ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB);
  405. return 0;
  406. }
  407. if (!ossl_dsa_ffc_params_fromdata(dsa, params)
  408. || !ossl_dsa_key_fromdata(dsa, params, 1)
  409. || !EVP_PKEY_assign_DSA(pkey, dsa)) {
  410. DSA_free(dsa);
  411. return 0;
  412. }
  413. return 1;
  414. }
  415. static int dsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
  416. {
  417. DSA *dsa = from->pkey.dsa;
  418. DSA *dupkey = NULL;
  419. int ret;
  420. if (dsa != NULL) {
  421. dupkey = ossl_dsa_dup(dsa, OSSL_KEYMGMT_SELECT_ALL);
  422. if (dupkey == NULL)
  423. return 0;
  424. }
  425. ret = EVP_PKEY_assign_DSA(to, dupkey);
  426. if (!ret)
  427. DSA_free(dupkey);
  428. return ret;
  429. }
  430. /* NB these are sorted in pkey_id order, lowest first */
  431. const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[5] = {
  432. {
  433. EVP_PKEY_DSA2,
  434. EVP_PKEY_DSA,
  435. ASN1_PKEY_ALIAS},
  436. {
  437. EVP_PKEY_DSA1,
  438. EVP_PKEY_DSA,
  439. ASN1_PKEY_ALIAS},
  440. {
  441. EVP_PKEY_DSA4,
  442. EVP_PKEY_DSA,
  443. ASN1_PKEY_ALIAS},
  444. {
  445. EVP_PKEY_DSA3,
  446. EVP_PKEY_DSA,
  447. ASN1_PKEY_ALIAS},
  448. {
  449. EVP_PKEY_DSA,
  450. EVP_PKEY_DSA,
  451. 0,
  452. "DSA",
  453. "OpenSSL DSA method",
  454. dsa_pub_decode,
  455. dsa_pub_encode,
  456. dsa_pub_cmp,
  457. dsa_pub_print,
  458. dsa_priv_decode,
  459. dsa_priv_encode,
  460. dsa_priv_print,
  461. int_dsa_size,
  462. dsa_bits,
  463. dsa_security_bits,
  464. dsa_param_decode,
  465. dsa_param_encode,
  466. dsa_missing_parameters,
  467. dsa_copy_parameters,
  468. dsa_cmp_parameters,
  469. dsa_param_print,
  470. dsa_sig_print,
  471. int_dsa_free,
  472. dsa_pkey_ctrl,
  473. old_dsa_priv_decode,
  474. old_dsa_priv_encode,
  475. NULL, NULL, NULL,
  476. NULL, NULL, NULL,
  477. NULL, NULL, NULL, NULL,
  478. dsa_pkey_dirty_cnt,
  479. dsa_pkey_export_to,
  480. dsa_pkey_import_from,
  481. dsa_pkey_copy
  482. }
  483. };