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