summaryrefslogtreecommitdiff
path: root/src/lib/libssl/tls13_record.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/tls13_record.c')
-rw-r--r--src/lib/libssl/tls13_record.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/src/lib/libssl/tls13_record.c b/src/lib/libssl/tls13_record.c
new file mode 100644
index 0000000000..857d3bee49
--- /dev/null
+++ b/src/lib/libssl/tls13_record.c
@@ -0,0 +1,160 @@
1/* $OpenBSD: tls13_record.c,v 1.1 2019/01/19 02:53:54 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 <openssl/curve25519.h>
21
22#include "tls13_internal.h"
23#include "tls13_record.h"
24
25struct tls13_record {
26 uint8_t content_type;
27 size_t rec_len;
28 uint8_t *data;
29 size_t data_len;
30 CBS cbs;
31
32 struct tls13_buffer *buf;
33};
34
35struct tls13_record *
36tls13_record_new(void)
37{
38 struct tls13_record *rec = NULL;
39
40 if ((rec = calloc(1, sizeof(struct tls13_record))) == NULL)
41 goto err;
42 if ((rec->buf = tls13_buffer_new(TLS13_RECORD_MAX_LEN)) == NULL)
43 goto err;
44
45 return rec;
46
47 err:
48 tls13_record_free(rec);
49
50 return NULL;
51}
52
53void
54tls13_record_free(struct tls13_record *rec)
55{
56 if (rec == NULL)
57 return;
58
59 tls13_buffer_free(rec->buf);
60
61 freezero(rec->data, rec->data_len);
62 freezero(rec, sizeof(struct tls13_record));
63}
64
65uint8_t
66tls13_record_content_type(struct tls13_record *rec)
67{
68 return rec->content_type;
69}
70
71int
72tls13_record_content(struct tls13_record *rec, CBS *cbs)
73{
74 CBS content;
75
76 tls13_record_data(rec, &content);
77
78 if (!CBS_skip(&content, TLS13_RECORD_HEADER_LEN))
79 return 0;
80
81 CBS_dup(&content, cbs);
82
83 return 1;
84}
85
86void
87tls13_record_data(struct tls13_record *rec, CBS *cbs)
88{
89 CBS_init(cbs, rec->data, rec->data_len);
90}
91
92void
93tls13_record_set_data(struct tls13_record *rec, uint8_t *data, size_t data_len)
94{
95 freezero(rec->data, rec->data_len);
96 rec->data = data;
97 rec->data_len = data_len;
98 CBS_init(&rec->cbs, rec->data, rec->data_len);
99}
100
101ssize_t
102tls13_record_recv(struct tls13_record *rec, tls13_read_cb wire_read,
103 void *wire_arg)
104{
105 uint16_t rec_len, rec_version;
106 uint8_t content_type;
107 CBS cbs;
108 int ret;
109
110 if (rec->data != NULL)
111 return TLS13_IO_FAILURE;
112
113 if (rec->content_type == 0) {
114 if ((ret = tls13_buffer_extend(rec->buf,
115 TLS13_RECORD_HEADER_LEN, wire_read, wire_arg)) <= 0)
116 return ret;
117
118 tls13_buffer_cbs(rec->buf, &cbs);
119
120 if (!CBS_get_u8(&cbs, &content_type))
121 return TLS13_IO_FAILURE;
122 if (!CBS_get_u16(&cbs, &rec_version))
123 return TLS13_IO_FAILURE;
124 if (!CBS_get_u16(&cbs, &rec_len))
125 return TLS13_IO_FAILURE;
126
127 rec->content_type = content_type;
128 rec->rec_len = rec_len;
129 }
130
131 if ((ret = tls13_buffer_extend(rec->buf,
132 TLS13_RECORD_HEADER_LEN + rec->rec_len, wire_read, wire_arg)) <= 0)
133 return ret;
134
135 if (!tls13_buffer_finish(rec->buf, &rec->data, &rec->data_len))
136 return TLS13_IO_FAILURE;
137
138 return rec->data_len;
139}
140
141ssize_t
142tls13_record_send(struct tls13_record *rec, tls13_write_cb wire_write,
143 void *wire_arg)
144{
145 ssize_t ret;
146
147 if (rec->data == NULL)
148 return TLS13_IO_FAILURE;
149
150 while (CBS_len(&rec->cbs) > 0) {
151 if ((ret = wire_write(CBS_data(&rec->cbs),
152 CBS_len(&rec->cbs), wire_arg)) <= 0)
153 return ret;
154
155 if (!CBS_skip(&rec->cbs, ret))
156 return TLS13_IO_FAILURE;
157 }
158
159 return rec->data_len;
160}