您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

758 行
19 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 int_rsa_size(const EVP_PKEY *pkey) {
  135. return RSA_size(pkey->pkey.rsa);
  136. }
  137. static int rsa_bits(const EVP_PKEY *pkey) {
  138. return BN_num_bits(pkey->pkey.rsa->n);
  139. }
  140. static void int_rsa_free(EVP_PKEY *pkey) { RSA_free(pkey->pkey.rsa); }
  141. static void update_buflen(const BIGNUM *b, size_t *pbuflen) {
  142. size_t i;
  143. if (!b) {
  144. return;
  145. }
  146. i = BN_num_bytes(b);
  147. if (*pbuflen < i) {
  148. *pbuflen = i;
  149. }
  150. }
  151. static int do_rsa_print(BIO *out, const RSA *rsa, int off,
  152. int include_private) {
  153. char *str;
  154. const char *s;
  155. uint8_t *m = NULL;
  156. int ret = 0, mod_len = 0;
  157. size_t buf_len = 0;
  158. update_buflen(rsa->n, &buf_len);
  159. update_buflen(rsa->e, &buf_len);
  160. if (include_private) {
  161. update_buflen(rsa->d, &buf_len);
  162. update_buflen(rsa->p, &buf_len);
  163. update_buflen(rsa->q, &buf_len);
  164. update_buflen(rsa->dmp1, &buf_len);
  165. update_buflen(rsa->dmq1, &buf_len);
  166. update_buflen(rsa->iqmp, &buf_len);
  167. }
  168. m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
  169. if (m == NULL) {
  170. OPENSSL_PUT_ERROR(EVP, do_rsa_print, ERR_R_MALLOC_FAILURE);
  171. goto err;
  172. }
  173. if (rsa->n != NULL) {
  174. mod_len = BN_num_bits(rsa->n);
  175. }
  176. if (!BIO_indent(out, off, 128)) {
  177. goto err;
  178. }
  179. if (include_private && rsa->d) {
  180. if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) {
  181. goto err;
  182. }
  183. str = "modulus:";
  184. s = "publicExponent:";
  185. } else {
  186. if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) {
  187. goto err;
  188. }
  189. str = "Modulus:";
  190. s = "Exponent:";
  191. }
  192. if (!ASN1_bn_print(out, str, rsa->n, m, off) ||
  193. !ASN1_bn_print(out, s, rsa->e, m, off)) {
  194. goto err;
  195. }
  196. if (include_private) {
  197. if (!ASN1_bn_print(out, "privateExponent:", rsa->d, m, off) ||
  198. !ASN1_bn_print(out, "prime1:", rsa->p, m, off) ||
  199. !ASN1_bn_print(out, "prime2:", rsa->q, m, off) ||
  200. !ASN1_bn_print(out, "exponent1:", rsa->dmp1, m, off) ||
  201. !ASN1_bn_print(out, "exponent2:", rsa->dmq1, m, off) ||
  202. !ASN1_bn_print(out, "coefficient:", rsa->iqmp, m, off)) {
  203. goto err;
  204. }
  205. }
  206. ret = 1;
  207. err:
  208. if (m != NULL) {
  209. OPENSSL_free(m);
  210. }
  211. return ret;
  212. }
  213. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  214. ASN1_PCTX *ctx) {
  215. return do_rsa_print(bp, pkey->pkey.rsa, indent, 0);
  216. }
  217. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  218. ASN1_PCTX *ctx) {
  219. return do_rsa_print(bp, pkey->pkey.rsa, indent, 1);
  220. }
  221. /* Given an MGF1 Algorithm ID decode to an Algorithm Identifier */
  222. static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg) {
  223. const uint8_t *p;
  224. int plen;
  225. if (alg == NULL ||
  226. OBJ_obj2nid(alg->algorithm) != NID_mgf1 ||
  227. alg->parameter->type != V_ASN1_SEQUENCE) {
  228. return NULL;
  229. }
  230. p = alg->parameter->value.sequence->data;
  231. plen = alg->parameter->value.sequence->length;
  232. return d2i_X509_ALGOR(NULL, &p, plen);
  233. }
  234. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg,
  235. X509_ALGOR **pmaskHash) {
  236. const uint8_t *p;
  237. int plen;
  238. RSA_PSS_PARAMS *pss;
  239. *pmaskHash = NULL;
  240. if (!alg->parameter || alg->parameter->type != V_ASN1_SEQUENCE) {
  241. return NULL;
  242. }
  243. p = alg->parameter->value.sequence->data;
  244. plen = alg->parameter->value.sequence->length;
  245. pss = d2i_RSA_PSS_PARAMS(NULL, &p, plen);
  246. if (!pss) {
  247. return NULL;
  248. }
  249. *pmaskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
  250. return pss;
  251. }
  252. static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss,
  253. X509_ALGOR *maskHash, int indent) {
  254. int rv = 0;
  255. if (!pss) {
  256. if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0) {
  257. return 0;
  258. }
  259. return 1;
  260. }
  261. if (BIO_puts(bp, "\n") <= 0 ||
  262. !BIO_indent(bp, indent, 128) ||
  263. BIO_puts(bp, "Hash Algorithm: ") <= 0) {
  264. goto err;
  265. }
  266. if (pss->hashAlgorithm) {
  267. if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0) {
  268. goto err;
  269. }
  270. } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
  271. goto err;
  272. }
  273. if (BIO_puts(bp, "\n") <= 0 ||
  274. !BIO_indent(bp, indent, 128) ||
  275. BIO_puts(bp, "Mask Algorithm: ") <= 0) {
  276. goto err;
  277. }
  278. if (pss->maskGenAlgorithm) {
  279. if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0 ||
  280. BIO_puts(bp, " with ") <= 0) {
  281. goto err;
  282. }
  283. if (maskHash) {
  284. if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) {
  285. goto err;
  286. }
  287. } else if (BIO_puts(bp, "INVALID") <= 0) {
  288. goto err;
  289. }
  290. } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
  291. goto err;
  292. }
  293. BIO_puts(bp, "\n");
  294. if (!BIO_indent(bp, indent, 128) ||
  295. BIO_puts(bp, "Salt Length: 0x") <= 0) {
  296. goto err;
  297. }
  298. if (pss->saltLength) {
  299. if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0) {
  300. goto err;
  301. }
  302. } else if (BIO_puts(bp, "14 (default)") <= 0) {
  303. goto err;
  304. }
  305. BIO_puts(bp, "\n");
  306. if (!BIO_indent(bp, indent, 128) ||
  307. BIO_puts(bp, "Trailer Field: 0x") <= 0) {
  308. goto err;
  309. }
  310. if (pss->trailerField) {
  311. if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0) {
  312. goto err;
  313. }
  314. } else if (BIO_puts(bp, "BC (default)") <= 0) {
  315. goto err;
  316. }
  317. BIO_puts(bp, "\n");
  318. rv = 1;
  319. err:
  320. return rv;
  321. }
  322. static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  323. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) {
  324. if (OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss) {
  325. int rv;
  326. RSA_PSS_PARAMS *pss;
  327. X509_ALGOR *maskHash;
  328. pss = rsa_pss_decode(sigalg, &maskHash);
  329. rv = rsa_pss_param_print(bp, pss, maskHash, indent);
  330. if (pss) {
  331. RSA_PSS_PARAMS_free(pss);
  332. }
  333. if (maskHash) {
  334. X509_ALGOR_free(maskHash);
  335. }
  336. if (!rv) {
  337. return 0;
  338. }
  339. } else if (!sig && BIO_puts(bp, "\n") <= 0) {
  340. return 0;
  341. }
  342. if (sig) {
  343. return X509_signature_dump(bp, sig, indent);
  344. }
  345. return 1;
  346. }
  347. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) {
  348. X509_ALGOR *alg = NULL;
  349. switch (op) {
  350. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  351. *(int *)arg2 = NID_sha1;
  352. return 1;
  353. default:
  354. return -2;
  355. }
  356. if (alg) {
  357. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  358. }
  359. return 1;
  360. }
  361. static int old_rsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder,
  362. int derlen) {
  363. RSA *rsa = d2i_RSAPrivateKey(NULL, pder, derlen);
  364. if (rsa == NULL) {
  365. OPENSSL_PUT_ERROR(EVP, old_rsa_priv_decode, ERR_R_RSA_LIB);
  366. return 0;
  367. }
  368. EVP_PKEY_assign_RSA(pkey, rsa);
  369. return 1;
  370. }
  371. static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) {
  372. return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
  373. }
  374. /* allocate and set algorithm ID from EVP_MD, default SHA1 */
  375. static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md) {
  376. if (EVP_MD_type(md) == NID_sha1) {
  377. return 1;
  378. }
  379. *palg = X509_ALGOR_new();
  380. if (!*palg) {
  381. return 0;
  382. }
  383. X509_ALGOR_set_md(*palg, md);
  384. return 1;
  385. }
  386. /* Allocate and set MGF1 algorithm ID from EVP_MD */
  387. static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) {
  388. X509_ALGOR *algtmp = NULL;
  389. ASN1_STRING *stmp = NULL;
  390. *palg = NULL;
  391. if (EVP_MD_type(mgf1md) == NID_sha1) {
  392. return 1;
  393. }
  394. /* need to embed algorithm ID inside another */
  395. if (!rsa_md_to_algor(&algtmp, mgf1md) ||
  396. !ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp)) {
  397. goto err;
  398. }
  399. *palg = X509_ALGOR_new();
  400. if (!*palg) {
  401. goto err;
  402. }
  403. X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
  404. stmp = NULL;
  405. err:
  406. if (stmp)
  407. ASN1_STRING_free(stmp);
  408. if (algtmp)
  409. X509_ALGOR_free(algtmp);
  410. if (*palg)
  411. return 1;
  412. return 0;
  413. }
  414. /* convert algorithm ID to EVP_MD, default SHA1 */
  415. static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg) {
  416. const EVP_MD *md;
  417. if (!alg) {
  418. return EVP_sha1();
  419. }
  420. md = EVP_get_digestbyobj(alg->algorithm);
  421. if (md == NULL) {
  422. OPENSSL_PUT_ERROR(EVP, rsa_algor_to_md, EVP_R_UNKNOWN_DIGEST);
  423. }
  424. return md;
  425. }
  426. /* convert MGF1 algorithm ID to EVP_MD, default SHA1 */
  427. static const EVP_MD *rsa_mgf1_to_md(X509_ALGOR *alg, X509_ALGOR *maskHash) {
  428. const EVP_MD *md;
  429. if (!alg) {
  430. return EVP_sha1();
  431. }
  432. /* Check mask and lookup mask hash algorithm */
  433. if (OBJ_obj2nid(alg->algorithm) != NID_mgf1) {
  434. OPENSSL_PUT_ERROR(EVP, rsa_mgf1_to_md, EVP_R_UNSUPPORTED_MASK_ALGORITHM);
  435. return NULL;
  436. }
  437. if (!maskHash) {
  438. OPENSSL_PUT_ERROR(EVP, rsa_mgf1_to_md, EVP_R_UNSUPPORTED_MASK_PARAMETER);
  439. return NULL;
  440. }
  441. md = EVP_get_digestbyobj(maskHash->algorithm);
  442. if (md == NULL) {
  443. OPENSSL_PUT_ERROR(EVP, rsa_mgf1_to_md, EVP_R_UNKNOWN_MASK_DIGEST);
  444. return NULL;
  445. }
  446. return md;
  447. }
  448. /* rsa_ctx_to_pss converts EVP_PKEY_CTX in PSS mode into corresponding
  449. * algorithm parameter, suitable for setting as an AlgorithmIdentifier. */
  450. static ASN1_STRING *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx) {
  451. const EVP_MD *sigmd, *mgf1md;
  452. RSA_PSS_PARAMS *pss = NULL;
  453. ASN1_STRING *os = NULL;
  454. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  455. int saltlen, rv = 0;
  456. if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0 ||
  457. EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0 ||
  458. !EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen)) {
  459. goto err;
  460. }
  461. if (saltlen == -1) {
  462. saltlen = EVP_MD_size(sigmd);
  463. } else if (saltlen == -2) {
  464. saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
  465. if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0) {
  466. saltlen--;
  467. }
  468. } else {
  469. goto err;
  470. }
  471. pss = RSA_PSS_PARAMS_new();
  472. if (!pss) {
  473. goto err;
  474. }
  475. if (saltlen != 20) {
  476. pss->saltLength = ASN1_INTEGER_new();
  477. if (!pss->saltLength ||
  478. !ASN1_INTEGER_set(pss->saltLength, saltlen)) {
  479. goto err;
  480. }
  481. }
  482. if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd) ||
  483. !rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md)) {
  484. goto err;
  485. }
  486. /* Finally create string with pss parameter encoding. */
  487. if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os)) {
  488. goto err;
  489. }
  490. rv = 1;
  491. err:
  492. if (pss)
  493. RSA_PSS_PARAMS_free(pss);
  494. if (rv)
  495. return os;
  496. if (os)
  497. ASN1_STRING_free(os);
  498. return NULL;
  499. }
  500. /* From PSS AlgorithmIdentifier set public key parameters. If pkey
  501. * isn't NULL then the EVP_MD_CTX is setup and initalised. If it
  502. * is NULL parameters are passed to pkctx instead. */
  503. static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
  504. X509_ALGOR *sigalg, EVP_PKEY *pkey) {
  505. int ret = -1;
  506. int saltlen;
  507. const EVP_MD *mgf1md = NULL, *md = NULL;
  508. RSA_PSS_PARAMS *pss;
  509. X509_ALGOR *maskHash;
  510. /* Sanity check: make sure it is PSS */
  511. if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) {
  512. OPENSSL_PUT_ERROR(EVP, rsa_pss_to_ctx, EVP_R_UNSUPPORTED_SIGNATURE_TYPE);
  513. return -1;
  514. }
  515. /* Decode PSS parameters */
  516. pss = rsa_pss_decode(sigalg, &maskHash);
  517. if (pss == NULL) {
  518. OPENSSL_PUT_ERROR(EVP, rsa_pss_to_ctx, EVP_R_INVALID_PSS_PARAMETERS);
  519. goto err;
  520. }
  521. mgf1md = rsa_mgf1_to_md(pss->maskGenAlgorithm, maskHash);
  522. if (!mgf1md) {
  523. goto err;
  524. }
  525. md = rsa_algor_to_md(pss->hashAlgorithm);
  526. if (!md) {
  527. goto err;
  528. }
  529. saltlen = 20;
  530. if (pss->saltLength) {
  531. saltlen = ASN1_INTEGER_get(pss->saltLength);
  532. /* Could perform more salt length sanity checks but the main
  533. * RSA routines will trap other invalid values anyway. */
  534. if (saltlen < 0) {
  535. OPENSSL_PUT_ERROR(EVP, rsa_pss_to_ctx, EVP_R_INVALID_SALT_LENGTH);
  536. goto err;
  537. }
  538. }
  539. /* low-level routines support only trailer field 0xbc (value 1)
  540. * and PKCS#1 says we should reject any other value anyway. */
  541. if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
  542. OPENSSL_PUT_ERROR(EVP, rsa_pss_to_ctx, EVP_R_INVALID_TRAILER);
  543. goto err;
  544. }
  545. if (pkey) {
  546. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey)) {
  547. goto err;
  548. }
  549. } else {
  550. const EVP_MD *checkmd;
  551. if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0) {
  552. goto err;
  553. }
  554. if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
  555. OPENSSL_PUT_ERROR(EVP, rsa_pss_to_ctx, EVP_R_DIGEST_DOES_NOT_MATCH);
  556. goto err;
  557. }
  558. }
  559. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0 ||
  560. EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0 ||
  561. EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0) {
  562. goto err;
  563. }
  564. ret = 1;
  565. err:
  566. RSA_PSS_PARAMS_free(pss);
  567. if (maskHash) {
  568. X509_ALGOR_free(maskHash);
  569. }
  570. return ret;
  571. }
  572. /* Customised RSA item verification routine. This is called
  573. * when a signature is encountered requiring special handling. We
  574. * currently only handle PSS. */
  575. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  576. X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
  577. EVP_PKEY *pkey) {
  578. /* Sanity check: make sure it is PSS */
  579. if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) {
  580. OPENSSL_PUT_ERROR(EVP, rsa_item_verify, EVP_R_UNSUPPORTED_SIGNATURE_TYPE);
  581. return -1;
  582. }
  583. if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey)) {
  584. /* Carry on */
  585. return 2;
  586. }
  587. return -1;
  588. }
  589. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  590. X509_ALGOR *alg1, X509_ALGOR *alg2,
  591. ASN1_BIT_STRING *sig) {
  592. int pad_mode;
  593. EVP_PKEY_CTX *pkctx = ctx->pctx;
  594. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0) {
  595. return 0;
  596. }
  597. if (pad_mode == RSA_PKCS1_PADDING) {
  598. return 2;
  599. }
  600. if (pad_mode == RSA_PKCS1_PSS_PADDING) {
  601. ASN1_STRING *os1 = rsa_ctx_to_pss(pkctx);
  602. if (!os1) {
  603. return 0;
  604. }
  605. /* Duplicate parameters if we have to */
  606. if (alg2) {
  607. ASN1_STRING *os2 = ASN1_STRING_dup(os1);
  608. if (!os2) {
  609. ASN1_STRING_free(os1);
  610. return 0;
  611. }
  612. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os2);
  613. }
  614. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_rsassaPss), V_ASN1_SEQUENCE, os1);
  615. return 3;
  616. }
  617. return 2;
  618. }
  619. const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = {
  620. EVP_PKEY_RSA,
  621. EVP_PKEY_RSA,
  622. ASN1_PKEY_SIGPARAM_NULL,
  623. "RSA",
  624. "OpenSSL RSA method",
  625. rsa_pub_decode,
  626. rsa_pub_encode,
  627. rsa_pub_cmp,
  628. rsa_pub_print,
  629. rsa_priv_decode,
  630. rsa_priv_encode,
  631. rsa_priv_print,
  632. rsa_opaque,
  633. int_rsa_size,
  634. rsa_bits,
  635. 0,0,0,0,0,0,
  636. rsa_sig_print,
  637. int_rsa_free,
  638. rsa_pkey_ctrl,
  639. old_rsa_priv_decode,
  640. old_rsa_priv_encode,
  641. rsa_item_verify,
  642. rsa_item_sign,
  643. };
  644. const EVP_PKEY_ASN1_METHOD rsa_asn1_meth_2 = {
  645. EVP_PKEY_RSA2,
  646. EVP_PKEY_RSA,
  647. ASN1_PKEY_ALIAS,
  648. };