]>
Commit | Line | Data |
---|---|---|
89abd1b6 | 1 | /* |
da1c088f | 2 | * Copyright 2019-2023 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 MC |
164 | if (prsactx->oaep_md == NULL) { |
165 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL); | |
c5aa7195 DB |
166 | if (prsactx->oaep_md == NULL) { |
167 | OPENSSL_free(tbuf); | |
168 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); | |
169 | return 0; | |
170 | } | |
afb638f1 | 171 | } |
f7f53d7d | 172 | ret = |
23b2fc0b P |
173 | ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(prsactx->libctx, tbuf, |
174 | rsasize, in, inlen, | |
175 | prsactx->oaep_label, | |
176 | prsactx->oaep_labellen, | |
177 | prsactx->oaep_md, | |
178 | prsactx->mgf1_md); | |
89abd1b6 MC |
179 | |
180 | if (!ret) { | |
181 | OPENSSL_free(tbuf); | |
182 | return 0; | |
183 | } | |
184 | ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa, | |
185 | RSA_NO_PADDING); | |
186 | OPENSSL_free(tbuf); | |
187 | } else { | |
188 | ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa, | |
189 | prsactx->pad_mode); | |
190 | } | |
191 | /* A ret value of 0 is not an error */ | |
192 | if (ret < 0) | |
193 | return ret; | |
194 | *outlen = ret; | |
195 | return 1; | |
196 | } | |
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 | { | |
201 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
202 | int ret; | |
5ab3ec1b | 203 | int pad_mode; |
d9a75107 | 204 | size_t len = RSA_size(prsactx->rsa); |
89abd1b6 | 205 | |
87fe138d P |
206 | if (!ossl_prov_is_running()) |
207 | return 0; | |
208 | ||
d9a75107 MC |
209 | if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) { |
210 | if (out == NULL) { | |
211 | *outlen = SSL_MAX_MASTER_KEY_LENGTH; | |
212 | return 1; | |
213 | } | |
214 | if (outsize < SSL_MAX_MASTER_KEY_LENGTH) { | |
215 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); | |
216 | return 0; | |
217 | } | |
218 | } else { | |
219 | if (out == NULL) { | |
220 | if (len == 0) { | |
221 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); | |
222 | return 0; | |
223 | } | |
224 | *outlen = len; | |
225 | return 1; | |
226 | } | |
89abd1b6 | 227 | |
d9a75107 MC |
228 | if (outsize < len) { |
229 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); | |
89abd1b6 MC |
230 | return 0; |
231 | } | |
89abd1b6 MC |
232 | } |
233 | ||
d9a75107 MC |
234 | if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING |
235 | || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) { | |
89abd1b6 MC |
236 | unsigned char *tbuf; |
237 | ||
e077455e | 238 | if ((tbuf = OPENSSL_malloc(len)) == NULL) |
89abd1b6 | 239 | return 0; |
89abd1b6 MC |
240 | ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa, |
241 | RSA_NO_PADDING); | |
d9a75107 MC |
242 | /* |
243 | * With no padding then, on success ret should be len, otherwise an | |
244 | * error occurred (non-constant time) | |
245 | */ | |
246 | if (ret != (int)len) { | |
89abd1b6 | 247 | OPENSSL_free(tbuf); |
d9a75107 | 248 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT); |
89abd1b6 MC |
249 | return 0; |
250 | } | |
d9a75107 | 251 | if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) { |
afb638f1 MC |
252 | if (prsactx->oaep_md == NULL) { |
253 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL); | |
254 | if (prsactx->oaep_md == NULL) { | |
bca681aa | 255 | OPENSSL_free(tbuf); |
6debc6ab | 256 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
afb638f1 MC |
257 | return 0; |
258 | } | |
259 | } | |
d9a75107 MC |
260 | ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf, |
261 | len, len, | |
262 | prsactx->oaep_label, | |
263 | prsactx->oaep_labellen, | |
264 | prsactx->oaep_md, | |
265 | prsactx->mgf1_md); | |
266 | } else { | |
267 | /* RSA_PKCS1_WITH_TLS_PADDING */ | |
268 | if (prsactx->client_version <= 0) { | |
269 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION); | |
bca681aa | 270 | OPENSSL_free(tbuf); |
d9a75107 MC |
271 | return 0; |
272 | } | |
23b2fc0b P |
273 | ret = ossl_rsa_padding_check_PKCS1_type_2_TLS( |
274 | prsactx->libctx, out, outsize, tbuf, len, | |
275 | prsactx->client_version, prsactx->alt_version); | |
d9a75107 | 276 | } |
89abd1b6 MC |
277 | OPENSSL_free(tbuf); |
278 | } else { | |
5ab3ec1b HK |
279 | if ((prsactx->implicit_rejection == 0) && |
280 | (prsactx->pad_mode == RSA_PKCS1_PADDING)) | |
281 | pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING; | |
282 | else | |
283 | pad_mode = prsactx->pad_mode; | |
284 | ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, pad_mode); | |
89abd1b6 MC |
285 | } |
286 | *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret); | |
287 | ret = constant_time_select_int(constant_time_msb(ret), 0, 1); | |
288 | return ret; | |
289 | } | |
290 | ||
291 | static void rsa_freectx(void *vprsactx) | |
292 | { | |
293 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
294 | ||
295 | RSA_free(prsactx->rsa); | |
296 | ||
297 | EVP_MD_free(prsactx->oaep_md); | |
298 | EVP_MD_free(prsactx->mgf1_md); | |
5606922c | 299 | OPENSSL_free(prsactx->oaep_label); |
89abd1b6 MC |
300 | |
301 | OPENSSL_free(prsactx); | |
302 | } | |
303 | ||
304 | static void *rsa_dupctx(void *vprsactx) | |
305 | { | |
306 | PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx; | |
307 | PROV_RSA_CTX *dstctx; | |
308 | ||
87fe138d P |
309 | if (!ossl_prov_is_running()) |
310 | return NULL; | |
311 | ||
89abd1b6 MC |
312 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
313 | if (dstctx == NULL) | |
314 | return NULL; | |
315 | ||
316 | *dstctx = *srcctx; | |
317 | if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) { | |
318 | OPENSSL_free(dstctx); | |
319 | return NULL; | |
320 | } | |
321 | ||
322 | if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) { | |
323 | RSA_free(dstctx->rsa); | |
324 | OPENSSL_free(dstctx); | |
325 | return NULL; | |
326 | } | |
327 | ||
328 | if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) { | |
329 | RSA_free(dstctx->rsa); | |
330 | EVP_MD_free(dstctx->oaep_md); | |
331 | OPENSSL_free(dstctx); | |
332 | return NULL; | |
333 | } | |
334 | ||
335 | return dstctx; | |
336 | } | |
337 | ||
338 | static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params) | |
339 | { | |
340 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
341 | OSSL_PARAM *p; | |
342 | ||
cbdeb04c | 343 | if (prsactx == NULL) |
89abd1b6 MC |
344 | return 0; |
345 | ||
346 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE); | |
31a796d1 RL |
347 | if (p != NULL) |
348 | switch (p->data_type) { | |
349 | case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */ | |
350 | if (!OSSL_PARAM_set_int(p, prsactx->pad_mode)) | |
351 | return 0; | |
352 | break; | |
353 | case OSSL_PARAM_UTF8_STRING: | |
354 | { | |
355 | int i; | |
356 | const char *word = NULL; | |
357 | ||
358 | for (i = 0; padding_item[i].id != 0; i++) { | |
359 | if (prsactx->pad_mode == (int)padding_item[i].id) { | |
360 | word = padding_item[i].ptr; | |
361 | break; | |
362 | } | |
363 | } | |
364 | ||
365 | if (word != NULL) { | |
366 | if (!OSSL_PARAM_set_utf8_string(p, word)) | |
367 | return 0; | |
368 | } else { | |
369 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); | |
370 | } | |
371 | } | |
372 | break; | |
373 | default: | |
374 | return 0; | |
375 | } | |
89abd1b6 MC |
376 | |
377 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST); | |
378 | if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL | |
379 | ? "" | |
ed576acd | 380 | : EVP_MD_get0_name(prsactx->oaep_md))) |
89abd1b6 MC |
381 | return 0; |
382 | ||
383 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST); | |
384 | if (p != NULL) { | |
385 | EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md | |
386 | : prsactx->mgf1_md; | |
387 | ||
388 | if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL | |
389 | ? "" | |
ed576acd | 390 | : EVP_MD_get0_name(mgf1_md))) |
89abd1b6 MC |
391 | return 0; |
392 | } | |
393 | ||
394 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL); | |
ba0a6d1d RL |
395 | if (p != NULL && |
396 | !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, | |
397 | prsactx->oaep_labellen)) | |
89abd1b6 MC |
398 | return 0; |
399 | ||
d9a75107 MC |
400 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION); |
401 | if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version)) | |
402 | return 0; | |
403 | ||
404 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION); | |
405 | if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version)) | |
406 | return 0; | |
407 | ||
5ab3ec1b HK |
408 | p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION); |
409 | if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->implicit_rejection)) | |
410 | return 0; | |
411 | ||
89abd1b6 MC |
412 | return 1; |
413 | } | |
414 | ||
415 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
416 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0), | |
31a796d1 | 417 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0), |
89abd1b6 MC |
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), | |
d9a75107 MC |
421 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL), |
422 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL), | |
5ab3ec1b | 423 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL), |
89abd1b6 MC |
424 | OSSL_PARAM_END |
425 | }; | |
426 | ||
fb67126e TM |
427 | static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx, |
428 | ossl_unused void *provctx) | |
89abd1b6 MC |
429 | { |
430 | return known_gettable_ctx_params; | |
431 | } | |
432 | ||
433 | static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) | |
434 | { | |
435 | PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; | |
436 | const OSSL_PARAM *p; | |
c24937d5 RL |
437 | char mdname[OSSL_MAX_NAME_SIZE]; |
438 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' }; | |
e5a7536e | 439 | char *str = NULL; |
89abd1b6 | 440 | |
cbdeb04c | 441 | if (prsactx == NULL) |
89abd1b6 | 442 | return 0; |
cbdeb04c P |
443 | if (params == NULL) |
444 | return 1; | |
89abd1b6 MC |
445 | |
446 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST); | |
447 | if (p != NULL) { | |
e5a7536e | 448 | str = mdname; |
89abd1b6 MC |
449 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname))) |
450 | return 0; | |
451 | ||
89abd1b6 MC |
452 | p = OSSL_PARAM_locate_const(params, |
453 | OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS); | |
454 | if (p != NULL) { | |
e5a7536e | 455 | str = mdprops; |
89abd1b6 MC |
456 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops))) |
457 | return 0; | |
458 | } | |
459 | ||
460 | EVP_MD_free(prsactx->oaep_md); | |
461 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops); | |
462 | ||
463 | if (prsactx->oaep_md == NULL) | |
464 | return 0; | |
465 | } | |
466 | ||
467 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE); | |
468 | if (p != NULL) { | |
31a796d1 RL |
469 | int pad_mode = 0; |
470 | ||
471 | switch (p->data_type) { | |
472 | case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */ | |
473 | if (!OSSL_PARAM_get_int(p, &pad_mode)) | |
474 | return 0; | |
475 | break; | |
476 | case OSSL_PARAM_UTF8_STRING: | |
477 | { | |
478 | int i; | |
479 | ||
480 | if (p->data == NULL) | |
481 | return 0; | |
482 | ||
483 | for (i = 0; padding_item[i].id != 0; i++) { | |
484 | if (strcmp(p->data, padding_item[i].ptr) == 0) { | |
485 | pad_mode = padding_item[i].id; | |
486 | break; | |
487 | } | |
488 | } | |
489 | } | |
490 | break; | |
491 | default: | |
89abd1b6 | 492 | return 0; |
31a796d1 RL |
493 | } |
494 | ||
89abd1b6 MC |
495 | /* |
496 | * PSS padding is for signatures only so is not compatible with | |
497 | * asymmetric cipher use. | |
498 | */ | |
499 | if (pad_mode == RSA_PKCS1_PSS_PADDING) | |
500 | return 0; | |
501 | if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) { | |
502 | prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops); | |
503 | if (prsactx->oaep_md == NULL) | |
504 | return 0; | |
505 | } | |
506 | prsactx->pad_mode = pad_mode; | |
507 | } | |
508 | ||
509 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST); | |
510 | if (p != NULL) { | |
e5a7536e | 511 | str = mdname; |
89abd1b6 MC |
512 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname))) |
513 | return 0; | |
514 | ||
89abd1b6 MC |
515 | p = OSSL_PARAM_locate_const(params, |
516 | OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS); | |
517 | if (p != NULL) { | |
e5a7536e | 518 | str = mdprops; |
89abd1b6 MC |
519 | if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops))) |
520 | return 0; | |
521 | } else { | |
522 | str = NULL; | |
523 | } | |
524 | ||
525 | EVP_MD_free(prsactx->mgf1_md); | |
526 | prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str); | |
527 | ||
528 | if (prsactx->mgf1_md == NULL) | |
529 | return 0; | |
530 | } | |
531 | ||
532 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL); | |
533 | if (p != NULL) { | |
534 | void *tmp_label = NULL; | |
535 | size_t tmp_labellen; | |
536 | ||
537 | if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen)) | |
538 | return 0; | |
539 | OPENSSL_free(prsactx->oaep_label); | |
540 | prsactx->oaep_label = (unsigned char *)tmp_label; | |
541 | prsactx->oaep_labellen = tmp_labellen; | |
542 | } | |
543 | ||
d9a75107 MC |
544 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION); |
545 | if (p != NULL) { | |
546 | unsigned int client_version; | |
547 | ||
548 | if (!OSSL_PARAM_get_uint(p, &client_version)) | |
549 | return 0; | |
550 | prsactx->client_version = client_version; | |
551 | } | |
552 | ||
553 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION); | |
554 | if (p != NULL) { | |
555 | unsigned int alt_version; | |
556 | ||
557 | if (!OSSL_PARAM_get_uint(p, &alt_version)) | |
558 | return 0; | |
559 | prsactx->alt_version = alt_version; | |
560 | } | |
5ab3ec1b HK |
561 | p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION); |
562 | if (p != NULL) { | |
563 | unsigned int implicit_rejection; | |
564 | ||
565 | if (!OSSL_PARAM_get_uint(p, &implicit_rejection)) | |
566 | return 0; | |
567 | prsactx->implicit_rejection = implicit_rejection; | |
568 | } | |
d9a75107 | 569 | |
89abd1b6 MC |
570 | return 1; |
571 | } | |
572 | ||
573 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
574 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0), | |
31a796d1 | 575 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0), |
89abd1b6 MC |
576 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0), |
577 | OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0), | |
578 | OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0), | |
d9a75107 MC |
579 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL), |
580 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL), | |
5ab3ec1b | 581 | OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL), |
89abd1b6 MC |
582 | OSSL_PARAM_END |
583 | }; | |
584 | ||
fb67126e TM |
585 | static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *vprsactx, |
586 | ossl_unused void *provctx) | |
89abd1b6 MC |
587 | { |
588 | return known_settable_ctx_params; | |
589 | } | |
590 | ||
1be63951 | 591 | const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = { |
89abd1b6 | 592 | { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx }, |
8d17cca5 | 593 | { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init }, |
89abd1b6 | 594 | { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt }, |
8d17cca5 | 595 | { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init }, |
89abd1b6 MC |
596 | { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt }, |
597 | { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx }, | |
598 | { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx }, | |
599 | { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS, | |
600 | (void (*)(void))rsa_get_ctx_params }, | |
601 | { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS, | |
602 | (void (*)(void))rsa_gettable_ctx_params }, | |
603 | { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS, | |
604 | (void (*)(void))rsa_set_ctx_params }, | |
605 | { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS, | |
606 | (void (*)(void))rsa_settable_ctx_params }, | |
1e6bd31e | 607 | OSSL_DISPATCH_END |
89abd1b6 | 608 | }; |