rsa_enc.c 20 KB

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