1 /* $OpenBSD: tls_server.c,v 1.44 2018/03/19 16:34:47 jsing Exp $ */
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
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.
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.
18 #include <sys/socket.h>
20 #include <arpa/inet.h>
22 #include <openssl/ec.h>
23 #include <openssl/err.h>
24 #include <openssl/ssl.h>
27 #include "tls_internal.h"
37 if ((ctx
= tls_new()) == NULL
)
40 ctx
->flags
|= TLS_SERVER
;
46 tls_server_conn(struct tls
*ctx
)
50 if ((conn_ctx
= tls_new()) == NULL
)
53 conn_ctx
->flags
|= TLS_SERVER_CONN
;
55 ctx
->config
->refcount
++;
57 conn_ctx
->config
= ctx
->config
;
58 conn_ctx
->keypair
= ctx
->config
->keypair
;
64 tls_server_alpn_cb(SSL
*ssl
, const unsigned char **out
, unsigned char *outlen
,
65 const unsigned char *in
, unsigned int inlen
, void *arg
)
67 struct tls
*ctx
= arg
;
69 if (SSL_select_next_proto((unsigned char**)out
, outlen
,
70 ctx
->config
->alpn
, ctx
->config
->alpn_len
, in
, inlen
) ==
71 OPENSSL_NPN_NEGOTIATED
)
72 return (SSL_TLSEXT_ERR_OK
);
74 return (SSL_TLSEXT_ERR_NOACK
);
78 tls_servername_cb(SSL
*ssl
, int *al
, void *arg
)
80 struct tls
*ctx
= (struct tls
*)arg
;
81 struct tls_sni_ctx
*sni_ctx
;
82 union tls_addr addrbuf
;
87 if ((conn_ctx
= SSL_get_app_data(ssl
)) == NULL
)
90 if ((name
= SSL_get_servername(ssl
, TLSEXT_NAMETYPE_host_name
)) ==
93 * The servername callback gets called even when there is no
94 * TLS servername extension provided by the client. Sigh!
96 return (SSL_TLSEXT_ERR_NOACK
);
100 * Per RFC 6066 section 3: ensure that name is not an IP literal.
102 * While we should treat this as an error, a number of clients
103 * (Python, Ruby and Safari) are not RFC compliant. To avoid handshake
104 * failures, pretend that we did not receive the extension.
106 if (inet_pton(AF_INET
, name
, &addrbuf
) == 1 ||
107 inet_pton(AF_INET6
, name
, &addrbuf
) == 1)
108 return (SSL_TLSEXT_ERR_NOACK
);
110 free((char *)conn_ctx
->servername
);
111 if ((conn_ctx
->servername
= strdup(name
)) == NULL
)
114 /* Find appropriate SSL context for requested servername. */
115 for (sni_ctx
= ctx
->sni_ctx
; sni_ctx
!= NULL
; sni_ctx
= sni_ctx
->next
) {
116 if (tls_check_name(ctx
, sni_ctx
->ssl_cert
, name
,
120 conn_ctx
->keypair
= sni_ctx
->keypair
;
121 SSL_set_SSL_CTX(conn_ctx
->ssl_conn
, sni_ctx
->ssl_ctx
);
122 return (SSL_TLSEXT_ERR_OK
);
126 /* No match, use the existing context/certificate. */
127 return (SSL_TLSEXT_ERR_OK
);
131 * There is no way to tell libssl that an internal failure occurred.
132 * The only option we have is to return a fatal alert.
134 *al
= TLS1_AD_INTERNAL_ERROR
;
135 return (SSL_TLSEXT_ERR_ALERT_FATAL
);
138 static struct tls_ticket_key
*
139 tls_server_ticket_key(struct tls_config
*config
, unsigned char *keyname
)
141 struct tls_ticket_key
*key
= NULL
;
146 if (config
->ticket_autorekey
== 1) {
147 if (now
- 3 * (config
->session_lifetime
/ 4) >
148 config
->ticket_keys
[0].time
) {
149 if (tls_config_ticket_autorekey(config
) == -1)
153 for (i
= 0; i
< TLS_NUM_TICKETS
; i
++) {
154 struct tls_ticket_key
*tk
= &config
->ticket_keys
[i
];
155 if (now
- config
->session_lifetime
> tk
->time
)
157 if (keyname
== NULL
|| timingsafe_memcmp(keyname
,
158 tk
->key_name
, sizeof(tk
->key_name
)) == 0) {
167 tls_server_ticket_cb(SSL
*ssl
, unsigned char *keyname
, unsigned char *iv
,
168 EVP_CIPHER_CTX
*ctx
, HMAC_CTX
*hctx
, int mode
)
170 struct tls_ticket_key
*key
;
173 if ((tls_ctx
= SSL_get_app_data(ssl
)) == NULL
)
177 /* create new session */
178 key
= tls_server_ticket_key(tls_ctx
->config
, NULL
);
180 tls_set_errorx(tls_ctx
, "no valid ticket key found");
184 memcpy(keyname
, key
->key_name
, sizeof(key
->key_name
));
185 arc4random_buf(iv
, EVP_MAX_IV_LENGTH
);
186 EVP_EncryptInit_ex(ctx
, EVP_aes_256_cbc(), NULL
,
188 HMAC_Init_ex(hctx
, key
->hmac_key
, sizeof(key
->hmac_key
),
192 /* get key by name */
193 key
= tls_server_ticket_key(tls_ctx
->config
, keyname
);
197 EVP_DecryptInit_ex(ctx
, EVP_aes_256_cbc(), NULL
,
199 HMAC_Init_ex(hctx
, key
->hmac_key
, sizeof(key
->hmac_key
),
202 /* time to renew the ticket? is it the primary key? */
203 if (key
!= &tls_ctx
->config
->ticket_keys
[0])
210 tls_configure_server_ssl(struct tls
*ctx
, SSL_CTX
**ssl_ctx
,
211 struct tls_keypair
*keypair
)
213 SSL_CTX_free(*ssl_ctx
);
215 if ((*ssl_ctx
= SSL_CTX_new(SSLv23_server_method())) == NULL
) {
216 tls_set_errorx(ctx
, "ssl context failure");
220 SSL_CTX_set_options(*ssl_ctx
, SSL_OP_NO_CLIENT_RENEGOTIATION
);
222 if (SSL_CTX_set_tlsext_servername_callback(*ssl_ctx
,
223 tls_servername_cb
) != 1) {
224 tls_set_error(ctx
, "failed to set servername callback");
227 if (SSL_CTX_set_tlsext_servername_arg(*ssl_ctx
, ctx
) != 1) {
228 tls_set_error(ctx
, "failed to set servername callback arg");
232 if (tls_configure_ssl(ctx
, *ssl_ctx
) != 0)
234 if (tls_configure_ssl_keypair(ctx
, *ssl_ctx
, keypair
, 1) != 0)
236 if (ctx
->config
->verify_client
!= 0) {
237 int verify
= SSL_VERIFY_PEER
;
238 if (ctx
->config
->verify_client
== 1)
239 verify
|= SSL_VERIFY_FAIL_IF_NO_PEER_CERT
;
240 if (tls_configure_ssl_verify(ctx
, *ssl_ctx
, verify
) == -1)
244 if (ctx
->config
->alpn
!= NULL
)
245 SSL_CTX_set_alpn_select_cb(*ssl_ctx
, tls_server_alpn_cb
,
248 if (ctx
->config
->dheparams
== -1)
249 SSL_CTX_set_dh_auto(*ssl_ctx
, 1);
250 else if (ctx
->config
->dheparams
== 1024)
251 SSL_CTX_set_dh_auto(*ssl_ctx
, 2);
253 if (ctx
->config
->ecdhecurves
!= NULL
) {
254 SSL_CTX_set_ecdh_auto(*ssl_ctx
, 1);
255 if (SSL_CTX_set1_groups(*ssl_ctx
, ctx
->config
->ecdhecurves
,
256 ctx
->config
->ecdhecurves_len
) != 1) {
257 tls_set_errorx(ctx
, "failed to set ecdhe curves");
262 if (ctx
->config
->ciphers_server
== 1)
263 SSL_CTX_set_options(*ssl_ctx
, SSL_OP_CIPHER_SERVER_PREFERENCE
);
265 if (SSL_CTX_set_tlsext_status_cb(*ssl_ctx
, tls_ocsp_stapling_cb
) != 1) {
266 tls_set_errorx(ctx
, "failed to add OCSP stapling callback");
270 if (ctx
->config
->session_lifetime
> 0) {
271 /* set the session lifetime and enable tickets */
272 SSL_CTX_set_timeout(*ssl_ctx
, ctx
->config
->session_lifetime
);
273 SSL_CTX_clear_options(*ssl_ctx
, SSL_OP_NO_TICKET
);
274 if (!SSL_CTX_set_tlsext_ticket_key_cb(*ssl_ctx
,
275 tls_server_ticket_cb
)) {
277 "failed to set the TLS ticket callback");
282 if (SSL_CTX_set_session_id_context(*ssl_ctx
, ctx
->config
->session_id
,
283 sizeof(ctx
->config
->session_id
)) != 1) {
284 tls_set_error(ctx
, "failed to set session id context");
291 SSL_CTX_free(*ssl_ctx
);
298 tls_configure_server_sni(struct tls
*ctx
)
300 struct tls_sni_ctx
**sni_ctx
;
301 struct tls_keypair
*kp
;
303 if (ctx
->config
->keypair
->next
== NULL
)
306 /* Set up additional SSL contexts for SNI. */
307 sni_ctx
= &ctx
->sni_ctx
;
308 for (kp
= ctx
->config
->keypair
->next
; kp
!= NULL
; kp
= kp
->next
) {
309 if ((*sni_ctx
= tls_sni_ctx_new()) == NULL
) {
310 tls_set_errorx(ctx
, "out of memory");
313 (*sni_ctx
)->keypair
= kp
;
314 if (tls_configure_server_ssl(ctx
, &(*sni_ctx
)->ssl_ctx
, kp
) == -1)
316 if (tls_keypair_load_cert(kp
, &ctx
->error
,
317 &(*sni_ctx
)->ssl_cert
) == -1)
319 sni_ctx
= &(*sni_ctx
)->next
;
329 tls_configure_server(struct tls
*ctx
)
331 if (tls_configure_server_ssl(ctx
, &ctx
->ssl_ctx
,
332 ctx
->config
->keypair
) == -1)
334 if (tls_configure_server_sni(ctx
) == -1)
344 tls_accept_common(struct tls
*ctx
)
346 struct tls
*conn_ctx
= NULL
;
348 if ((ctx
->flags
& TLS_SERVER
) == 0) {
349 tls_set_errorx(ctx
, "not a server context");
353 if ((conn_ctx
= tls_server_conn(ctx
)) == NULL
) {
354 tls_set_errorx(ctx
, "connection context failure");
358 if ((conn_ctx
->ssl_conn
= SSL_new(ctx
->ssl_ctx
)) == NULL
) {
359 tls_set_errorx(ctx
, "ssl failure");
363 if (SSL_set_app_data(conn_ctx
->ssl_conn
, conn_ctx
) != 1) {
364 tls_set_errorx(ctx
, "ssl application data failure");
377 tls_accept_socket(struct tls
*ctx
, struct tls
**cctx
, int s
)
379 return (tls_accept_fds(ctx
, cctx
, s
, s
));
383 tls_accept_fds(struct tls
*ctx
, struct tls
**cctx
, int fd_read
, int fd_write
)
385 struct tls
*conn_ctx
;
387 if ((conn_ctx
= tls_accept_common(ctx
)) == NULL
)
390 if (SSL_set_rfd(conn_ctx
->ssl_conn
, fd_read
) != 1 ||
391 SSL_set_wfd(conn_ctx
->ssl_conn
, fd_write
) != 1) {
392 tls_set_errorx(ctx
, "ssl file descriptor failure");
407 tls_accept_cbs(struct tls
*ctx
, struct tls
**cctx
,
408 tls_read_cb read_cb
, tls_write_cb write_cb
, void *cb_arg
)
410 struct tls
*conn_ctx
;
412 if ((conn_ctx
= tls_accept_common(ctx
)) == NULL
)
415 if (tls_set_cbs(conn_ctx
, read_cb
, write_cb
, cb_arg
) != 0)
429 tls_handshake_server(struct tls
*ctx
)
434 if ((ctx
->flags
& TLS_SERVER_CONN
) == 0) {
435 tls_set_errorx(ctx
, "not a server connection context");
439 ctx
->state
|= TLS_SSL_NEEDS_SHUTDOWN
;
442 if ((ssl_ret
= SSL_accept(ctx
->ssl_conn
)) != 1) {
443 rv
= tls_ssl_error(ctx
, ctx
->ssl_conn
, ssl_ret
, "handshake");
447 ctx
->state
|= TLS_HANDSHAKE_COMPLETE
;