選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

702 行
18 KiB

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