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