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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include <openssl/crypto.h>
17 #include <openssl/evp.h>
18 #include <openssl/core_numbers.h>
19 #include <openssl/core_names.h>
20 #include <openssl/rsa.h>
21 #include <openssl/params.h>
22 #include <openssl/err.h>
23 /* Just for SSL_MAX_MASTER_KEY_LENGTH */
24 #include <openssl/ssl.h>
25 #include "internal/constant_time.h"
26 #include "internal/sizes.h"
27 #include "crypto/rsa.h"
28 #include "prov/providercommonerr.h"
29 #include "prov/provider_ctx.h"
30 #include "prov/implementations.h"
34 static OSSL_OP_asym_cipher_newctx_fn rsa_newctx
;
35 static OSSL_OP_asym_cipher_encrypt_init_fn rsa_init
;
36 static OSSL_OP_asym_cipher_encrypt_fn rsa_encrypt
;
37 static OSSL_OP_asym_cipher_decrypt_init_fn rsa_init
;
38 static OSSL_OP_asym_cipher_decrypt_fn rsa_decrypt
;
39 static OSSL_OP_asym_cipher_freectx_fn rsa_freectx
;
40 static OSSL_OP_asym_cipher_dupctx_fn rsa_dupctx
;
41 static OSSL_OP_asym_cipher_get_ctx_params_fn rsa_get_ctx_params
;
42 static OSSL_OP_asym_cipher_gettable_ctx_params_fn rsa_gettable_ctx_params
;
43 static OSSL_OP_asym_cipher_set_ctx_params_fn rsa_set_ctx_params
;
44 static OSSL_OP_asym_cipher_settable_ctx_params_fn rsa_settable_ctx_params
;
46 static OSSL_ITEM padding_item
[] = {
47 { RSA_PKCS1_PADDING
, "pkcs1" },
48 { RSA_SSLV23_PADDING
, "sslv23" },
49 { RSA_NO_PADDING
, "none" },
50 { RSA_PKCS1_OAEP_PADDING
, "oaep" }, /* Correct spelling first */
51 { RSA_PKCS1_OAEP_PADDING
, "oeap" },
52 { RSA_X931_PADDING
, "x931" },
53 { RSA_PKCS1_PSS_PADDING
, "pss" },
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.
67 /* OAEP message digest */
69 /* message digest for MGF1 */
72 unsigned char *oaep_label
;
75 unsigned int client_version
;
76 unsigned int alt_version
;
79 static void *rsa_newctx(void *provctx
)
81 PROV_RSA_CTX
*prsactx
= OPENSSL_zalloc(sizeof(PROV_RSA_CTX
));
85 prsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
90 static int rsa_init(void *vprsactx
, void *vrsa
)
92 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
94 if (prsactx
== NULL
|| vrsa
== NULL
|| !RSA_up_ref(vrsa
))
96 RSA_free(prsactx
->rsa
);
98 prsactx
->pad_mode
= RSA_PKCS1_PADDING
;
102 static int rsa_encrypt(void *vprsactx
, unsigned char *out
, size_t *outlen
,
103 size_t outsize
, const unsigned char *in
, size_t inlen
)
105 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
109 size_t len
= RSA_size(prsactx
->rsa
);
112 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
119 if (prsactx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
120 int rsasize
= RSA_size(prsactx
->rsa
);
123 if ((tbuf
= OPENSSL_malloc(rsasize
)) == NULL
) {
124 PROVerr(0, ERR_R_MALLOC_FAILURE
);
127 if (prsactx
->oaep_md
== NULL
) {
129 prsactx
->oaep_md
= EVP_MD_fetch(prsactx
->libctx
, "SHA-1", NULL
);
130 PROVerr(0, ERR_R_INTERNAL_ERROR
);
133 ret
= RSA_padding_add_PKCS1_OAEP_mgf1(tbuf
, rsasize
, in
, inlen
,
135 prsactx
->oaep_labellen
,
143 ret
= RSA_public_encrypt(rsasize
, tbuf
, out
, prsactx
->rsa
,
147 ret
= RSA_public_encrypt(inlen
, in
, out
, prsactx
->rsa
,
150 /* A ret value of 0 is not an error */
157 static int rsa_decrypt(void *vprsactx
, unsigned char *out
, size_t *outlen
,
158 size_t outsize
, const unsigned char *in
, size_t inlen
)
160 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
162 size_t len
= RSA_size(prsactx
->rsa
);
164 if (prsactx
->pad_mode
== RSA_PKCS1_WITH_TLS_PADDING
) {
166 *outlen
= SSL_MAX_MASTER_KEY_LENGTH
;
169 if (outsize
< SSL_MAX_MASTER_KEY_LENGTH
) {
170 ERR_raise(ERR_LIB_PROV
, PROV_R_BAD_LENGTH
);
176 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
184 ERR_raise(ERR_LIB_PROV
, PROV_R_BAD_LENGTH
);
189 if (prsactx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
190 || prsactx
->pad_mode
== RSA_PKCS1_WITH_TLS_PADDING
) {
193 if ((tbuf
= OPENSSL_malloc(len
)) == NULL
) {
194 PROVerr(0, ERR_R_MALLOC_FAILURE
);
197 ret
= RSA_private_decrypt(inlen
, in
, tbuf
, prsactx
->rsa
,
200 * With no padding then, on success ret should be len, otherwise an
201 * error occurred (non-constant time)
203 if (ret
!= (int)len
) {
205 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_TO_DECRYPT
);
208 if (prsactx
->pad_mode
== RSA_PKCS1_OAEP_PADDING
) {
209 if (prsactx
->oaep_md
== NULL
) {
210 prsactx
->oaep_md
= EVP_MD_fetch(prsactx
->libctx
, "SHA-1", NULL
);
211 if (prsactx
->oaep_md
== NULL
) {
212 PROVerr(0, ERR_R_INTERNAL_ERROR
);
216 ret
= RSA_padding_check_PKCS1_OAEP_mgf1(out
, outsize
, tbuf
,
219 prsactx
->oaep_labellen
,
223 /* RSA_PKCS1_WITH_TLS_PADDING */
224 if (prsactx
->client_version
<= 0) {
225 ERR_raise(ERR_LIB_PROV
, PROV_R_BAD_TLS_CLIENT_VERSION
);
228 ret
= rsa_padding_check_PKCS1_type_2_TLS(out
, outsize
,
230 prsactx
->client_version
,
231 prsactx
->alt_version
);
235 ret
= RSA_private_decrypt(inlen
, in
, out
, prsactx
->rsa
,
238 *outlen
= constant_time_select_s(constant_time_msb_s(ret
), *outlen
, ret
);
239 ret
= constant_time_select_int(constant_time_msb(ret
), 0, 1);
243 static void rsa_freectx(void *vprsactx
)
245 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
247 RSA_free(prsactx
->rsa
);
249 EVP_MD_free(prsactx
->oaep_md
);
250 EVP_MD_free(prsactx
->mgf1_md
);
252 OPENSSL_free(prsactx
);
255 static void *rsa_dupctx(void *vprsactx
)
257 PROV_RSA_CTX
*srcctx
= (PROV_RSA_CTX
*)vprsactx
;
258 PROV_RSA_CTX
*dstctx
;
260 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
265 if (dstctx
->rsa
!= NULL
&& !RSA_up_ref(dstctx
->rsa
)) {
266 OPENSSL_free(dstctx
);
270 if (dstctx
->oaep_md
!= NULL
&& !EVP_MD_up_ref(dstctx
->oaep_md
)) {
271 RSA_free(dstctx
->rsa
);
272 OPENSSL_free(dstctx
);
276 if (dstctx
->mgf1_md
!= NULL
&& !EVP_MD_up_ref(dstctx
->mgf1_md
)) {
277 RSA_free(dstctx
->rsa
);
278 EVP_MD_free(dstctx
->oaep_md
);
279 OPENSSL_free(dstctx
);
286 static int rsa_get_ctx_params(void *vprsactx
, OSSL_PARAM
*params
)
288 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
291 if (prsactx
== NULL
|| params
== NULL
)
294 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_PAD_MODE
);
296 switch (p
->data_type
) {
297 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
298 if (!OSSL_PARAM_set_int(p
, prsactx
->pad_mode
))
301 case OSSL_PARAM_UTF8_STRING
:
304 const char *word
= NULL
;
306 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
307 if (prsactx
->pad_mode
== (int)padding_item
[i
].id
) {
308 word
= padding_item
[i
].ptr
;
314 if (!OSSL_PARAM_set_utf8_string(p
, word
))
317 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
325 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
);
326 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, prsactx
->oaep_md
== NULL
328 : EVP_MD_name(prsactx
->oaep_md
)))
331 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
);
333 EVP_MD
*mgf1_md
= prsactx
->mgf1_md
== NULL
? prsactx
->oaep_md
336 if (!OSSL_PARAM_set_utf8_string(p
, mgf1_md
== NULL
338 : EVP_MD_name(mgf1_md
)))
342 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
);
343 if (p
!= NULL
&& !OSSL_PARAM_set_octet_ptr(p
, prsactx
->oaep_label
, 0))
346 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN
);
347 if (p
!= NULL
&& !OSSL_PARAM_set_size_t(p
, prsactx
->oaep_labellen
))
350 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
);
351 if (p
!= NULL
&& !OSSL_PARAM_set_uint(p
, prsactx
->client_version
))
354 p
= OSSL_PARAM_locate(params
, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
);
355 if (p
!= NULL
&& !OSSL_PARAM_set_uint(p
, prsactx
->alt_version
))
361 static const OSSL_PARAM known_gettable_ctx_params
[] = {
362 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
, NULL
, 0),
363 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE
, NULL
, 0),
364 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
, NULL
, 0),
365 OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
, OSSL_PARAM_OCTET_PTR
,
367 OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN
, NULL
),
368 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
, NULL
),
369 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
, NULL
),
373 static const OSSL_PARAM
*rsa_gettable_ctx_params(void)
375 return known_gettable_ctx_params
;
378 static int rsa_set_ctx_params(void *vprsactx
, const OSSL_PARAM params
[])
380 PROV_RSA_CTX
*prsactx
= (PROV_RSA_CTX
*)vprsactx
;
382 char mdname
[OSSL_MAX_NAME_SIZE
];
383 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = { '\0' };
386 if (prsactx
== NULL
|| params
== NULL
)
389 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
);
391 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdname
)))
395 p
= OSSL_PARAM_locate_const(params
,
396 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS
);
398 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdprops
)))
402 EVP_MD_free(prsactx
->oaep_md
);
403 prsactx
->oaep_md
= EVP_MD_fetch(prsactx
->libctx
, mdname
, mdprops
);
405 if (prsactx
->oaep_md
== NULL
)
409 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_PAD_MODE
);
413 switch (p
->data_type
) {
414 case OSSL_PARAM_INTEGER
: /* Support for legacy pad mode number */
415 if (!OSSL_PARAM_get_int(p
, &pad_mode
))
418 case OSSL_PARAM_UTF8_STRING
:
425 for (i
= 0; padding_item
[i
].id
!= 0; i
++) {
426 if (strcmp(p
->data
, padding_item
[i
].ptr
) == 0) {
427 pad_mode
= padding_item
[i
].id
;
438 * PSS padding is for signatures only so is not compatible with
439 * asymmetric cipher use.
441 if (pad_mode
== RSA_PKCS1_PSS_PADDING
)
443 if (pad_mode
== RSA_PKCS1_OAEP_PADDING
&& prsactx
->oaep_md
== NULL
) {
444 prsactx
->oaep_md
= EVP_MD_fetch(prsactx
->libctx
, "SHA1", mdprops
);
445 if (prsactx
->oaep_md
== NULL
)
448 prsactx
->pad_mode
= pad_mode
;
451 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
);
453 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdname
)))
457 p
= OSSL_PARAM_locate_const(params
,
458 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS
);
460 if (!OSSL_PARAM_get_utf8_string(p
, &str
, sizeof(mdprops
)))
466 EVP_MD_free(prsactx
->mgf1_md
);
467 prsactx
->mgf1_md
= EVP_MD_fetch(prsactx
->libctx
, mdname
, str
);
469 if (prsactx
->mgf1_md
== NULL
)
473 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
);
475 void *tmp_label
= NULL
;
478 if (!OSSL_PARAM_get_octet_string(p
, &tmp_label
, 0, &tmp_labellen
))
480 OPENSSL_free(prsactx
->oaep_label
);
481 prsactx
->oaep_label
= (unsigned char *)tmp_label
;
482 prsactx
->oaep_labellen
= tmp_labellen
;
485 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
);
487 unsigned int client_version
;
489 if (!OSSL_PARAM_get_uint(p
, &client_version
))
491 prsactx
->client_version
= client_version
;
494 p
= OSSL_PARAM_locate_const(params
, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
);
496 unsigned int alt_version
;
498 if (!OSSL_PARAM_get_uint(p
, &alt_version
))
500 prsactx
->alt_version
= alt_version
;
506 static const OSSL_PARAM known_settable_ctx_params
[] = {
507 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST
, NULL
, 0),
508 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE
, NULL
, 0),
509 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST
, NULL
, 0),
510 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS
, NULL
, 0),
511 OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL
, NULL
, 0),
512 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION
, NULL
),
513 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION
, NULL
),
517 static const OSSL_PARAM
*rsa_settable_ctx_params(void)
519 return known_settable_ctx_params
;
522 const OSSL_DISPATCH rsa_asym_cipher_functions
[] = {
523 { OSSL_FUNC_ASYM_CIPHER_NEWCTX
, (void (*)(void))rsa_newctx
},
524 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT
, (void (*)(void))rsa_init
},
525 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT
, (void (*)(void))rsa_encrypt
},
526 { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT
, (void (*)(void))rsa_init
},
527 { OSSL_FUNC_ASYM_CIPHER_DECRYPT
, (void (*)(void))rsa_decrypt
},
528 { OSSL_FUNC_ASYM_CIPHER_FREECTX
, (void (*)(void))rsa_freectx
},
529 { OSSL_FUNC_ASYM_CIPHER_DUPCTX
, (void (*)(void))rsa_dupctx
},
530 { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS
,
531 (void (*)(void))rsa_get_ctx_params
},
532 { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS
,
533 (void (*)(void))rsa_gettable_ctx_params
},
534 { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS
,
535 (void (*)(void))rsa_set_ctx_params
},
536 { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS
,
537 (void (*)(void))rsa_settable_ctx_params
},