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