File: hash.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (347 lines) | stat: -rw-r--r-- 8,859 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
   Copyright (C) CFEngine AS

   This file is part of CFEngine 3 - written and maintained by CFEngine AS.

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; version 3.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

  To the extent this program is licensed as part of the Enterprise
  versions of CFEngine, the applicable Commercial Open Source License
  (COSL) may apply to this file if you as a licensee so wish it. See
  included file COSL.txt.
*/

#include <platform.h>

#include <openssl/evp.h>                                       /* EVP_* */
#include <openssl/bn.h>                                        /* BN_bn2bin */

#include <alloc.h>
#include <logging.h>
#include <hash.h>

static const char *const CF_DIGEST_TYPES[10] =
{
    "md5",
    "sha224",
    "sha256",
    "sha384",
    "sha512",
    "sha1",
    "sha",
    "best",
    "crypt",
    NULL
};

static const int CF_DIGEST_SIZES[10] =
{
    CF_MD5_LEN,
    CF_SHA224_LEN,
    CF_SHA256_LEN,
    CF_SHA384_LEN,
    CF_SHA512_LEN,
    CF_SHA1_LEN,
    CF_SHA_LEN,
    CF_BEST_LEN,
    CF_CRYPT_LEN,
    CF_NO_HASH
};

struct Hash {
    unsigned char digest[EVP_MAX_MD_SIZE];
    char printable[EVP_MAX_MD_SIZE * 4];
    HashMethod method;
    HashSize size;
};

/*
 * These methods are not exported through the public API.
 * These are internal methods used by the constructors of a
 * Hash object. Do not export them since they have very little
 * meaning outside of the constructors.
 */
Hash *HashBasicInit(HashMethod method)
{
    Hash *hash = xcalloc (1, sizeof(Hash));
    hash->size = CF_DIGEST_SIZES[method];
    hash->method = method;
    return hash;
}

void HashCalculatePrintableRepresentation(Hash *hash)
{
    switch (hash->method)
    {
        case HASH_METHOD_MD5:
            strcpy(hash->printable, "MD5=");
            break;
        case HASH_METHOD_SHA224:
        case HASH_METHOD_SHA256:
        case HASH_METHOD_SHA384:
        case HASH_METHOD_SHA512:
        case HASH_METHOD_SHA:
        case HASH_METHOD_SHA1:
            strcpy(hash->printable, "SHA=");
            break;
        default:
            strcpy(hash->printable, "UNK=");
            break;
    }

    unsigned int i;
    for (i = 0; i < hash->size; i++)
    {
        snprintf(hash->printable + 4 + 2 * i,
                 sizeof(hash->printable) - (4 + 2 * i), "%02x",
                 hash->digest[i]);
    }
    hash->printable[4 + 2 * hash->size] = '\0';
}

/*
 * Constructors
 * All constructors call two common methods: HashBasicInit(...) and HashCalculatePrintableRepresentation(...).
 * Each constructor reads the data to create the Hash from different sources so after the basic
 * initialization and up to the point where the hash is computed, each follows its own path.
 */
Hash *HashNew(const char *data, const unsigned int length, HashMethod method)
{
    if (!data || (length == 0))
    {
        return NULL;
    }
    if (method >= HASH_METHOD_NONE)
    {
        return NULL;
    }
    /*
     * OpenSSL documentation marked EVP_DigestInit and EVP_DigestFinal functions as deprecated and
     * recommends moving to EVP_DigestInit_ex and EVP_DigestFinal_ex.
     */
    EVP_MD_CTX *context = NULL;
    const EVP_MD *md = NULL;
    int md_len = 0;
    md = EVP_get_digestbyname(CF_DIGEST_TYPES[method]);
    if (md == NULL)
    {
        Log(LOG_LEVEL_INFO, "Digest type %s not supported by OpenSSL library", CF_DIGEST_TYPES[method]);
        return NULL;
    }
    Hash *hash = HashBasicInit(method);
    context = EVP_MD_CTX_create();
    EVP_DigestInit_ex(context, md, NULL);
    EVP_DigestUpdate(context, data, (size_t) length);
    EVP_DigestFinal_ex(context, hash->digest, &md_len);
    EVP_MD_CTX_destroy(context);
    /* Update the printable representation */
    HashCalculatePrintableRepresentation(hash);
    /* Return the hash */
    return hash;
}

Hash *HashNewFromDescriptor(const int descriptor, HashMethod method)
{
    if (descriptor < 0)
    {
        return NULL;
    }
    if (method >= HASH_METHOD_NONE)
    {
        return NULL;
    }
    char buffer[1024];
    int read_count = 0;
    EVP_MD_CTX *context = NULL;
    const EVP_MD *md = NULL;
    int md_len = 0;
    md = EVP_get_digestbyname(CF_DIGEST_TYPES[method]);
    if (md == NULL)
    {
        Log(LOG_LEVEL_INFO, "Digest type %s not supported by OpenSSL library", CF_DIGEST_TYPES[method]);
        return NULL;
    }
    Hash *hash = HashBasicInit(method);
    context = EVP_MD_CTX_create();
    EVP_DigestInit_ex(context, md, NULL);
    do {
        read_count = read(descriptor, buffer, 1024);
        EVP_DigestUpdate(context, buffer, (size_t) read_count);
    } while (read_count > 0);
    EVP_DigestFinal_ex(context, hash->digest, &md_len);
    EVP_MD_CTX_destroy(context);
    /* Update the printable representation */
    HashCalculatePrintableRepresentation(hash);
    /* Return the hash */
    return hash;
}

Hash *HashNewFromKey(const RSA *rsa, HashMethod method)
{
    if (!rsa)
    {
        return NULL;
    }
    if (method >= HASH_METHOD_NONE)
    {
        return NULL;
    }
    EVP_MD_CTX *context = NULL;
    const EVP_MD *md = NULL;
    int md_len = 0;
    unsigned char *buffer = NULL;
    int buffer_length = 0;
    int actual_length = 0;

    if (rsa->n)
    {
        buffer_length = (size_t) BN_num_bytes(rsa->n);
    }
    else
    {
        buffer_length = 0;
    }

    if (rsa->e)
    {
        if (buffer_length < (size_t) BN_num_bytes(rsa->e))
        {
            buffer_length = (size_t) BN_num_bytes(rsa->e);
        }
    }
    md = EVP_get_digestbyname(CF_DIGEST_TYPES[method]);
    if (md == NULL)
    {
        Log(LOG_LEVEL_INFO, "Digest type %s not supported by OpenSSL library", CF_DIGEST_TYPES[method]);
        return NULL;
    }
    Hash *hash = HashBasicInit(method);
    context = EVP_MD_CTX_create();
    EVP_DigestInit_ex(context, md, NULL);
    buffer = xmalloc(buffer_length);
    actual_length = BN_bn2bin(rsa->n, buffer);
    EVP_DigestUpdate(context, buffer, actual_length);
    actual_length = BN_bn2bin(rsa->e, buffer);
    EVP_DigestUpdate(context, buffer, actual_length);
    EVP_DigestFinal_ex(context, hash->digest, &md_len);
    EVP_MD_CTX_destroy(context);
    free (buffer);
    /* Update the printable representation */
    HashCalculatePrintableRepresentation(hash);
    /* Return the hash */
    return hash;
}

void HashDestroy(Hash **hash)
{
    if (!hash || !*hash)
    {
        return;
    }
    free (*hash);
    *hash = NULL;
}

int HashCopy(Hash *origin, Hash **destination)
{
    if (!origin || !destination)
    {
        return -1;
    }
    *destination = xmalloc(sizeof(Hash));
    memcpy((*destination)->digest, origin->digest, origin->size);
    strlcpy((*destination)->printable, origin->printable, (EVP_MAX_MD_SIZE * 4));
    (*destination)->method = origin->method;
    (*destination)->size = origin->size;
    return 0;
}

int HashEqual(const Hash *a, const Hash *b)
{
    if (!a && !b)
    {
        return true;
    }
    if (!a && b)
    {
        return false;
    }
    if (a && !b)
    {
        return false;
    }
    if (a->method != b->method)
    {
        return false;
    }
    int i = 0;
    for (i = 0; i < a->size; ++i)
    {
        if (a->digest[i] != b->digest[i])
        {
            return false;
        }
    }
    return true;
}

const unsigned char *HashData(const Hash *hash, unsigned int *length)
{
    if (!hash || !length)
    {
        return NULL;
    }
    *length = hash->size;
    return hash->digest;
}

const char *HashPrintable(const Hash *hash)
{
    return hash ? hash->printable : NULL;
}

HashMethod HashType(const Hash *hash)
{
    return hash ? hash->method : HASH_METHOD_NONE;
}

HashSize HashLength(const Hash *hash)
{
    return hash ? hash->size : CF_NO_HASH;
}

/* Class methods */
HashMethod HashIdFromName(const char *hash_name)
{
    int i;
    for (i = 0; CF_DIGEST_TYPES[i] != NULL; i++)
    {
        if (hash_name && (strcmp(hash_name, CF_DIGEST_TYPES[i]) == 0))
        {
            return (HashMethod) i;
        }
    }

    return HASH_METHOD_NONE;
}

const char *HashNameFromId(HashMethod hash_id)
{
    return (hash_id >= HASH_METHOD_NONE) ? NULL : CF_DIGEST_TYPES[hash_id];
}

HashSize HashSizeFromId(HashMethod hash_id)
{
    return (hash_id >= HASH_METHOD_NONE) ? CF_NO_HASH : CF_DIGEST_SIZES[hash_id];
}