2 * Copyright 2019-2023 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_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/rsa.h>
21 #include <openssl/params.h>
22 #include <openssl/err.h>
23 #include <openssl/proverr.h>
24 /* Just for SSL_MAX_MASTER_KEY_LENGTH */
25 #include <openssl/prov_ssl.h>
26 #include "internal/constant_time.h"
27 #include "internal/sizes.h"
28 #include "crypto/rsa.h"
29 #include "prov/provider_ctx.h"
30 #include "prov/implementations.h"
31 #include "prov/providercommon.h"
32 #include "prov/securitycheck.h"
36 static OSSL_FUNC_asym_cipher_newctx_fn rsa_newctx;
37 static OSSL_FUNC_asym_cipher_encrypt_init_fn rsa_encrypt_init;
38 static OSSL_FUNC_asym_cipher_encrypt_fn rsa_encrypt;
39 static OSSL_FUNC_asym_cipher_decrypt_init_fn rsa_decrypt_init;
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;
48 static OSSL_ITEM padding_item[] = {
49 { RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
50 { RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE },
51 { RSA_PKCS1_OAEP_PADDING, OSSL_PKEY_RSA_PAD_MODE_OAEP }, /* Correct spelling first */
52 { RSA_PKCS1_OAEP_PADDING, "oeap" },
53 { RSA_X931_PADDING, OSSL_PKEY_RSA_PAD_MODE_X931 },
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.
68 /* OAEP message digest */
70 /* message digest for MGF1 */
73 unsigned char *oaep_label;
76 unsigned int client_version;
77 unsigned int alt_version;
78 /* PKCS#1 v1.5 decryption mode */
79 unsigned int implicit_rejection;
82 static void *rsa_newctx(void *provctx)
84 PROV_RSA_CTX *prsactx;
86 if (!ossl_prov_is_running())
88 prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
91 prsactx->libctx = PROV_LIBCTX_OF(provctx);
96 static int rsa_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[],
99 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
101 if (!ossl_prov_is_running() || prsactx == NULL || vrsa == NULL)
104 if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation))
107 if (!RSA_up_ref(vrsa))
109 RSA_free(prsactx->rsa);
111 prsactx->operation = operation;
112 prsactx->implicit_rejection = 1;
114 switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
115 case RSA_FLAG_TYPE_RSA:
116 prsactx->pad_mode = RSA_PKCS1_PADDING;
119 /* This should not happen due to the check above */
120 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
123 return rsa_set_ctx_params(prsactx, params);
126 static int rsa_encrypt_init(void *vprsactx, void *vrsa,
127 const OSSL_PARAM params[])
129 return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCRYPT);
132 static int rsa_decrypt_init(void *vprsactx, void *vrsa,
133 const OSSL_PARAM params[])
135 return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECRYPT);
138 static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
139 size_t outsize, const unsigned char *in, size_t inlen)
141 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
144 if (!ossl_prov_is_running())
148 size_t len = RSA_size(prsactx->rsa);
151 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
158 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
159 int rsasize = RSA_size(prsactx->rsa);
162 if ((tbuf = OPENSSL_malloc(rsasize)) == NULL)
164 if (prsactx->oaep_md == NULL) {
165 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
166 if (prsactx->oaep_md == NULL) {
168 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
173 ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(prsactx->libctx, tbuf,
176 prsactx->oaep_labellen,
184 ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
188 ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
191 /* A ret value of 0 is not an error */
198 static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
199 size_t outsize, const unsigned char *in, size_t inlen)
201 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
204 size_t len = RSA_size(prsactx->rsa);
206 if (!ossl_prov_is_running())
209 if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
211 *outlen = SSL_MAX_MASTER_KEY_LENGTH;
214 if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
215 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
221 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
229 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
234 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
235 || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
238 if ((tbuf = OPENSSL_malloc(len)) == NULL)
240 ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
243 * With no padding then, on success ret should be len, otherwise an
244 * error occurred (non-constant time)
246 if (ret != (int)len) {
248 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
251 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
252 if (prsactx->oaep_md == NULL) {
253 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
254 if (prsactx->oaep_md == NULL) {
256 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
260 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf,
263 prsactx->oaep_labellen,
267 /* RSA_PKCS1_WITH_TLS_PADDING */
268 if (prsactx->client_version <= 0) {
269 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
273 ret = ossl_rsa_padding_check_PKCS1_type_2_TLS(
274 prsactx->libctx, out, outsize, tbuf, len,
275 prsactx->client_version, prsactx->alt_version);
279 if ((prsactx->implicit_rejection == 0) &&
280 (prsactx->pad_mode == RSA_PKCS1_PADDING))
281 pad_mode = RSA_PKCS1_NO_IMPLICIT_REJECT_PADDING;
283 pad_mode = prsactx->pad_mode;
284 ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, pad_mode);
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);
291 static void rsa_freectx(void *vprsactx)
293 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
295 RSA_free(prsactx->rsa);
297 EVP_MD_free(prsactx->oaep_md);
298 EVP_MD_free(prsactx->mgf1_md);
299 OPENSSL_free(prsactx->oaep_label);
301 OPENSSL_free(prsactx);
304 static void *rsa_dupctx(void *vprsactx)
306 PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
307 PROV_RSA_CTX *dstctx;
309 if (!ossl_prov_is_running())
312 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
317 if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
318 OPENSSL_free(dstctx);
322 if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
323 RSA_free(dstctx->rsa);
324 OPENSSL_free(dstctx);
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);
338 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
340 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
346 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
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))
353 case OSSL_PARAM_UTF8_STRING:
356 const char *word = NULL;
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;
366 if (!OSSL_PARAM_set_utf8_string(p, word))
369 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
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
380 : EVP_MD_get0_name(prsactx->oaep_md)))
383 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
385 EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
388 if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
390 : EVP_MD_get0_name(mgf1_md)))
394 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
396 !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label,
397 prsactx->oaep_labellen))
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))
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))
408 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION);
409 if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->implicit_rejection))
415 static const OSSL_PARAM known_gettable_ctx_params[] = {
416 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
417 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
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,
421 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
422 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
423 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
427 static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
428 ossl_unused void *provctx)
430 return known_gettable_ctx_params;
433 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
435 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
437 char mdname[OSSL_MAX_NAME_SIZE];
438 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
446 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
449 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
452 p = OSSL_PARAM_locate_const(params,
453 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
456 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
460 EVP_MD_free(prsactx->oaep_md);
461 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
463 if (prsactx->oaep_md == NULL)
467 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
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))
476 case OSSL_PARAM_UTF8_STRING:
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;
496 * PSS padding is for signatures only so is not compatible with
497 * asymmetric cipher use.
499 if (pad_mode == RSA_PKCS1_PSS_PADDING)
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)
506 prsactx->pad_mode = pad_mode;
509 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
512 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
515 p = OSSL_PARAM_locate_const(params,
516 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
519 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
525 EVP_MD_free(prsactx->mgf1_md);
526 prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
528 if (prsactx->mgf1_md == NULL)
532 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
534 void *tmp_label = NULL;
537 if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
539 OPENSSL_free(prsactx->oaep_label);
540 prsactx->oaep_label = (unsigned char *)tmp_label;
541 prsactx->oaep_labellen = tmp_labellen;
544 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
546 unsigned int client_version;
548 if (!OSSL_PARAM_get_uint(p, &client_version))
550 prsactx->client_version = client_version;
553 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
555 unsigned int alt_version;
557 if (!OSSL_PARAM_get_uint(p, &alt_version))
559 prsactx->alt_version = alt_version;
561 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION);
563 unsigned int implicit_rejection;
565 if (!OSSL_PARAM_get_uint(p, &implicit_rejection))
567 prsactx->implicit_rejection = implicit_rejection;
573 static const OSSL_PARAM known_settable_ctx_params[] = {
574 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
575 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, NULL, 0),
576 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
577 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
578 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
579 OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
580 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
581 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
582 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
586 static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *vprsactx,
587 ossl_unused void *provctx)
589 return known_settable_ctx_params;
592 const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = {
593 { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
594 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init },
595 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
596 { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init },
597 { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
598 { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
599 { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
600 { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
601 (void (*)(void))rsa_get_ctx_params },
602 { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
603 (void (*)(void))rsa_gettable_ctx_params },
604 { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
605 (void (*)(void))rsa_set_ctx_params },
606 { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
607 (void (*)(void))rsa_settable_ctx_params },