rsa_pmeth.c 25 KB

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