rsa_ameth.c 29 KB

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