diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2016-08-01 17:32:20 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2016-08-01 17:32:20 +0000 |
commit | c58c15f641989e12fbc789bcbc8280cc402c9f95 (patch) | |
tree | 1b7856af2f35382d71ef80c1dfc359786402cf24 /lib/libtls | |
parent | 95e03c1f4c98327a57bd4139e478395c180578e4 (diff) |
Add ALPN support to libtls.
ok beck@ doug@
Diffstat (limited to 'lib/libtls')
-rw-r--r-- | lib/libtls/tls.c | 10 | ||||
-rw-r--r-- | lib/libtls/tls.h | 6 | ||||
-rw-r--r-- | lib/libtls/tls_config.c | 70 | ||||
-rw-r--r-- | lib/libtls/tls_conninfo.c | 35 | ||||
-rw-r--r-- | lib/libtls/tls_init.3 | 36 | ||||
-rw-r--r-- | lib/libtls/tls_internal.h | 6 | ||||
-rw-r--r-- | lib/libtls/tls_server.c | 20 |
7 files changed, 168 insertions, 15 deletions
diff --git a/lib/libtls/tls.c b/lib/libtls/tls.c index 857b8d0811b..2584ceb88b4 100644 --- a/lib/libtls/tls.c +++ b/lib/libtls/tls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tls.c,v 1.41 2016/07/07 14:09:03 jsing Exp $ */ +/* $OpenBSD: tls.c,v 1.42 2016/08/01 17:32:19 jsing Exp $ */ /* * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> * @@ -310,6 +310,14 @@ tls_configure_ssl(struct tls *ctx) if ((ctx->config->protocols & TLS_PROTOCOL_TLSv1_2) == 0) SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1_2); + if (ctx->config->alpn != NULL) { + if (SSL_CTX_set_alpn_protos(ctx->ssl_ctx, ctx->config->alpn, + ctx->config->alpn_len) != 0) { + tls_set_errorx(ctx, "failed to set alpn"); + goto err; + } + } + if (ctx->config->ciphers != NULL) { if (SSL_CTX_set_cipher_list(ctx->ssl_ctx, ctx->config->ciphers) != 1) { diff --git a/lib/libtls/tls.h b/lib/libtls/tls.h index 75c46c1a1a8..a29030d8d93 100644 --- a/lib/libtls/tls.h +++ b/lib/libtls/tls.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tls.h,v 1.29 2016/05/27 14:21:24 jsing Exp $ */ +/* $OpenBSD: tls.h,v 1.30 2016/08/01 17:32:19 jsing Exp $ */ /* * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> * @@ -52,6 +52,7 @@ const char *tls_error(struct tls *_ctx); struct tls_config *tls_config_new(void); void tls_config_free(struct tls_config *_config); +int tls_config_set_alpn(struct tls_config *_config, const char *_alpn); int tls_config_set_ca_file(struct tls_config *_config, const char *_ca_file); int tls_config_set_ca_path(struct tls_config *_config, const char *_ca_path); int tls_config_set_ca_mem(struct tls_config *_config, const uint8_t *_ca, @@ -116,8 +117,9 @@ const char *tls_peer_cert_subject(struct tls *_ctx); time_t tls_peer_cert_notbefore(struct tls *_ctx); time_t tls_peer_cert_notafter(struct tls *_ctx); -const char *tls_conn_version(struct tls *_ctx); +const char *tls_conn_alpn_selected(struct tls *_ctx); const char *tls_conn_cipher(struct tls *_ctx); +const char *tls_conn_version(struct tls *_ctx); uint8_t *tls_load_file(const char *_file, size_t *_len, char *_password); diff --git a/lib/libtls/tls_config.c b/lib/libtls/tls_config.c index 43f06b00633..640a69e7831 100644 --- a/lib/libtls/tls_config.c +++ b/lib/libtls/tls_config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tls_config.c,v 1.22 2016/07/13 16:30:48 jsing Exp $ */ +/* $OpenBSD: tls_config.c,v 1.23 2016/08/01 17:32:19 jsing Exp $ */ /* * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> * @@ -166,6 +166,7 @@ tls_config_free(struct tls_config *config) free(config->error.msg); + free(config->alpn); free((char *)config->ca_file); free((char *)config->ca_mem); free((char *)config->ca_path); @@ -249,6 +250,73 @@ tls_config_parse_protocols(uint32_t *protocols, const char *protostr) return (0); } +static int +tls_config_parse_alpn(struct tls_config *config, const char *alpn, + char **alpn_data, size_t *alpn_len) +{ + size_t buf_len, i, len; + char *buf = NULL; + char *s = NULL; + char *p, *q; + + free(*alpn_data); + *alpn_data = NULL; + *alpn_len = 0; + + if ((buf_len = strlen(alpn) + 1) > 65535) { + tls_config_set_errorx(config, "alpn too large"); + goto err; + } + + if ((buf = malloc(buf_len)) == NULL) { + tls_config_set_errorx(config, "out of memory"); + goto err; + } + + if ((s = strdup(alpn)) == NULL) { + tls_config_set_errorx(config, "out of memory"); + goto err; + } + + i = 0; + q = s; + while ((p = strsep(&q, ",")) != NULL) { + if ((len = strlen(p)) == 0) { + tls_config_set_errorx(config, + "alpn protocol with zero length"); + goto err; + } + if (len > 255) { + tls_config_set_errorx(config, + "alpn protocol too long"); + goto err; + } + buf[i++] = len & 0xff; + memcpy(&buf[i], p, len); + i += len; + } + + free(s); + + *alpn_data = buf; + *alpn_len = buf_len; + + return (0); + + err: + free(buf); + free(s); + + return (-1); +} + +int +tls_config_set_alpn(struct tls_config *config, const char *alpn) +{ + return tls_config_parse_alpn(config, alpn, &config->alpn, + &config->alpn_len); +} + int tls_config_set_ca_file(struct tls_config *config, const char *ca_file) { diff --git a/lib/libtls/tls_conninfo.c b/lib/libtls/tls_conninfo.c index 1e134bfe598..93526fceeb1 100644 --- a/lib/libtls/tls_conninfo.c +++ b/lib/libtls/tls_conninfo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tls_conninfo.c,v 1.5 2015/10/07 23:33:38 beck Exp $ */ +/* $OpenBSD: tls_conninfo.c,v 1.6 2016/08/01 17:32:19 jsing Exp $ */ /* * Copyright (c) 2015 Joel Sing <jsing@openbsd.org> * Copyright (c) 2015 Bob Beck <beck@openbsd.org> @@ -150,6 +150,26 @@ tls_get_peer_cert_times(struct tls *ctx, time_t *notbefore, time_t *notafter) return (rv); } +static int +tls_conninfo_alpn_proto(struct tls *ctx) +{ + const unsigned char *p; + unsigned int len; + + free(ctx->conninfo->alpn); + ctx->conninfo->alpn = NULL; + + SSL_get0_alpn_selected(ctx->ssl_conn, &p, &len); + if (len > 0) { + if ((ctx->conninfo->alpn = malloc(len + 1)) == NULL) + return (-1); + memcpy(ctx->conninfo->alpn, p, len); + ctx->conninfo->alpn[len] = '\0'; + } + + return (0); +} + int tls_get_conninfo(struct tls *ctx) { const char * tmp; @@ -175,6 +195,9 @@ tls_get_conninfo(struct tls *ctx) { ctx->conninfo->cipher = strdup(tmp); if (ctx->conninfo->cipher == NULL) goto err; + if (tls_conninfo_alpn_proto(ctx) == -1) + goto err; + return (0); err: tls_free_conninfo(ctx->conninfo); @@ -184,6 +207,8 @@ err: void tls_free_conninfo(struct tls_conninfo *conninfo) { if (conninfo != NULL) { + free(conninfo->alpn); + conninfo->alpn = NULL; free(conninfo->hash); conninfo->hash = NULL; free(conninfo->subject); @@ -198,6 +223,14 @@ tls_free_conninfo(struct tls_conninfo *conninfo) { } const char * +tls_conn_alpn_selected(struct tls *ctx) +{ + if (ctx->conninfo == NULL) + return (NULL); + return (ctx->conninfo->alpn); +} + +const char * tls_conn_cipher(struct tls *ctx) { if (ctx->conninfo == NULL) diff --git a/lib/libtls/tls_init.3 b/lib/libtls/tls_init.3 index b4c6a7cc3a5..0969c093a1b 100644 --- a/lib/libtls/tls_init.3 +++ b/lib/libtls/tls_init.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tls_init.3,v 1.62 2016/07/13 16:30:48 jsing Exp $ +.\" $OpenBSD: tls_init.3,v 1.63 2016/08/01 17:32:19 jsing Exp $ .\" .\" Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: July 13 2016 $ +.Dd $Mdocdate: August 1 2016 $ .Dt TLS_INIT 3 .Os .Sh NAME @@ -24,6 +24,7 @@ .Nm tls_config_new , .Nm tls_config_free , .Nm tls_config_parse_protocols , +.Nm tls_config_set_alpn , .Nm tls_config_set_ca_file , .Nm tls_config_set_ca_path , .Nm tls_config_set_ca_mem , @@ -54,8 +55,9 @@ .Nm tls_peer_cert_hash , .Nm tls_peer_cert_notbefore , .Nm tls_peer_cert_notafter , -.Nm tls_conn_version , +.Nm tls_conn_alpn_selected , .Nm tls_conn_cipher , +.Nm tls_conn_version , .Nm tls_load_file , .Nm tls_client , .Nm tls_server , @@ -88,6 +90,8 @@ .Ft "int" .Fn tls_config_parse_protocols "uint32_t *protocols" "const char *protostr" .Ft "int" +.Fn tls_config_set_alpn "struct tls_config *config" "const char *alpn" +.Ft "int" .Fn tls_config_set_ca_file "struct tls_config *config" "const char *ca_file" .Ft "int" .Fn tls_config_set_ca_path "struct tls_config *config" "const char *ca_path" @@ -148,9 +152,11 @@ .Ft "time_t" .Fn tls_peer_cert_notafter "struct tls *ctx" .Ft "const char *" -.Fn tls_conn_version "struct tls *ctx" +.Fn tls_conn_alpn_selected "struct tls *ctx" .Ft "const char *" .Fn tls_conn_cipher "struct tls *ctx" +.Ft "const char *" +.Fn tls_conn_version "struct tls *ctx" .Ft "uint8_t *" .Fn tls_load_file "const char *file" "size_t *len" "char *password" .Ft "struct tls *" @@ -295,6 +301,11 @@ The following functions modify a configuration by setting parameters. Configuration options may apply to only clients or only servers or both. .Bl -bullet -offset four .It +.Fn tls_config_set_alpn +sets the ALPN protocols that are supported. +The alpn string is a comma separated list of protocols, in order of preference. +.Em (Client and Server) +.It .Fn tls_config_set_ca_file sets the filename used to load a file containing the root certificates. @@ -480,13 +491,14 @@ the peer certificate from will only succeed after the handshake is complete. .Em (Server and client) .It -.Fn tls_conn_version -returns a string -corresponding to a TLS version negotiated with the peer +.Fn tls_conn_alpn_selected +returns a string that specifies the ALPN protocol selected for use with the peer connected to .Ar ctx . -.Fn tls_conn_version +If no protocol was selected then NULL is returned. +.Fn tls_conn_alpn_selected will only succeed after the handshake is complete. +.Em (Server and Client) .It .Fn tls_conn_cipher returns a string @@ -497,6 +509,14 @@ connected to will only succeed after the handshake is complete. .Em (Server and client) .It +.Fn tls_conn_version +returns a string +corresponding to a TLS version negotiated with the peer +connected to +.Ar ctx . +.Fn tls_conn_version +will only succeed after the handshake is complete. +.It .Fn tls_load_file loads a certificate or key from disk into memory to be loaded with .Fn tls_config_set_ca_mem , diff --git a/lib/libtls/tls_internal.h b/lib/libtls/tls_internal.h index 6c56e6fb848..17358429c7e 100644 --- a/lib/libtls/tls_internal.h +++ b/lib/libtls/tls_internal.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tls_internal.h,v 1.32 2016/07/13 16:30:48 jsing Exp $ */ +/* $OpenBSD: tls_internal.h,v 1.33 2016/08/01 17:32:19 jsing Exp $ */ /* * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> @@ -55,6 +55,8 @@ struct tls_keypair { struct tls_config { struct tls_error error; + char *alpn; + size_t alpn_len; const char *ca_file; const char *ca_path; char *ca_mem; @@ -73,6 +75,7 @@ struct tls_config { }; struct tls_conninfo { + char *alpn; char *issuer; char *subject; char *hash; @@ -104,6 +107,7 @@ struct tls { SSL *ssl_conn; SSL_CTX *ssl_ctx; X509 *ssl_peer_cert; + struct tls_conninfo *conninfo; }; diff --git a/lib/libtls/tls_server.c b/lib/libtls/tls_server.c index 1d94c99bc01..f13c9db083f 100644 --- a/lib/libtls/tls_server.c +++ b/lib/libtls/tls_server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tls_server.c,v 1.19 2016/04/28 17:05:59 jsing Exp $ */ +/* $OpenBSD: tls_server.c,v 1.20 2016/08/01 17:32:19 jsing Exp $ */ /* * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> * @@ -48,6 +48,20 @@ tls_server_conn(struct tls *ctx) return (conn_ctx); } +static int +tls_server_alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, + const unsigned char *in, unsigned int inlen, void *arg) +{ + struct tls *ctx = arg; + + if (SSL_select_next_proto((unsigned char**)out, outlen, + ctx->config->alpn, ctx->config->alpn_len, in, inlen) == + OPENSSL_NPN_NEGOTIATED) + return (SSL_TLSEXT_ERR_OK); + + return (SSL_TLSEXT_ERR_NOACK); +} + int tls_configure_server(struct tls *ctx) { @@ -71,6 +85,10 @@ tls_configure_server(struct tls *ctx) goto err; } + if (ctx->config->alpn != NULL) + SSL_CTX_set_alpn_select_cb(ctx->ssl_ctx, tls_server_alpn_cb, + ctx); + if (ctx->config->dheparams == -1) SSL_CTX_set_dh_auto(ctx->ssl_ctx, 1); else if (ctx->config->dheparams == 1024) |