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(prsactx->libctx, out,
231 prsactx->client_version,
232 prsactx->alt_version);
236 ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
239 *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
240 ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
244 static void rsa_freectx(void *vprsactx)
246 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
248 RSA_free(prsactx->rsa);
250 EVP_MD_free(prsactx->oaep_md);
251 EVP_MD_free(prsactx->mgf1_md);
253 OPENSSL_free(prsactx);
256 static void *rsa_dupctx(void *vprsactx)
258 PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
259 PROV_RSA_CTX *dstctx;
261 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
266 if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
267 OPENSSL_free(dstctx);
271 if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
272 RSA_free(dstctx->rsa);
273 OPENSSL_free(dstctx);
277 if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
278 RSA_free(dstctx->rsa);
279 EVP_MD_free(dstctx->oaep_md);
280 OPENSSL_free(dstctx);
287 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
289 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
292 if (prsactx == NULL || params == NULL)
295 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
297 switch (p->data_type) {
298 case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
299 if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
302 case OSSL_PARAM_UTF8_STRING:
305 const char *word = NULL;
307 for (i = 0; padding_item[i].id != 0; i++) {
308 if (prsactx->pad_mode == (int)padding_item[i].id) {
309 word = padding_item[i].ptr;
315 if (!OSSL_PARAM_set_utf8_string(p, word))
318 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
326 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
327 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
329 : EVP_MD_name(prsactx->oaep_md)))
332 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
334 EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
337 if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
339 : EVP_MD_name(mgf1_md)))
343 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
344 if (p != NULL && !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, 0))
347 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN);
348 if (p != NULL && !OSSL_PARAM_set_size_t(p, prsactx->oaep_labellen))
351 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
352 if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
355 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
356 if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
362 static const OSSL_PARAM known_gettable_ctx_params[] = {
363 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
364 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
365 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
366 OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
368 OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL),
369 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
370 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
374 static const OSSL_PARAM *rsa_gettable_ctx_params(void)
376 return known_gettable_ctx_params;
379 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
381 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
383 char mdname[OSSL_MAX_NAME_SIZE];
384 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
387 if (prsactx == NULL || params == NULL)
390 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
392 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
396 p = OSSL_PARAM_locate_const(params,
397 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
399 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
403 EVP_MD_free(prsactx->oaep_md);
404 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
406 if (prsactx->oaep_md == NULL)
410 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
414 switch (p->data_type) {
415 case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
416 if (!OSSL_PARAM_get_int(p, &pad_mode))
419 case OSSL_PARAM_UTF8_STRING:
426 for (i = 0; padding_item[i].id != 0; i++) {
427 if (strcmp(p->data, padding_item[i].ptr) == 0) {
428 pad_mode = padding_item[i].id;
439 * PSS padding is for signatures only so is not compatible with
440 * asymmetric cipher use.
442 if (pad_mode == RSA_PKCS1_PSS_PADDING)
444 if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
445 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
446 if (prsactx->oaep_md == NULL)
449 prsactx->pad_mode = pad_mode;
452 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
454 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
458 p = OSSL_PARAM_locate_const(params,
459 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
461 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
467 EVP_MD_free(prsactx->mgf1_md);
468 prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
470 if (prsactx->mgf1_md == NULL)
474 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
476 void *tmp_label = NULL;
479 if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
481 OPENSSL_free(prsactx->oaep_label);
482 prsactx->oaep_label = (unsigned char *)tmp_label;
483 prsactx->oaep_labellen = tmp_labellen;
486 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
488 unsigned int client_version;
490 if (!OSSL_PARAM_get_uint(p, &client_version))
492 prsactx->client_version = client_version;
495 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
497 unsigned int alt_version;
499 if (!OSSL_PARAM_get_uint(p, &alt_version))
501 prsactx->alt_version = alt_version;
507 static const OSSL_PARAM known_settable_ctx_params[] = {
508 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
509 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
510 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
511 OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
512 OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
513 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
514 OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
518 static const OSSL_PARAM *rsa_settable_ctx_params(void)
520 return known_settable_ctx_params;
523 const OSSL_DISPATCH rsa_asym_cipher_functions[] = {
524 { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
525 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_init },
526 { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
527 { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_init },
528 { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
529 { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
530 { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
531 { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
532 (void (*)(void))rsa_get_ctx_params },
533 { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
534 (void (*)(void))rsa_gettable_ctx_params },
535 { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
536 (void (*)(void))rsa_set_ctx_params },
537 { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
538 (void (*)(void))rsa_settable_ctx_params },