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

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