Coverage Report

Created: 2018-08-29 13:53

/src/openssl/ssl/packet.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2015-2018 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/cryptlib.h"
11
#include "packet_locl.h"
12
#include <openssl/sslerr.h>
13
14
0
#define DEFAULT_BUF_SIZE    256
15
16
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
17
0
{
18
0
    if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
19
0
        return 0;
20
0
21
0
    pkt->written += len;
22
0
    pkt->curr += len;
23
0
    return 1;
24
0
}
25
26
int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
27
                                 unsigned char **allocbytes, size_t lenbytes)
28
0
{
29
0
    if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
30
0
            || !WPACKET_allocate_bytes(pkt, len, allocbytes)
31
0
            || !WPACKET_close(pkt))
32
0
        return 0;
33
0
34
0
    return 1;
35
0
}
36
37
0
#define GETBUF(p)   (((p)->staticbuf != NULL) \
38
0
                     ? (p)->staticbuf : (unsigned char *)(p)->buf->data)
39
40
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
41
0
{
42
0
    /* Internal API, so should not fail */
43
0
    if (!ossl_assert(pkt->subs != NULL && len != 0))
44
0
        return 0;
45
0
46
0
    if (pkt->maxsize - pkt->written < len)
47
0
        return 0;
48
0
49
0
    if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
50
0
        size_t newlen;
51
0
        size_t reflen;
52
0
53
0
        reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
54
0
55
0
        if (reflen > SIZE_MAX / 2) {
56
0
            newlen = SIZE_MAX;
57
0
        } else {
58
0
            newlen = reflen * 2;
59
0
            if (newlen < DEFAULT_BUF_SIZE)
60
0
                newlen = DEFAULT_BUF_SIZE;
61
0
        }
62
0
        if (BUF_MEM_grow(pkt->buf, newlen) == 0)
63
0
            return 0;
64
0
    }
65
0
    if (allocbytes != NULL)
66
0
        *allocbytes = WPACKET_get_curr(pkt);
67
0
68
0
    return 1;
69
0
}
70
71
int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
72
                                unsigned char **allocbytes, size_t lenbytes)
73
0
{
74
0
    if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
75
0
        return 0;
76
0
77
0
    *allocbytes += lenbytes;
78
0
79
0
    return 1;
80
0
}
81
82
static size_t maxmaxsize(size_t lenbytes)
83
0
{
84
0
    if (lenbytes >= sizeof(size_t) || lenbytes == 0)
85
0
        return SIZE_MAX;
86
0
87
0
    return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
88
0
}
89
90
static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
91
0
{
92
0
    unsigned char *lenchars;
93
0
94
0
    pkt->curr = 0;
95
0
    pkt->written = 0;
96
0
97
0
    if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL) {
98
0
        SSLerr(SSL_F_WPACKET_INTERN_INIT_LEN, ERR_R_MALLOC_FAILURE);
99
0
        return 0;
100
0
    }
101
0
102
0
    if (lenbytes == 0)
103
0
        return 1;
104
0
105
0
    pkt->subs->pwritten = lenbytes;
106
0
    pkt->subs->lenbytes = lenbytes;
107
0
108
0
    if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
109
0
        OPENSSL_free(pkt->subs);
110
0
        pkt->subs = NULL;
111
0
        return 0;
112
0
    }
113
0
    pkt->subs->packet_len = lenchars - GETBUF(pkt);
114
0
115
0
    return 1;
116
0
}
117
118
int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
119
                            size_t lenbytes)
120
0
{
121
0
    size_t max = maxmaxsize(lenbytes);
122
0
123
0
    /* Internal API, so should not fail */
124
0
    if (!ossl_assert(buf != NULL && len > 0))
125
0
        return 0;
126
0
127
0
    pkt->staticbuf = buf;
128
0
    pkt->buf = NULL;
129
0
    pkt->maxsize = (max < len) ? max : len;
130
0
131
0
    return wpacket_intern_init_len(pkt, lenbytes);
132
0
}
133
134
int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
135
0
{
136
0
    /* Internal API, so should not fail */
137
0
    if (!ossl_assert(buf != NULL))
138
0
        return 0;
139
0
140
0
    pkt->staticbuf = NULL;
141
0
    pkt->buf = buf;
142
0
    pkt->maxsize = maxmaxsize(lenbytes);
143
0
144
0
    return wpacket_intern_init_len(pkt, lenbytes);
145
0
}
146
147
int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
148
0
{
149
0
    return WPACKET_init_len(pkt, buf, 0);
150
0
}
151
152
int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
153
0
{
154
0
    /* Internal API, so should not fail */
155
0
    if (!ossl_assert(pkt->subs != NULL))
156
0
        return 0;
157
0
158
0
    pkt->subs->flags = flags;
159
0
160
0
    return 1;
161
0
}
162
163
/* Store the |value| of length |len| at location |data| */
164
static int put_value(unsigned char *data, size_t value, size_t len)
165
0
{
166
0
    for (data += len - 1; len > 0; len--) {
167
0
        *data = (unsigned char)(value & 0xff);
168
0
        data--;
169
0
        value >>= 8;
170
0
    }
171
0
172
0
    /* Check whether we could fit the value in the assigned number of bytes */
173
0
    if (value > 0)
174
0
        return 0;
175
0
176
0
    return 1;
177
0
}
178
179
180
/*
181
 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
182
 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
183
 * necessary. If |doclose| is 0 then it goes through the motions of closing
184
 * (i.e. it fills in all the lengths), but doesn't actually close anything.
185
 */
186
static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
187
0
{
188
0
    size_t packlen = pkt->written - sub->pwritten;
189
0
190
0
    if (packlen == 0
191
0
            && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
192
0
        return 0;
193
0
194
0
    if (packlen == 0
195
0
            && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
196
0
        /* We can't handle this case. Return an error */
197
0
        if (!doclose)
198
0
            return 0;
199
0
200
0
        /* Deallocate any bytes allocated for the length of the WPACKET */
201
0
        if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
202
0
            pkt->written -= sub->lenbytes;
203
0
            pkt->curr -= sub->lenbytes;
204
0
        }
205
0
206
0
        /* Don't write out the packet length */
207
0
        sub->packet_len = 0;
208
0
        sub->lenbytes = 0;
209
0
    }
210
0
211
0
    /* Write out the WPACKET length if needed */
212
0
    if (sub->lenbytes > 0
213
0
                && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,
214
0
                              sub->lenbytes))
215
0
            return 0;
216
0
217
0
    if (doclose) {
218
0
        pkt->subs = sub->parent;
219
0
        OPENSSL_free(sub);
220
0
    }
221
0
222
0
    return 1;
223
0
}
224
225
int WPACKET_fill_lengths(WPACKET *pkt)
226
0
{
227
0
    WPACKET_SUB *sub;
228
0
229
0
    if (!ossl_assert(pkt->subs != NULL))
230
0
        return 0;
231
0
232
0
    for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
233
0
        if (!wpacket_intern_close(pkt, sub, 0))
234
0
            return 0;
235
0
    }
236
0
237
0
    return 1;
238
0
}
239
240
int WPACKET_close(WPACKET *pkt)
241
0
{
242
0
    /*
243
0
     * Internal API, so should not fail - but we do negative testing of this
244
0
     * so no assert (otherwise the tests fail)
245
0
     */
246
0
    if (pkt->subs == NULL || pkt->subs->parent == NULL)
247
0
        return 0;
248
0
249
0
    return wpacket_intern_close(pkt, pkt->subs, 1);
250
0
}
251
252
int WPACKET_finish(WPACKET *pkt)
253
0
{
254
0
    int ret;
255
0
256
0
    /*
257
0
     * Internal API, so should not fail - but we do negative testing of this
258
0
     * so no assert (otherwise the tests fail)
259
0
     */
260
0
    if (pkt->subs == NULL || pkt->subs->parent != NULL)
261
0
        return 0;
262
0
263
0
    ret = wpacket_intern_close(pkt, pkt->subs, 1);
264
0
    if (ret) {
265
0
        OPENSSL_free(pkt->subs);
266
0
        pkt->subs = NULL;
267
0
    }
268
0
269
0
    return ret;
270
0
}
271
272
int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
273
0
{
274
0
    WPACKET_SUB *sub;
275
0
    unsigned char *lenchars;
276
0
277
0
    /* Internal API, so should not fail */
278
0
    if (!ossl_assert(pkt->subs != NULL))
279
0
        return 0;
280
0
281
0
    if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
282
0
        SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);
283
0
        return 0;
284
0
    }
285
0
286
0
    sub->parent = pkt->subs;
287
0
    pkt->subs = sub;
288
0
    sub->pwritten = pkt->written + lenbytes;
289
0
    sub->lenbytes = lenbytes;
290
0
291
0
    if (lenbytes == 0) {
292
0
        sub->packet_len = 0;
293
0
        return 1;
294
0
    }
295
0
296
0
    if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
297
0
        return 0;
298
0
    /* Convert to an offset in case the underlying BUF_MEM gets realloc'd */
299
0
    sub->packet_len = lenchars - GETBUF(pkt);
300
0
301
0
    return 1;
302
0
}
303
304
int WPACKET_start_sub_packet(WPACKET *pkt)
305
0
{
306
0
    return WPACKET_start_sub_packet_len__(pkt, 0);
307
0
}
308
309
int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
310
0
{
311
0
    unsigned char *data;
312
0
313
0
    /* Internal API, so should not fail */
314
0
    if (!ossl_assert(size <= sizeof(unsigned int))
315
0
            || !WPACKET_allocate_bytes(pkt, size, &data)
316
0
            || !put_value(data, val, size))
317
0
        return 0;
318
0
319
0
    return 1;
320
0
}
321
322
int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
323
0
{
324
0
    WPACKET_SUB *sub;
325
0
    size_t lenbytes;
326
0
327
0
    /* Internal API, so should not fail */
328
0
    if (!ossl_assert(pkt->subs != NULL))
329
0
        return 0;
330
0
331
0
    /* Find the WPACKET_SUB for the top level */
332
0
    for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
333
0
        continue;
334
0
335
0
    lenbytes = sub->lenbytes;
336
0
    if (lenbytes == 0)
337
0
        lenbytes = sizeof(pkt->maxsize);
338
0
339
0
    if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
340
0
        return 0;
341
0
342
0
    pkt->maxsize = maxsize;
343
0
344
0
    return 1;
345
0
}
346
347
int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
348
0
{
349
0
    unsigned char *dest;
350
0
351
0
    if (len == 0)
352
0
        return 1;
353
0
354
0
    if (!WPACKET_allocate_bytes(pkt, len, &dest))
355
0
        return 0;
356
0
357
0
    memset(dest, ch, len);
358
0
359
0
    return 1;
360
0
}
361
362
int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
363
0
{
364
0
    unsigned char *dest;
365
0
366
0
    if (len == 0)
367
0
        return 1;
368
0
369
0
    if (!WPACKET_allocate_bytes(pkt, len, &dest))
370
0
        return 0;
371
0
372
0
    memcpy(dest, src, len);
373
0
374
0
    return 1;
375
0
}
376
377
int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
378
                         size_t lenbytes)
379
0
{
380
0
    if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
381
0
            || !WPACKET_memcpy(pkt, src, len)
382
0
            || !WPACKET_close(pkt))
383
0
        return 0;
384
0
385
0
    return 1;
386
0
}
387
388
int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
389
0
{
390
0
    /* Internal API, so should not fail */
391
0
    if (!ossl_assert(written != NULL))
392
0
        return 0;
393
0
394
0
    *written = pkt->written;
395
0
396
0
    return 1;
397
0
}
398
399
int WPACKET_get_length(WPACKET *pkt, size_t *len)
400
0
{
401
0
    /* Internal API, so should not fail */
402
0
    if (!ossl_assert(pkt->subs != NULL && len != NULL))
403
0
        return 0;
404
0
405
0
    *len = pkt->written - pkt->subs->pwritten;
406
0
407
0
    return 1;
408
0
}
409
410
unsigned char *WPACKET_get_curr(WPACKET *pkt)
411
0
{
412
0
    return GETBUF(pkt) + pkt->curr;
413
0
}
414
415
void WPACKET_cleanup(WPACKET *pkt)
416
0
{
417
0
    WPACKET_SUB *sub, *parent;
418
0
419
0
    for (sub = pkt->subs; sub != NULL; sub = parent) {
420
0
        parent = sub->parent;
421
0
        OPENSSL_free(sub);
422
0
    }
423
0
    pkt->subs = NULL;
424
0
}