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