2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/rsa.h>
15 #include <openssl/bn.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/cms.h>
19 #include "internal/evp_int.h"
22 /* RSA pkey context structure */
25 /* Key gen parameters */
28 /* Keygen callback info */
30 /* RSA padding mode */
34 /* message digest for MGF1 */
41 unsigned char *oaep_label;
45 static int pkey_rsa_init(EVP_PKEY_CTX *ctx)
48 rctx = OPENSSL_zalloc(sizeof(*rctx));
52 if (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
53 rctx->pad_mode = RSA_PKCS1_PSS_PADDING;
55 rctx->pad_mode = RSA_PKCS1_PADDING;
58 ctx->keygen_info = rctx->gentmp;
59 ctx->keygen_info_count = 2;
64 static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
66 RSA_PKEY_CTX *dctx, *sctx;
67 if (!pkey_rsa_init(dst))
71 dctx->nbits = sctx->nbits;
73 dctx->pub_exp = BN_dup(sctx->pub_exp);
77 dctx->pad_mode = sctx->pad_mode;
79 dctx->mgf1md = sctx->mgf1md;
80 if (sctx->oaep_label) {
81 OPENSSL_free(dctx->oaep_label);
82 dctx->oaep_label = OPENSSL_memdup(sctx->oaep_label, sctx->oaep_labellen);
83 if (!dctx->oaep_label)
85 dctx->oaep_labellen = sctx->oaep_labellen;
90 static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)
94 ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));
95 if (ctx->tbuf == NULL)
100 static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
102 RSA_PKEY_CTX *rctx = ctx->data;
104 BN_free(rctx->pub_exp);
105 OPENSSL_free(rctx->tbuf);
106 OPENSSL_free(rctx->oaep_label);
111 static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
112 size_t *siglen, const unsigned char *tbs,
116 RSA_PKEY_CTX *rctx = ctx->data;
117 RSA *rsa = ctx->pkey->pkey.rsa;
120 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
121 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_INVALID_DIGEST_LENGTH);
125 if (EVP_MD_type(rctx->md) == NID_mdc2) {
127 if (rctx->pad_mode != RSA_PKCS1_PADDING)
129 ret = RSA_sign_ASN1_OCTET_STRING(0,
130 tbs, tbslen, sig, &sltmp, rsa);
135 } else if (rctx->pad_mode == RSA_X931_PADDING) {
136 if ((size_t)EVP_PKEY_size(ctx->pkey) < tbslen + 1) {
137 RSAerr(RSA_F_PKEY_RSA_SIGN, RSA_R_KEY_SIZE_TOO_SMALL);
140 if (!setup_tbuf(rctx, ctx)) {
141 RSAerr(RSA_F_PKEY_RSA_SIGN, ERR_R_MALLOC_FAILURE);
144 memcpy(rctx->tbuf, tbs, tbslen);
145 rctx->tbuf[tbslen] = RSA_X931_hash_id(EVP_MD_type(rctx->md));
146 ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf,
147 sig, rsa, RSA_X931_PADDING);
148 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
150 ret = RSA_sign(EVP_MD_type(rctx->md),
151 tbs, tbslen, sig, &sltmp, rsa);
155 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
156 if (!setup_tbuf(rctx, ctx))
158 if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa,
160 rctx->md, rctx->mgf1md,
163 ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
164 sig, rsa, RSA_NO_PADDING);
168 ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
176 static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
177 unsigned char *rout, size_t *routlen,
178 const unsigned char *sig, size_t siglen)
181 RSA_PKEY_CTX *rctx = ctx->data;
184 if (rctx->pad_mode == RSA_X931_PADDING) {
185 if (!setup_tbuf(rctx, ctx))
187 ret = RSA_public_decrypt(siglen, sig,
188 rctx->tbuf, ctx->pkey->pkey.rsa,
193 if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
194 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
195 RSA_R_ALGORITHM_MISMATCH);
198 if (ret != EVP_MD_size(rctx->md)) {
199 RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
200 RSA_R_INVALID_DIGEST_LENGTH);
204 memcpy(rout, rctx->tbuf, ret);
205 } else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
207 ret = int_rsa_verify(EVP_MD_type(rctx->md),
208 NULL, 0, rout, &sltmp,
209 sig, siglen, ctx->pkey->pkey.rsa);
216 ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
224 static int pkey_rsa_verify(EVP_PKEY_CTX *ctx,
225 const unsigned char *sig, size_t siglen,
226 const unsigned char *tbs, size_t tbslen)
228 RSA_PKEY_CTX *rctx = ctx->data;
229 RSA *rsa = ctx->pkey->pkey.rsa;
232 if (rctx->pad_mode == RSA_PKCS1_PADDING)
233 return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
235 if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
236 RSAerr(RSA_F_PKEY_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
239 if (rctx->pad_mode == RSA_X931_PADDING) {
240 if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, siglen) <= 0)
242 } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
244 if (!setup_tbuf(rctx, ctx))
246 ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
247 rsa, RSA_NO_PADDING);
250 ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs,
251 rctx->md, rctx->mgf1md,
252 rctx->tbuf, rctx->saltlen);
259 if (!setup_tbuf(rctx, ctx))
261 rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf,
262 rsa, rctx->pad_mode);
267 if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen))
274 static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
275 unsigned char *out, size_t *outlen,
276 const unsigned char *in, size_t inlen)
279 RSA_PKEY_CTX *rctx = ctx->data;
280 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
281 int klen = RSA_size(ctx->pkey->pkey.rsa);
282 if (!setup_tbuf(rctx, ctx))
284 if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
288 rctx->md, rctx->mgf1md))
290 ret = RSA_public_encrypt(klen, rctx->tbuf, out,
291 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
293 ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
301 static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,
302 unsigned char *out, size_t *outlen,
303 const unsigned char *in, size_t inlen)
306 RSA_PKEY_CTX *rctx = ctx->data;
307 if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
309 if (!setup_tbuf(rctx, ctx))
311 ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
312 ctx->pkey->pkey.rsa, RSA_NO_PADDING);
315 for (i = 0; i < ret; i++) {
319 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,
323 rctx->md, rctx->mgf1md);
325 ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
333 static int check_padding_md(const EVP_MD *md, int padding)
339 mdnid = EVP_MD_type(md);
341 if (padding == RSA_NO_PADDING) {
342 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE);
346 if (padding == RSA_X931_PADDING) {
347 if (RSA_X931_hash_id(mdnid) == -1) {
348 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_X931_DIGEST);
353 /* List of all supported RSA digests */
368 RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_DIGEST);
377 static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
379 RSA_PKEY_CTX *rctx = ctx->data;
381 case EVP_PKEY_CTRL_RSA_PADDING:
382 if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) {
383 if (!check_padding_md(rctx->md, p1))
385 if (p1 == RSA_PKCS1_PSS_PADDING) {
386 if (!(ctx->operation &
387 (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)))
390 rctx->md = EVP_sha1();
391 } else if (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS) {
394 if (p1 == RSA_PKCS1_OAEP_PADDING) {
395 if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT))
398 rctx->md = EVP_sha1();
404 RSAerr(RSA_F_PKEY_RSA_CTRL,
405 RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
408 case EVP_PKEY_CTRL_GET_RSA_PADDING:
409 *(int *)p2 = rctx->pad_mode;
412 case EVP_PKEY_CTRL_RSA_PSS_SALTLEN:
413 case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN:
414 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
415 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN);
418 if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN)
419 *(int *)p2 = rctx->saltlen;
427 case EVP_PKEY_CTRL_RSA_KEYGEN_BITS:
429 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL);
435 case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP:
436 if (p2 == NULL || !BN_is_odd((BIGNUM *)p2) || BN_is_one((BIGNUM *)p2)) {
437 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_BAD_E_VALUE);
440 BN_free(rctx->pub_exp);
444 case EVP_PKEY_CTRL_RSA_OAEP_MD:
445 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
446 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
447 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
450 if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
451 *(const EVP_MD **)p2 = rctx->md;
456 case EVP_PKEY_CTRL_MD:
457 if (!check_padding_md(p2, rctx->pad_mode))
462 case EVP_PKEY_CTRL_GET_MD:
463 *(const EVP_MD **)p2 = rctx->md;
466 case EVP_PKEY_CTRL_RSA_MGF1_MD:
467 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
468 if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING
469 && rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
470 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD);
473 if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) {
475 *(const EVP_MD **)p2 = rctx->mgf1md;
477 *(const EVP_MD **)p2 = rctx->md;
482 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
483 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
484 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
487 OPENSSL_free(rctx->oaep_label);
489 rctx->oaep_label = p2;
490 rctx->oaep_labellen = p1;
492 rctx->oaep_label = NULL;
493 rctx->oaep_labellen = 0;
497 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
498 if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
499 RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PADDING_MODE);
502 *(unsigned char **)p2 = rctx->oaep_label;
503 return rctx->oaep_labellen;
505 case EVP_PKEY_CTRL_DIGESTINIT:
506 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
507 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
508 case EVP_PKEY_CTRL_PKCS7_SIGN:
510 #ifndef OPENSSL_NO_CMS
511 case EVP_PKEY_CTRL_CMS_DECRYPT:
512 case EVP_PKEY_CTRL_CMS_ENCRYPT:
513 case EVP_PKEY_CTRL_CMS_SIGN:
516 case EVP_PKEY_CTRL_PEER_KEY:
517 RSAerr(RSA_F_PKEY_RSA_CTRL,
518 RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
527 static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx,
528 const char *type, const char *value)
531 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING);
534 if (strcmp(type, "rsa_padding_mode") == 0) {
536 if (strcmp(value, "pkcs1") == 0)
537 pm = RSA_PKCS1_PADDING;
538 else if (strcmp(value, "sslv23") == 0)
539 pm = RSA_SSLV23_PADDING;
540 else if (strcmp(value, "none") == 0)
542 else if (strcmp(value, "oeap") == 0)
543 pm = RSA_PKCS1_OAEP_PADDING;
544 else if (strcmp(value, "oaep") == 0)
545 pm = RSA_PKCS1_OAEP_PADDING;
546 else if (strcmp(value, "x931") == 0)
547 pm = RSA_X931_PADDING;
548 else if (strcmp(value, "pss") == 0)
549 pm = RSA_PKCS1_PSS_PADDING;
551 RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_UNKNOWN_PADDING_TYPE);
554 return EVP_PKEY_CTX_set_rsa_padding(ctx, pm);
557 if (strcmp(type, "rsa_pss_saltlen") == 0) {
559 saltlen = atoi(value);
560 return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, saltlen);
563 if (strcmp(type, "rsa_keygen_bits") == 0) {
566 return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, nbits);
569 if (strcmp(type, "rsa_keygen_pubexp") == 0) {
571 BIGNUM *pubexp = NULL;
572 if (!BN_asc2bn(&pubexp, value))
574 ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp);
580 if (strcmp(type, "rsa_mgf1_md") == 0)
581 return EVP_PKEY_CTX_md(ctx,
582 EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
583 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
585 if (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS) {
587 if (strcmp(type, "rsa_pss_keygen_mgf1_md") == 0)
588 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
589 EVP_PKEY_CTRL_RSA_MGF1_MD, value);
591 if (strcmp(type, "rsa_pss_keygen_md") == 0)
592 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_KEYGEN,
593 EVP_PKEY_CTRL_MD, value);
595 if (strcmp(type, "rsa_pss_keygen_saltlen") == 0) {
597 saltlen = atoi(value);
598 return EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx, saltlen);
602 if (strcmp(type, "rsa_oaep_md") == 0)
603 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_CRYPT,
604 EVP_PKEY_CTRL_RSA_OAEP_MD, value);
606 if (strcmp(type, "rsa_oaep_label") == 0) {
610 lab = OPENSSL_hexstr2buf(value, &lablen);
613 ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
622 /* Set PSS parameters when generating a key, if necessary */
623 static int rsa_set_pss_param(RSA *rsa, EVP_PKEY_CTX *ctx)
625 RSA_PKEY_CTX *rctx = ctx->data;
626 if (ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
628 if (rctx->md == NULL && rctx->mgf1md == NULL && rctx->saltlen == -2)
630 rsa->pss = rsa_pss_params_create(rctx->md, rctx->mgf1md,
631 rctx->saltlen == -2 ? 0 : rctx->saltlen);
632 if (rsa->pss == NULL)
637 static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
640 RSA_PKEY_CTX *rctx = ctx->data;
643 if (rctx->pub_exp == NULL) {
644 rctx->pub_exp = BN_new();
645 if (rctx->pub_exp == NULL || !BN_set_word(rctx->pub_exp, RSA_F4))
651 if (ctx->pkey_gencb) {
652 pcb = BN_GENCB_new();
657 evp_pkey_set_cb_translate(pcb, ctx);
660 ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb);
662 if (ret > 0 && !rsa_set_pss_param(rsa, ctx)) {
667 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, rsa);
673 const EVP_PKEY_METHOD rsa_pkey_meth = {
675 EVP_PKEY_FLAG_AUTOARGLEN,
692 pkey_rsa_verifyrecover,
708 const EVP_PKEY_METHOD rsa_pss_pkey_meth = {
710 EVP_PKEY_FLAG_AUTOARGLEN,
726 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,