dsa_ameth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 "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 "asn1_locl.h"
  68. static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  69. {
  70. const unsigned char *p, *pm;
  71. int pklen, pmlen;
  72. int ptype;
  73. void *pval;
  74. ASN1_STRING *pstr;
  75. X509_ALGOR *palg;
  76. ASN1_INTEGER *public_key = NULL;
  77. DSA *dsa = NULL;
  78. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  79. return 0;
  80. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  81. if (ptype == V_ASN1_SEQUENCE) {
  82. pstr = pval;
  83. pm = pstr->data;
  84. pmlen = pstr->length;
  85. if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) {
  86. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
  87. goto err;
  88. }
  89. } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
  90. if (!(dsa = DSA_new())) {
  91. DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
  92. goto err;
  93. }
  94. } else {
  95. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
  96. goto err;
  97. }
  98. if (!(public_key = d2i_ASN1_INTEGER(NULL, &p, pklen))) {
  99. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
  100. goto err;
  101. }
  102. if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) {
  103. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
  104. goto err;
  105. }
  106. ASN1_INTEGER_free(public_key);
  107. EVP_PKEY_assign_DSA(pkey, dsa);
  108. return 1;
  109. err:
  110. if (public_key)
  111. ASN1_INTEGER_free(public_key);
  112. if (dsa)
  113. DSA_free(dsa);
  114. return 0;
  115. }
  116. static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  117. {
  118. DSA *dsa;
  119. int ptype;
  120. unsigned char *penc = NULL;
  121. int penclen;
  122. ASN1_STRING *str = 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) {
  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. dsa->write_params = 0;
  139. penclen = i2d_DSAPublicKey(dsa, &penc);
  140. if (penclen <= 0) {
  141. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  142. goto err;
  143. }
  144. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA),
  145. ptype, str, penc, penclen))
  146. return 1;
  147. err:
  148. if (penc)
  149. OPENSSL_free(penc);
  150. if (str)
  151. ASN1_STRING_free(str);
  152. return 0;
  153. }
  154. /*
  155. * In PKCS#8 DSA: you just get a private key integer and parameters in the
  156. * AlgorithmIdentifier the pubkey must be recalculated.
  157. */
  158. static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  159. {
  160. const unsigned char *p, *pm;
  161. int pklen, pmlen;
  162. int ptype;
  163. void *pval;
  164. ASN1_STRING *pstr;
  165. X509_ALGOR *palg;
  166. ASN1_INTEGER *privkey = NULL;
  167. BN_CTX *ctx = NULL;
  168. STACK_OF(ASN1_TYPE) *ndsa = NULL;
  169. DSA *dsa = NULL;
  170. int ret = 0;
  171. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  172. return 0;
  173. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  174. /* Check for broken DSA PKCS#8, UGH! */
  175. if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
  176. ASN1_TYPE *t1, *t2;
  177. if (!(ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen)))
  178. goto decerr;
  179. if (sk_ASN1_TYPE_num(ndsa) != 2)
  180. goto decerr;
  181. /*-
  182. * Handle Two broken types:
  183. * SEQUENCE {parameters, priv_key}
  184. * SEQUENCE {pub_key, priv_key}
  185. */
  186. t1 = sk_ASN1_TYPE_value(ndsa, 0);
  187. t2 = sk_ASN1_TYPE_value(ndsa, 1);
  188. if (t1->type == V_ASN1_SEQUENCE) {
  189. p8->broken = PKCS8_EMBEDDED_PARAM;
  190. pval = t1->value.ptr;
  191. } else if (ptype == V_ASN1_SEQUENCE)
  192. p8->broken = PKCS8_NS_DB;
  193. else
  194. goto decerr;
  195. if (t2->type != V_ASN1_INTEGER)
  196. goto decerr;
  197. privkey = t2->value.integer;
  198. } else {
  199. const unsigned char *q = p;
  200. if (!(privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)))
  201. goto decerr;
  202. if (privkey->type == V_ASN1_NEG_INTEGER) {
  203. p8->broken = PKCS8_NEG_PRIVKEY;
  204. ASN1_STRING_clear_free(privkey);
  205. if (!(privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen)))
  206. goto decerr;
  207. }
  208. if (ptype != V_ASN1_SEQUENCE)
  209. goto decerr;
  210. }
  211. pstr = pval;
  212. pm = pstr->data;
  213. pmlen = pstr->length;
  214. if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen)))
  215. goto decerr;
  216. /* We have parameters now set private key */
  217. if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
  218. DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
  219. goto dsaerr;
  220. }
  221. /* Calculate public key */
  222. if (!(dsa->pub_key = BN_new())) {
  223. DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
  224. goto dsaerr;
  225. }
  226. if (!(ctx = BN_CTX_new())) {
  227. DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
  228. goto dsaerr;
  229. }
  230. if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
  231. DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
  232. goto dsaerr;
  233. }
  234. EVP_PKEY_assign_DSA(pkey, dsa);
  235. ret = 1;
  236. goto done;
  237. decerr:
  238. DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
  239. dsaerr:
  240. DSA_free(dsa);
  241. done:
  242. BN_CTX_free(ctx);
  243. if (ndsa)
  244. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  245. else
  246. ASN1_STRING_clear_free(privkey);
  247. return ret;
  248. }
  249. static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  250. {
  251. ASN1_STRING *params = NULL;
  252. ASN1_INTEGER *prkey = NULL;
  253. unsigned char *dp = NULL;
  254. int dplen;
  255. if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
  256. DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
  257. goto err;
  258. }
  259. params = ASN1_STRING_new();
  260. if (!params) {
  261. DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  262. goto err;
  263. }
  264. params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
  265. if (params->length <= 0) {
  266. DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  267. goto err;
  268. }
  269. params->type = V_ASN1_SEQUENCE;
  270. /* Get private key into integer */
  271. prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
  272. if (!prkey) {
  273. DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
  274. goto err;
  275. }
  276. dplen = i2d_ASN1_INTEGER(prkey, &dp);
  277. ASN1_STRING_clear_free(prkey);
  278. prkey = NULL;
  279. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
  280. V_ASN1_SEQUENCE, params, dp, dplen))
  281. goto err;
  282. return 1;
  283. err:
  284. if (dp != NULL)
  285. OPENSSL_free(dp);
  286. if (params != NULL)
  287. ASN1_STRING_free(params);
  288. if (prkey != NULL)
  289. ASN1_STRING_clear_free(prkey);
  290. return 0;
  291. }
  292. static int int_dsa_size(const EVP_PKEY *pkey)
  293. {
  294. return (DSA_size(pkey->pkey.dsa));
  295. }
  296. static int dsa_bits(const EVP_PKEY *pkey)
  297. {
  298. return BN_num_bits(pkey->pkey.dsa->p);
  299. }
  300. static int dsa_missing_parameters(const EVP_PKEY *pkey)
  301. {
  302. DSA *dsa;
  303. dsa = pkey->pkey.dsa;
  304. if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
  305. return 1;
  306. return 0;
  307. }
  308. static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  309. {
  310. BIGNUM *a;
  311. if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
  312. return 0;
  313. if (to->pkey.dsa->p != NULL)
  314. BN_free(to->pkey.dsa->p);
  315. to->pkey.dsa->p = a;
  316. if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
  317. return 0;
  318. if (to->pkey.dsa->q != NULL)
  319. BN_free(to->pkey.dsa->q);
  320. to->pkey.dsa->q = a;
  321. if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
  322. return 0;
  323. if (to->pkey.dsa->g != NULL)
  324. BN_free(to->pkey.dsa->g);
  325. to->pkey.dsa->g = a;
  326. return 1;
  327. }
  328. static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  329. {
  330. if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
  331. BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
  332. BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
  333. return 0;
  334. else
  335. return 1;
  336. }
  337. static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  338. {
  339. if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0)
  340. return 0;
  341. else
  342. return 1;
  343. }
  344. static void int_dsa_free(EVP_PKEY *pkey)
  345. {
  346. DSA_free(pkey->pkey.dsa);
  347. }
  348. static void update_buflen(const BIGNUM *b, size_t *pbuflen)
  349. {
  350. size_t i;
  351. if (!b)
  352. return;
  353. if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
  354. *pbuflen = i;
  355. }
  356. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
  357. {
  358. unsigned char *m = NULL;
  359. int ret = 0;
  360. size_t buf_len = 0;
  361. const char *ktype = NULL;
  362. const BIGNUM *priv_key, *pub_key;
  363. if (ptype == 2)
  364. priv_key = x->priv_key;
  365. else
  366. priv_key = NULL;
  367. if (ptype > 0)
  368. pub_key = x->pub_key;
  369. else
  370. pub_key = NULL;
  371. if (ptype == 2)
  372. ktype = "Private-Key";
  373. else if (ptype == 1)
  374. ktype = "Public-Key";
  375. else
  376. ktype = "DSA-Parameters";
  377. update_buflen(x->p, &buf_len);
  378. update_buflen(x->q, &buf_len);
  379. update_buflen(x->g, &buf_len);
  380. update_buflen(priv_key, &buf_len);
  381. update_buflen(pub_key, &buf_len);
  382. m = (unsigned char *)OPENSSL_malloc(buf_len + 10);
  383. if (m == NULL) {
  384. DSAerr(DSA_F_DO_DSA_PRINT, ERR_R_MALLOC_FAILURE);
  385. goto err;
  386. }
  387. if (priv_key) {
  388. if (!BIO_indent(bp, off, 128))
  389. goto err;
  390. if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
  391. <= 0)
  392. goto err;
  393. }
  394. if (!ASN1_bn_print(bp, "priv:", priv_key, m, off))
  395. goto err;
  396. if (!ASN1_bn_print(bp, "pub: ", pub_key, m, off))
  397. goto err;
  398. if (!ASN1_bn_print(bp, "P: ", x->p, m, off))
  399. goto err;
  400. if (!ASN1_bn_print(bp, "Q: ", x->q, m, off))
  401. goto err;
  402. if (!ASN1_bn_print(bp, "G: ", x->g, m, off))
  403. goto err;
  404. ret = 1;
  405. err:
  406. if (m != NULL)
  407. OPENSSL_free(m);
  408. return (ret);
  409. }
  410. static int dsa_param_decode(EVP_PKEY *pkey,
  411. const unsigned char **pder, int derlen)
  412. {
  413. DSA *dsa;
  414. if (!(dsa = d2i_DSAparams(NULL, pder, derlen))) {
  415. DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
  416. return 0;
  417. }
  418. EVP_PKEY_assign_DSA(pkey, dsa);
  419. return 1;
  420. }
  421. static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  422. {
  423. return i2d_DSAparams(pkey->pkey.dsa, pder);
  424. }
  425. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  426. ASN1_PCTX *ctx)
  427. {
  428. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  429. }
  430. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  431. ASN1_PCTX *ctx)
  432. {
  433. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  434. }
  435. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  436. ASN1_PCTX *ctx)
  437. {
  438. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  439. }
  440. static int old_dsa_priv_decode(EVP_PKEY *pkey,
  441. const unsigned char **pder, int derlen)
  442. {
  443. DSA *dsa;
  444. if (!(dsa = d2i_DSAPrivateKey(NULL, pder, derlen))) {
  445. DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
  446. return 0;
  447. }
  448. EVP_PKEY_assign_DSA(pkey, dsa);
  449. return 1;
  450. }
  451. static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  452. {
  453. return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
  454. }
  455. static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  456. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  457. {
  458. DSA_SIG *dsa_sig;
  459. const unsigned char *p;
  460. if (!sig) {
  461. if (BIO_puts(bp, "\n") <= 0)
  462. return 0;
  463. else
  464. return 1;
  465. }
  466. p = sig->data;
  467. dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
  468. if (dsa_sig) {
  469. int rv = 0;
  470. size_t buf_len = 0;
  471. unsigned char *m = NULL;
  472. update_buflen(dsa_sig->r, &buf_len);
  473. update_buflen(dsa_sig->s, &buf_len);
  474. m = OPENSSL_malloc(buf_len + 10);
  475. if (m == NULL) {
  476. DSAerr(DSA_F_DSA_SIG_PRINT, ERR_R_MALLOC_FAILURE);
  477. goto err;
  478. }
  479. if (BIO_write(bp, "\n", 1) != 1)
  480. goto err;
  481. if (!ASN1_bn_print(bp, "r: ", dsa_sig->r, m, indent))
  482. goto err;
  483. if (!ASN1_bn_print(bp, "s: ", dsa_sig->s, m, indent))
  484. goto err;
  485. rv = 1;
  486. err:
  487. if (m)
  488. OPENSSL_free(m);
  489. DSA_SIG_free(dsa_sig);
  490. return rv;
  491. }
  492. return X509_signature_dump(bp, sig, indent);
  493. }
  494. static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  495. {
  496. switch (op) {
  497. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  498. if (arg1 == 0) {
  499. int snid, hnid;
  500. X509_ALGOR *alg1, *alg2;
  501. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
  502. if (alg1 == NULL || alg1->algorithm == NULL)
  503. return -1;
  504. hnid = OBJ_obj2nid(alg1->algorithm);
  505. if (hnid == NID_undef)
  506. return -1;
  507. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  508. return -1;
  509. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  510. }
  511. return 1;
  512. #ifndef OPENSSL_NO_CMS
  513. case ASN1_PKEY_CTRL_CMS_SIGN:
  514. if (arg1 == 0) {
  515. int snid, hnid;
  516. X509_ALGOR *alg1, *alg2;
  517. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
  518. if (alg1 == NULL || alg1->algorithm == NULL)
  519. return -1;
  520. hnid = OBJ_obj2nid(alg1->algorithm);
  521. if (hnid == NID_undef)
  522. return -1;
  523. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  524. return -1;
  525. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  526. }
  527. return 1;
  528. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  529. *(int *)arg2 = CMS_RECIPINFO_NONE;
  530. return 1;
  531. #endif
  532. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  533. *(int *)arg2 = NID_sha256;
  534. return 2;
  535. default:
  536. return -2;
  537. }
  538. }
  539. /* NB these are sorted in pkey_id order, lowest first */
  540. const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = {
  541. {
  542. EVP_PKEY_DSA2,
  543. EVP_PKEY_DSA,
  544. ASN1_PKEY_ALIAS},
  545. {
  546. EVP_PKEY_DSA1,
  547. EVP_PKEY_DSA,
  548. ASN1_PKEY_ALIAS},
  549. {
  550. EVP_PKEY_DSA4,
  551. EVP_PKEY_DSA,
  552. ASN1_PKEY_ALIAS},
  553. {
  554. EVP_PKEY_DSA3,
  555. EVP_PKEY_DSA,
  556. ASN1_PKEY_ALIAS},
  557. {
  558. EVP_PKEY_DSA,
  559. EVP_PKEY_DSA,
  560. 0,
  561. "DSA",
  562. "OpenSSL DSA method",
  563. dsa_pub_decode,
  564. dsa_pub_encode,
  565. dsa_pub_cmp,
  566. dsa_pub_print,
  567. dsa_priv_decode,
  568. dsa_priv_encode,
  569. dsa_priv_print,
  570. int_dsa_size,
  571. dsa_bits,
  572. dsa_param_decode,
  573. dsa_param_encode,
  574. dsa_missing_parameters,
  575. dsa_copy_parameters,
  576. dsa_cmp_parameters,
  577. dsa_param_print,
  578. dsa_sig_print,
  579. int_dsa_free,
  580. dsa_pkey_ctrl,
  581. old_dsa_priv_decode,
  582. old_dsa_priv_encode}
  583. };