Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 2006.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/evp.h>
  56. #include <limits.h>
  57. #include <string.h>
  58. #include <openssl/bn.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/bytestring.h>
  61. #include <openssl/digest.h>
  62. #include <openssl/err.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/rsa.h>
  66. #include "../rsa/internal.h"
  67. #include "internal.h"
  68. typedef struct {
  69. /* Key gen parameters */
  70. int nbits;
  71. BIGNUM *pub_exp;
  72. /* RSA padding mode */
  73. int pad_mode;
  74. /* message digest */
  75. const EVP_MD *md;
  76. /* message digest for MGF1 */
  77. const EVP_MD *mgf1md;
  78. /* PSS salt length */
  79. int saltlen;
  80. /* tbuf is a buffer which is either NULL, or is the size of the RSA modulus.
  81. * It's used to store the output of RSA operations. */
  82. uint8_t *tbuf;
  83. /* OAEP label */
  84. uint8_t *oaep_label;
  85. size_t oaep_labellen;
  86. } RSA_PKEY_CTX;
  87. static int pkey_rsa_init(EVP_PKEY_CTX *ctx) {
  88. RSA_PKEY_CTX *rctx;
  89. rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
  90. if (!rctx) {
  91. return 0;
  92. }
  93. memset(rctx, 0, sizeof(RSA_PKEY_CTX));
  94. rctx->nbits = 2048;
  95. rctx->pad_mode = RSA_PKCS1_PADDING;
  96. rctx->saltlen = -2;
  97. ctx->data = rctx;
  98. return 1;
  99. }
  100. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
  101. RSA_PKEY_CTX *dctx, *sctx;
  102. if (!pkey_rsa_init(dst)) {
  103. return 0;
  104. }
  105. sctx = src->data;
  106. dctx = dst->data;
  107. dctx->nbits = sctx->nbits;
  108. if (sctx->pub_exp) {
  109. dctx->pub_exp = BN_dup(sctx->pub_exp);
  110. if (!dctx->pub_exp) {
  111. return 0;
  112. }
  113. }
  114. dctx->pad_mode = sctx->pad_mode;
  115. dctx->md = sctx->md;
  116. dctx->mgf1md = sctx->mgf1md;
  117. if (sctx->oaep_label) {
  118. OPENSSL_free(dctx->oaep_label);
  119. dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
  120. if (!dctx->oaep_label) {
  121. return 0;
  122. }
  123. dctx->oaep_labellen = sctx->oaep_labellen;
  124. }
  125. return 1;
  126. }
  127. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) {
  128. RSA_PKEY_CTX *rctx = ctx->data;
  129. if (rctx == NULL) {
  130. return;
  131. }
  132. BN_free(rctx->pub_exp);
  133. OPENSSL_free(rctx->tbuf);
  134. OPENSSL_free(rctx->oaep_label);
  135. OPENSSL_free(rctx);
  136. }
  137. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) {
  138. if (ctx->tbuf) {
  139. return 1;
  140. }
  141. ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
  142. if (!ctx->tbuf) {
  143. return 0;
  144. }
  145. return 1;
  146. }
  147. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  148. const uint8_t *tbs, size_t tbslen) {
  149. RSA_PKEY_CTX *rctx = ctx->data;
  150. RSA *rsa = ctx->pkey->pkey.rsa;
  151. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  152. if (!sig) {
  153. *siglen = key_len;
  154. return 1;
  155. }
  156. if (*siglen < key_len) {
  157. OPENSSL_PUT_ERROR(EVP, pkey_rsa_sign, EVP_R_BUFFER_TOO_SMALL);
  158. return 0;
  159. }
  160. if (rctx->md) {
  161. unsigned int out_len;
  162. if (tbslen != EVP_MD_size(rctx->md)) {
  163. OPENSSL_PUT_ERROR(EVP, pkey_rsa_sign, EVP_R_INVALID_DIGEST_LENGTH);
  164. return 0;
  165. }
  166. if (EVP_MD_type(rctx->md) == NID_mdc2) {
  167. OPENSSL_PUT_ERROR(EVP, pkey_rsa_sign, EVP_R_NO_MDC2_SUPPORT);
  168. return 0;
  169. }
  170. switch (rctx->pad_mode) {
  171. case RSA_PKCS1_PADDING:
  172. if (!RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig, &out_len, rsa)) {
  173. return 0;
  174. }
  175. *siglen = out_len;
  176. return 1;
  177. case RSA_PKCS1_PSS_PADDING:
  178. if (!setup_tbuf(rctx, ctx) ||
  179. !RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf, tbs, rctx->md,
  180. rctx->mgf1md, rctx->saltlen) ||
  181. !RSA_sign_raw(rsa, siglen, sig, *siglen, rctx->tbuf, key_len,
  182. RSA_NO_PADDING)) {
  183. return 0;
  184. }
  185. return 1;
  186. default:
  187. return 0;
  188. }
  189. }
  190. return RSA_sign_raw(rsa, siglen, sig, *siglen, tbs, tbslen, rctx->pad_mode);
  191. }
  192. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
  193. size_t siglen, const uint8_t *tbs,
  194. size_t tbslen) {
  195. RSA_PKEY_CTX *rctx = ctx->data;
  196. RSA *rsa = ctx->pkey->pkey.rsa;
  197. size_t rslen;
  198. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  199. if (rctx->md) {
  200. switch (rctx->pad_mode) {
  201. case RSA_PKCS1_PADDING:
  202. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, sig, siglen, rsa);
  203. case RSA_PKCS1_PSS_PADDING:
  204. if (!setup_tbuf(rctx, ctx) ||
  205. !RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen,
  206. RSA_NO_PADDING) ||
  207. !RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, rctx->md, rctx->mgf1md,
  208. rctx->tbuf, rctx->saltlen)) {
  209. return 0;
  210. }
  211. return 1;
  212. default:
  213. return 0;
  214. }
  215. }
  216. if (!setup_tbuf(rctx, ctx) ||
  217. !RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen,
  218. rctx->pad_mode) ||
  219. rslen != tbslen ||
  220. CRYPTO_memcmp(tbs, rctx->tbuf, rslen) != 0) {
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  226. const uint8_t *in, size_t inlen) {
  227. RSA_PKEY_CTX *rctx = ctx->data;
  228. RSA *rsa = ctx->pkey->pkey.rsa;
  229. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  230. if (!out) {
  231. *outlen = key_len;
  232. return 1;
  233. }
  234. if (*outlen < key_len) {
  235. OPENSSL_PUT_ERROR(EVP, pkey_rsa_encrypt, EVP_R_BUFFER_TOO_SMALL);
  236. return 0;
  237. }
  238. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  239. if (!setup_tbuf(rctx, ctx) ||
  240. !RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, key_len, in, inlen,
  241. rctx->oaep_label, rctx->oaep_labellen,
  242. rctx->md, rctx->mgf1md) ||
  243. !RSA_encrypt(rsa, outlen, out, *outlen, rctx->tbuf, key_len,
  244. RSA_NO_PADDING)) {
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. return RSA_encrypt(rsa, outlen, out, *outlen, in, inlen, rctx->pad_mode);
  250. }
  251. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
  252. size_t *outlen, const uint8_t *in,
  253. size_t inlen) {
  254. RSA_PKEY_CTX *rctx = ctx->data;
  255. RSA *rsa = ctx->pkey->pkey.rsa;
  256. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  257. if (!out) {
  258. *outlen = key_len;
  259. return 1;
  260. }
  261. if (*outlen < key_len) {
  262. OPENSSL_PUT_ERROR(EVP, pkey_rsa_decrypt, EVP_R_BUFFER_TOO_SMALL);
  263. return 0;
  264. }
  265. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  266. size_t plaintext_len;
  267. int message_len;
  268. if (!setup_tbuf(rctx, ctx) ||
  269. !RSA_decrypt(rsa, &plaintext_len, rctx->tbuf, key_len, in, inlen,
  270. RSA_NO_PADDING)) {
  271. return 0;
  272. }
  273. message_len = RSA_padding_check_PKCS1_OAEP_mgf1(
  274. out, key_len, rctx->tbuf, plaintext_len, rctx->oaep_label,
  275. rctx->oaep_labellen, rctx->md, rctx->mgf1md);
  276. if (message_len < 0) {
  277. return 0;
  278. }
  279. *outlen = message_len;
  280. return 1;
  281. }
  282. return RSA_decrypt(rsa, outlen, out, key_len, in, inlen, rctx->pad_mode);
  283. }
  284. static int check_padding_md(const EVP_MD *md, int padding) {
  285. if (!md) {
  286. return 1;
  287. }
  288. if (padding == RSA_NO_PADDING) {
  289. OPENSSL_PUT_ERROR(EVP, check_padding_md, EVP_R_INVALID_PADDING_MODE);
  290. return 0;
  291. }
  292. return 1;
  293. }
  294. static int is_known_padding(int padding_mode) {
  295. switch (padding_mode) {
  296. case RSA_PKCS1_PADDING:
  297. case RSA_NO_PADDING:
  298. case RSA_PKCS1_OAEP_PADDING:
  299. case RSA_PKCS1_PSS_PADDING:
  300. return 1;
  301. default:
  302. return 0;
  303. }
  304. }
  305. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  306. RSA_PKEY_CTX *rctx = ctx->data;
  307. switch (type) {
  308. case EVP_PKEY_CTRL_RSA_PADDING:
  309. if (!is_known_padding(p1) || !check_padding_md(rctx->md, p1) ||
  310. (p1 == RSA_PKCS1_PSS_PADDING &&
  311. 0 == (ctx->operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) ||
  312. (p1 == RSA_PKCS1_OAEP_PADDING &&
  313. 0 == (ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))) {
  314. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl,
  315. EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  316. return 0;
  317. }
  318. if ((p1 == RSA_PKCS1_PSS_PADDING || p1 == RSA_PKCS1_OAEP_PADDING) &&
  319. rctx->md == NULL) {
  320. rctx->md = EVP_sha1();
  321. }
  322. rctx->pad_mode = p1;
  323. return 1;
  324. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  325. *(int *)p2 = rctx->pad_mode;
  326. return 1;
  327. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  328. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  329. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  330. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_PSS_SALTLEN);
  331. return 0;
  332. }
  333. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
  334. *(int *)p2 = rctx->saltlen;
  335. } else {
  336. if (p1 < -2) {
  337. return 0;
  338. }
  339. rctx->saltlen = p1;
  340. }
  341. return 1;
  342. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  343. if (p1 < 256) {
  344. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_KEYBITS);
  345. return 0;
  346. }
  347. rctx->nbits = p1;
  348. return 1;
  349. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  350. if (!p2) {
  351. return 0;
  352. }
  353. BN_free(rctx->pub_exp);
  354. rctx->pub_exp = p2;
  355. return 1;
  356. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  357. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  358. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  359. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_PADDING_MODE);
  360. return 0;
  361. }
  362. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) {
  363. *(const EVP_MD **)p2 = rctx->md;
  364. } else {
  365. rctx->md = p2;
  366. }
  367. return 1;
  368. case EVP_PKEY_CTRL_MD:
  369. if (!check_padding_md(p2, rctx->pad_mode)) {
  370. return 0;
  371. }
  372. rctx->md = p2;
  373. return 1;
  374. case EVP_PKEY_CTRL_GET_MD:
  375. *(const EVP_MD **)p2 = rctx->md;
  376. return 1;
  377. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  378. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  379. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING &&
  380. rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  381. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_MGF1_MD);
  382. return 0;
  383. }
  384. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  385. if (rctx->mgf1md) {
  386. *(const EVP_MD **)p2 = rctx->mgf1md;
  387. } else {
  388. *(const EVP_MD **)p2 = rctx->md;
  389. }
  390. } else {
  391. rctx->mgf1md = p2;
  392. }
  393. return 1;
  394. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  395. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  396. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_PADDING_MODE);
  397. return 0;
  398. }
  399. OPENSSL_free(rctx->oaep_label);
  400. if (p2 && p1 > 0) {
  401. /* TODO(fork): this seems wrong. Shouldn't it take a copy of the
  402. * buffer? */
  403. rctx->oaep_label = p2;
  404. rctx->oaep_labellen = p1;
  405. } else {
  406. rctx->oaep_label = NULL;
  407. rctx->oaep_labellen = 0;
  408. }
  409. return 1;
  410. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  411. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  412. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_INVALID_PADDING_MODE);
  413. return 0;
  414. }
  415. CBS_init((CBS *)p2, rctx->oaep_label, rctx->oaep_labellen);
  416. return 1;
  417. default:
  418. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_COMMAND_NOT_SUPPORTED);
  419. return 0;
  420. }
  421. }
  422. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  423. RSA *rsa = NULL;
  424. RSA_PKEY_CTX *rctx = ctx->data;
  425. if (!rctx->pub_exp) {
  426. rctx->pub_exp = BN_new();
  427. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) {
  428. return 0;
  429. }
  430. }
  431. rsa = RSA_new();
  432. if (!rsa) {
  433. return 0;
  434. }
  435. if (!RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, NULL)) {
  436. RSA_free(rsa);
  437. return 0;
  438. }
  439. EVP_PKEY_assign_RSA(pkey, rsa);
  440. return 1;
  441. }
  442. const EVP_PKEY_METHOD rsa_pkey_meth = {
  443. EVP_PKEY_RSA, 0 /* flags */, pkey_rsa_init,
  444. pkey_rsa_copy, pkey_rsa_cleanup, 0 /* paramgen_init */,
  445. 0 /* paramgen */, 0 /* keygen_init */, pkey_rsa_keygen,
  446. 0 /* sign_init */, pkey_rsa_sign, 0 /* verify_init */,
  447. pkey_rsa_verify, 0 /* encrypt_init */, pkey_rsa_encrypt,
  448. 0 /* decrypt_init */, pkey_rsa_decrypt, 0 /* derive_init */,
  449. 0 /* derive */, pkey_rsa_ctrl,
  450. };
  451. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) {
  452. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING,
  453. padding, NULL);
  454. }
  455. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) {
  456. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  457. 0, out_padding);
  458. }
  459. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
  460. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  461. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  462. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, NULL);
  463. }
  464. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) {
  465. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  466. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  467. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len);
  468. }
  469. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
  470. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  471. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
  472. }
  473. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) {
  474. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  475. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e);
  476. }
  477. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  478. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  479. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
  480. }
  481. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  482. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  483. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void*) out_md);
  484. }
  485. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  486. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  487. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  488. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void*) md);
  489. }
  490. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  491. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  492. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  493. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void*) out_md);
  494. }
  495. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, const uint8_t *label,
  496. size_t label_len) {
  497. int label_len_int = label_len;
  498. if (((size_t) label_len_int) != label_len) {
  499. return 0;
  500. }
  501. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  502. EVP_PKEY_CTRL_RSA_OAEP_LABEL, label_len,
  503. (void *)label);
  504. }
  505. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  506. const uint8_t **out_label) {
  507. CBS label;
  508. if (!EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  509. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, &label)) {
  510. return -1;
  511. }
  512. if (CBS_len(&label) > INT_MAX) {
  513. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_CTX_get0_rsa_oaep_label, ERR_R_OVERFLOW);
  514. return -1;
  515. }
  516. *out_label = CBS_data(&label);
  517. return (int)CBS_len(&label);
  518. }