rsa_ameth.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/asn1t.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/core_names.h>
  20. #include <openssl/param_build.h>
  21. #include "crypto/asn1.h"
  22. #include "crypto/evp.h"
  23. #include "crypto/rsa.h"
  24. #include "rsa_local.h"
  25. /* Set any parameters associated with pkey */
  26. static int rsa_param_encode(const EVP_PKEY *pkey,
  27. ASN1_STRING **pstr, int *pstrtype)
  28. {
  29. const RSA *rsa = pkey->pkey.rsa;
  30. *pstr = NULL;
  31. /* If RSA it's just NULL type */
  32. if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != RSA_FLAG_TYPE_RSASSAPSS) {
  33. *pstrtype = V_ASN1_NULL;
  34. return 1;
  35. }
  36. /* If no PSS parameters we omit parameters entirely */
  37. if (rsa->pss == NULL) {
  38. *pstrtype = V_ASN1_UNDEF;
  39. return 1;
  40. }
  41. /* Encode PSS parameters */
  42. if (ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr) == NULL)
  43. return 0;
  44. *pstrtype = V_ASN1_SEQUENCE;
  45. return 1;
  46. }
  47. /* Decode any parameters and set them in RSA structure */
  48. static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  49. {
  50. unsigned char *penc = NULL;
  51. int penclen;
  52. ASN1_STRING *str;
  53. int strtype;
  54. if (!rsa_param_encode(pkey, &str, &strtype))
  55. return 0;
  56. penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
  57. if (penclen <= 0)
  58. return 0;
  59. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
  60. strtype, str, penc, penclen))
  61. return 1;
  62. OPENSSL_free(penc);
  63. return 0;
  64. }
  65. static int rsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
  66. {
  67. const unsigned char *p;
  68. int pklen;
  69. X509_ALGOR *alg;
  70. RSA *rsa = NULL;
  71. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
  72. return 0;
  73. if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL)
  74. return 0;
  75. if (!ossl_rsa_param_decode(rsa, alg)) {
  76. RSA_free(rsa);
  77. return 0;
  78. }
  79. RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
  80. switch (pkey->ameth->pkey_id) {
  81. case EVP_PKEY_RSA:
  82. RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
  83. break;
  84. case EVP_PKEY_RSA_PSS:
  85. RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
  86. break;
  87. default:
  88. /* Leave the type bits zero */
  89. break;
  90. }
  91. if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) {
  92. RSA_free(rsa);
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  98. {
  99. /*
  100. * Don't check the public/private key, this is mostly for smart
  101. * cards.
  102. */
  103. if (((RSA_flags(a->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
  104. || (RSA_flags(b->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK)) {
  105. return 1;
  106. }
  107. if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
  108. || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
  109. return 0;
  110. return 1;
  111. }
  112. static int old_rsa_priv_decode(EVP_PKEY *pkey,
  113. const unsigned char **pder, int derlen)
  114. {
  115. RSA *rsa;
  116. if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL)
  117. return 0;
  118. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
  119. return 1;
  120. }
  121. static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  122. {
  123. return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
  124. }
  125. static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  126. {
  127. unsigned char *rk = NULL;
  128. int rklen;
  129. ASN1_STRING *str;
  130. int strtype;
  131. if (!rsa_param_encode(pkey, &str, &strtype))
  132. return 0;
  133. rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
  134. if (rklen <= 0) {
  135. ERR_raise(ERR_LIB_RSA, ERR_R_ASN1_LIB);
  136. ASN1_STRING_free(str);
  137. return 0;
  138. }
  139. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
  140. strtype, str, rk, rklen)) {
  141. ERR_raise(ERR_LIB_RSA, ERR_R_ASN1_LIB);
  142. ASN1_STRING_free(str);
  143. OPENSSL_clear_free(rk, rklen);
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  149. {
  150. int ret = 0;
  151. RSA *rsa = ossl_rsa_key_from_pkcs8(p8, NULL, NULL);
  152. if (rsa != NULL) {
  153. ret = 1;
  154. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
  155. }
  156. return ret;
  157. }
  158. static int int_rsa_size(const EVP_PKEY *pkey)
  159. {
  160. return RSA_size(pkey->pkey.rsa);
  161. }
  162. static int rsa_bits(const EVP_PKEY *pkey)
  163. {
  164. return BN_num_bits(pkey->pkey.rsa->n);
  165. }
  166. static int rsa_security_bits(const EVP_PKEY *pkey)
  167. {
  168. return RSA_security_bits(pkey->pkey.rsa);
  169. }
  170. static void int_rsa_free(EVP_PKEY *pkey)
  171. {
  172. RSA_free(pkey->pkey.rsa);
  173. }
  174. static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
  175. int indent)
  176. {
  177. int rv = 0;
  178. X509_ALGOR *maskHash = NULL;
  179. if (!BIO_indent(bp, indent, 128))
  180. goto err;
  181. if (pss_key) {
  182. if (pss == NULL) {
  183. if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
  184. return 0;
  185. return 1;
  186. } else {
  187. if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
  188. return 0;
  189. }
  190. } else if (pss == NULL) {
  191. if (BIO_puts(bp, "(INVALID PSS PARAMETERS)\n") <= 0)
  192. return 0;
  193. return 1;
  194. }
  195. if (BIO_puts(bp, "\n") <= 0)
  196. goto err;
  197. if (pss_key)
  198. indent += 2;
  199. if (!BIO_indent(bp, indent, 128))
  200. goto err;
  201. if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
  202. goto err;
  203. if (pss->hashAlgorithm) {
  204. if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
  205. goto err;
  206. } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
  207. goto err;
  208. }
  209. if (BIO_puts(bp, "\n") <= 0)
  210. goto err;
  211. if (!BIO_indent(bp, indent, 128))
  212. goto err;
  213. if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
  214. goto err;
  215. if (pss->maskGenAlgorithm) {
  216. if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
  217. goto err;
  218. if (BIO_puts(bp, " with ") <= 0)
  219. goto err;
  220. maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm);
  221. if (maskHash != NULL) {
  222. if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
  223. goto err;
  224. } else if (BIO_puts(bp, "INVALID") <= 0) {
  225. goto err;
  226. }
  227. } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
  228. goto err;
  229. }
  230. BIO_puts(bp, "\n");
  231. if (!BIO_indent(bp, indent, 128))
  232. goto err;
  233. if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
  234. goto err;
  235. if (pss->saltLength) {
  236. if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
  237. goto err;
  238. } else if (BIO_puts(bp, "14 (default)") <= 0) {
  239. goto err;
  240. }
  241. BIO_puts(bp, "\n");
  242. if (!BIO_indent(bp, indent, 128))
  243. goto err;
  244. if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
  245. goto err;
  246. if (pss->trailerField) {
  247. if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
  248. goto err;
  249. } else if (BIO_puts(bp, "01 (default)") <= 0) {
  250. goto err;
  251. }
  252. BIO_puts(bp, "\n");
  253. rv = 1;
  254. err:
  255. X509_ALGOR_free(maskHash);
  256. return rv;
  257. }
  258. static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
  259. {
  260. const RSA *x = pkey->pkey.rsa;
  261. char *str;
  262. const char *s;
  263. int ret = 0, mod_len = 0, ex_primes;
  264. if (x->n != NULL)
  265. mod_len = BN_num_bits(x->n);
  266. ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos);
  267. if (!BIO_indent(bp, off, 128))
  268. goto err;
  269. if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ? "RSA-PSS" : "RSA") <= 0)
  270. goto err;
  271. if (priv && x->d) {
  272. if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n",
  273. mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0)
  274. goto err;
  275. str = "modulus:";
  276. s = "publicExponent:";
  277. } else {
  278. if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
  279. goto err;
  280. str = "Modulus:";
  281. s = "Exponent:";
  282. }
  283. if (!ASN1_bn_print(bp, str, x->n, NULL, off))
  284. goto err;
  285. if (!ASN1_bn_print(bp, s, x->e, NULL, off))
  286. goto err;
  287. if (priv) {
  288. int i;
  289. if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
  290. goto err;
  291. if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
  292. goto err;
  293. if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
  294. goto err;
  295. if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
  296. goto err;
  297. if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
  298. goto err;
  299. if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
  300. goto err;
  301. for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) {
  302. /* print multi-prime info */
  303. BIGNUM *bn = NULL;
  304. RSA_PRIME_INFO *pinfo;
  305. int j;
  306. pinfo = sk_RSA_PRIME_INFO_value(x->prime_infos, i);
  307. for (j = 0; j < 3; j++) {
  308. if (!BIO_indent(bp, off, 128))
  309. goto err;
  310. switch (j) {
  311. case 0:
  312. if (BIO_printf(bp, "prime%d:", i + 3) <= 0)
  313. goto err;
  314. bn = pinfo->r;
  315. break;
  316. case 1:
  317. if (BIO_printf(bp, "exponent%d:", i + 3) <= 0)
  318. goto err;
  319. bn = pinfo->d;
  320. break;
  321. case 2:
  322. if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0)
  323. goto err;
  324. bn = pinfo->t;
  325. break;
  326. default:
  327. break;
  328. }
  329. if (!ASN1_bn_print(bp, "", bn, NULL, off))
  330. goto err;
  331. }
  332. }
  333. }
  334. if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
  335. goto err;
  336. ret = 1;
  337. err:
  338. return ret;
  339. }
  340. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  341. ASN1_PCTX *ctx)
  342. {
  343. return pkey_rsa_print(bp, pkey, indent, 0);
  344. }
  345. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  346. ASN1_PCTX *ctx)
  347. {
  348. return pkey_rsa_print(bp, pkey, indent, 1);
  349. }
  350. static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  351. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  352. {
  353. if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
  354. int rv;
  355. RSA_PSS_PARAMS *pss = ossl_rsa_pss_decode(sigalg);
  356. rv = rsa_pss_param_print(bp, 0, pss, indent);
  357. RSA_PSS_PARAMS_free(pss);
  358. if (!rv)
  359. return 0;
  360. } else if (BIO_puts(bp, "\n") <= 0) {
  361. return 0;
  362. }
  363. if (sig)
  364. return X509_signature_dump(bp, sig, indent);
  365. return 1;
  366. }
  367. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  368. {
  369. const EVP_MD *md;
  370. const EVP_MD *mgf1md;
  371. int min_saltlen;
  372. switch (op) {
  373. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  374. if (pkey->pkey.rsa->pss != NULL) {
  375. if (!ossl_rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md,
  376. &min_saltlen)) {
  377. ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
  378. return 0;
  379. }
  380. *(int *)arg2 = EVP_MD_get_type(md);
  381. /* Return of 2 indicates this MD is mandatory */
  382. return 2;
  383. }
  384. *(int *)arg2 = NID_sha256;
  385. return 1;
  386. default:
  387. return -2;
  388. }
  389. }
  390. /*
  391. * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
  392. * suitable for setting an AlgorithmIdentifier.
  393. */
  394. static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
  395. {
  396. const EVP_MD *sigmd, *mgf1md;
  397. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  398. int saltlen;
  399. if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
  400. return NULL;
  401. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  402. return NULL;
  403. if (EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen) <= 0)
  404. return NULL;
  405. if (saltlen == -1) {
  406. saltlen = EVP_MD_get_size(sigmd);
  407. } else if (saltlen == -2 || saltlen == -3) {
  408. saltlen = EVP_PKEY_get_size(pk) - EVP_MD_get_size(sigmd) - 2;
  409. if ((EVP_PKEY_get_bits(pk) & 0x7) == 1)
  410. saltlen--;
  411. if (saltlen < 0)
  412. return NULL;
  413. }
  414. return ossl_rsa_pss_params_create(sigmd, mgf1md, saltlen);
  415. }
  416. RSA_PSS_PARAMS *ossl_rsa_pss_params_create(const EVP_MD *sigmd,
  417. const EVP_MD *mgf1md, int saltlen)
  418. {
  419. RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
  420. if (pss == NULL)
  421. goto err;
  422. if (saltlen != 20) {
  423. pss->saltLength = ASN1_INTEGER_new();
  424. if (pss->saltLength == NULL)
  425. goto err;
  426. if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
  427. goto err;
  428. }
  429. if (!ossl_x509_algor_new_from_md(&pss->hashAlgorithm, sigmd))
  430. goto err;
  431. if (mgf1md == NULL)
  432. mgf1md = sigmd;
  433. if (!ossl_x509_algor_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
  434. goto err;
  435. if (!ossl_x509_algor_new_from_md(&pss->maskHash, mgf1md))
  436. goto err;
  437. return pss;
  438. err:
  439. RSA_PSS_PARAMS_free(pss);
  440. return NULL;
  441. }
  442. ASN1_STRING *ossl_rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
  443. {
  444. RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
  445. ASN1_STRING *os;
  446. if (pss == NULL)
  447. return NULL;
  448. os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
  449. RSA_PSS_PARAMS_free(pss);
  450. return os;
  451. }
  452. /*
  453. * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
  454. * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
  455. * passed to pkctx instead.
  456. */
  457. int ossl_rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
  458. const X509_ALGOR *sigalg, EVP_PKEY *pkey)
  459. {
  460. int rv = -1;
  461. int saltlen;
  462. const EVP_MD *mgf1md = NULL, *md = NULL;
  463. RSA_PSS_PARAMS *pss;
  464. /* Sanity check: make sure it is PSS */
  465. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  466. ERR_raise(ERR_LIB_RSA, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  467. return -1;
  468. }
  469. /* Decode PSS parameters */
  470. pss = ossl_rsa_pss_decode(sigalg);
  471. if (!ossl_rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
  472. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS);
  473. goto err;
  474. }
  475. /* We have all parameters now set up context */
  476. if (pkey) {
  477. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
  478. goto err;
  479. } else {
  480. const EVP_MD *checkmd;
  481. if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
  482. goto err;
  483. if (EVP_MD_get_type(md) != EVP_MD_get_type(checkmd)) {
  484. ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_DOES_NOT_MATCH);
  485. goto err;
  486. }
  487. }
  488. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
  489. goto err;
  490. if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
  491. goto err;
  492. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  493. goto err;
  494. /* Carry on */
  495. rv = 1;
  496. err:
  497. RSA_PSS_PARAMS_free(pss);
  498. return rv;
  499. }
  500. static int rsa_pss_verify_param(const EVP_MD **pmd, const EVP_MD **pmgf1md,
  501. int *psaltlen, int *ptrailerField)
  502. {
  503. if (psaltlen != NULL && *psaltlen < 0) {
  504. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
  505. return 0;
  506. }
  507. /*
  508. * low-level routines support only trailer field 0xbc (value 1) and
  509. * PKCS#1 says we should reject any other value anyway.
  510. */
  511. if (ptrailerField != NULL && *ptrailerField != 1) {
  512. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_TRAILER);
  513. return 0;
  514. }
  515. return 1;
  516. }
  517. int ossl_rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
  518. const EVP_MD **pmgf1md, int *psaltlen)
  519. {
  520. /*
  521. * Callers do not care about the trailer field, and yet, we must
  522. * pass it from get_param to verify_param, since the latter checks
  523. * its value.
  524. *
  525. * When callers start caring, it's a simple thing to add another
  526. * argument to this function.
  527. */
  528. int trailerField = 0;
  529. return ossl_rsa_pss_get_param_unverified(pss, pmd, pmgf1md, psaltlen,
  530. &trailerField)
  531. && rsa_pss_verify_param(pmd, pmgf1md, psaltlen, &trailerField);
  532. }
  533. /*
  534. * Customised RSA item verification routine. This is called when a signature
  535. * is encountered requiring special handling. We currently only handle PSS.
  536. */
  537. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
  538. const void *asn, const X509_ALGOR *sigalg,
  539. const ASN1_BIT_STRING *sig, EVP_PKEY *pkey)
  540. {
  541. /* Sanity check: make sure it is PSS */
  542. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  543. ERR_raise(ERR_LIB_RSA, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  544. return -1;
  545. }
  546. if (ossl_rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
  547. /* Carry on */
  548. return 2;
  549. }
  550. return -1;
  551. }
  552. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, const void *asn,
  553. X509_ALGOR *alg1, X509_ALGOR *alg2,
  554. ASN1_BIT_STRING *sig)
  555. {
  556. int pad_mode;
  557. EVP_PKEY_CTX *pkctx = EVP_MD_CTX_get_pkey_ctx(ctx);
  558. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  559. return 0;
  560. if (pad_mode == RSA_PKCS1_PADDING)
  561. return 2;
  562. if (pad_mode == RSA_PKCS1_PSS_PADDING) {
  563. ASN1_STRING *os1 = ossl_rsa_ctx_to_pss_string(pkctx);
  564. if (os1 == NULL)
  565. return 0;
  566. /* Duplicate parameters if we have to */
  567. if (alg2 != NULL) {
  568. ASN1_STRING *os2 = ASN1_STRING_dup(os1);
  569. if (os2 == NULL)
  570. goto err;
  571. if (!X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  572. V_ASN1_SEQUENCE, os2)) {
  573. ASN1_STRING_free(os2);
  574. goto err;
  575. }
  576. }
  577. if (!X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  578. V_ASN1_SEQUENCE, os1))
  579. goto err;
  580. return 3;
  581. err:
  582. ASN1_STRING_free(os1);
  583. return 0;
  584. }
  585. return 2;
  586. }
  587. static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
  588. const ASN1_STRING *sig)
  589. {
  590. int rv = 0;
  591. int mdnid, saltlen;
  592. uint32_t flags;
  593. const EVP_MD *mgf1md = NULL, *md = NULL;
  594. RSA_PSS_PARAMS *pss;
  595. int secbits;
  596. /* Sanity check: make sure it is PSS */
  597. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
  598. return 0;
  599. /* Decode PSS parameters */
  600. pss = ossl_rsa_pss_decode(sigalg);
  601. if (!ossl_rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
  602. goto err;
  603. mdnid = EVP_MD_get_type(md);
  604. /*
  605. * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
  606. * match and salt length must equal digest size
  607. */
  608. if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
  609. && mdnid == EVP_MD_get_type(mgf1md)
  610. && saltlen == EVP_MD_get_size(md))
  611. flags = X509_SIG_INFO_TLS;
  612. else
  613. flags = 0;
  614. /* Note: security bits half number of digest bits */
  615. secbits = EVP_MD_get_size(md) * 4;
  616. /*
  617. * SHA1 and MD5 are known to be broken. Reduce security bits so that
  618. * they're no longer accepted at security level 1. The real values don't
  619. * really matter as long as they're lower than 80, which is our security
  620. * level 1.
  621. * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
  622. * 2^63.4
  623. * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
  624. * puts a chosen-prefix attack for MD5 at 2^39.
  625. */
  626. if (mdnid == NID_sha1)
  627. secbits = 64;
  628. else if (mdnid == NID_md5_sha1)
  629. secbits = 68;
  630. else if (mdnid == NID_md5)
  631. secbits = 39;
  632. X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
  633. flags);
  634. rv = 1;
  635. err:
  636. RSA_PSS_PARAMS_free(pss);
  637. return rv;
  638. }
  639. static int rsa_pkey_check(const EVP_PKEY *pkey)
  640. {
  641. return RSA_check_key_ex(pkey->pkey.rsa, NULL);
  642. }
  643. static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
  644. {
  645. return pkey->pkey.rsa->dirty_cnt;
  646. }
  647. /*
  648. * There is no need to do RSA_test_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS)
  649. * checks in this method since the caller tests EVP_KEYMGMT_is_a() first.
  650. */
  651. static int rsa_int_export_to(const EVP_PKEY *from, int rsa_type,
  652. void *to_keydata,
  653. OSSL_FUNC_keymgmt_import_fn *importer,
  654. OSSL_LIB_CTX *libctx, const char *propq)
  655. {
  656. RSA *rsa = from->pkey.rsa;
  657. OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
  658. OSSL_PARAM *params = NULL;
  659. int selection = 0;
  660. int rv = 0;
  661. if (tmpl == NULL)
  662. return 0;
  663. /* Public parameters must always be present */
  664. if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL)
  665. goto err;
  666. if (!ossl_rsa_todata(rsa, tmpl, NULL, 1))
  667. goto err;
  668. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  669. if (RSA_get0_d(rsa) != NULL)
  670. selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  671. if (rsa->pss != NULL) {
  672. const EVP_MD *md = NULL, *mgf1md = NULL;
  673. int md_nid, mgf1md_nid, saltlen, trailerfield;
  674. RSA_PSS_PARAMS_30 pss_params;
  675. if (!ossl_rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
  676. &saltlen, &trailerfield))
  677. goto err;
  678. md_nid = EVP_MD_get_type(md);
  679. mgf1md_nid = EVP_MD_get_type(mgf1md);
  680. if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
  681. || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
  682. || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
  683. mgf1md_nid)
  684. || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
  685. || !ossl_rsa_pss_params_30_todata(&pss_params, tmpl, NULL))
  686. goto err;
  687. selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
  688. }
  689. if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
  690. goto err;
  691. /* We export, the provider imports */
  692. rv = importer(to_keydata, selection, params);
  693. err:
  694. OSSL_PARAM_free(params);
  695. OSSL_PARAM_BLD_free(tmpl);
  696. return rv;
  697. }
  698. static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx,
  699. int rsa_type)
  700. {
  701. EVP_PKEY_CTX *pctx = vpctx;
  702. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  703. RSA *rsa = ossl_rsa_new_with_ctx(pctx->libctx);
  704. RSA_PSS_PARAMS_30 rsa_pss_params = { 0, };
  705. int pss_defaults_set = 0;
  706. int ok = 0;
  707. if (rsa == NULL) {
  708. ERR_raise(ERR_LIB_DH, ERR_R_RSA_LIB);
  709. return 0;
  710. }
  711. RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
  712. RSA_set_flags(rsa, rsa_type);
  713. if (!ossl_rsa_pss_params_30_fromdata(&rsa_pss_params, &pss_defaults_set,
  714. params, pctx->libctx))
  715. goto err;
  716. switch (rsa_type) {
  717. case RSA_FLAG_TYPE_RSA:
  718. /*
  719. * Were PSS parameters filled in?
  720. * In that case, something's wrong
  721. */
  722. if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params))
  723. goto err;
  724. break;
  725. case RSA_FLAG_TYPE_RSASSAPSS:
  726. /*
  727. * Were PSS parameters filled in? In that case, create the old
  728. * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key.
  729. */
  730. if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) {
  731. /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
  732. int mdnid = ossl_rsa_pss_params_30_hashalg(&rsa_pss_params);
  733. int mgf1mdnid = ossl_rsa_pss_params_30_maskgenhashalg(&rsa_pss_params);
  734. int saltlen = ossl_rsa_pss_params_30_saltlen(&rsa_pss_params);
  735. const EVP_MD *md = EVP_get_digestbynid(mdnid);
  736. const EVP_MD *mgf1md = EVP_get_digestbynid(mgf1mdnid);
  737. if ((rsa->pss = ossl_rsa_pss_params_create(md, mgf1md,
  738. saltlen)) == NULL)
  739. goto err;
  740. }
  741. break;
  742. default:
  743. /* RSA key sub-types we don't know how to handle yet */
  744. goto err;
  745. }
  746. if (!ossl_rsa_fromdata(rsa, params, 1))
  747. goto err;
  748. switch (rsa_type) {
  749. case RSA_FLAG_TYPE_RSA:
  750. ok = EVP_PKEY_assign_RSA(pkey, rsa);
  751. break;
  752. case RSA_FLAG_TYPE_RSASSAPSS:
  753. ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa);
  754. break;
  755. }
  756. err:
  757. if (!ok)
  758. RSA_free(rsa);
  759. return ok;
  760. }
  761. static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  762. OSSL_FUNC_keymgmt_import_fn *importer,
  763. OSSL_LIB_CTX *libctx, const char *propq)
  764. {
  765. return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata,
  766. importer, libctx, propq);
  767. }
  768. static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  769. OSSL_FUNC_keymgmt_import_fn *importer,
  770. OSSL_LIB_CTX *libctx, const char *propq)
  771. {
  772. return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata,
  773. importer, libctx, propq);
  774. }
  775. static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  776. {
  777. return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA);
  778. }
  779. static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  780. {
  781. return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS);
  782. }
  783. static int rsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
  784. {
  785. RSA *rsa = from->pkey.rsa;
  786. RSA *dupkey = NULL;
  787. int ret;
  788. if (rsa != NULL) {
  789. dupkey = ossl_rsa_dup(rsa, OSSL_KEYMGMT_SELECT_ALL);
  790. if (dupkey == NULL)
  791. return 0;
  792. }
  793. ret = EVP_PKEY_assign(to, from->type, dupkey);
  794. if (!ret)
  795. RSA_free(dupkey);
  796. return ret;
  797. }
  798. const EVP_PKEY_ASN1_METHOD ossl_rsa_asn1_meths[2] = {
  799. {
  800. EVP_PKEY_RSA,
  801. EVP_PKEY_RSA,
  802. ASN1_PKEY_SIGPARAM_NULL,
  803. "RSA",
  804. "OpenSSL RSA method",
  805. rsa_pub_decode,
  806. rsa_pub_encode,
  807. rsa_pub_cmp,
  808. rsa_pub_print,
  809. rsa_priv_decode,
  810. rsa_priv_encode,
  811. rsa_priv_print,
  812. int_rsa_size,
  813. rsa_bits,
  814. rsa_security_bits,
  815. 0, 0, 0, 0, 0, 0,
  816. rsa_sig_print,
  817. int_rsa_free,
  818. rsa_pkey_ctrl,
  819. old_rsa_priv_decode,
  820. old_rsa_priv_encode,
  821. rsa_item_verify,
  822. rsa_item_sign,
  823. rsa_sig_info_set,
  824. rsa_pkey_check,
  825. 0, 0,
  826. 0, 0, 0, 0,
  827. rsa_pkey_dirty_cnt,
  828. rsa_pkey_export_to,
  829. rsa_pkey_import_from,
  830. rsa_pkey_copy
  831. },
  832. {
  833. EVP_PKEY_RSA2,
  834. EVP_PKEY_RSA,
  835. ASN1_PKEY_ALIAS}
  836. };
  837. const EVP_PKEY_ASN1_METHOD ossl_rsa_pss_asn1_meth = {
  838. EVP_PKEY_RSA_PSS,
  839. EVP_PKEY_RSA_PSS,
  840. ASN1_PKEY_SIGPARAM_NULL,
  841. "RSA-PSS",
  842. "OpenSSL RSA-PSS method",
  843. rsa_pub_decode,
  844. rsa_pub_encode,
  845. rsa_pub_cmp,
  846. rsa_pub_print,
  847. rsa_priv_decode,
  848. rsa_priv_encode,
  849. rsa_priv_print,
  850. int_rsa_size,
  851. rsa_bits,
  852. rsa_security_bits,
  853. 0, 0, 0, 0, 0, 0,
  854. rsa_sig_print,
  855. int_rsa_free,
  856. rsa_pkey_ctrl,
  857. 0, 0,
  858. rsa_item_verify,
  859. rsa_item_sign,
  860. rsa_sig_info_set,
  861. rsa_pkey_check,
  862. 0, 0,
  863. 0, 0, 0, 0,
  864. rsa_pkey_dirty_cnt,
  865. rsa_pss_pkey_export_to,
  866. rsa_pss_pkey_import_from,
  867. rsa_pkey_copy
  868. };