dsa_ameth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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 "asn1_locl.h"
  63. static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  64. {
  65. const unsigned char *p, *pm;
  66. int pklen, pmlen;
  67. int ptype;
  68. void *pval;
  69. ASN1_STRING *pstr;
  70. X509_ALGOR *palg;
  71. ASN1_INTEGER *public_key = NULL;
  72. DSA *dsa = NULL;
  73. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  74. return 0;
  75. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  76. if (ptype == V_ASN1_SEQUENCE)
  77. {
  78. pstr = pval;
  79. pm = pstr->data;
  80. pmlen = pstr->length;
  81. if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen)))
  82. {
  83. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
  84. goto err;
  85. }
  86. }
  87. else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF))
  88. {
  89. if (!(dsa = DSA_new()))
  90. {
  91. DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
  92. goto err;
  93. }
  94. }
  95. else
  96. {
  97. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
  98. goto err;
  99. }
  100. if (!(public_key=d2i_ASN1_INTEGER(NULL, &p, pklen)))
  101. {
  102. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
  103. goto err;
  104. }
  105. if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)))
  106. {
  107. DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
  108. goto err;
  109. }
  110. ASN1_INTEGER_free(public_key);
  111. EVP_PKEY_assign_DSA(pkey, dsa);
  112. return 1;
  113. err:
  114. if (pubkey)
  115. ASN1_INTEGER_free(public_key);
  116. if (dsa)
  117. DSA_free(dsa);
  118. return 0;
  119. }
  120. static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  121. {
  122. DSA *dsa;
  123. void *pval = NULL;
  124. int ptype;
  125. unsigned char *penc = NULL;
  126. int penclen;
  127. dsa=pkey->pkey.dsa;
  128. if (pkey->save_parameters && dsa->p && dsa->q && dsa->g)
  129. {
  130. ASN1_STRING *str;
  131. str = ASN1_STRING_new();
  132. str->length = i2d_DSAparams(dsa, &str->data);
  133. if (str->length <= 0)
  134. {
  135. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  136. goto err;
  137. }
  138. pval = str;
  139. ptype = V_ASN1_SEQUENCE;
  140. }
  141. else
  142. ptype = V_ASN1_UNDEF;
  143. dsa->write_params=0;
  144. penclen = i2d_DSAPublicKey(dsa, &penc);
  145. if (penclen <= 0)
  146. {
  147. DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  148. goto err;
  149. }
  150. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA),
  151. ptype, pval, penc, penclen))
  152. return 1;
  153. err:
  154. if (penc)
  155. OPENSSL_free(penc);
  156. if (pval)
  157. ASN1_STRING_free(pval);
  158. return 0;
  159. }
  160. /* In PKCS#8 DSA: you just get a private key integer and parameters in the
  161. * AlgorithmIdentifier the pubkey must be recalculated.
  162. */
  163. static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  164. {
  165. const unsigned char *p, *pm;
  166. int pklen, pmlen;
  167. int ptype;
  168. void *pval;
  169. ASN1_STRING *pstr;
  170. X509_ALGOR *palg;
  171. ASN1_INTEGER *privkey = NULL;
  172. BN_CTX *ctx = NULL;
  173. STACK_OF(ASN1_TYPE) *ndsa = NULL;
  174. DSA *dsa = NULL;
  175. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  176. return 0;
  177. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  178. /* Check for broken DSA PKCS#8, UGH! */
  179. if (*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED))
  180. {
  181. ASN1_TYPE *t1, *t2;
  182. if(!(ndsa = ASN1_seq_unpack_ASN1_TYPE(p, pklen,
  183. d2i_ASN1_TYPE,
  184. ASN1_TYPE_free)))
  185. goto decerr;
  186. if (sk_ASN1_TYPE_num(ndsa) != 2)
  187. goto decerr;
  188. /* Handle Two broken types:
  189. * SEQUENCE {parameters, priv_key}
  190. * SEQUENCE {pub_key, priv_key}
  191. */
  192. t1 = sk_ASN1_TYPE_value(ndsa, 0);
  193. t2 = sk_ASN1_TYPE_value(ndsa, 1);
  194. if (t1->type == V_ASN1_SEQUENCE)
  195. {
  196. p8->broken = PKCS8_EMBEDDED_PARAM;
  197. pval = t1->value.ptr;
  198. }
  199. else if (ptype == V_ASN1_SEQUENCE)
  200. p8->broken = PKCS8_NS_DB;
  201. else
  202. goto decerr;
  203. if (t2->type != V_ASN1_INTEGER)
  204. goto decerr;
  205. privkey = t2->value.integer;
  206. }
  207. else
  208. {
  209. if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen)))
  210. goto decerr;
  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. {
  222. DSAerr(DSA_F_DSA_PRIV_DECODE,DSA_R_BN_ERROR);
  223. goto dsaerr;
  224. }
  225. /* Calculate public key */
  226. if (!(dsa->pub_key = BN_new()))
  227. {
  228. DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
  229. goto dsaerr;
  230. }
  231. if (!(ctx = BN_CTX_new()))
  232. {
  233. DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
  234. goto dsaerr;
  235. }
  236. if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx))
  237. {
  238. DSAerr(DSA_F_DSA_PRIV_DECODE,DSA_R_BN_ERROR);
  239. goto dsaerr;
  240. }
  241. EVP_PKEY_assign_DSA(pkey, dsa);
  242. BN_CTX_free (ctx);
  243. if(ndsa)
  244. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  245. else
  246. ASN1_INTEGER_free(privkey);
  247. return 1;
  248. decerr:
  249. DSAerr(DSA_F_DSA_PRIV_DECODE, EVP_R_DECODE_ERROR);
  250. dsaerr:
  251. BN_CTX_free (ctx);
  252. if (privkey)
  253. ASN1_INTEGER_free(privkey);
  254. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  255. DSA_free(dsa);
  256. return 0;
  257. }
  258. static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  259. {
  260. ASN1_STRING *params = NULL;
  261. ASN1_INTEGER *prkey = NULL;
  262. unsigned char *dp = NULL;
  263. int dplen;
  264. params = ASN1_STRING_new();
  265. if (!params)
  266. {
  267. DSAerr(DSA_F_DSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
  268. goto err;
  269. }
  270. params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
  271. if (params->length <= 0)
  272. {
  273. DSAerr(DSA_F_DSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
  274. goto err;
  275. }
  276. params->type = V_ASN1_SEQUENCE;
  277. /* Get private key into integer */
  278. prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
  279. if (!prkey)
  280. {
  281. DSAerr(DSA_F_DSA_PRIV_ENCODE,DSA_R_BN_ERROR);
  282. goto err;
  283. }
  284. dplen = i2d_ASN1_INTEGER(prkey, &dp);
  285. ASN1_INTEGER_free(prkey);
  286. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
  287. V_ASN1_SEQUENCE, params, dp, dplen))
  288. goto err;
  289. return 1;
  290. err:
  291. if (dp != NULL)
  292. OPENSSL_free(dp);
  293. if (params != NULL)
  294. ASN1_STRING_free(params);
  295. if (prkey != NULL)
  296. ASN1_INTEGER_free(prkey);
  297. return 0;
  298. }
  299. static int int_dsa_size(const EVP_PKEY *pkey)
  300. {
  301. return(DSA_size(pkey->pkey.dsa));
  302. }
  303. static int dsa_bits(const EVP_PKEY *pkey)
  304. {
  305. return BN_num_bits(pkey->pkey.dsa->p);
  306. }
  307. static int dsa_missing_parameters(const EVP_PKEY *pkey)
  308. {
  309. DSA *dsa;
  310. dsa=pkey->pkey.dsa;
  311. if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
  312. return 1;
  313. return 0;
  314. }
  315. static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  316. {
  317. BIGNUM *a;
  318. if ((a=BN_dup(from->pkey.dsa->p)) == NULL)
  319. return 0;
  320. if (to->pkey.dsa->p != NULL)
  321. BN_free(to->pkey.dsa->p);
  322. to->pkey.dsa->p=a;
  323. if ((a=BN_dup(from->pkey.dsa->q)) == NULL)
  324. return 0;
  325. if (to->pkey.dsa->q != NULL)
  326. BN_free(to->pkey.dsa->q);
  327. to->pkey.dsa->q=a;
  328. if ((a=BN_dup(from->pkey.dsa->g)) == NULL)
  329. return 0;
  330. if (to->pkey.dsa->g != NULL)
  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. 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=(unsigned char *)OPENSSL_malloc(buf_len+10);
  390. if (m == NULL)
  391. {
  392. DSAerr(DSA_F_DO_DSA_PRINT,ERR_R_MALLOC_FAILURE);
  393. goto err;
  394. }
  395. if (priv_key)
  396. {
  397. if(!BIO_indent(bp,off,128))
  398. goto err;
  399. if (BIO_printf(bp,"%s: (%d bit)\n",ktype, BN_num_bits(x->p))
  400. <= 0) goto err;
  401. }
  402. if (!ASN1_bn_print(bp,"priv:",priv_key,m,off))
  403. goto err;
  404. if (!ASN1_bn_print(bp,"pub: ",pub_key,m,off))
  405. goto err;
  406. if (!ASN1_bn_print(bp,"P: ",x->p,m,off)) goto err;
  407. if (!ASN1_bn_print(bp,"Q: ",x->q,m,off)) goto err;
  408. if (!ASN1_bn_print(bp,"G: ",x->g,m,off)) goto err;
  409. ret=1;
  410. err:
  411. if (m != NULL) 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. {
  420. DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
  421. return 0;
  422. }
  423. EVP_PKEY_assign_DSA(pkey, dsa);
  424. return 1;
  425. }
  426. static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  427. {
  428. return i2d_DSAparams(pkey->pkey.dsa, pder);
  429. }
  430. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  431. ASN1_PCTX *ctx)
  432. {
  433. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  434. }
  435. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  436. ASN1_PCTX *ctx)
  437. {
  438. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  439. }
  440. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  441. ASN1_PCTX *ctx)
  442. {
  443. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  444. }
  445. static int old_dsa_priv_decode(EVP_PKEY *pkey,
  446. const unsigned char **pder, int derlen)
  447. {
  448. DSA *dsa;
  449. if (!(dsa = d2i_DSAPrivateKey (NULL, pder, derlen)))
  450. {
  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_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  462. {
  463. switch (op)
  464. {
  465. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  466. if (arg1 == 0)
  467. {
  468. X509_ALGOR *alg1, *alg2;
  469. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
  470. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_sha1),
  471. V_ASN1_NULL, 0);
  472. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_dsaWithSHA1),
  473. V_ASN1_UNDEF, 0);
  474. }
  475. return 1;
  476. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  477. *(int *)arg2 = NID_sha1;
  478. return 2;
  479. default:
  480. return -2;
  481. }
  482. }
  483. /* NB these are sorted in pkey_id order, lowest first */
  484. const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] =
  485. {
  486. {
  487. EVP_PKEY_DSA2,
  488. EVP_PKEY_DSA,
  489. ASN1_PKEY_ALIAS
  490. },
  491. {
  492. EVP_PKEY_DSA1,
  493. EVP_PKEY_DSA,
  494. ASN1_PKEY_ALIAS
  495. },
  496. {
  497. EVP_PKEY_DSA4,
  498. EVP_PKEY_DSA,
  499. ASN1_PKEY_ALIAS
  500. },
  501. {
  502. EVP_PKEY_DSA3,
  503. EVP_PKEY_DSA,
  504. ASN1_PKEY_ALIAS
  505. },
  506. {
  507. EVP_PKEY_DSA,
  508. EVP_PKEY_DSA,
  509. 0,
  510. "DSA",
  511. "OpenSSL DSA method",
  512. dsa_pub_decode,
  513. dsa_pub_encode,
  514. dsa_pub_cmp,
  515. dsa_pub_print,
  516. dsa_priv_decode,
  517. dsa_priv_encode,
  518. dsa_priv_print,
  519. int_dsa_size,
  520. dsa_bits,
  521. dsa_param_decode,
  522. dsa_param_encode,
  523. dsa_missing_parameters,
  524. dsa_copy_parameters,
  525. dsa_cmp_parameters,
  526. dsa_param_print,
  527. int_dsa_free,
  528. dsa_pkey_ctrl,
  529. old_dsa_priv_decode,
  530. old_dsa_priv_encode
  531. }
  532. };