2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/rsa.h>
15 #include <openssl/bn.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/cms.h>
19 #include "internal/evp_int.h"
22 /* RSA pkey context structure */
25 /* Key gen parameters */
28 /* Keygen callback info */
30 /* RSA padding mode */
34 /* message digest for MGF1 */
38 /* Minimum salt length or -1 if no PSS parameter restriction */
43 unsigned char *oaep_label;
47 /* True if PSS parameters are restricted */
48 #define rsa_pss_restricted(rctx) (rctx->min_saltlen != -1)
50 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
52 RSA_PKEY_CTX *rctx = OPENSSL_zalloc(sizeof(*rctx));
56 if (pkey_ctx_is_pss(ctx))
57 rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
59 rctx->pad_mode = RSA_PKCS1_PADDING;
61 rctx->min_saltlen = -1;
63 ctx->keygen_info = rctx->gentmp;
64 ctx->keygen_info_count = 2;
69 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
71 RSA_PKEY_CTX *dctx, *sctx;
73 if (!pkey_rsa_init(dst))
77 dctx->nbits = sctx->nbits;
79 dctx->pub_exp = BN_dup(sctx->pub_exp);
83 dctx->pad_mode = sctx->pad_mode;
85 dctx->mgf1md = sctx->mgf1md;
86 if (sctx->oaep_label) {
87 OPENSSL_free(dctx->oaep_label);
88 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
89 if (!dctx->oaep_label)
91 dctx->oaep_labellen = sctx->oaep_labellen;
96 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
98 if (ctx->tbuf != NULL)
100 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
101 if (ctx->tbuf == NULL)
106 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
108 RSA_PKEY_CTX *rctx = ctx->data;
110 BN_free(rctx->pub_exp);
111 OPENSSL_free(rctx->tbuf);
112 OPENSSL_free(rctx->oaep_label);
117 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
118 size_t *siglen, const unsigned char *tbs,
122 RSA_PKEY_CTX *rctx = ctx->data;
123 RSA *rsa = ctx->pkey->pkey.rsa;
126 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
127 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
131 if (EVP_MD_type(rctx->md) == NID_mdc2) {
133 if (rctx->pad_mode != RSA_PKCS1_PADDING)
135 ret = RSA_sign_ASN1_OCTET_STRING(0,
136 tbs, tbslen, sig, &sltmp, rsa);
141 } else if (rctx->pad_mode == RSA_X931_PADDING) {
142 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
143 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
146 if (!setup_tbuf(rctx, ctx)) {
147 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
150 memcpy(rctx->tbuf, tbs, tbslen);
151 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
152 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
153 sig, rsa, RSA_X931_PADDING);
154 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
156 ret = RSA_sign(EVP_MD_type(rctx->md),
157 tbs, tbslen, sig, &sltmp, rsa);
161 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
162 if (!setup_tbuf(rctx, ctx))
164 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
166 rctx->md, rctx->mgf1md,
169 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
170 sig, rsa, RSA_NO_PADDING);
174 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
182 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
183 unsigned char *rout, size_t *routlen,
184 const unsigned char *sig, size_t siglen)
187 RSA_PKEY_CTX *rctx = ctx->data;
190 if (rctx->pad_mode == RSA_X931_PADDING) {
191 if (!setup_tbuf(rctx, ctx))
193 ret = RSA_public_decrypt(siglen, sig,
194 rctx->tbuf, ctx->pkey->pkey.rsa,
199 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
200 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
201 RSA_R_ALGORITHM_MISMATCH);
204 if (ret != EVP_MD_size(rctx->md)) {
205 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
206 RSA_R_INVALID_DIGEST_LENGTH);
210 memcpy(rout, rctx->tbuf, ret);
211 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
213 ret = int_rsa_verify(EVP_MD_type(rctx->md),
214 NULL, 0, rout, &sltmp,
215 sig, siglen, ctx->pkey->pkey.rsa);
222 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
230 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
231 const unsigned char *sig, size_t siglen,
232 const unsigned char *tbs, size_t tbslen)
234 RSA_PKEY_CTX *rctx = ctx->data;
235 RSA *rsa = ctx->pkey->pkey.rsa;
239 if (rctx->pad_mode == RSA_PKCS1_PADDING)
240 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
242 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
243 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
246 if (rctx->pad_mode == RSA_X931_PADDING) {
247 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
249 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
251 if (!setup_tbuf(rctx, ctx))
253 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
254 rsa, RSA_NO_PADDING);
257 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
258 rctx->md, rctx->mgf1md,
259 rctx->tbuf, rctx->saltlen);
266 if (!setup_tbuf(rctx, ctx))
268 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
269 rsa, rctx->pad_mode);
274 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
281 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
282 unsigned char *out, size_t *outlen,
283 const unsigned char *in, size_t inlen)
286 RSA_PKEY_CTX *rctx = ctx->data;
288 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
289 int klen = RSA_size(ctx->pkey->pkey.rsa);
290 if (!setup_tbuf(rctx, ctx))
292 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
296 rctx->md, rctx->mgf1md))
298 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
299 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
301 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
309 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
310 unsigned char *out, size_t *outlen,
311 const unsigned char *in, size_t inlen)
314 RSA_PKEY_CTX *rctx = ctx->data;
316 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
318 if (!setup_tbuf(rctx, ctx))
320 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
321 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
324 for (i = 0; i < ret; i++) {
328 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
332 rctx->md, rctx->mgf1md);
334 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
342 static int check_padding_md(const EVP_MD *md, int padding)
349 mdnid = EVP_MD_type(md);
351 if (padding == RSA_NO_PADDING) {
352 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
356 if (padding == RSA_X931_PADDING) {
357 if (RSA_X931_hash_id(mdnid) == -1) {
358 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
363 /* List of all supported RSA digests */
378 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
387 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
389 RSA_PKEY_CTX *rctx = ctx->data;
392 case EVP_PKEY_CTRL_RSA_PADDING:
393 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
394 if (!check_padding_md(rctx->md, p1))
396 if (p1 == RSA_PKCS1_PSS_PADDING) {
397 if (!(ctx->operation &
398 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
401 rctx->md = EVP_sha1();
402 } else if (pkey_ctx_is_pss(ctx)) {
405 if (p1 == RSA_PKCS1_OAEP_PADDING) {
406 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
409 rctx->md = EVP_sha1();
415 RSAerr(RSA_F_PKEY_RSA_CTRL,
416 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
419 case EVP_PKEY_CTRL_GET_RSA_PADDING:
420 *(int *)p2 = rctx->pad_mode;
423 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
424 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
425 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
426 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
429 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
430 *(int *)p2 = rctx->saltlen;
434 if (rsa_pss_restricted(rctx) && p1 < rctx->min_saltlen) {
435 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL);
442 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
444 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
450 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
451 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
452 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
455 BN_free(rctx->pub_exp);
459 case EVP_PKEY_CTRL_RSA_OAEP_MD:
460 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
461 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
462 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
465 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
466 *(const EVP_MD **)p2 = rctx->md;
471 case EVP_PKEY_CTRL_MD:
472 if (!check_padding_md(p2, rctx->pad_mode))
474 if (rsa_pss_restricted(rctx)) {
475 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
477 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED);
483 case EVP_PKEY_CTRL_GET_MD:
484 *(const EVP_MD **)p2 = rctx->md;
487 case EVP_PKEY_CTRL_RSA_MGF1_MD:
488 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
489 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
490 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
491 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
494 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
496 *(const EVP_MD **)p2 = rctx->mgf1md;
498 *(const EVP_MD **)p2 = rctx->md;
500 if (rsa_pss_restricted(rctx)) {
501 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
503 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
510 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
511 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
512 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
515 OPENSSL_free(rctx->oaep_label);
517 rctx->oaep_label = p2;
518 rctx->oaep_labellen = p1;
520 rctx->oaep_label = NULL;
521 rctx->oaep_labellen = 0;
525 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
526 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
527 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
530 *(unsigned char **)p2 = rctx->oaep_label;
531 return rctx->oaep_labellen;
533 case EVP_PKEY_CTRL_DIGESTINIT:
534 case EVP_PKEY_CTRL_PKCS7_SIGN:
535 #ifndef OPENSSL_NO_CMS
536 case EVP_PKEY_CTRL_CMS_SIGN:
540 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
541 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
542 #ifndef OPENSSL_NO_CMS
543 case EVP_PKEY_CTRL_CMS_DECRYPT:
544 case EVP_PKEY_CTRL_CMS_ENCRYPT:
545 if (!pkey_ctx_is_pss(ctx))
548 case EVP_PKEY_CTRL_PEER_KEY:
549 RSAerr(RSA_F_PKEY_RSA_CTRL,
550 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
559 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
560 const char *type, const char *value)
563 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
566 if (strcmp(type, "rsa_padding_mode") == 0) {
568 if (strcmp(value, "pkcs1") == 0)
569 pm = RSA_PKCS1_PADDING;
570 else if (strcmp(value, "sslv23") == 0)
571 pm = RSA_SSLV23_PADDING;
572 else if (strcmp(value, "none") == 0)
574 else if (strcmp(value, "oeap") == 0)
575 pm = RSA_PKCS1_OAEP_PADDING;
576 else if (strcmp(value, "oaep") == 0)
577 pm = RSA_PKCS1_OAEP_PADDING;
578 else if (strcmp(value, "x931") == 0)
579 pm = RSA_X931_PADDING;
580 else if (strcmp(value, "pss") == 0)
581 pm = RSA_PKCS1_PSS_PADDING;
583 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
586 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
589 if (strcmp(type, "rsa_pss_saltlen") == 0) {
591 saltlen = atoi(value);
592 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
595 if (strcmp(type, "rsa_keygen_bits") == 0) {
598 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
601 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
603 BIGNUM *pubexp = NULL;
604 if (!BN_asc2bn(&pubexp, value))
606 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
612 if (strcmp(type, "rsa_mgf1_md") == 0)
613 return EVP_PKEY_CTX_md(ctx,
614 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
615 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
617 if (pkey_ctx_is_pss(ctx)) {
619 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
620 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
621 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
623 if (strcmp(type, "rsa_pss_keygen_md") == 0)
624 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
625 EVP_PKEY_CTRL_MD, value);
627 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
629 saltlen = atoi(value);
630 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
634 if (strcmp(type, "rsa_oaep_md") == 0)
635 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
636 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
638 if (strcmp(type, "rsa_oaep_label") == 0) {
642 lab = OPENSSL_hexstr2buf(value, &lablen);
645 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
654 /* Set PSS parameters when generating a key, if necessary */
655 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
657 RSA_PKEY_CTX *rctx = ctx->data;
659 if (!pkey_ctx_is_pss(ctx))
661 /* If all parameters are default values don't set pss */
662 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
664 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
665 rctx->saltlen == -2 ? 0 : rctx->saltlen);
666 if (rsa->pss == NULL)
671 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
674 RSA_PKEY_CTX *rctx = ctx->data;
678 if (rctx->pub_exp == NULL) {
679 rctx->pub_exp = BN_new();
680 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
686 if (ctx->pkey_gencb) {
687 pcb = BN_GENCB_new();
692 evp_pkey_set_cb_translate(pcb, ctx);
695 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
697 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
702 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
708 const EVP_PKEY_METHOD rsa_pkey_meth = {
710 EVP_PKEY_FLAG_AUTOARGLEN,
727 pkey_rsa_verifyrecover,
744 * Called for PSS sign or verify initialisation: checks PSS parameter
745 * sanity and sets any restrictions on key usage.
748 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
751 RSA_PKEY_CTX *rctx = ctx->data;
753 const EVP_MD *mgf1md;
756 /* Should never happen */
757 if (!pkey_ctx_is_pss(ctx))
759 rsa = ctx->pkey->pkey.rsa;
760 /* If no restrictions just return */
761 if (rsa->pss == NULL)
763 /* Get and check parameters */
764 if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
767 rctx->min_saltlen = min_saltlen;
770 * Set PSS restrictions as defaults: we can then block any attempt to
771 * use invalid values in pkey_rsa_ctrl
775 rctx->mgf1md = mgf1md;
776 rctx->saltlen = min_saltlen;
781 const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
783 EVP_PKEY_FLAG_AUTOARGLEN,
799 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,