rsa_ameth.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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_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_size(sigmd);
  406. } else if (saltlen == -2 || saltlen == -3) {
  407. saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
  408. if ((EVP_PKEY_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_type(md) != EVP_MD_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_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_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_type(mgf1md) && saltlen == EVP_MD_size(md))
  605. flags = X509_SIG_INFO_TLS;
  606. else
  607. flags = 0;
  608. /* Note: security bits half number of digest bits */
  609. secbits = EVP_MD_size(md) * 4;
  610. /*
  611. * SHA1 and MD5 are known to be broken. Reduce security bits so that
  612. * they're no longer accepted at security level 1. The real values don't
  613. * really matter as long as they're lower than 80, which is our security
  614. * level 1.
  615. * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
  616. * 2^63.4
  617. * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
  618. * puts a chosen-prefix attack for MD5 at 2^39.
  619. */
  620. if (mdnid == NID_sha1)
  621. secbits = 64;
  622. else if (mdnid == NID_md5_sha1)
  623. secbits = 68;
  624. else if (mdnid == NID_md5)
  625. secbits = 39;
  626. X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
  627. flags);
  628. rv = 1;
  629. err:
  630. RSA_PSS_PARAMS_free(pss);
  631. return rv;
  632. }
  633. static int rsa_pkey_check(const EVP_PKEY *pkey)
  634. {
  635. return RSA_check_key_ex(pkey->pkey.rsa, NULL);
  636. }
  637. static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
  638. {
  639. return pkey->pkey.rsa->dirty_cnt;
  640. }
  641. /*
  642. * There is no need to do RSA_test_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS)
  643. * checks in this method since the caller tests EVP_KEYMGMT_is_a() first.
  644. */
  645. static int rsa_int_export_to(const EVP_PKEY *from, int rsa_type,
  646. void *to_keydata, EVP_KEYMGMT *to_keymgmt,
  647. OSSL_LIB_CTX *libctx, const char *propq)
  648. {
  649. RSA *rsa = from->pkey.rsa;
  650. OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
  651. OSSL_PARAM *params = NULL;
  652. int selection = 0;
  653. int rv = 0;
  654. if (tmpl == NULL)
  655. return 0;
  656. /*
  657. * If the RSA method is foreign, then we can't be sure of anything, and
  658. * can therefore not export or pretend to export.
  659. */
  660. if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
  661. goto err;
  662. /* Public parameters must always be present */
  663. if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL)
  664. goto err;
  665. if (!ossl_rsa_todata(rsa, tmpl, NULL))
  666. goto err;
  667. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  668. if (RSA_get0_d(rsa) != NULL)
  669. selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  670. if (rsa->pss != NULL) {
  671. const EVP_MD *md = NULL, *mgf1md = NULL;
  672. int md_nid, mgf1md_nid, saltlen, trailerfield;
  673. RSA_PSS_PARAMS_30 pss_params;
  674. if (!ossl_rsa_pss_get_param_unverified(rsa->pss, &md, &mgf1md,
  675. &saltlen, &trailerfield))
  676. goto err;
  677. md_nid = EVP_MD_type(md);
  678. mgf1md_nid = EVP_MD_type(mgf1md);
  679. if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
  680. || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
  681. || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
  682. mgf1md_nid)
  683. || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
  684. || !ossl_rsa_pss_params_30_todata(&pss_params, tmpl, NULL))
  685. goto err;
  686. selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
  687. }
  688. if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
  689. goto err;
  690. /* We export, the provider imports */
  691. rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
  692. err:
  693. OSSL_PARAM_BLD_free_params(params);
  694. OSSL_PARAM_BLD_free(tmpl);
  695. return rv;
  696. }
  697. static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx,
  698. int rsa_type)
  699. {
  700. EVP_PKEY_CTX *pctx = vpctx;
  701. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  702. RSA *rsa = ossl_rsa_new_with_ctx(pctx->libctx);
  703. RSA_PSS_PARAMS_30 rsa_pss_params = { 0, };
  704. int pss_defaults_set = 0;
  705. int ok = 0;
  706. if (rsa == NULL) {
  707. ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
  708. return 0;
  709. }
  710. RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
  711. RSA_set_flags(rsa, rsa_type);
  712. if (!ossl_rsa_pss_params_30_fromdata(&rsa_pss_params, &pss_defaults_set,
  713. params, pctx->libctx))
  714. goto err;
  715. switch (rsa_type) {
  716. case RSA_FLAG_TYPE_RSA:
  717. /*
  718. * Were PSS parameters filled in?
  719. * In that case, something's wrong
  720. */
  721. if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params))
  722. goto err;
  723. break;
  724. case RSA_FLAG_TYPE_RSASSAPSS:
  725. /*
  726. * Were PSS parameters filled in? In that case, create the old
  727. * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key.
  728. */
  729. if (!ossl_rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) {
  730. /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
  731. int mdnid = ossl_rsa_pss_params_30_hashalg(&rsa_pss_params);
  732. int mgf1mdnid = ossl_rsa_pss_params_30_maskgenhashalg(&rsa_pss_params);
  733. int saltlen = ossl_rsa_pss_params_30_saltlen(&rsa_pss_params);
  734. const EVP_MD *md = EVP_get_digestbynid(mdnid);
  735. const EVP_MD *mgf1md = EVP_get_digestbynid(mgf1mdnid);
  736. if ((rsa->pss = ossl_rsa_pss_params_create(md, mgf1md,
  737. saltlen)) == NULL)
  738. goto err;
  739. }
  740. break;
  741. default:
  742. /* RSA key sub-types we don't know how to handle yet */
  743. goto err;
  744. }
  745. if (!ossl_rsa_fromdata(rsa, params))
  746. goto err;
  747. switch (rsa_type) {
  748. case RSA_FLAG_TYPE_RSA:
  749. ok = EVP_PKEY_assign_RSA(pkey, rsa);
  750. break;
  751. case RSA_FLAG_TYPE_RSASSAPSS:
  752. ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa);
  753. break;
  754. }
  755. err:
  756. if (!ok)
  757. RSA_free(rsa);
  758. return ok;
  759. }
  760. static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  761. EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx,
  762. const char *propq)
  763. {
  764. return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata,
  765. to_keymgmt, libctx, propq);
  766. }
  767. static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  768. EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx,
  769. const char *propq)
  770. {
  771. return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata,
  772. to_keymgmt, libctx, propq);
  773. }
  774. static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  775. {
  776. return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA);
  777. }
  778. static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  779. {
  780. return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS);
  781. }
  782. static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f)
  783. {
  784. if (f != NULL && (*out = BN_dup(f)) == NULL)
  785. return 0;
  786. return 1;
  787. }
  788. static RSA *rsa_dup(const RSA *rsa)
  789. {
  790. RSA *dupkey = NULL;
  791. int pnum, i;
  792. /* Do not try to duplicate foreign RSA keys */
  793. if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
  794. return NULL;
  795. if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL)
  796. return NULL;
  797. /* private and public key */
  798. if (!rsa_bn_dup_check(&dupkey->n, rsa->n))
  799. goto err;
  800. if (!rsa_bn_dup_check(&dupkey->e, rsa->e))
  801. goto err;
  802. if (!rsa_bn_dup_check(&dupkey->d, rsa->d))
  803. goto err;
  804. /* factors and crt params */
  805. if (!rsa_bn_dup_check(&dupkey->p, rsa->p))
  806. goto err;
  807. if (!rsa_bn_dup_check(&dupkey->q, rsa->q))
  808. goto err;
  809. if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1))
  810. goto err;
  811. if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1))
  812. goto err;
  813. if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp))
  814. goto err;
  815. /* multiprime */
  816. pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
  817. if (pnum > 0) {
  818. dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  819. for (i = 0; i < pnum; i++) {
  820. const RSA_PRIME_INFO *pinfo = NULL;
  821. RSA_PRIME_INFO *duppinfo = NULL;
  822. if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL) {
  823. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  824. goto err;
  825. }
  826. /* push first so cleanup in error case works */
  827. (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo);
  828. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  829. if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r))
  830. goto err;
  831. if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d))
  832. goto err;
  833. if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t))
  834. goto err;
  835. }
  836. if (!ossl_rsa_multip_calc_product(dupkey))
  837. goto err;
  838. }
  839. dupkey->version = rsa->version;
  840. dupkey->flags = rsa->flags;
  841. dupkey->pss_params = rsa->pss_params;
  842. if (rsa->pss != NULL) {
  843. dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss);
  844. if (rsa->pss->maskGenAlgorithm != NULL
  845. && dupkey->pss->maskGenAlgorithm == NULL) {
  846. dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm);
  847. if (dupkey->pss->maskHash == NULL)
  848. goto err;
  849. }
  850. }
  851. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA,
  852. &dupkey->ex_data, &rsa->ex_data))
  853. goto err;
  854. return dupkey;
  855. err:
  856. RSA_free(dupkey);
  857. return NULL;
  858. }
  859. static int rsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
  860. {
  861. RSA *rsa = from->pkey.rsa;
  862. RSA *dupkey = NULL;
  863. int ret;
  864. if (rsa != NULL) {
  865. dupkey = rsa_dup(rsa);
  866. if (dupkey == NULL)
  867. return 0;
  868. }
  869. ret = EVP_PKEY_assign(to, from->type, dupkey);
  870. if (!ret)
  871. RSA_free(dupkey);
  872. return ret;
  873. }
  874. const EVP_PKEY_ASN1_METHOD ossl_rsa_asn1_meths[2] = {
  875. {
  876. EVP_PKEY_RSA,
  877. EVP_PKEY_RSA,
  878. ASN1_PKEY_SIGPARAM_NULL,
  879. "RSA",
  880. "OpenSSL RSA method",
  881. rsa_pub_decode,
  882. rsa_pub_encode,
  883. rsa_pub_cmp,
  884. rsa_pub_print,
  885. rsa_priv_decode,
  886. rsa_priv_encode,
  887. rsa_priv_print,
  888. int_rsa_size,
  889. rsa_bits,
  890. rsa_security_bits,
  891. 0, 0, 0, 0, 0, 0,
  892. rsa_sig_print,
  893. int_rsa_free,
  894. rsa_pkey_ctrl,
  895. old_rsa_priv_decode,
  896. old_rsa_priv_encode,
  897. rsa_item_verify,
  898. rsa_item_sign,
  899. rsa_sig_info_set,
  900. rsa_pkey_check,
  901. 0, 0,
  902. 0, 0, 0, 0,
  903. rsa_pkey_dirty_cnt,
  904. rsa_pkey_export_to,
  905. rsa_pkey_import_from,
  906. rsa_pkey_copy
  907. },
  908. {
  909. EVP_PKEY_RSA2,
  910. EVP_PKEY_RSA,
  911. ASN1_PKEY_ALIAS}
  912. };
  913. const EVP_PKEY_ASN1_METHOD ossl_rsa_pss_asn1_meth = {
  914. EVP_PKEY_RSA_PSS,
  915. EVP_PKEY_RSA_PSS,
  916. ASN1_PKEY_SIGPARAM_NULL,
  917. "RSA-PSS",
  918. "OpenSSL RSA-PSS method",
  919. rsa_pub_decode,
  920. rsa_pub_encode,
  921. rsa_pub_cmp,
  922. rsa_pub_print,
  923. rsa_priv_decode,
  924. rsa_priv_encode,
  925. rsa_priv_print,
  926. int_rsa_size,
  927. rsa_bits,
  928. rsa_security_bits,
  929. 0, 0, 0, 0, 0, 0,
  930. rsa_sig_print,
  931. int_rsa_free,
  932. rsa_pkey_ctrl,
  933. 0, 0,
  934. rsa_item_verify,
  935. rsa_item_sign,
  936. rsa_sig_info_set,
  937. rsa_pkey_check,
  938. 0, 0,
  939. 0, 0, 0, 0,
  940. rsa_pkey_dirty_cnt,
  941. rsa_pss_pkey_export_to,
  942. rsa_pss_pkey_import_from,
  943. rsa_pkey_copy
  944. };