rsa_enc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * Copyright 2019-2025 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 <openssl/crypto.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/core_dispatch.h>
  17. #include <openssl/core_names.h>
  18. #include <openssl/rsa.h>
  19. #include <openssl/params.h>
  20. #include <openssl/err.h>
  21. #include <openssl/proverr.h>
  22. /* Just for SSL_MAX_MASTER_KEY_LENGTH */
  23. #include <openssl/prov_ssl.h>
  24. #include "internal/constant_time.h"
  25. #include "internal/sizes.h"
  26. #include "crypto/rsa.h"
  27. #include "prov/provider_ctx.h"
  28. #include "prov/implementations.h"
  29. #include "prov/providercommon.h"
  30. #include "prov/securitycheck.h"
  31. #include <stdlib.h>
  32. static OSSL_FUNC_asym_cipher_newctx_fn rsa_newctx;
  33. static OSSL_FUNC_asym_cipher_encrypt_init_fn rsa_encrypt_init;
  34. static OSSL_FUNC_asym_cipher_encrypt_fn rsa_encrypt;
  35. static OSSL_FUNC_asym_cipher_decrypt_init_fn rsa_decrypt_init;
  36. static OSSL_FUNC_asym_cipher_decrypt_fn rsa_decrypt;
  37. static OSSL_FUNC_asym_cipher_freectx_fn rsa_freectx;
  38. static OSSL_FUNC_asym_cipher_dupctx_fn rsa_dupctx;
  39. static OSSL_FUNC_asym_cipher_get_ctx_params_fn rsa_get_ctx_params;
  40. static OSSL_FUNC_asym_cipher_gettable_ctx_params_fn rsa_gettable_ctx_params;
  41. static OSSL_FUNC_asym_cipher_set_ctx_params_fn rsa_set_ctx_params;
  42. static OSSL_FUNC_asym_cipher_settable_ctx_params_fn rsa_settable_ctx_params;
  43. static OSSL_ITEM padding_item[] = {
  44. { RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
  45. { RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE },
  46. { RSA_PKCS1_OAEP_PADDING, OSSL_PKEY_RSA_PAD_MODE_OAEP }, /* Correct spelling first */
  47. { RSA_PKCS1_OAEP_PADDING, "oeap" },
  48. { 0, NULL }
  49. };
  50. /*
  51. * What's passed as an actual key is defined by the KEYMGMT interface.
  52. * We happen to know that our KEYMGMT simply passes RSA structures, so
  53. * we use that here too.
  54. */
  55. typedef struct {
  56. OSSL_LIB_CTX *libctx;
  57. RSA *rsa;
  58. int pad_mode;
  59. int operation;
  60. /* OAEP message digest */
  61. EVP_MD *oaep_md;
  62. /* message digest for MGF1 */
  63. EVP_MD *mgf1_md;
  64. /* OAEP label */
  65. unsigned char *oaep_label;
  66. size_t oaep_labellen;
  67. /* TLS padding */
  68. unsigned int client_version;
  69. unsigned int alt_version;
  70. /* PKCS#1 v1.5 decryption mode */
  71. unsigned int implicit_rejection;
  72. OSSL_FIPS_IND_DECLARE
  73. } PROV_RSA_CTX;
  74. static void *rsa_newctx(void *provctx)
  75. {
  76. PROV_RSA_CTX *prsactx;
  77. if (!ossl_prov_is_running())
  78. return NULL;
  79. prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
  80. if (prsactx == NULL)
  81. return NULL;
  82. prsactx->libctx = PROV_LIBCTX_OF(provctx);
  83. OSSL_FIPS_IND_INIT(prsactx)
  84. return prsactx;
  85. }
  86. static int rsa_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[],
  87. int operation, const char *desc)
  88. {
  89. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  90. int protect = 0;
  91. if (!ossl_prov_is_running() || prsactx == NULL || vrsa == NULL)
  92. return 0;
  93. if (!ossl_rsa_key_op_get_protect(vrsa, operation, &protect))
  94. return 0;
  95. if (!RSA_up_ref(vrsa))
  96. return 0;
  97. RSA_free(prsactx->rsa);
  98. prsactx->rsa = vrsa;
  99. prsactx->operation = operation;
  100. prsactx->implicit_rejection = 1;
  101. switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
  102. case RSA_FLAG_TYPE_RSA:
  103. prsactx->pad_mode = RSA_PKCS1_PADDING;
  104. break;
  105. default:
  106. /* This should not happen due to the check above */
  107. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  108. return 0;
  109. }
  110. OSSL_FIPS_IND_SET_APPROVED(prsactx)
  111. if (!rsa_set_ctx_params(prsactx, params))
  112. return 0;
  113. #ifdef FIPS_MODULE
  114. if (!ossl_fips_ind_rsa_key_check(OSSL_FIPS_IND_GET(prsactx),
  115. OSSL_FIPS_IND_SETTABLE0, prsactx->libctx,
  116. prsactx->rsa, desc, protect))
  117. return 0;
  118. #endif
  119. return 1;
  120. }
  121. static int rsa_encrypt_init(void *vprsactx, void *vrsa,
  122. const OSSL_PARAM params[])
  123. {
  124. return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCRYPT,
  125. "RSA Encrypt Init");
  126. }
  127. static int rsa_decrypt_init(void *vprsactx, void *vrsa,
  128. const OSSL_PARAM params[])
  129. {
  130. return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECRYPT,
  131. "RSA Decrypt Init");
  132. }
  133. static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
  134. size_t outsize, const unsigned char *in, size_t inlen)
  135. {
  136. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  137. size_t len = RSA_size(prsactx->rsa);
  138. int ret;
  139. if (!ossl_prov_is_running())
  140. return 0;
  141. #ifdef FIPS_MODULE
  142. if ((prsactx->pad_mode == RSA_PKCS1_PADDING
  143. || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING)
  144. && !OSSL_FIPS_IND_ON_UNAPPROVED(prsactx, OSSL_FIPS_IND_SETTABLE1,
  145. prsactx->libctx, "RSA Encrypt",
  146. "PKCS#1 v1.5 padding",
  147. ossl_fips_config_rsa_pkcs15_padding_disabled)) {
  148. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
  149. return 0;
  150. }
  151. #endif
  152. if (len == 0) {
  153. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  154. return 0;
  155. }
  156. if (out == NULL) {
  157. *outlen = len;
  158. return 1;
  159. }
  160. if (outsize < len) {
  161. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  162. return 0;
  163. }
  164. if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  165. int rsasize = RSA_size(prsactx->rsa);
  166. unsigned char *tbuf;
  167. if ((tbuf = OPENSSL_malloc(rsasize)) == NULL)
  168. return 0;
  169. if (prsactx->oaep_md == NULL) {
  170. prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
  171. if (prsactx->oaep_md == NULL) {
  172. OPENSSL_free(tbuf);
  173. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  174. return 0;
  175. }
  176. }
  177. ret =
  178. ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(prsactx->libctx, tbuf,
  179. rsasize, in, inlen,
  180. prsactx->oaep_label,
  181. prsactx->oaep_labellen,
  182. prsactx->oaep_md,
  183. prsactx->mgf1_md);
  184. if (!ret) {
  185. OPENSSL_free(tbuf);
  186. return 0;
  187. }
  188. ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
  189. RSA_NO_PADDING);
  190. OPENSSL_free(tbuf);
  191. } else {
  192. ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
  193. prsactx->pad_mode);
  194. }
  195. /* A ret value of 0 is not an error */
  196. if (ret < 0)
  197. return ret;
  198. *outlen = ret;
  199. return 1;
  200. }
  201. static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
  202. size_t outsize, const unsigned char *in, size_t inlen)
  203. {
  204. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  205. int ret;
  206. int pad_mode;
  207. size_t len = RSA_size(prsactx->rsa);
  208. if (!ossl_prov_is_running())
  209. return 0;
  210. if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
  211. if (out == NULL) {
  212. *outlen = SSL_MAX_MASTER_KEY_LENGTH;
  213. return 1;
  214. }
  215. if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
  216. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  217. return 0;
  218. }
  219. } else {
  220. if (out == NULL) {
  221. if (len == 0) {
  222. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  223. return 0;
  224. }
  225. *outlen = len;
  226. return 1;
  227. }
  228. if (outsize < len) {
  229. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  230. return 0;
  231. }
  232. }
  233. if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
  234. || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
  235. unsigned char *tbuf;
  236. if ((tbuf = OPENSSL_malloc(len)) == NULL)
  237. return 0;
  238. ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
  239. RSA_NO_PADDING);
  240. /*
  241. * With no padding then, on success ret should be len, otherwise an
  242. * error occurred (non-constant time)
  243. */
  244. if (ret != (int)len) {
  245. OPENSSL_free(tbuf);
  246. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
  247. return 0;
  248. }
  249. if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
  250. if (prsactx->oaep_md == NULL) {
  251. prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
  252. if (prsactx->oaep_md == NULL) {
  253. OPENSSL_free(tbuf);
  254. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  255. return 0;
  256. }
  257. }
  258. ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf,
  259. len, len,
  260. prsactx->oaep_label,
  261. prsactx->oaep_labellen,
  262. prsactx->oaep_md,
  263. prsactx->mgf1_md);
  264. } else {
  265. /* RSA_PKCS1_WITH_TLS_PADDING */
  266. if (prsactx->client_version <= 0) {
  267. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
  268. OPENSSL_free(tbuf);
  269. return 0;
  270. }
  271. ret = ossl_rsa_padding_check_PKCS1_type_2_TLS(
  272. prsactx->libctx, out, outsize, tbuf, len,
  273. prsactx->client_version, prsactx->alt_version);
  274. }
  275. OPENSSL_free(tbuf);
  276. } else {
  277. if ((prsactx->implicit_rejection == 0) &&
  278. (prsactx->pad_mode == RSA_PKCS1_PADDING))
  279. pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
  280. else
  281. pad_mode = prsactx->pad_mode;
  282. ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, pad_mode);
  283. }
  284. *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
  285. ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
  286. return ret;
  287. }
  288. static void rsa_freectx(void *vprsactx)
  289. {
  290. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  291. RSA_free(prsactx->rsa);
  292. EVP_MD_free(prsactx->oaep_md);
  293. EVP_MD_free(prsactx->mgf1_md);
  294. OPENSSL_free(prsactx->oaep_label);
  295. OPENSSL_free(prsactx);
  296. }
  297. static void *rsa_dupctx(void *vprsactx)
  298. {
  299. PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
  300. PROV_RSA_CTX *dstctx;
  301. if (!ossl_prov_is_running())
  302. return NULL;
  303. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  304. if (dstctx == NULL)
  305. return NULL;
  306. *dstctx = *srcctx;
  307. if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
  308. OPENSSL_free(dstctx);
  309. return NULL;
  310. }
  311. if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
  312. RSA_free(dstctx->rsa);
  313. OPENSSL_free(dstctx);
  314. return NULL;
  315. }
  316. if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
  317. RSA_free(dstctx->rsa);
  318. EVP_MD_free(dstctx->oaep_md);
  319. OPENSSL_free(dstctx);
  320. return NULL;
  321. }
  322. return dstctx;
  323. }
  324. static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
  325. {
  326. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  327. OSSL_PARAM *p;
  328. if (prsactx == NULL)
  329. return 0;
  330. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
  331. if (p != NULL)
  332. switch (p->data_type) {
  333. case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
  334. if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
  335. return 0;
  336. break;
  337. case OSSL_PARAM_UTF8_STRING:
  338. {
  339. int i;
  340. const char *word = NULL;
  341. for (i = 0; padding_item[i].id != 0; i++) {
  342. if (prsactx->pad_mode == (int)padding_item[i].id) {
  343. word = padding_item[i].ptr;
  344. break;
  345. }
  346. }
  347. if (word != NULL) {
  348. if (!OSSL_PARAM_set_utf8_string(p, word))
  349. return 0;
  350. } else {
  351. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  352. }
  353. }
  354. break;
  355. default:
  356. return 0;
  357. }
  358. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
  359. if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
  360. ? ""
  361. : EVP_MD_get0_name(prsactx->oaep_md)))
  362. return 0;
  363. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
  364. if (p != NULL) {
  365. EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
  366. : prsactx->mgf1_md;
  367. if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
  368. ? ""
  369. : EVP_MD_get0_name(mgf1_md)))
  370. return 0;
  371. }
  372. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
  373. if (p != NULL &&
  374. !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label,
  375. prsactx->oaep_labellen))
  376. return 0;
  377. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
  378. if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
  379. return 0;
  380. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
  381. if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
  382. return 0;
  383. p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION);
  384. if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->implicit_rejection))
  385. return 0;
  386. if (!OSSL_FIPS_IND_GET_CTX_PARAM(prsactx, params))
  387. return 0;
  388. return 1;
  389. }
  390. static const OSSL_PARAM known_gettable_ctx_params[] = {
  391. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
  392. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
  393. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
  394. OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
  395. NULL, 0),
  396. OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
  397. OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
  398. OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
  399. OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
  400. OSSL_PARAM_END
  401. };
  402. static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
  403. ossl_unused void *provctx)
  404. {
  405. return known_gettable_ctx_params;
  406. }
  407. static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
  408. {
  409. PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
  410. const OSSL_PARAM *p;
  411. char mdname[OSSL_MAX_NAME_SIZE];
  412. char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
  413. char *str = NULL;
  414. if (prsactx == NULL)
  415. return 0;
  416. if (ossl_param_is_empty(params))
  417. return 1;
  418. if (!OSSL_FIPS_IND_SET_CTX_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE0, params,
  419. OSSL_ASYM_CIPHER_PARAM_FIPS_KEY_CHECK))
  420. return 0;
  421. if (!OSSL_FIPS_IND_SET_CTX_PARAM(prsactx, OSSL_FIPS_IND_SETTABLE1, params,
  422. OSSL_ASYM_CIPHER_PARAM_FIPS_RSA_PKCS15_PAD_DISABLED))
  423. return 0;
  424. p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
  425. if (p != NULL) {
  426. str = mdname;
  427. if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
  428. return 0;
  429. p = OSSL_PARAM_locate_const(params,
  430. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
  431. if (p != NULL) {
  432. str = mdprops;
  433. if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
  434. return 0;
  435. }
  436. EVP_MD_free(prsactx->oaep_md);
  437. prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
  438. if (prsactx->oaep_md == NULL)
  439. return 0;
  440. }
  441. p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
  442. if (p != NULL) {
  443. int pad_mode = 0;
  444. switch (p->data_type) {
  445. case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
  446. if (!OSSL_PARAM_get_int(p, &pad_mode))
  447. return 0;
  448. break;
  449. case OSSL_PARAM_UTF8_STRING:
  450. {
  451. int i;
  452. if (p->data == NULL)
  453. return 0;
  454. for (i = 0; padding_item[i].id != 0; i++) {
  455. if (strcmp(p->data, padding_item[i].ptr) == 0) {
  456. pad_mode = padding_item[i].id;
  457. break;
  458. }
  459. }
  460. }
  461. break;
  462. default:
  463. return 0;
  464. }
  465. /*
  466. * PSS padding is for signatures only so is not compatible with
  467. * asymmetric cipher use.
  468. */
  469. if (pad_mode == RSA_PKCS1_PSS_PADDING)
  470. return 0;
  471. if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
  472. prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
  473. if (prsactx->oaep_md == NULL)
  474. return 0;
  475. }
  476. prsactx->pad_mode = pad_mode;
  477. }
  478. p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
  479. if (p != NULL) {
  480. str = mdname;
  481. if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
  482. return 0;
  483. p = OSSL_PARAM_locate_const(params,
  484. OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
  485. if (p != NULL) {
  486. str = mdprops;
  487. if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
  488. return 0;
  489. } else {
  490. str = NULL;
  491. }
  492. EVP_MD_free(prsactx->mgf1_md);
  493. prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
  494. if (prsactx->mgf1_md == NULL)
  495. return 0;
  496. }
  497. p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
  498. if (p != NULL) {
  499. void *tmp_label = NULL;
  500. size_t tmp_labellen;
  501. if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
  502. return 0;
  503. OPENSSL_free(prsactx->oaep_label);
  504. prsactx->oaep_label = (unsigned char *)tmp_label;
  505. prsactx->oaep_labellen = tmp_labellen;
  506. }
  507. p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
  508. if (p != NULL) {
  509. unsigned int client_version;
  510. if (!OSSL_PARAM_get_uint(p, &client_version))
  511. return 0;
  512. prsactx->client_version = client_version;
  513. }
  514. p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
  515. if (p != NULL) {
  516. unsigned int alt_version;
  517. if (!OSSL_PARAM_get_uint(p, &alt_version))
  518. return 0;
  519. prsactx->alt_version = alt_version;
  520. }
  521. p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION);
  522. if (p != NULL) {
  523. unsigned int implicit_rejection;
  524. if (!OSSL_PARAM_get_uint(p, &implicit_rejection))
  525. return 0;
  526. prsactx->implicit_rejection = implicit_rejection;
  527. }
  528. return 1;
  529. }
  530. static const OSSL_PARAM known_settable_ctx_params[] = {
  531. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
  532. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, NULL, 0),
  533. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
  534. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
  535. OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
  536. OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
  537. OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
  538. OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
  539. OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
  540. OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_ASYM_CIPHER_PARAM_FIPS_KEY_CHECK)
  541. OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_ASYM_CIPHER_PARAM_FIPS_RSA_PKCS15_PAD_DISABLED)
  542. OSSL_PARAM_END
  543. };
  544. static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *vprsactx,
  545. ossl_unused void *provctx)
  546. {
  547. return known_settable_ctx_params;
  548. }
  549. const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = {
  550. { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
  551. { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init },
  552. { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
  553. { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init },
  554. { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
  555. { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
  556. { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
  557. { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
  558. (void (*)(void))rsa_get_ctx_params },
  559. { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
  560. (void (*)(void))rsa_gettable_ctx_params },
  561. { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
  562. (void (*)(void))rsa_set_ctx_params },
  563. { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
  564. (void (*)(void))rsa_settable_ctx_params },
  565. OSSL_DISPATCH_END
  566. };