2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2006-05-25 01:30:09 +08:00
|
|
|
*
|
2018-12-06 20:40:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:24:46 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2006-05-25 01:30:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2006-05-25 01:30:09 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/x509.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/evp.h"
|
2019-09-17 00:14:21 +08:00
|
|
|
#include "internal/provider.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "evp_local.h"
|
2006-05-25 01:30:09 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2019-10-08 00:47:04 +08:00
|
|
|
|
2017-05-20 04:31:46 +08:00
|
|
|
static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
|
|
|
|
{
|
|
|
|
EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-05 23:40:48 +08:00
|
|
|
/*
|
|
|
|
* If we get the "NULL" md then the name comes back as "UNDEF". We want to use
|
|
|
|
* NULL for this.
|
|
|
|
*/
|
|
|
|
static const char *canon_mdname(const char *mdname)
|
|
|
|
{
|
|
|
|
if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return mdname;
|
|
|
|
}
|
|
|
|
|
2006-08-29 01:01:04 +08:00
|
|
|
static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
2019-09-17 00:14:21 +08:00
|
|
|
const EVP_MD *type, const char *mdname,
|
2020-07-26 15:32:05 +08:00
|
|
|
OPENSSL_CTX *libctx, const char *props,
|
|
|
|
ENGINE *e, EVP_PKEY *pkey, int ver)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2019-09-17 00:14:21 +08:00
|
|
|
EVP_PKEY_CTX *locpctx = NULL;
|
2019-10-30 23:59:34 +08:00
|
|
|
EVP_SIGNATURE *signature = NULL;
|
2020-01-14 21:11:47 +08:00
|
|
|
EVP_KEYMGMT *tmp_keymgmt = NULL;
|
|
|
|
const char *supported_sig = NULL;
|
2020-01-13 15:49:44 +08:00
|
|
|
char locmdname[80] = ""; /* 80 chars should be enough */
|
2019-09-17 00:14:21 +08:00
|
|
|
void *provkey = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2019-09-26 21:31:56 +08:00
|
|
|
if (ctx->provctx != NULL) {
|
|
|
|
if (!ossl_assert(ctx->digest != NULL)) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ctx->digest->freectx != NULL)
|
|
|
|
ctx->digest->freectx(ctx->provctx);
|
|
|
|
ctx->provctx = NULL;
|
|
|
|
}
|
|
|
|
|
2020-03-12 22:39:47 +08:00
|
|
|
if (ctx->pctx == NULL) {
|
|
|
|
if (libctx != NULL)
|
|
|
|
ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
|
|
|
|
else
|
|
|
|
ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
|
|
|
|
}
|
2019-10-30 23:59:34 +08:00
|
|
|
if (ctx->pctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
locpctx = ctx->pctx;
|
|
|
|
evp_pkey_ctx_free_old_ops(locpctx);
|
|
|
|
|
2020-05-07 03:44:58 +08:00
|
|
|
if (props == NULL)
|
|
|
|
props = locpctx->propquery;
|
|
|
|
|
2020-01-11 00:50:03 +08:00
|
|
|
/*
|
|
|
|
* TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
|
|
|
|
* calls can be removed.
|
|
|
|
*/
|
|
|
|
ERR_set_mark();
|
|
|
|
|
EVP: Check that key methods aren't foreign when exporting
The EVP_PKEY_ASN1_METHOD function export_to() must check that the key
we're trying to export has a known libcrypto method, i.e. is a built
in RSA_METHOD, DSA_METHOD, etc. Otherwise, the method may be defined
by the calling application, by an engine, by another library, and we
simply cannot know all the quirks hidden behind that method, if we
have access to the key data, or much anything.
Such keys are simply deemed impossible to export to provider keys,
i.e. have export_to() return 0. This cascades back to functions like
evp_pkey_export_to_provider() and evp_pkey_upgrade_to_provider() and
their callers. In most cases, this is fine, but if these get mixed in
with provider side keys in any function, that function will fail.
Fixes #11179
Fixes #9915
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/11193)
2020-02-27 17:51:45 +08:00
|
|
|
if (locpctx->engine != NULL || locpctx->keytype == NULL)
|
2019-09-17 00:14:21 +08:00
|
|
|
goto legacy;
|
|
|
|
|
2020-02-21 03:26:16 +08:00
|
|
|
/*
|
|
|
|
* Ensure that the key is provided, either natively, or as a cached export.
|
|
|
|
*/
|
2020-01-14 21:11:47 +08:00
|
|
|
tmp_keymgmt = locpctx->keymgmt;
|
2020-02-21 03:26:16 +08:00
|
|
|
provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
|
|
|
|
&tmp_keymgmt, locpctx->propquery);
|
2020-07-28 23:47:03 +08:00
|
|
|
if (provkey == NULL) {
|
|
|
|
/*
|
|
|
|
* If we couldn't find a keymgmt at all try legacy.
|
|
|
|
* TODO(3.0): Once all legacy algorithms (SM2, HMAC etc) have provider
|
|
|
|
* based implementations this fallback shouldn't be necessary. Either
|
|
|
|
* we have an ENGINE based implementation (in which case we should have
|
|
|
|
* already fallen back in the test above here), or we don't have the
|
|
|
|
* provider based implementation loaded (in which case this is an
|
|
|
|
* application config error)
|
|
|
|
*/
|
|
|
|
if (locpctx->keymgmt == NULL)
|
|
|
|
goto legacy;
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2020-01-14 21:11:47 +08:00
|
|
|
if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
|
2020-01-11 00:50:03 +08:00
|
|
|
ERR_clear_last_mark();
|
2020-01-14 21:11:47 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
goto err;
|
2019-09-17 00:14:21 +08:00
|
|
|
}
|
2020-01-14 21:11:47 +08:00
|
|
|
EVP_KEYMGMT_free(locpctx->keymgmt);
|
|
|
|
locpctx->keymgmt = tmp_keymgmt;
|
|
|
|
|
|
|
|
if (locpctx->keymgmt->query_operation_name != NULL)
|
|
|
|
supported_sig =
|
|
|
|
locpctx->keymgmt->query_operation_name(OSSL_OP_SIGNATURE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we didn't get a supported sig, assume there is one with the
|
|
|
|
* same name as the key type.
|
|
|
|
*/
|
|
|
|
if (supported_sig == NULL)
|
|
|
|
supported_sig = locpctx->keytype;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Because we cleared out old ops, we shouldn't need to worry about
|
|
|
|
* checking if signature is already there.
|
|
|
|
*/
|
|
|
|
signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
|
|
|
|
locpctx->propquery);
|
|
|
|
|
|
|
|
if (signature == NULL
|
2019-10-30 23:59:34 +08:00
|
|
|
|| (EVP_KEYMGMT_provider(locpctx->keymgmt)
|
|
|
|
!= EVP_SIGNATURE_provider(signature))) {
|
|
|
|
/*
|
2020-01-11 00:50:03 +08:00
|
|
|
* We don't need to free ctx->keymgmt here, as it's not necessarily
|
|
|
|
* tied to this operation. It will be freed by EVP_PKEY_CTX_free().
|
2019-10-30 23:59:34 +08:00
|
|
|
*/
|
|
|
|
EVP_SIGNATURE_free(signature);
|
|
|
|
goto legacy;
|
|
|
|
}
|
|
|
|
|
2020-01-11 00:50:03 +08:00
|
|
|
/*
|
|
|
|
* TODO remove this when legacy is gone
|
|
|
|
* If we don't have the full support we need with provided methods,
|
|
|
|
* let's go see if legacy does.
|
|
|
|
*/
|
|
|
|
ERR_pop_to_mark();
|
|
|
|
|
2019-10-30 23:59:34 +08:00
|
|
|
/* No more legacy from here down to legacy: */
|
2019-09-17 00:14:21 +08:00
|
|
|
|
2020-01-13 15:49:44 +08:00
|
|
|
if (pctx != NULL)
|
|
|
|
*pctx = locpctx;
|
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
locpctx->op.sig.signature = signature;
|
2019-10-30 23:59:34 +08:00
|
|
|
locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
|
|
|
|
: EVP_PKEY_OP_SIGNCTX;
|
2019-09-17 00:14:21 +08:00
|
|
|
locpctx->op.sig.sigprovctx
|
2020-05-07 03:44:58 +08:00
|
|
|
= signature->newctx(ossl_provider_ctx(signature->prov), props);
|
2019-09-17 00:14:21 +08:00
|
|
|
if (locpctx->op.sig.sigprovctx == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2019-10-30 23:59:34 +08:00
|
|
|
if (type != NULL) {
|
2019-09-17 00:14:21 +08:00
|
|
|
ctx->reqdigest = type;
|
2020-01-13 15:49:44 +08:00
|
|
|
if (mdname == NULL)
|
2020-03-05 23:40:48 +08:00
|
|
|
mdname = canon_mdname(EVP_MD_name(type));
|
2019-09-17 00:14:21 +08:00
|
|
|
} else {
|
2020-04-20 15:29:16 +08:00
|
|
|
if (mdname == NULL) {
|
|
|
|
if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
|
|
|
|
locmdname,
|
|
|
|
sizeof(locmdname)) > 0) {
|
|
|
|
mdname = canon_mdname(locmdname);
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 15:49:44 +08:00
|
|
|
|
|
|
|
if (mdname != NULL) {
|
|
|
|
/*
|
|
|
|
* This might be requested by a later call to EVP_MD_CTX_md().
|
|
|
|
* In that case the "explicit fetch" rules apply for that
|
|
|
|
* function (as per man pages), i.e. the ref count is not updated
|
|
|
|
* so the EVP_MD should not be used beyound the lifetime of the
|
|
|
|
* EVP_MD_CTX.
|
|
|
|
*/
|
2020-08-11 00:11:39 +08:00
|
|
|
ctx->digest = ctx->reqdigest = ctx->fetched_digest =
|
2020-01-13 15:49:44 +08:00
|
|
|
EVP_MD_fetch(locpctx->libctx, mdname, props);
|
|
|
|
}
|
2019-09-17 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ver) {
|
|
|
|
if (signature->digest_verify_init == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2019-10-30 23:59:34 +08:00
|
|
|
ret = signature->digest_verify_init(locpctx->op.sig.sigprovctx,
|
2020-05-07 03:44:58 +08:00
|
|
|
mdname, provkey);
|
2019-09-17 00:14:21 +08:00
|
|
|
} else {
|
|
|
|
if (signature->digest_sign_init == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2019-10-30 23:59:34 +08:00
|
|
|
ret = signature->digest_sign_init(locpctx->op.sig.sigprovctx,
|
2020-05-07 03:44:58 +08:00
|
|
|
mdname, provkey);
|
2019-09-17 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret ? 1 : 0;
|
|
|
|
err:
|
|
|
|
evp_pkey_ctx_free_old_ops(locpctx);
|
|
|
|
locpctx->operation = EVP_PKEY_OP_UNDEFINED;
|
|
|
|
return 0;
|
2007-05-16 07:52:03 +08:00
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
legacy:
|
2020-01-11 00:50:03 +08:00
|
|
|
/*
|
|
|
|
* TODO remove this when legacy is gone
|
|
|
|
* If we don't have the full support we need with provided methods,
|
|
|
|
* let's go see if legacy does.
|
|
|
|
*/
|
|
|
|
ERR_pop_to_mark();
|
|
|
|
|
2020-03-17 00:04:12 +08:00
|
|
|
if (type == NULL && mdname != NULL)
|
|
|
|
type = evp_get_digestbyname_ex(locpctx->libctx, mdname);
|
|
|
|
|
2020-01-07 18:49:08 +08:00
|
|
|
if (ctx->pctx->pmeth == NULL) {
|
|
|
|
EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
2020-01-12 08:05:01 +08:00
|
|
|
return 0;
|
2020-01-07 18:49:08 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
|
2007-05-16 07:52:03 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (type == NULL) {
|
|
|
|
int def_nid;
|
|
|
|
if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
|
|
|
|
type = EVP_get_digestbynid(def_nid);
|
|
|
|
}
|
2010-02-08 23:31:35 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (type == NULL) {
|
|
|
|
EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-05-16 07:52:03 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ver) {
|
|
|
|
if (ctx->pctx->pmeth->verifyctx_init) {
|
|
|
|
if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
|
|
|
|
return 0;
|
|
|
|
ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
|
2017-05-20 04:31:46 +08:00
|
|
|
} else if (ctx->pctx->pmeth->digestverify != 0) {
|
|
|
|
ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
|
|
|
|
ctx->update = update;
|
|
|
|
} else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2017-05-20 04:31:46 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
} else {
|
|
|
|
if (ctx->pctx->pmeth->signctx_init) {
|
|
|
|
if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
|
|
|
|
return 0;
|
|
|
|
ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
|
2017-05-20 04:31:46 +08:00
|
|
|
} else if (ctx->pctx->pmeth->digestsign != 0) {
|
|
|
|
ctx->pctx->operation = EVP_PKEY_OP_SIGN;
|
|
|
|
ctx->update = update;
|
|
|
|
} else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2017-05-20 04:31:46 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
|
|
|
|
return 0;
|
|
|
|
if (pctx)
|
|
|
|
*pctx = ctx->pctx;
|
|
|
|
if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
|
|
|
|
return 1;
|
|
|
|
if (!EVP_DigestInit_ex(ctx, type, e))
|
|
|
|
return 0;
|
2018-09-04 17:21:10 +08:00
|
|
|
/*
|
|
|
|
* This indicates the current algorithm requires
|
|
|
|
* special treatment before hashing the tbs-message.
|
|
|
|
*/
|
2020-03-11 05:07:10 +08:00
|
|
|
ctx->pctx->flag_call_digest_custom = 0;
|
2018-09-05 15:19:17 +08:00
|
|
|
if (ctx->pctx->pmeth->digest_custom != NULL)
|
2020-03-11 05:07:10 +08:00
|
|
|
ctx->pctx->flag_call_digest_custom = 1;
|
2018-09-04 17:21:10 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2006-05-25 01:30:09 +08:00
|
|
|
|
2020-07-26 15:32:05 +08:00
|
|
|
int EVP_DigestSignInit_with_libctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
|
|
|
const char *mdname,
|
|
|
|
OPENSSL_CTX *libctx, const char *props,
|
|
|
|
EVP_PKEY *pkey)
|
2019-09-17 00:14:21 +08:00
|
|
|
{
|
2020-07-26 15:32:05 +08:00
|
|
|
return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0);
|
2019-09-17 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
2006-05-25 01:30:09 +08:00
|
|
|
int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
2015-01-22 11:40:55 +08:00
|
|
|
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
|
|
|
|
{
|
2020-07-26 15:32:05 +08:00
|
|
|
return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0);
|
2019-09-17 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
2020-07-26 15:32:05 +08:00
|
|
|
int EVP_DigestVerifyInit_with_libctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
|
|
|
const char *mdname,
|
|
|
|
OPENSSL_CTX *libctx, const char *props,
|
|
|
|
EVP_PKEY *pkey)
|
2019-09-17 00:14:21 +08:00
|
|
|
{
|
2020-07-26 15:32:05 +08:00
|
|
|
return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2006-05-25 01:30:09 +08:00
|
|
|
|
|
|
|
int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
2015-01-22 11:40:55 +08:00
|
|
|
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
|
|
|
|
{
|
2020-07-26 15:32:05 +08:00
|
|
|
return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1);
|
2019-09-17 00:14:21 +08:00
|
|
|
}
|
2019-10-08 00:47:04 +08:00
|
|
|
#endif /* FIPS_MDOE */
|
2019-09-17 00:14:21 +08:00
|
|
|
|
|
|
|
int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
|
|
|
|
{
|
|
|
|
EVP_PKEY_CTX *pctx = ctx->pctx;
|
|
|
|
|
|
|
|
if (pctx == NULL
|
|
|
|
|| pctx->operation != EVP_PKEY_OP_SIGNCTX
|
|
|
|
|| pctx->op.sig.sigprovctx == NULL
|
|
|
|
|| pctx->op.sig.signature == NULL)
|
|
|
|
goto legacy;
|
|
|
|
|
2020-03-05 23:40:48 +08:00
|
|
|
if (pctx->op.sig.signature->digest_sign_update == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.sigprovctx,
|
|
|
|
data, dsize);
|
|
|
|
|
|
|
|
legacy:
|
2020-04-27 05:51:16 +08:00
|
|
|
if (pctx != NULL) {
|
|
|
|
/* do_sigver_init() checked that |digest_custom| is non-NULL */
|
|
|
|
if (pctx->flag_call_digest_custom
|
|
|
|
&& !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
|
|
|
|
return 0;
|
|
|
|
pctx->flag_call_digest_custom = 0;
|
|
|
|
}
|
2020-03-11 05:07:10 +08:00
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
return EVP_DigestUpdate(ctx, data, dsize);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2006-05-25 01:30:09 +08:00
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
|
|
|
|
{
|
|
|
|
EVP_PKEY_CTX *pctx = ctx->pctx;
|
|
|
|
|
|
|
|
if (pctx == NULL
|
|
|
|
|| pctx->operation != EVP_PKEY_OP_VERIFYCTX
|
|
|
|
|| pctx->op.sig.sigprovctx == NULL
|
|
|
|
|| pctx->op.sig.signature == NULL)
|
|
|
|
goto legacy;
|
|
|
|
|
2020-03-05 23:40:48 +08:00
|
|
|
if (pctx->op.sig.signature->digest_verify_update == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.sigprovctx,
|
|
|
|
data, dsize);
|
|
|
|
|
|
|
|
legacy:
|
2020-04-27 06:26:39 +08:00
|
|
|
if (pctx != NULL) {
|
|
|
|
/* do_sigver_init() checked that |digest_custom| is non-NULL */
|
|
|
|
if (pctx->flag_call_digest_custom
|
|
|
|
&& !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
|
|
|
|
return 0;
|
|
|
|
pctx->flag_call_digest_custom = 0;
|
|
|
|
}
|
2020-03-11 05:07:10 +08:00
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
return EVP_DigestUpdate(ctx, data, dsize);
|
|
|
|
}
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2015-01-22 11:40:55 +08:00
|
|
|
int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
|
|
|
|
size_t *siglen)
|
|
|
|
{
|
2015-05-06 17:16:55 +08:00
|
|
|
int sctx = 0, r = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
EVP_PKEY_CTX *pctx = ctx->pctx;
|
2019-09-17 00:14:21 +08:00
|
|
|
|
|
|
|
if (pctx == NULL
|
|
|
|
|| pctx->operation != EVP_PKEY_OP_SIGNCTX
|
|
|
|
|| pctx->op.sig.sigprovctx == NULL
|
|
|
|
|| pctx->op.sig.signature == NULL)
|
|
|
|
goto legacy;
|
|
|
|
|
|
|
|
return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.sigprovctx,
|
|
|
|
sigret, siglen, SIZE_MAX);
|
|
|
|
|
|
|
|
legacy:
|
2020-01-11 03:40:11 +08:00
|
|
|
if (pctx == NULL || pctx->pmeth == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-11 05:07:10 +08:00
|
|
|
/* do_sigver_init() checked that |digest_custom| is non-NULL */
|
|
|
|
if (pctx->flag_call_digest_custom
|
|
|
|
&& !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
|
|
|
|
return 0;
|
|
|
|
pctx->flag_call_digest_custom = 0;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
|
2020-01-11 03:40:11 +08:00
|
|
|
if (sigret == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
|
|
|
|
if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
|
|
|
|
r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
|
|
|
|
else {
|
2020-01-11 03:40:11 +08:00
|
|
|
EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(pctx);
|
|
|
|
|
|
|
|
if (dctx == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
|
|
|
|
EVP_PKEY_CTX_free(dctx);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2020-01-11 03:40:11 +08:00
|
|
|
if (pctx->pmeth->signctx != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
sctx = 1;
|
|
|
|
else
|
|
|
|
sctx = 0;
|
2020-01-11 03:40:11 +08:00
|
|
|
if (sigret != NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
2015-05-06 17:16:55 +08:00
|
|
|
unsigned int mdlen = 0;
|
2020-01-11 03:40:11 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
|
|
|
|
if (sctx)
|
2020-01-11 03:40:11 +08:00
|
|
|
r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
else
|
|
|
|
r = EVP_DigestFinal_ex(ctx, md, &mdlen);
|
|
|
|
} else {
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
|
2020-01-11 03:40:11 +08:00
|
|
|
|
2017-06-13 00:05:19 +08:00
|
|
|
if (tmp_ctx == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2017-06-13 00:05:19 +08:00
|
|
|
if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
|
|
|
|
EVP_MD_CTX_free(tmp_ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
if (sctx)
|
2015-11-27 21:17:50 +08:00
|
|
|
r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
|
|
|
|
sigret, siglen, tmp_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
else
|
2015-11-27 21:17:50 +08:00
|
|
|
r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(tmp_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
if (sctx || !r)
|
|
|
|
return r;
|
2020-01-11 03:40:11 +08:00
|
|
|
if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (sctx) {
|
|
|
|
if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
int s = EVP_MD_size(ctx->digest);
|
2020-01-11 03:40:11 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2006-05-25 01:30:09 +08:00
|
|
|
|
2017-05-08 19:50:13 +08:00
|
|
|
int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
|
|
|
|
const unsigned char *tbs, size_t tbslen)
|
|
|
|
{
|
2020-03-05 23:40:48 +08:00
|
|
|
EVP_PKEY_CTX *pctx = ctx->pctx;
|
|
|
|
|
|
|
|
if (pctx != NULL
|
|
|
|
&& pctx->operation == EVP_PKEY_OP_SIGNCTX
|
|
|
|
&& pctx->op.sig.sigprovctx != NULL
|
|
|
|
&& pctx->op.sig.signature != NULL) {
|
|
|
|
if (pctx->op.sig.signature->digest_sign != NULL)
|
|
|
|
return pctx->op.sig.signature->digest_sign(pctx->op.sig.sigprovctx,
|
|
|
|
sigret, siglen, SIZE_MAX,
|
|
|
|
tbs, tbslen);
|
|
|
|
} else {
|
|
|
|
/* legacy */
|
|
|
|
if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
|
|
|
|
return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
|
|
|
|
}
|
|
|
|
|
2017-05-08 19:50:13 +08:00
|
|
|
if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
|
|
|
|
return 0;
|
|
|
|
return EVP_DigestSignFinal(ctx, sigret, siglen);
|
|
|
|
}
|
|
|
|
|
2013-11-15 05:00:40 +08:00
|
|
|
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t siglen)
|
|
|
|
{
|
|
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
2015-05-06 17:16:55 +08:00
|
|
|
int r = 0;
|
|
|
|
unsigned int mdlen = 0;
|
|
|
|
int vctx = 0;
|
2019-09-17 00:14:21 +08:00
|
|
|
EVP_PKEY_CTX *pctx = ctx->pctx;
|
|
|
|
|
|
|
|
if (pctx == NULL
|
|
|
|
|| pctx->operation != EVP_PKEY_OP_VERIFYCTX
|
|
|
|
|| pctx->op.sig.sigprovctx == NULL
|
|
|
|
|| pctx->op.sig.signature == NULL)
|
|
|
|
goto legacy;
|
|
|
|
|
|
|
|
return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.sigprovctx,
|
|
|
|
sig, siglen);
|
2008-12-30 00:11:58 +08:00
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
legacy:
|
2020-01-11 03:40:11 +08:00
|
|
|
if (pctx == NULL || pctx->pmeth == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-11 05:07:10 +08:00
|
|
|
/* do_sigver_init() checked that |digest_custom| is non-NULL */
|
|
|
|
if (pctx->flag_call_digest_custom
|
|
|
|
&& !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
|
|
|
|
return 0;
|
|
|
|
pctx->flag_call_digest_custom = 0;
|
|
|
|
|
2020-01-11 03:40:11 +08:00
|
|
|
if (pctx->pmeth->verifyctx != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
vctx = 1;
|
|
|
|
else
|
|
|
|
vctx = 0;
|
|
|
|
if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
|
2018-09-04 17:21:10 +08:00
|
|
|
if (vctx)
|
2020-01-11 03:40:11 +08:00
|
|
|
r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
|
2018-09-04 17:21:10 +08:00
|
|
|
else
|
2015-01-22 11:40:55 +08:00
|
|
|
r = EVP_DigestFinal_ex(ctx, md, &mdlen);
|
|
|
|
} else {
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
|
2017-06-13 00:05:19 +08:00
|
|
|
if (tmp_ctx == NULL)
|
|
|
|
return -1;
|
|
|
|
if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
|
|
|
|
EVP_MD_CTX_free(tmp_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
2017-06-13 00:05:19 +08:00
|
|
|
}
|
2018-09-04 17:21:10 +08:00
|
|
|
if (vctx)
|
2015-11-27 21:17:50 +08:00
|
|
|
r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
|
|
|
|
sig, siglen, tmp_ctx);
|
2018-09-04 17:21:10 +08:00
|
|
|
else
|
2015-11-27 21:17:50 +08:00
|
|
|
r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(tmp_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
if (vctx || !r)
|
|
|
|
return r;
|
2020-01-11 03:40:11 +08:00
|
|
|
return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-05-08 19:50:13 +08:00
|
|
|
|
|
|
|
int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
|
|
|
|
size_t siglen, const unsigned char *tbs, size_t tbslen)
|
|
|
|
{
|
2020-03-05 23:40:48 +08:00
|
|
|
EVP_PKEY_CTX *pctx = ctx->pctx;
|
|
|
|
|
|
|
|
if (pctx != NULL
|
|
|
|
&& pctx->operation == EVP_PKEY_OP_VERIFYCTX
|
|
|
|
&& pctx->op.sig.sigprovctx != NULL
|
|
|
|
&& pctx->op.sig.signature != NULL) {
|
|
|
|
if (pctx->op.sig.signature->digest_verify != NULL)
|
|
|
|
return pctx->op.sig.signature->digest_verify(pctx->op.sig.sigprovctx,
|
|
|
|
sigret, siglen,
|
|
|
|
tbs, tbslen);
|
|
|
|
} else {
|
|
|
|
/* legacy */
|
|
|
|
if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
|
|
|
|
return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
|
|
|
|
}
|
|
|
|
|
2017-05-08 19:50:13 +08:00
|
|
|
if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
|
|
|
|
return -1;
|
|
|
|
return EVP_DigestVerifyFinal(ctx, sigret, siglen);
|
|
|
|
}
|
2020-04-14 04:34:56 +08:00
|
|
|
#endif /* FIPS_MODULE */
|