rsa_enc.c 20 KB

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