diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libssl/tls13_buffer.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/lib/libssl/tls13_buffer.c b/src/lib/libssl/tls13_buffer.c new file mode 100644 index 0000000000..1b490c8b46 --- /dev/null +++ b/src/lib/libssl/tls13_buffer.c | |||
@@ -0,0 +1,125 @@ | |||
1 | /* $OpenBSD: tls13_buffer.c,v 1.1 2019/01/17 06:32:12 jsing Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include "ssl_locl.h" | ||
19 | |||
20 | #include "bytestring.h" | ||
21 | #include "tls13_internal.h" | ||
22 | |||
23 | struct tls13_buffer { | ||
24 | size_t capacity; | ||
25 | uint8_t *data; | ||
26 | size_t len; | ||
27 | size_t offset; | ||
28 | }; | ||
29 | |||
30 | static int tls13_buffer_resize(struct tls13_buffer *buf, size_t capacity); | ||
31 | |||
32 | struct tls13_buffer * | ||
33 | tls13_buffer_new(size_t init_size) | ||
34 | { | ||
35 | struct tls13_buffer *buf = NULL; | ||
36 | |||
37 | if ((buf = calloc(1, sizeof(struct tls13_buffer))) == NULL) | ||
38 | goto err; | ||
39 | |||
40 | if (!tls13_buffer_resize(buf, init_size)) | ||
41 | goto err; | ||
42 | |||
43 | return buf; | ||
44 | |||
45 | err: | ||
46 | tls13_buffer_free(buf); | ||
47 | |||
48 | return NULL; | ||
49 | } | ||
50 | |||
51 | void | ||
52 | tls13_buffer_free(struct tls13_buffer *buf) | ||
53 | { | ||
54 | if (buf == NULL) | ||
55 | return; | ||
56 | |||
57 | freezero(buf->data, buf->capacity); | ||
58 | freezero(buf, sizeof(struct tls13_buffer)); | ||
59 | } | ||
60 | |||
61 | static int | ||
62 | tls13_buffer_resize(struct tls13_buffer *buf, size_t capacity) | ||
63 | { | ||
64 | uint8_t *data; | ||
65 | |||
66 | if (buf->capacity == capacity) | ||
67 | return 1; | ||
68 | |||
69 | if ((data = recallocarray(buf->data, buf->capacity, capacity, 1)) == NULL) | ||
70 | return 0; | ||
71 | |||
72 | buf->data = data; | ||
73 | buf->capacity = capacity; | ||
74 | |||
75 | return 1; | ||
76 | } | ||
77 | |||
78 | ssize_t | ||
79 | tls13_buffer_extend(struct tls13_buffer *buf, size_t len, | ||
80 | tls13_read_cb read_cb, void *cb_arg) | ||
81 | { | ||
82 | ssize_t ret; | ||
83 | |||
84 | if (len == buf->len) | ||
85 | return buf->len; | ||
86 | |||
87 | if (len < buf->len) | ||
88 | return TLS13_IO_FAILURE; | ||
89 | |||
90 | if (!tls13_buffer_resize(buf, len)) | ||
91 | return TLS13_IO_FAILURE; | ||
92 | |||
93 | for (;;) { | ||
94 | if ((ret = read_cb(&buf->data[buf->len], | ||
95 | buf->capacity - buf->len, cb_arg)) <= 0) | ||
96 | return ret; | ||
97 | |||
98 | buf->len += ret; | ||
99 | |||
100 | if (buf->len == buf->capacity) | ||
101 | return buf->len; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | void | ||
106 | tls13_buffer_cbs(struct tls13_buffer *buf, CBS *cbs) | ||
107 | { | ||
108 | CBS_init(cbs, buf->data, buf->len); | ||
109 | } | ||
110 | |||
111 | int | ||
112 | tls13_buffer_finish(struct tls13_buffer *buf, uint8_t **out, size_t *out_len) | ||
113 | { | ||
114 | if (out == NULL || out_len == NULL) | ||
115 | return 0; | ||
116 | |||
117 | *out = buf->data; | ||
118 | *out_len = buf->len; | ||
119 | |||
120 | buf->capacity = 0; | ||
121 | buf->data = NULL; | ||
122 | buf->len = 0; | ||
123 | |||
124 | return 1; | ||
125 | } | ||