1
0

rsa_ameth.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/bn.h>
  14. #include <openssl/cms.h>
  15. #include "crypto/asn1.h"
  16. #include "crypto/evp.h"
  17. #include "rsa_local.h"
  18. #ifndef OPENSSL_NO_CMS
  19. static int rsa_cms_sign(CMS_SignerInfo *si);
  20. static int rsa_cms_verify(CMS_SignerInfo *si);
  21. static int rsa_cms_decrypt(CMS_RecipientInfo *ri);
  22. static int rsa_cms_encrypt(CMS_RecipientInfo *ri);
  23. #endif
  24. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg);
  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 (pkey->ameth->pkey_id != EVP_PKEY_RSA_PSS) {
  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_param_decode(RSA *rsa, const X509_ALGOR *alg)
  49. {
  50. const ASN1_OBJECT *algoid;
  51. const void *algp;
  52. int algptype;
  53. X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
  54. if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
  55. return 1;
  56. if (algptype == V_ASN1_UNDEF)
  57. return 1;
  58. if (algptype != V_ASN1_SEQUENCE) {
  59. RSAerr(RSA_F_RSA_PARAM_DECODE, RSA_R_INVALID_PSS_PARAMETERS);
  60. return 0;
  61. }
  62. rsa->pss = rsa_pss_decode(alg);
  63. if (rsa->pss == NULL)
  64. return 0;
  65. return 1;
  66. }
  67. static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  68. {
  69. unsigned char *penc = NULL;
  70. int penclen;
  71. ASN1_STRING *str;
  72. int strtype;
  73. if (!rsa_param_encode(pkey, &str, &strtype))
  74. return 0;
  75. penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc);
  76. if (penclen <= 0)
  77. return 0;
  78. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
  79. strtype, str, penc, penclen))
  80. return 1;
  81. OPENSSL_free(penc);
  82. return 0;
  83. }
  84. static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  85. {
  86. const unsigned char *p;
  87. int pklen;
  88. X509_ALGOR *alg;
  89. RSA *rsa = NULL;
  90. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &alg, pubkey))
  91. return 0;
  92. if ((rsa = d2i_RSAPublicKey(NULL, &p, pklen)) == NULL) {
  93. RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB);
  94. return 0;
  95. }
  96. if (!rsa_param_decode(rsa, alg)) {
  97. RSA_free(rsa);
  98. return 0;
  99. }
  100. if (!EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa)) {
  101. RSA_free(rsa);
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  107. {
  108. if (BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) != 0
  109. || BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) != 0)
  110. return 0;
  111. return 1;
  112. }
  113. static int old_rsa_priv_decode(EVP_PKEY *pkey,
  114. const unsigned char **pder, int derlen)
  115. {
  116. RSA *rsa;
  117. if ((rsa = d2i_RSAPrivateKey(NULL, pder, derlen)) == NULL) {
  118. RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  119. return 0;
  120. }
  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. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  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. RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  145. ASN1_STRING_free(str);
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. static int rsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  151. {
  152. const unsigned char *p;
  153. RSA *rsa;
  154. int pklen;
  155. const X509_ALGOR *alg;
  156. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8))
  157. return 0;
  158. rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
  159. if (rsa == NULL) {
  160. RSAerr(RSA_F_RSA_PRIV_DECODE, ERR_R_RSA_LIB);
  161. return 0;
  162. }
  163. if (!rsa_param_decode(rsa, alg)) {
  164. RSA_free(rsa);
  165. return 0;
  166. }
  167. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, rsa);
  168. return 1;
  169. }
  170. static int int_rsa_size(const EVP_PKEY *pkey)
  171. {
  172. return RSA_size(pkey->pkey.rsa);
  173. }
  174. static int rsa_bits(const EVP_PKEY *pkey)
  175. {
  176. return BN_num_bits(pkey->pkey.rsa->n);
  177. }
  178. static int rsa_security_bits(const EVP_PKEY *pkey)
  179. {
  180. return RSA_security_bits(pkey->pkey.rsa);
  181. }
  182. static void int_rsa_free(EVP_PKEY *pkey)
  183. {
  184. RSA_free(pkey->pkey.rsa);
  185. }
  186. static X509_ALGOR *rsa_mgf1_decode(X509_ALGOR *alg)
  187. {
  188. if (OBJ_obj2nid(alg->algorithm) != NID_mgf1)
  189. return NULL;
  190. return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
  191. alg->parameter);
  192. }
  193. static int rsa_pss_param_print(BIO *bp, int pss_key, RSA_PSS_PARAMS *pss,
  194. int indent)
  195. {
  196. int rv = 0;
  197. X509_ALGOR *maskHash = NULL;
  198. if (!BIO_indent(bp, indent, 128))
  199. goto err;
  200. if (pss_key) {
  201. if (pss == NULL) {
  202. if (BIO_puts(bp, "No PSS parameter restrictions\n") <= 0)
  203. return 0;
  204. return 1;
  205. } else {
  206. if (BIO_puts(bp, "PSS parameter restrictions:") <= 0)
  207. return 0;
  208. }
  209. } else if (pss == NULL) {
  210. if (BIO_puts(bp,"(INVALID PSS PARAMETERS)\n") <= 0)
  211. return 0;
  212. return 1;
  213. }
  214. if (BIO_puts(bp, "\n") <= 0)
  215. goto err;
  216. if (pss_key)
  217. indent += 2;
  218. if (!BIO_indent(bp, indent, 128))
  219. goto err;
  220. if (BIO_puts(bp, "Hash Algorithm: ") <= 0)
  221. goto err;
  222. if (pss->hashAlgorithm) {
  223. if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0)
  224. goto err;
  225. } else if (BIO_puts(bp, "sha1 (default)") <= 0) {
  226. goto err;
  227. }
  228. if (BIO_puts(bp, "\n") <= 0)
  229. goto err;
  230. if (!BIO_indent(bp, indent, 128))
  231. goto err;
  232. if (BIO_puts(bp, "Mask Algorithm: ") <= 0)
  233. goto err;
  234. if (pss->maskGenAlgorithm) {
  235. if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0)
  236. goto err;
  237. if (BIO_puts(bp, " with ") <= 0)
  238. goto err;
  239. maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
  240. if (maskHash != NULL) {
  241. if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0)
  242. goto err;
  243. } else if (BIO_puts(bp, "INVALID") <= 0) {
  244. goto err;
  245. }
  246. } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) {
  247. goto err;
  248. }
  249. BIO_puts(bp, "\n");
  250. if (!BIO_indent(bp, indent, 128))
  251. goto err;
  252. if (BIO_printf(bp, "%s Salt Length: 0x", pss_key ? "Minimum" : "") <= 0)
  253. goto err;
  254. if (pss->saltLength) {
  255. if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0)
  256. goto err;
  257. } else if (BIO_puts(bp, "14 (default)") <= 0) {
  258. goto err;
  259. }
  260. BIO_puts(bp, "\n");
  261. if (!BIO_indent(bp, indent, 128))
  262. goto err;
  263. if (BIO_puts(bp, "Trailer Field: 0x") <= 0)
  264. goto err;
  265. if (pss->trailerField) {
  266. if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0)
  267. goto err;
  268. } else if (BIO_puts(bp, "BC (default)") <= 0) {
  269. goto err;
  270. }
  271. BIO_puts(bp, "\n");
  272. rv = 1;
  273. err:
  274. X509_ALGOR_free(maskHash);
  275. return rv;
  276. }
  277. static int pkey_rsa_print(BIO *bp, const EVP_PKEY *pkey, int off, int priv)
  278. {
  279. const RSA *x = pkey->pkey.rsa;
  280. char *str;
  281. const char *s;
  282. int ret = 0, mod_len = 0, ex_primes;
  283. if (x->n != NULL)
  284. mod_len = BN_num_bits(x->n);
  285. ex_primes = sk_RSA_PRIME_INFO_num(x->prime_infos);
  286. if (!BIO_indent(bp, off, 128))
  287. goto err;
  288. if (BIO_printf(bp, "%s ", pkey_is_pss(pkey) ? "RSA-PSS" : "RSA") <= 0)
  289. goto err;
  290. if (priv && x->d) {
  291. if (BIO_printf(bp, "Private-Key: (%d bit, %d primes)\n",
  292. mod_len, ex_primes <= 0 ? 2 : ex_primes + 2) <= 0)
  293. goto err;
  294. str = "modulus:";
  295. s = "publicExponent:";
  296. } else {
  297. if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
  298. goto err;
  299. str = "Modulus:";
  300. s = "Exponent:";
  301. }
  302. if (!ASN1_bn_print(bp, str, x->n, NULL, off))
  303. goto err;
  304. if (!ASN1_bn_print(bp, s, x->e, NULL, off))
  305. goto err;
  306. if (priv) {
  307. int i;
  308. if (!ASN1_bn_print(bp, "privateExponent:", x->d, NULL, off))
  309. goto err;
  310. if (!ASN1_bn_print(bp, "prime1:", x->p, NULL, off))
  311. goto err;
  312. if (!ASN1_bn_print(bp, "prime2:", x->q, NULL, off))
  313. goto err;
  314. if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, NULL, off))
  315. goto err;
  316. if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, NULL, off))
  317. goto err;
  318. if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, NULL, off))
  319. goto err;
  320. for (i = 0; i < sk_RSA_PRIME_INFO_num(x->prime_infos); i++) {
  321. /* print multi-prime info */
  322. BIGNUM *bn = NULL;
  323. RSA_PRIME_INFO *pinfo;
  324. int j;
  325. pinfo = sk_RSA_PRIME_INFO_value(x->prime_infos, i);
  326. for (j = 0; j < 3; j++) {
  327. if (!BIO_indent(bp, off, 128))
  328. goto err;
  329. switch (j) {
  330. case 0:
  331. if (BIO_printf(bp, "prime%d:", i + 3) <= 0)
  332. goto err;
  333. bn = pinfo->r;
  334. break;
  335. case 1:
  336. if (BIO_printf(bp, "exponent%d:", i + 3) <= 0)
  337. goto err;
  338. bn = pinfo->d;
  339. break;
  340. case 2:
  341. if (BIO_printf(bp, "coefficient%d:", i + 3) <= 0)
  342. goto err;
  343. bn = pinfo->t;
  344. break;
  345. default:
  346. break;
  347. }
  348. if (!ASN1_bn_print(bp, "", bn, NULL, off))
  349. goto err;
  350. }
  351. }
  352. }
  353. if (pkey_is_pss(pkey) && !rsa_pss_param_print(bp, 1, x->pss, off))
  354. goto err;
  355. ret = 1;
  356. err:
  357. return ret;
  358. }
  359. static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  360. ASN1_PCTX *ctx)
  361. {
  362. return pkey_rsa_print(bp, pkey, indent, 0);
  363. }
  364. static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  365. ASN1_PCTX *ctx)
  366. {
  367. return pkey_rsa_print(bp, pkey, indent, 1);
  368. }
  369. static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg)
  370. {
  371. RSA_PSS_PARAMS *pss;
  372. pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
  373. alg->parameter);
  374. if (pss == NULL)
  375. return NULL;
  376. if (pss->maskGenAlgorithm != NULL) {
  377. pss->maskHash = rsa_mgf1_decode(pss->maskGenAlgorithm);
  378. if (pss->maskHash == NULL) {
  379. RSA_PSS_PARAMS_free(pss);
  380. return NULL;
  381. }
  382. }
  383. return pss;
  384. }
  385. static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  386. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
  387. {
  388. if (OBJ_obj2nid(sigalg->algorithm) == EVP_PKEY_RSA_PSS) {
  389. int rv;
  390. RSA_PSS_PARAMS *pss = rsa_pss_decode(sigalg);
  391. rv = rsa_pss_param_print(bp, 0, pss, indent);
  392. RSA_PSS_PARAMS_free(pss);
  393. if (!rv)
  394. return 0;
  395. } else if (!sig && BIO_puts(bp, "\n") <= 0) {
  396. return 0;
  397. }
  398. if (sig)
  399. return X509_signature_dump(bp, sig, indent);
  400. return 1;
  401. }
  402. static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  403. {
  404. X509_ALGOR *alg = NULL;
  405. const EVP_MD *md;
  406. const EVP_MD *mgf1md;
  407. int min_saltlen;
  408. switch (op) {
  409. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  410. if (arg1 == 0)
  411. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
  412. break;
  413. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  414. if (pkey_is_pss(pkey))
  415. return -2;
  416. if (arg1 == 0)
  417. PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
  418. break;
  419. #ifndef OPENSSL_NO_CMS
  420. case ASN1_PKEY_CTRL_CMS_SIGN:
  421. if (arg1 == 0)
  422. return rsa_cms_sign(arg2);
  423. else if (arg1 == 1)
  424. return rsa_cms_verify(arg2);
  425. break;
  426. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  427. if (pkey_is_pss(pkey))
  428. return -2;
  429. if (arg1 == 0)
  430. return rsa_cms_encrypt(arg2);
  431. else if (arg1 == 1)
  432. return rsa_cms_decrypt(arg2);
  433. break;
  434. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  435. if (pkey_is_pss(pkey))
  436. return -2;
  437. *(int *)arg2 = CMS_RECIPINFO_TRANS;
  438. return 1;
  439. #endif
  440. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  441. if (pkey->pkey.rsa->pss != NULL) {
  442. if (!rsa_pss_get_param(pkey->pkey.rsa->pss, &md, &mgf1md,
  443. &min_saltlen)) {
  444. RSAerr(0, ERR_R_INTERNAL_ERROR);
  445. return 0;
  446. }
  447. *(int *)arg2 = EVP_MD_type(md);
  448. /* Return of 2 indicates this MD is mandatory */
  449. return 2;
  450. }
  451. *(int *)arg2 = NID_sha256;
  452. return 1;
  453. default:
  454. return -2;
  455. }
  456. if (alg)
  457. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  458. return 1;
  459. }
  460. /* allocate and set algorithm ID from EVP_MD, default SHA1 */
  461. static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
  462. {
  463. if (md == NULL || EVP_MD_type(md) == NID_sha1)
  464. return 1;
  465. *palg = X509_ALGOR_new();
  466. if (*palg == NULL)
  467. return 0;
  468. X509_ALGOR_set_md(*palg, md);
  469. return 1;
  470. }
  471. /* Allocate and set MGF1 algorithm ID from EVP_MD */
  472. static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
  473. {
  474. X509_ALGOR *algtmp = NULL;
  475. ASN1_STRING *stmp = NULL;
  476. *palg = NULL;
  477. if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
  478. return 1;
  479. /* need to embed algorithm ID inside another */
  480. if (!rsa_md_to_algor(&algtmp, mgf1md))
  481. goto err;
  482. if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
  483. goto err;
  484. *palg = X509_ALGOR_new();
  485. if (*palg == NULL)
  486. goto err;
  487. X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
  488. stmp = NULL;
  489. err:
  490. ASN1_STRING_free(stmp);
  491. X509_ALGOR_free(algtmp);
  492. if (*palg)
  493. return 1;
  494. return 0;
  495. }
  496. /* convert algorithm ID to EVP_MD, default SHA1 */
  497. static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
  498. {
  499. const EVP_MD *md;
  500. if (!alg)
  501. return EVP_sha1();
  502. md = EVP_get_digestbyobj(alg->algorithm);
  503. if (md == NULL)
  504. RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
  505. return md;
  506. }
  507. /*
  508. * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
  509. * suitable for setting an AlgorithmIdentifier.
  510. */
  511. static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
  512. {
  513. const EVP_MD *sigmd, *mgf1md;
  514. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  515. int saltlen;
  516. if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
  517. return NULL;
  518. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  519. return NULL;
  520. if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
  521. return NULL;
  522. if (saltlen == -1) {
  523. saltlen = EVP_MD_size(sigmd);
  524. } else if (saltlen == -2 || saltlen == -3) {
  525. saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
  526. if ((EVP_PKEY_bits(pk) & 0x7) == 1)
  527. saltlen--;
  528. if (saltlen < 0)
  529. return NULL;
  530. }
  531. return rsa_pss_params_create(sigmd, mgf1md, saltlen);
  532. }
  533. RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
  534. const EVP_MD *mgf1md, int saltlen)
  535. {
  536. RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
  537. if (pss == NULL)
  538. goto err;
  539. if (saltlen != 20) {
  540. pss->saltLength = ASN1_INTEGER_new();
  541. if (pss->saltLength == NULL)
  542. goto err;
  543. if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
  544. goto err;
  545. }
  546. if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
  547. goto err;
  548. if (mgf1md == NULL)
  549. mgf1md = sigmd;
  550. if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
  551. goto err;
  552. if (!rsa_md_to_algor(&pss->maskHash, mgf1md))
  553. goto err;
  554. return pss;
  555. err:
  556. RSA_PSS_PARAMS_free(pss);
  557. return NULL;
  558. }
  559. static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
  560. {
  561. RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
  562. ASN1_STRING *os;
  563. if (pss == NULL)
  564. return NULL;
  565. os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
  566. RSA_PSS_PARAMS_free(pss);
  567. return os;
  568. }
  569. /*
  570. * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
  571. * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
  572. * passed to pkctx instead.
  573. */
  574. static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
  575. X509_ALGOR *sigalg, EVP_PKEY *pkey)
  576. {
  577. int rv = -1;
  578. int saltlen;
  579. const EVP_MD *mgf1md = NULL, *md = NULL;
  580. RSA_PSS_PARAMS *pss;
  581. /* Sanity check: make sure it is PSS */
  582. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  583. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  584. return -1;
  585. }
  586. /* Decode PSS parameters */
  587. pss = rsa_pss_decode(sigalg);
  588. if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
  589. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
  590. goto err;
  591. }
  592. /* We have all parameters now set up context */
  593. if (pkey) {
  594. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
  595. goto err;
  596. } else {
  597. const EVP_MD *checkmd;
  598. if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
  599. goto err;
  600. if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
  601. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
  602. goto err;
  603. }
  604. }
  605. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
  606. goto err;
  607. if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
  608. goto err;
  609. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  610. goto err;
  611. /* Carry on */
  612. rv = 1;
  613. err:
  614. RSA_PSS_PARAMS_free(pss);
  615. return rv;
  616. }
  617. int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
  618. const EVP_MD **pmgf1md, int *psaltlen)
  619. {
  620. if (pss == NULL)
  621. return 0;
  622. *pmd = rsa_algor_to_md(pss->hashAlgorithm);
  623. if (*pmd == NULL)
  624. return 0;
  625. *pmgf1md = rsa_algor_to_md(pss->maskHash);
  626. if (*pmgf1md == NULL)
  627. return 0;
  628. if (pss->saltLength) {
  629. *psaltlen = ASN1_INTEGER_get(pss->saltLength);
  630. if (*psaltlen < 0) {
  631. RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_SALT_LENGTH);
  632. return 0;
  633. }
  634. } else {
  635. *psaltlen = 20;
  636. }
  637. /*
  638. * low-level routines support only trailer field 0xbc (value 1) and
  639. * PKCS#1 says we should reject any other value anyway.
  640. */
  641. if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
  642. RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_TRAILER);
  643. return 0;
  644. }
  645. return 1;
  646. }
  647. #ifndef OPENSSL_NO_CMS
  648. static int rsa_cms_verify(CMS_SignerInfo *si)
  649. {
  650. int nid, nid2;
  651. X509_ALGOR *alg;
  652. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  653. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  654. nid = OBJ_obj2nid(alg->algorithm);
  655. if (nid == EVP_PKEY_RSA_PSS)
  656. return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
  657. /* Only PSS allowed for PSS keys */
  658. if (pkey_ctx_is_pss(pkctx)) {
  659. RSAerr(RSA_F_RSA_CMS_VERIFY, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  660. return 0;
  661. }
  662. if (nid == NID_rsaEncryption)
  663. return 1;
  664. /* Workaround for some implementation that use a signature OID */
  665. if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
  666. if (nid2 == NID_rsaEncryption)
  667. return 1;
  668. }
  669. return 0;
  670. }
  671. #endif
  672. /*
  673. * Customised RSA item verification routine. This is called when a signature
  674. * is encountered requiring special handling. We currently only handle PSS.
  675. */
  676. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  677. X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
  678. EVP_PKEY *pkey)
  679. {
  680. /* Sanity check: make sure it is PSS */
  681. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  682. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  683. return -1;
  684. }
  685. if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
  686. /* Carry on */
  687. return 2;
  688. }
  689. return -1;
  690. }
  691. #ifndef OPENSSL_NO_CMS
  692. static int rsa_cms_sign(CMS_SignerInfo *si)
  693. {
  694. int pad_mode = RSA_PKCS1_PADDING;
  695. X509_ALGOR *alg;
  696. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  697. ASN1_STRING *os = NULL;
  698. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  699. if (pkctx) {
  700. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  701. return 0;
  702. }
  703. if (pad_mode == RSA_PKCS1_PADDING) {
  704. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  705. return 1;
  706. }
  707. /* We don't support it */
  708. if (pad_mode != RSA_PKCS1_PSS_PADDING)
  709. return 0;
  710. os = rsa_ctx_to_pss_string(pkctx);
  711. if (!os)
  712. return 0;
  713. X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
  714. return 1;
  715. }
  716. #endif
  717. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  718. X509_ALGOR *alg1, X509_ALGOR *alg2,
  719. ASN1_BIT_STRING *sig)
  720. {
  721. int pad_mode;
  722. EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
  723. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  724. return 0;
  725. if (pad_mode == RSA_PKCS1_PADDING)
  726. return 2;
  727. if (pad_mode == RSA_PKCS1_PSS_PADDING) {
  728. ASN1_STRING *os1 = NULL;
  729. os1 = rsa_ctx_to_pss_string(pkctx);
  730. if (!os1)
  731. return 0;
  732. /* Duplicate parameters if we have to */
  733. if (alg2) {
  734. ASN1_STRING *os2 = ASN1_STRING_dup(os1);
  735. if (!os2) {
  736. ASN1_STRING_free(os1);
  737. return 0;
  738. }
  739. X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  740. V_ASN1_SEQUENCE, os2);
  741. }
  742. X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  743. V_ASN1_SEQUENCE, os1);
  744. return 3;
  745. }
  746. return 2;
  747. }
  748. static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
  749. const ASN1_STRING *sig)
  750. {
  751. int rv = 0;
  752. int mdnid, saltlen;
  753. uint32_t flags;
  754. const EVP_MD *mgf1md = NULL, *md = NULL;
  755. RSA_PSS_PARAMS *pss;
  756. /* Sanity check: make sure it is PSS */
  757. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
  758. return 0;
  759. /* Decode PSS parameters */
  760. pss = rsa_pss_decode(sigalg);
  761. if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
  762. goto err;
  763. mdnid = EVP_MD_type(md);
  764. /*
  765. * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
  766. * match and salt length must equal digest size
  767. */
  768. if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
  769. && mdnid == EVP_MD_type(mgf1md) && saltlen == EVP_MD_size(md))
  770. flags = X509_SIG_INFO_TLS;
  771. else
  772. flags = 0;
  773. /* Note: security bits half number of digest bits */
  774. X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, EVP_MD_size(md) * 4,
  775. flags);
  776. rv = 1;
  777. err:
  778. RSA_PSS_PARAMS_free(pss);
  779. return rv;
  780. }
  781. #ifndef OPENSSL_NO_CMS
  782. static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
  783. {
  784. RSA_OAEP_PARAMS *oaep;
  785. oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
  786. alg->parameter);
  787. if (oaep == NULL)
  788. return NULL;
  789. if (oaep->maskGenFunc != NULL) {
  790. oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
  791. if (oaep->maskHash == NULL) {
  792. RSA_OAEP_PARAMS_free(oaep);
  793. return NULL;
  794. }
  795. }
  796. return oaep;
  797. }
  798. static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
  799. {
  800. EVP_PKEY_CTX *pkctx;
  801. X509_ALGOR *cmsalg;
  802. int nid;
  803. int rv = -1;
  804. unsigned char *label = NULL;
  805. int labellen = 0;
  806. const EVP_MD *mgf1md = NULL, *md = NULL;
  807. RSA_OAEP_PARAMS *oaep;
  808. pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  809. if (pkctx == NULL)
  810. return 0;
  811. if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
  812. return -1;
  813. nid = OBJ_obj2nid(cmsalg->algorithm);
  814. if (nid == NID_rsaEncryption)
  815. return 1;
  816. if (nid != NID_rsaesOaep) {
  817. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
  818. return -1;
  819. }
  820. /* Decode OAEP parameters */
  821. oaep = rsa_oaep_decode(cmsalg);
  822. if (oaep == NULL) {
  823. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
  824. goto err;
  825. }
  826. mgf1md = rsa_algor_to_md(oaep->maskHash);
  827. if (mgf1md == NULL)
  828. goto err;
  829. md = rsa_algor_to_md(oaep->hashFunc);
  830. if (md == NULL)
  831. goto err;
  832. if (oaep->pSourceFunc != NULL) {
  833. X509_ALGOR *plab = oaep->pSourceFunc;
  834. if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
  835. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
  836. goto err;
  837. }
  838. if (plab->parameter->type != V_ASN1_OCTET_STRING) {
  839. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
  840. goto err;
  841. }
  842. label = plab->parameter->value.octet_string->data;
  843. /* Stop label being freed when OAEP parameters are freed */
  844. plab->parameter->value.octet_string->data = NULL;
  845. labellen = plab->parameter->value.octet_string->length;
  846. }
  847. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  848. goto err;
  849. if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
  850. goto err;
  851. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  852. goto err;
  853. if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
  854. goto err;
  855. /* Carry on */
  856. rv = 1;
  857. err:
  858. RSA_OAEP_PARAMS_free(oaep);
  859. return rv;
  860. }
  861. static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
  862. {
  863. const EVP_MD *md, *mgf1md;
  864. RSA_OAEP_PARAMS *oaep = NULL;
  865. ASN1_STRING *os = NULL;
  866. X509_ALGOR *alg;
  867. EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  868. int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
  869. unsigned char *label;
  870. if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
  871. return 0;
  872. if (pkctx) {
  873. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  874. return 0;
  875. }
  876. if (pad_mode == RSA_PKCS1_PADDING) {
  877. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  878. return 1;
  879. }
  880. /* Not supported */
  881. if (pad_mode != RSA_PKCS1_OAEP_PADDING)
  882. return 0;
  883. if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
  884. goto err;
  885. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  886. goto err;
  887. labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
  888. if (labellen < 0)
  889. goto err;
  890. oaep = RSA_OAEP_PARAMS_new();
  891. if (oaep == NULL)
  892. goto err;
  893. if (!rsa_md_to_algor(&oaep->hashFunc, md))
  894. goto err;
  895. if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
  896. goto err;
  897. if (labellen > 0) {
  898. ASN1_OCTET_STRING *los;
  899. oaep->pSourceFunc = X509_ALGOR_new();
  900. if (oaep->pSourceFunc == NULL)
  901. goto err;
  902. los = ASN1_OCTET_STRING_new();
  903. if (los == NULL)
  904. goto err;
  905. if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
  906. ASN1_OCTET_STRING_free(los);
  907. goto err;
  908. }
  909. X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
  910. V_ASN1_OCTET_STRING, los);
  911. }
  912. /* create string with pss parameter encoding. */
  913. if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
  914. goto err;
  915. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
  916. os = NULL;
  917. rv = 1;
  918. err:
  919. RSA_OAEP_PARAMS_free(oaep);
  920. ASN1_STRING_free(os);
  921. return rv;
  922. }
  923. #endif
  924. static int rsa_pkey_check(const EVP_PKEY *pkey)
  925. {
  926. return RSA_check_key_ex(pkey->pkey.rsa, NULL);
  927. }
  928. const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
  929. {
  930. EVP_PKEY_RSA,
  931. EVP_PKEY_RSA,
  932. ASN1_PKEY_SIGPARAM_NULL,
  933. "RSA",
  934. "OpenSSL RSA method",
  935. rsa_pub_decode,
  936. rsa_pub_encode,
  937. rsa_pub_cmp,
  938. rsa_pub_print,
  939. rsa_priv_decode,
  940. rsa_priv_encode,
  941. rsa_priv_print,
  942. int_rsa_size,
  943. rsa_bits,
  944. rsa_security_bits,
  945. 0, 0, 0, 0, 0, 0,
  946. rsa_sig_print,
  947. int_rsa_free,
  948. rsa_pkey_ctrl,
  949. old_rsa_priv_decode,
  950. old_rsa_priv_encode,
  951. rsa_item_verify,
  952. rsa_item_sign,
  953. rsa_sig_info_set,
  954. rsa_pkey_check
  955. },
  956. {
  957. EVP_PKEY_RSA2,
  958. EVP_PKEY_RSA,
  959. ASN1_PKEY_ALIAS}
  960. };
  961. const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
  962. EVP_PKEY_RSA_PSS,
  963. EVP_PKEY_RSA_PSS,
  964. ASN1_PKEY_SIGPARAM_NULL,
  965. "RSA-PSS",
  966. "OpenSSL RSA-PSS method",
  967. rsa_pub_decode,
  968. rsa_pub_encode,
  969. rsa_pub_cmp,
  970. rsa_pub_print,
  971. rsa_priv_decode,
  972. rsa_priv_encode,
  973. rsa_priv_print,
  974. int_rsa_size,
  975. rsa_bits,
  976. rsa_security_bits,
  977. 0, 0, 0, 0, 0, 0,
  978. rsa_sig_print,
  979. int_rsa_free,
  980. rsa_pkey_ctrl,
  981. 0, 0,
  982. rsa_item_verify,
  983. rsa_item_sign,
  984. 0,
  985. rsa_pkey_check
  986. };