rsa_pmeth.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "internal/constant_time.h"
  15. #include <stdio.h>
  16. #include "internal/cryptlib.h"
  17. #include <openssl/asn1t.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/rsa.h>
  20. #include <openssl/bn.h>
  21. #include <openssl/evp.h>
  22. #include <openssl/x509v3.h>
  23. #include <openssl/cms.h>
  24. #include "crypto/evp.h"
  25. #include "crypto/rsa.h"
  26. #include "rsa_local.h"
  27. /* RSA pkey context structure */
  28. typedef struct {
  29. /* Key gen parameters */
  30. int nbits;
  31. BIGNUM *pub_exp;
  32. int primes;
  33. /* Keygen callback info */
  34. int gentmp[2];
  35. /* RSA padding mode */
  36. int pad_mode;
  37. /* message digest */
  38. const EVP_MD *md;
  39. /* message digest for MGF1 */
  40. const EVP_MD *mgf1md;
  41. /* PSS salt length */
  42. int saltlen;
  43. /* Minimum salt length or -1 if no PSS parameter restriction */
  44. int min_saltlen;
  45. /* Temp buffer */
  46. unsigned char *tbuf;
  47. /* OAEP label */
  48. unsigned char *oaep_label;
  49. size_t oaep_labellen;
  50. /* if to use implicit rejection in PKCS#1 v1.5 decryption */
  51. int implicit_rejection;
  52. } RSA_PKEY_CTX;
  53. /* True if PSS parameters are restricted */
  54. #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
  55. static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
  56. {
  57. RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
  58. if (rctx == NULL)
  59. return 0;
  60. rctx->nbits = 2048;
  61. rctx->primes = RSA_DEFAULT_PRIME_NUM;
  62. if (pkey_ctx_is_pss(ctx))
  63. rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
  64. else
  65. rctx->pad_mode = RSA_PKCS1_PADDING;
  66. /* Maximum for sign, auto for verify */
  67. rctx->saltlen = RSA_PSS_SALTLEN_AUTO;
  68. rctx->min_saltlen = -1;
  69. rctx->implicit_rejection = 1;
  70. ctx->data = rctx;
  71. ctx->keygen_info = rctx->gentmp;
  72. ctx->keygen_info_count = 2;
  73. return 1;
  74. }
  75. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
  76. {
  77. RSA_PKEY_CTX *dctx, *sctx;
  78. if (!pkey_rsa_init(dst))
  79. return 0;
  80. sctx = src->data;
  81. dctx = dst->data;
  82. dctx->nbits = sctx->nbits;
  83. if (sctx->pub_exp) {
  84. dctx->pub_exp = BN_dup(sctx->pub_exp);
  85. if (!dctx->pub_exp)
  86. return 0;
  87. }
  88. dctx->pad_mode = sctx->pad_mode;
  89. dctx->md = sctx->md;
  90. dctx->mgf1md = sctx->mgf1md;
  91. dctx->saltlen = sctx->saltlen;
  92. dctx->implicit_rejection = sctx->implicit_rejection;
  93. if (sctx->oaep_label) {
  94. OPENSSL_free(dctx->oaep_label);
  95. dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
  96. if (!dctx->oaep_label)
  97. return 0;
  98. dctx->oaep_labellen = sctx->oaep_labellen;
  99. }
  100. return 1;
  101. }
  102. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
  103. {
  104. if (ctx->tbuf != NULL)
  105. return 1;
  106. if ((ctx->tbuf =
  107. OPENSSL_malloc(RSA_size(EVP_PKEY_get0_RSA(pk->pkey)))) == NULL)
  108. return 0;
  109. return 1;
  110. }
  111. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
  112. {
  113. RSA_PKEY_CTX *rctx = ctx->data;
  114. if (rctx) {
  115. BN_free(rctx->pub_exp);
  116. OPENSSL_free(rctx->tbuf);
  117. OPENSSL_free(rctx->oaep_label);
  118. OPENSSL_free(rctx);
  119. }
  120. }
  121. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
  122. size_t *siglen, const unsigned char *tbs,
  123. size_t tbslen)
  124. {
  125. int ret;
  126. RSA_PKEY_CTX *rctx = ctx->data;
  127. /*
  128. * Discard const. Its marked as const because this may be a cached copy of
  129. * the "real" key. These calls don't make any modifications that need to
  130. * be reflected back in the "original" key.
  131. */
  132. RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
  133. if (rctx->md) {
  134. if (tbslen != (size_t)EVP_MD_get_size(rctx->md)) {
  135. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
  136. return -1;
  137. }
  138. if (EVP_MD_get_type(rctx->md) == NID_mdc2) {
  139. unsigned int sltmp;
  140. if (rctx->pad_mode != RSA_PKCS1_PADDING)
  141. return -1;
  142. ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, tbslen, sig, &sltmp, rsa);
  143. if (ret <= 0)
  144. return ret;
  145. ret = sltmp;
  146. } else if (rctx->pad_mode == RSA_X931_PADDING) {
  147. if ((size_t)RSA_size(rsa) < tbslen + 1) {
  148. ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  149. return -1;
  150. }
  151. if (!setup_tbuf(rctx, ctx)) {
  152. ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);
  153. return -1;
  154. }
  155. memcpy(rctx->tbuf, tbs, tbslen);
  156. rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_get_type(rctx->md));
  157. ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
  158. sig, rsa, RSA_X931_PADDING);
  159. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  160. unsigned int sltmp;
  161. ret = RSA_sign(EVP_MD_get_type(rctx->md),
  162. tbs, tbslen, sig, &sltmp, rsa);
  163. if (ret <= 0)
  164. return ret;
  165. ret = sltmp;
  166. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  167. if (!setup_tbuf(rctx, ctx))
  168. return -1;
  169. if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
  170. rctx->tbuf, tbs,
  171. rctx->md, rctx->mgf1md,
  172. rctx->saltlen))
  173. return -1;
  174. ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
  175. sig, rsa, RSA_NO_PADDING);
  176. } else {
  177. return -1;
  178. }
  179. } else {
  180. ret = RSA_private_encrypt(tbslen, tbs, sig, rsa, rctx->pad_mode);
  181. }
  182. if (ret < 0)
  183. return ret;
  184. *siglen = ret;
  185. return 1;
  186. }
  187. static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
  188. unsigned char *rout, size_t *routlen,
  189. const unsigned char *sig, size_t siglen)
  190. {
  191. int ret;
  192. RSA_PKEY_CTX *rctx = ctx->data;
  193. /*
  194. * Discard const. Its marked as const because this may be a cached copy of
  195. * the "real" key. These calls don't make any modifications that need to
  196. * be reflected back in the "original" key.
  197. */
  198. RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
  199. if (rctx->md) {
  200. if (rctx->pad_mode == RSA_X931_PADDING) {
  201. if (!setup_tbuf(rctx, ctx))
  202. return -1;
  203. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, rsa,
  204. RSA_X931_PADDING);
  205. if (ret < 1)
  206. return 0;
  207. ret--;
  208. if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_get_type(rctx->md))) {
  209. ERR_raise(ERR_LIB_RSA, RSA_R_ALGORITHM_MISMATCH);
  210. return 0;
  211. }
  212. if (ret != EVP_MD_get_size(rctx->md)) {
  213. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
  214. return 0;
  215. }
  216. if (rout)
  217. memcpy(rout, rctx->tbuf, ret);
  218. } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
  219. size_t sltmp;
  220. ret = ossl_rsa_verify(EVP_MD_get_type(rctx->md),
  221. NULL, 0, rout, &sltmp,
  222. sig, siglen, rsa);
  223. if (ret <= 0)
  224. return 0;
  225. ret = sltmp;
  226. } else {
  227. return -1;
  228. }
  229. } else {
  230. ret = RSA_public_decrypt(siglen, sig, rout, rsa, rctx->pad_mode);
  231. }
  232. if (ret < 0)
  233. return ret;
  234. *routlen = ret;
  235. return 1;
  236. }
  237. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
  238. const unsigned char *sig, size_t siglen,
  239. const unsigned char *tbs, size_t tbslen)
  240. {
  241. RSA_PKEY_CTX *rctx = ctx->data;
  242. /*
  243. * Discard const. Its marked as const because this may be a cached copy of
  244. * the "real" key. These calls don't make any modifications that need to
  245. * be reflected back in the "original" key.
  246. */
  247. RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
  248. size_t rslen;
  249. if (rctx->md) {
  250. if (rctx->pad_mode == RSA_PKCS1_PADDING)
  251. return RSA_verify(EVP_MD_get_type(rctx->md), tbs, tbslen,
  252. sig, siglen, rsa);
  253. if (tbslen != (size_t)EVP_MD_get_size(rctx->md)) {
  254. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
  255. return -1;
  256. }
  257. if (rctx->pad_mode == RSA_X931_PADDING) {
  258. if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
  259. return 0;
  260. } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
  261. int ret;
  262. if (!setup_tbuf(rctx, ctx))
  263. return -1;
  264. ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  265. rsa, RSA_NO_PADDING);
  266. if (ret <= 0)
  267. return 0;
  268. ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
  269. rctx->md, rctx->mgf1md,
  270. rctx->tbuf, rctx->saltlen);
  271. if (ret <= 0)
  272. return 0;
  273. return 1;
  274. } else {
  275. return -1;
  276. }
  277. } else {
  278. if (!setup_tbuf(rctx, ctx))
  279. return -1;
  280. rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
  281. rsa, rctx->pad_mode);
  282. if (rslen == 0)
  283. return 0;
  284. }
  285. if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
  286. return 0;
  287. return 1;
  288. }
  289. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
  290. unsigned char *out, size_t *outlen,
  291. const unsigned char *in, size_t inlen)
  292. {
  293. int ret;
  294. RSA_PKEY_CTX *rctx = ctx->data;
  295. /*
  296. * Discard const. Its marked as const because this may be a cached copy of
  297. * the "real" key. These calls don't make any modifications that need to
  298. * be reflected back in the "original" key.
  299. */
  300. RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
  301. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  302. int klen = RSA_size(rsa);
  303. if (!setup_tbuf(rctx, ctx))
  304. return -1;
  305. if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
  306. in, inlen,
  307. rctx->oaep_label,
  308. rctx->oaep_labellen,
  309. rctx->md, rctx->mgf1md))
  310. return -1;
  311. ret = RSA_public_encrypt(klen, rctx->tbuf, out, rsa, RSA_NO_PADDING);
  312. } else {
  313. ret = RSA_public_encrypt(inlen, in, out, rsa, rctx->pad_mode);
  314. }
  315. if (ret < 0)
  316. return ret;
  317. *outlen = ret;
  318. return 1;
  319. }
  320. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
  321. unsigned char *out, size_t *outlen,
  322. const unsigned char *in, size_t inlen)
  323. {
  324. int ret;
  325. int pad_mode;
  326. RSA_PKEY_CTX *rctx = ctx->data;
  327. /*
  328. * Discard const. Its marked as const because this may be a cached copy of
  329. * the "real" key. These calls don't make any modifications that need to
  330. * be reflected back in the "original" key.
  331. */
  332. RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(ctx->pkey);
  333. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  334. if (!setup_tbuf(rctx, ctx))
  335. return -1;
  336. ret = RSA_private_decrypt(inlen, in, rctx->tbuf, rsa, RSA_NO_PADDING);
  337. if (ret <= 0)
  338. return ret;
  339. ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
  340. ret, ret,
  341. rctx->oaep_label,
  342. rctx->oaep_labellen,
  343. rctx->md, rctx->mgf1md);
  344. } else {
  345. if (rctx->pad_mode == RSA_PKCS1_PADDING &&
  346. rctx->implicit_rejection == 0)
  347. pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
  348. else
  349. pad_mode = rctx->pad_mode;
  350. ret = RSA_private_decrypt(inlen, in, out, rsa, pad_mode);
  351. }
  352. *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
  353. ret = constant_time_select_int(constant_time_msb(ret), ret, 1);
  354. return ret;
  355. }
  356. static int check_padding_md(const EVP_MD *md, int padding)
  357. {
  358. int mdnid;
  359. if (!md)
  360. return 1;
  361. mdnid = EVP_MD_get_type(md);
  362. if (padding == RSA_NO_PADDING) {
  363. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
  364. return 0;
  365. }
  366. if (padding == RSA_X931_PADDING) {
  367. if (RSA_X931_hash_id(mdnid) == -1) {
  368. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_X931_DIGEST);
  369. return 0;
  370. }
  371. } else {
  372. switch (mdnid) {
  373. /* List of all supported RSA digests */
  374. case NID_sha1:
  375. case NID_sha224:
  376. case NID_sha256:
  377. case NID_sha384:
  378. case NID_sha512:
  379. case NID_sha512_224:
  380. case NID_sha512_256:
  381. case NID_md5:
  382. case NID_md5_sha1:
  383. case NID_md2:
  384. case NID_md4:
  385. case NID_mdc2:
  386. case NID_ripemd160:
  387. case NID_sha3_224:
  388. case NID_sha3_256:
  389. case NID_sha3_384:
  390. case NID_sha3_512:
  391. return 1;
  392. default:
  393. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST);
  394. return 0;
  395. }
  396. }
  397. return 1;
  398. }
  399. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  400. {
  401. RSA_PKEY_CTX *rctx = ctx->data;
  402. switch (type) {
  403. case EVP_PKEY_CTRL_RSA_PADDING:
  404. if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
  405. if (!check_padding_md(rctx->md, p1))
  406. return 0;
  407. if (p1 == RSA_PKCS1_PSS_PADDING) {
  408. if (!(ctx->operation &
  409. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
  410. goto bad_pad;
  411. if (!rctx->md)
  412. rctx->md = EVP_sha1();
  413. } else if (pkey_ctx_is_pss(ctx)) {
  414. goto bad_pad;
  415. }
  416. if (p1 == RSA_PKCS1_OAEP_PADDING) {
  417. if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
  418. goto bad_pad;
  419. if (!rctx->md)
  420. rctx->md = EVP_sha1();
  421. }
  422. rctx->pad_mode = p1;
  423. return 1;
  424. }
  425. bad_pad:
  426. ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  427. return -2;
  428. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  429. *(int *)p2 = rctx->pad_mode;
  430. return 1;
  431. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  432. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  433. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  434. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
  435. return -2;
  436. }
  437. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
  438. *(int *)p2 = rctx->saltlen;
  439. } else {
  440. if (p1 < RSA_PSS_SALTLEN_MAX)
  441. return -2;
  442. if (rsa_pss_restricted(rctx)) {
  443. if (p1 == RSA_PSS_SALTLEN_AUTO
  444. && ctx->operation == EVP_PKEY_OP_VERIFY) {
  445. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_SALTLEN);
  446. return -2;
  447. }
  448. if ((p1 == RSA_PSS_SALTLEN_DIGEST
  449. && rctx->min_saltlen > EVP_MD_get_size(rctx->md))
  450. || (p1 >= 0 && p1 < rctx->min_saltlen)) {
  451. ERR_raise(ERR_LIB_RSA, RSA_R_PSS_SALTLEN_TOO_SMALL);
  452. return 0;
  453. }
  454. }
  455. rctx->saltlen = p1;
  456. }
  457. return 1;
  458. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  459. if (p1 < RSA_MIN_MODULUS_BITS) {
  460. ERR_raise(ERR_LIB_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  461. return -2;
  462. }
  463. rctx->nbits = p1;
  464. return 1;
  465. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  466. if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
  467. ERR_raise(ERR_LIB_RSA, RSA_R_BAD_E_VALUE);
  468. return -2;
  469. }
  470. BN_free(rctx->pub_exp);
  471. rctx->pub_exp = p2;
  472. return 1;
  473. case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES:
  474. if (p1 < RSA_DEFAULT_PRIME_NUM || p1 > RSA_MAX_PRIME_NUM) {
  475. ERR_raise(ERR_LIB_RSA, RSA_R_KEY_PRIME_NUM_INVALID);
  476. return -2;
  477. }
  478. rctx->primes = p1;
  479. return 1;
  480. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  481. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  482. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  483. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
  484. return -2;
  485. }
  486. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
  487. *(const EVP_MD **)p2 = rctx->md;
  488. else
  489. rctx->md = p2;
  490. return 1;
  491. case EVP_PKEY_CTRL_MD:
  492. if (!check_padding_md(p2, rctx->pad_mode))
  493. return 0;
  494. if (rsa_pss_restricted(rctx)) {
  495. if (EVP_MD_get_type(rctx->md) == EVP_MD_get_type(p2))
  496. return 1;
  497. ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_NOT_ALLOWED);
  498. return 0;
  499. }
  500. rctx->md = p2;
  501. return 1;
  502. case EVP_PKEY_CTRL_GET_MD:
  503. *(const EVP_MD **)p2 = rctx->md;
  504. return 1;
  505. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  506. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  507. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
  508. && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  509. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MGF1_MD);
  510. return -2;
  511. }
  512. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  513. if (rctx->mgf1md)
  514. *(const EVP_MD **)p2 = rctx->mgf1md;
  515. else
  516. *(const EVP_MD **)p2 = rctx->md;
  517. } else {
  518. if (rsa_pss_restricted(rctx)) {
  519. if (EVP_MD_get_type(rctx->mgf1md) == EVP_MD_get_type(p2))
  520. return 1;
  521. ERR_raise(ERR_LIB_RSA, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
  522. return 0;
  523. }
  524. rctx->mgf1md = p2;
  525. }
  526. return 1;
  527. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  528. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  529. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
  530. return -2;
  531. }
  532. OPENSSL_free(rctx->oaep_label);
  533. if (p2 && p1 > 0) {
  534. rctx->oaep_label = p2;
  535. rctx->oaep_labellen = p1;
  536. } else {
  537. rctx->oaep_label = NULL;
  538. rctx->oaep_labellen = 0;
  539. }
  540. return 1;
  541. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  542. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  543. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
  544. return -2;
  545. }
  546. if (p2 == NULL) {
  547. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  548. return 0;
  549. }
  550. *(unsigned char **)p2 = rctx->oaep_label;
  551. return rctx->oaep_labellen;
  552. case EVP_PKEY_CTRL_RSA_IMPLICIT_REJECTION:
  553. if (rctx->pad_mode != RSA_PKCS1_PADDING) {
  554. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PADDING_MODE);
  555. return -2;
  556. }
  557. rctx->implicit_rejection = p1;
  558. return 1;
  559. case EVP_PKEY_CTRL_DIGESTINIT:
  560. case EVP_PKEY_CTRL_PKCS7_SIGN:
  561. #ifndef OPENSSL_NO_CMS
  562. case EVP_PKEY_CTRL_CMS_SIGN:
  563. #endif
  564. return 1;
  565. case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
  566. case EVP_PKEY_CTRL_PKCS7_DECRYPT:
  567. #ifndef OPENSSL_NO_CMS
  568. case EVP_PKEY_CTRL_CMS_DECRYPT:
  569. case EVP_PKEY_CTRL_CMS_ENCRYPT:
  570. #endif
  571. if (!pkey_ctx_is_pss(ctx))
  572. return 1;
  573. /* fall through */
  574. case EVP_PKEY_CTRL_PEER_KEY:
  575. ERR_raise(ERR_LIB_RSA, RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  576. return -2;
  577. default:
  578. return -2;
  579. }
  580. }
  581. static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
  582. const char *type, const char *value)
  583. {
  584. if (value == NULL) {
  585. ERR_raise(ERR_LIB_RSA, RSA_R_VALUE_MISSING);
  586. return 0;
  587. }
  588. if (strcmp(type, "rsa_padding_mode") == 0) {
  589. int pm;
  590. if (strcmp(value, "pkcs1") == 0) {
  591. pm = RSA_PKCS1_PADDING;
  592. } else if (strcmp(value, "none") == 0) {
  593. pm = RSA_NO_PADDING;
  594. } else if (strcmp(value, "oeap") == 0) {
  595. pm = RSA_PKCS1_OAEP_PADDING;
  596. } else if (strcmp(value, "oaep") == 0) {
  597. pm = RSA_PKCS1_OAEP_PADDING;
  598. } else if (strcmp(value, "x931") == 0) {
  599. pm = RSA_X931_PADDING;
  600. } else if (strcmp(value, "pss") == 0) {
  601. pm = RSA_PKCS1_PSS_PADDING;
  602. } else {
  603. ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  604. return -2;
  605. }
  606. return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
  607. }
  608. if (strcmp(type, "rsa_pss_saltlen") == 0) {
  609. int saltlen;
  610. if (!strcmp(value, "digest"))
  611. saltlen = RSA_PSS_SALTLEN_DIGEST;
  612. else if (!strcmp(value, "max"))
  613. saltlen = RSA_PSS_SALTLEN_MAX;
  614. else if (!strcmp(value, "auto"))
  615. saltlen = RSA_PSS_SALTLEN_AUTO;
  616. else
  617. saltlen = atoi(value);
  618. return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
  619. }
  620. if (strcmp(type, "rsa_keygen_bits") == 0) {
  621. int nbits = atoi(value);
  622. return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
  623. }
  624. if (strcmp(type, "rsa_keygen_pubexp") == 0) {
  625. int ret;
  626. BIGNUM *pubexp = NULL;
  627. if (!BN_asc2bn(&pubexp, value))
  628. return 0;
  629. ret = EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, pubexp);
  630. BN_free(pubexp);
  631. return ret;
  632. }
  633. if (strcmp(type, "rsa_keygen_primes") == 0) {
  634. int nprimes = atoi(value);
  635. return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, nprimes);
  636. }
  637. if (strcmp(type, "rsa_mgf1_md") == 0)
  638. return EVP_PKEY_CTX_md(ctx,
  639. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  640. EVP_PKEY_CTRL_RSA_MGF1_MD, value);
  641. if (pkey_ctx_is_pss(ctx)) {
  642. if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
  643. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
  644. EVP_PKEY_CTRL_RSA_MGF1_MD, value);
  645. if (strcmp(type, "rsa_pss_keygen_md") == 0)
  646. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
  647. EVP_PKEY_CTRL_MD, value);
  648. if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
  649. int saltlen = atoi(value);
  650. return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
  651. }
  652. }
  653. if (strcmp(type, "rsa_oaep_md") == 0)
  654. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
  655. EVP_PKEY_CTRL_RSA_OAEP_MD, value);
  656. if (strcmp(type, "rsa_oaep_label") == 0) {
  657. unsigned char *lab;
  658. long lablen;
  659. int ret;
  660. lab = OPENSSL_hexstr2buf(value, &lablen);
  661. if (!lab)
  662. return 0;
  663. ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
  664. if (ret <= 0)
  665. OPENSSL_free(lab);
  666. return ret;
  667. }
  668. return -2;
  669. }
  670. /* Set PSS parameters when generating a key, if necessary */
  671. static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
  672. {
  673. RSA_PKEY_CTX *rctx = ctx->data;
  674. if (!pkey_ctx_is_pss(ctx))
  675. return 1;
  676. /* If all parameters are default values don't set pss */
  677. if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
  678. return 1;
  679. rsa->pss = ossl_rsa_pss_params_create(rctx->md, rctx->mgf1md,
  680. rctx->saltlen == -2
  681. ? 0 : rctx->saltlen);
  682. if (rsa->pss == NULL)
  683. return 0;
  684. return 1;
  685. }
  686. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  687. {
  688. RSA *rsa = NULL;
  689. RSA_PKEY_CTX *rctx = ctx->data;
  690. BN_GENCB *pcb;
  691. int ret;
  692. if (rctx->pub_exp == NULL) {
  693. rctx->pub_exp = BN_new();
  694. if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
  695. return 0;
  696. }
  697. rsa = RSA_new();
  698. if (rsa == NULL)
  699. return 0;
  700. if (ctx->pkey_gencb) {
  701. pcb = BN_GENCB_new();
  702. if (pcb == NULL) {
  703. RSA_free(rsa);
  704. return 0;
  705. }
  706. evp_pkey_set_cb_translate(pcb, ctx);
  707. } else {
  708. pcb = NULL;
  709. }
  710. ret = RSA_generate_multi_prime_key(rsa, rctx->nbits, rctx->primes,
  711. rctx->pub_exp, pcb);
  712. BN_GENCB_free(pcb);
  713. if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
  714. RSA_free(rsa);
  715. return 0;
  716. }
  717. if (ret > 0)
  718. EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
  719. else
  720. RSA_free(rsa);
  721. return ret;
  722. }
  723. static const EVP_PKEY_METHOD rsa_pkey_meth = {
  724. EVP_PKEY_RSA,
  725. EVP_PKEY_FLAG_AUTOARGLEN,
  726. pkey_rsa_init,
  727. pkey_rsa_copy,
  728. pkey_rsa_cleanup,
  729. 0, 0,
  730. 0,
  731. pkey_rsa_keygen,
  732. 0,
  733. pkey_rsa_sign,
  734. 0,
  735. pkey_rsa_verify,
  736. 0,
  737. pkey_rsa_verifyrecover,
  738. 0, 0, 0, 0,
  739. 0,
  740. pkey_rsa_encrypt,
  741. 0,
  742. pkey_rsa_decrypt,
  743. 0, 0,
  744. pkey_rsa_ctrl,
  745. pkey_rsa_ctrl_str
  746. };
  747. const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void)
  748. {
  749. return &rsa_pkey_meth;
  750. }
  751. /*
  752. * Called for PSS sign or verify initialisation: checks PSS parameter
  753. * sanity and sets any restrictions on key usage.
  754. */
  755. static int pkey_pss_init(EVP_PKEY_CTX *ctx)
  756. {
  757. const RSA *rsa;
  758. RSA_PKEY_CTX *rctx = ctx->data;
  759. const EVP_MD *md;
  760. const EVP_MD *mgf1md;
  761. int min_saltlen, max_saltlen;
  762. /* Should never happen */
  763. if (!pkey_ctx_is_pss(ctx))
  764. return 0;
  765. rsa = EVP_PKEY_get0_RSA(ctx->pkey);
  766. /* If no restrictions just return */
  767. if (rsa->pss == NULL)
  768. return 1;
  769. /* Get and check parameters */
  770. if (!ossl_rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
  771. return 0;
  772. /* See if minimum salt length exceeds maximum possible */
  773. max_saltlen = RSA_size(rsa) - EVP_MD_get_size(md);
  774. if ((RSA_bits(rsa) & 0x7) == 1)
  775. max_saltlen--;
  776. if (min_saltlen > max_saltlen) {
  777. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_SALT_LENGTH);
  778. return 0;
  779. }
  780. rctx->min_saltlen = min_saltlen;
  781. /*
  782. * Set PSS restrictions as defaults: we can then block any attempt to
  783. * use invalid values in pkey_rsa_ctrl
  784. */
  785. rctx->md = md;
  786. rctx->mgf1md = mgf1md;
  787. rctx->saltlen = min_saltlen;
  788. return 1;
  789. }
  790. static const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
  791. EVP_PKEY_RSA_PSS,
  792. EVP_PKEY_FLAG_AUTOARGLEN,
  793. pkey_rsa_init,
  794. pkey_rsa_copy,
  795. pkey_rsa_cleanup,
  796. 0, 0,
  797. 0,
  798. pkey_rsa_keygen,
  799. pkey_pss_init,
  800. pkey_rsa_sign,
  801. pkey_pss_init,
  802. pkey_rsa_verify,
  803. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  804. pkey_rsa_ctrl,
  805. pkey_rsa_ctrl_str
  806. };
  807. const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void)
  808. {
  809. return &rsa_pss_pkey_meth;
  810. }