2
0

dsa_ameth.c 15 KB

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