rsa_ameth.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /* crypto/rsa/rsa_ameth.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project 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/asn1t.h>
  61. #include <openssl/x509.h>
  62. #include <openssl/rsa.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 rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  69. {
  70. unsigned char *penc = NULL;
  71. int penclen;
  72. penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
  73. if (penclen <= 0)
  74. return 0;
  75. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_RSA),
  76. V_ASN1_NULL, NULL, penc, penclen))
  77. return 1;
  78. OPENSSL_free(penc);
  79. return 0;
  80. }
  81. static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  82. {
  83. const unsigned char *p;
  84. int pklen;
  85. RSA *rsa = NULL;
  86. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, NULL, pubkey))
  87. return 0;
  88. if (!(rsa = d2i_RSAPublicKey(NULL, &p, pklen)))
  89. {
  90. RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
  91. return 0;
  92. }
  93. EVP_PKEY_assign_RSA (pkey, rsa);
  94. return 1;
  95. }
  96. static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  97. {
  98. if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0
  99. || BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0)
  100. return 0;
  101. return 1;
  102. }
  103. static int old_rsa_priv_decode(EVP_PKEY *pkey,
  104. const unsigned char **pder, int derlen)
  105. {
  106. RSA *rsa;
  107. if (!(rsa = d2i_RSAPrivateKey (NULL, pder, derlen)))
  108. {
  109. RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  110. return 0;
  111. }
  112. EVP_PKEY_assign_RSA(pkey, rsa);
  113. return 1;
  114. }
  115. static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  116. {
  117. return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
  118. }
  119. static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  120. {
  121. unsigned char *rk = NULL;
  122. int rklen;
  123. rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
  124. if (rklen <= 0)
  125. {
  126. RSAerr(RSA_F_RSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
  127. return 0;
  128. }
  129. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_rsaEncryption), 0,
  130. V_ASN1_NULL, NULL, rk, rklen))
  131. {
  132. RSAerr(RSA_F_RSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
  133. return 0;
  134. }
  135. return 1;
  136. }
  137. static int rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  138. {
  139. const unsigned char *p;
  140. int pklen;
  141. if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
  142. return 0;
  143. return old_rsa_priv_decode(pkey, &p, pklen);
  144. }
  145. static int int_rsa_size(const EVP_PKEY *pkey)
  146. {
  147. return RSA_size(pkey->pkey.rsa);
  148. }
  149. static int rsa_bits(const EVP_PKEY *pkey)
  150. {
  151. return BN_num_bits(pkey->pkey.rsa->n);
  152. }
  153. static void int_rsa_free(EVP_PKEY *pkey)
  154. {
  155. RSA_free(pkey->pkey.rsa);
  156. }
  157. static void update_buflen(const BIGNUM *b, size_t *pbuflen)
  158. {
  159. size_t i;
  160. if (!b)
  161. return;
  162. if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
  163. *pbuflen = i;
  164. }
  165. static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv)
  166. {
  167. char *str;
  168. const char *s;
  169. unsigned char *m=NULL;
  170. int ret=0, mod_len = 0;
  171. size_t buf_len=0;
  172. update_buflen(x->n, &buf_len);
  173. update_buflen(x->e, &buf_len);
  174. if (priv)
  175. {
  176. update_buflen(x->d, &buf_len);
  177. update_buflen(x->p, &buf_len);
  178. update_buflen(x->q, &buf_len);
  179. update_buflen(x->dmp1, &buf_len);
  180. update_buflen(x->dmq1, &buf_len);
  181. update_buflen(x->iqmp, &buf_len);
  182. }
  183. m=(unsigned char *)OPENSSL_malloc(buf_len+10);
  184. if (m == NULL)
  185. {
  186. RSAerr(RSA_F_DO_RSA_PRINT,ERR_R_MALLOC_FAILURE);
  187. goto err;
  188. }
  189. if (x->n != NULL)
  190. mod_len = BN_num_bits(x->n);
  191. if(!BIO_indent(bp,off,128))
  192. goto err;
  193. if (priv && x->d)
  194. {
  195. if (BIO_printf(bp,"Private-Key: (%d bit)\n", mod_len)
  196. <= 0) goto err;
  197. str = "modulus:";
  198. s = "publicExponent:";
  199. }
  200. else
  201. {
  202. if (BIO_printf(bp,"Public-Key: (%d bit)\n", mod_len)
  203. <= 0) goto err;
  204. str = "Modulus:";
  205. s= "Exponent:";
  206. }
  207. if (!ASN1_bn_print(bp,str,x->n,m,off)) goto err;
  208. if (!ASN1_bn_print(bp,s,x->e,m,off))
  209. goto err;
  210. if (priv)
  211. {
  212. if (!ASN1_bn_print(bp,"privateExponent:",x->d,m,off))
  213. goto err;
  214. if (!ASN1_bn_print(bp,"prime1:",x->p,m,off))
  215. goto err;
  216. if (!ASN1_bn_print(bp,"prime2:",x->q,m,off))
  217. goto err;
  218. if (!ASN1_bn_print(bp,"exponent1:",x->dmp1,m,off))
  219. goto err;
  220. if (!ASN1_bn_print(bp,"exponent2:",x->dmq1,m,off))
  221. goto err;
  222. if (!ASN1_bn_print(bp,"coefficient:",x->iqmp,m,off))
  223. goto err;
  224. }
  225. ret=1;
  226. err:
  227. if (m != NULL) OPENSSL_free(m);
  228. return(ret);
  229. }
  230. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  231. ASN1_PCTX *ctx)
  232. {
  233. return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
  234. }
  235. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  236. ASN1_PCTX *ctx)
  237. {
  238. return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
  239. }
  240. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg,
  241. X509_ALGOR **pmaskHash)
  242. {
  243. const unsigned char *p;
  244. int plen;
  245. RSA_PSS_PARAMS *pss;
  246. *pmaskHash = NULL;
  247. if (!alg->parameter || alg->parameter->type != V_ASN1_SEQUENCE)
  248. return NULL;
  249. p = alg->parameter->value.sequence->data;
  250. plen = alg->parameter->value.sequence->length;
  251. pss = d2i_RSA_PSS_PARAMS(NULL, &p, plen);
  252. if (!pss)
  253. return NULL;
  254. if (pss->maskGenAlgorithm)
  255. {
  256. ASN1_TYPE *param = pss->maskGenAlgorithm->parameter;
  257. if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) == NID_mgf1
  258. && param->type == V_ASN1_SEQUENCE)
  259. {
  260. p = param->value.sequence->data;
  261. plen = param->value.sequence->length;
  262. *pmaskHash = d2i_X509_ALGOR(NULL, &p, plen);
  263. }
  264. }
  265. return pss;
  266. }
  267. static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss,
  268. X509_ALGOR *maskHash, int indent)
  269. {
  270. int rv = 0;
  271. if (!pss)
  272. {
  273. if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0)
  274. return 0;
  275. return 1;
  276. }
  277. if (BIO_puts(bp, "\n") <= 0)
  278. goto err;
  279. if (!BIO_indent(bp, indent, 128))
  280. goto err;
  281. if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
  282. goto err;
  283. if (pss->hashAlgorithm)
  284. {
  285. if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
  286. goto err;
  287. }
  288. else if (BIO_puts(bp, "sha1 (default)") <= 0)
  289. goto err;
  290. if (BIO_puts(bp, "\n") <= 0)
  291. goto err;
  292. if (!BIO_indent(bp, indent, 128))
  293. goto err;
  294. if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
  295. goto err;
  296. if (pss->maskGenAlgorithm)
  297. {
  298. if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
  299. goto err;
  300. if (BIO_puts(bp, " with ") <= 0)
  301. goto err;
  302. if (maskHash)
  303. {
  304. if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
  305. goto err;
  306. }
  307. else if (BIO_puts(bp, "INVALID") <= 0)
  308. goto err;
  309. }
  310. else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0)
  311. goto err;
  312. BIO_puts(bp, "\n");
  313. if (!BIO_indent(bp, indent, 128))
  314. goto err;
  315. if (BIO_puts(bp, "Salt Length: ") <= 0)
  316. goto err;
  317. if (pss->saltLength)
  318. {
  319. if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
  320. goto err;
  321. }
  322. else if (BIO_puts(bp, "20 (default)") <= 0)
  323. goto err;
  324. BIO_puts(bp, "\n");
  325. if (!BIO_indent(bp, indent, 128))
  326. goto err;
  327. if (BIO_puts(bp, "Trailer Field: ") <= 0)
  328. goto err;
  329. if (pss->trailerField)
  330. {
  331. if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
  332. goto err;
  333. }
  334. else if (BIO_puts(bp, "0xbc (default)") <= 0)
  335. goto err;
  336. BIO_puts(bp, "\n");
  337. rv = 1;
  338. err:
  339. return rv;
  340. }
  341. static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  342. const ASN1_STRING *sig,
  343. int indent, ASN1_PCTX *pctx)
  344. {
  345. if (OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss)
  346. {
  347. int rv;
  348. RSA_PSS_PARAMS *pss;
  349. X509_ALGOR *maskHash;
  350. pss = rsa_pss_decode(sigalg, &maskHash);
  351. rv = rsa_pss_param_print(bp, pss, maskHash, indent);
  352. if (pss)
  353. RSA_PSS_PARAMS_free(pss);
  354. if (maskHash)
  355. X509_ALGOR_free(maskHash);
  356. if (!rv)
  357. return 0;
  358. }
  359. else if (!sig && BIO_puts(bp, "\n") <= 0)
  360. return 0;
  361. if (sig)
  362. return X509_signature_dump(bp, sig, indent);
  363. return 1;
  364. }
  365. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  366. {
  367. X509_ALGOR *alg = NULL;
  368. switch (op)
  369. {
  370. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  371. if (arg1 == 0)
  372. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
  373. break;
  374. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  375. if (arg1 == 0)
  376. PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
  377. break;
  378. #ifndef OPENSSL_NO_CMS
  379. case ASN1_PKEY_CTRL_CMS_SIGN:
  380. if (arg1 == 0)
  381. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, NULL, &alg);
  382. break;
  383. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  384. if (arg1 == 0)
  385. CMS_RecipientInfo_ktri_get0_algs(arg2, NULL, NULL, &alg);
  386. break;
  387. #endif
  388. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  389. *(int *)arg2 = NID_sha1;
  390. return 1;
  391. default:
  392. return -2;
  393. }
  394. if (alg)
  395. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
  396. V_ASN1_NULL, 0);
  397. return 1;
  398. }
  399. /* Customised RSA item verification routine. This is called
  400. * when a signature is encountered requiring special handling. We
  401. * currently only handle PSS.
  402. */
  403. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  404. X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
  405. EVP_PKEY *pkey)
  406. {
  407. int rv = -1;
  408. int saltlen;
  409. const EVP_MD *mgf1md = NULL, *md = NULL;
  410. RSA_PSS_PARAMS *pss;
  411. X509_ALGOR *maskHash;
  412. EVP_PKEY_CTX *pkctx;
  413. /* Sanity check: make sure it is PSS */
  414. if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss)
  415. {
  416. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  417. return -1;
  418. }
  419. /* Decode PSS parameters */
  420. pss = rsa_pss_decode(sigalg, &maskHash);
  421. if (pss == NULL)
  422. {
  423. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_PSS_PARAMETERS);
  424. goto err;
  425. }
  426. /* Check mask and lookup mask hash algorithm */
  427. if (pss->maskGenAlgorithm)
  428. {
  429. if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) != NID_mgf1)
  430. {
  431. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_MASK_ALGORITHM);
  432. goto err;
  433. }
  434. if (!maskHash)
  435. {
  436. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_MASK_PARAMETER);
  437. goto err;
  438. }
  439. mgf1md = EVP_get_digestbyobj(maskHash->algorithm);
  440. if (mgf1md == NULL)
  441. {
  442. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNKNOWN_MASK_DIGEST);
  443. goto err;
  444. }
  445. }
  446. else
  447. mgf1md = EVP_sha1();
  448. if (pss->hashAlgorithm)
  449. {
  450. md = EVP_get_digestbyobj(pss->hashAlgorithm->algorithm);
  451. if (md == NULL)
  452. {
  453. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNKNOWN_PSS_DIGEST);
  454. goto err;
  455. }
  456. }
  457. else
  458. md = EVP_sha1();
  459. if (pss->saltLength)
  460. {
  461. saltlen = ASN1_INTEGER_get(pss->saltLength);
  462. /* Could perform more salt length sanity checks but the main
  463. * RSA routines will trap other invalid values anyway.
  464. */
  465. if (saltlen < 0)
  466. {
  467. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_SALT_LENGTH);
  468. goto err;
  469. }
  470. }
  471. else
  472. saltlen = 20;
  473. /* low-level routines support only trailer field 0xbc (value 1)
  474. * and PKCS#1 says we should reject any other value anyway.
  475. */
  476. if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1)
  477. {
  478. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_TRAILER);
  479. goto err;
  480. }
  481. /* We have all parameters now set up context */
  482. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
  483. goto err;
  484. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
  485. goto err;
  486. if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
  487. goto err;
  488. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  489. goto err;
  490. /* Carry on */
  491. rv = 2;
  492. err:
  493. RSA_PSS_PARAMS_free(pss);
  494. if (maskHash)
  495. X509_ALGOR_free(maskHash);
  496. return rv;
  497. }
  498. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  499. X509_ALGOR *alg1, X509_ALGOR *alg2,
  500. ASN1_BIT_STRING *sig)
  501. {
  502. int pad_mode;
  503. EVP_PKEY_CTX *pkctx = ctx->pctx;
  504. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  505. return 0;
  506. if (pad_mode == RSA_PKCS1_PADDING)
  507. return 2;
  508. if (pad_mode == RSA_PKCS1_PSS_PADDING)
  509. {
  510. const EVP_MD *sigmd, *mgf1md;
  511. RSA_PSS_PARAMS *pss = NULL;
  512. X509_ALGOR *mgf1alg = NULL;
  513. ASN1_STRING *os1 = NULL, *os2 = NULL;
  514. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  515. int saltlen, rv = 0;
  516. sigmd = EVP_MD_CTX_md(ctx);
  517. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  518. goto err;
  519. if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
  520. goto err;
  521. if (saltlen == -1)
  522. saltlen = EVP_MD_size(sigmd);
  523. else if (saltlen == -2)
  524. {
  525. saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
  526. if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0)
  527. saltlen--;
  528. }
  529. pss = RSA_PSS_PARAMS_new();
  530. if (!pss)
  531. goto err;
  532. if (saltlen != 20)
  533. {
  534. pss->saltLength = ASN1_INTEGER_new();
  535. if (!pss->saltLength)
  536. goto err;
  537. if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
  538. goto err;
  539. }
  540. if (EVP_MD_type(sigmd) != NID_sha1)
  541. {
  542. pss->hashAlgorithm = X509_ALGOR_new();
  543. if (!pss->hashAlgorithm)
  544. goto err;
  545. X509_ALGOR_set_md(pss->hashAlgorithm, sigmd);
  546. }
  547. if (EVP_MD_type(mgf1md) != NID_sha1)
  548. {
  549. ASN1_STRING *stmp = NULL;
  550. /* need to embed algorithm ID inside another */
  551. mgf1alg = X509_ALGOR_new();
  552. X509_ALGOR_set_md(mgf1alg, mgf1md);
  553. if (!ASN1_item_pack(mgf1alg, ASN1_ITEM_rptr(X509_ALGOR),
  554. &stmp))
  555. goto err;
  556. pss->maskGenAlgorithm = X509_ALGOR_new();
  557. if (!pss->maskGenAlgorithm)
  558. goto err;
  559. X509_ALGOR_set0(pss->maskGenAlgorithm,
  560. OBJ_nid2obj(NID_mgf1),
  561. V_ASN1_SEQUENCE, stmp);
  562. }
  563. /* Finally create string with pss parameter encoding. */
  564. if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os1))
  565. goto err;
  566. if (alg2)
  567. {
  568. os2 = ASN1_STRING_dup(os1);
  569. if (!os2)
  570. goto err;
  571. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_rsassaPss),
  572. V_ASN1_SEQUENCE, os2);
  573. }
  574. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_rsassaPss),
  575. V_ASN1_SEQUENCE, os1);
  576. os1 = os2 = NULL;
  577. rv = 3;
  578. err:
  579. if (mgf1alg)
  580. X509_ALGOR_free(mgf1alg);
  581. if (pss)
  582. RSA_PSS_PARAMS_free(pss);
  583. if (os1)
  584. ASN1_STRING_free(os1);
  585. return rv;
  586. }
  587. return 2;
  588. }
  589. const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] =
  590. {
  591. {
  592. EVP_PKEY_RSA,
  593. EVP_PKEY_RSA,
  594. ASN1_PKEY_SIGPARAM_NULL,
  595. "RSA",
  596. "OpenSSL RSA method",
  597. rsa_pub_decode,
  598. rsa_pub_encode,
  599. rsa_pub_cmp,
  600. rsa_pub_print,
  601. rsa_priv_decode,
  602. rsa_priv_encode,
  603. rsa_priv_print,
  604. int_rsa_size,
  605. rsa_bits,
  606. 0,0,0,0,0,0,
  607. rsa_sig_print,
  608. int_rsa_free,
  609. rsa_pkey_ctrl,
  610. old_rsa_priv_decode,
  611. old_rsa_priv_encode,
  612. rsa_item_verify,
  613. rsa_item_sign
  614. },
  615. {
  616. EVP_PKEY_RSA2,
  617. EVP_PKEY_RSA,
  618. ASN1_PKEY_ALIAS
  619. }
  620. };