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.
 
 
 
 
 
 

602 lines
18 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/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. case EVP_PKEY_CTRL_DIGESTINIT:
  418. return 1;
  419. default:
  420. OPENSSL_PUT_ERROR(EVP, pkey_rsa_ctrl, EVP_R_COMMAND_NOT_SUPPORTED);
  421. return 0;
  422. }
  423. }
  424. static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
  425. RSA *rsa = NULL;
  426. RSA_PKEY_CTX *rctx = ctx->data;
  427. if (!rctx->pub_exp) {
  428. rctx->pub_exp = BN_new();
  429. if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) {
  430. return 0;
  431. }
  432. }
  433. rsa = RSA_new();
  434. if (!rsa) {
  435. return 0;
  436. }
  437. if (!RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, NULL)) {
  438. RSA_free(rsa);
  439. return 0;
  440. }
  441. EVP_PKEY_assign_RSA(pkey, rsa);
  442. return 1;
  443. }
  444. const EVP_PKEY_METHOD rsa_pkey_meth = {
  445. EVP_PKEY_RSA, 0 /* flags */, pkey_rsa_init,
  446. pkey_rsa_copy, pkey_rsa_cleanup, 0 /* paramgen_init */,
  447. 0 /* paramgen */, 0 /* keygen_init */, pkey_rsa_keygen,
  448. 0 /* sign_init */, pkey_rsa_sign, 0 /* verify_init */,
  449. pkey_rsa_verify, 0 /* signctx_init */, 0 /* signctx */,
  450. 0 /* verifyctx_init */, 0 /* verifyctx */, 0 /* encrypt_init */,
  451. pkey_rsa_encrypt, 0 /* decrypt_init */, pkey_rsa_decrypt,
  452. 0 /* derive_init */, 0 /* derive */, pkey_rsa_ctrl,
  453. };
  454. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int padding) {
  455. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_RSA_PADDING,
  456. padding, NULL);
  457. }
  458. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *out_padding) {
  459. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  460. 0, out_padding);
  461. }
  462. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int salt_len) {
  463. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  464. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  465. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, salt_len, NULL);
  466. }
  467. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *out_salt_len) {
  468. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  469. (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY),
  470. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, out_salt_len);
  471. }
  472. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) {
  473. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  474. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
  475. }
  476. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *e) {
  477. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  478. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, e);
  479. }
  480. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  481. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  482. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
  483. }
  484. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  485. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  486. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void*) out_md);
  487. }
  488. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  489. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  490. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  491. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void*) md);
  492. }
  493. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  494. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  495. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  496. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void*) out_md);
  497. }
  498. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, const uint8_t *label,
  499. size_t label_len) {
  500. int label_len_int = label_len;
  501. if (((size_t) label_len_int) != label_len) {
  502. return 0;
  503. }
  504. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  505. EVP_PKEY_CTRL_RSA_OAEP_LABEL, label_len,
  506. (void *)label);
  507. }
  508. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx,
  509. const uint8_t **out_label) {
  510. CBS label;
  511. if (!EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  512. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, &label)) {
  513. return -1;
  514. }
  515. if (CBS_len(&label) > INT_MAX) {
  516. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_CTX_get0_rsa_oaep_label, ERR_R_OVERFLOW);
  517. return -1;
  518. }
  519. *out_label = CBS_data(&label);
  520. return (int)CBS_len(&label);
  521. }