rsa_ameth.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Copyright 2006-2018 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 "internal/asn1_int.h"
  16. #include "internal/evp_int.h"
  17. #include "rsa_locl.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. switch (op) {
  406. case ASN1_PKEY_CTRL_PKCS7_SIGN:
  407. if (arg1 == 0)
  408. PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg);
  409. break;
  410. case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
  411. if (pkey_is_pss(pkey))
  412. return -2;
  413. if (arg1 == 0)
  414. PKCS7_RECIP_INFO_get0_alg(arg2, &alg);
  415. break;
  416. #ifndef OPENSSL_NO_CMS
  417. case ASN1_PKEY_CTRL_CMS_SIGN:
  418. if (arg1 == 0)
  419. return rsa_cms_sign(arg2);
  420. else if (arg1 == 1)
  421. return rsa_cms_verify(arg2);
  422. break;
  423. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  424. if (pkey_is_pss(pkey))
  425. return -2;
  426. if (arg1 == 0)
  427. return rsa_cms_encrypt(arg2);
  428. else if (arg1 == 1)
  429. return rsa_cms_decrypt(arg2);
  430. break;
  431. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  432. if (pkey_is_pss(pkey))
  433. return -2;
  434. *(int *)arg2 = CMS_RECIPINFO_TRANS;
  435. return 1;
  436. #endif
  437. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  438. *(int *)arg2 = NID_sha256;
  439. return 1;
  440. default:
  441. return -2;
  442. }
  443. if (alg)
  444. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  445. return 1;
  446. }
  447. /* allocate and set algorithm ID from EVP_MD, default SHA1 */
  448. static int rsa_md_to_algor(X509_ALGOR **palg, const EVP_MD *md)
  449. {
  450. if (md == NULL || EVP_MD_type(md) == NID_sha1)
  451. return 1;
  452. *palg = X509_ALGOR_new();
  453. if (*palg == NULL)
  454. return 0;
  455. X509_ALGOR_set_md(*palg, md);
  456. return 1;
  457. }
  458. /* Allocate and set MGF1 algorithm ID from EVP_MD */
  459. static int rsa_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md)
  460. {
  461. X509_ALGOR *algtmp = NULL;
  462. ASN1_STRING *stmp = NULL;
  463. *palg = NULL;
  464. if (mgf1md == NULL || EVP_MD_type(mgf1md) == NID_sha1)
  465. return 1;
  466. /* need to embed algorithm ID inside another */
  467. if (!rsa_md_to_algor(&algtmp, mgf1md))
  468. goto err;
  469. if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL)
  470. goto err;
  471. *palg = X509_ALGOR_new();
  472. if (*palg == NULL)
  473. goto err;
  474. X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp);
  475. stmp = NULL;
  476. err:
  477. ASN1_STRING_free(stmp);
  478. X509_ALGOR_free(algtmp);
  479. if (*palg)
  480. return 1;
  481. return 0;
  482. }
  483. /* convert algorithm ID to EVP_MD, default SHA1 */
  484. static const EVP_MD *rsa_algor_to_md(X509_ALGOR *alg)
  485. {
  486. const EVP_MD *md;
  487. if (!alg)
  488. return EVP_sha1();
  489. md = EVP_get_digestbyobj(alg->algorithm);
  490. if (md == NULL)
  491. RSAerr(RSA_F_RSA_ALGOR_TO_MD, RSA_R_UNKNOWN_DIGEST);
  492. return md;
  493. }
  494. /*
  495. * Convert EVP_PKEY_CTX in PSS mode into corresponding algorithm parameter,
  496. * suitable for setting an AlgorithmIdentifier.
  497. */
  498. static RSA_PSS_PARAMS *rsa_ctx_to_pss(EVP_PKEY_CTX *pkctx)
  499. {
  500. const EVP_MD *sigmd, *mgf1md;
  501. EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx);
  502. int saltlen;
  503. if (EVP_PKEY_CTX_get_signature_md(pkctx, &sigmd) <= 0)
  504. return NULL;
  505. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  506. return NULL;
  507. if (!EVP_PKEY_CTX_get_rsa_pss_saltlen(pkctx, &saltlen))
  508. return NULL;
  509. if (saltlen == -1) {
  510. saltlen = EVP_MD_size(sigmd);
  511. } else if (saltlen == -2) {
  512. saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2;
  513. if ((EVP_PKEY_bits(pk) & 0x7) == 1)
  514. saltlen--;
  515. }
  516. return rsa_pss_params_create(sigmd, mgf1md, saltlen);
  517. }
  518. RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
  519. const EVP_MD *mgf1md, int saltlen)
  520. {
  521. RSA_PSS_PARAMS *pss = RSA_PSS_PARAMS_new();
  522. if (pss == NULL)
  523. goto err;
  524. if (saltlen != 20) {
  525. pss->saltLength = ASN1_INTEGER_new();
  526. if (pss->saltLength == NULL)
  527. goto err;
  528. if (!ASN1_INTEGER_set(pss->saltLength, saltlen))
  529. goto err;
  530. }
  531. if (!rsa_md_to_algor(&pss->hashAlgorithm, sigmd))
  532. goto err;
  533. if (mgf1md == NULL)
  534. mgf1md = sigmd;
  535. if (!rsa_md_to_mgf1(&pss->maskGenAlgorithm, mgf1md))
  536. goto err;
  537. if (!rsa_md_to_algor(&pss->maskHash, mgf1md))
  538. goto err;
  539. return pss;
  540. err:
  541. RSA_PSS_PARAMS_free(pss);
  542. return NULL;
  543. }
  544. static ASN1_STRING *rsa_ctx_to_pss_string(EVP_PKEY_CTX *pkctx)
  545. {
  546. RSA_PSS_PARAMS *pss = rsa_ctx_to_pss(pkctx);
  547. ASN1_STRING *os;
  548. if (pss == NULL)
  549. return NULL;
  550. os = ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), NULL);
  551. RSA_PSS_PARAMS_free(pss);
  552. return os;
  553. }
  554. /*
  555. * From PSS AlgorithmIdentifier set public key parameters. If pkey isn't NULL
  556. * then the EVP_MD_CTX is setup and initialised. If it is NULL parameters are
  557. * passed to pkctx instead.
  558. */
  559. static int rsa_pss_to_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pkctx,
  560. X509_ALGOR *sigalg, EVP_PKEY *pkey)
  561. {
  562. int rv = -1;
  563. int saltlen;
  564. const EVP_MD *mgf1md = NULL, *md = NULL;
  565. RSA_PSS_PARAMS *pss;
  566. /* Sanity check: make sure it is PSS */
  567. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  568. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  569. return -1;
  570. }
  571. /* Decode PSS parameters */
  572. pss = rsa_pss_decode(sigalg);
  573. if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen)) {
  574. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_INVALID_PSS_PARAMETERS);
  575. goto err;
  576. }
  577. /* We have all parameters now set up context */
  578. if (pkey) {
  579. if (!EVP_DigestVerifyInit(ctx, &pkctx, md, NULL, pkey))
  580. goto err;
  581. } else {
  582. const EVP_MD *checkmd;
  583. if (EVP_PKEY_CTX_get_signature_md(pkctx, &checkmd) <= 0)
  584. goto err;
  585. if (EVP_MD_type(md) != EVP_MD_type(checkmd)) {
  586. RSAerr(RSA_F_RSA_PSS_TO_CTX, RSA_R_DIGEST_DOES_NOT_MATCH);
  587. goto err;
  588. }
  589. }
  590. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_PSS_PADDING) <= 0)
  591. goto err;
  592. if (EVP_PKEY_CTX_set_rsa_pss_saltlen(pkctx, saltlen) <= 0)
  593. goto err;
  594. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  595. goto err;
  596. /* Carry on */
  597. rv = 1;
  598. err:
  599. RSA_PSS_PARAMS_free(pss);
  600. return rv;
  601. }
  602. int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
  603. const EVP_MD **pmgf1md, int *psaltlen)
  604. {
  605. if (pss == NULL)
  606. return 0;
  607. *pmd = rsa_algor_to_md(pss->hashAlgorithm);
  608. if (*pmd == NULL)
  609. return 0;
  610. *pmgf1md = rsa_algor_to_md(pss->maskHash);
  611. if (*pmgf1md == NULL)
  612. return 0;
  613. if (pss->saltLength) {
  614. *psaltlen = ASN1_INTEGER_get(pss->saltLength);
  615. if (*psaltlen < 0) {
  616. RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_SALT_LENGTH);
  617. return 0;
  618. }
  619. } else {
  620. *psaltlen = 20;
  621. }
  622. /*
  623. * low-level routines support only trailer field 0xbc (value 1) and
  624. * PKCS#1 says we should reject any other value anyway.
  625. */
  626. if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) {
  627. RSAerr(RSA_F_RSA_PSS_GET_PARAM, RSA_R_INVALID_TRAILER);
  628. return 0;
  629. }
  630. return 1;
  631. }
  632. #ifndef OPENSSL_NO_CMS
  633. static int rsa_cms_verify(CMS_SignerInfo *si)
  634. {
  635. int nid, nid2;
  636. X509_ALGOR *alg;
  637. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  638. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  639. nid = OBJ_obj2nid(alg->algorithm);
  640. if (nid == EVP_PKEY_RSA_PSS)
  641. return rsa_pss_to_ctx(NULL, pkctx, alg, NULL);
  642. /* Only PSS allowed for PSS keys */
  643. if (pkey_ctx_is_pss(pkctx)) {
  644. RSAerr(RSA_F_RSA_CMS_VERIFY, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  645. return 0;
  646. }
  647. if (nid == NID_rsaEncryption)
  648. return 1;
  649. /* Workaround for some implementation that use a signature OID */
  650. if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
  651. if (nid2 == NID_rsaEncryption)
  652. return 1;
  653. }
  654. return 0;
  655. }
  656. #endif
  657. /*
  658. * Customised RSA item verification routine. This is called when a signature
  659. * is encountered requiring special handling. We currently only handle PSS.
  660. */
  661. static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  662. X509_ALGOR *sigalg, ASN1_BIT_STRING *sig,
  663. EVP_PKEY *pkey)
  664. {
  665. /* Sanity check: make sure it is PSS */
  666. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS) {
  667. RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE);
  668. return -1;
  669. }
  670. if (rsa_pss_to_ctx(ctx, NULL, sigalg, pkey) > 0) {
  671. /* Carry on */
  672. return 2;
  673. }
  674. return -1;
  675. }
  676. #ifndef OPENSSL_NO_CMS
  677. static int rsa_cms_sign(CMS_SignerInfo *si)
  678. {
  679. int pad_mode = RSA_PKCS1_PADDING;
  680. X509_ALGOR *alg;
  681. EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
  682. ASN1_STRING *os = NULL;
  683. CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
  684. if (pkctx) {
  685. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  686. return 0;
  687. }
  688. if (pad_mode == RSA_PKCS1_PADDING) {
  689. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  690. return 1;
  691. }
  692. /* We don't support it */
  693. if (pad_mode != RSA_PKCS1_PSS_PADDING)
  694. return 0;
  695. os = rsa_ctx_to_pss_string(pkctx);
  696. if (!os)
  697. return 0;
  698. X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os);
  699. return 1;
  700. }
  701. #endif
  702. static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  703. X509_ALGOR *alg1, X509_ALGOR *alg2,
  704. ASN1_BIT_STRING *sig)
  705. {
  706. int pad_mode;
  707. EVP_PKEY_CTX *pkctx = EVP_MD_CTX_pkey_ctx(ctx);
  708. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  709. return 0;
  710. if (pad_mode == RSA_PKCS1_PADDING)
  711. return 2;
  712. if (pad_mode == RSA_PKCS1_PSS_PADDING) {
  713. ASN1_STRING *os1 = NULL;
  714. os1 = rsa_ctx_to_pss_string(pkctx);
  715. if (!os1)
  716. return 0;
  717. /* Duplicate parameters if we have to */
  718. if (alg2) {
  719. ASN1_STRING *os2 = ASN1_STRING_dup(os1);
  720. if (!os2) {
  721. ASN1_STRING_free(os1);
  722. return 0;
  723. }
  724. X509_ALGOR_set0(alg2, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  725. V_ASN1_SEQUENCE, os2);
  726. }
  727. X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_RSA_PSS),
  728. V_ASN1_SEQUENCE, os1);
  729. return 3;
  730. }
  731. return 2;
  732. }
  733. static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
  734. const ASN1_STRING *sig)
  735. {
  736. int rv = 0;
  737. int mdnid, saltlen;
  738. uint32_t flags;
  739. const EVP_MD *mgf1md = NULL, *md = NULL;
  740. RSA_PSS_PARAMS *pss;
  741. /* Sanity check: make sure it is PSS */
  742. if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
  743. return 0;
  744. /* Decode PSS parameters */
  745. pss = rsa_pss_decode(sigalg);
  746. if (!rsa_pss_get_param(pss, &md, &mgf1md, &saltlen))
  747. goto err;
  748. mdnid = EVP_MD_type(md);
  749. /*
  750. * For TLS need SHA256, SHA384 or SHA512, digest and MGF1 digest must
  751. * match and salt length must equal digest size
  752. */
  753. if ((mdnid == NID_sha256 || mdnid == NID_sha384 || mdnid == NID_sha512)
  754. && mdnid == EVP_MD_type(mgf1md) && saltlen == EVP_MD_size(md))
  755. flags = X509_SIG_INFO_TLS;
  756. else
  757. flags = 0;
  758. /* Note: security bits half number of digest bits */
  759. X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, EVP_MD_size(md) * 4,
  760. flags);
  761. rv = 1;
  762. err:
  763. RSA_PSS_PARAMS_free(pss);
  764. return rv;
  765. }
  766. #ifndef OPENSSL_NO_CMS
  767. static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
  768. {
  769. RSA_OAEP_PARAMS *oaep;
  770. oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
  771. alg->parameter);
  772. if (oaep == NULL)
  773. return NULL;
  774. if (oaep->maskGenFunc != NULL) {
  775. oaep->maskHash = rsa_mgf1_decode(oaep->maskGenFunc);
  776. if (oaep->maskHash == NULL) {
  777. RSA_OAEP_PARAMS_free(oaep);
  778. return NULL;
  779. }
  780. }
  781. return oaep;
  782. }
  783. static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
  784. {
  785. EVP_PKEY_CTX *pkctx;
  786. X509_ALGOR *cmsalg;
  787. int nid;
  788. int rv = -1;
  789. unsigned char *label = NULL;
  790. int labellen = 0;
  791. const EVP_MD *mgf1md = NULL, *md = NULL;
  792. RSA_OAEP_PARAMS *oaep;
  793. pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  794. if (pkctx == NULL)
  795. return 0;
  796. if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
  797. return -1;
  798. nid = OBJ_obj2nid(cmsalg->algorithm);
  799. if (nid == NID_rsaEncryption)
  800. return 1;
  801. if (nid != NID_rsaesOaep) {
  802. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_ENCRYPTION_TYPE);
  803. return -1;
  804. }
  805. /* Decode OAEP parameters */
  806. oaep = rsa_oaep_decode(cmsalg);
  807. if (oaep == NULL) {
  808. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_OAEP_PARAMETERS);
  809. goto err;
  810. }
  811. mgf1md = rsa_algor_to_md(oaep->maskHash);
  812. if (mgf1md == NULL)
  813. goto err;
  814. md = rsa_algor_to_md(oaep->hashFunc);
  815. if (md == NULL)
  816. goto err;
  817. if (oaep->pSourceFunc != NULL) {
  818. X509_ALGOR *plab = oaep->pSourceFunc;
  819. if (OBJ_obj2nid(plab->algorithm) != NID_pSpecified) {
  820. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_UNSUPPORTED_LABEL_SOURCE);
  821. goto err;
  822. }
  823. if (plab->parameter->type != V_ASN1_OCTET_STRING) {
  824. RSAerr(RSA_F_RSA_CMS_DECRYPT, RSA_R_INVALID_LABEL);
  825. goto err;
  826. }
  827. label = plab->parameter->value.octet_string->data;
  828. /* Stop label being freed when OAEP parameters are freed */
  829. plab->parameter->value.octet_string->data = NULL;
  830. labellen = plab->parameter->value.octet_string->length;
  831. }
  832. if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  833. goto err;
  834. if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
  835. goto err;
  836. if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
  837. goto err;
  838. if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, label, labellen) <= 0)
  839. goto err;
  840. /* Carry on */
  841. rv = 1;
  842. err:
  843. RSA_OAEP_PARAMS_free(oaep);
  844. return rv;
  845. }
  846. static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
  847. {
  848. const EVP_MD *md, *mgf1md;
  849. RSA_OAEP_PARAMS *oaep = NULL;
  850. ASN1_STRING *os = NULL;
  851. X509_ALGOR *alg;
  852. EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  853. int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
  854. unsigned char *label;
  855. if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
  856. return 0;
  857. if (pkctx) {
  858. if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
  859. return 0;
  860. }
  861. if (pad_mode == RSA_PKCS1_PADDING) {
  862. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), V_ASN1_NULL, 0);
  863. return 1;
  864. }
  865. /* Not supported */
  866. if (pad_mode != RSA_PKCS1_OAEP_PADDING)
  867. return 0;
  868. if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
  869. goto err;
  870. if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
  871. goto err;
  872. labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
  873. if (labellen < 0)
  874. goto err;
  875. oaep = RSA_OAEP_PARAMS_new();
  876. if (oaep == NULL)
  877. goto err;
  878. if (!rsa_md_to_algor(&oaep->hashFunc, md))
  879. goto err;
  880. if (!rsa_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
  881. goto err;
  882. if (labellen > 0) {
  883. ASN1_OCTET_STRING *los;
  884. oaep->pSourceFunc = X509_ALGOR_new();
  885. if (oaep->pSourceFunc == NULL)
  886. goto err;
  887. los = ASN1_OCTET_STRING_new();
  888. if (los == NULL)
  889. goto err;
  890. if (!ASN1_OCTET_STRING_set(los, label, labellen)) {
  891. ASN1_OCTET_STRING_free(los);
  892. goto err;
  893. }
  894. X509_ALGOR_set0(oaep->pSourceFunc, OBJ_nid2obj(NID_pSpecified),
  895. V_ASN1_OCTET_STRING, los);
  896. }
  897. /* create string with pss parameter encoding. */
  898. if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
  899. goto err;
  900. X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os);
  901. os = NULL;
  902. rv = 1;
  903. err:
  904. RSA_OAEP_PARAMS_free(oaep);
  905. ASN1_STRING_free(os);
  906. return rv;
  907. }
  908. #endif
  909. static int rsa_pkey_check(const EVP_PKEY *pkey)
  910. {
  911. return RSA_check_key_ex(pkey->pkey.rsa, NULL);
  912. }
  913. const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
  914. {
  915. EVP_PKEY_RSA,
  916. EVP_PKEY_RSA,
  917. ASN1_PKEY_SIGPARAM_NULL,
  918. "RSA",
  919. "OpenSSL RSA method",
  920. rsa_pub_decode,
  921. rsa_pub_encode,
  922. rsa_pub_cmp,
  923. rsa_pub_print,
  924. rsa_priv_decode,
  925. rsa_priv_encode,
  926. rsa_priv_print,
  927. int_rsa_size,
  928. rsa_bits,
  929. rsa_security_bits,
  930. 0, 0, 0, 0, 0, 0,
  931. rsa_sig_print,
  932. int_rsa_free,
  933. rsa_pkey_ctrl,
  934. old_rsa_priv_decode,
  935. old_rsa_priv_encode,
  936. rsa_item_verify,
  937. rsa_item_sign,
  938. rsa_sig_info_set,
  939. rsa_pkey_check
  940. },
  941. {
  942. EVP_PKEY_RSA2,
  943. EVP_PKEY_RSA,
  944. ASN1_PKEY_ALIAS}
  945. };
  946. const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
  947. EVP_PKEY_RSA_PSS,
  948. EVP_PKEY_RSA_PSS,
  949. ASN1_PKEY_SIGPARAM_NULL,
  950. "RSA-PSS",
  951. "OpenSSL RSA-PSS method",
  952. rsa_pub_decode,
  953. rsa_pub_encode,
  954. rsa_pub_cmp,
  955. rsa_pub_print,
  956. rsa_priv_decode,
  957. rsa_priv_encode,
  958. rsa_priv_print,
  959. int_rsa_size,
  960. rsa_bits,
  961. rsa_security_bits,
  962. 0, 0, 0, 0, 0, 0,
  963. rsa_sig_print,
  964. int_rsa_free,
  965. rsa_pkey_ctrl,
  966. 0, 0,
  967. rsa_item_verify,
  968. rsa_item_sign,
  969. 0,
  970. rsa_pkey_check
  971. };