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 "crypto/rsa.h"
21 #include "prov/providercommonerr.h"
22 #include "prov/provider_ctx.h"
23 #include "prov/implementations.h"
27 static OSSL_OP_asym_cipher_newctx_fn rsa_newctx;
28 static OSSL_OP_asym_cipher_encrypt_init_fn rsa_init;
29 static OSSL_OP_asym_cipher_encrypt_fn rsa_encrypt;
30 static OSSL_OP_asym_cipher_decrypt_init_fn rsa_init;
31 static OSSL_OP_asym_cipher_decrypt_fn rsa_decrypt;
32 static OSSL_OP_asym_cipher_freectx_fn rsa_freectx;
33 static OSSL_OP_asym_cipher_dupctx_fn rsa_dupctx;
34 static OSSL_OP_asym_cipher_get_ctx_params_fn rsa_get_ctx_params;
35 static OSSL_OP_asym_cipher_gettable_ctx_params_fn rsa_gettable_ctx_params;
36 static OSSL_OP_asym_cipher_set_ctx_params_fn rsa_set_ctx_params;
37 static OSSL_OP_asym_cipher_settable_ctx_params_fn rsa_settable_ctx_params;
41 * What's passed as an actual key is defined by the KEYMGMT interface.
42 * We happen to know that our KEYMGMT simply passes RSA structures, so
43 * we use that here too.
50 /* OAEP message digest */
52 /* message digest for MGF1 */
55 unsigned char *oaep_label;
58 unsigned int client_version;
59 unsigned int alt_version;
62 static void *rsa_newctx(void *provctx)
64 PROV_RSA_CTX *prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
68 prsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
73 static int rsa_init(void *vprsactx, void *vrsa)
75 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
77 if (prsactx == NULL || vrsa == NULL || !RSA_up_ref(vrsa))
79 RSA_free(prsactx->rsa);
81 prsactx->pad_mode = RSA_PKCS1_PADDING;
85 static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
86 size_t outsize, const unsigned char *in, size_t inlen)
88 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
92 size_t len = RSA_size(prsactx->rsa);
95 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
102 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
103 int rsasize = RSA_size(prsactx->rsa);
106 if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
107 PROVerr(0, ERR_R_MALLOC_FAILURE);
110 ret = RSA_padding_add_PKCS1_OAEP_mgf1(tbuf, rsasize, in, inlen,
112 prsactx->oaep_labellen,
120 ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
124 ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
127 /* A ret value of 0 is not an error */
134 static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
135 size_t outsize, const unsigned char *in, size_t inlen)
137 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
139 size_t len = RSA_size(prsactx->rsa);
141 if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
143 *outlen = SSL_MAX_MASTER_KEY_LENGTH;
146 if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
147 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
153 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
161 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
166 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
167 || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
170 if ((tbuf = OPENSSL_malloc(len)) == NULL) {
171 PROVerr(0, ERR_R_MALLOC_FAILURE);
174 ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
177 * With no padding then, on success ret should be len, otherwise an
178 * error occurred (non-constant time)
180 if (ret != (int)len) {
182 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
185 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
186 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf,
189 prsactx->oaep_labellen,
193 /* RSA_PKCS1_WITH_TLS_PADDING */
194 if (prsactx->client_version <= 0) {
195 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
198 ret = rsa_padding_check_PKCS1_type_2_TLS(out, outsize,
200 prsactx->client_version,
201 prsactx->alt_version);
205 ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
208 *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
209 ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
213 static void rsa_freectx(void *vprsactx)
215 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
217 RSA_free(prsactx->rsa);
219 EVP_MD_free(prsactx->oaep_md);
220 EVP_MD_free(prsactx->mgf1_md);
222 OPENSSL_free(prsactx);
225 static void *rsa_dupctx(void *vprsactx)
227 PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
228 PROV_RSA_CTX *dstctx;
230 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
235 if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
236 OPENSSL_free(dstctx);
240 if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
241 RSA_free(dstctx->rsa);
242 OPENSSL_free(dstctx);
246 if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
247 RSA_free(dstctx->rsa);
248 EVP_MD_free(dstctx->oaep_md);
249 OPENSSL_free(dstctx);
256 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
258 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
261 if (prsactx == NULL || params == NULL)
264 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
265 if (p != NULL && !OSSL_PARAM_set_int(p, prsactx->pad_mode))
268 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
269 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
271 : EVP_MD_name(prsactx->oaep_md)))
274 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
276 EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
279 if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
281 : EVP_MD_name(mgf1_md)))
285 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
286 if (p != NULL && !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, 0))
289 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN);
290 if (p != NULL && !OSSL_PARAM_set_size_t(p, prsactx->oaep_labellen))
293 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
294 if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
297 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
298 if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
304 static const OSSL_PARAM known_gettable_ctx_params[] = {
305 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
306 OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL),
307 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
308 OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
310 OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL),
311 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
312 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
316 static const OSSL_PARAM *rsa_gettable_ctx_params(void)
318 return known_gettable_ctx_params;
321 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
323 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
325 /* Should be big enough */
326 char mdname[80], mdprops[80] = { '\0' };
330 if (prsactx == NULL || params == NULL)
333 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
335 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
339 p = OSSL_PARAM_locate_const(params,
340 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
342 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
346 EVP_MD_free(prsactx->oaep_md);
347 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
349 if (prsactx->oaep_md == NULL)
353 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
355 if (!OSSL_PARAM_get_int(p, &pad_mode))
358 * PSS padding is for signatures only so is not compatible with
359 * asymmetric cipher use.
361 if (pad_mode == RSA_PKCS1_PSS_PADDING)
363 if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
364 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
365 if (prsactx->oaep_md == NULL)
368 prsactx->pad_mode = pad_mode;
371 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
373 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
377 p = OSSL_PARAM_locate_const(params,
378 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
380 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
386 EVP_MD_free(prsactx->mgf1_md);
387 prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
389 if (prsactx->mgf1_md == NULL)
393 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
395 void *tmp_label = NULL;
398 if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
400 OPENSSL_free(prsactx->oaep_label);
401 prsactx->oaep_label = (unsigned char *)tmp_label;
402 prsactx->oaep_labellen = tmp_labellen;
405 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
407 unsigned int client_version;
409 if (!OSSL_PARAM_get_uint(p, &client_version))
411 prsactx->client_version = client_version;
414 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
416 unsigned int alt_version;
418 if (!OSSL_PARAM_get_uint(p, &alt_version))
420 prsactx->alt_version = alt_version;
426 static const OSSL_PARAM known_settable_ctx_params[] = {
427 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
428 OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL),
429 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
430 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
431 OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
432 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
433 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
437 static const OSSL_PARAM *rsa_settable_ctx_params(void)
439 return known_settable_ctx_params;
442 const OSSL_DISPATCH rsa_asym_cipher_functions[] = {
443 { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
444 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_init },
445 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
446 { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_init },
447 { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
448 { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
449 { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
450 { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
451 (void (*)(void))rsa_get_ctx_params },
452 { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
453 (void (*)(void))rsa_gettable_ctx_params },
454 { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
455 (void (*)(void))rsa_set_ctx_params },
456 { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
457 (void (*)(void))rsa_settable_ctx_params },