dsa_ameth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  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. ASN1_OBJECT *aobj;
  124. dsa = pkey->pkey.dsa;
  125. if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
  126. str = ASN1_STRING_new();
  127. if (!str) {
  128. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  129. goto err;
  130. }
  131. str->length = i2d_DSAparams(dsa, &str->data);
  132. if (str->length <= 0) {
  133. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  134. goto err;
  135. }
  136. ptype = V_ASN1_SEQUENCE;
  137. } else
  138. ptype = V_ASN1_UNDEF;
  139. dsa->write_params = 0;
  140. penclen = i2d_DSAPublicKey(dsa, &penc);
  141. if (penclen <= 0) {
  142. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  143. goto err;
  144. }
  145. aobj = OBJ_nid2obj(EVP_PKEY_DSA);
  146. if (aobj == NULL)
  147. goto err;
  148. if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
  149. return 1;
  150. err:
  151. if (penc)
  152. OPENSSL_free(penc);
  153. if (str)
  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. int ret = 0;
  174. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  175. return 0;
  176. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  177. /* Check for broken DSA PKCS#8, UGH! */
  178. if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
  179. ASN1_TYPE *t1, *t2;
  180. if (!(ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen)))
  181. goto decerr;
  182. if (sk_ASN1_TYPE_num(ndsa) != 2)
  183. goto decerr;
  184. /*-
  185. * Handle Two broken types:
  186. * SEQUENCE {parameters, priv_key}
  187. * SEQUENCE {pub_key, priv_key}
  188. */
  189. t1 = sk_ASN1_TYPE_value(ndsa, 0);
  190. t2 = sk_ASN1_TYPE_value(ndsa, 1);
  191. if (t1->type == V_ASN1_SEQUENCE) {
  192. p8->broken = PKCS8_EMBEDDED_PARAM;
  193. pval = t1->value.ptr;
  194. } else if (ptype == V_ASN1_SEQUENCE)
  195. p8->broken = PKCS8_NS_DB;
  196. else
  197. goto decerr;
  198. if (t2->type != V_ASN1_INTEGER)
  199. goto decerr;
  200. privkey = t2->value.integer;
  201. } else {
  202. const unsigned char *q = p;
  203. if (!(privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)))
  204. goto decerr;
  205. if (privkey->type == V_ASN1_NEG_INTEGER) {
  206. p8->broken = PKCS8_NEG_PRIVKEY;
  207. ASN1_STRING_clear_free(privkey);
  208. if (!(privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen)))
  209. goto decerr;
  210. }
  211. if (ptype != V_ASN1_SEQUENCE)
  212. goto decerr;
  213. }
  214. pstr = pval;
  215. pm = pstr->data;
  216. pmlen = pstr->length;
  217. if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen)))
  218. goto decerr;
  219. /* We have parameters now set private key */
  220. if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
  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())) {
  226. DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
  227. goto dsaerr;
  228. }
  229. if (!(ctx = BN_CTX_new())) {
  230. DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
  231. goto dsaerr;
  232. }
  233. BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
  234. if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
  235. DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
  236. goto dsaerr;
  237. }
  238. EVP_PKEY_assign_DSA(pkey, dsa);
  239. ret = 1;
  240. goto done;
  241. decerr:
  242. DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
  243. dsaerr:
  244. DSA_free(dsa);
  245. done:
  246. BN_CTX_free(ctx);
  247. if (ndsa)
  248. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  249. else
  250. ASN1_STRING_clear_free(privkey);
  251. return ret;
  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) {
  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. if (dp != NULL)
  289. OPENSSL_free(dp);
  290. if (params != NULL)
  291. ASN1_STRING_free(params);
  292. if (prkey != NULL)
  293. ASN1_STRING_clear_free(prkey);
  294. return 0;
  295. }
  296. static int int_dsa_size(const EVP_PKEY *pkey)
  297. {
  298. return (DSA_size(pkey->pkey.dsa));
  299. }
  300. static int dsa_bits(const EVP_PKEY *pkey)
  301. {
  302. return BN_num_bits(pkey->pkey.dsa->p);
  303. }
  304. static int dsa_missing_parameters(const EVP_PKEY *pkey)
  305. {
  306. DSA *dsa;
  307. dsa = pkey->pkey.dsa;
  308. if (dsa == NULL || dsa->p == NULL || dsa->q == NULL || dsa->g == NULL)
  309. return 1;
  310. return 0;
  311. }
  312. static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  313. {
  314. BIGNUM *a;
  315. if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
  316. return 0;
  317. if (to->pkey.dsa->p != NULL)
  318. BN_free(to->pkey.dsa->p);
  319. to->pkey.dsa->p = a;
  320. if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
  321. return 0;
  322. if (to->pkey.dsa->q != NULL)
  323. BN_free(to->pkey.dsa->q);
  324. to->pkey.dsa->q = a;
  325. if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
  326. return 0;
  327. if (to->pkey.dsa->g != NULL)
  328. BN_free(to->pkey.dsa->g);
  329. to->pkey.dsa->g = a;
  330. return 1;
  331. }
  332. static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  333. {
  334. if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
  335. BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
  336. BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
  337. return 0;
  338. else
  339. return 1;
  340. }
  341. static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  342. {
  343. if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0)
  344. return 0;
  345. else
  346. return 1;
  347. }
  348. static void int_dsa_free(EVP_PKEY *pkey)
  349. {
  350. DSA_free(pkey->pkey.dsa);
  351. }
  352. static void update_buflen(const BIGNUM *b, size_t *pbuflen)
  353. {
  354. size_t i;
  355. if (!b)
  356. return;
  357. if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
  358. *pbuflen = i;
  359. }
  360. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
  361. {
  362. unsigned char *m = NULL;
  363. int ret = 0;
  364. size_t buf_len = 0;
  365. const char *ktype = NULL;
  366. const BIGNUM *priv_key, *pub_key;
  367. if (ptype == 2)
  368. priv_key = x->priv_key;
  369. else
  370. priv_key = NULL;
  371. if (ptype > 0)
  372. pub_key = x->pub_key;
  373. else
  374. pub_key = NULL;
  375. if (ptype == 2)
  376. ktype = "Private-Key";
  377. else if (ptype == 1)
  378. ktype = "Public-Key";
  379. else
  380. ktype = "DSA-Parameters";
  381. update_buflen(x->p, &buf_len);
  382. update_buflen(x->q, &buf_len);
  383. update_buflen(x->g, &buf_len);
  384. update_buflen(priv_key, &buf_len);
  385. update_buflen(pub_key, &buf_len);
  386. m = (unsigned char *)OPENSSL_malloc(buf_len + 10);
  387. if (m == NULL) {
  388. DSAerr(DSA_F_DO_DSA_PRINT, ERR_R_MALLOC_FAILURE);
  389. goto err;
  390. }
  391. if (priv_key) {
  392. if (!BIO_indent(bp, off, 128))
  393. goto err;
  394. if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
  395. <= 0)
  396. goto err;
  397. }
  398. if (!ASN1_bn_print(bp, "priv:", priv_key, m, off))
  399. goto err;
  400. if (!ASN1_bn_print(bp, "pub: ", pub_key, m, off))
  401. goto err;
  402. if (!ASN1_bn_print(bp, "P: ", x->p, m, off))
  403. goto err;
  404. if (!ASN1_bn_print(bp, "Q: ", x->q, m, off))
  405. goto err;
  406. if (!ASN1_bn_print(bp, "G: ", x->g, m, off))
  407. goto err;
  408. ret = 1;
  409. err:
  410. if (m != NULL)
  411. OPENSSL_free(m);
  412. return (ret);
  413. }
  414. static int dsa_param_decode(EVP_PKEY *pkey,
  415. const unsigned char **pder, int derlen)
  416. {
  417. DSA *dsa;
  418. if (!(dsa = d2i_DSAparams(NULL, pder, derlen))) {
  419. DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
  420. return 0;
  421. }
  422. EVP_PKEY_assign_DSA(pkey, dsa);
  423. return 1;
  424. }
  425. static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  426. {
  427. return i2d_DSAparams(pkey->pkey.dsa, pder);
  428. }
  429. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  430. ASN1_PCTX *ctx)
  431. {
  432. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  433. }
  434. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  435. ASN1_PCTX *ctx)
  436. {
  437. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  438. }
  439. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  440. ASN1_PCTX *ctx)
  441. {
  442. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  443. }
  444. static int old_dsa_priv_decode(EVP_PKEY *pkey,
  445. const unsigned char **pder, int derlen)
  446. {
  447. DSA *dsa;
  448. if (!(dsa = d2i_DSAPrivateKey(NULL, pder, derlen))) {
  449. DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
  450. return 0;
  451. }
  452. EVP_PKEY_assign_DSA(pkey, dsa);
  453. return 1;
  454. }
  455. static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  456. {
  457. return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
  458. }
  459. static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  460. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  461. {
  462. DSA_SIG *dsa_sig;
  463. const unsigned char *p;
  464. if (!sig) {
  465. if (BIO_puts(bp, "\n") <= 0)
  466. return 0;
  467. else
  468. return 1;
  469. }
  470. p = sig->data;
  471. dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
  472. if (dsa_sig) {
  473. int rv = 0;
  474. size_t buf_len = 0;
  475. unsigned char *m = NULL;
  476. update_buflen(dsa_sig->r, &buf_len);
  477. update_buflen(dsa_sig->s, &buf_len);
  478. m = OPENSSL_malloc(buf_len + 10);
  479. if (m == NULL) {
  480. DSAerr(DSA_F_DSA_SIG_PRINT, ERR_R_MALLOC_FAILURE);
  481. goto err;
  482. }
  483. if (BIO_write(bp, "\n", 1) != 1)
  484. goto err;
  485. if (!ASN1_bn_print(bp, "r: ", dsa_sig->r, m, indent))
  486. goto err;
  487. if (!ASN1_bn_print(bp, "s: ", dsa_sig->s, m, indent))
  488. goto err;
  489. rv = 1;
  490. err:
  491. if (m)
  492. OPENSSL_free(m);
  493. DSA_SIG_free(dsa_sig);
  494. return rv;
  495. }
  496. return X509_signature_dump(bp, sig, indent);
  497. }
  498. static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  499. {
  500. switch (op) {
  501. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  502. if (arg1 == 0) {
  503. int snid, hnid;
  504. X509_ALGOR *alg1, *alg2;
  505. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
  506. if (alg1 == NULL || alg1->algorithm == NULL)
  507. return -1;
  508. hnid = OBJ_obj2nid(alg1->algorithm);
  509. if (hnid == NID_undef)
  510. return -1;
  511. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  512. return -1;
  513. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  514. }
  515. return 1;
  516. #ifndef OPENSSL_NO_CMS
  517. case ASN1_PKEY_CTRL_CMS_SIGN:
  518. if (arg1 == 0) {
  519. int snid, hnid;
  520. X509_ALGOR *alg1, *alg2;
  521. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
  522. if (alg1 == NULL || alg1->algorithm == NULL)
  523. return -1;
  524. hnid = OBJ_obj2nid(alg1->algorithm);
  525. if (hnid == NID_undef)
  526. return -1;
  527. if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
  528. return -1;
  529. X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
  530. }
  531. return 1;
  532. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  533. *(int *)arg2 = CMS_RECIPINFO_NONE;
  534. return 1;
  535. #endif
  536. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  537. *(int *)arg2 = NID_sha256;
  538. return 2;
  539. default:
  540. return -2;
  541. }
  542. }
  543. /* NB these are sorted in pkey_id order, lowest first */
  544. const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = {
  545. {
  546. EVP_PKEY_DSA2,
  547. EVP_PKEY_DSA,
  548. ASN1_PKEY_ALIAS},
  549. {
  550. EVP_PKEY_DSA1,
  551. EVP_PKEY_DSA,
  552. ASN1_PKEY_ALIAS},
  553. {
  554. EVP_PKEY_DSA4,
  555. EVP_PKEY_DSA,
  556. ASN1_PKEY_ALIAS},
  557. {
  558. EVP_PKEY_DSA3,
  559. EVP_PKEY_DSA,
  560. ASN1_PKEY_ALIAS},
  561. {
  562. EVP_PKEY_DSA,
  563. EVP_PKEY_DSA,
  564. 0,
  565. "DSA",
  566. "OpenSSL DSA method",
  567. dsa_pub_decode,
  568. dsa_pub_encode,
  569. dsa_pub_cmp,
  570. dsa_pub_print,
  571. dsa_priv_decode,
  572. dsa_priv_encode,
  573. dsa_priv_print,
  574. int_dsa_size,
  575. dsa_bits,
  576. dsa_param_decode,
  577. dsa_param_encode,
  578. dsa_missing_parameters,
  579. dsa_copy_parameters,
  580. dsa_cmp_parameters,
  581. dsa_param_print,
  582. dsa_sig_print,
  583. int_dsa_free,
  584. dsa_pkey_ctrl,
  585. old_dsa_priv_decode,
  586. old_dsa_priv_encode}
  587. };