2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2021-02-18 22:57:13 +08:00
|
|
|
* Copyright 2006-2021 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>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/evp.h"
|
2019-09-17 00:14:21 +08:00
|
|
|
#include "internal/provider.h"
|
2021-05-18 03:38:51 +08:00
|
|
|
#include "internal/numbers.h" /* includes SIZE_MAX */
|
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)
|
|
|
|
{
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_ONLY_ONESHOT_SUPPORTED);
|
2017-05-20 04:31:46 +08:00
|
|
|
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-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx, const char *props,
|
2021-03-02 18:20:25 +08:00
|
|
|
ENGINE *e, EVP_PKEY *pkey, int ver,
|
2021-07-14 08:03:22 +08:00
|
|
|
const OSSL_PARAM params[])
|
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;
|
2021-10-01 20:05:02 +08:00
|
|
|
const OSSL_PROVIDER *tmp_prov = NULL;
|
2020-01-14 21:11:47 +08:00
|
|
|
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;
|
2021-11-04 18:06:26 +08:00
|
|
|
int ret, iter, reinit = 1;
|
2019-09-17 00:14:21 +08:00
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
if (ctx->algctx != NULL) {
|
2019-09-26 21:31:56 +08:00
|
|
|
if (!ossl_assert(ctx->digest != NULL)) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ctx->digest->freectx != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
ctx->digest->freectx(ctx->algctx);
|
|
|
|
ctx->algctx = NULL;
|
2019-09-26 21:31:56 +08:00
|
|
|
}
|
|
|
|
|
2020-03-12 22:39:47 +08:00
|
|
|
if (ctx->pctx == NULL) {
|
2021-11-04 18:06:26 +08:00
|
|
|
reinit = 0;
|
2021-02-15 18:53:45 +08:00
|
|
|
if (e == NULL)
|
2020-03-12 22:39:47 +08:00
|
|
|
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;
|
2020-01-11 00:50:03 +08:00
|
|
|
ERR_set_mark();
|
|
|
|
|
2020-09-30 23:22:27 +08:00
|
|
|
if (evp_pkey_ctx_is_legacy(locpctx))
|
2019-09-17 00:14:21 +08:00
|
|
|
goto legacy;
|
|
|
|
|
2021-11-04 18:06:26 +08:00
|
|
|
/* do not reinitialize if pkey is set or operation is different */
|
|
|
|
if (reinit
|
|
|
|
&& (pkey != NULL
|
|
|
|
|| locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX
|
|
|
|
: EVP_PKEY_OP_SIGNCTX)
|
|
|
|
|| (signature = locpctx->op.sig.signature) == NULL
|
|
|
|
|| locpctx->op.sig.algctx == NULL))
|
|
|
|
reinit = 0;
|
|
|
|
|
|
|
|
if (props == NULL)
|
|
|
|
props = locpctx->propquery;
|
|
|
|
|
2021-10-01 21:02:15 +08:00
|
|
|
if (locpctx->pkey == NULL) {
|
|
|
|
ERR_clear_last_mark();
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:06:26 +08:00
|
|
|
if (!reinit) {
|
|
|
|
evp_pkey_ctx_free_old_ops(locpctx);
|
|
|
|
} else {
|
|
|
|
if (mdname == NULL && type == NULL)
|
|
|
|
mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
|
|
|
|
goto reinitialize;
|
|
|
|
}
|
|
|
|
|
2020-02-21 03:26:16 +08:00
|
|
|
/*
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
* Try to derive the supported signature from |locpctx->keymgmt|.
|
2020-02-21 03:26:16 +08:00
|
|
|
*/
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
if (!ossl_assert(locpctx->pkey->keymgmt == NULL
|
|
|
|
|| locpctx->pkey->keymgmt == locpctx->keymgmt)) {
|
2021-07-22 21:25:32 +08:00
|
|
|
ERR_clear_last_mark();
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
2020-07-28 23:47:03 +08:00
|
|
|
goto err;
|
|
|
|
}
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
|
|
|
|
OSSL_OP_SIGNATURE);
|
|
|
|
if (supported_sig == NULL) {
|
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
|
|
|
|
|
|
|
/*
|
2021-10-01 20:05:02 +08:00
|
|
|
* We perform two iterations:
|
|
|
|
*
|
|
|
|
* 1. Do the normal signature fetch, using the fetching data given by
|
|
|
|
* the EVP_PKEY_CTX.
|
|
|
|
* 2. Do the provider specific signature fetch, from the same provider
|
|
|
|
* as |ctx->keymgmt|
|
|
|
|
*
|
|
|
|
* We then try to fetch the keymgmt from the same provider as the
|
|
|
|
* signature, and try to export |ctx->pkey| to that keymgmt (when
|
|
|
|
* this keymgmt happens to be the same as |ctx->keymgmt|, the export
|
|
|
|
* is a no-op, but we call it anyway to not complicate the code even
|
|
|
|
* more).
|
|
|
|
* If the export call succeeds (returns a non-NULL provider key pointer),
|
|
|
|
* we're done and can perform the operation itself. If not, we perform
|
|
|
|
* the second iteration, or jump to legacy.
|
2020-01-14 21:11:47 +08:00
|
|
|
*/
|
2021-10-01 20:05:02 +08:00
|
|
|
for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
|
|
|
|
EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're on the second iteration, free the results from the first.
|
|
|
|
* They are NULL on the first iteration, so no need to check what
|
|
|
|
* iteration we're on.
|
|
|
|
*/
|
|
|
|
EVP_SIGNATURE_free(signature);
|
|
|
|
EVP_KEYMGMT_free(tmp_keymgmt);
|
|
|
|
|
|
|
|
switch (iter) {
|
|
|
|
case 1:
|
|
|
|
signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
|
|
|
|
locpctx->propquery);
|
|
|
|
if (signature != NULL)
|
|
|
|
tmp_prov = EVP_SIGNATURE_get0_provider(signature);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
|
|
|
|
signature =
|
|
|
|
evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
|
|
|
|
supported_sig, locpctx->propquery);
|
|
|
|
if (signature == NULL)
|
|
|
|
goto legacy;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (signature == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the key is provided, either natively, or as a cached
|
|
|
|
* export. We start by fetching the keymgmt with the same name as
|
|
|
|
* |locpctx->pkey|, but from the provider of the signature method, using
|
|
|
|
* the same property query as when fetching the signature method.
|
|
|
|
* With the keymgmt we found (if we did), we try to export |locpctx->pkey|
|
|
|
|
* to it (evp_pkey_export_to_provider() is smart enough to only actually
|
|
|
|
|
|
|
|
* export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt)
|
|
|
|
*/
|
|
|
|
tmp_keymgmt_tofree = tmp_keymgmt =
|
|
|
|
evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
|
|
|
|
EVP_KEYMGMT_get0_name(locpctx->keymgmt),
|
|
|
|
locpctx->propquery);
|
|
|
|
if (tmp_keymgmt != NULL)
|
|
|
|
provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
|
|
|
|
&tmp_keymgmt, locpctx->propquery);
|
|
|
|
if (tmp_keymgmt == NULL)
|
|
|
|
EVP_KEYMGMT_free(tmp_keymgmt_tofree);
|
|
|
|
}
|
2019-10-30 23:59:34 +08:00
|
|
|
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
if (provkey == NULL) {
|
2021-10-01 20:05:02 +08:00
|
|
|
EVP_SIGNATURE_free(signature);
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
ERR_clear_last_mark();
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-01-11 00:50:03 +08:00
|
|
|
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
|
|
|
|
|
|
|
locpctx->op.sig.signature = signature;
|
2019-10-30 23:59:34 +08:00
|
|
|
locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
|
|
|
|
: EVP_PKEY_OP_SIGNCTX;
|
2021-05-14 11:08:42 +08:00
|
|
|
locpctx->op.sig.algctx
|
2020-05-07 03:44:58 +08:00
|
|
|
= signature->newctx(ossl_provider_ctx(signature->prov), props);
|
2021-05-14 11:08:42 +08:00
|
|
|
if (locpctx->op.sig.algctx == NULL) {
|
2019-09-17 00:14:21 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2021-11-04 18:06:26 +08:00
|
|
|
|
|
|
|
reinitialize:
|
|
|
|
if (pctx != NULL)
|
|
|
|
*pctx = locpctx;
|
|
|
|
|
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)
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
mdname = canon_mdname(EVP_MD_get0_name(type));
|
2019-09-17 00:14:21 +08:00
|
|
|
} else {
|
2021-11-04 18:06:26 +08:00
|
|
|
if (mdname == NULL && !reinit) {
|
2020-04-20 15:29:16 +08:00
|
|
|
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) {
|
2020-09-03 18:50:30 +08:00
|
|
|
/*
|
|
|
|
* We're about to get a new digest so clear anything associated with
|
|
|
|
* an old digest.
|
|
|
|
*/
|
|
|
|
evp_md_ctx_clear_digest(ctx, 1);
|
|
|
|
|
2020-09-14 23:33:29 +08:00
|
|
|
/* legacy code support for engines */
|
|
|
|
ERR_set_mark();
|
2020-01-13 15:49:44 +08:00
|
|
|
/*
|
Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new. Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field. The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md(). Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md(). Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
if (flags & x) --> if ((flags & x) != 0)
if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
2021-02-17 06:51:56 +08:00
|
|
|
* This might be requested by a later call to EVP_MD_CTX_get0_md().
|
2020-01-13 15:49:44 +08:00
|
|
|
* In that case the "explicit fetch" rules apply for that
|
|
|
|
* function (as per man pages), i.e. the ref count is not updated
|
2022-01-03 07:00:27 +08:00
|
|
|
* so the EVP_MD should not be used beyond the lifetime of the
|
2020-01-13 15:49:44 +08:00
|
|
|
* EVP_MD_CTX.
|
|
|
|
*/
|
2020-09-14 23:33:29 +08:00
|
|
|
ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
|
|
|
|
if (ctx->fetched_digest != NULL) {
|
|
|
|
ctx->digest = ctx->reqdigest = ctx->fetched_digest;
|
|
|
|
} else {
|
|
|
|
/* legacy engine support : remove the mark when this is deleted */
|
|
|
|
ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname);
|
|
|
|
if (ctx->digest == NULL) {
|
|
|
|
(void)ERR_clear_last_mark();
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2020-09-03 18:50:30 +08:00
|
|
|
}
|
2020-09-14 23:33:29 +08:00
|
|
|
(void)ERR_pop_to_mark();
|
2020-01-13 15:49:44 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
2021-05-14 11:08:42 +08:00
|
|
|
ret = signature->digest_verify_init(locpctx->op.sig.algctx,
|
2021-03-02 18:20:25 +08:00
|
|
|
mdname, provkey, params);
|
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;
|
|
|
|
}
|
2021-05-14 11:08:42 +08:00
|
|
|
ret = signature->digest_sign_init(locpctx->op.sig.algctx,
|
2021-03-02 18:20:25 +08:00
|
|
|
mdname, provkey, params);
|
2019-09-17 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
2021-07-07 14:32:16 +08:00
|
|
|
/*
|
|
|
|
* If the operation was not a success and no digest was found, an error
|
|
|
|
* needs to be raised.
|
|
|
|
*/
|
|
|
|
if (ret > 0 || mdname != NULL)
|
|
|
|
goto end;
|
|
|
|
if (type == NULL) /* This check is redundant but clarifies matters */
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
|
2020-09-02 21:54:13 +08:00
|
|
|
|
2019-09-17 00:14:21 +08:00
|
|
|
err:
|
|
|
|
evp_pkey_ctx_free_old_ops(locpctx);
|
|
|
|
locpctx->operation = EVP_PKEY_OP_UNDEFINED;
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
EVP_KEYMGMT_free(tmp_keymgmt);
|
2019-09-17 00:14:21 +08:00
|
|
|
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
|
|
|
/*
|
|
|
|
* If we don't have the full support we need with provided methods,
|
|
|
|
* let's go see if legacy does.
|
|
|
|
*/
|
|
|
|
ERR_pop_to_mark();
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
EVP_KEYMGMT_free(tmp_keymgmt);
|
|
|
|
tmp_keymgmt = NULL;
|
2020-01-11 00:50:03 +08:00
|
|
|
|
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) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, 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) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
|
2015-01-22 11:40:55 +08:00
|
|
|
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
|
|
|
|
2020-09-02 21:54:13 +08:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
end:
|
|
|
|
#ifndef FIPS_MODULE
|
|
|
|
if (ret > 0)
|
|
|
|
ret = evp_pkey_ctx_use_cached_data(locpctx);
|
|
|
|
#endif
|
|
|
|
|
EVP: Reverse the fetch logic in all pkey using functionality
In all initializing functions for functionality that use an EVP_PKEY, the
coded logic was to find an KEYMGMT implementation first, and then try to
find the operation method (for example, SIGNATURE implementation) in the
same provider.
This implies that in providers where there is a KEYMGMT implementation,
there must also be a SIGNATURE implementation, along with a KEYEXCH,
ASYM_CIPHER, etc implementation.
The intended design was, however, the opposite implication, i.e. that
where there is a SIGNATURE implementation, there must also be KEYMGMT.
This change reverses the logic of the code to be closer to the intended
design.
There is a consequence; we now use the query_operation_name function from
the KEYMGMT of the EVP_PKEY given by the EVP_PKEY_CTX (ultimately given by
the application). Previously, we used the query_operation_name function
from the KEYMGMT found alongside the SIGNATURE implementation.
Another minor consequence is that the |keymgmt| field in EVP_PKEY_CTX
is now always a reference to the KEYMGMT of the |pkey| field if that
one is given (|pkey| isn't NULL) and is provided (|pkey->keymgmt|
isn't NULL).
Fixes #16614
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)
2021-10-01 14:57:03 +08:00
|
|
|
EVP_KEYMGMT_free(tmp_keymgmt);
|
2020-09-02 21:54:13 +08:00
|
|
|
return ret > 0 ? 1 : 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2006-05-25 01:30:09 +08:00
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
2020-10-15 17:55:50 +08:00
|
|
|
const char *mdname, OSSL_LIB_CTX *libctx,
|
2021-03-02 18:20:25 +08:00
|
|
|
const char *props, EVP_PKEY *pkey,
|
2021-07-14 08:03:22 +08:00
|
|
|
const OSSL_PARAM params[])
|
2019-09-17 00:14:21 +08:00
|
|
|
{
|
2021-03-02 18:20:25 +08:00
|
|
|
return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0,
|
|
|
|
params);
|
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)
|
|
|
|
{
|
2021-03-02 18:20:25 +08:00
|
|
|
return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0,
|
|
|
|
NULL);
|
2019-09-17 00:14:21 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
2020-10-15 17:55:50 +08:00
|
|
|
const char *mdname, OSSL_LIB_CTX *libctx,
|
2021-03-02 18:20:25 +08:00
|
|
|
const char *props, EVP_PKEY *pkey,
|
2021-07-14 08:03:22 +08:00
|
|
|
const OSSL_PARAM params[])
|
2019-09-17 00:14:21 +08:00
|
|
|
{
|
2021-03-02 18:20:25 +08:00
|
|
|
return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1,
|
|
|
|
params);
|
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)
|
|
|
|
{
|
2021-03-02 18:20:25 +08:00
|
|
|
return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1,
|
|
|
|
NULL);
|
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
|
2021-05-14 11:08:42 +08:00
|
|
|
|| pctx->op.sig.algctx == NULL
|
2019-09-17 00:14:21 +08:00
|
|
|
|| 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;
|
|
|
|
}
|
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx,
|
2019-09-17 00:14:21 +08:00
|
|
|
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
|
2021-05-14 11:08:42 +08:00
|
|
|
|| pctx->op.sig.algctx == NULL
|
2019-09-17 00:14:21 +08:00
|
|
|
|| 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;
|
|
|
|
}
|
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx,
|
2019-09-17 00:14:21 +08:00
|
|
|
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;
|
2021-08-25 19:50:40 +08:00
|
|
|
EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
|
2019-09-17 00:14:21 +08:00
|
|
|
|
|
|
|
if (pctx == NULL
|
|
|
|
|| pctx->operation != EVP_PKEY_OP_SIGNCTX
|
2021-05-14 11:08:42 +08:00
|
|
|
|| pctx->op.sig.algctx == NULL
|
2019-09-17 00:14:21 +08:00
|
|
|
|| pctx->op.sig.signature == NULL)
|
|
|
|
goto legacy;
|
|
|
|
|
2021-08-25 19:50:40 +08:00
|
|
|
if (sigret == NULL || (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
|
|
|
|
return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx,
|
|
|
|
sigret, siglen,
|
2021-11-04 10:46:58 +08:00
|
|
|
(siglen == NULL) ? 0 : *siglen);
|
2021-08-25 19:50:40 +08:00
|
|
|
dctx = EVP_PKEY_CTX_dup(pctx);
|
|
|
|
if (dctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = dctx->op.sig.signature->digest_sign_final(dctx->op.sig.algctx,
|
|
|
|
sigret, siglen,
|
2021-11-04 10:46:58 +08:00
|
|
|
(siglen == NULL) ? 0 : *siglen);
|
2021-08-25 19:50:40 +08:00
|
|
|
EVP_PKEY_CTX_free(dctx);
|
|
|
|
return r;
|
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;
|
|
|
|
|
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 {
|
2021-08-25 19:50:40 +08:00
|
|
|
dctx = EVP_PKEY_CTX_dup(pctx);
|
2020-01-11 03:40:11 +08:00
|
|
|
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 {
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
int s = EVP_MD_get_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
|
2021-05-14 11:08:42 +08:00
|
|
|
&& pctx->op.sig.algctx != NULL
|
2020-03-05 23:40:48 +08:00
|
|
|
&& pctx->op.sig.signature != NULL) {
|
|
|
|
if (pctx->op.sig.signature->digest_sign != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx,
|
2021-10-07 18:33:17 +08:00
|
|
|
sigret, siglen,
|
|
|
|
sigret == NULL ? 0 : *siglen,
|
2020-03-05 23:40:48 +08:00
|
|
|
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;
|
2021-08-25 19:50:40 +08:00
|
|
|
EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
|
2019-09-17 00:14:21 +08:00
|
|
|
|
|
|
|
if (pctx == NULL
|
|
|
|
|| pctx->operation != EVP_PKEY_OP_VERIFYCTX
|
2021-05-14 11:08:42 +08:00
|
|
|
|| pctx->op.sig.algctx == NULL
|
2019-09-17 00:14:21 +08:00
|
|
|
|| pctx->op.sig.signature == NULL)
|
|
|
|
goto legacy;
|
|
|
|
|
2021-08-25 19:50:40 +08:00
|
|
|
if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
|
|
|
|
return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx,
|
|
|
|
sig, siglen);
|
|
|
|
dctx = EVP_PKEY_CTX_dup(pctx);
|
|
|
|
if (dctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = dctx->op.sig.signature->digest_verify_final(dctx->op.sig.algctx,
|
|
|
|
sig, siglen);
|
|
|
|
EVP_PKEY_CTX_free(dctx);
|
|
|
|
return r;
|
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
|
2021-05-14 11:08:42 +08:00
|
|
|
&& pctx->op.sig.algctx != NULL
|
2020-03-05 23:40:48 +08:00
|
|
|
&& pctx->op.sig.signature != NULL) {
|
|
|
|
if (pctx->op.sig.signature->digest_verify != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx,
|
2020-03-05 23:40:48 +08:00
|
|
|
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 */
|