1 /* crypto/rsa/rsa_pmeth.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/asn1t.h>
62 #include <openssl/x509.h>
63 #include <openssl/rsa.h>
64 #include <openssl/bn.h>
65 #include <openssl/evp.h>
66 #ifndef OPENSSL_NO_CMS
67 #include <openssl/cms.h>
70 #include <openssl/fips.h>
75 /* RSA pkey context structure */
79 /* Key gen parameters */
82 /* Keygen callback info */
84 /* RSA padding mode */
88 /* message digest for MGF1 */
90 /* PSS/OAEP salt length */
96 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
99 rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
103 rctx->pub_exp = NULL;
104 rctx->pad_mode = RSA_PKCS1_PADDING;
112 ctx->keygen_info = rctx->gentmp;
113 ctx->keygen_info_count = 2;
118 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
120 RSA_PKEY_CTX *dctx, *sctx;
121 if (!pkey_rsa_init(dst))
125 dctx->nbits = sctx->nbits;
128 dctx->pub_exp = BN_dup(sctx->pub_exp);
132 dctx->pad_mode = sctx->pad_mode;
137 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
141 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
147 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
149 RSA_PKEY_CTX *rctx = ctx->data;
153 BN_free(rctx->pub_exp);
155 OPENSSL_free(rctx->tbuf);
160 /* FIP checker. Return value indicates status of context parameters:
161 * 1 : redirect to FIPS.
162 * 0 : don't redirect to FIPS.
163 * -1 : illegal operation in FIPS mode.
166 static int pkey_fips_check_ctx(EVP_PKEY_CTX *ctx)
168 RSA_PKEY_CTX *rctx = ctx->data;
169 RSA *rsa = ctx->pkey->pkey.rsa;
173 if (rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
175 if (!(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) && rv)
177 if (rctx->md && !(rctx->md->flags & EVP_MD_FLAG_FIPS))
179 if (rctx->mgf1md && !(rctx->mgf1md->flags & EVP_MD_FLAG_FIPS))
185 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
186 const unsigned char *tbs, size_t tbslen)
189 RSA_PKEY_CTX *rctx = ctx->data;
190 RSA *rsa = ctx->pkey->pkey.rsa;
193 ret = pkey_fips_check_ctx(ctx);
196 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
203 if (tbslen != (size_t)EVP_MD_size(rctx->md))
205 RSAerr(RSA_F_PKEY_RSA_SIGN,
206 RSA_R_INVALID_DIGEST_LENGTH);
213 ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, rctx->md,
225 if (rctx->pad_mode == RSA_X931_PADDING)
227 if (!setup_tbuf(rctx, ctx))
229 memcpy(rctx->tbuf, tbs, tbslen);
231 RSA_X931_hash_id(EVP_MD_type(rctx->md));
232 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
233 sig, rsa, RSA_X931_PADDING);
235 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
238 ret = RSA_sign(EVP_MD_type(rctx->md),
239 tbs, tbslen, sig, &sltmp, rsa);
244 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
246 if (!setup_tbuf(rctx, ctx))
248 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
250 rctx->md, rctx->mgf1md,
253 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
254 sig, rsa, RSA_NO_PADDING);
260 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
269 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
270 unsigned char *rout, size_t *routlen,
271 const unsigned char *sig, size_t siglen)
274 RSA_PKEY_CTX *rctx = ctx->data;
278 if (rctx->pad_mode == RSA_X931_PADDING)
280 if (!setup_tbuf(rctx, ctx))
282 ret = RSA_public_decrypt(siglen, sig,
283 rctx->tbuf, ctx->pkey->pkey.rsa,
288 if (rctx->tbuf[ret] !=
289 RSA_X931_hash_id(EVP_MD_type(rctx->md)))
291 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
292 RSA_R_ALGORITHM_MISMATCH);
295 if (ret != EVP_MD_size(rctx->md))
297 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
298 RSA_R_INVALID_DIGEST_LENGTH);
302 memcpy(rout, rctx->tbuf, ret);
304 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
307 ret = int_rsa_verify(EVP_MD_type(rctx->md),
308 NULL, 0, rout, &sltmp,
309 sig, siglen, ctx->pkey->pkey.rsa);
318 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
326 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
327 const unsigned char *sig, size_t siglen,
328 const unsigned char *tbs, size_t tbslen)
330 RSA_PKEY_CTX *rctx = ctx->data;
331 RSA *rsa = ctx->pkey->pkey.rsa;
335 rv = pkey_fips_check_ctx(ctx);
338 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
347 return FIPS_rsa_verify_digest(rsa,
357 if (rctx->pad_mode == RSA_PKCS1_PADDING)
358 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
360 if (rctx->pad_mode == RSA_X931_PADDING)
362 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen,
366 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
369 if (!setup_tbuf(rctx, ctx))
371 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
372 rsa, RSA_NO_PADDING);
375 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
376 rctx->md, rctx->mgf1md,
377 rctx->tbuf, rctx->saltlen);
387 if (!setup_tbuf(rctx, ctx))
389 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
390 rsa, rctx->pad_mode);
395 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
403 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
404 unsigned char *out, size_t *outlen,
405 const unsigned char *in, size_t inlen)
408 RSA_PKEY_CTX *rctx = ctx->data;
409 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
417 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
418 unsigned char *out, size_t *outlen,
419 const unsigned char *in, size_t inlen)
422 RSA_PKEY_CTX *rctx = ctx->data;
423 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
431 static int check_padding_md(const EVP_MD *md, int padding)
436 if (padding == RSA_NO_PADDING)
438 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
442 if (padding == RSA_X931_PADDING)
444 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1)
446 RSAerr(RSA_F_CHECK_PADDING_MD,
447 RSA_R_INVALID_X931_DIGEST);
457 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
459 RSA_PKEY_CTX *rctx = ctx->data;
462 case EVP_PKEY_CTRL_RSA_PADDING:
463 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING))
465 if (!check_padding_md(rctx->md, p1))
467 if (p1 == RSA_PKCS1_PSS_PADDING)
469 if (!(ctx->operation &
470 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
473 rctx->md = EVP_sha1();
475 if (p1 == RSA_PKCS1_OAEP_PADDING)
477 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
480 rctx->md = EVP_sha1();
486 RSAerr(RSA_F_PKEY_RSA_CTRL,
487 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
490 case EVP_PKEY_CTRL_GET_RSA_PADDING:
491 *(int *)p2 = rctx->pad_mode;
494 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
495 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
496 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
498 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
501 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
502 *(int *)p2 = rctx->saltlen;
511 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
514 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
520 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
526 case EVP_PKEY_CTRL_MD:
527 if (!check_padding_md(p2, rctx->pad_mode))
532 case EVP_PKEY_CTRL_RSA_MGF1_MD:
533 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
534 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
536 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
539 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD)
542 *(const EVP_MD **)p2 = rctx->mgf1md;
544 *(const EVP_MD **)p2 = rctx->md;
550 case EVP_PKEY_CTRL_DIGESTINIT:
551 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
552 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
553 case EVP_PKEY_CTRL_PKCS7_SIGN:
555 #ifndef OPENSSL_NO_CMS
556 case EVP_PKEY_CTRL_CMS_DECRYPT:
558 X509_ALGOR *alg = NULL;
559 ASN1_OBJECT *encalg = NULL;
561 CMS_RecipientInfo_ktri_get0_algs(p2, NULL, NULL, &alg);
563 X509_ALGOR_get0(&encalg, NULL, NULL, alg);
564 if (encalg && OBJ_obj2nid(encalg) == NID_rsaesOaep)
565 rctx->pad_mode = RSA_PKCS1_OAEP_PADDING;
567 case EVP_PKEY_CTRL_CMS_ENCRYPT:
568 case EVP_PKEY_CTRL_CMS_SIGN:
571 case EVP_PKEY_CTRL_PEER_KEY:
572 RSAerr(RSA_F_PKEY_RSA_CTRL,
573 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
582 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
583 const char *type, const char *value)
587 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
590 if (!strcmp(type, "rsa_padding_mode"))
593 if (!strcmp(value, "pkcs1"))
594 pm = RSA_PKCS1_PADDING;
595 else if (!strcmp(value, "sslv23"))
596 pm = RSA_SSLV23_PADDING;
597 else if (!strcmp(value, "none"))
599 else if (!strcmp(value, "oeap"))
600 pm = RSA_PKCS1_OAEP_PADDING;
601 else if (!strcmp(value, "x931"))
602 pm = RSA_X931_PADDING;
603 else if (!strcmp(value, "pss"))
604 pm = RSA_PKCS1_PSS_PADDING;
607 RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
608 RSA_R_UNKNOWN_PADDING_TYPE);
611 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
614 if (!strcmp(type, "rsa_pss_saltlen"))
617 saltlen = atoi(value);
618 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
621 if (!strcmp(type, "rsa_keygen_bits"))
625 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
628 if (!strcmp(type, "rsa_keygen_pubexp"))
631 BIGNUM *pubexp = NULL;
632 if (!BN_asc2bn(&pubexp, value))
634 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
643 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
646 RSA_PKEY_CTX *rctx = ctx->data;
651 rctx->pub_exp = BN_new();
652 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
661 evp_pkey_set_cb_translate(pcb, ctx);
665 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
667 EVP_PKEY_assign_RSA(pkey, rsa);
673 const EVP_PKEY_METHOD rsa_pkey_meth =
676 EVP_PKEY_FLAG_AUTOARGLEN,
693 pkey_rsa_verifyrecover,