rsa_ameth.c 30 KB

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