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));
57 if (pkey_ctx_is_pss(ctx))
58 rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
60 rctx->pad_mode = RSA_PKCS1_PADDING;
62 rctx->min_saltlen = -1;
64 ctx->keygen_info = rctx->gentmp;
65 ctx->keygen_info_count = 2;
70 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
72 RSA_PKEY_CTX *dctx, *sctx;
74 if (!pkey_rsa_init(dst))
78 dctx->nbits = sctx->nbits;
80 dctx->pub_exp = BN_dup(sctx->pub_exp);
84 dctx->pad_mode = sctx->pad_mode;
86 dctx->mgf1md = sctx->mgf1md;
87 if (sctx->oaep_label) {
88 OPENSSL_free(dctx->oaep_label);
89 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
90 if (!dctx->oaep_label)
92 dctx->oaep_labellen = sctx->oaep_labellen;
97 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
99 if (ctx->tbuf != NULL)
101 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
102 if (ctx->tbuf == NULL)
107 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
109 RSA_PKEY_CTX *rctx = ctx->data;
111 BN_free(rctx->pub_exp);
112 OPENSSL_free(rctx->tbuf);
113 OPENSSL_free(rctx->oaep_label);
118 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
119 size_t *siglen, const unsigned char *tbs,
123 RSA_PKEY_CTX *rctx = ctx->data;
124 RSA *rsa = ctx->pkey->pkey.rsa;
127 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
128 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
132 if (EVP_MD_type(rctx->md) == NID_mdc2) {
134 if (rctx->pad_mode != RSA_PKCS1_PADDING)
136 ret = RSA_sign_ASN1_OCTET_STRING(0,
137 tbs, tbslen, sig, &sltmp, rsa);
142 } else if (rctx->pad_mode == RSA_X931_PADDING) {
143 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
144 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
147 if (!setup_tbuf(rctx, ctx)) {
148 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
151 memcpy(rctx->tbuf, tbs, tbslen);
152 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
153 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
154 sig, rsa, RSA_X931_PADDING);
155 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
157 ret = RSA_sign(EVP_MD_type(rctx->md),
158 tbs, tbslen, sig, &sltmp, rsa);
162 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
163 if (!setup_tbuf(rctx, ctx))
165 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
167 rctx->md, rctx->mgf1md,
170 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
171 sig, rsa, RSA_NO_PADDING);
175 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
183 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
184 unsigned char *rout, size_t *routlen,
185 const unsigned char *sig, size_t siglen)
188 RSA_PKEY_CTX *rctx = ctx->data;
191 if (rctx->pad_mode == RSA_X931_PADDING) {
192 if (!setup_tbuf(rctx, ctx))
194 ret = RSA_public_decrypt(siglen, sig,
195 rctx->tbuf, ctx->pkey->pkey.rsa,
200 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
201 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
202 RSA_R_ALGORITHM_MISMATCH);
205 if (ret != EVP_MD_size(rctx->md)) {
206 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
207 RSA_R_INVALID_DIGEST_LENGTH);
211 memcpy(rout, rctx->tbuf, ret);
212 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
214 ret = int_rsa_verify(EVP_MD_type(rctx->md),
215 NULL, 0, rout, &sltmp,
216 sig, siglen, ctx->pkey->pkey.rsa);
223 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
231 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
232 const unsigned char *sig, size_t siglen,
233 const unsigned char *tbs, size_t tbslen)
235 RSA_PKEY_CTX *rctx = ctx->data;
236 RSA *rsa = ctx->pkey->pkey.rsa;
240 if (rctx->pad_mode == RSA_PKCS1_PADDING)
241 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
243 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
244 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
247 if (rctx->pad_mode == RSA_X931_PADDING) {
248 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
250 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
252 if (!setup_tbuf(rctx, ctx))
254 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
255 rsa, RSA_NO_PADDING);
258 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
259 rctx->md, rctx->mgf1md,
260 rctx->tbuf, rctx->saltlen);
267 if (!setup_tbuf(rctx, ctx))
269 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
270 rsa, rctx->pad_mode);
275 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
282 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
283 unsigned char *out, size_t *outlen,
284 const unsigned char *in, size_t inlen)
287 RSA_PKEY_CTX *rctx = ctx->data;
289 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
290 int klen = RSA_size(ctx->pkey->pkey.rsa);
291 if (!setup_tbuf(rctx, ctx))
293 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
297 rctx->md, rctx->mgf1md))
299 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
300 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
302 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
310 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
311 unsigned char *out, size_t *outlen,
312 const unsigned char *in, size_t inlen)
315 RSA_PKEY_CTX *rctx = ctx->data;
317 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
319 if (!setup_tbuf(rctx, ctx))
321 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
322 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
325 for (i = 0; i < ret; i++) {
329 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
333 rctx->md, rctx->mgf1md);
335 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
343 static int check_padding_md(const EVP_MD *md, int padding)
350 mdnid = EVP_MD_type(md);
352 if (padding == RSA_NO_PADDING) {
353 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
357 if (padding == RSA_X931_PADDING) {
358 if (RSA_X931_hash_id(mdnid) == -1) {
359 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
364 /* List of all supported RSA digests */
379 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
388 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
390 RSA_PKEY_CTX *rctx = ctx->data;
393 case EVP_PKEY_CTRL_RSA_PADDING:
394 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
395 if (!check_padding_md(rctx->md, p1))
397 if (p1 == RSA_PKCS1_PSS_PADDING) {
398 if (!(ctx->operation &
399 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
402 rctx->md = EVP_sha1();
403 } else if (pkey_ctx_is_pss(ctx)) {
406 if (p1 == RSA_PKCS1_OAEP_PADDING) {
407 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
410 rctx->md = EVP_sha1();
416 RSAerr(RSA_F_PKEY_RSA_CTRL,
417 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
420 case EVP_PKEY_CTRL_GET_RSA_PADDING:
421 *(int *)p2 = rctx->pad_mode;
424 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
425 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
426 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
427 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
430 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
431 *(int *)p2 = rctx->saltlen;
435 if (rsa_pss_restricted(rctx)) {
436 if (p1 == -2 && ctx->operation == EVP_PKEY_OP_VERIFY) {
437 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
440 if ((p1 == -1 && rctx->min_saltlen > EVP_MD_size(rctx->md))
441 || (p1 >= 0 && p1 < rctx->min_saltlen)) {
442 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL);
450 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
452 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
458 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
459 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
460 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
463 BN_free(rctx->pub_exp);
467 case EVP_PKEY_CTRL_RSA_OAEP_MD:
468 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
469 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
470 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
473 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
474 *(const EVP_MD **)p2 = rctx->md;
479 case EVP_PKEY_CTRL_MD:
480 if (!check_padding_md(p2, rctx->pad_mode))
482 if (rsa_pss_restricted(rctx)) {
483 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
485 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED);
491 case EVP_PKEY_CTRL_GET_MD:
492 *(const EVP_MD **)p2 = rctx->md;
495 case EVP_PKEY_CTRL_RSA_MGF1_MD:
496 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
497 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
498 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
499 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
502 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
504 *(const EVP_MD **)p2 = rctx->mgf1md;
506 *(const EVP_MD **)p2 = rctx->md;
508 if (rsa_pss_restricted(rctx)) {
509 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
511 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
518 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
519 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
520 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
523 OPENSSL_free(rctx->oaep_label);
525 rctx->oaep_label = p2;
526 rctx->oaep_labellen = p1;
528 rctx->oaep_label = NULL;
529 rctx->oaep_labellen = 0;
533 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
534 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
535 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
538 *(unsigned char **)p2 = rctx->oaep_label;
539 return rctx->oaep_labellen;
541 case EVP_PKEY_CTRL_DIGESTINIT:
542 case EVP_PKEY_CTRL_PKCS7_SIGN:
543 #ifndef OPENSSL_NO_CMS
544 case EVP_PKEY_CTRL_CMS_SIGN:
548 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
549 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
550 #ifndef OPENSSL_NO_CMS
551 case EVP_PKEY_CTRL_CMS_DECRYPT:
552 case EVP_PKEY_CTRL_CMS_ENCRYPT:
553 if (!pkey_ctx_is_pss(ctx))
556 case EVP_PKEY_CTRL_PEER_KEY:
557 RSAerr(RSA_F_PKEY_RSA_CTRL,
558 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
567 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
568 const char *type, const char *value)
571 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
574 if (strcmp(type, "rsa_padding_mode") == 0) {
576 if (strcmp(value, "pkcs1") == 0)
577 pm = RSA_PKCS1_PADDING;
578 else if (strcmp(value, "sslv23") == 0)
579 pm = RSA_SSLV23_PADDING;
580 else if (strcmp(value, "none") == 0)
582 else if (strcmp(value, "oeap") == 0)
583 pm = RSA_PKCS1_OAEP_PADDING;
584 else if (strcmp(value, "oaep") == 0)
585 pm = RSA_PKCS1_OAEP_PADDING;
586 else if (strcmp(value, "x931") == 0)
587 pm = RSA_X931_PADDING;
588 else if (strcmp(value, "pss") == 0)
589 pm = RSA_PKCS1_PSS_PADDING;
591 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
594 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
597 if (strcmp(type, "rsa_pss_saltlen") == 0) {
599 saltlen = atoi(value);
600 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
603 if (strcmp(type, "rsa_keygen_bits") == 0) {
606 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
609 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
611 BIGNUM *pubexp = NULL;
612 if (!BN_asc2bn(&pubexp, value))
614 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
620 if (strcmp(type, "rsa_mgf1_md") == 0)
621 return EVP_PKEY_CTX_md(ctx,
622 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
623 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
625 if (pkey_ctx_is_pss(ctx)) {
627 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
628 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
629 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
631 if (strcmp(type, "rsa_pss_keygen_md") == 0)
632 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
633 EVP_PKEY_CTRL_MD, value);
635 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
636 int saltlen = atoi(value);
638 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
642 if (strcmp(type, "rsa_oaep_md") == 0)
643 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
644 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
646 if (strcmp(type, "rsa_oaep_label") == 0) {
650 lab = OPENSSL_hexstr2buf(value, &lablen);
653 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
662 /* Set PSS parameters when generating a key, if necessary */
663 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
665 RSA_PKEY_CTX *rctx = ctx->data;
667 if (!pkey_ctx_is_pss(ctx))
669 /* If all parameters are default values don't set pss */
670 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
672 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
673 rctx->saltlen == -2 ? 0 : rctx->saltlen);
674 if (rsa->pss == NULL)
679 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
682 RSA_PKEY_CTX *rctx = ctx->data;
686 if (rctx->pub_exp == NULL) {
687 rctx->pub_exp = BN_new();
688 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
694 if (ctx->pkey_gencb) {
695 pcb = BN_GENCB_new();
700 evp_pkey_set_cb_translate(pcb, ctx);
703 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
705 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
710 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
716 const EVP_PKEY_METHOD rsa_pkey_meth = {
718 EVP_PKEY_FLAG_AUTOARGLEN,
735 pkey_rsa_verifyrecover,
752 * Called for PSS sign or verify initialisation: checks PSS parameter
753 * sanity and sets any restrictions on key usage.
756 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
759 RSA_PKEY_CTX *rctx = ctx->data;
761 const EVP_MD *mgf1md;
762 int min_saltlen, max_saltlen;
764 /* Should never happen */
765 if (!pkey_ctx_is_pss(ctx))
767 rsa = ctx->pkey->pkey.rsa;
768 /* If no restrictions just return */
769 if (rsa->pss == NULL)
771 /* Get and check parameters */
772 if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
775 /* See if minumum salt length exceeds maximum possible */
776 max_saltlen = RSA_size(rsa) - EVP_MD_size(md);
777 if ((RSA_bits(rsa) & 0x7) == 1)
779 if (min_saltlen > max_saltlen) {
780 RSAerr(RSA_F_PKEY_PSS_INIT, RSA_R_INVALID_SALT_LENGTH);
784 rctx->min_saltlen = min_saltlen;
787 * Set PSS restrictions as defaults: we can then block any attempt to
788 * use invalid values in pkey_rsa_ctrl
792 rctx->mgf1md = mgf1md;
793 rctx->saltlen = min_saltlen;
798 const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
800 EVP_PKEY_FLAG_AUTOARGLEN,
816 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,