2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/crypto.h>
11 #include <openssl/evp.h>
12 #include <openssl/core_numbers.h>
13 #include <openssl/core_names.h>
14 #include <openssl/rsa.h>
15 #include <openssl/params.h>
16 #include <openssl/err.h>
17 /* Just for SSL_MAX_MASTER_KEY_LENGTH */
18 #include <openssl/ssl.h>
19 #include "internal/constant_time.h"
20 #include "internal/sizes.h"
21 #include "crypto/rsa.h"
22 #include "prov/providercommonerr.h"
23 #include "prov/provider_ctx.h"
24 #include "prov/implementations.h"
28 static OSSL_OP_asym_cipher_newctx_fn rsa_newctx
;
29 static OSSL_OP_asym_cipher_encrypt_init_fn rsa_init
;
30 static OSSL_OP_asym_cipher_encrypt_fn rsa_encrypt
;
31 static OSSL_OP_asym_cipher_decrypt_init_fn rsa_init
;
32 static OSSL_OP_asym_cipher_decrypt_fn rsa_decrypt
;
33 static OSSL_OP_asym_cipher_freectx_fn rsa_freectx
;
34 static OSSL_OP_asym_cipher_dupctx_fn rsa_dupctx
;
35 static OSSL_OP_asym_cipher_get_ctx_params_fn rsa_get_ctx_params
;
36 static OSSL_OP_asym_cipher_gettable_ctx_params_fn rsa_gettable_ctx_params
;
37 static OSSL_OP_asym_cipher_set_ctx_params_fn rsa_set_ctx_params
;
38 static OSSL_OP_asym_cipher_settable_ctx_params_fn rsa_settable_ctx_params
;
42 * What's passed as an actual key is defined by the KEYMGMT interface.
43 * We happen to know that our KEYMGMT simply passes RSA structures, so
44 * we use that here too.
51 /* OAEP message digest */
53 /* message digest for MGF1 */
56 unsigned char *oaep_label
;
59 unsigned int client_version
;
60 unsigned int alt_version
;
63 static void *rsa_newctx(void *provctx
)
65 PROV_RSA_CTX
*prsactx
= OPENSSL_zalloc(sizeof(PROV_RSA_CTX
));
69 prsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
74 static int rsa_init(void *vprsactx
, void *vrsa
)
76 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
78 if (prsactx
== NULL
|| vrsa
== NULL
|| !RSA_up_ref(vrsa
))
80 RSA_free(prsactx
->rsa
);
82 prsactx
->pad_mode
= RSA_PKCS1_PADDING
;
86 static int rsa_encrypt(void *vprsactx
, unsigned char *out
, size_t *outlen
,
87 size_t outsize
, const unsigned char *in
, size_t inlen
)
89 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
93 size_t len
= RSA_size(prsactx
->rsa
);
96 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
103 if (prsactx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
104 int rsasize
= RSA_size(prsactx
->rsa
);
107 if ((tbuf
= OPENSSL_malloc(rsasize
)) == NULL
) {
108 PROVerr(0, ERR_R_MALLOC_FAILURE
);
111 ret
= RSA_padding_add_PKCS1_OAEP_mgf1(tbuf
, rsasize
, in
, inlen
,
113 prsactx
->oaep_labellen
,
121 ret
= RSA_public_encrypt(rsasize
, tbuf
, out
, prsactx
->rsa
,
125 ret
= RSA_public_encrypt(inlen
, in
, out
, prsactx
->rsa
,
128 /* A ret value of 0 is not an error */
135 static int rsa_decrypt(void *vprsactx
, unsigned char *out
, size_t *outlen
,
136 size_t outsize
, const unsigned char *in
, size_t inlen
)
138 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
140 size_t len
= RSA_size(prsactx
->rsa
);
142 if (prsactx
->pad_mode
== RSA_PKCS1_WITH_TLS_PADDING
) {
144 *outlen
= SSL_MAX_MASTER_KEY_LENGTH
;
147 if (outsize
< SSL_MAX_MASTER_KEY_LENGTH
) {
148 ERR_raise(ERR_LIB_PROV
, PROV_R_BAD_LENGTH
);
154 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
162 ERR_raise(ERR_LIB_PROV
, PROV_R_BAD_LENGTH
);
167 if (prsactx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
168 || prsactx
->pad_mode
== RSA_PKCS1_WITH_TLS_PADDING
) {
171 if ((tbuf
= OPENSSL_malloc(len
)) == NULL
) {
172 PROVerr(0, ERR_R_MALLOC_FAILURE
);
175 ret
= RSA_private_decrypt(inlen
, in
, tbuf
, prsactx
->rsa
,
178 * With no padding then, on success ret should be len, otherwise an
179 * error occurred (non-constant time)
181 if (ret
!= (int)len
) {
183 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_TO_DECRYPT
);
186 if (prsactx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
187 ret
= RSA_padding_check_PKCS1_OAEP_mgf1(out
, outsize
, tbuf
,
190 prsactx
->oaep_labellen
,
194 /* RSA_PKCS1_WITH_TLS_PADDING */
195 if (prsactx
->client_version
<= 0) {
196 ERR_raise(ERR_LIB_PROV
, PROV_R_BAD_TLS_CLIENT_VERSION
);
199 ret
= rsa_padding_check_PKCS1_type_2_TLS(out
, outsize
,
201 prsactx
->client_version
,
202 prsactx
->alt_version
);
206 ret
= RSA_private_decrypt(inlen
, in
, out
, prsactx
->rsa
,
209 *outlen
= constant_time_select_s(constant_time_msb_s(ret
), *outlen
, ret
);
210 ret
= constant_time_select_int(constant_time_msb(ret
), 0, 1);
214 static void rsa_freectx(void *vprsactx
)
216 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
218 RSA_free(prsactx
->rsa
);
220 EVP_MD_free(prsactx
->oaep_md
);
221 EVP_MD_free(prsactx
->mgf1_md
);
223 OPENSSL_free(prsactx
);
226 static void *rsa_dupctx(void *vprsactx
)
228 PROV_RSA_CTX
*srcctx
= (PROV_RSA_CTX
*)vprsactx
;
229 PROV_RSA_CTX
*dstctx
;
231 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
236 if (dstctx
->rsa
!= NULL
&& !RSA_up_ref(dstctx
->rsa
)) {
237 OPENSSL_free(dstctx
);
241 if (dstctx
->oaep_md
!= NULL
&& !EVP_MD_up_ref(dstctx
->oaep_md
)) {
242 RSA_free(dstctx
->rsa
);
243 OPENSSL_free(dstctx
);
247 if (dstctx
->mgf1_md
!= NULL
&& !EVP_MD_up_ref(dstctx
->mgf1_md
)) {
248 RSA_free(dstctx
->rsa
);
249 EVP_MD_free(dstctx
->oaep_md
);
250 OPENSSL_free(dstctx
);
257 static int rsa_get_ctx_params(void *vprsactx
, OSSL_PARAM
*params
)
259 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
262 if (prsactx
== NULL
|| params
== NULL
)
265 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_PAD_MODE
);
266 if (p
!= NULL
&& !OSSL_PARAM_set_int(p
, prsactx
->pad_mode
))
269 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
);
270 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->oaep_md
== NULL
272 : EVP_MD_name(prsactx
->oaep_md
)))
275 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
);
277 EVP_MD
*mgf1_md
= prsactx
->mgf1_md
== NULL
? prsactx
->oaep_md
280 if (!OSSL_PARAM_set_utf8_string(p
, mgf1_md
== NULL
282 : EVP_MD_name(mgf1_md
)))
286 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
);
287 if (p
!= NULL
&& !OSSL_PARAM_set_octet_ptr(p
, prsactx
->oaep_label
, 0))
290 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN
);
291 if (p
!= NULL
&& !OSSL_PARAM_set_size_t(p
, prsactx
->oaep_labellen
))
294 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
);
295 if (p
!= NULL
&& !OSSL_PARAM_set_uint(p
, prsactx
->client_version
))
298 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
);
299 if (p
!= NULL
&& !OSSL_PARAM_set_uint(p
, prsactx
->alt_version
))
305 static const OSSL_PARAM known_gettable_ctx_params
[] = {
306 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
, NULL
, 0),
307 OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE
, NULL
),
308 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
, NULL
, 0),
309 OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
, OSSL_PARAM_OCTET_PTR
,
311 OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN
, NULL
),
312 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
, NULL
),
313 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
, NULL
),
317 static const OSSL_PARAM
*rsa_gettable_ctx_params(void)
319 return known_gettable_ctx_params
;
322 static int rsa_set_ctx_params(void *vprsactx
, const OSSL_PARAM params
[])
324 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
326 char mdname
[OSSL_MAX_NAME_SIZE
];
327 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = { '\0' };
331 if (prsactx
== NULL
|| params
== NULL
)
334 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
);
336 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdname
)))
340 p
= OSSL_PARAM_locate_const(params
,
341 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS
);
343 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdprops
)))
347 EVP_MD_free(prsactx
->oaep_md
);
348 prsactx
->oaep_md
= EVP_MD_fetch(prsactx
->libctx
, mdname
, mdprops
);
350 if (prsactx
->oaep_md
== NULL
)
354 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_PAD_MODE
);
356 if (!OSSL_PARAM_get_int(p
, &pad_mode
))
359 * PSS padding is for signatures only so is not compatible with
360 * asymmetric cipher use.
362 if (pad_mode
== RSA_PKCS1_PSS_PADDING
)
364 if (pad_mode
== RSA_PKCS1_OAEP_PADDING
&& prsactx
->oaep_md
== NULL
) {
365 prsactx
->oaep_md
= EVP_MD_fetch(prsactx
->libctx
, "SHA1", mdprops
);
366 if (prsactx
->oaep_md
== NULL
)
369 prsactx
->pad_mode
= pad_mode
;
372 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
);
374 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdname
)))
378 p
= OSSL_PARAM_locate_const(params
,
379 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS
);
381 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdprops
)))
387 EVP_MD_free(prsactx
->mgf1_md
);
388 prsactx
->mgf1_md
= EVP_MD_fetch(prsactx
->libctx
, mdname
, str
);
390 if (prsactx
->mgf1_md
== NULL
)
394 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
);
396 void *tmp_label
= NULL
;
399 if (!OSSL_PARAM_get_octet_string(p
, &tmp_label
, 0, &tmp_labellen
))
401 OPENSSL_free(prsactx
->oaep_label
);
402 prsactx
->oaep_label
= (unsigned char *)tmp_label
;
403 prsactx
->oaep_labellen
= tmp_labellen
;
406 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
);
408 unsigned int client_version
;
410 if (!OSSL_PARAM_get_uint(p
, &client_version
))
412 prsactx
->client_version
= client_version
;
415 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
);
417 unsigned int alt_version
;
419 if (!OSSL_PARAM_get_uint(p
, &alt_version
))
421 prsactx
->alt_version
= alt_version
;
427 static const OSSL_PARAM known_settable_ctx_params
[] = {
428 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
, NULL
, 0),
429 OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE
, NULL
),
430 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
, NULL
, 0),
431 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS
, NULL
, 0),
432 OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
, NULL
, 0),
433 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
, NULL
),
434 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
, NULL
),
438 static const OSSL_PARAM
*rsa_settable_ctx_params(void)
440 return known_settable_ctx_params
;
443 const OSSL_DISPATCH rsa_asym_cipher_functions
[] = {
444 { OSSL_FUNC_ASYM_CIPHER_NEWCTX
, (void (*)(void))rsa_newctx
},
445 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT
, (void (*)(void))rsa_init
},
446 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT
, (void (*)(void))rsa_encrypt
},
447 { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT
, (void (*)(void))rsa_init
},
448 { OSSL_FUNC_ASYM_CIPHER_DECRYPT
, (void (*)(void))rsa_decrypt
},
449 { OSSL_FUNC_ASYM_CIPHER_FREECTX
, (void (*)(void))rsa_freectx
},
450 { OSSL_FUNC_ASYM_CIPHER_DUPCTX
, (void (*)(void))rsa_dupctx
},
451 { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS
,
452 (void (*)(void))rsa_get_ctx_params
},
453 { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS
,
454 (void (*)(void))rsa_gettable_ctx_params
},
455 { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS
,
456 (void (*)(void))rsa_set_ctx_params
},
457 { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS
,
458 (void (*)(void))rsa_settable_ctx_params
},