summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2015-03-31 14:03:38 +0000
committerjsing <>2015-03-31 14:03:38 +0000
commita0cae064c7fb3e6f9795357b0897fc7cfbada29a (patch)
treec543b48853afa3779822fdf734c77e67d886bf12 /src/lib
parente23e95b70f14a01a428a6d966e2cb510a4bf1cd6 (diff)
downloadopenbsd-a0cae064c7fb3e6f9795357b0897fc7cfbada29a.tar.gz
openbsd-a0cae064c7fb3e6f9795357b0897fc7cfbada29a.tar.bz2
openbsd-a0cae064c7fb3e6f9795357b0897fc7cfbada29a.zip
Provide a tls_accept_fds() function, which allows a TLS connection to be
accepted via an existing pair of file descriptors. Based on a diff from Jan Klemkow.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libtls/Makefile3
-rw-r--r--src/lib/libtls/tls.h4
-rw-r--r--src/lib/libtls/tls_init.320
-rw-r--r--src/lib/libtls/tls_server.c21
4 files changed, 39 insertions, 9 deletions
diff --git a/src/lib/libtls/Makefile b/src/lib/libtls/Makefile
index e5434f5071..9d88c15923 100644
--- a/src/lib/libtls/Makefile
+++ b/src/lib/libtls/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.7 2015/02/22 15:09:54 jsing Exp $ 1# $OpenBSD: Makefile,v 1.8 2015/03/31 14:03:38 jsing Exp $
2 2
3CFLAGS+= -Wall -Werror -Wimplicit 3CFLAGS+= -Wall -Werror -Wimplicit
4CFLAGS+= -DLIBRESSL_INTERNAL 4CFLAGS+= -DLIBRESSL_INTERNAL
@@ -49,6 +49,7 @@ MLINKS+=tls_init.3 tls_connect.3
49MLINKS+=tls_init.3 tls_connect_fds.3 49MLINKS+=tls_init.3 tls_connect_fds.3
50MLINKS+=tls_init.3 tls_connect_servername.3 50MLINKS+=tls_init.3 tls_connect_servername.3
51MLINKS+=tls_init.3 tls_connect_socket.3 51MLINKS+=tls_init.3 tls_connect_socket.3
52MLINKS+=tls_init.3 tls_accept_fds.3
52MLINKS+=tls_init.3 tls_accept_socket.3 53MLINKS+=tls_init.3 tls_accept_socket.3
53MLINKS+=tls_init.3 tls_read.3 54MLINKS+=tls_init.3 tls_read.3
54MLINKS+=tls_init.3 tls_write.3 55MLINKS+=tls_init.3 tls_write.3
diff --git a/src/lib/libtls/tls.h b/src/lib/libtls/tls.h
index de58b92963..e9971ade0e 100644
--- a/src/lib/libtls/tls.h
+++ b/src/lib/libtls/tls.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls.h,v 1.11 2015/02/26 10:36:30 jsing Exp $ */ 1/* $OpenBSD: tls.h,v 1.12 2015/03/31 14:03:38 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -72,6 +72,8 @@ int tls_configure(struct tls *_ctx, struct tls_config *_config);
72void tls_reset(struct tls *_ctx); 72void tls_reset(struct tls *_ctx);
73void tls_free(struct tls *_ctx); 73void tls_free(struct tls *_ctx);
74 74
75int tls_accept_fds(struct tls *_ctx, struct tls **_cctx, int _fd_read,
76 int _fd_write);
75int tls_accept_socket(struct tls *_ctx, struct tls **_cctx, int _socket); 77int tls_accept_socket(struct tls *_ctx, struct tls **_cctx, int _socket);
76int tls_connect(struct tls *_ctx, const char *_host, const char *_port); 78int tls_connect(struct tls *_ctx, const char *_host, const char *_port);
77int tls_connect_fds(struct tls *_ctx, int _fd_read, int _fd_write, 79int tls_connect_fds(struct tls *_ctx, int _fd_read, int _fd_write,
diff --git a/src/lib/libtls/tls_init.3 b/src/lib/libtls/tls_init.3
index 3e888115e8..f038f9313a 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.18 2015/02/22 15:09:54 jsing Exp $ 1.\" $OpenBSD: tls_init.3,v 1.19 2015/03/31 14:03:38 jsing Exp $
2.\" 2.\"
3.\" Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> 3.\" Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
4.\" 4.\"
@@ -14,7 +14,7 @@
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\" 16.\"
17.Dd $Mdocdate: February 22 2015 $ 17.Dd $Mdocdate: March 31 2015 $
18.Dt TLS 3 18.Dt TLS 3
19.Os 19.Os
20.Sh NAME 20.Sh NAME
@@ -50,6 +50,7 @@
50.Nm tls_connect_fds , 50.Nm tls_connect_fds ,
51.Nm tls_connect_servername , 51.Nm tls_connect_servername ,
52.Nm tls_connect_socket , 52.Nm tls_connect_socket ,
53.Nm tls_accept_fds ,
53.Nm tls_accept_socket , 54.Nm tls_accept_socket ,
54.Nm tls_read , 55.Nm tls_read ,
55.Nm tls_write 56.Nm tls_write
@@ -120,6 +121,8 @@
120.Ft "int" 121.Ft "int"
121.Fn tls_connect_socket "struct tls *ctx" "int s" "const char *servername" 122.Fn tls_connect_socket "struct tls *ctx" "int s" "const char *servername"
122.Ft "int" 123.Ft "int"
124.Fn tls_accept_fds "struct tls *tls" "struct tls **cctx" "int fd_read" "int fd_write"
125.Ft "int"
123.Fn tls_accept_socket "struct tls *tls" "struct tls **cctx" "int socket" 126.Fn tls_accept_socket "struct tls *tls" "struct tls **cctx" "int socket"
124.Ft "int" 127.Ft "int"
125.Fn tls_read "struct tls *ctx" "void *buf" "size_t buflen" "size_t *outlen" 128.Fn tls_read "struct tls *ctx" "void *buf" "size_t buflen" "size_t *outlen"
@@ -180,6 +183,9 @@ file descriptors by calling
180A server can accept a new client connection by calling 183A server can accept a new client connection by calling
181.Fn tls_accept_socket 184.Fn tls_accept_socket
182on an already established socket connection. 185on an already established socket connection.
186Alternatively, a new client connection can be accepted over a pair of existing
187file descriptors by calling
188.Fn tls_accept_fds .
183.Pp 189.Pp
184Two functions are provided for input and output, 190Two functions are provided for input and output,
185.Fn tls_read 191.Fn tls_read
@@ -357,6 +363,16 @@ connects a client context to a pair of existing file descriptors.
357.Fn tls_connect_socket 363.Fn tls_connect_socket
358connects a client context to an already established socket connection. 364connects a client context to an already established socket connection.
359.It 365.It
366.Fn tls_accept_fds
367creates a new context suitable for reading and writing on an existing pair of
368file descriptors and returns it in
369.Fa *cctx .
370A configured server context should be passed in
371.Fa ctx
372and
373.Fa *cctx
374should be initialized to NULL.
375.It
360.Fn tls_accept_socket 376.Fn tls_accept_socket
361creates a new context suitable for reading and writing on an already 377creates a new context suitable for reading and writing on an already
362established socket connection and returns it in 378established socket connection and returns it in
diff --git a/src/lib/libtls/tls_server.c b/src/lib/libtls/tls_server.c
index cbe064e2f5..55b19e472c 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.6 2015/03/31 12:21:27 jsing Exp $ */ 1/* $OpenBSD: tls_server.c,v 1.7 2015/03/31 14:03:38 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -99,7 +99,7 @@ err:
99} 99}
100 100
101int 101int
102tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket) 102tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write)
103{ 103{
104 struct tls *conn_ctx = *cctx; 104 struct tls *conn_ctx = *cctx;
105 int ret, err; 105 int ret, err;
@@ -116,14 +116,13 @@ tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket)
116 } 116 }
117 *cctx = conn_ctx; 117 *cctx = conn_ctx;
118 118
119 conn_ctx->socket = socket;
120
121 if ((conn_ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) { 119 if ((conn_ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) {
122 tls_set_error(ctx, "ssl failure"); 120 tls_set_error(ctx, "ssl failure");
123 goto err; 121 goto err;
124 } 122 }
125 123
126 if (SSL_set_fd(conn_ctx->ssl_conn, socket) != 1) { 124 if (SSL_set_rfd(conn_ctx->ssl_conn, fd_read) != 1 ||
125 SSL_set_wfd(conn_ctx->ssl_conn, fd_write) != 1) {
127 tls_set_error(ctx, "ssl set fd failure"); 126 tls_set_error(ctx, "ssl set fd failure");
128 goto err; 127 goto err;
129 } 128 }
@@ -143,3 +142,15 @@ tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket)
143err: 142err:
144 return (-1); 143 return (-1);
145} 144}
145
146int
147tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket)
148{
149 int rv;
150
151 rv = tls_accept_fds(ctx, cctx, socket, socket);
152 if (*cctx != NULL)
153 (*cctx)->socket = socket;
154
155 return (rv);
156}