/src/openssl32/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 | | /* PKCS#1 v1.5 decryption mode */ |
79 | | unsigned int implicit_rejection; |
80 | | } PROV_RSA_CTX; |
81 | | |
82 | | static void *rsa_newctx(void *provctx) |
83 | 6.96k | { |
84 | 6.96k | PROV_RSA_CTX *prsactx; |
85 | | |
86 | 6.96k | if (!ossl_prov_is_running()) |
87 | 0 | return NULL; |
88 | 6.96k | prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX)); |
89 | 6.96k | if (prsactx == NULL) |
90 | 0 | return NULL; |
91 | 6.96k | prsactx->libctx = PROV_LIBCTX_OF(provctx); |
92 | | |
93 | 6.96k | return prsactx; |
94 | 6.96k | } |
95 | | |
96 | | static int rsa_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[], |
97 | | int operation) |
98 | 4.09k | { |
99 | 4.09k | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
100 | | |
101 | 4.09k | if (!ossl_prov_is_running() || prsactx == NULL || vrsa == NULL) |
102 | 0 | return 0; |
103 | | |
104 | 4.09k | if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation)) |
105 | 0 | return 0; |
106 | | |
107 | 4.09k | if (!RSA_up_ref(vrsa)) |
108 | 0 | return 0; |
109 | 4.09k | RSA_free(prsactx->rsa); |
110 | 4.09k | prsactx->rsa = vrsa; |
111 | 4.09k | prsactx->operation = operation; |
112 | 4.09k | prsactx->implicit_rejection = 1; |
113 | | |
114 | 4.09k | switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) { |
115 | 4.09k | case RSA_FLAG_TYPE_RSA: |
116 | 4.09k | prsactx->pad_mode = RSA_PKCS1_PADDING; |
117 | 4.09k | 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 | 4.09k | } |
123 | 4.09k | return rsa_set_ctx_params(prsactx, params); |
124 | 4.09k | } |
125 | | |
126 | | static int rsa_encrypt_init(void *vprsactx, void *vrsa, |
127 | | const OSSL_PARAM params[]) |
128 | 1.36k | { |
129 | 1.36k | return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCRYPT); |
130 | 1.36k | } |
131 | | |
132 | | static int rsa_decrypt_init(void *vprsactx, void *vrsa, |
133 | | const OSSL_PARAM params[]) |
134 | 5.60k | { |
135 | 5.60k | return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECRYPT); |
136 | 5.60k | } |
137 | | |
138 | | static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen, |
139 | | size_t outsize, const unsigned char *in, size_t inlen) |
140 | 2.71k | { |
141 | 2.71k | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
142 | 2.71k | int ret; |
143 | | |
144 | 2.71k | if (!ossl_prov_is_running()) |
145 | 0 | return 0; |
146 | | |
147 | 2.71k | if (out == NULL) { |
148 | 1.36k | size_t len = RSA_size(prsactx->rsa); |
149 | | |
150 | 1.36k | if (len == 0) { |
151 | 4 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
152 | 4 | return 0; |
153 | 4 | } |
154 | 1.35k | *outlen = len; |
155 | 1.35k | return 1; |
156 | 1.36k | } |
157 | | |
158 | 1.35k | if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
159 | 0 | int rsasize = RSA_size(prsactx->rsa); |
160 | 0 | unsigned char *tbuf; |
161 | |
|
162 | 0 | if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) |
163 | 0 | return 0; |
164 | 0 | if (prsactx->oaep_md == NULL) { |
165 | 0 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL); |
166 | 0 | if (prsactx->oaep_md == NULL) { |
167 | 0 | OPENSSL_free(tbuf); |
168 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
169 | 0 | return 0; |
170 | 0 | } |
171 | 0 | } |
172 | 0 | ret = |
173 | 0 | ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(prsactx->libctx, tbuf, |
174 | 0 | rsasize, in, inlen, |
175 | 0 | prsactx->oaep_label, |
176 | 0 | prsactx->oaep_labellen, |
177 | 0 | prsactx->oaep_md, |
178 | 0 | prsactx->mgf1_md); |
179 | |
|
180 | 0 | if (!ret) { |
181 | 0 | OPENSSL_free(tbuf); |
182 | 0 | return 0; |
183 | 0 | } |
184 | 0 | ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa, |
185 | 0 | RSA_NO_PADDING); |
186 | 0 | OPENSSL_free(tbuf); |
187 | 1.35k | } else { |
188 | 1.35k | ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa, |
189 | 1.35k | prsactx->pad_mode); |
190 | 1.35k | } |
191 | | /* A ret value of 0 is not an error */ |
192 | 1.35k | if (ret < 0) |
193 | 266 | return ret; |
194 | 1.09k | *outlen = ret; |
195 | 1.09k | return 1; |
196 | 1.35k | } |
197 | | |
198 | | static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen, |
199 | | size_t outsize, const unsigned char *in, size_t inlen) |
200 | 3.82k | { |
201 | 3.82k | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
202 | 3.82k | int ret; |
203 | 3.82k | int pad_mode; |
204 | 3.82k | size_t len = RSA_size(prsactx->rsa); |
205 | | |
206 | 3.82k | if (!ossl_prov_is_running()) |
207 | 0 | return 0; |
208 | | |
209 | 3.82k | if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) { |
210 | 3.82k | if (out == NULL) { |
211 | 0 | *outlen = SSL_MAX_MASTER_KEY_LENGTH; |
212 | 0 | return 1; |
213 | 0 | } |
214 | 3.82k | if (outsize < SSL_MAX_MASTER_KEY_LENGTH) { |
215 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); |
216 | 0 | return 0; |
217 | 0 | } |
218 | 3.82k | } else { |
219 | 0 | if (out == NULL) { |
220 | 0 | if (len == 0) { |
221 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
222 | 0 | return 0; |
223 | 0 | } |
224 | 0 | *outlen = len; |
225 | 0 | return 1; |
226 | 0 | } |
227 | | |
228 | 0 | if (outsize < len) { |
229 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); |
230 | 0 | return 0; |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | 3.82k | if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING |
235 | 3.82k | || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) { |
236 | 3.82k | unsigned char *tbuf; |
237 | | |
238 | 3.82k | if ((tbuf = OPENSSL_malloc(len)) == NULL) |
239 | 0 | return 0; |
240 | 3.82k | ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa, |
241 | 3.82k | 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 | 3.82k | if (ret != (int)len) { |
247 | 14 | OPENSSL_free(tbuf); |
248 | 14 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT); |
249 | 14 | return 0; |
250 | 14 | } |
251 | 3.81k | 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 | 3.81k | } else { |
267 | | /* RSA_PKCS1_WITH_TLS_PADDING */ |
268 | 3.81k | if (prsactx->client_version <= 0) { |
269 | 13 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION); |
270 | 13 | OPENSSL_free(tbuf); |
271 | 13 | return 0; |
272 | 13 | } |
273 | 3.79k | ret = ossl_rsa_padding_check_PKCS1_type_2_TLS( |
274 | 3.79k | prsactx->libctx, out, outsize, tbuf, len, |
275 | 3.79k | prsactx->client_version, prsactx->alt_version); |
276 | 3.79k | } |
277 | 3.79k | OPENSSL_free(tbuf); |
278 | 3.79k | } else { |
279 | 0 | if ((prsactx->implicit_rejection == 0) && |
280 | 0 | (prsactx->pad_mode == RSA_PKCS1_PADDING)) |
281 | 0 | pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING; |
282 | 0 | else |
283 | 0 | pad_mode = prsactx->pad_mode; |
284 | 0 | ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, pad_mode); |
285 | 0 | } |
286 | 3.79k | *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret); |
287 | 3.79k | ret = constant_time_select_int(constant_time_msb(ret), 0, 1); |
288 | 3.79k | return ret; |
289 | 3.82k | } |
290 | | |
291 | | static void rsa_freectx(void *vprsactx) |
292 | 6.96k | { |
293 | 6.96k | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
294 | | |
295 | 6.96k | RSA_free(prsactx->rsa); |
296 | | |
297 | 6.96k | EVP_MD_free(prsactx->oaep_md); |
298 | 6.96k | EVP_MD_free(prsactx->mgf1_md); |
299 | 6.96k | OPENSSL_free(prsactx->oaep_label); |
300 | | |
301 | 6.96k | OPENSSL_free(prsactx); |
302 | 6.96k | } |
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 | { |
355 | 0 | int i; |
356 | 0 | const char *word = NULL; |
357 | |
|
358 | 0 | for (i = 0; padding_item[i].id != 0; i++) { |
359 | 0 | if (prsactx->pad_mode == (int)padding_item[i].id) { |
360 | 0 | word = padding_item[i].ptr; |
361 | 0 | break; |
362 | 0 | } |
363 | 0 | } |
364 | |
|
365 | 0 | if (word != NULL) { |
366 | 0 | if (!OSSL_PARAM_set_utf8_string(p, word)) |
367 | 0 | return 0; |
368 | 0 | } else { |
369 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
370 | 0 | } |
371 | 0 | } |
372 | 0 | break; |
373 | 0 | default: |
374 | 0 | return 0; |
375 | 0 | } |
376 | | |
377 | 0 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST); |
378 | 0 | if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL |
379 | 0 | ? "" |
380 | 0 | : EVP_MD_get0_name(prsactx->oaep_md))) |
381 | 0 | return 0; |
382 | | |
383 | 0 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST); |
384 | 0 | if (p != NULL) { |
385 | 0 | EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md |
386 | 0 | : prsactx->mgf1_md; |
387 | |
|
388 | 0 | if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL |
389 | 0 | ? "" |
390 | 0 | : EVP_MD_get0_name(mgf1_md))) |
391 | 0 | return 0; |
392 | 0 | } |
393 | | |
394 | 0 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL); |
395 | 0 | if (p != NULL && |
396 | 0 | !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, |
397 | 0 | prsactx->oaep_labellen)) |
398 | 0 | return 0; |
399 | | |
400 | 0 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION); |
401 | 0 | if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version)) |
402 | 0 | return 0; |
403 | | |
404 | 0 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION); |
405 | 0 | if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version)) |
406 | 0 | return 0; |
407 | | |
408 | 0 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION); |
409 | 0 | if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->implicit_rejection)) |
410 | 0 | return 0; |
411 | | |
412 | 0 | return 1; |
413 | 0 | } |
414 | | |
415 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
416 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0), |
417 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0), |
418 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0), |
419 | | OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR, |
420 | | NULL, 0), |
421 | | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL), |
422 | | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL), |
423 | | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL), |
424 | | OSSL_PARAM_END |
425 | | }; |
426 | | |
427 | | static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx, |
428 | | ossl_unused void *provctx) |
429 | 0 | { |
430 | 0 | return known_gettable_ctx_params; |
431 | 0 | } |
432 | | |
433 | | static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) |
434 | 4.29k | { |
435 | 4.29k | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; |
436 | 4.29k | const OSSL_PARAM *p; |
437 | 4.29k | char mdname[OSSL_MAX_NAME_SIZE]; |
438 | 4.29k | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' }; |
439 | 4.29k | char *str = NULL; |
440 | | |
441 | 4.29k | if (prsactx == NULL) |
442 | 0 | return 0; |
443 | 4.29k | if (params == NULL) |
444 | 1.68k | return 1; |
445 | | |
446 | 2.60k | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST); |
447 | 2.60k | if (p != NULL) { |
448 | 0 | str = mdname; |
449 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname))) |
450 | 0 | return 0; |
451 | | |
452 | 0 | p = OSSL_PARAM_locate_const(params, |
453 | 0 | OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS); |
454 | 0 | if (p != NULL) { |
455 | 0 | str = mdprops; |
456 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops))) |
457 | 0 | return 0; |
458 | 0 | } |
459 | | |
460 | 0 | EVP_MD_free(prsactx->oaep_md); |
461 | 0 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops); |
462 | |
|
463 | 0 | if (prsactx->oaep_md == NULL) |
464 | 0 | return 0; |
465 | 0 | } |
466 | | |
467 | 2.60k | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE); |
468 | 2.60k | if (p != NULL) { |
469 | 1.30k | int pad_mode = 0; |
470 | | |
471 | 1.30k | switch (p->data_type) { |
472 | 1.30k | case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */ |
473 | 1.30k | if (!OSSL_PARAM_get_int(p, &pad_mode)) |
474 | 0 | return 0; |
475 | 1.30k | break; |
476 | 1.30k | case OSSL_PARAM_UTF8_STRING: |
477 | 0 | { |
478 | 0 | int i; |
479 | |
|
480 | 0 | if (p->data == NULL) |
481 | 0 | return 0; |
482 | | |
483 | 0 | for (i = 0; padding_item[i].id != 0; i++) { |
484 | 0 | if (strcmp(p->data, padding_item[i].ptr) == 0) { |
485 | 0 | pad_mode = padding_item[i].id; |
486 | 0 | break; |
487 | 0 | } |
488 | 0 | } |
489 | 0 | } |
490 | 0 | break; |
491 | 0 | default: |
492 | 0 | return 0; |
493 | 1.30k | } |
494 | | |
495 | | /* |
496 | | * PSS padding is for signatures only so is not compatible with |
497 | | * asymmetric cipher use. |
498 | | */ |
499 | 1.30k | if (pad_mode == RSA_PKCS1_PSS_PADDING) |
500 | 0 | return 0; |
501 | 1.30k | if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) { |
502 | 0 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops); |
503 | 0 | if (prsactx->oaep_md == NULL) |
504 | 0 | return 0; |
505 | 0 | } |
506 | 1.30k | prsactx->pad_mode = pad_mode; |
507 | 1.30k | } |
508 | | |
509 | 2.60k | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST); |
510 | 2.60k | if (p != NULL) { |
511 | 0 | str = mdname; |
512 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname))) |
513 | 0 | return 0; |
514 | | |
515 | 0 | p = OSSL_PARAM_locate_const(params, |
516 | 0 | OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS); |
517 | 0 | if (p != NULL) { |
518 | 0 | str = mdprops; |
519 | 0 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops))) |
520 | 0 | return 0; |
521 | 0 | } else { |
522 | 0 | str = NULL; |
523 | 0 | } |
524 | | |
525 | 0 | EVP_MD_free(prsactx->mgf1_md); |
526 | 0 | prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str); |
527 | |
|
528 | 0 | if (prsactx->mgf1_md == NULL) |
529 | 0 | return 0; |
530 | 0 | } |
531 | | |
532 | 2.60k | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL); |
533 | 2.60k | if (p != NULL) { |
534 | 0 | void *tmp_label = NULL; |
535 | 0 | size_t tmp_labellen; |
536 | |
|
537 | 0 | if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen)) |
538 | 0 | return 0; |
539 | 0 | OPENSSL_free(prsactx->oaep_label); |
540 | 0 | prsactx->oaep_label = (unsigned char *)tmp_label; |
541 | 0 | prsactx->oaep_labellen = tmp_labellen; |
542 | 0 | } |
543 | | |
544 | 2.60k | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION); |
545 | 2.60k | if (p != NULL) { |
546 | 1.30k | unsigned int client_version; |
547 | | |
548 | 1.30k | if (!OSSL_PARAM_get_uint(p, &client_version)) |
549 | 0 | return 0; |
550 | 1.30k | prsactx->client_version = client_version; |
551 | 1.30k | } |
552 | | |
553 | 2.60k | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION); |
554 | 2.60k | if (p != NULL) { |
555 | 0 | unsigned int alt_version; |
556 | |
|
557 | 0 | if (!OSSL_PARAM_get_uint(p, &alt_version)) |
558 | 0 | return 0; |
559 | 0 | prsactx->alt_version = alt_version; |
560 | 0 | } |
561 | 2.60k | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION); |
562 | 2.60k | if (p != NULL) { |
563 | 0 | unsigned int implicit_rejection; |
564 | |
|
565 | 0 | if (!OSSL_PARAM_get_uint(p, &implicit_rejection)) |
566 | 0 | return 0; |
567 | 0 | prsactx->implicit_rejection = implicit_rejection; |
568 | 0 | } |
569 | | |
570 | 2.60k | return 1; |
571 | 2.60k | } |
572 | | |
573 | | static const OSSL_PARAM known_settable_ctx_params[] = { |
574 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0), |
575 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, NULL, 0), |
576 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0), |
577 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0), |
578 | | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0), |
579 | | OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0), |
580 | | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL), |
581 | | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL), |
582 | | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL), |
583 | | OSSL_PARAM_END |
584 | | }; |
585 | | |
586 | | static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *vprsactx, |
587 | | ossl_unused void *provctx) |
588 | 5.61k | { |
589 | 5.61k | return known_settable_ctx_params; |
590 | 5.61k | } |
591 | | |
592 | | const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = { |
593 | | { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx }, |
594 | | { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init }, |
595 | | { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt }, |
596 | | { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init }, |
597 | | { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt }, |
598 | | { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx }, |
599 | | { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx }, |
600 | | { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS, |
601 | | (void (*)(void))rsa_get_ctx_params }, |
602 | | { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS, |
603 | | (void (*)(void))rsa_gettable_ctx_params }, |
604 | | { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS, |
605 | | (void (*)(void))rsa_set_ctx_params }, |
606 | | { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS, |
607 | | (void (*)(void))rsa_settable_ctx_params }, |
608 | | OSSL_DISPATCH_END |
609 | | }; |