2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include <sys/types.h>
14 #include "internal/cryptlib.h"
16 #include <openssl/bn.h>
17 #include <openssl/x509.h>
18 #include <openssl/objects.h>
19 #include <openssl/buffer.h>
20 #include <openssl/evp.h>
21 #include "crypto/asn1.h"
22 #include "crypto/evp.h"
24 #ifndef OPENSSL_NO_DEPRECATED_3_0
26 int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature,
27 char *data, EVP_PKEY *pkey)
29 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
31 unsigned char *p, *buf_in = NULL;
35 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_MALLOC_FAILURE);
38 i = OBJ_obj2nid(a->algorithm);
39 type = EVP_get_digestbyname(OBJ_nid2sn(i));
41 ASN1err(ASN1_F_ASN1_VERIFY, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
45 if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
46 ASN1err(ASN1_F_ASN1_VERIFY, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
50 inl = i2d(data, NULL);
52 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_INTERNAL_ERROR);
55 buf_in = OPENSSL_malloc((unsigned int)inl);
57 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_MALLOC_FAILURE);
63 ret = EVP_VerifyInit_ex(ctx, type, NULL)
64 && EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl);
66 OPENSSL_clear_free(buf_in, (unsigned int)inl);
69 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB);
74 if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data,
75 (unsigned int)signature->length, pkey) <= 0) {
76 ASN1err(ASN1_F_ASN1_VERIFY, ERR_R_EVP_LIB);
88 int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg,
89 const ASN1_BIT_STRING *signature, const void *data,
92 return ASN1_item_verify_with_libctx(it, alg, signature, data, NULL, pkey,
96 int ASN1_item_verify_with_libctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
97 const ASN1_BIT_STRING *signature,
99 const ASN1_OCTET_STRING *id, EVP_PKEY *pkey,
100 OPENSSL_CTX *libctx, const char *propq)
105 if ((ctx = evp_md_ctx_new_with_libctx(pkey, id, libctx, propq)) != NULL) {
106 rv = ASN1_item_verify_ctx(it, alg, signature, data, ctx);
107 EVP_PKEY_CTX_free(EVP_MD_CTX_pkey_ctx(ctx));
108 EVP_MD_CTX_free(ctx);
113 int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg,
114 const ASN1_BIT_STRING *signature, const void *data,
118 unsigned char *buf_in = NULL;
119 int ret = -1, inl = 0;
123 pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
126 ASN1err(0, ERR_R_PASSED_NULL_PARAMETER);
130 if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) {
131 ASN1err(0, ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
135 /* Convert signature OID into digest and public key OIDs */
136 if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)) {
137 ASN1err(0, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
141 if (mdnid == NID_undef) {
142 if (pkey->ameth == NULL || pkey->ameth->item_verify == NULL) {
143 ASN1err(0, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM);
146 ret = pkey->ameth->item_verify(ctx, it, data, alg, signature, pkey);
148 * Return values meaning:
150 * 1: method does everything.
151 * 2: carry on as normal, method has called EVP_DigestVerifyInit()
154 ASN1err(0, ERR_R_EVP_LIB);
158 const EVP_MD *type = EVP_get_digestbynid(mdnid);
161 ASN1err(0, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
165 /* Check public key OID matches public key type */
166 if (EVP_PKEY_type(pknid) != pkey->ameth->pkey_id) {
167 ASN1err(0, ASN1_R_WRONG_PUBLIC_KEY_TYPE);
171 if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) {
172 ASN1err(0, ERR_R_EVP_LIB);
178 inl = ASN1_item_i2d(data, &buf_in, it);
180 ASN1err(0, ERR_R_INTERNAL_ERROR);
183 if (buf_in == NULL) {
184 ASN1err(0, ERR_R_MALLOC_FAILURE);
189 ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length,
192 ASN1err(0, ERR_R_EVP_LIB);
197 OPENSSL_clear_free(buf_in, inll);