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 #include <openssl/x509v3.h>
67 #ifndef OPENSSL_NO_CMS
68 #include <openssl/cms.h>
73 /* RSA pkey context structure */
77 /* Key gen parameters */
80 /* Keygen callback info */
82 /* RSA padding mode */
86 /* message digest for MGF1 */
93 unsigned char *oaep_label;
97 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
100 rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
104 rctx->pub_exp = NULL;
105 rctx->pad_mode = RSA_PKCS1_PADDING;
112 rctx->oaep_label = NULL;
113 rctx->oaep_labellen = 0;
116 ctx->keygen_info = rctx->gentmp;
117 ctx->keygen_info_count = 2;
122 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
124 RSA_PKEY_CTX *dctx, *sctx;
125 if (!pkey_rsa_init(dst))
129 dctx->nbits = sctx->nbits;
132 dctx->pub_exp = BN_dup(sctx->pub_exp);
136 dctx->pad_mode = sctx->pad_mode;
138 dctx->mgf1md = sctx->mgf1md;
139 if (sctx->oaep_label)
141 if (dctx->oaep_label)
142 OPENSSL_free(dctx->oaep_label);
143 dctx->oaep_label = BUF_memdup(sctx->oaep_label,
144 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;
168 BN_free(rctx->pub_exp);
170 OPENSSL_free(rctx->tbuf);
171 if (rctx->oaep_label)
172 OPENSSL_free(rctx->oaep_label);
177 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
178 const unsigned char *tbs, size_t tbslen)
181 RSA_PKEY_CTX *rctx = ctx->data;
182 RSA *rsa = ctx->pkey->pkey.rsa;
186 if (tbslen != (size_t)EVP_MD_size(rctx->md))
188 RSAerr(RSA_F_PKEY_RSA_SIGN,
189 RSA_R_INVALID_DIGEST_LENGTH);
193 if (EVP_MD_type(rctx->md) == NID_mdc2)
196 if (rctx->pad_mode != RSA_PKCS1_PADDING)
198 ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2,
199 tbs, tbslen, sig, &sltmp, rsa);
205 else if (rctx->pad_mode == RSA_X931_PADDING)
207 if (!setup_tbuf(rctx, ctx))
209 memcpy(rctx->tbuf, tbs, tbslen);
211 RSA_X931_hash_id(EVP_MD_type(rctx->md));
212 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
213 sig, rsa, RSA_X931_PADDING);
215 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
218 ret = RSA_sign(EVP_MD_type(rctx->md),
219 tbs, tbslen, sig, &sltmp, rsa);
224 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
226 if (!setup_tbuf(rctx, ctx))
228 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
230 rctx->md, rctx->mgf1md,
233 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
234 sig, rsa, RSA_NO_PADDING);
240 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
249 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
250 unsigned char *rout, size_t *routlen,
251 const unsigned char *sig, size_t siglen)
254 RSA_PKEY_CTX *rctx = ctx->data;
258 if (rctx->pad_mode == RSA_X931_PADDING)
260 if (!setup_tbuf(rctx, ctx))
262 ret = RSA_public_decrypt(siglen, sig,
263 rctx->tbuf, ctx->pkey->pkey.rsa,
268 if (rctx->tbuf[ret] !=
269 RSA_X931_hash_id(EVP_MD_type(rctx->md)))
271 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
272 RSA_R_ALGORITHM_MISMATCH);
275 if (ret != EVP_MD_size(rctx->md))
277 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
278 RSA_R_INVALID_DIGEST_LENGTH);
282 memcpy(rout, rctx->tbuf, ret);
284 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
287 ret = int_rsa_verify(EVP_MD_type(rctx->md),
288 NULL, 0, rout, &sltmp,
289 sig, siglen, ctx->pkey->pkey.rsa);
298 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
306 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
307 const unsigned char *sig, size_t siglen,
308 const unsigned char *tbs, size_t tbslen)
310 RSA_PKEY_CTX *rctx = ctx->data;
311 RSA *rsa = ctx->pkey->pkey.rsa;
315 if (rctx->pad_mode == RSA_PKCS1_PADDING)
316 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
318 if (rctx->pad_mode == RSA_X931_PADDING)
320 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen,
324 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
327 if (!setup_tbuf(rctx, ctx))
329 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
330 rsa, RSA_NO_PADDING);
333 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
334 rctx->md, rctx->mgf1md,
335 rctx->tbuf, rctx->saltlen);
345 if (!setup_tbuf(rctx, ctx))
347 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
348 rsa, rctx->pad_mode);
353 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
360 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
361 unsigned char *out, size_t *outlen,
362 const unsigned char *in, size_t inlen)
365 RSA_PKEY_CTX *rctx = ctx->data;
366 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING)
368 int klen = RSA_size(ctx->pkey->pkey.rsa);
369 if (!setup_tbuf(rctx, ctx))
371 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
375 rctx->md, rctx->mgf1md))
377 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
382 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
390 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
391 unsigned char *out, size_t *outlen,
392 const unsigned char *in, size_t inlen)
395 RSA_PKEY_CTX *rctx = ctx->data;
396 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING)
399 if (!setup_tbuf(rctx, ctx))
401 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
406 for (i = 0; i < ret; i++)
411 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out,ret,rctx->tbuf + i,
415 rctx->md, rctx->mgf1md);
418 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
426 static int check_padding_md(const EVP_MD *md, int padding)
431 if (padding == RSA_NO_PADDING)
433 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
437 if (padding == RSA_X931_PADDING)
439 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1)
441 RSAerr(RSA_F_CHECK_PADDING_MD,
442 RSA_R_INVALID_X931_DIGEST);
452 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
454 RSA_PKEY_CTX *rctx = ctx->data;
457 case EVP_PKEY_CTRL_RSA_PADDING:
458 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING))
460 if (!check_padding_md(rctx->md, p1))
462 if (p1 == RSA_PKCS1_PSS_PADDING)
464 if (!(ctx->operation &
465 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
468 rctx->md = EVP_sha1();
470 if (p1 == RSA_PKCS1_OAEP_PADDING)
472 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
475 rctx->md = EVP_sha1();
481 RSAerr(RSA_F_PKEY_RSA_CTRL,
482 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
485 case EVP_PKEY_CTRL_GET_RSA_PADDING:
486 *(int *)p2 = rctx->pad_mode;
489 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
490 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
491 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
493 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
496 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
497 *(int *)p2 = rctx->saltlen;
506 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
509 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
515 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
518 BN_free(rctx->pub_exp);
522 case EVP_PKEY_CTRL_RSA_OAEP_MD:
523 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING)
525 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
531 case EVP_PKEY_CTRL_MD:
532 if (!check_padding_md(p2, rctx->pad_mode))
537 case EVP_PKEY_CTRL_GET_MD:
538 *(const EVP_MD **)p2 = rctx->md;
541 case EVP_PKEY_CTRL_RSA_MGF1_MD:
542 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
543 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
544 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING)
546 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
549 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD)
552 *(const EVP_MD **)p2 = rctx->mgf1md;
554 *(const EVP_MD **)p2 = rctx->md;
560 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
561 OPENSSL_free(rctx->oaep_label);
562 rctx->oaep_label = p2;
563 rctx->oaep_labellen = p1;
566 case EVP_PKEY_CTRL_DIGESTINIT:
567 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
568 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
569 case EVP_PKEY_CTRL_PKCS7_SIGN:
571 #ifndef OPENSSL_NO_CMS
572 case EVP_PKEY_CTRL_CMS_DECRYPT:
574 X509_ALGOR *alg = NULL;
575 ASN1_OBJECT *encalg = NULL;
577 CMS_RecipientInfo_ktri_get0_algs(p2, NULL, NULL, &alg);
579 X509_ALGOR_get0(&encalg, NULL, NULL, alg);
580 if (encalg && OBJ_obj2nid(encalg) == NID_rsaesOaep)
581 rctx->pad_mode = RSA_PKCS1_OAEP_PADDING;
583 case EVP_PKEY_CTRL_CMS_ENCRYPT:
584 case EVP_PKEY_CTRL_CMS_SIGN:
587 case EVP_PKEY_CTRL_PEER_KEY:
588 RSAerr(RSA_F_PKEY_RSA_CTRL,
589 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
598 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
599 const char *type, const char *value)
603 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
606 if (!strcmp(type, "rsa_padding_mode"))
609 if (!strcmp(value, "pkcs1"))
610 pm = RSA_PKCS1_PADDING;
611 else if (!strcmp(value, "sslv23"))
612 pm = RSA_SSLV23_PADDING;
613 else if (!strcmp(value, "none"))
615 else if (!strcmp(value, "oeap"))
616 pm = RSA_PKCS1_OAEP_PADDING;
617 else if (!strcmp(value, "oaep"))
618 pm = RSA_PKCS1_OAEP_PADDING;
619 else if (!strcmp(value, "x931"))
620 pm = RSA_X931_PADDING;
621 else if (!strcmp(value, "pss"))
622 pm = RSA_PKCS1_PSS_PADDING;
625 RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
626 RSA_R_UNKNOWN_PADDING_TYPE);
629 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
632 if (!strcmp(type, "rsa_pss_saltlen"))
635 saltlen = atoi(value);
636 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
639 if (!strcmp(type, "rsa_keygen_bits"))
643 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
646 if (!strcmp(type, "rsa_keygen_pubexp"))
649 BIGNUM *pubexp = NULL;
650 if (!BN_asc2bn(&pubexp, value))
652 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
658 if (!strcmp(type, "rsa_mgf1_md"))
661 if (!(md = EVP_get_digestbyname(value)))
663 RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
664 RSA_R_INVALID_DIGEST);
667 return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
670 if (!strcmp(type, "rsa_oaep_md"))
673 if (!(md = EVP_get_digestbyname(value)))
675 RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
676 RSA_R_INVALID_DIGEST);
679 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
681 if (!strcmp(type, "rsa_oaep_label"))
686 lab = string_to_hex(value, &lablen);
689 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
698 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
701 RSA_PKEY_CTX *rctx = ctx->data;
706 rctx->pub_exp = BN_new();
707 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
716 evp_pkey_set_cb_translate(pcb, ctx);
720 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
722 EVP_PKEY_assign_RSA(pkey, rsa);
728 const EVP_PKEY_METHOD rsa_pkey_meth =
731 EVP_PKEY_FLAG_AUTOARGLEN,
748 pkey_rsa_verifyrecover,