rsa_ameth.c 29 KB

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