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>
72 /* RSA pkey context structure */
76 /* Key gen parameters */
79 /* Keygen callback info */
81 /* RSA padding mode */
85 /* message digest for MGF1 */
87 /* PSS/OAEP salt length */
93 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
96 rctx = OPENSSL_malloc(sizeof(RSA_PKEY_CTX));
100 rctx->pub_exp = NULL;
101 rctx->pad_mode = RSA_PKCS1_PADDING;
109 ctx->keygen_info = rctx->gentmp;
110 ctx->keygen_info_count = 2;
115 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
117 RSA_PKEY_CTX *dctx, *sctx;
118 if (!pkey_rsa_init(dst))
122 dctx->nbits = sctx->nbits;
125 dctx->pub_exp = BN_dup(sctx->pub_exp);
129 dctx->pad_mode = sctx->pad_mode;
134 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
138 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
144 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
146 RSA_PKEY_CTX *rctx = ctx->data;
150 BN_free(rctx->pub_exp);
152 OPENSSL_free(rctx->tbuf);
157 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
158 const unsigned char *tbs, size_t tbslen)
161 RSA_PKEY_CTX *rctx = ctx->data;
162 RSA *rsa = ctx->pkey->pkey.rsa;
166 if (tbslen != (size_t)EVP_MD_size(rctx->md))
168 RSAerr(RSA_F_PKEY_RSA_SIGN,
169 RSA_R_INVALID_DIGEST_LENGTH);
172 if (rctx->pad_mode == RSA_X931_PADDING)
174 if (!setup_tbuf(rctx, ctx))
176 memcpy(rctx->tbuf, tbs, tbslen);
178 RSA_X931_hash_id(EVP_MD_type(rctx->md));
179 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
180 sig, rsa, RSA_X931_PADDING);
182 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
185 ret = RSA_sign(EVP_MD_type(rctx->md),
186 tbs, tbslen, sig, &sltmp, rsa);
191 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
193 if (!setup_tbuf(rctx, ctx))
195 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
197 rctx->md, rctx->mgf1md,
200 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
201 sig, rsa, RSA_NO_PADDING);
207 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
216 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
217 unsigned char *rout, size_t *routlen,
218 const unsigned char *sig, size_t siglen)
221 RSA_PKEY_CTX *rctx = ctx->data;
225 if (rctx->pad_mode == RSA_X931_PADDING)
227 if (!setup_tbuf(rctx, ctx))
229 ret = RSA_public_decrypt(siglen, sig,
230 rctx->tbuf, ctx->pkey->pkey.rsa,
235 if (rctx->tbuf[ret] !=
236 RSA_X931_hash_id(EVP_MD_type(rctx->md)))
238 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
239 RSA_R_ALGORITHM_MISMATCH);
242 if (ret != EVP_MD_size(rctx->md))
244 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
245 RSA_R_INVALID_DIGEST_LENGTH);
249 memcpy(rout, rctx->tbuf, ret);
251 else if (rctx->pad_mode == RSA_PKCS1_PADDING)
254 ret = int_rsa_verify(EVP_MD_type(rctx->md),
255 NULL, 0, rout, &sltmp,
256 sig, siglen, ctx->pkey->pkey.rsa);
265 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
273 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
274 const unsigned char *sig, size_t siglen,
275 const unsigned char *tbs, size_t tbslen)
277 RSA_PKEY_CTX *rctx = ctx->data;
278 RSA *rsa = ctx->pkey->pkey.rsa;
282 if (rctx->pad_mode == RSA_PKCS1_PADDING)
283 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
285 if (rctx->pad_mode == RSA_X931_PADDING)
287 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen,
291 else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING)
294 if (!setup_tbuf(rctx, ctx))
296 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
297 rsa, RSA_NO_PADDING);
300 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
301 rctx->md, rctx->mgf1md,
302 rctx->tbuf, rctx->saltlen);
312 if (!setup_tbuf(rctx, ctx))
314 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
315 rsa, rctx->pad_mode);
320 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
328 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
329 unsigned char *out, size_t *outlen,
330 const unsigned char *in, size_t inlen)
333 RSA_PKEY_CTX *rctx = ctx->data;
334 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
342 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
343 unsigned char *out, size_t *outlen,
344 const unsigned char *in, size_t inlen)
347 RSA_PKEY_CTX *rctx = ctx->data;
348 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
356 static int check_padding_md(const EVP_MD *md, int padding)
361 if (padding == RSA_NO_PADDING)
363 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
367 if (padding == RSA_X931_PADDING)
369 if (RSA_X931_hash_id(EVP_MD_type(md)) == -1)
371 RSAerr(RSA_F_CHECK_PADDING_MD,
372 RSA_R_INVALID_X931_DIGEST);
382 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
384 RSA_PKEY_CTX *rctx = ctx->data;
387 case EVP_PKEY_CTRL_RSA_PADDING:
388 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING))
390 if (!check_padding_md(rctx->md, p1))
392 if (p1 == RSA_PKCS1_PSS_PADDING)
394 if (!(ctx->operation &
395 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
398 rctx->md = EVP_sha1();
400 if (p1 == RSA_PKCS1_OAEP_PADDING)
402 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
405 rctx->md = EVP_sha1();
411 RSAerr(RSA_F_PKEY_RSA_CTRL,
412 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
415 case EVP_PKEY_CTRL_GET_RSA_PADDING:
416 *(int *)p2 = rctx->pad_mode;
419 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
420 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
421 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
423 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
426 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
427 *(int *)p2 = rctx->saltlen;
436 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
439 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS);
445 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
451 case EVP_PKEY_CTRL_MD:
452 if (!check_padding_md(p2, rctx->pad_mode))
457 case EVP_PKEY_CTRL_RSA_MGF1_MD:
458 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
459 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING)
461 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
464 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD)
467 *(const EVP_MD **)p2 = rctx->mgf1md;
469 *(const EVP_MD **)p2 = rctx->md;
475 case EVP_PKEY_CTRL_DIGESTINIT:
476 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
477 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
478 case EVP_PKEY_CTRL_PKCS7_SIGN:
480 #ifndef OPENSSL_NO_CMS
481 case EVP_PKEY_CTRL_CMS_DECRYPT:
483 X509_ALGOR *alg = NULL;
484 ASN1_OBJECT *encalg = NULL;
486 CMS_RecipientInfo_ktri_get0_algs(p2, NULL, NULL, &alg);
488 X509_ALGOR_get0(&encalg, NULL, NULL, alg);
489 if (encalg && OBJ_obj2nid(encalg) == NID_rsaesOaep)
490 rctx->pad_mode = RSA_PKCS1_OAEP_PADDING;
492 case EVP_PKEY_CTRL_CMS_ENCRYPT:
493 case EVP_PKEY_CTRL_CMS_SIGN:
496 case EVP_PKEY_CTRL_PEER_KEY:
497 RSAerr(RSA_F_PKEY_RSA_CTRL,
498 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
507 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
508 const char *type, const char *value)
512 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
515 if (!strcmp(type, "rsa_padding_mode"))
518 if (!strcmp(value, "pkcs1"))
519 pm = RSA_PKCS1_PADDING;
520 else if (!strcmp(value, "sslv23"))
521 pm = RSA_SSLV23_PADDING;
522 else if (!strcmp(value, "none"))
524 else if (!strcmp(value, "oeap"))
525 pm = RSA_PKCS1_OAEP_PADDING;
526 else if (!strcmp(value, "x931"))
527 pm = RSA_X931_PADDING;
528 else if (!strcmp(value, "pss"))
529 pm = RSA_PKCS1_PSS_PADDING;
532 RSAerr(RSA_F_PKEY_RSA_CTRL_STR,
533 RSA_R_UNKNOWN_PADDING_TYPE);
536 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
539 if (!strcmp(type, "rsa_pss_saltlen"))
542 saltlen = atoi(value);
543 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
546 if (!strcmp(type, "rsa_keygen_bits"))
550 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
553 if (!strcmp(type, "rsa_keygen_pubexp"))
556 BIGNUM *pubexp = NULL;
557 if (!BN_asc2bn(&pubexp, value))
559 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
568 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
571 RSA_PKEY_CTX *rctx = ctx->data;
576 rctx->pub_exp = BN_new();
577 if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4))
586 evp_pkey_set_cb_translate(pcb, ctx);
590 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
592 EVP_PKEY_assign_RSA(pkey, rsa);
598 const EVP_PKEY_METHOD rsa_pkey_meth =
601 EVP_PKEY_FLAG_AUTOARGLEN,
618 pkey_rsa_verifyrecover,