You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

674 lines
19 KiB

  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/nid.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, 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, EVP_R_INVALID_DIGEST_LENGTH);
  164. return 0;
  165. }
  166. if (EVP_MD_type(rctx->md) == NID_mdc2) {
  167. OPENSSL_PUT_ERROR(EVP, 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_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
  226. size_t *out_len, const uint8_t *sig,
  227. size_t sig_len) {
  228. RSA_PKEY_CTX *rctx = ctx->data;
  229. RSA *rsa = ctx->pkey->pkey.rsa;
  230. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  231. if (out == NULL) {
  232. *out_len = key_len;
  233. return 1;
  234. }
  235. if (*out_len < key_len) {
  236. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  237. return 0;
  238. }
  239. if (!setup_tbuf(rctx, ctx)) {
  240. return 0;
  241. }
  242. if (rctx->md == NULL) {
  243. const int ret = RSA_public_decrypt(sig_len, sig, rctx->tbuf,
  244. ctx->pkey->pkey.rsa, rctx->pad_mode);
  245. if (ret < 0) {
  246. return 0;
  247. }
  248. *out_len = ret;
  249. memcpy(out, rctx->tbuf, *out_len);
  250. return 1;
  251. }
  252. if (rctx->pad_mode != RSA_PKCS1_PADDING) {
  253. return 0;
  254. }
  255. uint8_t *asn1_prefix;
  256. size_t asn1_prefix_len;
  257. int asn1_prefix_allocated;
  258. if (!RSA_add_pkcs1_prefix(&asn1_prefix, &asn1_prefix_len,
  259. &asn1_prefix_allocated, EVP_MD_type(rctx->md), NULL,
  260. 0)) {
  261. return 0;
  262. }
  263. size_t rslen;
  264. int ok = 1;
  265. if (!RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, sig_len,
  266. RSA_PKCS1_PADDING) ||
  267. rslen < asn1_prefix_len ||
  268. CRYPTO_memcmp(rctx->tbuf, asn1_prefix, asn1_prefix_len) != 0) {
  269. ok = 0;
  270. }
  271. if (asn1_prefix_allocated) {
  272. OPENSSL_free(asn1_prefix);
  273. }
  274. if (!ok) {
  275. return 0;
  276. }
  277. const size_t result_len = rslen - asn1_prefix_len;
  278. if (result_len != EVP_MD_size(rctx->md)) {
  279. return 0;
  280. }
  281. if (out != NULL) {
  282. memcpy(out, rctx->tbuf + asn1_prefix_len, result_len);
  283. }
  284. *out_len = result_len;
  285. return 1;
  286. }
  287. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  288. const uint8_t *in, size_t inlen) {
  289. RSA_PKEY_CTX *rctx = ctx->data;
  290. RSA *rsa = ctx->pkey->pkey.rsa;
  291. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  292. if (!out) {
  293. *outlen = key_len;
  294. return 1;
  295. }
  296. if (*outlen < key_len) {
  297. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  298. return 0;
  299. }
  300. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  301. if (!setup_tbuf(rctx, ctx) ||
  302. !RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, key_len, in, inlen,
  303. rctx->oaep_label, rctx->oaep_labellen,
  304. rctx->md, rctx->mgf1md) ||
  305. !RSA_encrypt(rsa, outlen, out, *outlen, rctx->tbuf, key_len,
  306. RSA_NO_PADDING)) {
  307. return 0;
  308. }
  309. return 1;
  310. }
  311. return RSA_encrypt(rsa, outlen, out, *outlen, in, inlen, rctx->pad_mode);
  312. }
  313. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
  314. size_t *outlen, const uint8_t *in,
  315. size_t inlen) {
  316. RSA_PKEY_CTX *rctx = ctx->data;
  317. RSA *rsa = ctx->pkey->pkey.rsa;
  318. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  319. if (!out) {
  320. *outlen = key_len;
  321. return 1;
  322. }
  323. if (*outlen < key_len) {
  324. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  325. return 0;
  326. }
  327. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  328. size_t plaintext_len;
  329. int message_len;
  330. if (!setup_tbuf(rctx, ctx) ||
  331. !RSA_decrypt(rsa, &plaintext_len, rctx->tbuf, key_len, in, inlen,
  332. RSA_NO_PADDING)) {
  333. return 0;
  334. }
  335. message_len = RSA_padding_check_PKCS1_OAEP_mgf1(
  336. out, key_len, rctx->tbuf, plaintext_len, rctx->oaep_label,
  337. rctx->oaep_labellen, rctx->md, rctx->mgf1md);
  338. if (message_len < 0) {
  339. return 0;
  340. }
  341. *outlen = message_len;
  342. return 1;
  343. }
  344. return RSA_decrypt(rsa, outlen, out, key_len, in, inlen, rctx->pad_mode);
  345. }
  346. static int check_padding_md(const EVP_MD *md, int padding) {
  347. if (!md) {
  348. return 1;
  349. }
  350. if (padding == RSA_NO_PADDING) {
  351. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  352. return 0;
  353. }
  354. return 1;
  355. }
  356. static int is_known_padding(int padding_mode) {
  357. switch (padding_mode) {
  358. case RSA_PKCS1_PADDING:
  359. case RSA_NO_PADDING:
  360. case RSA_PKCS1_OAEP_PADDING:
  361. case RSA_PKCS1_PSS_PADDING:
  362. return 1;
  363. default:
  364. return 0;
  365. }
  366. }
  367. static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
  368. RSA_PKEY_CTX *rctx = ctx->data;
  369. switch (type) {
  370. case EVP_PKEY_CTRL_RSA_PADDING:
  371. if (!is_known_padding(p1) || !check_padding_md(rctx->md, p1) ||
  372. (p1 == RSA_PKCS1_PSS_PADDING &&
  373. 0 == (ctx->operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) ||
  374. (p1 == RSA_PKCS1_OAEP_PADDING &&
  375. 0 == (ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))) {
  376. OPENSSL_PUT_ERROR(EVP, EVP_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
  377. return 0;
  378. }
  379. if ((p1 == RSA_PKCS1_PSS_PADDING || p1 == RSA_PKCS1_OAEP_PADDING) &&
  380. rctx->md == NULL) {
  381. rctx->md = EVP_sha1();
  382. }
  383. rctx->pad_mode = p1;
  384. return 1;
  385. case EVP_PKEY_CTRL_GET_RSA_PADDING:
  386. *(int *)p2 = rctx->pad_mode;
  387. return 1;
  388. case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
  389. case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
  390. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
  391. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PSS_SALTLEN);
  392. return 0;
  393. }
  394. if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
  395. *(int *)p2 = rctx->saltlen;
  396. } else {
  397. if (p1 < -2) {
  398. return 0;
  399. }
  400. rctx->saltlen = p1;
  401. }
  402. return 1;
  403. case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
  404. if (p1 < 256) {
  405. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_KEYBITS);
  406. return 0;
  407. }
  408. rctx->nbits = p1;
  409. return 1;
  410. case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
  411. if (!p2) {
  412. return 0;
  413. }
  414. BN_free(rctx->pub_exp);
  415. rctx->pub_exp = p2;
  416. return 1;
  417. case EVP_PKEY_CTRL_RSA_OAEP_MD:
  418. case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
  419. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  420. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  421. return 0;
  422. }
  423. if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD) {
  424. *(const EVP_MD **)p2 = rctx->md;
  425. } else {
  426. rctx->md = p2;
  427. }
  428. return 1;
  429. case EVP_PKEY_CTRL_MD:
  430. if (!check_padding_md(p2, rctx->pad_mode)) {
  431. return 0;
  432. }
  433. rctx->md = p2;
  434. return 1;
  435. case EVP_PKEY_CTRL_GET_MD:
  436. *(const EVP_MD **)p2 = rctx->md;
  437. return 1;
  438. case EVP_PKEY_CTRL_RSA_MGF1_MD:
  439. case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
  440. if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING &&
  441. rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  442. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_MGF1_MD);
  443. return 0;
  444. }
  445. if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
  446. if (rctx->mgf1md) {
  447. *(const EVP_MD **)p2 = rctx->mgf1md;
  448. } else {
  449. *(const EVP_MD **)p2 = rctx->md;
  450. }
  451. } else {
  452. rctx->mgf1md = p2;
  453. }
  454. return 1;
  455. case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
  456. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  457. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  458. return 0;
  459. }
  460. OPENSSL_free(rctx->oaep_label);
  461. if (p2 && p1 > 0) {
  462. rctx->oaep_label = p2;
  463. rctx->oaep_labellen = p1;
  464. } else {
  465. rctx->oaep_label = NULL;
  466. rctx->oaep_labellen = 0;
  467. }
  468. return 1;
  469. case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
  470. if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  471. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PADDING_MODE);
  472. return 0;
  473. }
  474. CBS_init((CBS *)p2, rctx->oaep_label, rctx->oaep_labellen);
  475. return 1;
  476. default:
  477. OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  478. return 0;
  479. }
  480. }
  481. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  482. RSA *rsa = NULL;
  483. RSA_PKEY_CTX *rctx = ctx->data;
  484. if (!rctx->pub_exp) {
  485. rctx->pub_exp = BN_new();
  486. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) {
  487. return 0;
  488. }
  489. }
  490. rsa = RSA_new();
  491. if (!rsa) {
  492. return 0;
  493. }
  494. if (!RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, NULL)) {
  495. RSA_free(rsa);
  496. return 0;
  497. }
  498. EVP_PKEY_assign_RSA(pkey, rsa);
  499. return 1;
  500. }
  501. const EVP_PKEY_METHOD rsa_pkey_meth = {
  502. EVP_PKEY_RSA,
  503. pkey_rsa_init,
  504. pkey_rsa_copy,
  505. pkey_rsa_cleanup,
  506. pkey_rsa_keygen,
  507. pkey_rsa_sign,
  508. pkey_rsa_verify,
  509. pkey_rsa_verify_recover,
  510. pkey_rsa_encrypt,
  511. pkey_rsa_decrypt,
  512. 0 /* derive */,
  513. pkey_rsa_ctrl,
  514. };
  515. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) {
  516. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING,
  517. padding, NULL);
  518. }
  519. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) {
  520. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  521. 0, out_padding);
  522. }
  523. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
  524. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  525. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  526. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, NULL);
  527. }
  528. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) {
  529. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  530. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  531. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len);
  532. }
  533. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
  534. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  535. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
  536. }
  537. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) {
  538. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  539. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e);
  540. }
  541. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  542. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  543. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
  544. }
  545. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  546. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  547. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void*) out_md);
  548. }
  549. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  550. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  551. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  552. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void*) md);
  553. }
  554. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  555. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  556. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  557. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void*) out_md);
  558. }
  559. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, uint8_t *label,
  560. size_t label_len) {
  561. if (label_len > INT_MAX) {
  562. return 0;
  563. }
  564. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  565. EVP_PKEY_CTRL_RSA_OAEP_LABEL, (int)label_len,
  566. (void *)label);
  567. }
  568. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  569. const uint8_t **out_label) {
  570. CBS label;
  571. if (!EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  572. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, &label)) {
  573. return -1;
  574. }
  575. if (CBS_len(&label) > INT_MAX) {
  576. OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
  577. return -1;
  578. }
  579. *out_label = CBS_data(&label);
  580. return (int)CBS_len(&label);
  581. }