rsa_ameth.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * Copyright 2006-2020 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/cms.h>
  20. #include <openssl/core_names.h>
  21. #include <openssl/param_build.h>
  22. #include "crypto/asn1.h"
  23. #include "crypto/evp.h"
  24. #include "crypto/rsa.h"
  25. #include "rsa_local.h"
  26. #ifndef OPENSSL_NO_CMS
  27. static int rsa_cms_sign(CMS_SignerInfo *si);
  28. static int rsa_cms_verify(CMS_SignerInfo *si);
  29. static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
  30. static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
  31. #endif
  32. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg);
  33. /* Set any parameters associated with pkey */
  34. static int rsa_param_encode(const EVP_PKEY *pkey,
  35. ASN1_STRING **pstr, int *pstrtype)
  36. {
  37. const RSA *rsa = pkey->pkey.rsa;
  38. *pstr = NULL;
  39. /* If RSA it's just NULL type */
  40. if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != RSA_FLAG_TYPE_RSASSAPSS) {
  41. *pstrtype = V_ASN1_NULL;
  42. return 1;
  43. }
  44. /* If no PSS parameters we omit parameters entirely */
  45. if (rsa->pss == NULL) {
  46. *pstrtype = V_ASN1_UNDEF;
  47. return 1;
  48. }
  49. /* Encode PSS parameters */
  50. if (ASN1_item_pack(rsa->pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr) == NULL)
  51. return 0;
  52. *pstrtype = V_ASN1_SEQUENCE;
  53. return 1;
  54. }
  55. /* Decode any parameters and set them in RSA structure */
  56. static int rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
  57. {
  58. const ASN1_OBJECT *algoid;
  59. const void *algp;
  60. int algptype;
  61. X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
  62. if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
  63. return 1;
  64. if (algptype == V_ASN1_UNDEF)
  65. return 1;
  66. if (algptype != V_ASN1_SEQUENCE) {
  67. RSAerr(RSA_F_RSA_PARAM_DECODE, RSA_R_INVALID_PSS_PARAMETERS);
  68. return 0;
  69. }
  70. rsa->pss = rsa_pss_decode(alg);
  71. if (rsa->pss == NULL)
  72. return 0;
  73. return 1;
  74. }
  75. static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  76. {
  77. unsigned char *penc = NULL;
  78. int penclen;
  79. ASN1_STRING *str;
  80. int strtype;
  81. if (!rsa_param_encode(pkey, &str, &strtype))
  82. return 0;
  83. penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
  84. if (penclen <= 0)
  85. return 0;
  86. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
  87. strtype, str, penc, penclen))
  88. return 1;
  89. OPENSSL_free(penc);
  90. return 0;
  91. }
  92. static int rsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
  93. {
  94. const unsigned char *p;
  95. int pklen;
  96. X509_ALGOR *alg;
  97. RSA *rsa = NULL;
  98. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
  99. return 0;
  100. if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
  101. RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
  102. return 0;
  103. }
  104. if (!rsa_param_decode(rsa, alg)) {
  105. RSA_free(rsa);
  106. return 0;
  107. }
  108. if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) {
  109. RSA_free(rsa);
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  115. {
  116. if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
  117. || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
  118. return 0;
  119. return 1;
  120. }
  121. static int old_rsa_priv_decode(EVP_PKEY *pkey,
  122. const unsigned char **pder, int derlen)
  123. {
  124. RSA *rsa;
  125. if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
  126. RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  127. return 0;
  128. }
  129. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
  130. return 1;
  131. }
  132. static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
  133. {
  134. return i2d_RSAPrivateKey(pkey->pkey.rsa, pder);
  135. }
  136. static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  137. {
  138. unsigned char *rk = NULL;
  139. int rklen;
  140. ASN1_STRING *str;
  141. int strtype;
  142. if (!rsa_param_encode(pkey, &str, &strtype))
  143. return 0;
  144. rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk);
  145. if (rklen <= 0) {
  146. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  147. ASN1_STRING_free(str);
  148. return 0;
  149. }
  150. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
  151. strtype, str, rk, rklen)) {
  152. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  153. ASN1_STRING_free(str);
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  159. {
  160. const unsigned char *p;
  161. RSA *rsa;
  162. int pklen;
  163. const X509_ALGOR *alg;
  164. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8))
  165. return 0;
  166. rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
  167. if (rsa == NULL) {
  168. RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  169. return 0;
  170. }
  171. if (!rsa_param_decode(rsa, alg)) {
  172. RSA_free(rsa);
  173. return 0;
  174. }
  175. RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
  176. switch (pkey->ameth->pkey_id) {
  177. case EVP_PKEY_RSA:
  178. RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
  179. break;
  180. case EVP_PKEY_RSA_PSS:
  181. RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
  182. break;
  183. default:
  184. /* Leave the type bits zero */
  185. break;
  186. }
  187. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
  188. return 1;
  189. }
  190. static int int_rsa_size(const EVP_PKEY *pkey)
  191. {
  192. return RSA_size(pkey->pkey.rsa);
  193. }
  194. static int rsa_bits(const EVP_PKEY *pkey)
  195. {
  196. return BN_num_bits(pkey->pkey.rsa->n);
  197. }
  198. static int rsa_security_bits(const EVP_PKEY *pkey)
  199. {
  200. return RSA_security_bits(pkey->pkey.rsa);
  201. }
  202. static void int_rsa_free(EVP_PKEY *pkey)
  203. {
  204. RSA_free(pkey->pkey.rsa);
  205. }
  206. static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
  207. {
  208. if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
  209. return NULL;
  210. return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
  211. alg->parameter);
  212. }
  213. static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
  214. int indent)
  215. {
  216. int rv = 0;
  217. X509_ALGOR *maskHash = NULL;
  218. if (!BIO_indent(bp, indent, 128))
  219. goto err;
  220. if (pss_key) {
  221. if (pss == NULL) {
  222. if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
  223. return 0;
  224. return 1;
  225. } else {
  226. if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
  227. return 0;
  228. }
  229. } else if (pss == NULL) {
  230. if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
  231. return 0;
  232. return 1;
  233. }
  234. if (BIO_puts(bp, "\n") <= 0)
  235. goto err;
  236. if (pss_key)
  237. indent += 2;
  238. if (!BIO_indent(bp, indent, 128))
  239. goto err;
  240. if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
  241. goto err;
  242. if (pss->hashAlgorithm) {
  243. if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
  244. goto err;
  245. } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
  246. goto err;
  247. }
  248. if (BIO_puts(bp, "\n") <= 0)
  249. goto err;
  250. if (!BIO_indent(bp, indent, 128))
  251. goto err;
  252. if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
  253. goto err;
  254. if (pss->maskGenAlgorithm) {
  255. if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
  256. goto err;
  257. if (BIO_puts(bp, " with ") <= 0)
  258. goto err;
  259. maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
  260. if (maskHash != NULL) {
  261. if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
  262. goto err;
  263. } else if (BIO_puts(bp, "INVALID") <= 0) {
  264. goto err;
  265. }
  266. } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
  267. goto err;
  268. }
  269. BIO_puts(bp, "\n");
  270. if (!BIO_indent(bp, indent, 128))
  271. goto err;
  272. if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
  273. goto err;
  274. if (pss->saltLength) {
  275. if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
  276. goto err;
  277. } else if (BIO_puts(bp, "14 (default)") <= 0) {
  278. goto err;
  279. }
  280. BIO_puts(bp, "\n");
  281. if (!BIO_indent(bp, indent, 128))
  282. goto err;
  283. if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
  284. goto err;
  285. if (pss->trailerField) {
  286. if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
  287. goto err;
  288. } else if (BIO_puts(bp, "BC (default)") <= 0) {
  289. goto err;
  290. }
  291. BIO_puts(bp, "\n");
  292. rv = 1;
  293. err:
  294. X509_ALGOR_free(maskHash);
  295. return rv;
  296. }
  297. static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
  298. {
  299. const RSA *x = pkey->pkey.rsa;
  300. char *str;
  301. const char *s;
  302. int ret = 0, mod_len = 0, ex_primes;
  303. if (x->n != NULL)
  304. mod_len = BN_num_bits(x->n);
  305. ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos);
  306. if (!BIO_indent(bp, off, 128))
  307. goto err;
  308. if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ? "RSA-PSS" : "RSA") <= 0)
  309. goto err;
  310. if (priv && x->d) {
  311. if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n",
  312. mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0)
  313. goto err;
  314. str = "modulus:";
  315. s = "publicExponent:";
  316. } else {
  317. if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
  318. goto err;
  319. str = "Modulus:";
  320. s = "Exponent:";
  321. }
  322. if (!ASN1_bn_print(bp, str, x->n, NULL, off))
  323. goto err;
  324. if (!ASN1_bn_print(bp, s, x->e, NULL, off))
  325. goto err;
  326. if (priv) {
  327. int i;
  328. if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
  329. goto err;
  330. if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
  331. goto err;
  332. if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
  333. goto err;
  334. if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
  335. goto err;
  336. if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
  337. goto err;
  338. if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
  339. goto err;
  340. for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) {
  341. /* print multi-prime info */
  342. BIGNUM *bn = NULL;
  343. RSA_PRIME_INFO *pinfo;
  344. int j;
  345. pinfo = sk_RSA_PRIME_INFO_value(x->prime_infos, i);
  346. for (j = 0; j < 3; j++) {
  347. if (!BIO_indent(bp, off, 128))
  348. goto err;
  349. switch (j) {
  350. case 0:
  351. if (BIO_printf(bp, "prime%d:", i + 3) <= 0)
  352. goto err;
  353. bn = pinfo->r;
  354. break;
  355. case 1:
  356. if (BIO_printf(bp, "exponent%d:", i + 3) <= 0)
  357. goto err;
  358. bn = pinfo->d;
  359. break;
  360. case 2:
  361. if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0)
  362. goto err;
  363. bn = pinfo->t;
  364. break;
  365. default:
  366. break;
  367. }
  368. if (!ASN1_bn_print(bp, "", bn, NULL, off))
  369. goto err;
  370. }
  371. }
  372. }
  373. if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
  374. goto err;
  375. ret = 1;
  376. err:
  377. return ret;
  378. }
  379. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  380. ASN1_PCTX *ctx)
  381. {
  382. return pkey_rsa_print(bp, pkey, indent, 0);
  383. }
  384. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  385. ASN1_PCTX *ctx)
  386. {
  387. return pkey_rsa_print(bp, pkey, indent, 1);
  388. }
  389. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg)
  390. {
  391. RSA_PSS_PARAMS *pss;
  392. pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
  393. alg->parameter);
  394. if (pss == NULL)
  395. return NULL;
  396. if (pss->maskGenAlgorithm != NULL) {
  397. pss->maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
  398. if (pss->maskHash == NULL) {
  399. RSA_PSS_PARAMS_free(pss);
  400. return NULL;
  401. }
  402. }
  403. return pss;
  404. }
  405. static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  406. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  407. {
  408. if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
  409. int rv;
  410. RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
  411. rv = rsa_pss_param_print(bp, 0, pss, indent);
  412. RSA_PSS_PARAMS_free(pss);
  413. if (!rv)
  414. return 0;
  415. } else if (BIO_puts(bp, "\n") <= 0) {
  416. return 0;
  417. }
  418. if (sig)
  419. return X509_signature_dump(bp, sig, indent);
  420. return 1;
  421. }
  422. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  423. {
  424. X509_ALGOR *alg = NULL;
  425. const EVP_MD *md;
  426. const EVP_MD *mgf1md;
  427. int min_saltlen;
  428. switch (op) {
  429. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  430. if (arg1 == 0)
  431. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
  432. break;
  433. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  434. if (pkey_is_pss(pkey))
  435. return -2;
  436. if (arg1 == 0)
  437. PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
  438. break;
  439. #ifndef OPENSSL_NO_CMS
  440. case ASN1_PKEY_CTRL_CMS_SIGN:
  441. if (arg1 == 0)
  442. return rsa_cms_sign(arg2);
  443. else if (arg1 == 1)
  444. return rsa_cms_verify(arg2);
  445. break;
  446. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  447. if (pkey_is_pss(pkey))
  448. return -2;
  449. if (arg1 == 0)
  450. return rsa_cms_encrypt(arg2);
  451. else if (arg1 == 1)
  452. return rsa_cms_decrypt(arg2);
  453. break;
  454. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  455. if (pkey_is_pss(pkey))
  456. return -2;
  457. *(int *)arg2 = CMS_RECIPINFO_TRANS;
  458. return 1;
  459. #endif
  460. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  461. if (pkey->pkey.rsa->pss != NULL) {
  462. if (!rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md,
  463. &min_saltlen)) {
  464. RSAerr(0, ERR_R_INTERNAL_ERROR);
  465. return 0;
  466. }
  467. *(int *)arg2 = EVP_MD_type(md);
  468. /* Return of 2 indicates this MD is mandatory */
  469. return 2;
  470. }
  471. *(int *)arg2 = NID_sha256;
  472. return 1;
  473. default:
  474. return -2;
  475. }
  476. if (alg)
  477. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  478. return 1;
  479. }
  480. /* allocate and set algorithm ID from EVP_MD, default SHA1 */
  481. static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
  482. {
  483. if (md == NULL || EVP_MD_type(md) == NID_sha1)
  484. return 1;
  485. *palg = X509_ALGOR_new();
  486. if (*palg == NULL)
  487. return 0;
  488. X509_ALGOR_set_md(*palg, md);
  489. return 1;
  490. }
  491. /* Allocate and set MGF1 algorithm ID from EVP_MD */
  492. static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
  493. {
  494. X509_ALGOR *algtmp = NULL;
  495. ASN1_STRING *stmp = NULL;
  496. *palg = NULL;
  497. if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
  498. return 1;
  499. /* need to embed algorithm ID inside another */
  500. if (!rsa_md_to_algor(&algtmp, mgf1md))
  501. goto err;
  502. if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
  503. goto err;
  504. *palg = X509_ALGOR_new();
  505. if (*palg == NULL)
  506. goto err;
  507. X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
  508. stmp = NULL;
  509. err:
  510. ASN1_STRING_free(stmp);
  511. X509_ALGOR_free(algtmp);
  512. if (*palg)
  513. return 1;
  514. return 0;
  515. }
  516. /* convert algorithm ID to EVP_MD, default SHA1 */
  517. static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
  518. {
  519. const EVP_MD *md;
  520. if (!alg)
  521. return EVP_sha1();
  522. md = EVP_get_digestbyobj(alg->algorithm);
  523. if (md == NULL)
  524. RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
  525. return md;
  526. }
  527. /*
  528. * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
  529. * suitable for setting an AlgorithmIdentifier.
  530. */
  531. static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
  532. {
  533. const EVP_MD *sigmd, *mgf1md;
  534. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  535. RSA *rsa = EVP_PKEY_get0_RSA(pk);
  536. int saltlen;
  537. if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
  538. return NULL;
  539. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  540. return NULL;
  541. if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
  542. return NULL;
  543. if (saltlen == -1) {
  544. saltlen = EVP_MD_size(sigmd);
  545. } else if (saltlen == -2 || saltlen == -3) {
  546. saltlen = RSA_size(rsa) - EVP_MD_size(sigmd) - 2;
  547. if ((EVP_PKEY_bits(pk) & 0x7) == 1)
  548. saltlen--;
  549. if (saltlen < 0)
  550. return NULL;
  551. }
  552. return rsa_pss_params_create(sigmd, mgf1md, saltlen);
  553. }
  554. RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
  555. const EVP_MD *mgf1md, int saltlen)
  556. {
  557. RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
  558. if (pss == NULL)
  559. goto err;
  560. if (saltlen != 20) {
  561. pss->saltLength = ASN1_INTEGER_new();
  562. if (pss->saltLength == NULL)
  563. goto err;
  564. if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
  565. goto err;
  566. }
  567. if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
  568. goto err;
  569. if (mgf1md == NULL)
  570. mgf1md = sigmd;
  571. if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
  572. goto err;
  573. if (!rsa_md_to_algor(&pss->maskHash, mgf1md))
  574. goto err;
  575. return pss;
  576. err:
  577. RSA_PSS_PARAMS_free(pss);
  578. return NULL;
  579. }
  580. static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
  581. {
  582. RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
  583. ASN1_STRING *os;
  584. if (pss == NULL)
  585. return NULL;
  586. os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
  587. RSA_PSS_PARAMS_free(pss);
  588. return os;
  589. }
  590. /*
  591. * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
  592. * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
  593. * passed to pkctx instead.
  594. */
  595. static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
  596. X509_ALGOR *sigalg, EVP_PKEY *pkey)
  597. {
  598. int rv = -1;
  599. int saltlen;
  600. const EVP_MD *mgf1md = NULL, *md = NULL;
  601. RSA_PSS_PARAMS *pss;
  602. /* Sanity check: make sure it is PSS */
  603. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  604. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  605. return -1;
  606. }
  607. /* Decode PSS parameters */
  608. pss = rsa_pss_decode(sigalg);
  609. if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
  610. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
  611. goto err;
  612. }
  613. /* We have all parameters now set up context */
  614. if (pkey) {
  615. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
  616. goto err;
  617. } else {
  618. const EVP_MD *checkmd;
  619. if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
  620. goto err;
  621. if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
  622. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
  623. goto err;
  624. }
  625. }
  626. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
  627. goto err;
  628. if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
  629. goto err;
  630. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  631. goto err;
  632. /* Carry on */
  633. rv = 1;
  634. err:
  635. RSA_PSS_PARAMS_free(pss);
  636. return rv;
  637. }
  638. int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
  639. const EVP_MD **pmgf1md, int *psaltlen)
  640. {
  641. if (pss == NULL)
  642. return 0;
  643. *pmd = rsa_algor_to_md(pss->hashAlgorithm);
  644. if (*pmd == NULL)
  645. return 0;
  646. *pmgf1md = rsa_algor_to_md(pss->maskHash);
  647. if (*pmgf1md == NULL)
  648. return 0;
  649. if (pss->saltLength) {
  650. *psaltlen = ASN1_INTEGER_get(pss->saltLength);
  651. if (*psaltlen < 0) {
  652. RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_SALT_LENGTH);
  653. return 0;
  654. }
  655. } else {
  656. *psaltlen = 20;
  657. }
  658. /*
  659. * low-level routines support only trailer field 0xbc (value 1) and
  660. * PKCS#1 says we should reject any other value anyway.
  661. */
  662. if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
  663. RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_TRAILER);
  664. return 0;
  665. }
  666. return 1;
  667. }
  668. #ifndef OPENSSL_NO_CMS
  669. static int rsa_cms_verify(CMS_SignerInfo *si)
  670. {
  671. int nid, nid2;
  672. X509_ALGOR *alg;
  673. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  674. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  675. nid = OBJ_obj2nid(alg->algorithm);
  676. if (nid == EVP_PKEY_RSA_PSS)
  677. return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
  678. /* Only PSS allowed for PSS keys */
  679. if (pkey_ctx_is_pss(pkctx)) {
  680. RSAerr(RSA_F_RSA_CMS_VERIFY, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  681. return 0;
  682. }
  683. if (nid == NID_rsaEncryption)
  684. return 1;
  685. /* Workaround for some implementation that use a signature OID */
  686. if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
  687. if (nid2 == NID_rsaEncryption)
  688. return 1;
  689. }
  690. return 0;
  691. }
  692. #endif
  693. /*
  694. * Customised RSA item verification routine. This is called when a signature
  695. * is encountered requiring special handling. We currently only handle PSS.
  696. */
  697. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  698. X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
  699. EVP_PKEY *pkey)
  700. {
  701. /* Sanity check: make sure it is PSS */
  702. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  703. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  704. return -1;
  705. }
  706. if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
  707. /* Carry on */
  708. return 2;
  709. }
  710. return -1;
  711. }
  712. #ifndef OPENSSL_NO_CMS
  713. static int rsa_cms_sign(CMS_SignerInfo *si)
  714. {
  715. int pad_mode = RSA_PKCS1_PADDING;
  716. X509_ALGOR *alg;
  717. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  718. ASN1_STRING *os = NULL;
  719. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  720. if (pkctx) {
  721. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  722. return 0;
  723. }
  724. if (pad_mode == RSA_PKCS1_PADDING) {
  725. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  726. return 1;
  727. }
  728. /* We don't support it */
  729. if (pad_mode != RSA_PKCS1_PSS_PADDING)
  730. return 0;
  731. os = rsa_ctx_to_pss_string(pkctx);
  732. if (!os)
  733. return 0;
  734. X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
  735. return 1;
  736. }
  737. #endif
  738. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  739. X509_ALGOR *alg1, X509_ALGOR *alg2,
  740. ASN1_BIT_STRING *sig)
  741. {
  742. int pad_mode;
  743. EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
  744. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  745. return 0;
  746. if (pad_mode == RSA_PKCS1_PADDING)
  747. return 2;
  748. if (pad_mode == RSA_PKCS1_PSS_PADDING) {
  749. ASN1_STRING *os1 = NULL;
  750. os1 = rsa_ctx_to_pss_string(pkctx);
  751. if (!os1)
  752. return 0;
  753. /* Duplicate parameters if we have to */
  754. if (alg2) {
  755. ASN1_STRING *os2 = ASN1_STRING_dup(os1);
  756. if (!os2) {
  757. ASN1_STRING_free(os1);
  758. return 0;
  759. }
  760. X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  761. V_ASN1_SEQUENCE, os2);
  762. }
  763. X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  764. V_ASN1_SEQUENCE, os1);
  765. return 3;
  766. }
  767. return 2;
  768. }
  769. static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
  770. const ASN1_STRING *sig)
  771. {
  772. int rv = 0;
  773. int mdnid, saltlen;
  774. uint32_t flags;
  775. const EVP_MD *mgf1md = NULL, *md = NULL;
  776. RSA_PSS_PARAMS *pss;
  777. int secbits;
  778. /* Sanity check: make sure it is PSS */
  779. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
  780. return 0;
  781. /* Decode PSS parameters */
  782. pss = rsa_pss_decode(sigalg);
  783. if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
  784. goto err;
  785. mdnid = EVP_MD_type(md);
  786. /*
  787. * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
  788. * match and salt length must equal digest size
  789. */
  790. if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
  791. && mdnid == EVP_MD_type(mgf1md) && saltlen == EVP_MD_size(md))
  792. flags = X509_SIG_INFO_TLS;
  793. else
  794. flags = 0;
  795. /* Note: security bits half number of digest bits */
  796. secbits = EVP_MD_size(md) * 4;
  797. /*
  798. * SHA1 and MD5 are known to be broken. Reduce security bits so that
  799. * they're no longer accepted at security level 1. The real values don't
  800. * really matter as long as they're lower than 80, which is our security
  801. * level 1.
  802. * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
  803. * 2^63.4
  804. * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
  805. * puts a chosen-prefix attack for MD5 at 2^39.
  806. */
  807. if (mdnid == NID_sha1)
  808. secbits = 64;
  809. else if (mdnid == NID_md5_sha1)
  810. secbits = 68;
  811. else if (mdnid == NID_md5)
  812. secbits = 39;
  813. X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
  814. flags);
  815. rv = 1;
  816. err:
  817. RSA_PSS_PARAMS_free(pss);
  818. return rv;
  819. }
  820. #ifndef OPENSSL_NO_CMS
  821. static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
  822. {
  823. RSA_OAEP_PARAMS *oaep;
  824. oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
  825. alg->parameter);
  826. if (oaep == NULL)
  827. return NULL;
  828. if (oaep->maskGenFunc != NULL) {
  829. oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
  830. if (oaep->maskHash == NULL) {
  831. RSA_OAEP_PARAMS_free(oaep);
  832. return NULL;
  833. }
  834. }
  835. return oaep;
  836. }
  837. static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
  838. {
  839. EVP_PKEY_CTX *pkctx;
  840. X509_ALGOR *cmsalg;
  841. int nid;
  842. int rv = -1;
  843. unsigned char *label = NULL;
  844. int labellen = 0;
  845. const EVP_MD *mgf1md = NULL, *md = NULL;
  846. RSA_OAEP_PARAMS *oaep;
  847. pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  848. if (pkctx == NULL)
  849. return 0;
  850. if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
  851. return -1;
  852. nid = OBJ_obj2nid(cmsalg->algorithm);
  853. if (nid == NID_rsaEncryption)
  854. return 1;
  855. if (nid != NID_rsaesOaep) {
  856. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
  857. return -1;
  858. }
  859. /* Decode OAEP parameters */
  860. oaep = rsa_oaep_decode(cmsalg);
  861. if (oaep == NULL) {
  862. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
  863. goto err;
  864. }
  865. mgf1md = rsa_algor_to_md(oaep->maskHash);
  866. if (mgf1md == NULL)
  867. goto err;
  868. md = rsa_algor_to_md(oaep->hashFunc);
  869. if (md == NULL)
  870. goto err;
  871. if (oaep->pSourceFunc != NULL) {
  872. X509_ALGOR *plab = oaep->pSourceFunc;
  873. if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
  874. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
  875. goto err;
  876. }
  877. if (plab->parameter->type != V_ASN1_OCTET_STRING) {
  878. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
  879. goto err;
  880. }
  881. label = plab->parameter->value.octet_string->data;
  882. /* Stop label being freed when OAEP parameters are freed */
  883. plab->parameter->value.octet_string->data = NULL;
  884. labellen = plab->parameter->value.octet_string->length;
  885. }
  886. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  887. goto err;
  888. if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
  889. goto err;
  890. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  891. goto err;
  892. if (label != NULL
  893. && EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
  894. goto err;
  895. /* Carry on */
  896. rv = 1;
  897. err:
  898. RSA_OAEP_PARAMS_free(oaep);
  899. return rv;
  900. }
  901. static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
  902. {
  903. const EVP_MD *md, *mgf1md;
  904. RSA_OAEP_PARAMS *oaep = NULL;
  905. ASN1_STRING *os = NULL;
  906. X509_ALGOR *alg;
  907. EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  908. int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
  909. unsigned char *label;
  910. if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
  911. return 0;
  912. if (pkctx) {
  913. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  914. return 0;
  915. }
  916. if (pad_mode == RSA_PKCS1_PADDING) {
  917. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  918. return 1;
  919. }
  920. /* Not supported */
  921. if (pad_mode != RSA_PKCS1_OAEP_PADDING)
  922. return 0;
  923. if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
  924. goto err;
  925. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  926. goto err;
  927. labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
  928. if (labellen < 0)
  929. goto err;
  930. oaep = RSA_OAEP_PARAMS_new();
  931. if (oaep == NULL)
  932. goto err;
  933. if (!rsa_md_to_algor(&oaep->hashFunc, md))
  934. goto err;
  935. if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
  936. goto err;
  937. if (labellen > 0) {
  938. ASN1_OCTET_STRING *los;
  939. oaep->pSourceFunc = X509_ALGOR_new();
  940. if (oaep->pSourceFunc == NULL)
  941. goto err;
  942. los = ASN1_OCTET_STRING_new();
  943. if (los == NULL)
  944. goto err;
  945. if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
  946. ASN1_OCTET_STRING_free(los);
  947. goto err;
  948. }
  949. X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
  950. V_ASN1_OCTET_STRING, los);
  951. }
  952. /* create string with pss parameter encoding. */
  953. if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
  954. goto err;
  955. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
  956. os = NULL;
  957. rv = 1;
  958. err:
  959. RSA_OAEP_PARAMS_free(oaep);
  960. ASN1_STRING_free(os);
  961. return rv;
  962. }
  963. #endif
  964. static int rsa_pkey_check(const EVP_PKEY *pkey)
  965. {
  966. return RSA_check_key_ex(pkey->pkey.rsa, NULL);
  967. }
  968. static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
  969. {
  970. return pkey->pkey.rsa->dirty_cnt;
  971. }
  972. /*
  973. * For the moment, we trust the call path, where keys going through
  974. * rsa_pkey_export_to() match a KEYMGMT for the "RSA" keytype, while
  975. * keys going through rsa_pss_pkey_export_to() match a KEYMGMT for the
  976. * "RSA-PSS" keytype.
  977. * TODO(3.0) Investigate whether we should simply continue to trust the
  978. * call path, or if we should strengthen this function by checking that
  979. * |rsa_type| matches the RSA key subtype. The latter requires ensuring
  980. * that the type flag for the RSA key is properly set by other functions
  981. * in this file.
  982. */
  983. static int rsa_int_export_to(const EVP_PKEY *from, int rsa_type,
  984. void *to_keydata, EVP_KEYMGMT *to_keymgmt,
  985. OPENSSL_CTX *libctx, const char *propq)
  986. {
  987. RSA *rsa = from->pkey.rsa;
  988. OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
  989. OSSL_PARAM *params = NULL;
  990. int selection = 0;
  991. int rv = 0;
  992. if (tmpl == NULL)
  993. return 0;
  994. /*
  995. * If the RSA method is foreign, then we can't be sure of anything, and
  996. * can therefore not export or pretend to export.
  997. */
  998. if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
  999. goto err;
  1000. /* Public parameters must always be present */
  1001. if (RSA_get0_n(rsa) == NULL || RSA_get0_e(rsa) == NULL)
  1002. goto err;
  1003. if (!rsa_todata(rsa, tmpl, NULL))
  1004. goto err;
  1005. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  1006. if (RSA_get0_d(rsa) != NULL)
  1007. selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  1008. if (rsa->pss != NULL) {
  1009. const EVP_MD *md = NULL, *mgf1md = NULL;
  1010. int md_nid, mgf1md_nid, saltlen;
  1011. RSA_PSS_PARAMS_30 pss_params;
  1012. if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &saltlen))
  1013. goto err;
  1014. md_nid = EVP_MD_type(md);
  1015. mgf1md_nid = EVP_MD_type(mgf1md);
  1016. if (!rsa_pss_params_30_set_defaults(&pss_params)
  1017. || !rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
  1018. || !rsa_pss_params_30_set_maskgenhashalg(&pss_params, mgf1md_nid)
  1019. || !rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
  1020. || !rsa_pss_params_30_todata(&pss_params, propq, tmpl, NULL))
  1021. goto err;
  1022. selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
  1023. }
  1024. if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
  1025. goto err;
  1026. /* We export, the provider imports */
  1027. rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
  1028. err:
  1029. OSSL_PARAM_BLD_free_params(params);
  1030. OSSL_PARAM_BLD_free(tmpl);
  1031. return rv;
  1032. }
  1033. static int rsa_int_import_from(const OSSL_PARAM params[], void *vpctx,
  1034. int rsa_type)
  1035. {
  1036. EVP_PKEY_CTX *pctx = vpctx;
  1037. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  1038. RSA *rsa = rsa_new_with_ctx(pctx->libctx);
  1039. RSA_PSS_PARAMS_30 rsa_pss_params = { 0, };
  1040. int ok = 0;
  1041. if (rsa == NULL) {
  1042. ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
  1043. return 0;
  1044. }
  1045. RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
  1046. RSA_set_flags(rsa, rsa_type);
  1047. if (!rsa_pss_params_30_fromdata(&rsa_pss_params, params, pctx->libctx))
  1048. goto err;
  1049. switch (rsa_type) {
  1050. case RSA_FLAG_TYPE_RSA:
  1051. /*
  1052. * Were PSS parameters filled in?
  1053. * In that case, something's wrong
  1054. */
  1055. if (!rsa_pss_params_30_is_unrestricted(&rsa_pss_params))
  1056. goto err;
  1057. break;
  1058. case RSA_FLAG_TYPE_RSASSAPSS:
  1059. /*
  1060. * Were PSS parameters filled in? In that case, create the old
  1061. * RSA_PSS_PARAMS structure. Otherwise, this is an unrestricted key.
  1062. */
  1063. if (!rsa_pss_params_30_is_unrestricted(&rsa_pss_params)) {
  1064. /* Create the older RSA_PSS_PARAMS from RSA_PSS_PARAMS_30 data */
  1065. int mdnid = rsa_pss_params_30_hashalg(&rsa_pss_params);
  1066. int mgf1mdnid = rsa_pss_params_30_maskgenhashalg(&rsa_pss_params);
  1067. int saltlen = rsa_pss_params_30_saltlen(&rsa_pss_params);
  1068. const EVP_MD *md = EVP_get_digestbynid(mdnid);
  1069. const EVP_MD *mgf1md = EVP_get_digestbynid(mgf1mdnid);
  1070. if ((rsa->pss = rsa_pss_params_create(md, mgf1md, saltlen)) == NULL)
  1071. goto err;
  1072. }
  1073. break;
  1074. default:
  1075. /* RSA key sub-types we don't know how to handle yet */
  1076. goto err;
  1077. }
  1078. if (!rsa_fromdata(rsa, params))
  1079. goto err;
  1080. switch (rsa_type) {
  1081. case RSA_FLAG_TYPE_RSA:
  1082. ok = EVP_PKEY_assign_RSA(pkey, rsa);
  1083. break;
  1084. case RSA_FLAG_TYPE_RSASSAPSS:
  1085. ok = EVP_PKEY_assign(pkey, EVP_PKEY_RSA_PSS, rsa);
  1086. break;
  1087. }
  1088. err:
  1089. if (!ok)
  1090. RSA_free(rsa);
  1091. return ok;
  1092. }
  1093. static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  1094. EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
  1095. const char *propq)
  1096. {
  1097. return rsa_int_export_to(from, RSA_FLAG_TYPE_RSA, to_keydata,
  1098. to_keymgmt, libctx, propq);
  1099. }
  1100. static int rsa_pss_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  1101. EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
  1102. const char *propq)
  1103. {
  1104. return rsa_int_export_to(from, RSA_FLAG_TYPE_RSASSAPSS, to_keydata,
  1105. to_keymgmt, libctx, propq);
  1106. }
  1107. static int rsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  1108. {
  1109. return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSA);
  1110. }
  1111. static int rsa_pss_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  1112. {
  1113. return rsa_int_import_from(params, vpctx, RSA_FLAG_TYPE_RSASSAPSS);
  1114. }
  1115. const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
  1116. {
  1117. EVP_PKEY_RSA,
  1118. EVP_PKEY_RSA,
  1119. ASN1_PKEY_SIGPARAM_NULL,
  1120. "RSA",
  1121. "OpenSSL RSA method",
  1122. rsa_pub_decode,
  1123. rsa_pub_encode,
  1124. rsa_pub_cmp,
  1125. rsa_pub_print,
  1126. rsa_priv_decode,
  1127. rsa_priv_encode,
  1128. rsa_priv_print,
  1129. int_rsa_size,
  1130. rsa_bits,
  1131. rsa_security_bits,
  1132. 0, 0, 0, 0, 0, 0,
  1133. rsa_sig_print,
  1134. int_rsa_free,
  1135. rsa_pkey_ctrl,
  1136. old_rsa_priv_decode,
  1137. old_rsa_priv_encode,
  1138. rsa_item_verify,
  1139. rsa_item_sign,
  1140. rsa_sig_info_set,
  1141. rsa_pkey_check,
  1142. 0, 0,
  1143. 0, 0, 0, 0,
  1144. rsa_pkey_dirty_cnt,
  1145. rsa_pkey_export_to,
  1146. rsa_pkey_import_from
  1147. },
  1148. {
  1149. EVP_PKEY_RSA2,
  1150. EVP_PKEY_RSA,
  1151. ASN1_PKEY_ALIAS}
  1152. };
  1153. const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
  1154. EVP_PKEY_RSA_PSS,
  1155. EVP_PKEY_RSA_PSS,
  1156. ASN1_PKEY_SIGPARAM_NULL,
  1157. "RSA-PSS",
  1158. "OpenSSL RSA-PSS method",
  1159. rsa_pub_decode,
  1160. rsa_pub_encode,
  1161. rsa_pub_cmp,
  1162. rsa_pub_print,
  1163. rsa_priv_decode,
  1164. rsa_priv_encode,
  1165. rsa_priv_print,
  1166. int_rsa_size,
  1167. rsa_bits,
  1168. rsa_security_bits,
  1169. 0, 0, 0, 0, 0, 0,
  1170. rsa_sig_print,
  1171. int_rsa_free,
  1172. rsa_pkey_ctrl,
  1173. 0, 0,
  1174. rsa_item_verify,
  1175. rsa_item_sign,
  1176. 0,
  1177. rsa_pkey_check,
  1178. 0, 0,
  1179. 0, 0, 0, 0,
  1180. rsa_pkey_dirty_cnt,
  1181. rsa_pss_pkey_export_to,
  1182. rsa_pss_pkey_import_from
  1183. };