summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2019-01-17 06:32:12 +0000
committerjsing <>2019-01-17 06:32:12 +0000
commit338694cf0d2ff38a00bab7103081ffa3db2cbeca (patch)
treeaaa2cc958ec6b223edb11b9ce2f0c7cfe55040f0
parentcc9200b6a36a087c17fb0668d7adccb6a6261dd5 (diff)
downloadopenbsd-338694cf0d2ff38a00bab7103081ffa3db2cbeca.tar.gz
openbsd-338694cf0d2ff38a00bab7103081ffa3db2cbeca.tar.bz2
openbsd-338694cf0d2ff38a00bab7103081ffa3db2cbeca.zip
Provide an extensible buffer implementation that uses a read callback.
The read callback returns a TLS13_IO_* value on EOF, failure, want pollin or want pollout, or a positive value indicating the number of bytes read. This will be used by upcoming TLSv1.3 handshake message and record processing code, both of which need the ability to read a fixed size header, before extending the buffer to the number of bytes specified in the header. ok beck@ tb@
-rw-r--r--src/lib/libssl/Makefile5
-rw-r--r--src/lib/libssl/tls13_buffer.c125
-rw-r--r--src/lib/libssl/tls13_internal.h21
3 files changed, 148 insertions, 3 deletions
diff --git a/src/lib/libssl/Makefile b/src/lib/libssl/Makefile
index 8923c05bd4..a1300bc979 100644
--- a/src/lib/libssl/Makefile
+++ b/src/lib/libssl/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.44 2019/01/17 00:56:57 beck Exp $ 1# $OpenBSD: Makefile,v 1.45 2019/01/17 06:32:12 jsing Exp $
2 2
3.include <bsd.own.mk> 3.include <bsd.own.mk>
4.ifndef NOMAN 4.ifndef NOMAN
@@ -34,7 +34,8 @@ SRCS= \
34 ssl_asn1.c ssl_txt.c ssl_algs.c \ 34 ssl_asn1.c ssl_txt.c ssl_algs.c \
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_handshake.c tls13_key_schedule.c tls13_tlsext.c ssl_sigalgs.c 37 tls13_buffer.c tls13_handshake.c tls13_key_schedule.c tls13_tlsext.c \
38 ssl_sigalgs.c
38SRCS+= s3_cbc.c 39SRCS+= s3_cbc.c
39SRCS+= bs_ber.c bs_cbb.c bs_cbs.c 40SRCS+= bs_ber.c bs_cbb.c bs_cbs.c
40 41
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
23struct tls13_buffer {
24 size_t capacity;
25 uint8_t *data;
26 size_t len;
27 size_t offset;
28};
29
30static int tls13_buffer_resize(struct tls13_buffer *buf, size_t capacity);
31
32struct tls13_buffer *
33tls13_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
51void
52tls13_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
61static int
62tls13_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
78ssize_t
79tls13_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
105void
106tls13_buffer_cbs(struct tls13_buffer *buf, CBS *cbs)
107{
108 CBS_init(cbs, buf->data, buf->len);
109}
110
111int
112tls13_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}
diff --git a/src/lib/libssl/tls13_internal.h b/src/lib/libssl/tls13_internal.h
index 83f9988140..872aced77c 100644
--- a/src/lib/libssl/tls13_internal.h
+++ b/src/lib/libssl/tls13_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls13_internal.h,v 1.5 2018/11/09 23:56:20 jsing Exp $ */ 1/* $OpenBSD: tls13_internal.h,v 1.6 2019/01/17 06:32:12 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2018 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2018 Bob Beck <beck@openbsd.org>
4 * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
@@ -21,8 +21,27 @@
21 21
22#include <openssl/evp.h> 22#include <openssl/evp.h>
23 23
24#include "bytestring.h"
25
24__BEGIN_HIDDEN_DECLS 26__BEGIN_HIDDEN_DECLS
25 27
28#define TLS13_IO_EOF 0
29#define TLS13_IO_FAILURE -1
30#define TLS13_IO_WANT_POLLIN -2
31#define TLS13_IO_WANT_POLLOUT -3
32
33typedef ssize_t (*tls13_read_cb)(void *_buf, size_t _buflen, void *_cb_arg);
34typedef ssize_t (*tls13_write_cb)(const void *_buf, size_t _buflen, void *_cb_arg);
35
36struct tls13_buffer;
37
38struct tls13_buffer *tls13_buffer_new(size_t init_size);
39void tls13_buffer_free(struct tls13_buffer *buf);
40ssize_t tls13_buffer_extend(struct tls13_buffer *buf, size_t len,
41 tls13_read_cb read_cb, void *cb_arg);
42void tls13_buffer_cbs(struct tls13_buffer *buf, CBS *cbs);
43int tls13_buffer_finish(struct tls13_buffer *buf, uint8_t **out, size_t *out_len);
44
26struct tls13_secret { 45struct tls13_secret {
27 uint8_t *data; 46 uint8_t *data;
28 size_t len; 47 size_t len;