diff options
author | bcook <> | 2016-09-04 12:26:43 +0000 |
---|---|---|
committer | bcook <> | 2016-09-04 12:26:43 +0000 |
commit | ad909e37b20a2c933e88e0e359b2fdb401d05092 (patch) | |
tree | 00adeae01265c1c035072ffd2eb37000b9640ece /src | |
parent | 50df0f10141bc06d1ff2d6bd98be2f5ab87857e6 (diff) | |
download | openbsd-ad909e37b20a2c933e88e0e359b2fdb401d05092.tar.gz openbsd-ad909e37b20a2c933e88e0e359b2fdb401d05092.tar.bz2 openbsd-ad909e37b20a2c933e88e0e359b2fdb401d05092.zip |
Add callback-based interface to libtls.
This allows working with buffers and callback functions instead of directly on
sockets or file descriptors.
Original patch from Tobias Pape <tobias_at_netshed.de>.
ok beck@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libtls/Makefile | 3 | ||||
-rw-r--r-- | src/lib/libtls/shlib_version | 2 | ||||
-rw-r--r-- | src/lib/libtls/tls.c | 6 | ||||
-rw-r--r-- | src/lib/libtls/tls.h | 11 | ||||
-rw-r--r-- | src/lib/libtls/tls_bio_cb.c | 224 | ||||
-rw-r--r-- | src/lib/libtls/tls_client.c | 76 | ||||
-rw-r--r-- | src/lib/libtls/tls_init.3 | 14 | ||||
-rw-r--r-- | src/lib/libtls/tls_internal.h | 9 | ||||
-rw-r--r-- | src/lib/libtls/tls_server.c | 60 | ||||
-rw-r--r-- | src/lib/libtls/tls_verify.c | 3 |
10 files changed, 371 insertions, 37 deletions
diff --git a/src/lib/libtls/Makefile b/src/lib/libtls/Makefile index ca2f00bc8f..356c6f5b2e 100644 --- a/src/lib/libtls/Makefile +++ b/src/lib/libtls/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.23 2016/03/30 06:38:43 jmc Exp $ | 1 | # $OpenBSD: Makefile,v 1.24 2016/09/04 12:26:43 bcook Exp $ |
2 | 2 | ||
3 | CFLAGS+= -Wall -Werror -Wimplicit | 3 | CFLAGS+= -Wall -Werror -Wimplicit |
4 | CFLAGS+= -DLIBRESSL_INTERNAL | 4 | CFLAGS+= -DLIBRESSL_INTERNAL |
@@ -13,6 +13,7 @@ LDADD+= -L${BSDOBJDIR}/lib/libssl/ssl -lssl | |||
13 | HDRS= tls.h | 13 | HDRS= tls.h |
14 | 14 | ||
15 | SRCS= tls.c \ | 15 | SRCS= tls.c \ |
16 | tls_bio_cb.c \ | ||
16 | tls_client.c \ | 17 | tls_client.c \ |
17 | tls_config.c \ | 18 | tls_config.c \ |
18 | tls_conninfo.c \ | 19 | tls_conninfo.c \ |
diff --git a/src/lib/libtls/shlib_version b/src/lib/libtls/shlib_version index 5e6c3dd51b..f6b149e586 100644 --- a/src/lib/libtls/shlib_version +++ b/src/lib/libtls/shlib_version | |||
@@ -1,2 +1,2 @@ | |||
1 | major=11 | 1 | major=11 |
2 | minor=3 | 2 | minor=4 |
diff --git a/src/lib/libtls/tls.c b/src/lib/libtls/tls.c index 85faedd56d..00564edb3c 100644 --- a/src/lib/libtls/tls.c +++ b/src/lib/libtls/tls.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls.c,v 1.48 2016/08/22 17:12:35 jsing Exp $ */ | 1 | /* $OpenBSD: tls.c,v 1.49 2016/09/04 12:26:43 bcook Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -424,6 +424,10 @@ tls_reset(struct tls *ctx) | |||
424 | tls_sni_ctx_free(sni); | 424 | tls_sni_ctx_free(sni); |
425 | } | 425 | } |
426 | ctx->sni_ctx = NULL; | 426 | ctx->sni_ctx = NULL; |
427 | |||
428 | ctx->read_cb = NULL; | ||
429 | ctx->write_cb = NULL; | ||
430 | ctx->cb_arg = NULL; | ||
427 | } | 431 | } |
428 | 432 | ||
429 | int | 433 | int |
diff --git a/src/lib/libtls/tls.h b/src/lib/libtls/tls.h index 107614c759..e4dd31775a 100644 --- a/src/lib/libtls/tls.h +++ b/src/lib/libtls/tls.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls.h,v 1.35 2016/08/22 14:58:26 jsing Exp $ */ | 1 | /* $OpenBSD: tls.h,v 1.36 2016/09/04 12:26:43 bcook Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -44,6 +44,11 @@ extern "C" { | |||
44 | struct tls; | 44 | struct tls; |
45 | struct tls_config; | 45 | struct tls_config; |
46 | 46 | ||
47 | typedef ssize_t (*tls_read_cb)(void *_ctx, void *_buf, size_t _buflen, | ||
48 | void *_cb_arg); | ||
49 | typedef ssize_t (*tls_write_cb)(void *_ctx, const void *_buf, | ||
50 | size_t _buflen, void *_cb_arg); | ||
51 | |||
47 | int tls_init(void); | 52 | int tls_init(void); |
48 | 53 | ||
49 | const char *tls_config_error(struct tls_config *_config); | 54 | const char *tls_config_error(struct tls_config *_config); |
@@ -102,12 +107,16 @@ void tls_free(struct tls *_ctx); | |||
102 | int tls_accept_fds(struct tls *_ctx, struct tls **_cctx, int _fd_read, | 107 | int tls_accept_fds(struct tls *_ctx, struct tls **_cctx, int _fd_read, |
103 | int _fd_write); | 108 | int _fd_write); |
104 | int tls_accept_socket(struct tls *_ctx, struct tls **_cctx, int _socket); | 109 | int tls_accept_socket(struct tls *_ctx, struct tls **_cctx, int _socket); |
110 | int tls_accept_cbs(struct tls *_ctx, struct tls **_cctx, | ||
111 | tls_read_cb _read_cb, tls_write_cb _write_cb, void *_cb_arg); | ||
105 | int tls_connect(struct tls *_ctx, const char *_host, const char *_port); | 112 | int tls_connect(struct tls *_ctx, const char *_host, const char *_port); |
106 | int tls_connect_fds(struct tls *_ctx, int _fd_read, int _fd_write, | 113 | int tls_connect_fds(struct tls *_ctx, int _fd_read, int _fd_write, |
107 | const char *_servername); | 114 | const char *_servername); |
108 | int tls_connect_servername(struct tls *_ctx, const char *_host, | 115 | int tls_connect_servername(struct tls *_ctx, const char *_host, |
109 | const char *_port, const char *_servername); | 116 | const char *_port, const char *_servername); |
110 | int tls_connect_socket(struct tls *_ctx, int _s, const char *_servername); | 117 | int tls_connect_socket(struct tls *_ctx, int _s, const char *_servername); |
118 | int tls_connect_cbs(struct tls *_ctx, tls_read_cb _read_cb, | ||
119 | tls_write_cb _write_cb, void *_cb_arg, const char *_servername); | ||
111 | int tls_handshake(struct tls *_ctx); | 120 | int tls_handshake(struct tls *_ctx); |
112 | ssize_t tls_read(struct tls *_ctx, void *_buf, size_t _buflen); | 121 | ssize_t tls_read(struct tls *_ctx, void *_buf, size_t _buflen); |
113 | ssize_t tls_write(struct tls *_ctx, const void *_buf, size_t _buflen); | 122 | ssize_t tls_write(struct tls *_ctx, const void *_buf, size_t _buflen); |
diff --git a/src/lib/libtls/tls_bio_cb.c b/src/lib/libtls/tls_bio_cb.c new file mode 100644 index 0000000000..fc1c964912 --- /dev/null +++ b/src/lib/libtls/tls_bio_cb.c | |||
@@ -0,0 +1,224 @@ | |||
1 | /* $ID$ */ | ||
2 | /* | ||
3 | * Copyright (c) 2016 Tobias Pape <tobias@netshed.de> | ||
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 <unistd.h> | ||
20 | #include <fcntl.h> | ||
21 | |||
22 | #include "tls.h" | ||
23 | #include "tls_internal.h" | ||
24 | |||
25 | #include <openssl/bio.h> | ||
26 | |||
27 | static int write_cb(BIO *b, const char *buf, int num); | ||
28 | static int read_cb(BIO *b, char *buf, int size); | ||
29 | static int puts_cb(BIO *b, const char *str); | ||
30 | static long ctrl_cb(BIO *b, int cmd, long num, void *ptr); | ||
31 | static int new_cb(BIO *b); | ||
32 | static int free_cb(BIO *data); | ||
33 | |||
34 | struct bio_cb_st { | ||
35 | int (*write_cb)(BIO *h, const char *buf, int num, void *cb_arg); | ||
36 | int (*read_cb)(BIO *h, char *buf, int size, void *cb_arg); | ||
37 | void *cb_arg; | ||
38 | }; | ||
39 | |||
40 | static BIO_METHOD cb_method = { | ||
41 | .type = BIO_TYPE_MEM, | ||
42 | .name = "libtls_callbacks", | ||
43 | .bwrite = write_cb, | ||
44 | .bread = read_cb, | ||
45 | .bputs = puts_cb, | ||
46 | .ctrl = ctrl_cb, | ||
47 | .create = new_cb, | ||
48 | .destroy = free_cb | ||
49 | }; | ||
50 | |||
51 | static BIO_METHOD * | ||
52 | bio_s_cb(void) | ||
53 | { | ||
54 | return (&cb_method); | ||
55 | } | ||
56 | |||
57 | static int | ||
58 | bio_set_write_cb(BIO *bi, | ||
59 | int (*write_cb)(BIO *h, const char *buf, int num, void *cb_arg)) | ||
60 | { | ||
61 | struct bio_cb_st *b; | ||
62 | b = (struct bio_cb_st *)bi->ptr; | ||
63 | b->write_cb = write_cb; | ||
64 | return (0); | ||
65 | } | ||
66 | |||
67 | static int | ||
68 | bio_set_read_cb(BIO *bi, | ||
69 | int (*read_cb)(BIO *h, char *buf, int size, void *cb_arg)) | ||
70 | { | ||
71 | struct bio_cb_st *b; | ||
72 | b = (struct bio_cb_st *)bi->ptr; | ||
73 | b->read_cb = read_cb; | ||
74 | return (0); | ||
75 | } | ||
76 | |||
77 | static int | ||
78 | bio_set_cb_arg(BIO *bi, void *cb_arg) | ||
79 | { | ||
80 | struct bio_cb_st *b; | ||
81 | b = (struct bio_cb_st *)bi->ptr; | ||
82 | b->cb_arg = cb_arg; | ||
83 | return (0); | ||
84 | } | ||
85 | |||
86 | static int | ||
87 | new_cb(BIO *bi) | ||
88 | { | ||
89 | struct bio_cb_st *bcb; | ||
90 | |||
91 | bcb = calloc(1, sizeof(struct bio_cb_st)); | ||
92 | if (bcb == NULL) | ||
93 | return (0); | ||
94 | |||
95 | bi->shutdown = 1; | ||
96 | bi->init = 1; | ||
97 | bi->num = -1; | ||
98 | bi->ptr = (char *)bcb; | ||
99 | |||
100 | return (1); | ||
101 | } | ||
102 | |||
103 | static int | ||
104 | free_cb(BIO *bi) | ||
105 | { | ||
106 | if (bi == NULL) | ||
107 | return (0); | ||
108 | |||
109 | if (bi->shutdown) { | ||
110 | if ((bi->init) && (bi->ptr != NULL)) { | ||
111 | struct bio_cb_st *b; | ||
112 | b = (struct bio_cb_st *)bi->ptr; | ||
113 | free(b); | ||
114 | bi->ptr = NULL; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | return (1); | ||
119 | } | ||
120 | |||
121 | static int | ||
122 | read_cb(BIO *b, char *buf, int size) | ||
123 | { | ||
124 | struct bio_cb_st *bcb = b->ptr; | ||
125 | return (bcb->read_cb(b, buf, size, bcb->cb_arg)); | ||
126 | } | ||
127 | |||
128 | static int | ||
129 | write_cb(BIO *b, const char *buf, int num) | ||
130 | { | ||
131 | struct bio_cb_st *bcb = b->ptr; | ||
132 | return (bcb->write_cb(b, buf, num, bcb->cb_arg)); | ||
133 | } | ||
134 | |||
135 | static int | ||
136 | puts_cb(BIO *b, const char *str) | ||
137 | { | ||
138 | int n; | ||
139 | |||
140 | n = strlen(str); | ||
141 | return (write_cb(b, str, n)); | ||
142 | } | ||
143 | |||
144 | static long | ||
145 | ctrl_cb(BIO *b, int cmd, long num, void *ptr) | ||
146 | { | ||
147 | long ret = 1; | ||
148 | |||
149 | switch (cmd) { | ||
150 | case BIO_CTRL_GET_CLOSE: | ||
151 | ret = (long)b->shutdown; | ||
152 | break; | ||
153 | case BIO_CTRL_SET_CLOSE: | ||
154 | b->shutdown = (int)num; | ||
155 | break; | ||
156 | case BIO_CTRL_DUP: | ||
157 | break; | ||
158 | case BIO_CTRL_INFO: | ||
159 | case BIO_CTRL_GET: | ||
160 | case BIO_CTRL_SET: | ||
161 | default: | ||
162 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
163 | } | ||
164 | |||
165 | return (ret); | ||
166 | } | ||
167 | |||
168 | static int | ||
169 | tls_bio_write_cb(BIO *h, const char *buf, int num, void *cb_arg) | ||
170 | { | ||
171 | struct tls *ctx = cb_arg; | ||
172 | return (ctx->write_cb)(ctx, buf, num, ctx->cb_arg); | ||
173 | } | ||
174 | |||
175 | static int | ||
176 | tls_bio_read_cb(BIO *h, char *buf, int size, void *cb_arg) | ||
177 | { | ||
178 | struct tls *ctx = cb_arg; | ||
179 | return (ctx->read_cb)(ctx, buf, size, ctx->cb_arg); | ||
180 | } | ||
181 | |||
182 | static BIO * | ||
183 | tls_get_new_cb_bio(struct tls *ctx) | ||
184 | { | ||
185 | BIO *bcb; | ||
186 | if (ctx->read_cb == NULL || ctx->write_cb == NULL) | ||
187 | tls_set_errorx(ctx, "no callbacks registered"); | ||
188 | |||
189 | bcb = BIO_new(bio_s_cb()); | ||
190 | if (bcb == NULL) { | ||
191 | tls_set_errorx(ctx, "failed to create callback i/o"); | ||
192 | return (NULL); | ||
193 | } | ||
194 | |||
195 | bio_set_write_cb(bcb, tls_bio_write_cb); | ||
196 | bio_set_read_cb(bcb, tls_bio_read_cb); | ||
197 | bio_set_cb_arg(bcb, ctx); | ||
198 | |||
199 | return (bcb); | ||
200 | } | ||
201 | |||
202 | int | ||
203 | tls_set_cbs(struct tls *ctx, tls_read_cb read_cb, tls_write_cb write_cb, | ||
204 | void *cb_arg) | ||
205 | { | ||
206 | int rv = -1; | ||
207 | BIO *bcb; | ||
208 | ctx->read_cb = read_cb; | ||
209 | ctx->write_cb = write_cb; | ||
210 | ctx->cb_arg = cb_arg; | ||
211 | |||
212 | bcb = tls_get_new_cb_bio(ctx); | ||
213 | if (bcb == NULL) { | ||
214 | tls_set_errorx(ctx, "failed to create callback i/o"); | ||
215 | goto err; | ||
216 | } | ||
217 | |||
218 | SSL_set_bio(ctx->ssl_conn, bcb, bcb); | ||
219 | |||
220 | rv = 0; | ||
221 | |||
222 | err: | ||
223 | return (rv); | ||
224 | } | ||
diff --git a/src/lib/libtls/tls_client.c b/src/lib/libtls/tls_client.c index c360ecad52..f8d35a18c1 100644 --- a/src/lib/libtls/tls_client.c +++ b/src/lib/libtls/tls_client.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls_client.c,v 1.34 2016/08/15 14:04:23 jsing Exp $ */ | 1 | /* $OpenBSD: tls_client.c,v 1.35 2016/09/04 12:26:43 bcook Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -158,15 +158,8 @@ tls_connect_servername(struct tls *ctx, const char *host, const char *port, | |||
158 | return (rv); | 158 | return (rv); |
159 | } | 159 | } |
160 | 160 | ||
161 | int | 161 | static int |
162 | tls_connect_socket(struct tls *ctx, int s, const char *servername) | 162 | connect_common(struct tls *ctx, const char *servername) |
163 | { | ||
164 | return tls_connect_fds(ctx, s, s, servername); | ||
165 | } | ||
166 | |||
167 | int | ||
168 | tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | ||
169 | const char *servername) | ||
170 | { | 163 | { |
171 | union tls_addr addrbuf; | 164 | union tls_addr addrbuf; |
172 | int rv = -1; | 165 | int rv = -1; |
@@ -176,11 +169,6 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
176 | goto err; | 169 | goto err; |
177 | } | 170 | } |
178 | 171 | ||
179 | if (fd_read < 0 || fd_write < 0) { | ||
180 | tls_set_errorx(ctx, "invalid file descriptors"); | ||
181 | goto err; | ||
182 | } | ||
183 | |||
184 | if (servername != NULL) { | 172 | if (servername != NULL) { |
185 | if ((ctx->servername = strdup(servername)) == NULL) { | 173 | if ((ctx->servername = strdup(servername)) == NULL) { |
186 | tls_set_errorx(ctx, "out of memory"); | 174 | tls_set_errorx(ctx, "out of memory"); |
@@ -195,6 +183,7 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
195 | 183 | ||
196 | if (tls_configure_ssl(ctx, ctx->ssl_ctx) != 0) | 184 | if (tls_configure_ssl(ctx, ctx->ssl_ctx) != 0) |
197 | goto err; | 185 | goto err; |
186 | |||
198 | if (tls_configure_ssl_keypair(ctx, ctx->ssl_ctx, | 187 | if (tls_configure_ssl_keypair(ctx, ctx->ssl_ctx, |
199 | ctx->config->keypair, 0) != 0) | 188 | ctx->config->keypair, 0) != 0) |
200 | goto err; | 189 | goto err; |
@@ -205,6 +194,7 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
205 | goto err; | 194 | goto err; |
206 | } | 195 | } |
207 | } | 196 | } |
197 | |||
208 | if (ctx->config->verify_cert && | 198 | if (ctx->config->verify_cert && |
209 | (tls_configure_ssl_verify(ctx, ctx->ssl_ctx, | 199 | (tls_configure_ssl_verify(ctx, ctx->ssl_ctx, |
210 | SSL_VERIFY_PEER) == -1)) | 200 | SSL_VERIFY_PEER) == -1)) |
@@ -214,15 +204,11 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
214 | tls_set_errorx(ctx, "ssl connection failure"); | 204 | tls_set_errorx(ctx, "ssl connection failure"); |
215 | goto err; | 205 | goto err; |
216 | } | 206 | } |
207 | |||
217 | if (SSL_set_app_data(ctx->ssl_conn, ctx) != 1) { | 208 | if (SSL_set_app_data(ctx->ssl_conn, ctx) != 1) { |
218 | tls_set_errorx(ctx, "ssl application data failure"); | 209 | tls_set_errorx(ctx, "ssl application data failure"); |
219 | goto err; | 210 | goto err; |
220 | } | 211 | } |
221 | if (SSL_set_rfd(ctx->ssl_conn, fd_read) != 1 || | ||
222 | SSL_set_wfd(ctx->ssl_conn, fd_write) != 1) { | ||
223 | tls_set_errorx(ctx, "ssl file descriptor failure"); | ||
224 | goto err; | ||
225 | } | ||
226 | 212 | ||
227 | /* | 213 | /* |
228 | * RFC4366 (SNI): Literal IPv4 and IPv6 addresses are not | 214 | * RFC4366 (SNI): Literal IPv4 and IPv6 addresses are not |
@@ -236,6 +222,56 @@ tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | |||
236 | goto err; | 222 | goto err; |
237 | } | 223 | } |
238 | } | 224 | } |
225 | rv = 0; | ||
226 | |||
227 | err: | ||
228 | return (rv); | ||
229 | } | ||
230 | |||
231 | int | ||
232 | tls_connect_socket(struct tls *ctx, int s, const char *servername) | ||
233 | { | ||
234 | return tls_connect_fds(ctx, s, s, servername); | ||
235 | } | ||
236 | |||
237 | int | ||
238 | tls_connect_fds(struct tls *ctx, int fd_read, int fd_write, | ||
239 | const char *servername) | ||
240 | { | ||
241 | int rv = -1; | ||
242 | |||
243 | if (fd_read < 0 || fd_write < 0) { | ||
244 | tls_set_errorx(ctx, "invalid file descriptors"); | ||
245 | goto err; | ||
246 | } | ||
247 | |||
248 | if (connect_common(ctx, servername) != 0) | ||
249 | goto err; | ||
250 | |||
251 | if (SSL_set_rfd(ctx->ssl_conn, fd_read) != 1 || | ||
252 | SSL_set_wfd(ctx->ssl_conn, fd_write) != 1) { | ||
253 | tls_set_errorx(ctx, "ssl file descriptor failure"); | ||
254 | goto err; | ||
255 | } | ||
256 | |||
257 | rv = 0; | ||
258 | err: | ||
259 | return (rv); | ||
260 | } | ||
261 | |||
262 | int | ||
263 | tls_connect_cbs(struct tls *ctx, tls_read_cb read_cb, | ||
264 | tls_write_cb write_cb, void *cb_arg, const char *servername) | ||
265 | { | ||
266 | int rv = -1; | ||
267 | |||
268 | if (connect_common(ctx, servername) != 0) | ||
269 | goto err; | ||
270 | |||
271 | if (tls_set_cbs(ctx, read_cb, write_cb, cb_arg) != 0) { | ||
272 | tls_set_errorx(ctx, "callback registration failure"); | ||
273 | goto err; | ||
274 | } | ||
239 | 275 | ||
240 | rv = 0; | 276 | rv = 0; |
241 | 277 | ||
diff --git a/src/lib/libtls/tls_init.3 b/src/lib/libtls/tls_init.3 index f9a8ac1f80..762a76633b 100644 --- a/src/lib/libtls/tls_init.3 +++ b/src/lib/libtls/tls_init.3 | |||
@@ -1,4 +1,4 @@ | |||
1 | .\" $OpenBSD: tls_init.3,v 1.68 2016/09/04 12:03:24 jsing Exp $ | 1 | .\" $OpenBSD: tls_init.3,v 1.69 2016/09/04 12:26:43 bcook Exp $ |
2 | .\" | 2 | .\" |
3 | .\" Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> | 3 | .\" Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> |
4 | .\" | 4 | .\" |
@@ -71,8 +71,10 @@ | |||
71 | .Nm tls_connect_fds , | 71 | .Nm tls_connect_fds , |
72 | .Nm tls_connect_servername , | 72 | .Nm tls_connect_servername , |
73 | .Nm tls_connect_socket , | 73 | .Nm tls_connect_socket , |
74 | .Nm tls_connect_cbs , | ||
74 | .Nm tls_accept_fds , | 75 | .Nm tls_accept_fds , |
75 | .Nm tls_accept_socket , | 76 | .Nm tls_accept_socket , |
77 | .Nm tls_accept_cbs , | ||
76 | .Nm tls_handshake , | 78 | .Nm tls_handshake , |
77 | .Nm tls_read , | 79 | .Nm tls_read , |
78 | .Nm tls_write , | 80 | .Nm tls_write , |
@@ -187,10 +189,14 @@ | |||
187 | .Ft "int" | 189 | .Ft "int" |
188 | .Fn tls_connect_socket "struct tls *ctx" "int s" "const char *servername" | 190 | .Fn tls_connect_socket "struct tls *ctx" "int s" "const char *servername" |
189 | .Ft "int" | 191 | .Ft "int" |
192 | .Fn tls_connect_cbs "struct tls *ctx" "ssize_t (*tls_read_cb)(void *ctx, void *buf, size_t buflen, void *cb_arg)" "ssize_t (*tls_write_cb)(void *ctx, const void *buf, size_t buflen, void *cb_arg)" "void *cb_arg" "const char *servername" | ||
193 | .Ft "int" | ||
190 | .Fn tls_accept_fds "struct tls *tls" "struct tls **cctx" "int fd_read" "int fd_write" | 194 | .Fn tls_accept_fds "struct tls *tls" "struct tls **cctx" "int fd_read" "int fd_write" |
191 | .Ft "int" | 195 | .Ft "int" |
192 | .Fn tls_accept_socket "struct tls *tls" "struct tls **cctx" "int socket" | 196 | .Fn tls_accept_socket "struct tls *tls" "struct tls **cctx" "int socket" |
193 | .Ft "int" | 197 | .Ft "int" |
198 | .Fn tls_accept_cbs "struct tls *ctx" "struct tls **cctx" "ssize_t (*tls_read_cb)(void *ctx, void *buf, size_t buflen, void *cb_arg)" "ssize_t (*tls_write_cb)(void *ctx, const void *buf, size_t buflen, void *cb_arg)" "void *cb_arg" | ||
199 | .Ft "int" | ||
194 | .Fn tls_handshake "struct tls *ctx" | 200 | .Fn tls_handshake "struct tls *ctx" |
195 | .Ft "ssize_t" | 201 | .Ft "ssize_t" |
196 | .Fn tls_read "struct tls *ctx" "void *buf" "size_t buflen" | 202 | .Fn tls_read "struct tls *ctx" "void *buf" "size_t buflen" |
@@ -247,6 +253,9 @@ An already existing socket can be upgraded to a secure connection by calling | |||
247 | Alternatively, a secure connection can be established over a pair of existing | 253 | Alternatively, a secure connection can be established over a pair of existing |
248 | file descriptors by calling | 254 | file descriptors by calling |
249 | .Fn tls_connect_fds . | 255 | .Fn tls_connect_fds . |
256 | Calling | ||
257 | .Fn tls_connect_cbs | ||
258 | allows specifying read and write callback functions to handle data transfer. The specified cb_arg parameter is passed back to the functions, and can contain a pointer to any caller-specified data. | ||
250 | .Pp | 259 | .Pp |
251 | A server can accept a new client connection by calling | 260 | A server can accept a new client connection by calling |
252 | .Fn tls_accept_socket | 261 | .Fn tls_accept_socket |
@@ -254,6 +263,9 @@ on an already established socket connection. | |||
254 | Alternatively, a new client connection can be accepted over a pair of existing | 263 | Alternatively, a new client connection can be accepted over a pair of existing |
255 | file descriptors by calling | 264 | file descriptors by calling |
256 | .Fn tls_accept_fds . | 265 | .Fn tls_accept_fds . |
266 | Calling | ||
267 | .Fn tls_accept_cbs | ||
268 | allows specifying read and write callback functions to handle data transfer. The specified cb_arg parameter is passed back to the functions, and can contain a pointer to any caller-specified data. | ||
257 | .Pp | 269 | .Pp |
258 | The TLS handshake can be completed by calling | 270 | The TLS handshake can be completed by calling |
259 | .Fn tls_handshake . | 271 | .Fn tls_handshake . |
diff --git a/src/lib/libtls/tls_internal.h b/src/lib/libtls/tls_internal.h index c7bf50af83..c272038055 100644 --- a/src/lib/libtls/tls_internal.h +++ b/src/lib/libtls/tls_internal.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls_internal.h,v 1.42 2016/08/22 17:12:35 jsing Exp $ */ | 1 | /* $OpenBSD: tls_internal.h,v 1.43 2016/09/04 12:26:43 bcook Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> | 3 | * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> |
4 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 4 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
@@ -117,6 +117,10 @@ struct tls { | |||
117 | X509 *ssl_peer_cert; | 117 | X509 *ssl_peer_cert; |
118 | 118 | ||
119 | struct tls_conninfo *conninfo; | 119 | struct tls_conninfo *conninfo; |
120 | |||
121 | tls_read_cb read_cb; | ||
122 | tls_write_cb write_cb; | ||
123 | void *cb_arg; | ||
120 | }; | 124 | }; |
121 | 125 | ||
122 | struct tls_sni_ctx *tls_sni_ctx_new(void); | 126 | struct tls_sni_ctx *tls_sni_ctx_new(void); |
@@ -140,6 +144,9 @@ int tls_config_load_file(struct tls_error *error, const char *filetype, | |||
140 | const char *filename, char **buf, size_t *len); | 144 | const char *filename, char **buf, size_t *len); |
141 | int tls_host_port(const char *hostport, char **host, char **port); | 145 | int tls_host_port(const char *hostport, char **host, char **port); |
142 | 146 | ||
147 | int tls_set_cbs(struct tls *ctx, | ||
148 | tls_read_cb read_cb, tls_write_cb write_cb, void *cb_arg); | ||
149 | |||
143 | int tls_error_set(struct tls_error *error, const char *fmt, ...) | 150 | int tls_error_set(struct tls_error *error, const char *fmt, ...) |
144 | __attribute__((__format__ (printf, 2, 3))) | 151 | __attribute__((__format__ (printf, 2, 3))) |
145 | __attribute__((__nonnull__ (2))); | 152 | __attribute__((__nonnull__ (2))); |
diff --git a/src/lib/libtls/tls_server.c b/src/lib/libtls/tls_server.c index 044678c705..01f9ed3b7f 100644 --- a/src/lib/libtls/tls_server.c +++ b/src/lib/libtls/tls_server.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls_server.c,v 1.25 2016/08/22 14:51:37 jsing Exp $ */ | 1 | /* $OpenBSD: tls_server.c,v 1.26 2016/09/04 12:26:43 bcook Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -279,14 +279,8 @@ tls_configure_server(struct tls *ctx) | |||
279 | return (-1); | 279 | return (-1); |
280 | } | 280 | } |
281 | 281 | ||
282 | int | 282 | static struct tls * |
283 | tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket) | 283 | accept_common(struct tls *ctx) |
284 | { | ||
285 | return (tls_accept_fds(ctx, cctx, socket, socket)); | ||
286 | } | ||
287 | |||
288 | int | ||
289 | tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write) | ||
290 | { | 284 | { |
291 | struct tls *conn_ctx = NULL; | 285 | struct tls *conn_ctx = NULL; |
292 | 286 | ||
@@ -304,10 +298,34 @@ tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write) | |||
304 | tls_set_errorx(ctx, "ssl failure"); | 298 | tls_set_errorx(ctx, "ssl failure"); |
305 | goto err; | 299 | goto err; |
306 | } | 300 | } |
301 | |||
307 | if (SSL_set_app_data(conn_ctx->ssl_conn, conn_ctx) != 1) { | 302 | if (SSL_set_app_data(conn_ctx->ssl_conn, conn_ctx) != 1) { |
308 | tls_set_errorx(ctx, "ssl application data failure"); | 303 | tls_set_errorx(ctx, "ssl application data failure"); |
309 | goto err; | 304 | goto err; |
310 | } | 305 | } |
306 | |||
307 | return conn_ctx; | ||
308 | |||
309 | err: | ||
310 | tls_free(conn_ctx); | ||
311 | |||
312 | return (NULL); | ||
313 | } | ||
314 | |||
315 | int | ||
316 | tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket) | ||
317 | { | ||
318 | return (tls_accept_fds(ctx, cctx, socket, socket)); | ||
319 | } | ||
320 | |||
321 | int | ||
322 | tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write) | ||
323 | { | ||
324 | struct tls *conn_ctx; | ||
325 | |||
326 | if ((conn_ctx = accept_common(ctx)) == NULL) | ||
327 | goto err; | ||
328 | |||
311 | if (SSL_set_rfd(conn_ctx->ssl_conn, fd_read) != 1 || | 329 | if (SSL_set_rfd(conn_ctx->ssl_conn, fd_read) != 1 || |
312 | SSL_set_wfd(conn_ctx->ssl_conn, fd_write) != 1) { | 330 | SSL_set_wfd(conn_ctx->ssl_conn, fd_write) != 1) { |
313 | tls_set_errorx(ctx, "ssl file descriptor failure"); | 331 | tls_set_errorx(ctx, "ssl file descriptor failure"); |
@@ -317,10 +335,32 @@ tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write) | |||
317 | *cctx = conn_ctx; | 335 | *cctx = conn_ctx; |
318 | 336 | ||
319 | return (0); | 337 | return (0); |
320 | |||
321 | err: | 338 | err: |
322 | tls_free(conn_ctx); | 339 | tls_free(conn_ctx); |
340 | *cctx = NULL; | ||
341 | |||
342 | return (-1); | ||
343 | } | ||
344 | |||
345 | int | ||
346 | tls_accept_cbs(struct tls *ctx, struct tls **cctx, | ||
347 | tls_read_cb read_cb, tls_write_cb write_cb, void *cb_arg) | ||
348 | { | ||
349 | struct tls *conn_ctx; | ||
350 | |||
351 | if ((conn_ctx = accept_common(ctx)) == NULL) | ||
352 | goto err; | ||
353 | |||
354 | if (tls_set_cbs(ctx, read_cb, write_cb, cb_arg) != 0) { | ||
355 | tls_set_errorx(ctx, "callback registration failure"); | ||
356 | goto err; | ||
357 | } | ||
358 | |||
359 | *cctx = conn_ctx; | ||
323 | 360 | ||
361 | return (0); | ||
362 | err: | ||
363 | tls_free(conn_ctx); | ||
324 | *cctx = NULL; | 364 | *cctx = NULL; |
325 | 365 | ||
326 | return (-1); | 366 | return (-1); |
diff --git a/src/lib/libtls/tls_verify.c b/src/lib/libtls/tls_verify.c index 9e73750a6e..e1073e863a 100644 --- a/src/lib/libtls/tls_verify.c +++ b/src/lib/libtls/tls_verify.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls_verify.c,v 1.16 2016/08/02 07:47:11 jsing Exp $ */ | 1 | /* $OpenBSD: tls_verify.c,v 1.17 2016/09/04 12:26:43 bcook Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> | 3 | * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> |
4 | * | 4 | * |
@@ -24,6 +24,7 @@ | |||
24 | 24 | ||
25 | #include <openssl/x509v3.h> | 25 | #include <openssl/x509v3.h> |
26 | 26 | ||
27 | #include <tls.h> | ||
27 | #include "tls_internal.h" | 28 | #include "tls_internal.h" |
28 | 29 | ||
29 | static int tls_match_name(const char *cert_name, const char *name); | 30 | static int tls_match_name(const char *cert_name, const char *name); |