]>
Commit | Line | Data |
---|---|---|
89abd1b6 | 1 | /* |
33388b44 | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
89abd1b6 MC |
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 | ||
c5f87134 P |
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 | ||
89abd1b6 MC |
16 | #include <openssl/crypto.h> |
17 | #include <openssl/evp.h> | |
23c48d94 | 18 | #include <openssl/core_dispatch.h> |
89abd1b6 MC |
19 | #include <openssl/core_names.h> |
20 | #include <openssl/rsa.h> | |
21 | #include <openssl/params.h> | |
22 | #include <openssl/err.h> | |
d9a75107 MC |
23 | /* Just for SSL_MAX_MASTER_KEY_LENGTH */ |
24 | #include <openssl/ssl.h> | |
89abd1b6 | 25 | #include "internal/constant_time.h" |
c24937d5 | 26 | #include "internal/sizes.h" |
d9a75107 | 27 | #include "crypto/rsa.h" |
89abd1b6 MC |
28 | #include "prov/providercommonerr.h" |
29 | #include "prov/provider_ctx.h" | |
30 | #include "prov/implementations.h" | |
87fe138d | 31 | #include "prov/providercommon.h" |
7a810fac | 32 | #include "prov/securitycheck.h" |
89abd1b6 MC |
33 | |
34 | #include <stdlib.h> | |
35 | ||
363b1e5d | 36 | static OSSL_FUNC_asym_cipher_newctx_fn rsa_newctx; |
8d17cca5 | 37 | static OSSL_FUNC_asym_cipher_encrypt_init_fn rsa_encrypt_init; |
363b1e5d | 38 | static OSSL_FUNC_asym_cipher_encrypt_fn rsa_encrypt; |
8d17cca5 | 39 | static OSSL_FUNC_asym_cipher_decrypt_init_fn rsa_decrypt_init; |
363b1e5d DMSP |
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; | |
89abd1b6 | 47 | |
31a796d1 | 48 | static OSSL_ITEM padding_item[] = { |
b8086652 SL |
49 | { RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 }, |
50 | { RSA_SSLV23_PADDING, OSSL_PKEY_RSA_PAD_MODE_SSLV23 }, | |
51 | { RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE }, | |
52 | { RSA_PKCS1_OAEP_PADDING, OSSL_PKEY_RSA_PAD_MODE_OAEP }, /* Correct spelling first */ | |
31a796d1 | 53 | { RSA_PKCS1_OAEP_PADDING, "oeap" }, |
b8086652 | 54 | { RSA_X931_PADDING, OSSL_PKEY_RSA_PAD_MODE_X931 }, |
31a796d1 RL |
55 | { 0, NULL } |
56 | }; | |
89abd1b6 MC |
57 | |
58 | /* | |
59 | * What's passed as an actual key is defined by the KEYMGMT interface. | |
60 | * We happen to know that our KEYMGMT simply passes RSA structures, so | |
61 | * we use that here too. | |
62 | */ | |
63 | ||
64 | typedef struct { | |
65 | OPENSSL_CTX *libctx; | |
66 | RSA *rsa; | |
67 | int pad_mode; | |
8d17cca5 | 68 | int operation; |
89abd1b6 MC |
69 | /* OAEP message digest */ |
70 | EVP_MD *oaep_md; | |
71 | /* message digest for MGF1 */ | |
72 | EVP_MD *mgf1_md; | |
73 | /* OAEP label */ | |
74 | unsigned char *oaep_label; | |
75 | size_t oaep_labellen; | |
d9a75107 MC |
76 | /* TLS padding */ |
77 | unsigned int client_version; | |
78 | unsigned int alt_version; | |
89abd1b6 MC |
79 | } PROV_RSA_CTX; |
80 | ||
81 | static void *rsa_newctx(void *provctx) | |
82 | { | |
87fe138d | 83 | PROV_RSA_CTX *prsactx; |
89abd1b6 | 84 | |
87fe138d P |
85 | if (!ossl_prov_is_running()) |
86 | return NULL; | |
87 | prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX)); | |
89abd1b6 MC |
88 | if (prsactx == NULL) |
89 | return NULL; | |
90 | prsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx); | |
91 | ||
92 | return prsactx; | |
93 | } | |
94 | ||
8d17cca5 | 95 | static int rsa_init(void *vprsactx, void *vrsa, int operation) |
89abd1b6 MC |
96 | { |
97 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
98 | ||
87fe138d P |
99 | if (!ossl_prov_is_running() |
100 | || prsactx == NULL | |
101 | || vrsa == NULL | |
102 | || !RSA_up_ref(vrsa)) | |
89abd1b6 MC |
103 | return 0; |
104 | RSA_free(prsactx->rsa); | |
105 | prsactx->rsa = vrsa; | |
8d17cca5 | 106 | prsactx->operation = operation; |
106ec30b RL |
107 | |
108 | switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) { | |
109 | case RSA_FLAG_TYPE_RSA: | |
110 | prsactx->pad_mode = RSA_PKCS1_PADDING; | |
111 | break; | |
112 | default: | |
113 | ERR_raise(ERR_LIB_PROV, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
114 | return 0; | |
115 | } | |
8d17cca5 SL |
116 | if (!rsa_check_key(vrsa, operation == EVP_PKEY_OP_ENCRYPT)) { |
117 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); | |
118 | return 0; | |
119 | } | |
89abd1b6 MC |
120 | return 1; |
121 | } | |
122 | ||
8d17cca5 SL |
123 | static int rsa_encrypt_init(void *vprsactx, void *vrsa) |
124 | { | |
125 | return rsa_init(vprsactx, vrsa, EVP_PKEY_OP_ENCRYPT); | |
126 | } | |
127 | ||
128 | static int rsa_decrypt_init(void *vprsactx, void *vrsa) | |
129 | { | |
130 | return rsa_init(vprsactx, vrsa, EVP_PKEY_OP_DECRYPT); | |
131 | } | |
132 | ||
89abd1b6 MC |
133 | static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen, |
134 | size_t outsize, const unsigned char *in, size_t inlen) | |
135 | { | |
136 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
137 | int ret; | |
138 | ||
87fe138d P |
139 | if (!ossl_prov_is_running()) |
140 | return 0; | |
141 | ||
89abd1b6 MC |
142 | if (out == NULL) { |
143 | size_t len = RSA_size(prsactx->rsa); | |
144 | ||
145 | if (len == 0) { | |
146 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); | |
147 | return 0; | |
148 | } | |
149 | *outlen = len; | |
150 | return 1; | |
151 | } | |
152 | ||
153 | if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) { | |
154 | int rsasize = RSA_size(prsactx->rsa); | |
155 | unsigned char *tbuf; | |
156 | ||
157 | if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) { | |
158 | PROVerr(0, ERR_R_MALLOC_FAILURE); | |
159 | return 0; | |
160 | } | |
afb638f1 | 161 | if (prsactx->oaep_md == NULL) { |
5472821e | 162 | OPENSSL_free(tbuf); |
afb638f1 MC |
163 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL); |
164 | PROVerr(0, ERR_R_INTERNAL_ERROR); | |
165 | return 0; | |
166 | } | |
f7f53d7d RL |
167 | ret = |
168 | rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(prsactx->libctx, tbuf, | |
169 | rsasize, in, inlen, | |
170 | prsactx->oaep_label, | |
171 | prsactx->oaep_labellen, | |
172 | prsactx->oaep_md, | |
173 | prsactx->mgf1_md); | |
89abd1b6 MC |
174 | |
175 | if (!ret) { | |
176 | OPENSSL_free(tbuf); | |
177 | return 0; | |
178 | } | |
179 | ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa, | |
180 | RSA_NO_PADDING); | |
181 | OPENSSL_free(tbuf); | |
182 | } else { | |
183 | ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa, | |
184 | prsactx->pad_mode); | |
185 | } | |
186 | /* A ret value of 0 is not an error */ | |
187 | if (ret < 0) | |
188 | return ret; | |
189 | *outlen = ret; | |
190 | return 1; | |
191 | } | |
192 | ||
193 | static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen, | |
194 | size_t outsize, const unsigned char *in, size_t inlen) | |
195 | { | |
196 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
197 | int ret; | |
d9a75107 | 198 | size_t len = RSA_size(prsactx->rsa); |
89abd1b6 | 199 | |
87fe138d P |
200 | if (!ossl_prov_is_running()) |
201 | return 0; | |
202 | ||
d9a75107 MC |
203 | if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) { |
204 | if (out == NULL) { | |
205 | *outlen = SSL_MAX_MASTER_KEY_LENGTH; | |
206 | return 1; | |
207 | } | |
208 | if (outsize < SSL_MAX_MASTER_KEY_LENGTH) { | |
209 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); | |
210 | return 0; | |
211 | } | |
212 | } else { | |
213 | if (out == NULL) { | |
214 | if (len == 0) { | |
215 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); | |
216 | return 0; | |
217 | } | |
218 | *outlen = len; | |
219 | return 1; | |
220 | } | |
89abd1b6 | 221 | |
d9a75107 MC |
222 | if (outsize < len) { |
223 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); | |
89abd1b6 MC |
224 | return 0; |
225 | } | |
89abd1b6 MC |
226 | } |
227 | ||
d9a75107 MC |
228 | if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING |
229 | || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) { | |
89abd1b6 MC |
230 | unsigned char *tbuf; |
231 | ||
d9a75107 | 232 | if ((tbuf = OPENSSL_malloc(len)) == NULL) { |
89abd1b6 MC |
233 | PROVerr(0, ERR_R_MALLOC_FAILURE); |
234 | return 0; | |
235 | } | |
236 | ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa, | |
237 | RSA_NO_PADDING); | |
d9a75107 MC |
238 | /* |
239 | * With no padding then, on success ret should be len, otherwise an | |
240 | * error occurred (non-constant time) | |
241 | */ | |
242 | if (ret != (int)len) { | |
89abd1b6 | 243 | OPENSSL_free(tbuf); |
d9a75107 | 244 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT); |
89abd1b6 MC |
245 | return 0; |
246 | } | |
d9a75107 | 247 | if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
afb638f1 MC |
248 | if (prsactx->oaep_md == NULL) { |
249 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL); | |
250 | if (prsactx->oaep_md == NULL) { | |
251 | PROVerr(0, ERR_R_INTERNAL_ERROR); | |
252 | return 0; | |
253 | } | |
254 | } | |
d9a75107 MC |
255 | ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf, |
256 | len, len, | |
257 | prsactx->oaep_label, | |
258 | prsactx->oaep_labellen, | |
259 | prsactx->oaep_md, | |
260 | prsactx->mgf1_md); | |
261 | } else { | |
262 | /* RSA_PKCS1_WITH_TLS_PADDING */ | |
263 | if (prsactx->client_version <= 0) { | |
264 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION); | |
265 | return 0; | |
266 | } | |
0f2deef5 MC |
267 | ret = rsa_padding_check_PKCS1_type_2_TLS(prsactx->libctx, out, |
268 | outsize, | |
d9a75107 MC |
269 | tbuf, len, |
270 | prsactx->client_version, | |
271 | prsactx->alt_version); | |
272 | } | |
89abd1b6 MC |
273 | OPENSSL_free(tbuf); |
274 | } else { | |
275 | ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, | |
276 | prsactx->pad_mode); | |
277 | } | |
278 | *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret); | |
279 | ret = constant_time_select_int(constant_time_msb(ret), 0, 1); | |
280 | return ret; | |
281 | } | |
282 | ||
283 | static void rsa_freectx(void *vprsactx) | |
284 | { | |
285 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
286 | ||
287 | RSA_free(prsactx->rsa); | |
288 | ||
289 | EVP_MD_free(prsactx->oaep_md); | |
290 | EVP_MD_free(prsactx->mgf1_md); | |
5606922c | 291 | OPENSSL_free(prsactx->oaep_label); |
89abd1b6 MC |
292 | |
293 | OPENSSL_free(prsactx); | |
294 | } | |
295 | ||
296 | static void *rsa_dupctx(void *vprsactx) | |
297 | { | |
298 | PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx; | |
299 | PROV_RSA_CTX *dstctx; | |
300 | ||
87fe138d P |
301 | if (!ossl_prov_is_running()) |
302 | return NULL; | |
303 | ||
89abd1b6 MC |
304 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
305 | if (dstctx == NULL) | |
306 | return NULL; | |
307 | ||
308 | *dstctx = *srcctx; | |
309 | if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) { | |
310 | OPENSSL_free(dstctx); | |
311 | return NULL; | |
312 | } | |
313 | ||
314 | if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) { | |
315 | RSA_free(dstctx->rsa); | |
316 | OPENSSL_free(dstctx); | |
317 | return NULL; | |
318 | } | |
319 | ||
320 | if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) { | |
321 | RSA_free(dstctx->rsa); | |
322 | EVP_MD_free(dstctx->oaep_md); | |
323 | OPENSSL_free(dstctx); | |
324 | return NULL; | |
325 | } | |
326 | ||
327 | return dstctx; | |
328 | } | |
329 | ||
330 | static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params) | |
331 | { | |
332 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
333 | OSSL_PARAM *p; | |
334 | ||
335 | if (prsactx == NULL || params == NULL) | |
336 | return 0; | |
337 | ||
338 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE); | |
31a796d1 RL |
339 | if (p != NULL) |
340 | switch (p->data_type) { | |
341 | case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */ | |
342 | if (!OSSL_PARAM_set_int(p, prsactx->pad_mode)) | |
343 | return 0; | |
344 | break; | |
345 | case OSSL_PARAM_UTF8_STRING: | |
346 | { | |
347 | int i; | |
348 | const char *word = NULL; | |
349 | ||
350 | for (i = 0; padding_item[i].id != 0; i++) { | |
351 | if (prsactx->pad_mode == (int)padding_item[i].id) { | |
352 | word = padding_item[i].ptr; | |
353 | break; | |
354 | } | |
355 | } | |
356 | ||
357 | if (word != NULL) { | |
358 | if (!OSSL_PARAM_set_utf8_string(p, word)) | |
359 | return 0; | |
360 | } else { | |
361 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); | |
362 | } | |
363 | } | |
364 | break; | |
365 | default: | |
366 | return 0; | |
367 | } | |
89abd1b6 MC |
368 | |
369 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST); | |
370 | if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL | |
371 | ? "" | |
372 | : EVP_MD_name(prsactx->oaep_md))) | |
373 | return 0; | |
374 | ||
375 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST); | |
376 | if (p != NULL) { | |
377 | EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md | |
378 | : prsactx->mgf1_md; | |
379 | ||
380 | if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL | |
381 | ? "" | |
382 | : EVP_MD_name(mgf1_md))) | |
383 | return 0; | |
384 | } | |
385 | ||
386 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL); | |
387 | if (p != NULL && !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, 0)) | |
388 | return 0; | |
389 | ||
390 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN); | |
391 | if (p != NULL && !OSSL_PARAM_set_size_t(p, prsactx->oaep_labellen)) | |
392 | return 0; | |
393 | ||
d9a75107 MC |
394 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION); |
395 | if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version)) | |
396 | return 0; | |
397 | ||
398 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION); | |
399 | if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version)) | |
400 | return 0; | |
401 | ||
89abd1b6 MC |
402 | return 1; |
403 | } | |
404 | ||
405 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
406 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0), | |
31a796d1 | 407 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0), |
89abd1b6 MC |
408 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0), |
409 | OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR, | |
410 | NULL, 0), | |
411 | OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL), | |
d9a75107 MC |
412 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL), |
413 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL), | |
89abd1b6 MC |
414 | OSSL_PARAM_END |
415 | }; | |
416 | ||
1017ab21 | 417 | static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *provctx) |
89abd1b6 MC |
418 | { |
419 | return known_gettable_ctx_params; | |
420 | } | |
421 | ||
422 | static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) | |
423 | { | |
424 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
425 | const OSSL_PARAM *p; | |
c24937d5 RL |
426 | char mdname[OSSL_MAX_NAME_SIZE]; |
427 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' }; | |
89abd1b6 | 428 | char *str = mdname; |
89abd1b6 MC |
429 | |
430 | if (prsactx == NULL || params == NULL) | |
431 | return 0; | |
432 | ||
433 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST); | |
434 | if (p != NULL) { | |
435 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname))) | |
436 | return 0; | |
437 | ||
438 | str = mdprops; | |
439 | p = OSSL_PARAM_locate_const(params, | |
440 | OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS); | |
441 | if (p != NULL) { | |
442 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops))) | |
443 | return 0; | |
444 | } | |
445 | ||
446 | EVP_MD_free(prsactx->oaep_md); | |
447 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops); | |
448 | ||
449 | if (prsactx->oaep_md == NULL) | |
450 | return 0; | |
451 | } | |
452 | ||
453 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE); | |
454 | if (p != NULL) { | |
31a796d1 RL |
455 | int pad_mode = 0; |
456 | ||
457 | switch (p->data_type) { | |
458 | case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */ | |
459 | if (!OSSL_PARAM_get_int(p, &pad_mode)) | |
460 | return 0; | |
461 | break; | |
462 | case OSSL_PARAM_UTF8_STRING: | |
463 | { | |
464 | int i; | |
465 | ||
466 | if (p->data == NULL) | |
467 | return 0; | |
468 | ||
469 | for (i = 0; padding_item[i].id != 0; i++) { | |
470 | if (strcmp(p->data, padding_item[i].ptr) == 0) { | |
471 | pad_mode = padding_item[i].id; | |
472 | break; | |
473 | } | |
474 | } | |
475 | } | |
476 | break; | |
477 | default: | |
89abd1b6 | 478 | return 0; |
31a796d1 RL |
479 | } |
480 | ||
89abd1b6 MC |
481 | /* |
482 | * PSS padding is for signatures only so is not compatible with | |
483 | * asymmetric cipher use. | |
484 | */ | |
485 | if (pad_mode == RSA_PKCS1_PSS_PADDING) | |
486 | return 0; | |
487 | if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) { | |
488 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops); | |
489 | if (prsactx->oaep_md == NULL) | |
490 | return 0; | |
491 | } | |
492 | prsactx->pad_mode = pad_mode; | |
493 | } | |
494 | ||
495 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST); | |
496 | if (p != NULL) { | |
497 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname))) | |
498 | return 0; | |
499 | ||
500 | str = mdprops; | |
501 | p = OSSL_PARAM_locate_const(params, | |
502 | OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS); | |
503 | if (p != NULL) { | |
504 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops))) | |
505 | return 0; | |
506 | } else { | |
507 | str = NULL; | |
508 | } | |
509 | ||
510 | EVP_MD_free(prsactx->mgf1_md); | |
511 | prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str); | |
512 | ||
513 | if (prsactx->mgf1_md == NULL) | |
514 | return 0; | |
515 | } | |
516 | ||
517 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL); | |
518 | if (p != NULL) { | |
519 | void *tmp_label = NULL; | |
520 | size_t tmp_labellen; | |
521 | ||
522 | if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen)) | |
523 | return 0; | |
524 | OPENSSL_free(prsactx->oaep_label); | |
525 | prsactx->oaep_label = (unsigned char *)tmp_label; | |
526 | prsactx->oaep_labellen = tmp_labellen; | |
527 | } | |
528 | ||
d9a75107 MC |
529 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION); |
530 | if (p != NULL) { | |
531 | unsigned int client_version; | |
532 | ||
533 | if (!OSSL_PARAM_get_uint(p, &client_version)) | |
534 | return 0; | |
535 | prsactx->client_version = client_version; | |
536 | } | |
537 | ||
538 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION); | |
539 | if (p != NULL) { | |
540 | unsigned int alt_version; | |
541 | ||
542 | if (!OSSL_PARAM_get_uint(p, &alt_version)) | |
543 | return 0; | |
544 | prsactx->alt_version = alt_version; | |
545 | } | |
546 | ||
89abd1b6 MC |
547 | return 1; |
548 | } | |
549 | ||
550 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
551 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0), | |
31a796d1 | 552 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0), |
89abd1b6 MC |
553 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0), |
554 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0), | |
555 | OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0), | |
d9a75107 MC |
556 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL), |
557 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL), | |
89abd1b6 MC |
558 | OSSL_PARAM_END |
559 | }; | |
560 | ||
1017ab21 | 561 | static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *provctx) |
89abd1b6 MC |
562 | { |
563 | return known_settable_ctx_params; | |
564 | } | |
565 | ||
1be63951 | 566 | const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = { |
89abd1b6 | 567 | { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx }, |
8d17cca5 | 568 | { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init }, |
89abd1b6 | 569 | { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt }, |
8d17cca5 | 570 | { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init }, |
89abd1b6 MC |
571 | { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt }, |
572 | { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx }, | |
573 | { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx }, | |
574 | { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS, | |
575 | (void (*)(void))rsa_get_ctx_params }, | |
576 | { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS, | |
577 | (void (*)(void))rsa_gettable_ctx_params }, | |
578 | { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS, | |
579 | (void (*)(void))rsa_set_ctx_params }, | |
580 | { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS, | |
581 | (void (*)(void))rsa_settable_ctx_params }, | |
582 | { 0, NULL } | |
583 | }; |