Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

675 рядки
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 "../internal.h"
  67. #include "../rsa/internal.h"
  68. #include "internal.h"
  69. typedef struct {
  70. /* Key gen parameters */
  71. int nbits;
  72. BIGNUM *pub_exp;
  73. /* RSA padding mode */
  74. int pad_mode;
  75. /* message digest */
  76. const EVP_MD *md;
  77. /* message digest for MGF1 */
  78. const EVP_MD *mgf1md;
  79. /* PSS salt length */
  80. int saltlen;
  81. /* tbuf is a buffer which is either NULL, or is the size of the RSA modulus.
  82. * It's used to store the output of RSA operations. */
  83. uint8_t *tbuf;
  84. /* OAEP label */
  85. uint8_t *oaep_label;
  86. size_t oaep_labellen;
  87. } RSA_PKEY_CTX;
  88. static int pkey_rsa_init(EVP_PKEY_CTX *ctx) {
  89. RSA_PKEY_CTX *rctx;
  90. rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
  91. if (!rctx) {
  92. return 0;
  93. }
  94. OPENSSL_memset(rctx, 0, sizeof(RSA_PKEY_CTX));
  95. rctx->nbits = 2048;
  96. rctx->pad_mode = RSA_PKCS1_PADDING;
  97. rctx->saltlen = -2;
  98. ctx->data = rctx;
  99. return 1;
  100. }
  101. static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) {
  102. RSA_PKEY_CTX *dctx, *sctx;
  103. if (!pkey_rsa_init(dst)) {
  104. return 0;
  105. }
  106. sctx = src->data;
  107. dctx = dst->data;
  108. dctx->nbits = sctx->nbits;
  109. if (sctx->pub_exp) {
  110. dctx->pub_exp = BN_dup(sctx->pub_exp);
  111. if (!dctx->pub_exp) {
  112. return 0;
  113. }
  114. }
  115. dctx->pad_mode = sctx->pad_mode;
  116. dctx->md = sctx->md;
  117. dctx->mgf1md = sctx->mgf1md;
  118. if (sctx->oaep_label) {
  119. OPENSSL_free(dctx->oaep_label);
  120. dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
  121. if (!dctx->oaep_label) {
  122. return 0;
  123. }
  124. dctx->oaep_labellen = sctx->oaep_labellen;
  125. }
  126. return 1;
  127. }
  128. static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) {
  129. RSA_PKEY_CTX *rctx = ctx->data;
  130. if (rctx == NULL) {
  131. return;
  132. }
  133. BN_free(rctx->pub_exp);
  134. OPENSSL_free(rctx->tbuf);
  135. OPENSSL_free(rctx->oaep_label);
  136. OPENSSL_free(rctx);
  137. }
  138. static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) {
  139. if (ctx->tbuf) {
  140. return 1;
  141. }
  142. ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
  143. if (!ctx->tbuf) {
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *siglen,
  149. const uint8_t *tbs, size_t tbslen) {
  150. RSA_PKEY_CTX *rctx = ctx->data;
  151. RSA *rsa = ctx->pkey->pkey.rsa;
  152. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  153. if (!sig) {
  154. *siglen = key_len;
  155. return 1;
  156. }
  157. if (*siglen < key_len) {
  158. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  159. return 0;
  160. }
  161. if (rctx->md) {
  162. unsigned int out_len;
  163. if (tbslen != EVP_MD_size(rctx->md)) {
  164. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_LENGTH);
  165. return 0;
  166. }
  167. if (EVP_MD_type(rctx->md) == NID_mdc2) {
  168. OPENSSL_PUT_ERROR(EVP, EVP_R_NO_MDC2_SUPPORT);
  169. return 0;
  170. }
  171. switch (rctx->pad_mode) {
  172. case RSA_PKCS1_PADDING:
  173. if (!RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig, &out_len, rsa)) {
  174. return 0;
  175. }
  176. *siglen = out_len;
  177. return 1;
  178. case RSA_PKCS1_PSS_PADDING:
  179. if (!setup_tbuf(rctx, ctx) ||
  180. !RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf, tbs, rctx->md,
  181. rctx->mgf1md, rctx->saltlen) ||
  182. !RSA_sign_raw(rsa, siglen, sig, *siglen, rctx->tbuf, key_len,
  183. RSA_NO_PADDING)) {
  184. return 0;
  185. }
  186. return 1;
  187. default:
  188. return 0;
  189. }
  190. }
  191. return RSA_sign_raw(rsa, siglen, sig, *siglen, tbs, tbslen, rctx->pad_mode);
  192. }
  193. static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig,
  194. size_t siglen, const uint8_t *tbs,
  195. size_t tbslen) {
  196. RSA_PKEY_CTX *rctx = ctx->data;
  197. RSA *rsa = ctx->pkey->pkey.rsa;
  198. size_t rslen;
  199. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  200. if (rctx->md) {
  201. switch (rctx->pad_mode) {
  202. case RSA_PKCS1_PADDING:
  203. return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, sig, siglen, rsa);
  204. case RSA_PKCS1_PSS_PADDING:
  205. if (tbslen != EVP_MD_size(rctx->md)) {
  206. OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_DIGEST_LENGTH);
  207. return 0;
  208. }
  209. if (!setup_tbuf(rctx, ctx) ||
  210. !RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen,
  211. RSA_NO_PADDING) ||
  212. !RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, rctx->md, rctx->mgf1md,
  213. rctx->tbuf, rctx->saltlen)) {
  214. return 0;
  215. }
  216. return 1;
  217. default:
  218. return 0;
  219. }
  220. }
  221. if (!setup_tbuf(rctx, ctx) ||
  222. !RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, siglen,
  223. rctx->pad_mode) ||
  224. rslen != tbslen ||
  225. CRYPTO_memcmp(tbs, rctx->tbuf, rslen) != 0) {
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. static int pkey_rsa_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out,
  231. size_t *out_len, const uint8_t *sig,
  232. size_t sig_len) {
  233. RSA_PKEY_CTX *rctx = ctx->data;
  234. RSA *rsa = ctx->pkey->pkey.rsa;
  235. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  236. if (out == NULL) {
  237. *out_len = key_len;
  238. return 1;
  239. }
  240. if (*out_len < key_len) {
  241. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  242. return 0;
  243. }
  244. if (!setup_tbuf(rctx, ctx)) {
  245. return 0;
  246. }
  247. if (rctx->md == NULL) {
  248. const int ret = RSA_public_decrypt(sig_len, sig, rctx->tbuf,
  249. ctx->pkey->pkey.rsa, rctx->pad_mode);
  250. if (ret < 0) {
  251. return 0;
  252. }
  253. *out_len = ret;
  254. OPENSSL_memcpy(out, rctx->tbuf, *out_len);
  255. return 1;
  256. }
  257. if (rctx->pad_mode != RSA_PKCS1_PADDING) {
  258. return 0;
  259. }
  260. uint8_t *asn1_prefix;
  261. size_t asn1_prefix_len;
  262. int asn1_prefix_allocated;
  263. if (!RSA_add_pkcs1_prefix(&asn1_prefix, &asn1_prefix_len,
  264. &asn1_prefix_allocated, EVP_MD_type(rctx->md), NULL,
  265. 0)) {
  266. return 0;
  267. }
  268. size_t rslen;
  269. int ok = 1;
  270. if (!RSA_verify_raw(rsa, &rslen, rctx->tbuf, key_len, sig, sig_len,
  271. RSA_PKCS1_PADDING) ||
  272. rslen < asn1_prefix_len ||
  273. CRYPTO_memcmp(rctx->tbuf, asn1_prefix, asn1_prefix_len) != 0) {
  274. ok = 0;
  275. }
  276. if (asn1_prefix_allocated) {
  277. OPENSSL_free(asn1_prefix);
  278. }
  279. if (!ok) {
  280. return 0;
  281. }
  282. const size_t result_len = rslen - asn1_prefix_len;
  283. if (result_len != EVP_MD_size(rctx->md)) {
  284. return 0;
  285. }
  286. if (out != NULL) {
  287. OPENSSL_memcpy(out, rctx->tbuf + asn1_prefix_len, result_len);
  288. }
  289. *out_len = result_len;
  290. return 1;
  291. }
  292. static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  293. const uint8_t *in, size_t inlen) {
  294. RSA_PKEY_CTX *rctx = ctx->data;
  295. RSA *rsa = ctx->pkey->pkey.rsa;
  296. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  297. if (!out) {
  298. *outlen = key_len;
  299. return 1;
  300. }
  301. if (*outlen < key_len) {
  302. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  303. return 0;
  304. }
  305. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  306. if (!setup_tbuf(rctx, ctx) ||
  307. !RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, key_len, in, inlen,
  308. rctx->oaep_label, rctx->oaep_labellen,
  309. rctx->md, rctx->mgf1md) ||
  310. !RSA_encrypt(rsa, outlen, out, *outlen, rctx->tbuf, key_len,
  311. RSA_NO_PADDING)) {
  312. return 0;
  313. }
  314. return 1;
  315. }
  316. return RSA_encrypt(rsa, outlen, out, *outlen, in, inlen, rctx->pad_mode);
  317. }
  318. static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out,
  319. size_t *outlen, const uint8_t *in,
  320. size_t inlen) {
  321. RSA_PKEY_CTX *rctx = ctx->data;
  322. RSA *rsa = ctx->pkey->pkey.rsa;
  323. const size_t key_len = EVP_PKEY_size(ctx->pkey);
  324. if (!out) {
  325. *outlen = key_len;
  326. return 1;
  327. }
  328. if (*outlen < key_len) {
  329. OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
  330. return 0;
  331. }
  332. if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  333. size_t padded_len;
  334. if (!setup_tbuf(rctx, ctx) ||
  335. !RSA_decrypt(rsa, &padded_len, rctx->tbuf, key_len, in, inlen,
  336. RSA_NO_PADDING) ||
  337. !RSA_padding_check_PKCS1_OAEP_mgf1(
  338. out, outlen, key_len, rctx->tbuf, padded_len, rctx->oaep_label,
  339. rctx->oaep_labellen, rctx->md, rctx->mgf1md)) {
  340. return 0;
  341. }
  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. NULL /* sign_message */,
  509. pkey_rsa_verify,
  510. NULL /* verify_message */,
  511. pkey_rsa_verify_recover,
  512. pkey_rsa_encrypt,
  513. pkey_rsa_decrypt,
  514. 0 /* derive */,
  515. pkey_rsa_ctrl,
  516. };
  517. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) {
  518. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING,
  519. padding, NULL);
  520. }
  521. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) {
  522. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  523. 0, out_padding);
  524. }
  525. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
  526. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  527. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  528. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, NULL);
  529. }
  530. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) {
  531. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  532. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  533. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len);
  534. }
  535. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
  536. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  537. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
  538. }
  539. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) {
  540. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  541. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e);
  542. }
  543. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  544. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  545. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
  546. }
  547. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  548. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  549. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void*) out_md);
  550. }
  551. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  552. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  553. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  554. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void*) md);
  555. }
  556. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  557. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  558. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  559. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void*) out_md);
  560. }
  561. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, uint8_t *label,
  562. size_t label_len) {
  563. if (label_len > INT_MAX) {
  564. return 0;
  565. }
  566. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  567. EVP_PKEY_CTRL_RSA_OAEP_LABEL, (int)label_len,
  568. (void *)label);
  569. }
  570. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  571. const uint8_t **out_label) {
  572. CBS label;
  573. if (!EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  574. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, &label)) {
  575. return -1;
  576. }
  577. if (CBS_len(&label) > INT_MAX) {
  578. OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
  579. return -1;
  580. }
  581. *out_label = CBS_data(&label);
  582. return (int)CBS_len(&label);
  583. }