summaryrefslogtreecommitdiff
path: root/src/lib/libssl/tls_buffer.c
diff options
context:
space:
mode:
authorjsing <>2021-10-23 13:12:14 +0000
committerjsing <>2021-10-23 13:12:14 +0000
commitd2039e7348559d4183f5d055e691e70e1dadf349 (patch)
treebfbb08766325a11dd03fa658249cdacb09f2c3c0 /src/lib/libssl/tls_buffer.c
parentc4a5b1f8676a44f32abf55d9aff5ae7d2c36a4be (diff)
downloadopenbsd-d2039e7348559d4183f5d055e691e70e1dadf349.tar.gz
openbsd-d2039e7348559d4183f5d055e691e70e1dadf349.tar.bz2
openbsd-d2039e7348559d4183f5d055e691e70e1dadf349.zip
Rename tls13_buffer to tls_buffer.
This code will soon be used in the DTLSv1.2 and TLSv1.2 stack. Also introduce tls_internal.h and move/rename the read/write/flush callbacks. ok beck@ tb@
Diffstat (limited to 'src/lib/libssl/tls_buffer.c')
-rw-r--r--src/lib/libssl/tls_buffer.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/lib/libssl/tls_buffer.c b/src/lib/libssl/tls_buffer.c
new file mode 100644
index 0000000000..5c0ca7e40e
--- /dev/null
+++ b/src/lib/libssl/tls_buffer.c
@@ -0,0 +1,138 @@
1/* $OpenBSD: tls_buffer.c,v 1.1 2021/10/23 13:12:14 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 <stdlib.h>
19#include <string.h>
20
21#include "bytestring.h"
22#include "tls_internal.h"
23
24struct tls_buffer {
25 size_t capacity;
26 uint8_t *data;
27 size_t len;
28 size_t offset;
29};
30
31static int tls_buffer_resize(struct tls_buffer *buf, size_t capacity);
32
33struct tls_buffer *
34tls_buffer_new(size_t init_size)
35{
36 struct tls_buffer *buf = NULL;
37
38 if ((buf = calloc(1, sizeof(struct tls_buffer))) == NULL)
39 goto err;
40
41 if (!tls_buffer_resize(buf, init_size))
42 goto err;
43
44 return buf;
45
46 err:
47 tls_buffer_free(buf);
48
49 return NULL;
50}
51
52void
53tls_buffer_free(struct tls_buffer *buf)
54{
55 if (buf == NULL)
56 return;
57
58 freezero(buf->data, buf->capacity);
59 freezero(buf, sizeof(struct tls_buffer));
60}
61
62static int
63tls_buffer_resize(struct tls_buffer *buf, size_t capacity)
64{
65 uint8_t *data;
66
67 if (buf->capacity == capacity)
68 return 1;
69
70 if ((data = recallocarray(buf->data, buf->capacity, capacity, 1)) == NULL)
71 return 0;
72
73 buf->data = data;
74 buf->capacity = capacity;
75
76 return 1;
77}
78
79int
80tls_buffer_set_data(struct tls_buffer *buf, CBS *data)
81{
82 if (!tls_buffer_resize(buf, CBS_len(data)))
83 return 0;
84 memcpy(buf->data, CBS_data(data), CBS_len(data));
85 return 1;
86}
87
88ssize_t
89tls_buffer_extend(struct tls_buffer *buf, size_t len,
90 tls_read_cb read_cb, void *cb_arg)
91{
92 ssize_t ret;
93
94 if (len == buf->len)
95 return buf->len;
96
97 if (len < buf->len)
98 return TLS_IO_FAILURE;
99
100 if (!tls_buffer_resize(buf, len))
101 return TLS_IO_FAILURE;
102
103 for (;;) {
104 if ((ret = read_cb(&buf->data[buf->len],
105 buf->capacity - buf->len, cb_arg)) <= 0)
106 return ret;
107
108 if (ret > buf->capacity - buf->len)
109 return TLS_IO_FAILURE;
110
111 buf->len += ret;
112
113 if (buf->len == buf->capacity)
114 return buf->len;
115 }
116}
117
118void
119tls_buffer_cbs(struct tls_buffer *buf, CBS *cbs)
120{
121 CBS_init(cbs, buf->data, buf->len);
122}
123
124int
125tls_buffer_finish(struct tls_buffer *buf, uint8_t **out, size_t *out_len)
126{
127 if (out == NULL || out_len == NULL)
128 return 0;
129
130 *out = buf->data;
131 *out_len = buf->len;
132
133 buf->capacity = 0;
134 buf->data = NULL;
135 buf->len = 0;
136
137 return 1;
138}