]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_digest.c
Rename all getters to use get/get0 in name
[thirdparty/openssl.git] / crypto / asn1 / a_digest.c
CommitLineData
2039c421 1/*
3c2bdd7d 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
6725682d
SL
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
d02b48c6
RE
13#include <stdio.h>
14#include <time.h>
b379fe6c 15#include <sys/types.h>
d02b48c6 16
b39fc560 17#include "internal/cryptlib.h"
17f389bb 18
6725682d 19#include <openssl/engine.h>
a0e7c8ee 20#include <openssl/err.h>
ec577822 21#include <openssl/evp.h>
ec577822 22#include <openssl/buffer.h>
1d5edd08 23#include <openssl/x509.h>
6725682d 24#include "crypto/x509.h"
d02b48c6 25
12d99aac 26#ifndef OPENSSL_NO_DEPRECATED_3_0
73e92de5 27
8bb826ee 28int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
0f113f3e
MC
29 unsigned char *md, unsigned int *len)
30{
da84249b 31 int inl;
0f113f3e 32 unsigned char *str, *p;
d02b48c6 33
da84249b
F
34 inl = i2d(data, NULL);
35 if (inl <= 0) {
9311d0c4 36 ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
da84249b
F
37 return 0;
38 }
39 if ((str = OPENSSL_malloc(inl)) == NULL) {
9311d0c4 40 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
26a7d938 41 return 0;
0f113f3e
MC
42 }
43 p = str;
44 i2d(data, &p);
d02b48c6 45
da84249b 46 if (!EVP_Digest(str, inl, md, len, type, NULL)) {
83b4049a 47 OPENSSL_free(str);
0f113f3e 48 return 0;
83b4049a 49 }
0f113f3e 50 OPENSSL_free(str);
208fb891 51 return 1;
0f113f3e 52}
d02b48c6 53
73e92de5
DSH
54#endif
55
adf7e6d1
SL
56int ossl_asn1_item_digest_ex(const ASN1_ITEM *it, const EVP_MD *md, void *asn,
57 unsigned char *data, unsigned int *len,
58 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e 59{
6725682d 60 int i, ret = 0;
0f113f3e 61 unsigned char *str = NULL;
6725682d 62 EVP_MD *fetched_md = (EVP_MD *)md;
09ab755c 63
0f113f3e 64 i = ASN1_item_i2d(asn, &str, it);
e499a64b 65 if (i < 0 || str == NULL)
26a7d938 66 return 0;
09ab755c 67
ed576acd 68 if (EVP_MD_get0_provider(md) == NULL) {
6725682d 69#if !defined(OPENSSL_NO_ENGINE)
ed576acd 70 ENGINE *tmpeng = ENGINE_get_digest_engine(EVP_MD_get_type(md));
0f9fdefe
MC
71
72 if (tmpeng != NULL)
73 ENGINE_finish(tmpeng);
74 else
6725682d 75#endif
ed576acd 76 fetched_md = EVP_MD_fetch(libctx, EVP_MD_get0_name(md), propq);
83b4049a 77 }
2ec64916
P
78 if (fetched_md == NULL)
79 goto err;
6725682d
SL
80
81 ret = EVP_Digest(str, i, data, len, fetched_md, NULL);
82err:
0f113f3e 83 OPENSSL_free(str);
6725682d
SL
84 if (fetched_md != md)
85 EVP_MD_free(fetched_md);
86 return ret;
0f113f3e 87}
6725682d
SL
88
89int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *md, void *asn,
90 unsigned char *data, unsigned int *len)
91{
adf7e6d1 92 return ossl_asn1_item_digest_ex(it, md, asn, data, len, NULL, NULL);
6725682d
SL
93}
94