rsa_ameth.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /* crypto/rsa/rsa_ameth.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
  4. * 2006.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/asn1t.h>
  62. #include <openssl/x509.h>
  63. #include <openssl/rsa.h>
  64. #include <openssl/bn.h>
  65. #ifndef OPENSSL_NO_CMS
  66. # include <openssl/cms.h>
  67. #endif
  68. #include "asn1_locl.h"
  69. static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  70. {
  71. unsigned char *penc = NULL;
  72. int penclen;
  73. penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
  74. if (penclen <= 0)
  75. return 0;
  76. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_RSA),
  77. V_ASN1_NULL, NULL, penc, penclen))
  78. return 1;
  79. OPENSSL_free(penc);
  80. return 0;
  81. }
  82. static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  83. {
  84. const unsigned char *p;
  85. int pklen;
  86. RSA *rsa = NULL;
  87. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, NULL, pubkey))
  88. return 0;
  89. if (!(rsa = d2i_RSAPublicKey(NULL, &p, pklen))) {
  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. RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  109. return 0;
  110. }
  111. EVP_PKEY_assign_RSA(pkey, rsa);
  112. return 1;
  113. }
  114. static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  115. {
  116. return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
  117. }
  118. static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  119. {
  120. unsigned char *rk = NULL;
  121. int rklen;
  122. rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
  123. if (rklen <= 0) {
  124. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  125. return 0;
  126. }
  127. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_rsaEncryption), 0,
  128. V_ASN1_NULL, NULL, rk, rklen)) {
  129. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  130. return 0;
  131. }
  132. return 1;
  133. }
  134. static int rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  135. {
  136. const unsigned char *p;
  137. int pklen;
  138. if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
  139. return 0;
  140. return old_rsa_priv_decode(pkey, &p, pklen);
  141. }
  142. static int int_rsa_size(const EVP_PKEY *pkey)
  143. {
  144. return RSA_size(pkey->pkey.rsa);
  145. }
  146. static int rsa_bits(const EVP_PKEY *pkey)
  147. {
  148. return BN_num_bits(pkey->pkey.rsa->n);
  149. }
  150. static void int_rsa_free(EVP_PKEY *pkey)
  151. {
  152. RSA_free(pkey->pkey.rsa);
  153. }
  154. static void update_buflen(const BIGNUM *b, size_t *pbuflen)
  155. {
  156. size_t i;
  157. if (!b)
  158. return;
  159. if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
  160. *pbuflen = i;
  161. }
  162. static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv)
  163. {
  164. char *str;
  165. const char *s;
  166. unsigned char *m = NULL;
  167. int ret = 0, mod_len = 0;
  168. size_t buf_len = 0;
  169. update_buflen(x->n, &buf_len);
  170. update_buflen(x->e, &buf_len);
  171. if (priv) {
  172. update_buflen(x->d, &buf_len);
  173. update_buflen(x->p, &buf_len);
  174. update_buflen(x->q, &buf_len);
  175. update_buflen(x->dmp1, &buf_len);
  176. update_buflen(x->dmq1, &buf_len);
  177. update_buflen(x->iqmp, &buf_len);
  178. }
  179. m = (unsigned char *)OPENSSL_malloc(buf_len + 10);
  180. if (m == NULL) {
  181. RSAerr(RSA_F_DO_RSA_PRINT, ERR_R_MALLOC_FAILURE);
  182. goto err;
  183. }
  184. if (x->n != NULL)
  185. mod_len = BN_num_bits(x->n);
  186. if (!BIO_indent(bp, off, 128))
  187. goto err;
  188. if (priv && x->d) {
  189. if (BIO_printf(bp, "Private-Key: (%d bit)\n", mod_len)
  190. <= 0)
  191. goto err;
  192. str = "modulus:";
  193. s = "publicExponent:";
  194. } else {
  195. if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len)
  196. <= 0)
  197. goto err;
  198. str = "Modulus:";
  199. s = "Exponent:";
  200. }
  201. if (!ASN1_bn_print(bp, str, x->n, m, off))
  202. goto err;
  203. if (!ASN1_bn_print(bp, s, x->e, m, off))
  204. goto err;
  205. if (priv) {
  206. if (!ASN1_bn_print(bp, "privateExponent:", x->d, m, off))
  207. goto err;
  208. if (!ASN1_bn_print(bp, "prime1:", x->p, m, off))
  209. goto err;
  210. if (!ASN1_bn_print(bp, "prime2:", x->q, m, off))
  211. goto err;
  212. if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, m, off))
  213. goto err;
  214. if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, m, off))
  215. goto err;
  216. if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, m, off))
  217. goto err;
  218. }
  219. ret = 1;
  220. err:
  221. if (m != NULL)
  222. OPENSSL_free(m);
  223. return (ret);
  224. }
  225. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  226. ASN1_PCTX *ctx)
  227. {
  228. return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
  229. }
  230. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  231. ASN1_PCTX *ctx)
  232. {
  233. return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
  234. }
  235. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg,
  236. X509_ALGOR **pmaskHash)
  237. {
  238. const unsigned char *p;
  239. int plen;
  240. RSA_PSS_PARAMS *pss;
  241. *pmaskHash = NULL;
  242. if (!alg->parameter || alg->parameter->type != V_ASN1_SEQUENCE)
  243. return NULL;
  244. p = alg->parameter->value.sequence->data;
  245. plen = alg->parameter->value.sequence->length;
  246. pss = d2i_RSA_PSS_PARAMS(NULL, &p, plen);
  247. if (!pss)
  248. return NULL;
  249. if (pss->maskGenAlgorithm) {
  250. ASN1_TYPE *param = pss->maskGenAlgorithm->parameter;
  251. if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) == NID_mgf1
  252. && param->type == V_ASN1_SEQUENCE) {
  253. p = param->value.sequence->data;
  254. plen = param->value.sequence->length;
  255. *pmaskHash = d2i_X509_ALGOR(NULL, &p, plen);
  256. }
  257. }
  258. return pss;
  259. }
  260. static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss,
  261. X509_ALGOR *maskHash, int indent)
  262. {
  263. int rv = 0;
  264. if (!pss) {
  265. if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0)
  266. return 0;
  267. return 1;
  268. }
  269. if (BIO_puts(bp, "\n") <= 0)
  270. goto err;
  271. if (!BIO_indent(bp, indent, 128))
  272. goto err;
  273. if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
  274. goto err;
  275. if (pss->hashAlgorithm) {
  276. if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
  277. goto err;
  278. } else if (BIO_puts(bp, "sha1 (default)") <= 0)
  279. goto err;
  280. if (BIO_puts(bp, "\n") <= 0)
  281. goto err;
  282. if (!BIO_indent(bp, indent, 128))
  283. goto err;
  284. if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
  285. goto err;
  286. if (pss->maskGenAlgorithm) {
  287. if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
  288. goto err;
  289. if (BIO_puts(bp, " with ") <= 0)
  290. goto err;
  291. if (maskHash) {
  292. if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
  293. goto err;
  294. } else if (BIO_puts(bp, "INVALID") <= 0)
  295. goto err;
  296. } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0)
  297. goto err;
  298. BIO_puts(bp, "\n");
  299. if (!BIO_indent(bp, indent, 128))
  300. goto err;
  301. if (BIO_puts(bp, "Salt Length: 0x") <= 0)
  302. goto err;
  303. if (pss->saltLength) {
  304. if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
  305. goto err;
  306. } else if (BIO_puts(bp, "14 (default)") <= 0)
  307. goto err;
  308. BIO_puts(bp, "\n");
  309. if (!BIO_indent(bp, indent, 128))
  310. goto err;
  311. if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
  312. goto err;
  313. if (pss->trailerField) {
  314. if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
  315. goto err;
  316. } else if (BIO_puts(bp, "BC (default)") <= 0)
  317. goto err;
  318. BIO_puts(bp, "\n");
  319. rv = 1;
  320. err:
  321. return rv;
  322. }
  323. static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  324. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  325. {
  326. if (OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss) {
  327. int rv;
  328. RSA_PSS_PARAMS *pss;
  329. X509_ALGOR *maskHash;
  330. pss = rsa_pss_decode(sigalg, &maskHash);
  331. rv = rsa_pss_param_print(bp, pss, maskHash, indent);
  332. if (pss)
  333. RSA_PSS_PARAMS_free(pss);
  334. if (maskHash)
  335. X509_ALGOR_free(maskHash);
  336. if (!rv)
  337. return 0;
  338. } else if (!sig && BIO_puts(bp, "\n") <= 0)
  339. return 0;
  340. if (sig)
  341. return X509_signature_dump(bp, sig, indent);
  342. return 1;
  343. }
  344. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  345. {
  346. X509_ALGOR *alg = NULL;
  347. switch (op) {
  348. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  349. if (arg1 == 0)
  350. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
  351. break;
  352. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  353. if (arg1 == 0)
  354. PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
  355. break;
  356. #ifndef OPENSSL_NO_CMS
  357. case ASN1_PKEY_CTRL_CMS_SIGN:
  358. if (arg1 == 0)
  359. CMS_SignerInfo_get0_algs(arg2, NULL, NULL, NULL, &alg);
  360. break;
  361. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  362. if (arg1 == 0)
  363. CMS_RecipientInfo_ktri_get0_algs(arg2, NULL, NULL, &alg);
  364. break;
  365. #endif
  366. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  367. *(int *)arg2 = NID_sha1;
  368. return 1;
  369. default:
  370. return -2;
  371. }
  372. if (alg)
  373. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  374. return 1;
  375. }
  376. /*
  377. * Customised RSA item verification routine. This is called when a signature
  378. * is encountered requiring special handling. We currently only handle PSS.
  379. */
  380. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  381. X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
  382. EVP_PKEY *pkey)
  383. {
  384. int rv = -1;
  385. int saltlen;
  386. const EVP_MD *mgf1md = NULL, *md = NULL;
  387. RSA_PSS_PARAMS *pss;
  388. X509_ALGOR *maskHash;
  389. EVP_PKEY_CTX *pkctx;
  390. /* Sanity check: make sure it is PSS */
  391. if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) {
  392. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  393. return -1;
  394. }
  395. /* Decode PSS parameters */
  396. pss = rsa_pss_decode(sigalg, &maskHash);
  397. if (pss == NULL) {
  398. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_PSS_PARAMETERS);
  399. goto err;
  400. }
  401. /* Check mask and lookup mask hash algorithm */
  402. if (pss->maskGenAlgorithm) {
  403. if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) != NID_mgf1) {
  404. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_MASK_ALGORITHM);
  405. goto err;
  406. }
  407. if (!maskHash) {
  408. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_MASK_PARAMETER);
  409. goto err;
  410. }
  411. mgf1md = EVP_get_digestbyobj(maskHash->algorithm);
  412. if (mgf1md == NULL) {
  413. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNKNOWN_MASK_DIGEST);
  414. goto err;
  415. }
  416. } else
  417. mgf1md = EVP_sha1();
  418. if (pss->hashAlgorithm) {
  419. md = EVP_get_digestbyobj(pss->hashAlgorithm->algorithm);
  420. if (md == NULL) {
  421. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNKNOWN_PSS_DIGEST);
  422. goto err;
  423. }
  424. } else
  425. md = EVP_sha1();
  426. if (pss->saltLength) {
  427. saltlen = ASN1_INTEGER_get(pss->saltLength);
  428. /*
  429. * Could perform more salt length sanity checks but the main RSA
  430. * routines will trap other invalid values anyway.
  431. */
  432. if (saltlen < 0) {
  433. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_SALT_LENGTH);
  434. goto err;
  435. }
  436. } else
  437. saltlen = 20;
  438. /*
  439. * low-level routines support only trailer field 0xbc (value 1) and
  440. * PKCS#1 says we should reject any other value anyway.
  441. */
  442. if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
  443. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_TRAILER);
  444. goto err;
  445. }
  446. /* We have all parameters now set up context */
  447. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
  448. goto err;
  449. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
  450. goto err;
  451. if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
  452. goto err;
  453. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  454. goto err;
  455. /* Carry on */
  456. rv = 2;
  457. err:
  458. RSA_PSS_PARAMS_free(pss);
  459. if (maskHash)
  460. X509_ALGOR_free(maskHash);
  461. return rv;
  462. }
  463. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  464. X509_ALGOR *alg1, X509_ALGOR *alg2,
  465. ASN1_BIT_STRING *sig)
  466. {
  467. int pad_mode;
  468. EVP_PKEY_CTX *pkctx = ctx->pctx;
  469. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  470. return 0;
  471. if (pad_mode == RSA_PKCS1_PADDING)
  472. return 2;
  473. if (pad_mode == RSA_PKCS1_PSS_PADDING) {
  474. const EVP_MD *sigmd, *mgf1md;
  475. RSA_PSS_PARAMS *pss = NULL;
  476. X509_ALGOR *mgf1alg = NULL;
  477. ASN1_STRING *os1 = NULL, *os2 = NULL;
  478. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  479. int saltlen, rv = 0;
  480. sigmd = EVP_MD_CTX_md(ctx);
  481. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  482. goto err;
  483. if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
  484. goto err;
  485. if (saltlen == -1)
  486. saltlen = EVP_MD_size(sigmd);
  487. else if (saltlen == -2) {
  488. saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
  489. if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0)
  490. saltlen--;
  491. }
  492. pss = RSA_PSS_PARAMS_new();
  493. if (!pss)
  494. goto err;
  495. if (saltlen != 20) {
  496. pss->saltLength = ASN1_INTEGER_new();
  497. if (!pss->saltLength)
  498. goto err;
  499. if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
  500. goto err;
  501. }
  502. if (EVP_MD_type(sigmd) != NID_sha1) {
  503. pss->hashAlgorithm = X509_ALGOR_new();
  504. if (!pss->hashAlgorithm)
  505. goto err;
  506. X509_ALGOR_set_md(pss->hashAlgorithm, sigmd);
  507. }
  508. if (EVP_MD_type(mgf1md) != NID_sha1) {
  509. ASN1_STRING *stmp = NULL;
  510. /* need to embed algorithm ID inside another */
  511. mgf1alg = X509_ALGOR_new();
  512. X509_ALGOR_set_md(mgf1alg, mgf1md);
  513. if (!ASN1_item_pack(mgf1alg, ASN1_ITEM_rptr(X509_ALGOR), &stmp))
  514. goto err;
  515. pss->maskGenAlgorithm = X509_ALGOR_new();
  516. if (!pss->maskGenAlgorithm)
  517. goto err;
  518. X509_ALGOR_set0(pss->maskGenAlgorithm,
  519. OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
  520. }
  521. /* Finally create string with pss parameter encoding. */
  522. if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os1))
  523. goto err;
  524. if (alg2) {
  525. os2 = ASN1_STRING_dup(os1);
  526. if (!os2)
  527. goto err;
  528. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_rsassaPss),
  529. V_ASN1_SEQUENCE, os2);
  530. }
  531. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_rsassaPss),
  532. V_ASN1_SEQUENCE, os1);
  533. os1 = os2 = NULL;
  534. rv = 3;
  535. err:
  536. if (mgf1alg)
  537. X509_ALGOR_free(mgf1alg);
  538. if (pss)
  539. RSA_PSS_PARAMS_free(pss);
  540. if (os1)
  541. ASN1_STRING_free(os1);
  542. return rv;
  543. }
  544. return 2;
  545. }
  546. const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] = {
  547. {
  548. EVP_PKEY_RSA,
  549. EVP_PKEY_RSA,
  550. ASN1_PKEY_SIGPARAM_NULL,
  551. "RSA",
  552. "OpenSSL RSA method",
  553. rsa_pub_decode,
  554. rsa_pub_encode,
  555. rsa_pub_cmp,
  556. rsa_pub_print,
  557. rsa_priv_decode,
  558. rsa_priv_encode,
  559. rsa_priv_print,
  560. int_rsa_size,
  561. rsa_bits,
  562. 0, 0, 0, 0, 0, 0,
  563. rsa_sig_print,
  564. int_rsa_free,
  565. rsa_pkey_ctrl,
  566. old_rsa_priv_decode,
  567. old_rsa_priv_encode,
  568. rsa_item_verify,
  569. rsa_item_sign},
  570. {
  571. EVP_PKEY_RSA2,
  572. EVP_PKEY_RSA,
  573. ASN1_PKEY_ALIAS}
  574. };