| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/x509.h> |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <sys/types.h> |
| 8 |
|
| 9 |
#include <openssl/bn.h> |
| 10 |
#include <openssl/digest.h> |
| 11 |
#include <openssl/err.h> |
| 12 |
#include <openssl/evp.h> |
| 13 |
#include <openssl/mem.h> |
| 14 |
#include <openssl/obj.h> |
| 15 |
|
| 16 |
#include "internal.h" |
| 17 |
|
| 18 |
int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *a, |
| 19 |
const ASN1_BIT_STRING *signature, void *asn, |
| 20 |
EVP_PKEY *pkey) { |
| 21 |
if (!pkey) { |
| 22 |
OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); |
| 23 |
return 0; |
| 24 |
} |
| 25 |
|
| 26 |
size_t sig_len; |
| 27 |
if (signature->type == V_ASN1_BIT_STRING) { |
| 28 |
if (!ASN1_BIT_STRING_num_bytes(signature, &sig_len)) { |
| 29 |
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_BIT_STRING_BITS_LEFT); |
| 30 |
return 0; |
| 31 |
} |
| 32 |
} else { |
| 33 |
sig_len = (size_t)ASN1_STRING_length(signature); |
| 34 |
} |
| 35 |
|
| 36 |
EVP_MD_CTX ctx; |
| 37 |
uint8_t *buf_in = NULL; |
| 38 |
int ret = 0, inl = 0; |
| 39 |
EVP_MD_CTX_init(&ctx); |
| 40 |
|
| 41 |
if (!x509_digest_verify_init(&ctx, a, pkey)) { |
| 42 |
goto err; |
| 43 |
} |
| 44 |
|
| 45 |
inl = ASN1_item_i2d(asn, &buf_in, it); |
| 46 |
|
| 47 |
if (buf_in == NULL) { |
| 48 |
goto err; |
| 49 |
} |
| 50 |
|
| 51 |
if (!EVP_DigestVerify(&ctx, ASN1_STRING_get0_data(signature), sig_len, buf_in, |
| 52 |
inl)) { |
| 53 |
OPENSSL_PUT_ERROR(X509, ERR_R_EVP_LIB); |
| 54 |
goto err; |
| 55 |
} |
| 56 |
|
| 57 |
ret = 1; |
| 58 |
|
| 59 |
err: |
| 60 |
OPENSSL_free(buf_in); |
| 61 |
EVP_MD_CTX_cleanup(&ctx); |
| 62 |
return ret; |
| 63 |
} |