dsa_ameth.c 18 KB

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