xref: /openbsd-src/lib/libtls/tls_server.c (revision d4741794dd2f512d997014f8bd85fbb24d935059)
1 /* $OpenBSD: tls_server.c,v 1.32 2017/01/12 16:15:58 jsing Exp $ */
2 /*
3  * Copyright (c) 2014 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 <sys/socket.h>
19 
20 #include <arpa/inet.h>
21 
22 #include <openssl/ec.h>
23 #include <openssl/err.h>
24 #include <openssl/ssl.h>
25 
26 #include <tls.h>
27 #include "tls_internal.h"
28 
29 struct tls *
30 tls_server(void)
31 {
32 	struct tls *ctx;
33 
34 	if ((ctx = tls_new()) == NULL)
35 		return (NULL);
36 
37 	ctx->flags |= TLS_SERVER;
38 
39 	return (ctx);
40 }
41 
42 struct tls *
43 tls_server_conn(struct tls *ctx)
44 {
45 	struct tls *conn_ctx;
46 
47 	if ((conn_ctx = tls_new()) == NULL)
48 		return (NULL);
49 
50 	conn_ctx->flags |= TLS_SERVER_CONN;
51 	conn_ctx->config = ctx->config;
52 
53 	return (conn_ctx);
54 }
55 
56 static int
57 tls_server_alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen,
58     const unsigned char *in, unsigned int inlen, void *arg)
59 {
60 	struct tls *ctx = arg;
61 
62 	if (SSL_select_next_proto((unsigned char**)out, outlen,
63 	    ctx->config->alpn, ctx->config->alpn_len, in, inlen) ==
64 	    OPENSSL_NPN_NEGOTIATED)
65 		return (SSL_TLSEXT_ERR_OK);
66 
67 	return (SSL_TLSEXT_ERR_NOACK);
68 }
69 
70 static int
71 tls_servername_cb(SSL *ssl, int *al, void *arg)
72 {
73 	struct tls *ctx = (struct tls *)arg;
74 	struct tls_sni_ctx *sni_ctx;
75 	union tls_addr addrbuf;
76 	struct tls *conn_ctx;
77 	const char *name;
78 
79 	if ((conn_ctx = SSL_get_app_data(ssl)) == NULL)
80 		goto err;
81 
82 	if ((name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name)) == NULL) {
83 		/*
84 		 * The servername callback gets called even when there is no
85 		 * TLS servername extension provided by the client. Sigh!
86 		 */
87 		return (SSL_TLSEXT_ERR_NOACK);
88 	}
89 
90 	/* Per RFC 6066 section 3: ensure that name is not an IP literal. */
91 	if (inet_pton(AF_INET, name, &addrbuf) == 1 ||
92             inet_pton(AF_INET6, name, &addrbuf) == 1)
93 		goto err;
94 
95 	free((char *)conn_ctx->servername);
96 	if ((conn_ctx->servername = strdup(name)) == NULL)
97 		goto err;
98 
99 	/* Find appropriate SSL context for requested servername. */
100 	for (sni_ctx = ctx->sni_ctx; sni_ctx != NULL; sni_ctx = sni_ctx->next) {
101 		if (tls_check_name(ctx, sni_ctx->ssl_cert, name) == 0) {
102 			SSL_set_SSL_CTX(conn_ctx->ssl_conn, sni_ctx->ssl_ctx);
103 			return (SSL_TLSEXT_ERR_OK);
104 		}
105 	}
106 
107 	/* No match, use the existing context/certificate. */
108 	return (SSL_TLSEXT_ERR_OK);
109 
110  err:
111 	/*
112 	 * There is no way to tell libssl that an internal failure occurred.
113 	 * The only option we have is to return a fatal alert.
114 	 */
115 	*al = TLS1_AD_INTERNAL_ERROR;
116 	return (SSL_TLSEXT_ERR_ALERT_FATAL);
117 }
118 
119 static int
120 tls_keypair_load_cert(struct tls_keypair *keypair, struct tls_error *error,
121     X509 **cert)
122 {
123 	char *errstr = "unknown";
124 	BIO *cert_bio = NULL;
125 	int ssl_err;
126 
127 	X509_free(*cert);
128 	*cert = NULL;
129 
130 	if (keypair->cert_mem == NULL) {
131 		tls_error_set(error, "keypair has no certificate");
132 		goto err;
133 	}
134 	if ((cert_bio = BIO_new_mem_buf(keypair->cert_mem,
135 	    keypair->cert_len)) == NULL) {
136 		tls_error_set(error, "failed to create certificate bio");
137 		goto err;
138 	}
139 	if ((*cert = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL)) == NULL) {
140 		if ((ssl_err = ERR_peek_error()) != 0)
141 		    errstr = ERR_error_string(ssl_err, NULL);
142 		tls_error_set(error, "failed to load certificate: %s", errstr);
143 		goto err;
144 	}
145 
146 	BIO_free(cert_bio);
147 
148 	return (0);
149 
150  err:
151 	BIO_free(cert_bio);
152 
153 	return (-1);
154 }
155 
156 static int
157 tls_configure_server_ssl(struct tls *ctx, SSL_CTX **ssl_ctx,
158     struct tls_keypair *keypair)
159 {
160 	unsigned char sid[SSL_MAX_SSL_SESSION_ID_LENGTH];
161 	EC_KEY *ecdh_key;
162 
163 	SSL_CTX_free(*ssl_ctx);
164 
165 	if ((*ssl_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL) {
166 		tls_set_errorx(ctx, "ssl context failure");
167 		goto err;
168 	}
169 
170 	if (SSL_CTX_set_tlsext_servername_callback(*ssl_ctx,
171 	    tls_servername_cb) != 1) {
172 		tls_set_error(ctx, "failed to set servername callback");
173 		goto err;
174 	}
175 	if (SSL_CTX_set_tlsext_servername_arg(*ssl_ctx, ctx) != 1) {
176 		tls_set_error(ctx, "failed to set servername callback arg");
177 		goto err;
178 	}
179 
180 	if (tls_configure_ssl(ctx, *ssl_ctx) != 0)
181 		goto err;
182 	if (tls_configure_ssl_keypair(ctx, *ssl_ctx, keypair, 1) != 0)
183 		goto err;
184 	if (ctx->config->verify_client != 0) {
185 		int verify = SSL_VERIFY_PEER;
186 		if (ctx->config->verify_client == 1)
187 			verify |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
188 		if (tls_configure_ssl_verify(ctx, *ssl_ctx, verify) == -1)
189 			goto err;
190 	}
191 
192 	if (ctx->config->alpn != NULL)
193 		SSL_CTX_set_alpn_select_cb(*ssl_ctx, tls_server_alpn_cb,
194 		    ctx);
195 
196 	if (ctx->config->dheparams == -1)
197 		SSL_CTX_set_dh_auto(*ssl_ctx, 1);
198 	else if (ctx->config->dheparams == 1024)
199 		SSL_CTX_set_dh_auto(*ssl_ctx, 2);
200 
201 	if (ctx->config->ecdhecurve == -1) {
202 		SSL_CTX_set_ecdh_auto(*ssl_ctx, 1);
203 	} else if (ctx->config->ecdhecurve != NID_undef) {
204 		if ((ecdh_key = EC_KEY_new_by_curve_name(
205 		    ctx->config->ecdhecurve)) == NULL) {
206 			tls_set_errorx(ctx, "failed to set ECDHE curve");
207 			goto err;
208 		}
209 		SSL_CTX_set_options(*ssl_ctx, SSL_OP_SINGLE_ECDH_USE);
210 		SSL_CTX_set_tmp_ecdh(*ssl_ctx, ecdh_key);
211 		EC_KEY_free(ecdh_key);
212 	}
213 
214 	if (ctx->config->ciphers_server == 1)
215 		SSL_CTX_set_options(*ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
216 
217 	if (SSL_CTX_set_tlsext_status_cb(*ssl_ctx, tls_ocsp_stapling_cb) != 1) {
218 		tls_set_errorx(ctx, "failed to add OCSP stapling callback");
219 		goto err;
220 	}
221 
222 	/*
223 	 * Set session ID context to a random value.  We don't support
224 	 * persistent caching of sessions so it is OK to set a temporary
225 	 * session ID context that is valid during run time.
226 	 */
227 	arc4random_buf(sid, sizeof(sid));
228 	if (SSL_CTX_set_session_id_context(*ssl_ctx, sid,
229 	    sizeof(sid)) != 1) {
230 		tls_set_error(ctx, "failed to set session id context");
231 		goto err;
232 	}
233 
234 	return (0);
235 
236   err:
237 	SSL_CTX_free(*ssl_ctx);
238 	*ssl_ctx = NULL;
239 
240 	return (-1);
241 }
242 
243 static int
244 tls_configure_server_sni(struct tls *ctx)
245 {
246 	struct tls_sni_ctx **sni_ctx;
247 	struct tls_keypair *kp;
248 
249 	if (ctx->config->keypair->next == NULL)
250 		return (0);
251 
252 	/* Set up additional SSL contexts for SNI. */
253 	sni_ctx = &ctx->sni_ctx;
254 	for (kp = ctx->config->keypair->next; kp != NULL; kp = kp->next) {
255 		if ((*sni_ctx = tls_sni_ctx_new()) == NULL) {
256 			tls_set_errorx(ctx, "out of memory");
257 			goto err;
258 		}
259 		if (tls_configure_server_ssl(ctx, &(*sni_ctx)->ssl_ctx, kp) == -1)
260 			goto err;
261 		if (tls_keypair_load_cert(kp, &ctx->error,
262 		    &(*sni_ctx)->ssl_cert) == -1)
263 			goto err;
264 		sni_ctx = &(*sni_ctx)->next;
265 	}
266 
267 	return (0);
268 
269  err:
270 	return (-1);
271 }
272 
273 int
274 tls_configure_server(struct tls *ctx)
275 {
276 	if (tls_configure_server_ssl(ctx, &ctx->ssl_ctx,
277 	    ctx->config->keypair) == -1)
278 		goto err;
279 	if (tls_configure_server_sni(ctx) == -1)
280 		goto err;
281 
282 	return (0);
283 
284  err:
285 	return (-1);
286 }
287 
288 static struct tls *
289 tls_accept_common(struct tls *ctx)
290 {
291 	struct tls *conn_ctx = NULL;
292 
293 	if ((ctx->flags & TLS_SERVER) == 0) {
294 		tls_set_errorx(ctx, "not a server context");
295 		goto err;
296 	}
297 
298 	if ((conn_ctx = tls_server_conn(ctx)) == NULL) {
299 		tls_set_errorx(ctx, "connection context failure");
300 		goto err;
301 	}
302 
303 	if ((conn_ctx->ssl_conn = SSL_new(ctx->ssl_ctx)) == NULL) {
304 		tls_set_errorx(ctx, "ssl failure");
305 		goto err;
306 	}
307 
308 	if (SSL_set_app_data(conn_ctx->ssl_conn, conn_ctx) != 1) {
309 		tls_set_errorx(ctx, "ssl application data failure");
310 		goto err;
311 	}
312 
313 	return conn_ctx;
314 
315  err:
316 	tls_free(conn_ctx);
317 
318 	return (NULL);
319 }
320 
321 int
322 tls_accept_socket(struct tls *ctx, struct tls **cctx, int s)
323 {
324 	return (tls_accept_fds(ctx, cctx, s, s));
325 }
326 
327 int
328 tls_accept_fds(struct tls *ctx, struct tls **cctx, int fd_read, int fd_write)
329 {
330 	struct tls *conn_ctx;
331 
332 	if ((conn_ctx = tls_accept_common(ctx)) == NULL)
333 		goto err;
334 
335 	if (SSL_set_rfd(conn_ctx->ssl_conn, fd_read) != 1 ||
336 	    SSL_set_wfd(conn_ctx->ssl_conn, fd_write) != 1) {
337 		tls_set_errorx(ctx, "ssl file descriptor failure");
338 		goto err;
339 	}
340 
341 	*cctx = conn_ctx;
342 
343 	return (0);
344  err:
345 	tls_free(conn_ctx);
346 	*cctx = NULL;
347 
348 	return (-1);
349 }
350 
351 int
352 tls_accept_cbs(struct tls *ctx, struct tls **cctx,
353     tls_read_cb read_cb, tls_write_cb write_cb, void *cb_arg)
354 {
355 	struct tls *conn_ctx;
356 
357 	if ((conn_ctx = tls_accept_common(ctx)) == NULL)
358 		goto err;
359 
360 	if (tls_set_cbs(conn_ctx, read_cb, write_cb, cb_arg) != 0)
361 		goto err;
362 
363 	*cctx = conn_ctx;
364 
365 	return (0);
366  err:
367 	tls_free(conn_ctx);
368 	*cctx = NULL;
369 
370 	return (-1);
371 }
372 
373 int
374 tls_handshake_server(struct tls *ctx)
375 {
376 	int ssl_ret;
377 	int rv = -1;
378 
379 	if ((ctx->flags & TLS_SERVER_CONN) == 0) {
380 		tls_set_errorx(ctx, "not a server connection context");
381 		goto err;
382 	}
383 
384 	ERR_clear_error();
385 	if ((ssl_ret = SSL_accept(ctx->ssl_conn)) != 1) {
386 		rv = tls_ssl_error(ctx, ctx->ssl_conn, ssl_ret, "handshake");
387 		goto err;
388 	}
389 
390 	ctx->state |= TLS_HANDSHAKE_COMPLETE;
391 	rv = 0;
392 
393  err:
394 	return (rv);
395 }
396