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;
80 static void *rsa_newctx(void *provctx)
82 PROV_RSA_CTX *prsactx;
84 if (!ossl_prov_is_running())
86 prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
89 prsactx->libctx = PROV_LIBCTX_OF(provctx);
94 static int rsa_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[],
97 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
99 if (!ossl_prov_is_running() || prsactx == NULL || vrsa == NULL)
102 if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation))
105 if (!RSA_up_ref(vrsa))
107 RSA_free(prsactx->rsa);
109 prsactx->operation = operation;
111 switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
112 case RSA_FLAG_TYPE_RSA:
113 prsactx->pad_mode = RSA_PKCS1_PADDING;
116 /* This should not happen due to the check above */
117 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
120 return rsa_set_ctx_params(prsactx, params);
123 static int rsa_encrypt_init(void *vprsactx, void *vrsa,
124 const OSSL_PARAM params[])
126 return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCRYPT);
129 static int rsa_decrypt_init(void *vprsactx, void *vrsa,
130 const OSSL_PARAM params[])
132 return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECRYPT);
135 static int rsa_encrypt(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;
141 if (!ossl_prov_is_running())
145 size_t len = RSA_size(prsactx->rsa);
148 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
155 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
156 int rsasize = RSA_size(prsactx->rsa);
159 if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
160 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
163 if (prsactx->oaep_md == NULL) {
164 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
165 if (prsactx->oaep_md == NULL) {
167 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
172 ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(prsactx->libctx, tbuf,
175 prsactx->oaep_labellen,
183 ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
187 ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
190 /* A ret value of 0 is not an error */
197 static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
198 size_t outsize, const unsigned char *in, size_t inlen)
200 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
202 size_t len = RSA_size(prsactx->rsa);
204 if (!ossl_prov_is_running())
207 if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
209 *outlen = SSL_MAX_MASTER_KEY_LENGTH;
212 if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
213 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
219 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
227 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
232 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
233 || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
236 if ((tbuf = OPENSSL_malloc(len)) == NULL) {
237 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
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 ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
282 *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
283 ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
287 static void rsa_freectx(void *vprsactx)
289 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
291 RSA_free(prsactx->rsa);
293 EVP_MD_free(prsactx->oaep_md);
294 EVP_MD_free(prsactx->mgf1_md);
295 OPENSSL_free(prsactx->oaep_label);
297 OPENSSL_free(prsactx);
300 static void *rsa_dupctx(void *vprsactx)
302 PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
303 PROV_RSA_CTX *dstctx;
305 if (!ossl_prov_is_running())
308 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
313 if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
314 OPENSSL_free(dstctx);
318 if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
319 RSA_free(dstctx->rsa);
320 OPENSSL_free(dstctx);
324 if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
325 RSA_free(dstctx->rsa);
326 EVP_MD_free(dstctx->oaep_md);
327 OPENSSL_free(dstctx);
334 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
336 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
342 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
344 switch (p->data_type) {
345 case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
346 if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
349 case OSSL_PARAM_UTF8_STRING:
352 const char *word = NULL;
354 for (i = 0; padding_item[i].id != 0; i++) {
355 if (prsactx->pad_mode == (int)padding_item[i].id) {
356 word = padding_item[i].ptr;
362 if (!OSSL_PARAM_set_utf8_string(p, word))
365 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
373 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
374 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
376 : EVP_MD_get0_name(prsactx->oaep_md)))
379 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
381 EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
384 if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
386 : EVP_MD_get0_name(mgf1_md)))
390 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
392 !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label,
393 prsactx->oaep_labellen))
396 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
397 if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
400 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
401 if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
407 static const OSSL_PARAM known_gettable_ctx_params[] = {
408 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
409 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
410 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
411 OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
413 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
414 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
418 static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
419 ossl_unused void *provctx)
421 return known_gettable_ctx_params;
424 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
426 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
428 char mdname[OSSL_MAX_NAME_SIZE];
429 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
437 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
440 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
443 p = OSSL_PARAM_locate_const(params,
444 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
447 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
451 EVP_MD_free(prsactx->oaep_md);
452 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
454 if (prsactx->oaep_md == NULL)
458 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
462 switch (p->data_type) {
463 case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
464 if (!OSSL_PARAM_get_int(p, &pad_mode))
467 case OSSL_PARAM_UTF8_STRING:
474 for (i = 0; padding_item[i].id != 0; i++) {
475 if (strcmp(p->data, padding_item[i].ptr) == 0) {
476 pad_mode = padding_item[i].id;
487 * PSS padding is for signatures only so is not compatible with
488 * asymmetric cipher use.
490 if (pad_mode == RSA_PKCS1_PSS_PADDING)
492 if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
493 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
494 if (prsactx->oaep_md == NULL)
497 prsactx->pad_mode = pad_mode;
500 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
503 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
506 p = OSSL_PARAM_locate_const(params,
507 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
510 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
516 EVP_MD_free(prsactx->mgf1_md);
517 prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
519 if (prsactx->mgf1_md == NULL)
523 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
525 void *tmp_label = NULL;
528 if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
530 OPENSSL_free(prsactx->oaep_label);
531 prsactx->oaep_label = (unsigned char *)tmp_label;
532 prsactx->oaep_labellen = tmp_labellen;
535 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
537 unsigned int client_version;
539 if (!OSSL_PARAM_get_uint(p, &client_version))
541 prsactx->client_version = client_version;
544 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
546 unsigned int alt_version;
548 if (!OSSL_PARAM_get_uint(p, &alt_version))
550 prsactx->alt_version = alt_version;
556 static const OSSL_PARAM known_settable_ctx_params[] = {
557 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
558 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
559 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
560 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
561 OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
562 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
563 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
567 static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *vprsactx,
568 ossl_unused void *provctx)
570 return known_settable_ctx_params;
573 const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = {
574 { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
575 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init },
576 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
577 { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init },
578 { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
579 { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
580 { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
581 { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
582 (void (*)(void))rsa_get_ctx_params },
583 { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
584 (void (*)(void))rsa_gettable_ctx_params },
585 { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
586 (void (*)(void))rsa_set_ctx_params },
587 { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
588 (void (*)(void))rsa_settable_ctx_params },