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 #ifndef OPENSSL_NO_CMS
68 # include <openssl/cms.h>
71 # include <openssl/fips.h>
76 /* 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;
127 dctx->pub_exp = BN_dup(sctx->pub_exp);
131 dctx->pad_mode = sctx->pad_mode;
136 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
140 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
146 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
148 RSA_PKEY_CTX *rctx = ctx->data;
151 BN_free(rctx->pub_exp);
153 OPENSSL_free(rctx->tbuf);
160 * FIP checker. Return value indicates status of context parameters: 1 :
161 * redirect to FIPS. 0 : don't redirect to FIPS. -1 : illegal operation in
165 static int pkey_fips_check_ctx(EVP_PKEY_CTX *ctx)
167 RSA_PKEY_CTX *rctx = ctx->data;
168 RSA *rsa = ctx->pkey->pkey.rsa;
172 if (rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
174 if (!(rsa->meth->flags & RSA_FLAG_FIPS_METHOD) && rv)
176 if (rctx->md && !(rctx->md->flags & EVP_MD_FLAG_FIPS))
178 if (rctx->mgf1md && !(rctx->mgf1md->flags & EVP_MD_FLAG_FIPS))
184 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
185 size_t *siglen, const unsigned char *tbs,
189 RSA_PKEY_CTX *rctx = ctx->data;
190 RSA *rsa = ctx->pkey->pkey.rsa;
193 ret = pkey_fips_check_ctx(ctx);
195 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
201 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
202 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
208 ret = FIPS_rsa_sign_digest(rsa, tbs, tbslen, rctx->md,
211 rctx->mgf1md, sig, &slen);
220 if (EVP_MD_type(rctx->md) == NID_mdc2) {
222 if (rctx->pad_mode != RSA_PKCS1_PADDING)
224 ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
225 tbs, tbslen, sig, &sltmp, rsa);
230 } else if (rctx->pad_mode == RSA_X931_PADDING) {
231 if (!setup_tbuf(rctx, ctx))
233 memcpy(rctx->tbuf, tbs, tbslen);
234 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
235 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
236 sig, rsa, RSA_X931_PADDING);
237 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
239 ret = RSA_sign(EVP_MD_type(rctx->md),
240 tbs, tbslen, sig, &sltmp, rsa);
244 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
245 if (!setup_tbuf(rctx, ctx))
247 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
249 rctx->md, rctx->mgf1md,
252 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
253 sig, rsa, RSA_NO_PADDING);
257 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
265 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
266 unsigned char *rout, size_t *routlen,
267 const unsigned char *sig, size_t siglen)
270 RSA_PKEY_CTX *rctx = ctx->data;
273 if (rctx->pad_mode == RSA_X931_PADDING) {
274 if (!setup_tbuf(rctx, ctx))
276 ret = RSA_public_decrypt(siglen, sig,
277 rctx->tbuf, ctx->pkey->pkey.rsa,
282 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
283 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
284 RSA_R_ALGORITHM_MISMATCH);
287 if (ret != EVP_MD_size(rctx->md)) {
288 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
289 RSA_R_INVALID_DIGEST_LENGTH);
293 memcpy(rout, rctx->tbuf, ret);
294 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
296 ret = int_rsa_verify(EVP_MD_type(rctx->md),
297 NULL, 0, rout, &sltmp,
298 sig, siglen, ctx->pkey->pkey.rsa);
305 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
313 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
314 const unsigned char *sig, size_t siglen,
315 const unsigned char *tbs, size_t tbslen)
317 RSA_PKEY_CTX *rctx = ctx->data;
318 RSA *rsa = ctx->pkey->pkey.rsa;
322 rv = pkey_fips_check_ctx(ctx);
324 RSAerr(RSA_F_PKEY_RSA_VERIFY,
325 RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE);
332 return FIPS_rsa_verify_digest(rsa,
337 rctx->mgf1md, sig, siglen);
341 if (rctx->pad_mode == RSA_PKCS1_PADDING)
342 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
344 if (rctx->pad_mode == RSA_X931_PADDING) {
345 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
347 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
349 if (!setup_tbuf(rctx, ctx))
351 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
352 rsa, RSA_NO_PADDING);
355 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
356 rctx->md, rctx->mgf1md,
357 rctx->tbuf, rctx->saltlen);
364 if (!setup_tbuf(rctx, ctx))
366 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
367 rsa, rctx->pad_mode);
372 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
379 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
380 unsigned char *out, size_t *outlen,
381 const unsigned char *in, size_t inlen)
384 RSA_PKEY_CTX *rctx = ctx->data;
385 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
393 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
394 unsigned char *out, size_t *outlen,
395 const unsigned char *in, size_t inlen)
398 RSA_PKEY_CTX *rctx = ctx->data;
399 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
407 static int check_padding_md(const EVP_MD *md, int padding)
412 if (padding == RSA_NO_PADDING) {
413 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
417 if (padding == RSA_X931_PADDING) {
418 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) {
419 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
428 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
430 RSA_PKEY_CTX *rctx = ctx->data;
432 case EVP_PKEY_CTRL_RSA_PADDING:
433 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
434 if (!check_padding_md(rctx->md, p1))
436 if (p1 == RSA_PKCS1_PSS_PADDING) {
437 if (!(ctx->operation &
438 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
441 rctx->md = EVP_sha1();
443 if (p1 == RSA_PKCS1_OAEP_PADDING) {
444 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
447 rctx->md = EVP_sha1();
453 RSAerr(RSA_F_PKEY_RSA_CTRL,
454 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
457 case EVP_PKEY_CTRL_GET_RSA_PADDING:
458 *(int *)p2 = rctx->pad_mode;
461 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
462 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
463 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
464 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
467 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
468 *(int *)p2 = rctx->saltlen;
476 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
478 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
484 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
490 case EVP_PKEY_CTRL_MD:
491 if (!check_padding_md(p2, rctx->pad_mode))
496 case EVP_PKEY_CTRL_RSA_MGF1_MD:
497 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
498 if (rctx->pad_mode != RSA_PKCS1_PSS_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;
511 case EVP_PKEY_CTRL_DIGESTINIT:
512 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
513 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
514 case EVP_PKEY_CTRL_PKCS7_SIGN:
516 #ifndef OPENSSL_NO_CMS
517 case EVP_PKEY_CTRL_CMS_DECRYPT:
519 X509_ALGOR *alg = NULL;
520 ASN1_OBJECT *encalg = NULL;
522 CMS_RecipientInfo_ktri_get0_algs(p2, NULL, NULL, &alg);
524 X509_ALGOR_get0(&encalg, NULL, NULL, alg);
525 if (encalg && OBJ_obj2nid(encalg) == NID_rsaesOaep)
526 rctx->pad_mode = RSA_PKCS1_OAEP_PADDING;
528 case EVP_PKEY_CTRL_CMS_ENCRYPT:
529 case EVP_PKEY_CTRL_CMS_SIGN:
532 case EVP_PKEY_CTRL_PEER_KEY:
533 RSAerr(RSA_F_PKEY_RSA_CTRL,
534 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
543 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
544 const char *type, const char *value)
547 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
550 if (!strcmp(type, "rsa_padding_mode")) {
552 if (!strcmp(value, "pkcs1"))
553 pm = RSA_PKCS1_PADDING;
554 else if (!strcmp(value, "sslv23"))
555 pm = RSA_SSLV23_PADDING;
556 else if (!strcmp(value, "none"))
558 else if (!strcmp(value, "oeap"))
559 pm = RSA_PKCS1_OAEP_PADDING;
560 else if (!strcmp(value, "oaep"))
561 pm = RSA_PKCS1_OAEP_PADDING;
562 else if (!strcmp(value, "x931"))
563 pm = RSA_X931_PADDING;
564 else if (!strcmp(value, "pss"))
565 pm = RSA_PKCS1_PSS_PADDING;
567 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
570 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
573 if (!strcmp(type, "rsa_pss_saltlen")) {
575 saltlen = atoi(value);
576 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
579 if (!strcmp(type, "rsa_keygen_bits")) {
582 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
585 if (!strcmp(type, "rsa_keygen_pubexp")) {
587 BIGNUM *pubexp = NULL;
588 if (!BN_asc2bn(&pubexp, value))
590 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
599 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
602 RSA_PKEY_CTX *rctx = ctx->data;
605 if (!rctx->pub_exp) {
606 rctx->pub_exp = BN_new();
607 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
613 if (ctx->pkey_gencb) {
615 evp_pkey_set_cb_translate(pcb, ctx);
618 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
620 EVP_PKEY_assign_RSA(pkey, rsa);
626 const EVP_PKEY_METHOD rsa_pkey_meth = {
628 EVP_PKEY_FLAG_AUTOARGLEN,
645 pkey_rsa_verifyrecover,