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