2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
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).
60 #include "internal/cryptlib.h"
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>
70 #include "internal/evp_int.h"
73 /* RSA pkey context structure */
76 /* Key gen parameters */
79 /* Keygen callback info */
81 /* RSA padding mode */
85 /* message digest for MGF1 */
92 unsigned char *oaep_label;
96 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
99 rctx = OPENSSL_zalloc(sizeof(*rctx));
103 rctx->pad_mode = RSA_PKCS1_PADDING;
106 ctx->keygen_info = rctx->gentmp;
107 ctx->keygen_info_count = 2;
112 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
114 RSA_PKEY_CTX *dctx, *sctx;
115 if (!pkey_rsa_init(dst))
119 dctx->nbits = sctx->nbits;
121 dctx->pub_exp = BN_dup(sctx->pub_exp);
125 dctx->pad_mode = sctx->pad_mode;
127 dctx->mgf1md = sctx->mgf1md;
128 if (sctx->oaep_label) {
129 OPENSSL_free(dctx->oaep_label);
130 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
131 if (!dctx->oaep_label)
133 dctx->oaep_labellen = sctx->oaep_labellen;
138 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
142 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
143 if (ctx->tbuf == NULL)
148 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
150 RSA_PKEY_CTX *rctx = ctx->data;
152 BN_free(rctx->pub_exp);
153 OPENSSL_free(rctx->tbuf);
154 OPENSSL_free(rctx->oaep_label);
159 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
160 size_t *siglen, const unsigned char *tbs,
164 RSA_PKEY_CTX *rctx = ctx->data;
165 RSA *rsa = ctx->pkey->pkey.rsa;
168 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
169 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
173 if (EVP_MD_type(rctx->md) == NID_mdc2) {
175 if (rctx->pad_mode != RSA_PKCS1_PADDING)
177 ret = RSA_sign_ASN1_OCTET_STRING(0,
178 tbs, tbslen, sig, &sltmp, rsa);
183 } else if (rctx->pad_mode == RSA_X931_PADDING) {
184 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
185 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
188 if (!setup_tbuf(rctx, ctx)) {
189 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
192 memcpy(rctx->tbuf, tbs, tbslen);
193 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
194 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
195 sig, rsa, RSA_X931_PADDING);
196 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
198 ret = RSA_sign(EVP_MD_type(rctx->md),
199 tbs, tbslen, sig, &sltmp, rsa);
203 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
204 if (!setup_tbuf(rctx, ctx))
206 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
208 rctx->md, rctx->mgf1md,
211 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
212 sig, rsa, RSA_NO_PADDING);
216 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
224 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
225 unsigned char *rout, size_t *routlen,
226 const unsigned char *sig, size_t siglen)
229 RSA_PKEY_CTX *rctx = ctx->data;
232 if (rctx->pad_mode == RSA_X931_PADDING) {
233 if (!setup_tbuf(rctx, ctx))
235 ret = RSA_public_decrypt(siglen, sig,
236 rctx->tbuf, ctx->pkey->pkey.rsa,
241 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
242 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
243 RSA_R_ALGORITHM_MISMATCH);
246 if (ret != EVP_MD_size(rctx->md)) {
247 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
248 RSA_R_INVALID_DIGEST_LENGTH);
252 memcpy(rout, rctx->tbuf, ret);
253 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
255 ret = int_rsa_verify(EVP_MD_type(rctx->md),
256 NULL, 0, rout, &sltmp,
257 sig, siglen, ctx->pkey->pkey.rsa);
264 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
272 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
273 const unsigned char *sig, size_t siglen,
274 const unsigned char *tbs, size_t tbslen)
276 RSA_PKEY_CTX *rctx = ctx->data;
277 RSA *rsa = ctx->pkey->pkey.rsa;
280 if (rctx->pad_mode == RSA_PKCS1_PADDING)
281 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
283 if (rctx->pad_mode == RSA_X931_PADDING) {
284 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
286 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
288 if (!setup_tbuf(rctx, ctx))
290 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
291 rsa, RSA_NO_PADDING);
294 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
295 rctx->md, rctx->mgf1md,
296 rctx->tbuf, rctx->saltlen);
303 if (!setup_tbuf(rctx, ctx))
305 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
306 rsa, rctx->pad_mode);
311 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
318 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
319 unsigned char *out, size_t *outlen,
320 const unsigned char *in, size_t inlen)
323 RSA_PKEY_CTX *rctx = ctx->data;
324 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
325 int klen = RSA_size(ctx->pkey->pkey.rsa);
326 if (!setup_tbuf(rctx, ctx))
328 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
332 rctx->md, rctx->mgf1md))
334 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
335 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
337 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
345 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
346 unsigned char *out, size_t *outlen,
347 const unsigned char *in, size_t inlen)
350 RSA_PKEY_CTX *rctx = ctx->data;
351 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
353 if (!setup_tbuf(rctx, ctx))
355 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
356 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
359 for (i = 0; i < ret; i++) {
363 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
367 rctx->md, rctx->mgf1md);
369 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
377 static int check_padding_md(const EVP_MD *md, int padding)
383 mdnid = EVP_MD_type(md);
385 if (padding == RSA_NO_PADDING) {
386 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
390 if (padding == RSA_X931_PADDING) {
391 if (RSA_X931_hash_id(mdnid) == -1) {
392 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
397 /* List of all supported RSA digests */
412 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
421 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
423 RSA_PKEY_CTX *rctx = ctx->data;
425 case EVP_PKEY_CTRL_RSA_PADDING:
426 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
427 if (!check_padding_md(rctx->md, p1))
429 if (p1 == RSA_PKCS1_PSS_PADDING) {
430 if (!(ctx->operation &
431 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
434 rctx->md = EVP_sha1();
436 if (p1 == RSA_PKCS1_OAEP_PADDING) {
437 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
440 rctx->md = EVP_sha1();
446 RSAerr(RSA_F_PKEY_RSA_CTRL,
447 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
450 case EVP_PKEY_CTRL_GET_RSA_PADDING:
451 *(int *)p2 = rctx->pad_mode;
454 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
455 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
456 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
457 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
460 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
461 *(int *)p2 = rctx->saltlen;
469 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
471 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
477 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
480 BN_free(rctx->pub_exp);
484 case EVP_PKEY_CTRL_RSA_OAEP_MD:
485 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
486 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
487 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
490 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
491 *(const EVP_MD **)p2 = rctx->md;
496 case EVP_PKEY_CTRL_MD:
497 if (!check_padding_md(p2, rctx->pad_mode))
502 case EVP_PKEY_CTRL_GET_MD:
503 *(const EVP_MD **)p2 = rctx->md;
506 case EVP_PKEY_CTRL_RSA_MGF1_MD:
507 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
508 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
509 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
510 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
513 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
515 *(const EVP_MD **)p2 = rctx->mgf1md;
517 *(const EVP_MD **)p2 = rctx->md;
522 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
523 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
524 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
527 OPENSSL_free(rctx->oaep_label);
529 rctx->oaep_label = p2;
530 rctx->oaep_labellen = p1;
532 rctx->oaep_label = NULL;
533 rctx->oaep_labellen = 0;
537 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
538 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
539 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
542 *(unsigned char **)p2 = rctx->oaep_label;
543 return rctx->oaep_labellen;
545 case EVP_PKEY_CTRL_DIGESTINIT:
546 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
547 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
548 case EVP_PKEY_CTRL_PKCS7_SIGN:
550 #ifndef OPENSSL_NO_CMS
551 case EVP_PKEY_CTRL_CMS_DECRYPT:
552 case EVP_PKEY_CTRL_CMS_ENCRYPT:
553 case EVP_PKEY_CTRL_CMS_SIGN:
556 case EVP_PKEY_CTRL_PEER_KEY:
557 RSAerr(RSA_F_PKEY_RSA_CTRL,
558 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
567 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
568 const char *type, const char *value)
571 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
574 if (strcmp(type, "rsa_padding_mode") == 0) {
576 if (strcmp(value, "pkcs1") == 0)
577 pm = RSA_PKCS1_PADDING;
578 else if (strcmp(value, "sslv23") == 0)
579 pm = RSA_SSLV23_PADDING;
580 else if (strcmp(value, "none") == 0)
582 else if (strcmp(value, "oeap") == 0)
583 pm = RSA_PKCS1_OAEP_PADDING;
584 else if (strcmp(value, "oaep") == 0)
585 pm = RSA_PKCS1_OAEP_PADDING;
586 else if (strcmp(value, "x931") == 0)
587 pm = RSA_X931_PADDING;
588 else if (strcmp(value, "pss") == 0)
589 pm = RSA_PKCS1_PSS_PADDING;
591 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
594 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
597 if (strcmp(type, "rsa_pss_saltlen") == 0) {
599 saltlen = atoi(value);
600 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
603 if (strcmp(type, "rsa_keygen_bits") == 0) {
606 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
609 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
611 BIGNUM *pubexp = NULL;
612 if (!BN_asc2bn(&pubexp, value))
614 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
620 if (strcmp(type, "rsa_mgf1_md") == 0) {
622 if ((md = EVP_get_digestbyname(value)) == NULL) {
623 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
626 return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
629 if (strcmp(type, "rsa_oaep_md") == 0) {
631 if ((md = EVP_get_digestbyname(value)) == NULL) {
632 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
635 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
637 if (strcmp(type, "rsa_oaep_label") == 0) {
641 lab = string_to_hex(value, &lablen);
644 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
653 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
656 RSA_PKEY_CTX *rctx = ctx->data;
659 if (rctx->pub_exp == NULL) {
660 rctx->pub_exp = BN_new();
661 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
667 if (ctx->pkey_gencb) {
668 pcb = BN_GENCB_new();
673 evp_pkey_set_cb_translate(pcb, ctx);
676 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
679 EVP_PKEY_assign_RSA(pkey, rsa);
685 const EVP_PKEY_METHOD rsa_pkey_meth = {
687 EVP_PKEY_FLAG_AUTOARGLEN,
704 pkey_rsa_verifyrecover,