p_rsa_asn1.c 20 KB

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