]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/a_digest.c
Add X509 related libctx changes.
[thirdparty/openssl.git] / crypto / asn1 / a_digest.c
1 /*
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <stdio.h>
14 #include <time.h>
15 #include <sys/types.h>
16
17 #include "internal/cryptlib.h"
18
19 #include <openssl/engine.h>
20 #include <openssl/err.h>
21 #include <openssl/evp.h>
22 #include <openssl/buffer.h>
23 #include <openssl/x509.h>
24 #include "crypto/x509.h"
25
26 #ifndef OPENSSL_NO_DEPRECATED_3_0
27
28 int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
29 unsigned char *md, unsigned int *len)
30 {
31 int inl;
32 unsigned char *str, *p;
33
34 inl = i2d(data, NULL);
35 if (inl <= 0) {
36 ASN1err(ASN1_F_ASN1_DIGEST, ERR_R_INTERNAL_ERROR);
37 return 0;
38 }
39 if ((str = OPENSSL_malloc(inl)) == NULL) {
40 ASN1err(ASN1_F_ASN1_DIGEST, ERR_R_MALLOC_FAILURE);
41 return 0;
42 }
43 p = str;
44 i2d(data, &p);
45
46 if (!EVP_Digest(str, inl, md, len, type, NULL)) {
47 OPENSSL_free(str);
48 return 0;
49 }
50 OPENSSL_free(str);
51 return 1;
52 }
53
54 #endif
55
56 int asn1_item_digest_with_libctx(const ASN1_ITEM *it, const EVP_MD *md,
57 void *asn, unsigned char *data,
58 unsigned int *len, OPENSSL_CTX *libctx,
59 const char *propq)
60 {
61 int i, ret = 0;
62 unsigned char *str = NULL;
63 EVP_MD *fetched_md = (EVP_MD *)md;
64
65 i = ASN1_item_i2d(asn, &str, it);
66 if (str == NULL)
67 return 0;
68
69 if (EVP_MD_provider(md) == NULL) {
70 #if !defined(OPENSSL_NO_ENGINE)
71 if (ENGINE_get_digest_engine(EVP_MD_type(md)) == NULL)
72 #endif
73 fetched_md = EVP_MD_fetch(libctx, EVP_MD_name(md), propq);
74 }
75 if (fetched_md == NULL)
76 goto err;
77
78 ret = EVP_Digest(str, i, data, len, fetched_md, NULL);
79 err:
80 OPENSSL_free(str);
81 if (fetched_md != md)
82 EVP_MD_free(fetched_md);
83 return ret;
84 }
85
86 int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *md, void *asn,
87 unsigned char *data, unsigned int *len)
88 {
89 return asn1_item_digest_with_libctx(it, md, asn, data, len, NULL, NULL);
90 }
91