Coverage Report

Created: 2018-08-29 13:53

/src/openssl/ssl/s3_cbc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2012-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 "internal/constant_time_locl.h"
11
#include "ssl_locl.h"
12
#include "internal/cryptlib.h"
13
14
#include <openssl/md5.h>
15
#include <openssl/sha.h>
16
17
/*
18
 * MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's
19
 * length field. (SHA-384/512 have 128-bit length.)
20
 */
21
#define MAX_HASH_BIT_COUNT_BYTES 16
22
23
/*
24
 * MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
25
 * Currently SHA-384/512 has a 128-byte block size and that's the largest
26
 * supported by TLS.)
27
 */
28
#define MAX_HASH_BLOCK_SIZE 128
29
30
/*
31
 * u32toLE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
32
 * little-endian order. The value of p is advanced by four.
33
 */
34
#define u32toLE(n, p) \
35
0
        (*((p)++)=(unsigned char)(n), \
36
0
         *((p)++)=(unsigned char)(n>>8), \
37
0
         *((p)++)=(unsigned char)(n>>16), \
38
0
         *((p)++)=(unsigned char)(n>>24))
39
40
/*
41
 * These functions serialize the state of a hash and thus perform the
42
 * standard "final" operation without adding the padding and length that such
43
 * a function typically does.
44
 */
45
static void tls1_md5_final_raw(void *ctx, unsigned char *md_out)
46
0
{
47
0
    MD5_CTX *md5 = ctx;
48
0
    u32toLE(md5->A, md_out);
49
0
    u32toLE(md5->B, md_out);
50
0
    u32toLE(md5->C, md_out);
51
0
    u32toLE(md5->D, md_out);
52
0
}
53
54
static void tls1_sha1_final_raw(void *ctx, unsigned char *md_out)
55
0
{
56
0
    SHA_CTX *sha1 = ctx;
57
0
    l2n(sha1->h0, md_out);
58
0
    l2n(sha1->h1, md_out);
59
0
    l2n(sha1->h2, md_out);
60
0
    l2n(sha1->h3, md_out);
61
0
    l2n(sha1->h4, md_out);
62
0
}
63
64
static void tls1_sha256_final_raw(void *ctx, unsigned char *md_out)
65
0
{
66
0
    SHA256_CTX *sha256 = ctx;
67
0
    unsigned i;
68
0
69
0
    for (i = 0; i < 8; i++) {
70
0
        l2n(sha256->h[i], md_out);
71
0
    }
72
0
}
73
74
static void tls1_sha512_final_raw(void *ctx, unsigned char *md_out)
75
0
{
76
0
    SHA512_CTX *sha512 = ctx;
77
0
    unsigned i;
78
0
79
0
    for (i = 0; i < 8; i++) {
80
0
        l2n8(sha512->h[i], md_out);
81
0
    }
82
0
}
83
84
#undef  LARGEST_DIGEST_CTX
85
#define LARGEST_DIGEST_CTX SHA512_CTX
86
87
/*
88
 * ssl3_cbc_record_digest_supported returns 1 iff |ctx| uses a hash function
89
 * which ssl3_cbc_digest_record supports.
90
 */
91
char ssl3_cbc_record_digest_supported(const EVP_MD_CTX *ctx)
92
0
{
93
0
    switch (EVP_MD_CTX_type(ctx)) {
94
0
    case NID_md5:
95
0
    case NID_sha1:
96
0
    case NID_sha224:
97
0
    case NID_sha256:
98
0
    case NID_sha384:
99
0
    case NID_sha512:
100
0
        return 1;
101
0
    default:
102
0
        return 0;
103
0
    }
104
0
}
105
106
/*-
107
 * ssl3_cbc_digest_record computes the MAC of a decrypted, padded SSLv3/TLS
108
 * record.
109
 *
110
 *   ctx: the EVP_MD_CTX from which we take the hash function.
111
 *     ssl3_cbc_record_digest_supported must return true for this EVP_MD_CTX.
112
 *   md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
113
 *   md_out_size: if non-NULL, the number of output bytes is written here.
114
 *   header: the 13-byte, TLS record header.
115
 *   data: the record data itself, less any preceding explicit IV.
116
 *   data_plus_mac_size: the secret, reported length of the data and MAC
117
 *     once the padding has been removed.
118
 *   data_plus_mac_plus_padding_size: the public length of the whole
119
 *     record, including padding.
120
 *   is_sslv3: non-zero if we are to use SSLv3. Otherwise, TLS.
121
 *
122
 * On entry: by virtue of having been through one of the remove_padding
123
 * functions, above, we know that data_plus_mac_size is large enough to contain
124
 * a padding byte and MAC. (If the padding was invalid, it might contain the
125
 * padding too. )
126
 * Returns 1 on success or 0 on error
127
 */
128
int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,
129
                           unsigned char *md_out,
130
                           size_t *md_out_size,
131
                           const unsigned char header[13],
132
                           const unsigned char *data,
133
                           size_t data_plus_mac_size,
134
                           size_t data_plus_mac_plus_padding_size,
135
                           const unsigned char *mac_secret,
136
                           size_t mac_secret_length, char is_sslv3)
137
0
{
138
0
    union {
139
0
        double align;
140
0
        unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
141
0
    } md_state;
142
0
    void (*md_final_raw) (void *ctx, unsigned char *md_out);
143
0
    void (*md_transform) (void *ctx, const unsigned char *block);
144
0
    size_t md_size, md_block_size = 64;
145
0
    size_t sslv3_pad_length = 40, header_length, variance_blocks,
146
0
        len, max_mac_bytes, num_blocks,
147
0
        num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
148
0
    size_t bits;          /* at most 18 bits */
149
0
    unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
150
0
    /* hmac_pad is the masked HMAC key. */
151
0
    unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
152
0
    unsigned char first_block[MAX_HASH_BLOCK_SIZE];
153
0
    unsigned char mac_out[EVP_MAX_MD_SIZE];
154
0
    size_t i, j;
155
0
    unsigned md_out_size_u;
156
0
    EVP_MD_CTX *md_ctx = NULL;
157
0
    /*
158
0
     * mdLengthSize is the number of bytes in the length field that
159
0
     * terminates * the hash.
160
0
     */
161
0
    size_t md_length_size = 8;
162
0
    char length_is_big_endian = 1;
163
0
    int ret;
164
0
165
0
    /*
166
0
     * This is a, hopefully redundant, check that allows us to forget about
167
0
     * many possible overflows later in this function.
168
0
     */
169
0
    if (!ossl_assert(data_plus_mac_plus_padding_size < 1024 * 1024))
170
0
        return 0;
171
0
172
0
    switch (EVP_MD_CTX_type(ctx)) {
173
0
    case NID_md5:
174
0
        if (MD5_Init((MD5_CTX *)md_state.c) <= 0)
175
0
            return 0;
176
0
        md_final_raw = tls1_md5_final_raw;
177
0
        md_transform =
178
0
            (void (*)(void *ctx, const unsigned char *block))MD5_Transform;
179
0
        md_size = 16;
180
0
        sslv3_pad_length = 48;
181
0
        length_is_big_endian = 0;
182
0
        break;
183
0
    case NID_sha1:
184
0
        if (SHA1_Init((SHA_CTX *)md_state.c) <= 0)
185
0
            return 0;
186
0
        md_final_raw = tls1_sha1_final_raw;
187
0
        md_transform =
188
0
            (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
189
0
        md_size = 20;
190
0
        break;
191
0
    case NID_sha224:
192
0
        if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0)
193
0
            return 0;
194
0
        md_final_raw = tls1_sha256_final_raw;
195
0
        md_transform =
196
0
            (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
197
0
        md_size = 224 / 8;
198
0
        break;
199
0
    case NID_sha256:
200
0
        if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0)
201
0
            return 0;
202
0
        md_final_raw = tls1_sha256_final_raw;
203
0
        md_transform =
204
0
            (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
205
0
        md_size = 32;
206
0
        break;
207
0
    case NID_sha384:
208
0
        if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0)
209
0
            return 0;
210
0
        md_final_raw = tls1_sha512_final_raw;
211
0
        md_transform =
212
0
            (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
213
0
        md_size = 384 / 8;
214
0
        md_block_size = 128;
215
0
        md_length_size = 16;
216
0
        break;
217
0
    case NID_sha512:
218
0
        if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0)
219
0
            return 0;
220
0
        md_final_raw = tls1_sha512_final_raw;
221
0
        md_transform =
222
0
            (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
223
0
        md_size = 64;
224
0
        md_block_size = 128;
225
0
        md_length_size = 16;
226
0
        break;
227
0
    default:
228
0
        /*
229
0
         * ssl3_cbc_record_digest_supported should have been called first to
230
0
         * check that the hash function is supported.
231
0
         */
232
0
        if (md_out_size != NULL)
233
0
            *md_out_size = 0;
234
0
        return ossl_assert(0);
235
0
    }
236
0
237
0
    if (!ossl_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES)
238
0
            || !ossl_assert(md_block_size <= MAX_HASH_BLOCK_SIZE)
239
0
            || !ossl_assert(md_size <= EVP_MAX_MD_SIZE))
240
0
        return 0;
241
0
242
0
    header_length = 13;
243
0
    if (is_sslv3) {
244
0
        header_length = mac_secret_length + sslv3_pad_length + 8 /* sequence
245
0
                                                                  * number */  +
246
0
            1 /* record type */  +
247
0
            2 /* record length */ ;
248
0
    }
249
0
250
0
    /*
251
0
     * variance_blocks is the number of blocks of the hash that we have to
252
0
     * calculate in constant time because they could be altered by the
253
0
     * padding value. In SSLv3, the padding must be minimal so the end of
254
0
     * the plaintext varies by, at most, 15+20 = 35 bytes. (We conservatively
255
0
     * assume that the MAC size varies from 0..20 bytes.) In case the 9 bytes
256
0
     * of hash termination (0x80 + 64-bit length) don't fit in the final
257
0
     * block, we say that the final two blocks can vary based on the padding.
258
0
     * TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
259
0
     * required to be minimal. Therefore we say that the final six blocks can
260
0
     * vary based on the padding. Later in the function, if the message is
261
0
     * short and there obviously cannot be this many blocks then
262
0
     * variance_blocks can be reduced.
263
0
     */
264
0
    variance_blocks = is_sslv3 ? 2 : 6;
265
0
    /*
266
0
     * From now on we're dealing with the MAC, which conceptually has 13
267
0
     * bytes of `header' before the start of the data (TLS) or 71/75 bytes
268
0
     * (SSLv3)
269
0
     */
270
0
    len = data_plus_mac_plus_padding_size + header_length;
271
0
    /*
272
0
     * max_mac_bytes contains the maximum bytes of bytes in the MAC,
273
0
     * including * |header|, assuming that there's no padding.
274
0
     */
275
0
    max_mac_bytes = len - md_size - 1;
276
0
    /* num_blocks is the maximum number of hash blocks. */
277
0
    num_blocks =
278
0
        (max_mac_bytes + 1 + md_length_size + md_block_size -
279
0
         1) / md_block_size;
280
0
    /*
281
0
     * In order to calculate the MAC in constant time we have to handle the
282
0
     * final blocks specially because the padding value could cause the end
283
0
     * to appear somewhere in the final |variance_blocks| blocks and we can't
284
0
     * leak where. However, |num_starting_blocks| worth of data can be hashed
285
0
     * right away because no padding value can affect whether they are
286
0
     * plaintext.
287
0
     */
288
0
    num_starting_blocks = 0;
289
0
    /*
290
0
     * k is the starting byte offset into the conceptual header||data where
291
0
     * we start processing.
292
0
     */
293
0
    k = 0;
294
0
    /*
295
0
     * mac_end_offset is the index just past the end of the data to be MACed.
296
0
     */
297
0
    mac_end_offset = data_plus_mac_size + header_length - md_size;
298
0
    /*
299
0
     * c is the index of the 0x80 byte in the final hash block that contains
300
0
     * application data.
301
0
     */
302
0
    c = mac_end_offset % md_block_size;
303
0
    /*
304
0
     * index_a is the hash block number that contains the 0x80 terminating
305
0
     * value.
306
0
     */
307
0
    index_a = mac_end_offset / md_block_size;
308
0
    /*
309
0
     * index_b is the hash block number that contains the 64-bit hash length,
310
0
     * in bits.
311
0
     */
312
0
    index_b = (mac_end_offset + md_length_size) / md_block_size;
313
0
    /*
314
0
     * bits is the hash-length in bits. It includes the additional hash block
315
0
     * for the masked HMAC key, or whole of |header| in the case of SSLv3.
316
0
     */
317
0
318
0
    /*
319
0
     * For SSLv3, if we're going to have any starting blocks then we need at
320
0
     * least two because the header is larger than a single block.
321
0
     */
322
0
    if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
323
0
        num_starting_blocks = num_blocks - variance_blocks;
324
0
        k = md_block_size * num_starting_blocks;
325
0
    }
326
0
327
0
    bits = 8 * mac_end_offset;
328
0
    if (!is_sslv3) {
329
0
        /*
330
0
         * Compute the initial HMAC block. For SSLv3, the padding and secret
331
0
         * bytes are included in |header| because they take more than a
332
0
         * single block.
333
0
         */
334
0
        bits += 8 * md_block_size;
335
0
        memset(hmac_pad, 0, md_block_size);
336
0
        if (!ossl_assert(mac_secret_length <= sizeof(hmac_pad)))
337
0
            return 0;
338
0
        memcpy(hmac_pad, mac_secret, mac_secret_length);
339
0
        for (i = 0; i < md_block_size; i++)
340
0
            hmac_pad[i] ^= 0x36;
341
0
342
0
        md_transform(md_state.c, hmac_pad);
343
0
    }
344
0
345
0
    if (length_is_big_endian) {
346
0
        memset(length_bytes, 0, md_length_size - 4);
347
0
        length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
348
0
        length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
349
0
        length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
350
0
        length_bytes[md_length_size - 1] = (unsigned char)bits;
351
0
    } else {
352
0
        memset(length_bytes, 0, md_length_size);
353
0
        length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
354
0
        length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
355
0
        length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
356
0
        length_bytes[md_length_size - 8] = (unsigned char)bits;
357
0
    }
358
0
359
0
    if (k > 0) {
360
0
        if (is_sslv3) {
361
0
            size_t overhang;
362
0
363
0
            /*
364
0
             * The SSLv3 header is larger than a single block. overhang is
365
0
             * the number of bytes beyond a single block that the header
366
0
             * consumes: either 7 bytes (SHA1) or 11 bytes (MD5). There are no
367
0
             * ciphersuites in SSLv3 that are not SHA1 or MD5 based and
368
0
             * therefore we can be confident that the header_length will be
369
0
             * greater than |md_block_size|. However we add a sanity check just
370
0
             * in case
371
0
             */
372
0
            if (header_length <= md_block_size) {
373
0
                /* Should never happen */
374
0
                return 0;
375
0
            }
376
0
            overhang = header_length - md_block_size;
377
0
            md_transform(md_state.c, header);
378
0
            memcpy(first_block, header + md_block_size, overhang);
379
0
            memcpy(first_block + overhang, data, md_block_size - overhang);
380
0
            md_transform(md_state.c, first_block);
381
0
            for (i = 1; i < k / md_block_size - 1; i++)
382
0
                md_transform(md_state.c, data + md_block_size * i - overhang);
383
0
        } else {
384
0
            /* k is a multiple of md_block_size. */
385
0
            memcpy(first_block, header, 13);
386
0
            memcpy(first_block + 13, data, md_block_size - 13);
387
0
            md_transform(md_state.c, first_block);
388
0
            for (i = 1; i < k / md_block_size; i++)
389
0
                md_transform(md_state.c, data + md_block_size * i - 13);
390
0
        }
391
0
    }
392
0
393
0
    memset(mac_out, 0, sizeof(mac_out));
394
0
395
0
    /*
396
0
     * We now process the final hash blocks. For each block, we construct it
397
0
     * in constant time. If the |i==index_a| then we'll include the 0x80
398
0
     * bytes and zero pad etc. For each block we selectively copy it, in
399
0
     * constant time, to |mac_out|.
400
0
     */
401
0
    for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
402
0
         i++) {
403
0
        unsigned char block[MAX_HASH_BLOCK_SIZE];
404
0
        unsigned char is_block_a = constant_time_eq_8_s(i, index_a);
405
0
        unsigned char is_block_b = constant_time_eq_8_s(i, index_b);
406
0
        for (j = 0; j < md_block_size; j++) {
407
0
            unsigned char b = 0, is_past_c, is_past_cp1;
408
0
            if (k < header_length)
409
0
                b = header[k];
410
0
            else if (k < data_plus_mac_plus_padding_size + header_length)
411
0
                b = data[k - header_length];
412
0
            k++;
413
0
414
0
            is_past_c = is_block_a & constant_time_ge_8_s(j, c);
415
0
            is_past_cp1 = is_block_a & constant_time_ge_8_s(j, c + 1);
416
0
            /*
417
0
             * If this is the block containing the end of the application
418
0
             * data, and we are at the offset for the 0x80 value, then
419
0
             * overwrite b with 0x80.
420
0
             */
421
0
            b = constant_time_select_8(is_past_c, 0x80, b);
422
0
            /*
423
0
             * If this block contains the end of the application data
424
0
             * and we're past the 0x80 value then just write zero.
425
0
             */
426
0
            b = b & ~is_past_cp1;
427
0
            /*
428
0
             * If this is index_b (the final block), but not index_a (the end
429
0
             * of the data), then the 64-bit length didn't fit into index_a
430
0
             * and we're having to add an extra block of zeros.
431
0
             */
432
0
            b &= ~is_block_b | is_block_a;
433
0
434
0
            /*
435
0
             * The final bytes of one of the blocks contains the length.
436
0
             */
437
0
            if (j >= md_block_size - md_length_size) {
438
0
                /* If this is index_b, write a length byte. */
439
0
                b = constant_time_select_8(is_block_b,
440
0
                                           length_bytes[j -
441
0
                                                        (md_block_size -
442
0
                                                         md_length_size)], b);
443
0
            }
444
0
            block[j] = b;
445
0
        }
446
0
447
0
        md_transform(md_state.c, block);
448
0
        md_final_raw(md_state.c, block);
449
0
        /* If this is index_b, copy the hash value to |mac_out|. */
450
0
        for (j = 0; j < md_size; j++)
451
0
            mac_out[j] |= block[j] & is_block_b;
452
0
    }
453
0
454
0
    md_ctx = EVP_MD_CTX_new();
455
0
    if (md_ctx == NULL)
456
0
        goto err;
457
0
    if (EVP_DigestInit_ex(md_ctx, EVP_MD_CTX_md(ctx), NULL /* engine */ ) <= 0)
458
0
        goto err;
459
0
    if (is_sslv3) {
460
0
        /* We repurpose |hmac_pad| to contain the SSLv3 pad2 block. */
461
0
        memset(hmac_pad, 0x5c, sslv3_pad_length);
462
0
463
0
        if (EVP_DigestUpdate(md_ctx, mac_secret, mac_secret_length) <= 0
464
0
            || EVP_DigestUpdate(md_ctx, hmac_pad, sslv3_pad_length) <= 0
465
0
            || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
466
0
            goto err;
467
0
    } else {
468
0
        /* Complete the HMAC in the standard manner. */
469
0
        for (i = 0; i < md_block_size; i++)
470
0
            hmac_pad[i] ^= 0x6a;
471
0
472
0
        if (EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size) <= 0
473
0
            || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
474
0
            goto err;
475
0
    }
476
0
    /* TODO(size_t): Convert me */
477
0
    ret = EVP_DigestFinal(md_ctx, md_out, &md_out_size_u);
478
0
    if (ret && md_out_size)
479
0
        *md_out_size = md_out_size_u;
480
0
    EVP_MD_CTX_free(md_ctx);
481
0
482
0
    return 1;
483
0
 err:
484
0
    EVP_MD_CTX_free(md_ctx);
485
0
    return 0;
486
0
}