1 /* crypto/rsa/rsa_pmeth.c */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/asn1t.h>
63 #include <openssl/x509.h>
64 #include <openssl/rsa.h>
65 #include <openssl/bn.h>
66 #include <openssl/evp.h>
67 #include <openssl/x509v3.h>
68 #ifndef OPENSSL_NO_CMS
69 # include <openssl/cms.h>
72 # include <openssl/fips.h>
77 /* RSA pkey context structure */
80 /* Key gen parameters */
83 /* Keygen callback info */
85 /* RSA padding mode */
89 /* message digest for MGF1 */
96 unsigned char *oaep_label;
100 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
103 rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
107 rctx->pub_exp = NULL;
108 rctx->pad_mode = RSA_PKCS1_PADDING;
115 rctx->oaep_label = NULL;
116 rctx->oaep_labellen = 0;
119 ctx->keygen_info = rctx->gentmp;
120 ctx->keygen_info_count = 2;
125 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
127 RSA_PKEY_CTX *dctx, *sctx;
128 if (!pkey_rsa_init(dst))
132 dctx->nbits = sctx->nbits;
134 dctx->pub_exp = BN_dup(sctx->pub_exp);
138 dctx->pad_mode = sctx->pad_mode;
140 dctx->mgf1md = sctx->mgf1md;
141 if (sctx->oaep_label) {
142 if (dctx->oaep_label)
143 OPENSSL_free(dctx->oaep_label);
144 dctx->oaep_label = BUF_memdup(sctx->oaep_label, sctx->oaep_labellen);
145 if (!dctx->oaep_label)
147 dctx->oaep_labellen = sctx->oaep_labellen;
152 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
156 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
162 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
164 RSA_PKEY_CTX *rctx = ctx->data;
167 BN_free(rctx->pub_exp);
169 OPENSSL_free(rctx->tbuf);
170 if (rctx->oaep_label)
171 OPENSSL_free(rctx->oaep_label);
178 * FIP checker. Return value indicates status of context parameters: 1 :
179 * redirect to FIPS. 0 : don't redirect to FIPS. -1 : illegal operation in
183 static int pkey_fips_check_ctx(EVP_PKEY_CTX *ctx)
185 RSA_PKEY_CTX *rctx = ctx->data;
186 RSA *rsa = ctx->pkey->pkey.rsa;
190 if (rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
192 if (!(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) && rv)
196 fmd = FIPS_get_digestbynid(EVP_MD_type(rctx->md));
197 if (!fmd || !(fmd->flags & EVP_MD_FLAG_FIPS))
200 if (rctx->mgf1md && !(rctx->mgf1md->flags & EVP_MD_FLAG_FIPS)) {
202 fmd = FIPS_get_digestbynid(EVP_MD_type(rctx->mgf1md));
203 if (!fmd || !(fmd->flags & EVP_MD_FLAG_FIPS))
210 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
211 size_t *siglen, const unsigned char *tbs,
215 RSA_PKEY_CTX *rctx = ctx->data;
216 RSA *rsa = ctx->pkey->pkey.rsa;
219 ret = pkey_fips_check_ctx(ctx);
221 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
227 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
228 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
234 ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, rctx->md,
237 rctx->mgf1md, sig, &slen);
246 if (EVP_MD_type(rctx->md) == NID_mdc2) {
248 if (rctx->pad_mode != RSA_PKCS1_PADDING)
250 ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
251 tbs, tbslen, sig, &sltmp, rsa);
256 } else if (rctx->pad_mode == RSA_X931_PADDING) {
257 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
258 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
261 if (!setup_tbuf(rctx, ctx)) {
262 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
265 memcpy(rctx->tbuf, tbs, tbslen);
266 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
267 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
268 sig, rsa, RSA_X931_PADDING);
269 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
271 ret = RSA_sign(EVP_MD_type(rctx->md),
272 tbs, tbslen, sig, &sltmp, rsa);
276 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
277 if (!setup_tbuf(rctx, ctx))
279 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
281 rctx->md, rctx->mgf1md,
284 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
285 sig, rsa, RSA_NO_PADDING);
289 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
297 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
298 unsigned char *rout, size_t *routlen,
299 const unsigned char *sig, size_t siglen)
302 RSA_PKEY_CTX *rctx = ctx->data;
305 if (rctx->pad_mode == RSA_X931_PADDING) {
306 if (!setup_tbuf(rctx, ctx))
308 ret = RSA_public_decrypt(siglen, sig,
309 rctx->tbuf, ctx->pkey->pkey.rsa,
314 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
315 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
316 RSA_R_ALGORITHM_MISMATCH);
319 if (ret != EVP_MD_size(rctx->md)) {
320 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
321 RSA_R_INVALID_DIGEST_LENGTH);
325 memcpy(rout, rctx->tbuf, ret);
326 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
328 ret = int_rsa_verify(EVP_MD_type(rctx->md),
329 NULL, 0, rout, &sltmp,
330 sig, siglen, ctx->pkey->pkey.rsa);
337 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
345 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
346 const unsigned char *sig, size_t siglen,
347 const unsigned char *tbs, size_t tbslen)
349 RSA_PKEY_CTX *rctx = ctx->data;
350 RSA *rsa = ctx->pkey->pkey.rsa;
354 rv = pkey_fips_check_ctx(ctx);
356 RSAerr(RSA_F_PKEY_RSA_VERIFY,
357 RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
364 return FIPS_rsa_verify_digest(rsa,
369 rctx->mgf1md, sig, siglen);
373 if (rctx->pad_mode == RSA_PKCS1_PADDING)
374 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
376 if (rctx->pad_mode == RSA_X931_PADDING) {
377 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
379 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
381 if (!setup_tbuf(rctx, ctx))
383 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
384 rsa, RSA_NO_PADDING);
387 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
388 rctx->md, rctx->mgf1md,
389 rctx->tbuf, rctx->saltlen);
396 if (!setup_tbuf(rctx, ctx))
398 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
399 rsa, rctx->pad_mode);
404 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
411 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
412 unsigned char *out, size_t *outlen,
413 const unsigned char *in, size_t inlen)
416 RSA_PKEY_CTX *rctx = ctx->data;
417 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
418 int klen = RSA_size(ctx->pkey->pkey.rsa);
419 if (!setup_tbuf(rctx, ctx))
421 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
425 rctx->md, rctx->mgf1md))
427 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
428 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
430 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
438 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
439 unsigned char *out, size_t *outlen,
440 const unsigned char *in, size_t inlen)
443 RSA_PKEY_CTX *rctx = ctx->data;
444 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
446 if (!setup_tbuf(rctx, ctx))
448 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
449 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
452 for (i = 0; i < ret; i++) {
456 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
460 rctx->md, rctx->mgf1md);
462 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
470 static int check_padding_md(const EVP_MD *md, int padding)
475 if (padding == RSA_NO_PADDING) {
476 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
480 if (padding == RSA_X931_PADDING) {
481 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
482 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
491 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
493 RSA_PKEY_CTX *rctx = ctx->data;
495 case EVP_PKEY_CTRL_RSA_PADDING:
496 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
497 if (!check_padding_md(rctx->md, p1))
499 if (p1 == RSA_PKCS1_PSS_PADDING) {
500 if (!(ctx->operation &
501 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
504 rctx->md = EVP_sha1();
506 if (p1 == RSA_PKCS1_OAEP_PADDING) {
507 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
510 rctx->md = EVP_sha1();
516 RSAerr(RSA_F_PKEY_RSA_CTRL,
517 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
520 case EVP_PKEY_CTRL_GET_RSA_PADDING:
521 *(int *)p2 = rctx->pad_mode;
524 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
525 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
526 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
527 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
530 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
531 *(int *)p2 = rctx->saltlen;
539 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
541 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
547 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
550 BN_free(rctx->pub_exp);
554 case EVP_PKEY_CTRL_RSA_OAEP_MD:
555 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
556 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
557 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
560 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
561 *(const EVP_MD **)p2 = rctx->md;
566 case EVP_PKEY_CTRL_MD:
567 if (!check_padding_md(p2, rctx->pad_mode))
572 case EVP_PKEY_CTRL_GET_MD:
573 *(const EVP_MD **)p2 = rctx->md;
576 case EVP_PKEY_CTRL_RSA_MGF1_MD:
577 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
578 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
579 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
580 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
583 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
585 *(const EVP_MD **)p2 = rctx->mgf1md;
587 *(const EVP_MD **)p2 = rctx->md;
592 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
593 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
594 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
597 if (rctx->oaep_label)
598 OPENSSL_free(rctx->oaep_label);
600 rctx->oaep_label = p2;
601 rctx->oaep_labellen = p1;
603 rctx->oaep_label = NULL;
604 rctx->oaep_labellen = 0;
608 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
609 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
610 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
613 *(unsigned char **)p2 = rctx->oaep_label;
614 return rctx->oaep_labellen;
616 case EVP_PKEY_CTRL_DIGESTINIT:
617 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
618 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
619 case EVP_PKEY_CTRL_PKCS7_SIGN:
621 #ifndef OPENSSL_NO_CMS
622 case EVP_PKEY_CTRL_CMS_DECRYPT:
623 case EVP_PKEY_CTRL_CMS_ENCRYPT:
624 case EVP_PKEY_CTRL_CMS_SIGN:
627 case EVP_PKEY_CTRL_PEER_KEY:
628 RSAerr(RSA_F_PKEY_RSA_CTRL,
629 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
638 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
639 const char *type, const char *value)
642 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
645 if (!strcmp(type, "rsa_padding_mode")) {
647 if (!strcmp(value, "pkcs1"))
648 pm = RSA_PKCS1_PADDING;
649 else if (!strcmp(value, "sslv23"))
650 pm = RSA_SSLV23_PADDING;
651 else if (!strcmp(value, "none"))
653 else if (!strcmp(value, "oeap"))
654 pm = RSA_PKCS1_OAEP_PADDING;
655 else if (!strcmp(value, "oaep"))
656 pm = RSA_PKCS1_OAEP_PADDING;
657 else if (!strcmp(value, "x931"))
658 pm = RSA_X931_PADDING;
659 else if (!strcmp(value, "pss"))
660 pm = RSA_PKCS1_PSS_PADDING;
662 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
665 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
668 if (!strcmp(type, "rsa_pss_saltlen")) {
670 saltlen = atoi(value);
671 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
674 if (!strcmp(type, "rsa_keygen_bits")) {
677 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
680 if (!strcmp(type, "rsa_keygen_pubexp")) {
682 BIGNUM *pubexp = NULL;
683 if (!BN_asc2bn(&pubexp, value))
685 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
691 if (!strcmp(type, "rsa_mgf1_md")) {
693 if (!(md = EVP_get_digestbyname(value))) {
694 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
697 return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
700 if (!strcmp(type, "rsa_oaep_md")) {
702 if (!(md = EVP_get_digestbyname(value))) {
703 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
706 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
708 if (!strcmp(type, "rsa_oaep_label")) {
712 lab = string_to_hex(value, &lablen);
715 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
724 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
727 RSA_PKEY_CTX *rctx = ctx->data;
730 if (!rctx->pub_exp) {
731 rctx->pub_exp = BN_new();
732 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
738 if (ctx->pkey_gencb) {
740 evp_pkey_set_cb_translate(pcb, ctx);
743 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
745 EVP_PKEY_assign_RSA(pkey, rsa);
751 const EVP_PKEY_METHOD rsa_pkey_meth = {
753 EVP_PKEY_FLAG_AUTOARGLEN,
770 pkey_rsa_verifyrecover,