]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/packet.c
Add "endfirst" writing to WPACKET
[thirdparty/openssl.git] / crypto / packet.c
CommitLineData
b7273855 1/*
28428130 2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
b7273855 3 *
2c18d164 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b7273855
MC
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
67dc995e 10#include "internal/cryptlib.h"
0d345f0e 11#include "internal/packet.h"
cdb10bae 12#include <openssl/sslerr.h>
b7273855 13
871bc59b
MC
14#define DEFAULT_BUF_SIZE 256
15
0217dd19 16int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
1ff84340
MC
17{
18 if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
19 return 0;
ff819477 20
1ff84340
MC
21 pkt->written += len;
22 pkt->curr += len;
1ff84340
MC
23 return 1;
24}
25
26int WPACKET_sub_allocate_bytes__(WPACKET *pkt, size_t len,
27 unsigned char **allocbytes, size_t lenbytes)
28{
29 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
30 || !WPACKET_allocate_bytes(pkt, len, allocbytes)
31 || !WPACKET_close(pkt))
32 return 0;
33
34 return 1;
35}
36
9b36b7d9 37#define GETBUF(p) (((p)->staticbuf != NULL) \
15cb0f09
MC
38 ? (p)->staticbuf \
39 : ((p)->buf != NULL \
40 ? (unsigned char *)(p)->buf->data \
41 : NULL))
9b36b7d9 42
1ff84340 43int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
b7273855 44{
c39609aa 45 /* Internal API, so should not fail */
b77f3ed1 46 if (!ossl_assert(pkt->subs != NULL && len != 0))
0217dd19 47 return 0;
b7273855 48
6ae4f5e0 49 if (pkt->maxsize - pkt->written < len)
b7273855
MC
50 return 0;
51
15cb0f09 52 if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
b7273855 53 size_t newlen;
f789b04f 54 size_t reflen;
b7273855 55
f789b04f
MC
56 reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
57
58 if (reflen > SIZE_MAX / 2) {
b7273855 59 newlen = SIZE_MAX;
871bc59b 60 } else {
f789b04f
MC
61 newlen = reflen * 2;
62 if (newlen < DEFAULT_BUF_SIZE)
63 newlen = DEFAULT_BUF_SIZE;
871bc59b 64 }
0217dd19
MC
65 if (BUF_MEM_grow(pkt->buf, newlen) == 0)
66 return 0;
b7273855 67 }
d3ba3916 68 if (allocbytes != NULL) {
829754a6 69 *allocbytes = WPACKET_get_curr(pkt);
d3ba3916
MC
70 if (pkt->endfirst && *allocbytes != NULL)
71 *allocbytes -= len;
72 }
b7273855 73
0217dd19 74 return 1;
b7273855
MC
75}
76
1ff84340
MC
77int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
78 unsigned char **allocbytes, size_t lenbytes)
b2b3024e 79{
d3ba3916
MC
80 if (pkt->endfirst && lenbytes > 0)
81 return 0;
82
1ff84340 83 if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
b2b3024e
MC
84 return 0;
85
15cb0f09
MC
86 if (*allocbytes != NULL)
87 *allocbytes += lenbytes;
1ff84340 88
b2b3024e
MC
89 return 1;
90}
91
871bc59b
MC
92static size_t maxmaxsize(size_t lenbytes)
93{
94 if (lenbytes >= sizeof(size_t) || lenbytes == 0)
95 return SIZE_MAX;
df065a2b
MC
96
97 return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
871bc59b
MC
98}
99
9b36b7d9 100static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
b7273855 101{
de451856
MC
102 unsigned char *lenchars;
103
de451856 104 pkt->curr = 0;
0217dd19 105 pkt->written = 0;
b7273855 106
cdb10bae
RS
107 if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL) {
108 SSLerr(SSL_F_WPACKET_INTERN_INIT_LEN, ERR_R_MALLOC_FAILURE);
0217dd19 109 return 0;
cdb10bae 110 }
b7273855 111
0217dd19 112 if (lenbytes == 0)
b7273855 113 return 1;
b7273855 114
0217dd19
MC
115 pkt->subs->pwritten = lenbytes;
116 pkt->subs->lenbytes = lenbytes;
117
de451856 118 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
0217dd19
MC
119 OPENSSL_free(pkt->subs);
120 pkt->subs = NULL;
b7273855
MC
121 return 0;
122 }
15cb0f09 123 pkt->subs->packet_len = 0;
b7273855
MC
124
125 return 1;
126}
127
9b36b7d9
MC
128int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
129 size_t lenbytes)
130{
131 size_t max = maxmaxsize(lenbytes);
132
133 /* Internal API, so should not fail */
b77f3ed1 134 if (!ossl_assert(buf != NULL && len > 0))
9b36b7d9
MC
135 return 0;
136
137 pkt->staticbuf = buf;
138 pkt->buf = NULL;
139 pkt->maxsize = (max < len) ? max : len;
d3ba3916 140 pkt->endfirst = 0;
9b36b7d9
MC
141
142 return wpacket_intern_init_len(pkt, lenbytes);
143}
144
d3ba3916
MC
145int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
146{
147 /* Internal API, so should not fail */
148 if (!ossl_assert(buf != NULL && len > 0))
149 return 0;
150
151 pkt->staticbuf = buf;
152 pkt->buf = NULL;
153 pkt->maxsize = len;
154 pkt->endfirst = 1;
155
156 return wpacket_intern_init_len(pkt, 0);
157}
158
9b36b7d9
MC
159int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
160{
161 /* Internal API, so should not fail */
b77f3ed1 162 if (!ossl_assert(buf != NULL))
9b36b7d9
MC
163 return 0;
164
165 pkt->staticbuf = NULL;
166 pkt->buf = buf;
167 pkt->maxsize = maxmaxsize(lenbytes);
d3ba3916 168 pkt->endfirst = 0;
9b36b7d9
MC
169
170 return wpacket_intern_init_len(pkt, lenbytes);
171}
172
ae2f7b37 173int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
b7273855 174{
ae2f7b37 175 return WPACKET_init_len(pkt, buf, 0);
b7273855
MC
176}
177
15cb0f09
MC
178int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
179{
180 pkt->staticbuf = NULL;
181 pkt->buf = NULL;
182 pkt->maxsize = maxmaxsize(lenbytes);
d3ba3916
MC
183 pkt->endfirst = 0;
184
185 return wpacket_intern_init_len(pkt, 0);
186}
187
188int WPACKET_init_null_der(WPACKET *pkt)
189{
190 pkt->staticbuf = NULL;
191 pkt->buf = NULL;
192 pkt->maxsize = SIZE_MAX;
193 pkt->endfirst = 1;
15cb0f09
MC
194
195 return wpacket_intern_init_len(pkt, 0);
196}
197
ae2f7b37 198int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
b7273855 199{
c39609aa 200 /* Internal API, so should not fail */
b77f3ed1 201 if (!ossl_assert(pkt->subs != NULL))
0217dd19
MC
202 return 0;
203
204 pkt->subs->flags = flags;
b7273855
MC
205
206 return 1;
207}
208
c0f9e23c
MC
209/* Store the |value| of length |len| at location |data| */
210static int put_value(unsigned char *data, size_t value, size_t len)
82657355 211{
15cb0f09
MC
212 if (data == NULL)
213 return 1;
214
c0f9e23c 215 for (data += len - 1; len > 0; len--) {
82657355
MC
216 *data = (unsigned char)(value & 0xff);
217 data--;
218 value >>= 8;
219 }
220
221 /* Check whether we could fit the value in the assigned number of bytes */
222 if (value > 0)
223 return 0;
224
225 return 1;
226}
227
0217dd19 228
b7273855 229/*
a2b7e655
MC
230 * Internal helper function used by WPACKET_close(), WPACKET_finish() and
231 * WPACKET_fill_lengths() to close a sub-packet and write out its length if
232 * necessary. If |doclose| is 0 then it goes through the motions of closing
233 * (i.e. it fills in all the lengths), but doesn't actually close anything.
b7273855 234 */
a2b7e655 235static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
b7273855 236{
de451856 237 size_t packlen = pkt->written - sub->pwritten;
b7273855 238
0217dd19 239 if (packlen == 0
c0f9e23c 240 && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
b7273855
MC
241 return 0;
242
243 if (packlen == 0
de451856 244 && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
a2b7e655
MC
245 /* We can't handle this case. Return an error */
246 if (!doclose)
247 return 0;
248
ae2f7b37 249 /* Deallocate any bytes allocated for the length of the WPACKET */
0217dd19
MC
250 if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
251 pkt->written -= sub->lenbytes;
252 pkt->curr -= sub->lenbytes;
b7273855
MC
253 }
254
255 /* Don't write out the packet length */
de451856
MC
256 sub->packet_len = 0;
257 sub->lenbytes = 0;
b7273855
MC
258 }
259
ae2f7b37 260 /* Write out the WPACKET length if needed */
15cb0f09
MC
261 if (sub->lenbytes > 0) {
262 unsigned char *buf = GETBUF(pkt);
263
264 if (buf != NULL
265 && !put_value(&buf[sub->packet_len], packlen,
9b36b7d9 266 sub->lenbytes))
b7273855 267 return 0;
d3ba3916
MC
268 } else if (pkt->endfirst && sub->parent != NULL) {
269 size_t tmplen = packlen;
270 size_t numlenbytes = 1;
271
272 while ((tmplen = tmplen >> 8) > 0)
273 numlenbytes++;
274 if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
275 return 0;
276 if (packlen > 0x7f) {
277 numlenbytes |= 0x80;
278 if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
279 return 0;
280 }
15cb0f09 281 }
b7273855 282
a2b7e655
MC
283 if (doclose) {
284 pkt->subs = sub->parent;
285 OPENSSL_free(sub);
286 }
287
288 return 1;
289}
290
291int WPACKET_fill_lengths(WPACKET *pkt)
292{
293 WPACKET_SUB *sub;
294
b77f3ed1 295 if (!ossl_assert(pkt->subs != NULL))
a2b7e655
MC
296 return 0;
297
fed60a78 298 for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
a2b7e655
MC
299 if (!wpacket_intern_close(pkt, sub, 0))
300 return 0;
fed60a78 301 }
b7273855
MC
302
303 return 1;
304}
305
0217dd19 306int WPACKET_close(WPACKET *pkt)
b7273855 307{
c39609aa
MC
308 /*
309 * Internal API, so should not fail - but we do negative testing of this
310 * so no assert (otherwise the tests fail)
311 */
0217dd19 312 if (pkt->subs == NULL || pkt->subs->parent == NULL)
b7273855
MC
313 return 0;
314
a2b7e655 315 return wpacket_intern_close(pkt, pkt->subs, 1);
b7273855
MC
316}
317
0217dd19 318int WPACKET_finish(WPACKET *pkt)
b7273855 319{
0217dd19
MC
320 int ret;
321
c39609aa
MC
322 /*
323 * Internal API, so should not fail - but we do negative testing of this
324 * so no assert (otherwise the tests fail)
325 */
0217dd19
MC
326 if (pkt->subs == NULL || pkt->subs->parent != NULL)
327 return 0;
328
a2b7e655 329 ret = wpacket_intern_close(pkt, pkt->subs, 1);
871bc59b
MC
330 if (ret) {
331 OPENSSL_free(pkt->subs);
332 pkt->subs = NULL;
333 }
de451856 334
0217dd19 335 return ret;
b7273855
MC
336}
337
869d0a37 338int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
b7273855 339{
0217dd19 340 WPACKET_SUB *sub;
de451856 341 unsigned char *lenchars;
b7273855 342
c39609aa 343 /* Internal API, so should not fail */
b77f3ed1 344 if (!ossl_assert(pkt->subs != NULL))
b7273855
MC
345 return 0;
346
d3ba3916
MC
347 /* We don't support lenbytes greater than 0 when doing endfirst writing */
348 if (lenbytes > 0 && pkt->endfirst)
349 return 0;
350
cdb10bae
RS
351 if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {
352 SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);
b7273855 353 return 0;
cdb10bae 354 }
b7273855 355
0217dd19
MC
356 sub->parent = pkt->subs;
357 pkt->subs = sub;
358 sub->pwritten = pkt->written + lenbytes;
359 sub->lenbytes = lenbytes;
360
361 if (lenbytes == 0) {
de451856 362 sub->packet_len = 0;
0217dd19
MC
363 return 1;
364 }
365
15cb0f09
MC
366 sub->packet_len = pkt->written;
367
de451856 368 if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
0217dd19 369 return 0;
b7273855
MC
370
371 return 1;
372}
373
0217dd19
MC
374int WPACKET_start_sub_packet(WPACKET *pkt)
375{
869d0a37 376 return WPACKET_start_sub_packet_len__(pkt, 0);
0217dd19
MC
377}
378
08029dfa 379int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)
b7273855
MC
380{
381 unsigned char *data;
382
c39609aa 383 /* Internal API, so should not fail */
b77f3ed1 384 if (!ossl_assert(size <= sizeof(unsigned int))
82657355
MC
385 || !WPACKET_allocate_bytes(pkt, size, &data)
386 || !put_value(data, val, size))
b7273855
MC
387 return 0;
388
389 return 1;
390}
391
ae2f7b37 392int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
b7273855 393{
871bc59b
MC
394 WPACKET_SUB *sub;
395 size_t lenbytes;
396
c39609aa 397 /* Internal API, so should not fail */
b77f3ed1 398 if (!ossl_assert(pkt->subs != NULL))
871bc59b
MC
399 return 0;
400
401 /* Find the WPACKET_SUB for the top level */
de451856
MC
402 for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
403 continue;
871bc59b
MC
404
405 lenbytes = sub->lenbytes;
406 if (lenbytes == 0)
407 lenbytes = sizeof(pkt->maxsize);
408
409 if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
410 return 0;
411
0217dd19 412 pkt->maxsize = maxsize;
b7273855
MC
413
414 return 1;
415}
416
c649d10d
TS
417int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
418{
419 unsigned char *dest;
420
421 if (len == 0)
422 return 1;
423
424 if (!WPACKET_allocate_bytes(pkt, len, &dest))
425 return 0;
426
15cb0f09
MC
427 if (dest != NULL)
428 memset(dest, ch, len);
c649d10d
TS
429
430 return 1;
431}
432
ae2f7b37 433int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
b7273855
MC
434{
435 unsigned char *dest;
436
437 if (len == 0)
438 return 1;
439
ae2f7b37 440 if (!WPACKET_allocate_bytes(pkt, len, &dest))
b7273855
MC
441 return 0;
442
15cb0f09
MC
443 if (dest != NULL)
444 memcpy(dest, src, len);
b7273855
MC
445
446 return 1;
447}
448
869d0a37
MC
449int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
450 size_t lenbytes)
fb790f16 451{
869d0a37 452 if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
fb790f16
MC
453 || !WPACKET_memcpy(pkt, src, len)
454 || !WPACKET_close(pkt))
455 return 0;
456
457 return 1;
458}
459
ae2f7b37 460int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
b7273855 461{
c39609aa 462 /* Internal API, so should not fail */
b77f3ed1 463 if (!ossl_assert(written != NULL))
b7273855
MC
464 return 0;
465
0217dd19 466 *written = pkt->written;
b7273855
MC
467
468 return 1;
469}
470
ae2f7b37 471int WPACKET_get_length(WPACKET *pkt, size_t *len)
b7273855 472{
c39609aa 473 /* Internal API, so should not fail */
b77f3ed1 474 if (!ossl_assert(pkt->subs != NULL && len != NULL))
b7273855
MC
475 return 0;
476
0217dd19 477 *len = pkt->written - pkt->subs->pwritten;
b7273855
MC
478
479 return 1;
480}
871bc59b 481
3171bad6
MC
482unsigned char *WPACKET_get_curr(WPACKET *pkt)
483{
15cb0f09
MC
484 unsigned char *buf = GETBUF(pkt);
485
486 if (buf == NULL)
487 return NULL;
488
d3ba3916
MC
489 if (pkt->endfirst)
490 return buf + pkt->maxsize - pkt->curr;
491
15cb0f09
MC
492 return buf + pkt->curr;
493}
494
495int WPACKET_is_null_buf(WPACKET *pkt)
496{
497 return pkt->buf == NULL && pkt->staticbuf == NULL;
3171bad6
MC
498}
499
871bc59b
MC
500void WPACKET_cleanup(WPACKET *pkt)
501{
502 WPACKET_SUB *sub, *parent;
503
504 for (sub = pkt->subs; sub != NULL; sub = parent) {
505 parent = sub->parent;
506 OPENSSL_free(sub);
507 }
508 pkt->subs = NULL;
509}