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