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 #include <openssl/cms.h>
68 #include "internal/evp_int.h"
71 /* RSA pkey context structure */
74 /* Key gen parameters */
77 /* Keygen callback info */
79 /* RSA padding mode */
83 /* message digest for MGF1 */
90 unsigned char *oaep_label;
94 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
97 rctx = OPENSSL_zalloc(sizeof(*rctx));
101 rctx->pad_mode = RSA_PKCS1_PADDING;
104 ctx->keygen_info = rctx->gentmp;
105 ctx->keygen_info_count = 2;
110 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
112 RSA_PKEY_CTX *dctx, *sctx;
113 if (!pkey_rsa_init(dst))
117 dctx->nbits = sctx->nbits;
119 dctx->pub_exp = BN_dup(sctx->pub_exp);
123 dctx->pad_mode = sctx->pad_mode;
125 dctx->mgf1md = sctx->mgf1md;
126 if (sctx->oaep_label) {
127 OPENSSL_free(dctx->oaep_label);
128 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
129 if (!dctx->oaep_label)
131 dctx->oaep_labellen = sctx->oaep_labellen;
136 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
140 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
141 if (ctx->tbuf == NULL)
146 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
148 RSA_PKEY_CTX *rctx = ctx->data;
150 BN_free(rctx->pub_exp);
151 OPENSSL_free(rctx->tbuf);
152 OPENSSL_free(rctx->oaep_label);
157 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
158 size_t *siglen, const unsigned char *tbs,
162 RSA_PKEY_CTX *rctx = ctx->data;
163 RSA *rsa = ctx->pkey->pkey.rsa;
166 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
167 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
171 if (EVP_MD_type(rctx->md) == NID_mdc2) {
173 if (rctx->pad_mode != RSA_PKCS1_PADDING)
175 ret = RSA_sign_ASN1_OCTET_STRING(0,
176 tbs, tbslen, sig, &sltmp, rsa);
181 } else if (rctx->pad_mode == RSA_X931_PADDING) {
182 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
183 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
186 if (!setup_tbuf(rctx, ctx)) {
187 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
190 memcpy(rctx->tbuf, tbs, tbslen);
191 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
192 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
193 sig, rsa, RSA_X931_PADDING);
194 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
196 ret = RSA_sign(EVP_MD_type(rctx->md),
197 tbs, tbslen, sig, &sltmp, rsa);
201 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
202 if (!setup_tbuf(rctx, ctx))
204 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
206 rctx->md, rctx->mgf1md,
209 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
210 sig, rsa, RSA_NO_PADDING);
214 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
222 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
223 unsigned char *rout, size_t *routlen,
224 const unsigned char *sig, size_t siglen)
227 RSA_PKEY_CTX *rctx = ctx->data;
230 if (rctx->pad_mode == RSA_X931_PADDING) {
231 if (!setup_tbuf(rctx, ctx))
233 ret = RSA_public_decrypt(siglen, sig,
234 rctx->tbuf, ctx->pkey->pkey.rsa,
239 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
240 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
241 RSA_R_ALGORITHM_MISMATCH);
244 if (ret != EVP_MD_size(rctx->md)) {
245 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
246 RSA_R_INVALID_DIGEST_LENGTH);
250 memcpy(rout, rctx->tbuf, ret);
251 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
253 ret = int_rsa_verify(EVP_MD_type(rctx->md),
254 NULL, 0, rout, &sltmp,
255 sig, siglen, ctx->pkey->pkey.rsa);
262 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
270 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
271 const unsigned char *sig, size_t siglen,
272 const unsigned char *tbs, size_t tbslen)
274 RSA_PKEY_CTX *rctx = ctx->data;
275 RSA *rsa = ctx->pkey->pkey.rsa;
278 if (rctx->pad_mode == RSA_PKCS1_PADDING)
279 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
281 if (rctx->pad_mode == RSA_X931_PADDING) {
282 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
284 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
286 if (!setup_tbuf(rctx, ctx))
288 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
289 rsa, RSA_NO_PADDING);
292 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
293 rctx->md, rctx->mgf1md,
294 rctx->tbuf, rctx->saltlen);
301 if (!setup_tbuf(rctx, ctx))
303 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
304 rsa, rctx->pad_mode);
309 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
316 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
317 unsigned char *out, size_t *outlen,
318 const unsigned char *in, size_t inlen)
321 RSA_PKEY_CTX *rctx = ctx->data;
322 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
323 int klen = RSA_size(ctx->pkey->pkey.rsa);
324 if (!setup_tbuf(rctx, ctx))
326 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
330 rctx->md, rctx->mgf1md))
332 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
333 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
335 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
343 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
344 unsigned char *out, size_t *outlen,
345 const unsigned char *in, size_t inlen)
348 RSA_PKEY_CTX *rctx = ctx->data;
349 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
351 if (!setup_tbuf(rctx, ctx))
353 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
354 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
357 for (i = 0; i < ret; i++) {
361 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
365 rctx->md, rctx->mgf1md);
367 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
375 static int check_padding_md(const EVP_MD *md, int padding)
381 mdnid = EVP_MD_type(md);
383 if (padding == RSA_NO_PADDING) {
384 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
388 if (padding == RSA_X931_PADDING) {
389 if (RSA_X931_hash_id(mdnid) == -1) {
390 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
395 /* List of all supported RSA digests */
410 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
419 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
421 RSA_PKEY_CTX *rctx = ctx->data;
423 case EVP_PKEY_CTRL_RSA_PADDING:
424 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
425 if (!check_padding_md(rctx->md, p1))
427 if (p1 == RSA_PKCS1_PSS_PADDING) {
428 if (!(ctx->operation &
429 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
432 rctx->md = EVP_sha1();
434 if (p1 == RSA_PKCS1_OAEP_PADDING) {
435 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
438 rctx->md = EVP_sha1();
444 RSAerr(RSA_F_PKEY_RSA_CTRL,
445 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
448 case EVP_PKEY_CTRL_GET_RSA_PADDING:
449 *(int *)p2 = rctx->pad_mode;
452 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
453 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
454 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
455 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
458 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
459 *(int *)p2 = rctx->saltlen;
467 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
469 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
475 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
478 BN_free(rctx->pub_exp);
482 case EVP_PKEY_CTRL_RSA_OAEP_MD:
483 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
484 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
485 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
488 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
489 *(const EVP_MD **)p2 = rctx->md;
494 case EVP_PKEY_CTRL_MD:
495 if (!check_padding_md(p2, rctx->pad_mode))
500 case EVP_PKEY_CTRL_GET_MD:
501 *(const EVP_MD **)p2 = rctx->md;
504 case EVP_PKEY_CTRL_RSA_MGF1_MD:
505 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
506 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
507 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
508 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
511 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
513 *(const EVP_MD **)p2 = rctx->mgf1md;
515 *(const EVP_MD **)p2 = rctx->md;
520 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
521 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
522 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
525 OPENSSL_free(rctx->oaep_label);
527 rctx->oaep_label = p2;
528 rctx->oaep_labellen = p1;
530 rctx->oaep_label = NULL;
531 rctx->oaep_labellen = 0;
535 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
536 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
537 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
540 *(unsigned char **)p2 = rctx->oaep_label;
541 return rctx->oaep_labellen;
543 case EVP_PKEY_CTRL_DIGESTINIT:
544 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
545 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
546 case EVP_PKEY_CTRL_PKCS7_SIGN:
548 #ifndef OPENSSL_NO_CMS
549 case EVP_PKEY_CTRL_CMS_DECRYPT:
550 case EVP_PKEY_CTRL_CMS_ENCRYPT:
551 case EVP_PKEY_CTRL_CMS_SIGN:
554 case EVP_PKEY_CTRL_PEER_KEY:
555 RSAerr(RSA_F_PKEY_RSA_CTRL,
556 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
565 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
566 const char *type, const char *value)
569 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
572 if (strcmp(type, "rsa_padding_mode") == 0) {
574 if (strcmp(value, "pkcs1") == 0)
575 pm = RSA_PKCS1_PADDING;
576 else if (strcmp(value, "sslv23") == 0)
577 pm = RSA_SSLV23_PADDING;
578 else if (strcmp(value, "none") == 0)
580 else if (strcmp(value, "oeap") == 0)
581 pm = RSA_PKCS1_OAEP_PADDING;
582 else if (strcmp(value, "oaep") == 0)
583 pm = RSA_PKCS1_OAEP_PADDING;
584 else if (strcmp(value, "x931") == 0)
585 pm = RSA_X931_PADDING;
586 else if (strcmp(value, "pss") == 0)
587 pm = RSA_PKCS1_PSS_PADDING;
589 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
592 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
595 if (strcmp(type, "rsa_pss_saltlen") == 0) {
597 saltlen = atoi(value);
598 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
601 if (strcmp(type, "rsa_keygen_bits") == 0) {
604 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
607 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
609 BIGNUM *pubexp = NULL;
610 if (!BN_asc2bn(&pubexp, value))
612 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
618 if (strcmp(type, "rsa_mgf1_md") == 0) {
620 if ((md = EVP_get_digestbyname(value)) == NULL) {
621 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
624 return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
627 if (strcmp(type, "rsa_oaep_md") == 0) {
629 if ((md = EVP_get_digestbyname(value)) == NULL) {
630 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_INVALID_DIGEST);
633 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
635 if (strcmp(type, "rsa_oaep_label") == 0) {
639 lab = string_to_hex(value, &lablen);
642 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
651 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
654 RSA_PKEY_CTX *rctx = ctx->data;
657 if (rctx->pub_exp == NULL) {
658 rctx->pub_exp = BN_new();
659 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
665 if (ctx->pkey_gencb) {
666 pcb = BN_GENCB_new();
671 evp_pkey_set_cb_translate(pcb, ctx);
674 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
677 EVP_PKEY_assign_RSA(pkey, rsa);
683 const EVP_PKEY_METHOD rsa_pkey_meth = {
685 EVP_PKEY_FLAG_AUTOARGLEN,
702 pkey_rsa_verifyrecover,