~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to libclamav/asn1.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include <time.h>
26
26
 
 
27
#include <openssl/ssl.h>
 
28
#include <openssl/err.h>
 
29
#include "libclamav/crypto.h"
 
30
 
27
31
#include "asn1.h"
28
 
#include "sha1.h"
29
 
#include "md5.h"
30
32
#include "bignum.h"
31
33
#include "matcher-hash.h"
32
34
 
40
42
#define OID_1_2_840_113549_1_1_1 "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01"
41
43
#define OID_rsaEncryption OID_1_2_840_113549_1_1_1
42
44
 
 
45
#define OID_1_2_840_113549_1_1_2 "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x02"
 
46
#define OID_md2WithRSAEncryption OID_1_2_840_113549_1_1_2
 
47
 
43
48
#define OID_1_2_840_113549_1_1_4 "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x04"
44
49
#define OID_md5WithRSAEncryption OID_1_2_840_113549_1_1_4
45
50
 
46
51
#define OID_1_2_840_113549_1_1_5 "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05"
47
52
#define OID_sha1WithRSAEncryption OID_1_2_840_113549_1_1_5
48
53
 
 
54
#define OID_1_2_840_113549_1_1_11 "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x0b"
 
55
#define OID_sha256WithRSAEncryption OID_1_2_840_113549_1_1_11
 
56
 
 
57
#define OID_1_2_840_113549_1_1_13 "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x0d"
 
58
#define OID_sha512WithRSAEncryption OID_1_2_840_113549_1_1_13
 
59
 
49
60
#define OID_1_2_840_113549_1_7_1 "\x2a\x86\x48\x86\xf7\x0d\x01\x07\x01"
50
61
#define OID_pkcs7_data OID_1_2_840_113549_1_7_1
51
62
 
97
108
};
98
109
 
99
110
static int map_sha1(fmap_t *map, const void *data, unsigned int len, uint8_t sha1[SHA1_HASH_SIZE]) {
100
 
    SHA1Context ctx;
101
111
    if(!fmap_need_ptr_once(map, data, len)) {
102
112
        cli_dbgmsg("map_sha1: failed to read hash data\n");
103
113
        return 1;
104
114
    }
105
 
    SHA1Init(&ctx);
106
 
    while(len) {
107
 
        unsigned int todo = MIN(len, map->pgsz);
108
 
        SHA1Update(&ctx, data, todo);
109
 
        data = (uint8_t *)data + todo;
110
 
        len -= todo;
111
 
    }
112
 
    SHA1Final(&ctx, sha1);
113
 
    return 0;
 
115
    return (cl_sha1(data, len, sha1, NULL) == NULL);
114
116
}
115
117
 
116
118
static int map_md5(fmap_t *map, const void *data, unsigned int len, uint8_t *md5) {
117
 
    cli_md5_ctx ctx;
118
119
    if(!fmap_need_ptr_once(map, data, len)) {
119
120
        cli_dbgmsg("map_md5: failed to read hash data\n");
120
121
        return 1;
121
122
    }
122
 
    cli_md5_init(&ctx);
123
 
    while(len) {
124
 
        unsigned int todo = MIN(len, map->pgsz);
125
 
        cli_md5_update(&ctx, data, len);
126
 
        data = (uint8_t *)data + todo;
127
 
        len -= todo;
128
 
    }
129
 
    cli_md5_final(md5, &ctx);
130
 
    return 0;
 
123
    return (cl_hash_data("md5", data, len, md5, NULL) == NULL);
131
124
}
132
125
 
133
126
 
264
257
        *hashtype = CLI_SHA1RSA; /* sha1withRSAEncryption 1.2.840.113549.1.1.5 */
265
258
    else if(obj.size == lenof(OID_md5WithRSAEncryption) && !memcmp(obj.content, OID_md5WithRSAEncryption, lenof(OID_md5WithRSAEncryption)))
266
259
        *hashtype = CLI_MD5RSA; /* md5withRSAEncryption 1.2.840.113549.1.1.4 */
 
260
    else if(obj.size == lenof(OID_md2WithRSAEncryption) && !memcmp(obj.content, OID_md2WithRSAEncryption, lenof(OID_md2WithRSAEncryption))) {
 
261
        cli_dbgmsg("asn1_expect_rsa: MD2 with RSA (not yet supported)\n");
 
262
        return 1;
 
263
    }
 
264
    else if(obj.size == lenof(OID_sha256WithRSAEncryption) && !memcmp(obj.content, OID_sha256WithRSAEncryption, lenof(OID_sha256WithRSAEncryption))) {
 
265
        cli_dbgmsg("asn1_expect_rsa: SHA256 with RSA (not yet supported)\n");
 
266
        return 1;
 
267
    }
 
268
    else if(obj.size == lenof(OID_sha512WithRSAEncryption) && !memcmp(obj.content, OID_sha512WithRSAEncryption, lenof(OID_sha512WithRSAEncryption))) {
 
269
        cli_dbgmsg("asn1_expect_rsa: SHA512 with RSA (not yet supported)\n");
 
270
        return 1;
 
271
    }
267
272
    else {
268
 
        cli_dbgmsg("asn1_expect_rsa: OID mismatch\n");
 
273
        cli_dbgmsg("asn1_expect_rsa: OID mismatch (size %u)\n", obj.size);
269
274
        return 1;
270
275
    }
271
276
    if((ret = asn1_expect_obj(map, &obj.next, &avail, 0x05, 0, NULL))) /* NULL */
746
751
    const uint8_t *message, *attrs;
747
752
    unsigned int dsize, message_size, attrs_size;
748
753
    cli_crt_hashtype hashtype;
749
 
    SHA1Context ctx;
750
754
    cli_crt *x509;
 
755
    void *ctx;
751
756
    int result;
752
757
    int isBlacklisted = 0;
753
758
 
1037
1042
            break;
1038
1043
        }
1039
1044
 
1040
 
        SHA1Init(&ctx);
1041
 
        SHA1Update(&ctx, "\x31", 1);
1042
 
        SHA1Update(&ctx, attrs + 1, attrs_size - 1);
1043
 
        SHA1Final(&ctx, sha1);
 
1045
    ctx = cl_hash_init("sha1");
 
1046
    if (!(ctx))
 
1047
        break;
 
1048
 
 
1049
        cl_update_hash(ctx, "\x31", 1);
 
1050
        cl_update_hash(ctx, attrs + 1, attrs_size - 1);
 
1051
        cl_finish_hash(ctx, sha1);
1044
1052
 
1045
1053
        if(!fmap_need_ptr_once(map, asn1.content, asn1.size)) {
1046
1054
            cli_dbgmsg("asn1_parse_mscat: failed to read encryptedDigest\n");
1278
1286
        }
1279
1287
 
1280
1288
        if(hashtype == CLI_SHA1RSA) {
1281
 
            SHA1Init(&ctx);
1282
 
            SHA1Update(&ctx, "\x31", 1);
1283
 
            SHA1Update(&ctx, attrs + 1, attrs_size - 1);
1284
 
            SHA1Final(&ctx, sha1);
 
1289
        ctx = cl_hash_init("sha1");
 
1290
        if (!(ctx))
 
1291
            break;
 
1292
 
 
1293
        cl_update_hash(ctx, "\x31", 1);
 
1294
        cl_update_hash(ctx, attrs + 1, attrs_size - 1);
 
1295
        cl_finish_hash(ctx, sha1);
1285
1296
        } else {
1286
 
            cli_md5_ctx ctx;
1287
 
            cli_md5_init(&ctx);
1288
 
            cli_md5_update(&ctx, "\x31", 1);
1289
 
            cli_md5_update(&ctx, attrs + 1, attrs_size - 1);
1290
 
            cli_md5_final(sha1, &ctx);
 
1297
        ctx = cl_hash_init("md5");
 
1298
        if (!(ctx))
 
1299
            break;
 
1300
 
 
1301
        cl_update_hash(ctx, "\x31", 1);
 
1302
        cl_update_hash(ctx, attrs + 1, attrs_size - 1);
 
1303
        cl_finish_hash(ctx, sha1);
1291
1304
        }
1292
1305
 
1293
1306
        if(!fmap_need_ptr_once(map, asn1.content, asn1.size)) {
1299
1312
            break;
1300
1313
        }
1301
1314
 
1302
 
        cli_dbgmsg("asn1_parse_mscat: catalog succesfully parsed\n");
 
1315
        cli_dbgmsg("asn1_parse_mscat: catalog successfully parsed\n");
1303
1316
    if (isBlacklisted) {
1304
1317
        return 1;
1305
1318
    }