1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/x509.h>
16 
17 #include <openssl/asn1.h>
18 #include <openssl/digest.h>
19 #include <openssl/err.h>
20 #include <openssl/mem.h>
21 
ASN1_digest(i2d_of_void * i2d,const EVP_MD * type,char * data,unsigned char * md,unsigned int * len)22 int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,
23                 unsigned char *md, unsigned int *len) {
24   int i, ret;
25   unsigned char *str, *p;
26 
27   i = i2d(data, NULL);
28   if ((str = (unsigned char *)OPENSSL_malloc(i)) == NULL) {
29     return 0;
30   }
31   p = str;
32   i2d(data, &p);
33 
34   ret = EVP_Digest(str, i, md, len, type, NULL);
35   OPENSSL_free(str);
36   return ret;
37 }
38 
ASN1_item_digest(const ASN1_ITEM * it,const EVP_MD * type,void * asn,unsigned char * md,unsigned int * len)39 int ASN1_item_digest(const ASN1_ITEM *it, const EVP_MD *type, void *asn,
40                      unsigned char *md, unsigned int *len) {
41   int i, ret;
42   unsigned char *str = NULL;
43 
44   i = ASN1_item_i2d(reinterpret_cast<ASN1_VALUE *>(asn), &str, it);
45   if (!str) {
46     return 0;
47   }
48 
49   ret = EVP_Digest(str, i, md, len, type, NULL);
50   OPENSSL_free(str);
51   return ret;
52 }
53