diff options
Diffstat (limited to 'src/lib/libtls/tls_server.c')
-rw-r--r-- | src/lib/libtls/tls_server.c | 76 |
1 files changed, 46 insertions, 30 deletions
diff --git a/src/lib/libtls/tls_server.c b/src/lib/libtls/tls_server.c index a3cee09596..3dfd29ac19 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.14 2015/09/10 09:10:42 jsing Exp $ */ | 1 | /* $OpenBSD: tls_server.c,v 1.15 2015/09/10 10:14:20 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -110,54 +110,70 @@ tls_configure_server(struct tls *ctx) | |||
110 | } | 110 | } |
111 | 111 | ||
112 | int | 112 | int |
113 | tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket) | ||
114 | { | ||
115 | return (tls_accept_fds(ctx, cctx, socket, socket)); | ||
116 | } | ||
117 | |||
118 | int | ||
113 | tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write) | 119 | tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write) |
114 | { | 120 | { |
115 | struct tls *conn_ctx = *cctx; | 121 | struct tls *conn_ctx = NULL; |
116 | int ret, err; | ||
117 | 122 | ||
118 | if ((ctx->flags & TLS_SERVER) == 0) { | 123 | if ((ctx->flags & TLS_SERVER) == 0) { |
119 | tls_set_errorx(ctx, "not a server context"); | 124 | tls_set_errorx(ctx, "not a server context"); |
120 | goto err; | 125 | goto err; |
121 | } | 126 | } |
122 | 127 | ||
123 | if (conn_ctx == NULL) { | 128 | if ((conn_ctx = tls_server_conn(ctx)) == NULL) { |
124 | if ((conn_ctx = tls_server_conn(ctx)) == NULL) { | 129 | tls_set_errorx(ctx, "connection context failure"); |
125 | tls_set_errorx(ctx, "connection context failure"); | 130 | goto err; |
126 | goto err; | ||
127 | } | ||
128 | *cctx = conn_ctx; | ||
129 | |||
130 | if ((conn_ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) { | ||
131 | tls_set_errorx(ctx, "ssl failure"); | ||
132 | goto err; | ||
133 | } | ||
134 | if (SSL_set_app_data(conn_ctx->ssl_conn, conn_ctx) != 1) { | ||
135 | tls_set_errorx(ctx, "ssl application data failure"); | ||
136 | goto err; | ||
137 | } | ||
138 | if (SSL_set_rfd(conn_ctx->ssl_conn, fd_read) != 1 || | ||
139 | SSL_set_wfd(conn_ctx->ssl_conn, fd_write) != 1) { | ||
140 | tls_set_errorx(ctx, "ssl file descriptor failure"); | ||
141 | goto err; | ||
142 | } | ||
143 | } | 131 | } |
144 | 132 | ||
145 | if ((ret = SSL_accept(conn_ctx->ssl_conn)) != 1) { | 133 | if ((conn_ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) { |
146 | err = tls_ssl_error(ctx, conn_ctx->ssl_conn, ret, "accept"); | 134 | tls_set_errorx(ctx, "ssl failure"); |
147 | if (err == TLS_READ_AGAIN || err == TLS_WRITE_AGAIN) { | 135 | goto err; |
148 | return (err); | 136 | } |
149 | } | 137 | if (SSL_set_app_data(conn_ctx->ssl_conn, conn_ctx) != 1) { |
138 | tls_set_errorx(ctx, "ssl application data failure"); | ||
139 | goto err; | ||
140 | } | ||
141 | if (SSL_set_rfd(conn_ctx->ssl_conn, fd_read) != 1 || | ||
142 | SSL_set_wfd(conn_ctx->ssl_conn, fd_write) != 1) { | ||
143 | tls_set_errorx(ctx, "ssl file descriptor failure"); | ||
150 | goto err; | 144 | goto err; |
151 | } | 145 | } |
152 | 146 | ||
147 | *cctx = conn_ctx; | ||
148 | |||
153 | return (0); | 149 | return (0); |
154 | 150 | ||
155 | err: | 151 | err: |
152 | tls_free(conn_ctx); | ||
153 | |||
154 | *cctx = NULL; | ||
155 | |||
156 | return (-1); | 156 | return (-1); |
157 | } | 157 | } |
158 | 158 | ||
159 | int | 159 | int |
160 | tls_accept_socket(struct tls *ctx, struct tls **cctx, int socket) | 160 | tls_handshake_server(struct tls *ctx) |
161 | { | 161 | { |
162 | return (tls_accept_fds(ctx, cctx, socket, socket)); | 162 | int ssl_ret; |
163 | int rv = -1; | ||
164 | |||
165 | if ((ctx->flags & TLS_SERVER_CONN) == 0) { | ||
166 | tls_set_errorx(ctx, "not a server connection context"); | ||
167 | goto err; | ||
168 | } | ||
169 | |||
170 | if ((ssl_ret = SSL_accept(ctx->ssl_conn)) != 1) { | ||
171 | rv = tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "handshake"); | ||
172 | goto err; | ||
173 | } | ||
174 | |||
175 | ctx->state |= TLS_HANDSHAKE_COMPLETE; | ||
176 | |||
177 | err: | ||
178 | return (rv); | ||
163 | } | 179 | } |