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_param(rctx) (rctx->min_saltlen != -1)
50 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
53 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;
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)
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;
238 if (rctx->pad_mode == RSA_PKCS1_PADDING)
239 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
241 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
242 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
245 if (rctx->pad_mode == RSA_X931_PADDING) {
246 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
248 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
250 if (!setup_tbuf(rctx, ctx))
252 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
253 rsa, RSA_NO_PADDING);
256 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
257 rctx->md, rctx->mgf1md,
258 rctx->tbuf, rctx->saltlen);
265 if (!setup_tbuf(rctx, ctx))
267 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
268 rsa, rctx->pad_mode);
273 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
280 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
281 unsigned char *out, size_t *outlen,
282 const unsigned char *in, size_t inlen)
285 RSA_PKEY_CTX *rctx = ctx->data;
286 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
287 int klen = RSA_size(ctx->pkey->pkey.rsa);
288 if (!setup_tbuf(rctx, ctx))
290 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
294 rctx->md, rctx->mgf1md))
296 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
297 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
299 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
307 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
308 unsigned char *out, size_t *outlen,
309 const unsigned char *in, size_t inlen)
312 RSA_PKEY_CTX *rctx = ctx->data;
313 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
315 if (!setup_tbuf(rctx, ctx))
317 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
318 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
321 for (i = 0; i < ret; i++) {
325 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
329 rctx->md, rctx->mgf1md);
331 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
339 static int check_padding_md(const EVP_MD *md, int padding)
345 mdnid = EVP_MD_type(md);
347 if (padding == RSA_NO_PADDING) {
348 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
352 if (padding == RSA_X931_PADDING) {
353 if (RSA_X931_hash_id(mdnid) == -1) {
354 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
359 /* List of all supported RSA digests */
374 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
383 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
385 RSA_PKEY_CTX *rctx = ctx->data;
387 case EVP_PKEY_CTRL_RSA_PADDING:
388 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
389 if (!check_padding_md(rctx->md, p1))
391 if (p1 == RSA_PKCS1_PSS_PADDING) {
392 if (!(ctx->operation &
393 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
396 rctx->md = EVP_sha1();
397 } else if (pkey_ctx_is_pss(ctx)) {
400 if (p1 == RSA_PKCS1_OAEP_PADDING) {
401 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
404 rctx->md = EVP_sha1();
410 RSAerr(RSA_F_PKEY_RSA_CTRL,
411 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
414 case EVP_PKEY_CTRL_GET_RSA_PADDING:
415 *(int *)p2 = rctx->pad_mode;
418 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
419 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
420 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
421 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
424 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) {
425 *(int *)p2 = rctx->saltlen;
429 if (rsa_pss_param(rctx) && p1 < rctx->min_saltlen) {
430 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_PSS_SALTLEN_TOO_SMALL);
437 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
439 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
445 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
446 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
447 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
450 BN_free(rctx->pub_exp);
454 case EVP_PKEY_CTRL_RSA_OAEP_MD:
455 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
456 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
457 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
460 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
461 *(const EVP_MD **)p2 = rctx->md;
466 case EVP_PKEY_CTRL_MD:
467 if (!check_padding_md(p2, rctx->pad_mode))
469 if (rsa_pss_param(rctx)) {
470 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
472 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_DIGEST_NOT_ALLOWED);
478 case EVP_PKEY_CTRL_GET_MD:
479 *(const EVP_MD **)p2 = rctx->md;
482 case EVP_PKEY_CTRL_RSA_MGF1_MD:
483 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
484 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
485 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
486 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
489 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
491 *(const EVP_MD **)p2 = rctx->mgf1md;
493 *(const EVP_MD **)p2 = rctx->md;
495 if (rsa_pss_param(rctx)) {
496 if (EVP_MD_type(rctx->md) == EVP_MD_type(p2))
498 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_MGF1_DIGEST_NOT_ALLOWED);
505 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
506 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
507 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
510 OPENSSL_free(rctx->oaep_label);
512 rctx->oaep_label = p2;
513 rctx->oaep_labellen = p1;
515 rctx->oaep_label = NULL;
516 rctx->oaep_labellen = 0;
520 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
521 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
522 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
525 *(unsigned char **)p2 = rctx->oaep_label;
526 return rctx->oaep_labellen;
528 case EVP_PKEY_CTRL_DIGESTINIT:
529 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
530 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
531 case EVP_PKEY_CTRL_PKCS7_SIGN:
533 #ifndef OPENSSL_NO_CMS
534 case EVP_PKEY_CTRL_CMS_DECRYPT:
535 case EVP_PKEY_CTRL_CMS_ENCRYPT:
536 case EVP_PKEY_CTRL_CMS_SIGN:
539 case EVP_PKEY_CTRL_PEER_KEY:
540 RSAerr(RSA_F_PKEY_RSA_CTRL,
541 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
550 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
551 const char *type, const char *value)
554 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
557 if (strcmp(type, "rsa_padding_mode") == 0) {
559 if (strcmp(value, "pkcs1") == 0)
560 pm = RSA_PKCS1_PADDING;
561 else if (strcmp(value, "sslv23") == 0)
562 pm = RSA_SSLV23_PADDING;
563 else if (strcmp(value, "none") == 0)
565 else if (strcmp(value, "oeap") == 0)
566 pm = RSA_PKCS1_OAEP_PADDING;
567 else if (strcmp(value, "oaep") == 0)
568 pm = RSA_PKCS1_OAEP_PADDING;
569 else if (strcmp(value, "x931") == 0)
570 pm = RSA_X931_PADDING;
571 else if (strcmp(value, "pss") == 0)
572 pm = RSA_PKCS1_PSS_PADDING;
574 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
577 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
580 if (strcmp(type, "rsa_pss_saltlen") == 0) {
582 saltlen = atoi(value);
583 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
586 if (strcmp(type, "rsa_keygen_bits") == 0) {
589 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
592 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
594 BIGNUM *pubexp = NULL;
595 if (!BN_asc2bn(&pubexp, value))
597 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
603 if (strcmp(type, "rsa_mgf1_md") == 0)
604 return EVP_PKEY_CTX_md(ctx,
605 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
606 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
608 if (pkey_ctx_is_pss(ctx)) {
610 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
611 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
612 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
614 if (strcmp(type, "rsa_pss_keygen_md") == 0)
615 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
616 EVP_PKEY_CTRL_MD, value);
618 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
620 saltlen = atoi(value);
621 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
625 if (strcmp(type, "rsa_oaep_md") == 0)
626 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
627 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
629 if (strcmp(type, "rsa_oaep_label") == 0) {
633 lab = OPENSSL_hexstr2buf(value, &lablen);
636 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
645 /* Set PSS parameters when generating a key, if necessary */
646 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
648 RSA_PKEY_CTX *rctx = ctx->data;
649 if (!pkey_ctx_is_pss(ctx))
651 /* If all parameters are default values don't set pss */
652 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
654 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
655 rctx->saltlen == -2 ? 0 : rctx->saltlen);
656 if (rsa->pss == NULL)
661 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
664 RSA_PKEY_CTX *rctx = ctx->data;
667 if (rctx->pub_exp == NULL) {
668 rctx->pub_exp = BN_new();
669 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
675 if (ctx->pkey_gencb) {
676 pcb = BN_GENCB_new();
681 evp_pkey_set_cb_translate(pcb, ctx);
684 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
686 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
691 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
697 const EVP_PKEY_METHOD rsa_pkey_meth = {
699 EVP_PKEY_FLAG_AUTOARGLEN,
716 pkey_rsa_verifyrecover,
733 * Called for PSS sign or verify initialisation: checks PSS parameter
734 * sanity and sets any restrictions on key usage.
737 static int pkey_pss_init(EVP_PKEY_CTX *ctx)
740 RSA_PKEY_CTX *rctx = ctx->data;
742 const EVP_MD *mgf1md;
744 /* Should never happen */
745 if (!pkey_ctx_is_pss(ctx))
747 rsa = ctx->pkey->pkey.rsa;
748 /* If no restrictions just return */
749 if (rsa->pss == NULL)
751 /* Get and check parameters */
752 if (!rsa_pss_get_param(rsa->pss, &md, &mgf1md, &min_saltlen))
755 rctx->min_saltlen = min_saltlen;
758 * Set PSS restrictions as defaults: we can then block any attempt to
759 * use invalid values in pkey_rsa_ctrl
763 rctx->mgf1md = mgf1md;
764 rctx->saltlen = min_saltlen;
769 const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
771 EVP_PKEY_FLAG_AUTOARGLEN,
787 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,