summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2014-11-02 14:45:05 +0000
committerjsing <>2014-11-02 14:45:05 +0000
commitae4a0ba982e7f6609f71539c65c23a5bdfdf446d (patch)
treed9142429aca61b41c92ba09dd825948905416233 /src
parent2ac1fcf6771c75502e194a147db7f1f45d5e41c7 (diff)
downloadopenbsd-ae4a0ba982e7f6609f71539c65c23a5bdfdf446d.tar.gz
openbsd-ae4a0ba982e7f6609f71539c65c23a5bdfdf446d.tar.bz2
openbsd-ae4a0ba982e7f6609f71539c65c23a5bdfdf446d.zip
Add a tls_connect_fds() function that allows a secure connection to be
established using a pair of existing file descriptors. Based on a diff/request from Jan Klemkow. Rides previous libtls rename/library bump. Discussed with tedu@.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libtls/tls.h4
-rw-r--r--src/lib/libtls/tls_client.c19
-rw-r--r--src/lib/libtls/tls_init.317
3 files changed, 34 insertions, 6 deletions
diff --git a/src/lib/libtls/tls.h b/src/lib/libtls/tls.h
index 0fa776e584..21e1d74b35 100644
--- a/src/lib/libtls/tls.h
+++ b/src/lib/libtls/tls.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls.h,v 1.1 2014/10/31 13:46:17 jsing Exp $ */ 1/* $OpenBSD: tls.h,v 1.2 2014/11/02 14:45:05 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -66,6 +66,8 @@ void tls_free(struct tls *ctx);
66 66
67int tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket); 67int tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket);
68int tls_connect(struct tls *ctx, const char *host, const char *port); 68int tls_connect(struct tls *ctx, const char *host, const char *port);
69int tls_connect_fds(struct tls *ctx, int fd_read, int fd_write,
70 const char *hostname);
69int tls_connect_socket(struct tls *ctx, int s, const char *hostname); 71int tls_connect_socket(struct tls *ctx, int s, const char *hostname);
70int tls_read(struct tls *ctx, void *buf, size_t buflen, size_t *outlen); 72int tls_read(struct tls *ctx, void *buf, size_t buflen, size_t *outlen);
71int tls_write(struct tls *ctx, const void *buf, size_t buflen, size_t *outlen); 73int tls_write(struct tls *ctx, const void *buf, size_t buflen, size_t *outlen);
diff --git a/src/lib/libtls/tls_client.c b/src/lib/libtls/tls_client.c
index 853766f87b..a4528b9b87 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.1 2014/10/31 13:46:17 jsing Exp $ */ 1/* $OpenBSD: tls_client.c,v 1.2 2014/11/02 14:45:05 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -123,6 +123,15 @@ err:
123int 123int
124tls_connect_socket(struct tls *ctx, int socket, const char *hostname) 124tls_connect_socket(struct tls *ctx, int socket, const char *hostname)
125{ 125{
126 ctx->socket = socket;
127
128 return tls_connect_fds(ctx, socket, socket, hostname);
129}
130
131int
132tls_connect_fds(struct tls *ctx, int fd_read, int fd_write,
133 const char *hostname)
134{
126 union { struct in_addr ip4; struct in6_addr ip6; } addrbuf; 135 union { struct in_addr ip4; struct in6_addr ip6; } addrbuf;
127 X509 *cert = NULL; 136 X509 *cert = NULL;
128 int ret; 137 int ret;
@@ -132,7 +141,10 @@ tls_connect_socket(struct tls *ctx, int socket, const char *hostname)
132 goto err; 141 goto err;
133 } 142 }
134 143
135 ctx->socket = socket; 144 if (fd_read < 0 || fd_write < 0) {
145 tls_set_error(ctx, "invalid file descriptors");
146 return (-1);
147 }
136 148
137 if ((ctx->ssl_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) { 149 if ((ctx->ssl_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) {
138 tls_set_error(ctx, "ssl context failure"); 150 tls_set_error(ctx, "ssl context failure");
@@ -166,7 +178,8 @@ tls_connect_socket(struct tls *ctx, int socket, const char *hostname)
166 tls_set_error(ctx, "ssl connection failure"); 178 tls_set_error(ctx, "ssl connection failure");
167 goto err; 179 goto err;
168 } 180 }
169 if (SSL_set_fd(ctx->ssl_conn, ctx->socket) != 1) { 181 if (SSL_set_rfd(ctx->ssl_conn, fd_read) != 1 ||
182 SSL_set_wfd(ctx->ssl_conn, fd_write) != 1) {
170 tls_set_error(ctx, "ssl file descriptor failure"); 183 tls_set_error(ctx, "ssl file descriptor failure");
171 goto err; 184 goto err;
172 } 185 }
diff --git a/src/lib/libtls/tls_init.3 b/src/lib/libtls/tls_init.3
index faa9b99539..5873f15686 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.1 2014/10/31 13:46:17 jsing Exp $ 1.\" $OpenBSD: tls_init.3,v 1.2 2014/11/02 14:45:05 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: October 31 2014 $ 17.Dd $Mdocdate: November 2 2014 $
18.Dt TLS 3 18.Dt TLS 3
19.Os 19.Os
20.Sh NAME 20.Sh NAME
@@ -43,6 +43,7 @@
43.Nm tls_close , 43.Nm tls_close ,
44.Nm tls_free , 44.Nm tls_free ,
45.Nm tls_connect , 45.Nm tls_connect ,
46.Nm tls_connect_fds ,
46.Nm tls_connect_socket , 47.Nm tls_connect_socket ,
47.Nm tls_read , 48.Nm tls_read ,
48.Nm tls_write , 49.Nm tls_write ,
@@ -100,6 +101,8 @@
100.Ft "int" 101.Ft "int"
101.Fn tls_connect "struct tls *ctx" "const char *host" "const char *port" 102.Fn tls_connect "struct tls *ctx" "const char *host" "const char *port"
102.Ft "int" 103.Ft "int"
104.Fn tls_connect_fds "struct tls *ctx" "int fd_read" "int fd_write" "const char *hostname"
105.Ft "int"
103.Fn tls_connect_socket "struct tls *ctx" "int s" "const char *hostname" 106.Fn tls_connect_socket "struct tls *ctx" "int s" "const char *hostname"
104.Ft "int" 107.Ft "int"
105.Fn tls_read "struct tls *ctx" "void *buf" "size_t buflen" "size_t *outlen" 108.Fn tls_read "struct tls *ctx" "void *buf" "size_t buflen" "size_t *outlen"
@@ -146,6 +149,9 @@ This function will create a new socket, connect to the specified host and
146port, and then establish a secure connection. 149port, and then establish a secure connection.
147An already existing socket can be upgraded to a secure connection by calling 150An already existing socket can be upgraded to a secure connection by calling
148.Fn tls_connect_socket . 151.Fn tls_connect_socket .
152Alternatively, a secure connection can be established over a pair of existing
153file descriptors by calling
154.Fn tls_connect_fds .
149.Pp 155.Pp
150Two functions are provided for input and output, 156Two functions are provided for input and output,
151.Fn tls_read 157.Fn tls_read
@@ -263,6 +269,10 @@ options.
263.It 269.It
264.Fn tls_close 270.Fn tls_close
265closes a connection after use. 271closes a connection after use.
272If the connection was established using
273.Fn tls_connect_fds ,
274only the TLS layer will be closed and it is the caller's responsibility to close
275the file descriptors.
266.It 276.It
267.Fn tls_free 277.Fn tls_free
268frees a tls context after use. 278frees a tls context after use.
@@ -280,6 +290,9 @@ The
280may be numeric or a service name. 290may be numeric or a service name.
281If it is NULL then a host of the format "hostname:port" is permitted. 291If it is NULL then a host of the format "hostname:port" is permitted.
282.It 292.It
293.Fn tls_connect_fds
294connects a client context to a pair of existing file descriptors.
295.It
283.Fn tls_connect_socket 296.Fn tls_connect_socket
284connects a client context to an already established socket connection. 297connects a client context to an already established socket connection.
285.It 298.It