2
0

dsa_ameth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/x509.h>
  61. #include <openssl/asn1.h>
  62. #include <openssl/dsa.h>
  63. #include <openssl/bn.h>
  64. #ifndef OPENSSL_NO_CMS
  65. # include <openssl/cms.h>
  66. #endif
  67. #include "internal/asn1_int.h"
  68. #include "internal/evp_int.h"
  69. static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  70. {
  71. const unsigned char *p, *pm;
  72. int pklen, pmlen;
  73. int ptype;
  74. void *pval;
  75. ASN1_STRING *pstr;
  76. X509_ALGOR *palg;
  77. ASN1_INTEGER *public_key = NULL;
  78. DSA *dsa = NULL;
  79. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  80. return 0;
  81. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  82. if (ptype == V_ASN1_SEQUENCE) {
  83. pstr = pval;
  84. pm = pstr->data;
  85. pmlen = pstr->length;
  86. if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
  87. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
  88. goto err;
  89. }
  90. } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
  91. if ((dsa = DSA_new()) == NULL) {
  92. DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
  93. goto err;
  94. }
  95. } else {
  96. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
  97. goto err;
  98. }
  99. if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
  100. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
  101. goto err;
  102. }
  103. if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
  104. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
  105. goto err;
  106. }
  107. ASN1_INTEGER_free(public_key);
  108. EVP_PKEY_assign_DSA(pkey, dsa);
  109. return 1;
  110. err:
  111. ASN1_INTEGER_free(public_key);
  112. DSA_free(dsa);
  113. return 0;
  114. }
  115. static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  116. {
  117. DSA *dsa;
  118. int ptype;
  119. unsigned char *penc = NULL;
  120. int penclen;
  121. ASN1_STRING *str = NULL;
  122. ASN1_INTEGER *pubint = NULL;
  123. dsa = pkey->pkey.dsa;
  124. if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
  125. str = ASN1_STRING_new();
  126. if (str == NULL) {
  127. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  128. goto err;
  129. }
  130. str->length = i2d_DSAparams(dsa, &str->data);
  131. if (str->length <= 0) {
  132. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  133. goto err;
  134. }
  135. ptype = V_ASN1_SEQUENCE;
  136. } else
  137. ptype = V_ASN1_UNDEF;
  138. pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
  139. if (pubint == NULL) {
  140. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  141. goto err;
  142. }
  143. penclen = i2d_ASN1_INTEGER(pubint, &penc);
  144. ASN1_INTEGER_free(pubint);
  145. if (penclen <= 0) {
  146. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  147. goto err;
  148. }
  149. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA),
  150. ptype, str, penc, penclen))
  151. return 1;
  152. err:
  153. OPENSSL_free(penc);
  154. ASN1_STRING_free(str);
  155. return 0;
  156. }
  157. /*
  158. * In PKCS#8 DSA: you just get a private key integer and parameters in the
  159. * AlgorithmIdentifier the pubkey must be recalculated.
  160. */
  161. static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  162. {
  163. const unsigned char *p, *pm;
  164. int pklen, pmlen;
  165. int ptype;
  166. void *pval;
  167. ASN1_STRING *pstr;
  168. X509_ALGOR *palg;
  169. ASN1_INTEGER *privkey = NULL;
  170. BN_CTX *ctx = NULL;
  171. STACK_OF(ASN1_TYPE) *ndsa = NULL;
  172. DSA *dsa = NULL;
  173. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  174. return 0;
  175. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  176. /* Check for broken DSA PKCS#8, UGH! */
  177. if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
  178. ASN1_TYPE *t1, *t2;
  179. if ((ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen)) == NULL)
  180. goto decerr;
  181. if (sk_ASN1_TYPE_num(ndsa) != 2)
  182. goto decerr;
  183. /*-
  184. * Handle Two broken types:
  185. * SEQUENCE {parameters, priv_key}
  186. * SEQUENCE {pub_key, priv_key}
  187. */
  188. t1 = sk_ASN1_TYPE_value(ndsa, 0);
  189. t2 = sk_ASN1_TYPE_value(ndsa, 1);
  190. if (t1->type == V_ASN1_SEQUENCE) {
  191. p8->broken = PKCS8_EMBEDDED_PARAM;
  192. pval = t1->value.ptr;
  193. } else if (ptype == V_ASN1_SEQUENCE)
  194. p8->broken = PKCS8_NS_DB;
  195. else
  196. goto decerr;
  197. if (t2->type != V_ASN1_INTEGER)
  198. goto decerr;
  199. privkey = t2->value.integer;
  200. } else {
  201. const unsigned char *q = p;
  202. if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
  203. goto decerr;
  204. if (privkey->type == V_ASN1_NEG_INTEGER) {
  205. p8->broken = PKCS8_NEG_PRIVKEY;
  206. ASN1_STRING_clear_free(privkey);
  207. if ((privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen)) == NULL)
  208. goto decerr;
  209. }
  210. if (ptype != V_ASN1_SEQUENCE)
  211. goto decerr;
  212. }
  213. pstr = pval;
  214. pm = pstr->data;
  215. pmlen = pstr->length;
  216. if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
  217. goto decerr;
  218. /* We have parameters now set private key */
  219. if ((dsa->priv_key = BN_secure_new()) == NULL
  220. || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
  221. DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
  222. goto dsaerr;
  223. }
  224. /* Calculate public key */
  225. if ((dsa->pub_key = BN_new()) == NULL) {
  226. DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
  227. goto dsaerr;
  228. }
  229. if ((ctx = BN_CTX_new()) == NULL) {
  230. DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
  231. goto dsaerr;
  232. }
  233. if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
  234. DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
  235. goto dsaerr;
  236. }
  237. EVP_PKEY_assign_DSA(pkey, dsa);
  238. BN_CTX_free(ctx);
  239. if (ndsa)
  240. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  241. else
  242. ASN1_STRING_clear_free(privkey);
  243. return 1;
  244. decerr:
  245. DSAerr(DSA_F_DSA_PRIV_DECODE, EVP_R_DECODE_ERROR);
  246. dsaerr:
  247. BN_CTX_free(ctx);
  248. ASN1_STRING_clear_free(privkey);
  249. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  250. DSA_free(dsa);
  251. return 0;
  252. }
  253. static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  254. {
  255. ASN1_STRING *params = NULL;
  256. ASN1_INTEGER *prkey = NULL;
  257. unsigned char *dp = NULL;
  258. int dplen;
  259. if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
  260. DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
  261. goto err;
  262. }
  263. params = ASN1_STRING_new();
  264. if (params == NULL) {
  265. DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  266. goto err;
  267. }
  268. params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
  269. if (params->length <= 0) {
  270. DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  271. goto err;
  272. }
  273. params->type = V_ASN1_SEQUENCE;
  274. /* Get private key into integer */
  275. prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
  276. if (!prkey) {
  277. DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
  278. goto err;
  279. }
  280. dplen = i2d_ASN1_INTEGER(prkey, &dp);
  281. ASN1_STRING_clear_free(prkey);
  282. prkey = NULL;
  283. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
  284. V_ASN1_SEQUENCE, params, dp, dplen))
  285. goto err;
  286. return 1;
  287. err:
  288. OPENSSL_free(dp);
  289. ASN1_STRING_free(params);
  290. ASN1_STRING_clear_free(prkey);
  291. return 0;
  292. }
  293. static int int_dsa_size(const EVP_PKEY *pkey)
  294. {
  295. return (DSA_size(pkey->pkey.dsa));
  296. }
  297. static int dsa_bits(const EVP_PKEY *pkey)
  298. {
  299. return BN_num_bits(pkey->pkey.dsa->p);
  300. }
  301. static int dsa_security_bits(const EVP_PKEY *pkey)
  302. {
  303. return DSA_security_bits(pkey->pkey.dsa);
  304. }
  305. static int dsa_missing_parameters(const EVP_PKEY *pkey)
  306. {
  307. DSA *dsa;
  308. dsa = pkey->pkey.dsa;
  309. if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
  310. return 1;
  311. return 0;
  312. }
  313. static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  314. {
  315. BIGNUM *a;
  316. if (to->pkey.dsa == NULL) {
  317. to->pkey.dsa = DSA_new();
  318. if (to->pkey.dsa == NULL)
  319. return 0;
  320. }
  321. if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
  322. return 0;
  323. BN_free(to->pkey.dsa->p);
  324. to->pkey.dsa->p = a;
  325. if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
  326. return 0;
  327. BN_free(to->pkey.dsa->q);
  328. to->pkey.dsa->q = a;
  329. if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
  330. return 0;
  331. BN_free(to->pkey.dsa->g);
  332. to->pkey.dsa->g = a;
  333. return 1;
  334. }
  335. static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  336. {
  337. if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
  338. BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
  339. BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
  340. return 0;
  341. else
  342. return 1;
  343. }
  344. static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  345. {
  346. if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0)
  347. return 0;
  348. else
  349. return 1;
  350. }
  351. static void int_dsa_free(EVP_PKEY *pkey)
  352. {
  353. DSA_free(pkey->pkey.dsa);
  354. }
  355. static void update_buflen(const BIGNUM *b, size_t *pbuflen)
  356. {
  357. size_t i;
  358. if (!b)
  359. return;
  360. if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
  361. *pbuflen = i;
  362. }
  363. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
  364. {
  365. unsigned char *m = NULL;
  366. int ret = 0;
  367. size_t buf_len = 0;
  368. const char *ktype = NULL;
  369. const BIGNUM *priv_key, *pub_key;
  370. if (ptype == 2)
  371. priv_key = x->priv_key;
  372. else
  373. priv_key = NULL;
  374. if (ptype > 0)
  375. pub_key = x->pub_key;
  376. else
  377. pub_key = NULL;
  378. if (ptype == 2)
  379. ktype = "Private-Key";
  380. else if (ptype == 1)
  381. ktype = "Public-Key";
  382. else
  383. ktype = "DSA-Parameters";
  384. update_buflen(x->p, &buf_len);
  385. update_buflen(x->q, &buf_len);
  386. update_buflen(x->g, &buf_len);
  387. update_buflen(priv_key, &buf_len);
  388. update_buflen(pub_key, &buf_len);
  389. m = OPENSSL_malloc(buf_len + 10);
  390. if (m == NULL) {
  391. DSAerr(DSA_F_DO_DSA_PRINT, ERR_R_MALLOC_FAILURE);
  392. goto err;
  393. }
  394. if (priv_key) {
  395. if (!BIO_indent(bp, off, 128))
  396. goto err;
  397. if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
  398. <= 0)
  399. goto err;
  400. }
  401. if (!ASN1_bn_print(bp, "priv:", priv_key, m, off))
  402. goto err;
  403. if (!ASN1_bn_print(bp, "pub: ", pub_key, m, off))
  404. goto err;
  405. if (!ASN1_bn_print(bp, "P: ", x->p, m, off))
  406. goto err;
  407. if (!ASN1_bn_print(bp, "Q: ", x->q, m, off))
  408. goto err;
  409. if (!ASN1_bn_print(bp, "G: ", x->g, m, off))
  410. goto err;
  411. ret = 1;
  412. err:
  413. OPENSSL_free(m);
  414. return (ret);
  415. }
  416. static int dsa_param_decode(EVP_PKEY *pkey,
  417. const unsigned char **pder, int derlen)
  418. {
  419. DSA *dsa;
  420. if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
  421. DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
  422. return 0;
  423. }
  424. EVP_PKEY_assign_DSA(pkey, dsa);
  425. return 1;
  426. }
  427. static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  428. {
  429. return i2d_DSAparams(pkey->pkey.dsa, pder);
  430. }
  431. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  432. ASN1_PCTX *ctx)
  433. {
  434. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  435. }
  436. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  437. ASN1_PCTX *ctx)
  438. {
  439. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  440. }
  441. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  442. ASN1_PCTX *ctx)
  443. {
  444. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  445. }
  446. static int old_dsa_priv_decode(EVP_PKEY *pkey,
  447. const unsigned char **pder, int derlen)
  448. {
  449. DSA *dsa;
  450. if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
  451. DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
  452. return 0;
  453. }
  454. EVP_PKEY_assign_DSA(pkey, dsa);
  455. return 1;
  456. }
  457. static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  458. {
  459. return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
  460. }
  461. static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  462. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  463. {
  464. DSA_SIG *dsa_sig;
  465. const unsigned char *p;
  466. if (!sig) {
  467. if (BIO_puts(bp, "\n") <= 0)
  468. return 0;
  469. else
  470. return 1;
  471. }
  472. p = sig->data;
  473. dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
  474. if (dsa_sig) {
  475. int rv = 0;
  476. size_t buf_len = 0;
  477. unsigned char *m = NULL;
  478. update_buflen(dsa_sig->r, &buf_len);
  479. update_buflen(dsa_sig->s, &buf_len);
  480. m = OPENSSL_malloc(buf_len + 10);
  481. if (m == NULL) {
  482. DSAerr(DSA_F_DSA_SIG_PRINT, ERR_R_MALLOC_FAILURE);
  483. goto err;
  484. }
  485. if (BIO_write(bp, "\n", 1) != 1)
  486. goto err;
  487. if (!ASN1_bn_print(bp, "r: ", dsa_sig->r, m, indent))
  488. goto err;
  489. if (!ASN1_bn_print(bp, "s: ", dsa_sig->s, m, indent))
  490. goto err;
  491. rv = 1;
  492. err:
  493. OPENSSL_free(m);
  494. DSA_SIG_free(dsa_sig);
  495. return rv;
  496. }
  497. return X509_signature_dump(bp, sig, indent);
  498. }
  499. static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  500. {
  501. switch (op) {
  502. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  503. if (arg1 == 0) {
  504. int snid, hnid;
  505. X509_ALGOR *alg1, *alg2;
  506. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
  507. if (alg1 == NULL || alg1->algorithm == NULL)
  508. return -1;
  509. hnid = OBJ_obj2nid(alg1->algorithm);
  510. if (hnid == NID_undef)
  511. return -1;
  512. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  513. return -1;
  514. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  515. }
  516. return 1;
  517. #ifndef OPENSSL_NO_CMS
  518. case ASN1_PKEY_CTRL_CMS_SIGN:
  519. if (arg1 == 0) {
  520. int snid, hnid;
  521. X509_ALGOR *alg1, *alg2;
  522. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
  523. if (alg1 == NULL || alg1->algorithm == NULL)
  524. return -1;
  525. hnid = OBJ_obj2nid(alg1->algorithm);
  526. if (hnid == NID_undef)
  527. return -1;
  528. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  529. return -1;
  530. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  531. }
  532. return 1;
  533. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  534. *(int *)arg2 = CMS_RECIPINFO_NONE;
  535. return 1;
  536. #endif
  537. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  538. *(int *)arg2 = NID_sha256;
  539. return 2;
  540. default:
  541. return -2;
  542. }
  543. }
  544. /* NB these are sorted in pkey_id order, lowest first */
  545. const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = {
  546. {
  547. EVP_PKEY_DSA2,
  548. EVP_PKEY_DSA,
  549. ASN1_PKEY_ALIAS},
  550. {
  551. EVP_PKEY_DSA1,
  552. EVP_PKEY_DSA,
  553. ASN1_PKEY_ALIAS},
  554. {
  555. EVP_PKEY_DSA4,
  556. EVP_PKEY_DSA,
  557. ASN1_PKEY_ALIAS},
  558. {
  559. EVP_PKEY_DSA3,
  560. EVP_PKEY_DSA,
  561. ASN1_PKEY_ALIAS},
  562. {
  563. EVP_PKEY_DSA,
  564. EVP_PKEY_DSA,
  565. 0,
  566. "DSA",
  567. "OpenSSL DSA method",
  568. dsa_pub_decode,
  569. dsa_pub_encode,
  570. dsa_pub_cmp,
  571. dsa_pub_print,
  572. dsa_priv_decode,
  573. dsa_priv_encode,
  574. dsa_priv_print,
  575. int_dsa_size,
  576. dsa_bits,
  577. dsa_security_bits,
  578. dsa_param_decode,
  579. dsa_param_encode,
  580. dsa_missing_parameters,
  581. dsa_copy_parameters,
  582. dsa_cmp_parameters,
  583. dsa_param_print,
  584. dsa_sig_print,
  585. int_dsa_free,
  586. dsa_pkey_ctrl,
  587. old_dsa_priv_decode,
  588. old_dsa_priv_encode}
  589. };