2016-05-18 02:51:34 +08:00
|
|
|
/*
|
2021-06-17 20:24:59 +08:00
|
|
|
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:17:34 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:34 +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
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2017-08-25 20:51:45 +08:00
|
|
|
#include <sys/types.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-09-12 01:54:18 +08:00
|
|
|
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/evp.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/asn1.h"
|
|
|
|
#include "crypto/evp.h"
|
2021-05-25 19:38:19 +08:00
|
|
|
#include "crypto/rsa.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2020-02-25 05:33:52 +08:00
|
|
|
#ifndef OPENSSL_NO_DEPRECATED_3_0
|
2000-12-29 06:24:50 +08:00
|
|
|
|
2005-03-31 21:57:54 +08:00
|
|
|
int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
|
2015-01-22 11:40:55 +08:00
|
|
|
char *data, EVP_PKEY *pkey)
|
|
|
|
{
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
2015-01-22 11:40:55 +08:00
|
|
|
const EVP_MD *type;
|
|
|
|
unsigned char *p, *buf_in = NULL;
|
|
|
|
int ret = -1, i, inl;
|
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
if (ctx == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
|
2015-11-27 21:02:12 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
i = OBJ_obj2nid(a->algorithm);
|
|
|
|
type = EVP_get_digestbyname(OBJ_nid2sn(i));
|
|
|
|
if (type == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
inl = i2d(data, NULL);
|
2018-10-01 04:39:38 +08:00
|
|
|
if (inl <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
|
2018-10-01 04:39:38 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
buf_in = OPENSSL_malloc((unsigned int)inl);
|
2022-09-29 19:57:34 +08:00
|
|
|
if (buf_in == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
p = buf_in;
|
|
|
|
|
|
|
|
i2d(data, &p);
|
2015-11-27 21:02:12 +08:00
|
|
|
ret = EVP_VerifyInit_ex(ctx, type, NULL)
|
|
|
|
&& EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-05-01 05:57:32 +08:00
|
|
|
OPENSSL_clear_free(buf_in, (unsigned int)inl);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (!ret) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ret = -1;
|
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data,
|
2015-01-22 11:40:55 +08:00
|
|
|
(unsigned int)signature->length, pkey) <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
err:
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-12-29 03:18:48 +08:00
|
|
|
|
2000-12-29 06:24:50 +08:00
|
|
|
#endif
|
|
|
|
|
2020-05-15 03:09:49 +08:00
|
|
|
int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg,
|
|
|
|
const ASN1_BIT_STRING *signature, const void *data,
|
|
|
|
EVP_PKEY *pkey)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return ASN1_item_verify_ex(it, alg, signature, data, NULL, pkey, NULL, NULL);
|
2020-05-15 03:09:49 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
int ASN1_item_verify_ex(const ASN1_ITEM *it, const X509_ALGOR *alg,
|
|
|
|
const ASN1_BIT_STRING *signature, const void *data,
|
|
|
|
const ASN1_OCTET_STRING *id, EVP_PKEY *pkey,
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
2020-05-15 03:09:49 +08:00
|
|
|
{
|
|
|
|
EVP_MD_CTX *ctx;
|
2020-01-25 01:04:19 +08:00
|
|
|
int rv = -1;
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
if ((ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq)) != NULL) {
|
2020-05-15 03:09:49 +08:00
|
|
|
rv = ASN1_item_verify_ctx(it, alg, signature, data, ctx);
|
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
|
|
|
EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx));
|
2020-05-15 03:09:49 +08:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2020-01-25 01:04:19 +08:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:09:49 +08:00
|
|
|
int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
|
|
|
|
const ASN1_BIT_STRING *signature, const void *data,
|
2020-01-25 01:04:19 +08:00
|
|
|
EVP_MD_CTX *ctx)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char *buf_in = NULL;
|
2017-05-08 19:50:13 +08:00
|
|
|
int ret = -1, inl = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
int mdnid, pknid;
|
2018-10-01 04:39:38 +08:00
|
|
|
size_t inll = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
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
|
|
|
pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx));
|
2020-01-25 01:04:19 +08:00
|
|
|
|
2018-10-26 21:34:08 +08:00
|
|
|
if (pkey == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert signature OID into digest and public key OIDs */
|
2020-05-15 03:09:49 +08:00
|
|
|
if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2020-01-25 01:04:19 +08:00
|
|
|
|
2021-05-25 19:38:19 +08:00
|
|
|
if (mdnid == NID_undef && evp_pkey_is_legacy(pkey)) {
|
2019-09-17 03:28:57 +08:00
|
|
|
if (pkey->ameth == NULL || pkey->ameth->item_verify == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2020-05-15 03:09:49 +08:00
|
|
|
ret = pkey->ameth->item_verify(ctx, it, data, alg, signature, pkey);
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2020-01-25 01:04:19 +08:00
|
|
|
* Return values meaning:
|
|
|
|
* <=0: error.
|
|
|
|
* 1: method does everything.
|
|
|
|
* 2: carry on as normal, method has called EVP_DigestVerifyInit()
|
2015-01-22 11:40:55 +08:00
|
|
|
*/
|
2020-01-25 01:04:19 +08:00
|
|
|
if (ret <= 0)
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
|
2020-01-25 01:04:19 +08:00
|
|
|
if (ret <= 1)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
} else {
|
2021-05-25 19:38:19 +08:00
|
|
|
const EVP_MD *type = NULL;
|
2018-10-01 04:39:38 +08:00
|
|
|
|
2021-05-25 19:38:19 +08:00
|
|
|
/*
|
|
|
|
* We don't yet have the ability for providers to be able to handle
|
|
|
|
* X509_ALGOR style parameters. Fortunately the only one that needs this
|
|
|
|
* so far is RSA-PSS, so we just special case this for now. In some
|
|
|
|
* future version of OpenSSL we should push this to the provider.
|
|
|
|
*/
|
|
|
|
if (mdnid == NID_undef && pknid == EVP_PKEY_RSA_PSS) {
|
|
|
|
if (!EVP_PKEY_is_a(pkey, "RSA") && !EVP_PKEY_is_a(pkey, "RSA-PSS")) {
|
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* This function also calls EVP_DigestVerifyInit */
|
|
|
|
if (ossl_rsa_pss_to_ctx(ctx, NULL, alg, pkey) <= 0) {
|
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Check public key OID matches public key type */
|
|
|
|
if (!EVP_PKEY_is_a(pkey, OBJ_nid2sn(pknid))) {
|
|
|
|
ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mdnid != NID_undef) {
|
|
|
|
type = EVP_get_digestbynid(mdnid);
|
|
|
|
if (type == NULL) {
|
2023-06-15 23:03:21 +08:00
|
|
|
ERR_raise_data(ERR_LIB_ASN1,
|
|
|
|
ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM,
|
|
|
|
"nid=0x%x", mdnid);
|
2021-05-25 19:38:19 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that some algorithms (notably Ed25519 and Ed448) may allow
|
|
|
|
* a NULL digest value.
|
|
|
|
*/
|
|
|
|
if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) {
|
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
|
|
|
|
ret = 0;
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:09:49 +08:00
|
|
|
inl = ASN1_item_i2d(data, &buf_in, it);
|
2018-10-01 04:39:38 +08:00
|
|
|
if (inl <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
|
2018-10-01 04:39:38 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
if (buf_in == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2018-10-01 04:39:38 +08:00
|
|
|
inll = inl;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-05-08 19:50:13 +08:00
|
|
|
ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length,
|
|
|
|
buf_in, inl);
|
|
|
|
if (ret <= 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
err:
|
2018-10-01 04:39:38 +08:00
|
|
|
OPENSSL_clear_free(buf_in, inll);
|
2017-05-08 19:50:13 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|