-
Notifications
You must be signed in to change notification settings - Fork 2
/
HashOpenSSL.c
92 lines (74 loc) · 2.07 KB
/
HashOpenSSL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "HashOpenSSL.h"
#ifdef HAVE_LIBSSL
#include <openssl/evp.h>
#include <openssl/objects.h>
static void OpenSSLFreeHashCTX(HASH *Hash)
{
#ifdef HAVE_EVP_MD_CTX_FREE
EVP_MD_CTX_free(Hash->Ctx);
#else
EVP_MD_CTX_destroy(Hash->Ctx);
#endif
Hash->Ctx=NULL;
}
static int OpenSSLFinishHash(HASH *Hash, char **Digest)
{
unsigned int Len;
*Digest=SetStrLen(*Digest, EVP_MAX_MD_SIZE);
EVP_DigestFinal((EVP_MD_CTX *) Hash->Ctx, (unsigned char *) *Digest, &Len);
OpenSSLFreeHashCTX(Hash);
return((int) Len);
}
static void OpenSSLUpdateHash(HASH *Hash, const char *Bytes, int Len)
{
EVP_DigestUpdate((EVP_MD_CTX *) Hash->Ctx, Bytes, Len);
}
static int OpenSSLInitHash(HASH *Hash, const char *Name, int Size)
{
const EVP_MD *MD;
const char *p_Name;
p_Name=Name;
if (strncmp(p_Name, "openssl:", 8)==0) p_Name += 8;
MD=EVP_get_digestbyname(p_Name);
if (MD)
{
#ifdef HAVE_EVP_MD_CTX_NEW
Hash->Ctx=(EVP_MD_CTX *) EVP_MD_CTX_new();
#else
Hash->Ctx=(EVP_MD_CTX *) EVP_MD_CTX_create();
#endif
if (! EVP_DigestInit(Hash->Ctx, MD))
{
OpenSSLFreeHashCTX(Hash);
return(FALSE);
}
Hash->Update=OpenSSLUpdateHash;
Hash->Finish=OpenSSLFinishHash;
return(TRUE);
}
return(FALSE);
}
//this function gets 'called back' by the call to 'OBJ_NAME_do_all' in HashRegisterOpenSSL
//and is called for each algorithm name that openssl supports
static void OpenSSLDigestCallback(const OBJ_NAME *obj, void *arg)
{
char *Tempstr=NULL;
HASH *Hash;
Hash=(HASH *) calloc(1, sizeof(HASH));
if (OpenSSLInitHash(Hash, obj->name, 0))
{
HashRegister(obj->name, 0, OpenSSLInitHash);
Tempstr=MCopyStr(Tempstr, "openssl:", obj->name, NULL);
HashRegister(Tempstr, 0, OpenSSLInitHash);
}
OpenSSLFreeHashCTX(Hash);
Destroy(Tempstr);
}
#endif
void HashRegisterOpenSSL()
{
#ifdef HAVE_LIBSSL
OpenSSL_add_all_digests(); //make sure they're loaded
OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, OpenSSLDigestCallback, NULL);
#endif
}