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