summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2019-01-19 02:53:54 +0000
committerjsing <>2019-01-19 02:53:54 +0000
commit6a367516df1cd7a6eb819c9bc94f4e4cb1d4f0eb (patch)
tree443b5e635b8534df280f5fb625336515a38ff99b /src
parent9f5c0205159ecdf058272249e3a756589faa7f3b (diff)
downloadopenbsd-6a367516df1cd7a6eb819c9bc94f4e4cb1d4f0eb.tar.gz
openbsd-6a367516df1cd7a6eb819c9bc94f4e4cb1d4f0eb.tar.bz2
openbsd-6a367516df1cd7a6eb819c9bc94f4e4cb1d4f0eb.zip
Provide a TLS record handling implementation.
This is a self-contained struct and set of functions that knows how to decode and read a TLS record from data supplied via a read callback, and send itself via a write callback. This will soon be used to build the TLSv1.3 record layer handling code. ok beck@ tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libssl/Makefile4
-rw-r--r--src/lib/libssl/tls13_record.c160
-rw-r--r--src/lib/libssl/tls13_record.h58
3 files changed, 220 insertions, 2 deletions
diff --git a/src/lib/libssl/Makefile b/src/lib/libssl/Makefile
index 9fc010d534..a5a1f2927f 100644
--- a/src/lib/libssl/Makefile
+++ b/src/lib/libssl/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.46 2019/01/18 03:41:30 beck Exp $ 1# $OpenBSD: Makefile,v 1.47 2019/01/19 02:53:54 jsing Exp $
2 2
3.include <bsd.own.mk> 3.include <bsd.own.mk>
4.ifndef NOMAN 4.ifndef NOMAN
@@ -35,7 +35,7 @@ SRCS= \
35 bio_ssl.c ssl_err.c ssl_methods.c \ 35 bio_ssl.c ssl_err.c ssl_methods.c \
36 ssl_packet.c ssl_tlsext.c ssl_versions.c pqueue.c ssl_init.c \ 36 ssl_packet.c ssl_tlsext.c ssl_versions.c pqueue.c ssl_init.c \
37 tls13_buffer.c tls13_handshake.c tls13_key_schedule.c \ 37 tls13_buffer.c tls13_handshake.c tls13_key_schedule.c \
38 ssl_sigalgs.c 38 tls13_record.c ssl_sigalgs.c
39SRCS+= s3_cbc.c 39SRCS+= s3_cbc.c
40SRCS+= bs_ber.c bs_cbb.c bs_cbs.c 40SRCS+= bs_ber.c bs_cbb.c bs_cbs.c
41 41
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}
diff --git a/src/lib/libssl/tls13_record.h b/src/lib/libssl/tls13_record.h
new file mode 100644
index 0000000000..ca7a63f99c
--- /dev/null
+++ b/src/lib/libssl/tls13_record.h
@@ -0,0 +1,58 @@
1/* $OpenBSD: tls13_record.h,v 1.1 2019/01/19 02:53:54 jsing Exp $ */
2/*
3 * Copyright (c) 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#ifndef HEADER_TLS13_RECORD_H
19#define HEADER_TLS13_RECORD_H
20
21#include "bytestring.h"
22#include "tls13_internal.h"
23
24__BEGIN_HIDDEN_DECLS
25
26/*
27 * TLSv1.3 - RFC 8446 section 5.
28 *
29 * The maximum plaintext is 2^14, however for inner plaintext an additional
30 * byte is allowed for the content type. A maximum AEAD overhead of 255-bytes
31 * is permitted, along with a 5-byte header, giving a maximum size of
32 * 5 + 2^14 + 1 + 255 = 16,645-bytes.
33 */
34#define TLS13_RECORD_HEADER_LEN 5
35#define TLS13_RECORD_MAX_AEAD_OVERHEAD 255
36#define TLS13_RECORD_MAX_PLAINTEXT_LEN 16384
37#define TLS13_RECORD_MAX_INNER_PLAINTEXT_LEN \
38 (TLS13_RECORD_MAX_PLAINTEXT_LEN + 1)
39#define TLS13_RECORD_MAX_CIPHERTEXT \
40 (TLS13_RECORD_MAX_INNER_PLAINTEXT_LEN + TLS13_RECORD_MAX_AEAD_OVERHEAD)
41#define TLS13_RECORD_MAX_LEN \
42 (TLS13_RECORD_HEADER_LEN + TLS13_RECORD_MAX_CIPHERTEXT)
43
44struct tls13_record *tls13_record_new(void);
45void tls13_record_free(struct tls13_record *_rec);
46uint8_t tls13_record_content_type(struct tls13_record *_rec);
47int tls13_record_content(struct tls13_record *_rec, CBS *_cbs);
48void tls13_record_data(struct tls13_record *_rec, CBS *_cbs);
49void tls13_record_set_data(struct tls13_record *_rec, uint8_t *_data,
50 size_t _data_len);
51ssize_t tls13_record_recv(struct tls13_record *_rec, tls13_read_cb _wire_read,
52 void *_wire_arg);
53ssize_t tls13_record_send(struct tls13_record *_rec, tls13_write_cb _wire_write,
54 void *_wire_arg);
55
56__END_HIDDEN_DECLS
57
58#endif