Fix migration guide mappings for i2o/o2i_ECPublicKey
[openssl.git] / crypto / packet.c
1 /*
2  * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "internal/packet.h"
12 #include <openssl/err.h>
13
14 #define DEFAULT_BUF_SIZE    256
15
16 int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
17 {
18     if (!WPACKET_reserve_bytes(pkt, len, allocbytes))
19         return 0;
20
21     pkt->written += len;
22     pkt->curr += len;
23     return 1;
24 }
25
26 int 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
37 #define GETBUF(p)   (((p)->staticbuf != NULL) \
38                      ? (p)->staticbuf \
39                      : ((p)->buf != NULL \
40                         ? (unsigned char *)(p)->buf->data \
41                         : NULL))
42
43 int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
44 {
45     /* Internal API, so should not fail */
46     if (!ossl_assert(pkt->subs != NULL && len != 0))
47         return 0;
48
49     if (pkt->maxsize - pkt->written < len)
50         return 0;
51
52     if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
53         size_t newlen;
54         size_t reflen;
55
56         reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
57
58         if (reflen > SIZE_MAX / 2) {
59             newlen = SIZE_MAX;
60         } else {
61             newlen = reflen * 2;
62             if (newlen < DEFAULT_BUF_SIZE)
63                 newlen = DEFAULT_BUF_SIZE;
64         }
65         if (BUF_MEM_grow(pkt->buf, newlen) == 0)
66             return 0;
67     }
68     if (allocbytes != NULL) {
69         *allocbytes = WPACKET_get_curr(pkt);
70         if (pkt->endfirst && *allocbytes != NULL)
71             *allocbytes -= len;
72     }
73
74     return 1;
75 }
76
77 int WPACKET_sub_reserve_bytes__(WPACKET *pkt, size_t len,
78                                 unsigned char **allocbytes, size_t lenbytes)
79 {
80     if (pkt->endfirst && lenbytes > 0)
81         return 0;
82
83     if (!WPACKET_reserve_bytes(pkt, lenbytes + len, allocbytes))
84         return 0;
85
86     if (*allocbytes != NULL)
87         *allocbytes += lenbytes;
88
89     return 1;
90 }
91
92 static size_t maxmaxsize(size_t lenbytes)
93 {
94     if (lenbytes >= sizeof(size_t) || lenbytes == 0)
95         return SIZE_MAX;
96
97     return ((size_t)1 << (lenbytes * 8)) - 1 + lenbytes;
98 }
99
100 static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)
101 {
102     unsigned char *lenchars;
103
104     pkt->curr = 0;
105     pkt->written = 0;
106
107     if ((pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs))) == NULL)
108         return 0;
109
110     if (lenbytes == 0)
111         return 1;
112
113     pkt->subs->pwritten = lenbytes;
114     pkt->subs->lenbytes = lenbytes;
115
116     if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {
117         OPENSSL_free(pkt->subs);
118         pkt->subs = NULL;
119         return 0;
120     }
121     pkt->subs->packet_len = 0;
122
123     return 1;
124 }
125
126 int WPACKET_init_static_len(WPACKET *pkt, unsigned char *buf, size_t len,
127                             size_t lenbytes)
128 {
129     size_t max = maxmaxsize(lenbytes);
130
131     /* Internal API, so should not fail */
132     if (!ossl_assert(buf != NULL && len > 0))
133         return 0;
134
135     pkt->staticbuf = buf;
136     pkt->buf = NULL;
137     pkt->maxsize = (max < len) ? max : len;
138     pkt->endfirst = 0;
139
140     return wpacket_intern_init_len(pkt, lenbytes);
141 }
142
143 int WPACKET_init_der(WPACKET *pkt, unsigned char *buf, size_t len)
144 {
145     /* Internal API, so should not fail */
146     if (!ossl_assert(buf != NULL && len > 0))
147         return 0;
148
149     pkt->staticbuf = buf;
150     pkt->buf = NULL;
151     pkt->maxsize = len;
152     pkt->endfirst = 1;
153
154     return wpacket_intern_init_len(pkt, 0);
155 }
156
157 int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)
158 {
159     /* Internal API, so should not fail */
160     if (!ossl_assert(buf != NULL))
161         return 0;
162
163     pkt->staticbuf = NULL;
164     pkt->buf = buf;
165     pkt->maxsize = maxmaxsize(lenbytes);
166     pkt->endfirst = 0;
167
168     return wpacket_intern_init_len(pkt, lenbytes);
169 }
170
171 int WPACKET_init(WPACKET *pkt, BUF_MEM *buf)
172 {
173     return WPACKET_init_len(pkt, buf, 0);
174 }
175
176 int WPACKET_init_null(WPACKET *pkt, size_t lenbytes)
177 {
178     pkt->staticbuf = NULL;
179     pkt->buf = NULL;
180     pkt->maxsize = maxmaxsize(lenbytes);
181     pkt->endfirst = 0;
182
183     return wpacket_intern_init_len(pkt, 0);
184 }
185
186 int WPACKET_init_null_der(WPACKET *pkt)
187 {
188     pkt->staticbuf = NULL;
189     pkt->buf = NULL;
190     pkt->maxsize = SIZE_MAX;
191     pkt->endfirst = 1;
192
193     return wpacket_intern_init_len(pkt, 0);
194 }
195
196 int WPACKET_set_flags(WPACKET *pkt, unsigned int flags)
197 {
198     /* Internal API, so should not fail */
199     if (!ossl_assert(pkt->subs != NULL))
200         return 0;
201
202     pkt->subs->flags = flags;
203
204     return 1;
205 }
206
207 /* Store the |value| of length |len| at location |data| */
208 static int put_value(unsigned char *data, uint64_t value, size_t len)
209 {
210     if (data == NULL)
211         return 1;
212
213     for (data += len - 1; len > 0; len--) {
214         *data = (unsigned char)(value & 0xff);
215         data--;
216         value >>= 8;
217     }
218
219     /* Check whether we could fit the value in the assigned number of bytes */
220     if (value > 0)
221         return 0;
222
223     return 1;
224 }
225
226 #ifndef OPENSSL_NO_QUIC
227 static int put_quic_value(unsigned char *data, size_t value, size_t len)
228 {
229     if (data == NULL)
230         return 1;
231
232     /* Value too large for field. */
233     if (ossl_quic_vlint_encode_len(value) > len)
234         return 0;
235
236     ossl_quic_vlint_encode_n(data, value, len);
237     return 1;
238 }
239 #endif
240
241 /*
242  * Internal helper function used by WPACKET_close(), WPACKET_finish() and
243  * WPACKET_fill_lengths() to close a sub-packet and write out its length if
244  * necessary. If |doclose| is 0 then it goes through the motions of closing
245  * (i.e. it fills in all the lengths), but doesn't actually close anything.
246  */
247 static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
248 {
249     size_t packlen = pkt->written - sub->pwritten;
250
251     if (packlen == 0
252             && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
253         return 0;
254
255     if (packlen == 0
256             && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
257         /* We can't handle this case. Return an error */
258         if (!doclose)
259             return 0;
260
261         /* Deallocate any bytes allocated for the length of the WPACKET */
262         if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
263             pkt->written -= sub->lenbytes;
264             pkt->curr -= sub->lenbytes;
265         }
266
267         /* Don't write out the packet length */
268         sub->packet_len = 0;
269         sub->lenbytes = 0;
270     }
271
272     /* Write out the WPACKET length if needed */
273     if (sub->lenbytes > 0) {
274         unsigned char *buf = GETBUF(pkt);
275
276         if (buf != NULL) {
277 #ifndef OPENSSL_NO_QUIC
278             if ((sub->flags & WPACKET_FLAGS_QUIC_VLINT) == 0) {
279                 if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
280                     return 0;
281             } else {
282                 if (!put_quic_value(&buf[sub->packet_len], packlen, sub->lenbytes))
283                     return 0;
284             }
285 #else
286             if (!put_value(&buf[sub->packet_len], packlen, sub->lenbytes))
287                 return 0;
288 #endif
289         }
290     } else if (pkt->endfirst && sub->parent != NULL
291                && (packlen != 0
292                    || (sub->flags
293                        & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) == 0)) {
294         size_t tmplen = packlen;
295         size_t numlenbytes = 1;
296
297         while ((tmplen = tmplen >> 8) > 0)
298             numlenbytes++;
299         if (!WPACKET_put_bytes__(pkt, packlen, numlenbytes))
300             return 0;
301         if (packlen > 0x7f) {
302             numlenbytes |= 0x80;
303             if (!WPACKET_put_bytes_u8(pkt, numlenbytes))
304                 return 0;
305         }
306     }
307
308     if (doclose) {
309         pkt->subs = sub->parent;
310         OPENSSL_free(sub);
311     }
312
313     return 1;
314 }
315
316 int WPACKET_fill_lengths(WPACKET *pkt)
317 {
318     WPACKET_SUB *sub;
319
320     if (!ossl_assert(pkt->subs != NULL))
321         return 0;
322
323     for (sub = pkt->subs; sub != NULL; sub = sub->parent) {
324         if (!wpacket_intern_close(pkt, sub, 0))
325             return 0;
326     }
327
328     return 1;
329 }
330
331 int WPACKET_close(WPACKET *pkt)
332 {
333     /*
334      * Internal API, so should not fail - but we do negative testing of this
335      * so no assert (otherwise the tests fail)
336      */
337     if (pkt->subs == NULL || pkt->subs->parent == NULL)
338         return 0;
339
340     return wpacket_intern_close(pkt, pkt->subs, 1);
341 }
342
343 int WPACKET_finish(WPACKET *pkt)
344 {
345     int ret;
346
347     /*
348      * Internal API, so should not fail - but we do negative testing of this
349      * so no assert (otherwise the tests fail)
350      */
351     if (pkt->subs == NULL || pkt->subs->parent != NULL)
352         return 0;
353
354     ret = wpacket_intern_close(pkt, pkt->subs, 1);
355     if (ret) {
356         OPENSSL_free(pkt->subs);
357         pkt->subs = NULL;
358     }
359
360     return ret;
361 }
362
363 int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)
364 {
365     WPACKET_SUB *sub;
366     unsigned char *lenchars;
367
368     /* Internal API, so should not fail */
369     if (!ossl_assert(pkt->subs != NULL))
370         return 0;
371
372     /* We don't support lenbytes greater than 0 when doing endfirst writing */
373     if (lenbytes > 0 && pkt->endfirst)
374         return 0;
375
376     if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL)
377         return 0;
378
379     sub->parent = pkt->subs;
380     pkt->subs = sub;
381     sub->pwritten = pkt->written + lenbytes;
382     sub->lenbytes = lenbytes;
383
384     if (lenbytes == 0) {
385         sub->packet_len = 0;
386         return 1;
387     }
388
389     sub->packet_len = pkt->written;
390
391     if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))
392         return 0;
393
394     return 1;
395 }
396
397 int WPACKET_start_sub_packet(WPACKET *pkt)
398 {
399     return WPACKET_start_sub_packet_len__(pkt, 0);
400 }
401
402 int WPACKET_put_bytes__(WPACKET *pkt, uint64_t val, size_t size)
403 {
404     unsigned char *data;
405
406     /* Internal API, so should not fail */
407     if (!ossl_assert(size <= sizeof(uint64_t))
408             || !WPACKET_allocate_bytes(pkt, size, &data)
409             || !put_value(data, val, size))
410         return 0;
411
412     return 1;
413 }
414
415 int WPACKET_set_max_size(WPACKET *pkt, size_t maxsize)
416 {
417     WPACKET_SUB *sub;
418     size_t lenbytes;
419
420     /* Internal API, so should not fail */
421     if (!ossl_assert(pkt->subs != NULL))
422         return 0;
423
424     /* Find the WPACKET_SUB for the top level */
425     for (sub = pkt->subs; sub->parent != NULL; sub = sub->parent)
426         continue;
427
428     lenbytes = sub->lenbytes;
429     if (lenbytes == 0)
430         lenbytes = sizeof(pkt->maxsize);
431
432     if (maxmaxsize(lenbytes) < maxsize || maxsize < pkt->written)
433         return 0;
434
435     pkt->maxsize = maxsize;
436
437     return 1;
438 }
439
440 int WPACKET_memset(WPACKET *pkt, int ch, size_t len)
441 {
442     unsigned char *dest;
443
444     if (len == 0)
445         return 1;
446
447     if (!WPACKET_allocate_bytes(pkt, len, &dest))
448         return 0;
449
450     if (dest != NULL)
451         memset(dest, ch, len);
452
453     return 1;
454 }
455
456 int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)
457 {
458     unsigned char *dest;
459
460     if (len == 0)
461         return 1;
462
463     if (!WPACKET_allocate_bytes(pkt, len, &dest))
464         return 0;
465
466     if (dest != NULL)
467         memcpy(dest, src, len);
468
469     return 1;
470 }
471
472 int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,
473                          size_t lenbytes)
474 {
475     if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)
476             || !WPACKET_memcpy(pkt, src, len)
477             || !WPACKET_close(pkt))
478         return 0;
479
480     return 1;
481 }
482
483 int WPACKET_get_total_written(WPACKET *pkt, size_t *written)
484 {
485     /* Internal API, so should not fail */
486     if (!ossl_assert(written != NULL))
487         return 0;
488
489     *written = pkt->written;
490
491     return 1;
492 }
493
494 int WPACKET_get_length(WPACKET *pkt, size_t *len)
495 {
496     /* Internal API, so should not fail */
497     if (!ossl_assert(pkt->subs != NULL && len != NULL))
498         return 0;
499
500     *len = pkt->written - pkt->subs->pwritten;
501
502     return 1;
503 }
504
505 unsigned char *WPACKET_get_curr(WPACKET *pkt)
506 {
507     unsigned char *buf = GETBUF(pkt);
508
509     if (buf == NULL)
510         return NULL;
511
512     if (pkt->endfirst)
513         return buf + pkt->maxsize - pkt->curr;
514
515     return buf + pkt->curr;
516 }
517
518 int WPACKET_is_null_buf(WPACKET *pkt)
519 {
520     return pkt->buf == NULL && pkt->staticbuf == NULL;
521 }
522
523 void WPACKET_cleanup(WPACKET *pkt)
524 {
525     WPACKET_SUB *sub, *parent;
526
527     for (sub = pkt->subs; sub != NULL; sub = parent) {
528         parent = sub->parent;
529         OPENSSL_free(sub);
530     }
531     pkt->subs = NULL;
532 }
533
534 #ifndef OPENSSL_NO_QUIC
535
536 int WPACKET_start_quic_sub_packet_bound(WPACKET *pkt, size_t max_len)
537 {
538     size_t enclen = ossl_quic_vlint_encode_len(max_len);
539
540     if (enclen == 0)
541         return 0;
542
543     if (WPACKET_start_sub_packet_len__(pkt, enclen) == 0)
544         return 0;
545
546     pkt->subs->flags |= WPACKET_FLAGS_QUIC_VLINT;
547     return 1;
548 }
549
550 int WPACKET_start_quic_sub_packet(WPACKET *pkt)
551 {
552     /*
553      * Assume no (sub)packet will exceed 4GiB, thus the 8-byte encoding need not
554      * be used.
555      */
556     return WPACKET_start_quic_sub_packet_bound(pkt, OSSL_QUIC_VLINT_4B_MIN);
557 }
558
559 int WPACKET_quic_sub_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
560 {
561     if (!WPACKET_start_quic_sub_packet_bound(pkt, len)
562             || !WPACKET_allocate_bytes(pkt, len, allocbytes)
563             || !WPACKET_close(pkt))
564         return 0;
565
566     return 1;
567 }
568
569 /*
570  * Write a QUIC variable-length integer to the packet.
571  */
572 int WPACKET_quic_write_vlint(WPACKET *pkt, uint64_t v)
573 {
574     unsigned char *b = NULL;
575     size_t enclen = ossl_quic_vlint_encode_len(v);
576
577     if (enclen == 0)
578         return 0;
579
580     if (WPACKET_allocate_bytes(pkt, enclen, &b) == 0)
581         return 0;
582
583     ossl_quic_vlint_encode(b, v);
584     return 1;
585 }
586
587 #endif