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