2
0

rsa_ameth.c 28 KB

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