Free buffer on error in a2i_ASN1_INTEGER()
[openssl.git] / crypto / asn1 / a_digest.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include <stdio.h>
11 #include <time.h>
12
13 #include "internal/cryptlib.h"
14
15 #ifndef NO_SYS_TYPES_H
16 # include <sys/types.h>
17 #endif
18
19 #include <openssl/err.h>
20 #include <openssl/evp.h>
21 #include <openssl/buffer.h>
22 #include <openssl/x509.h>
23
24 #ifndef NO_ASN1_OLD
25
26 int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
27                 unsigned char *md, unsigned int *len)
28 {
29     int i;
30     unsigned char *str, *p;
31
32     i = i2d(data, NULL);
33     if ((str = OPENSSL_malloc(i)) == NULL) {
34         ASN1err(ASN1_F_ASN1_DIGEST, ERR_R_MALLOC_FAILURE);
35         return (0);
36     }
37     p = str;
38     i2d(data, &p);
39
40     if (!EVP_Digest(str, i, md, len, type, NULL))
41         return 0;
42     OPENSSL_free(str);
43     return (1);
44 }
45
46 #endif
47
48 int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type, void *asn,
49                      unsigned char *md, unsigned int *len)
50 {
51     int i;
52     unsigned char *str = NULL;
53
54     i = ASN1_item_i2d(asn, &str, it);
55     if (!str)
56         return (0);
57
58     if (!EVP_Digest(str, i, md, len, type, NULL))
59         return 0;
60     OPENSSL_free(str);
61     return (1);
62 }