You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p_rsa_asn1.c 19 KiB

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