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