summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbeck <>2016-11-05 15:13:26 +0000
committerbeck <>2016-11-05 15:13:26 +0000
commita554fd917ad5e5050665b441a614e66959938ede (patch)
tree539491edf35461b59c4b7f94d33635fed5473983
parente48d894dfa7188c6a08be7d172039bdcbfa8d471 (diff)
downloadopenbsd-a554fd917ad5e5050665b441a614e66959938ede.tar.gz
openbsd-a554fd917ad5e5050665b441a614e66959938ede.tar.bz2
openbsd-a554fd917ad5e5050665b441a614e66959938ede.zip
Add support for server side OCSP stapling to libtls.
Add support for server side OCSP stapling to netcat.
-rw-r--r--src/lib/libtls/Symbols.list2
-rw-r--r--src/lib/libtls/tls.h4
-rw-r--r--src/lib/libtls/tls_config.c16
-rw-r--r--src/lib/libtls/tls_init.318
-rw-r--r--src/lib/libtls/tls_internal.h9
-rw-r--r--src/lib/libtls/tls_ocsp.c34
-rw-r--r--src/lib/libtls/tls_server.c8
-rw-r--r--src/usr.bin/nc/nc.111
-rw-r--r--src/usr.bin/nc/netcat.c12
9 files changed, 98 insertions, 16 deletions
diff --git a/src/lib/libtls/Symbols.list b/src/lib/libtls/Symbols.list
index 9074d5e011..7ed1d58bdc 100644
--- a/src/lib/libtls/Symbols.list
+++ b/src/lib/libtls/Symbols.list
@@ -29,6 +29,8 @@ tls_config_set_key_file
29tls_config_set_key_mem 29tls_config_set_key_mem
30tls_config_set_keypair_file 30tls_config_set_keypair_file
31tls_config_set_keypair_mem 31tls_config_set_keypair_mem
32tls_config_set_ocsp_staple_mem
33tls_config_set_ocsp_staple_file
32tls_config_set_protocols 34tls_config_set_protocols
33tls_config_set_verify_depth 35tls_config_set_verify_depth
34tls_config_verify 36tls_config_verify
diff --git a/src/lib/libtls/tls.h b/src/lib/libtls/tls.h
index 2f998d4561..2f8c721a15 100644
--- a/src/lib/libtls/tls.h
+++ b/src/lib/libtls/tls.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls.h,v 1.40 2016/11/04 05:13:13 beck Exp $ */ 1/* $OpenBSD: tls.h,v 1.41 2016/11/05 15:13:26 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -106,6 +106,8 @@ int tls_config_set_keypair_file(struct tls_config *_config,
106 const char *_cert_file, const char *_key_file); 106 const char *_cert_file, const char *_key_file);
107int tls_config_set_keypair_mem(struct tls_config *_config, const uint8_t *_cert, 107int tls_config_set_keypair_mem(struct tls_config *_config, const uint8_t *_cert,
108 size_t _cert_len, const uint8_t *_key, size_t _key_len); 108 size_t _cert_len, const uint8_t *_key, size_t _key_len);
109int tls_config_set_ocsp_staple_mem(struct tls_config *_config, char *_staple, size_t _len);
110int tls_config_set_ocsp_staple_file(struct tls_config *_config, const char *_staple_file);
109void tls_config_set_protocols(struct tls_config *_config, uint32_t _protocols); 111void tls_config_set_protocols(struct tls_config *_config, uint32_t _protocols);
110void tls_config_set_verify_depth(struct tls_config *_config, int _verify_depth); 112void tls_config_set_verify_depth(struct tls_config *_config, int _verify_depth);
111 113
diff --git a/src/lib/libtls/tls_config.c b/src/lib/libtls/tls_config.c
index 218a4c4e72..3ac674e597 100644
--- a/src/lib/libtls/tls_config.c
+++ b/src/lib/libtls/tls_config.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_config.c,v 1.31 2016/11/04 19:01:04 jsing Exp $ */ 1/* $OpenBSD: tls_config.c,v 1.32 2016/11/05 15:13:26 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -227,6 +227,7 @@ tls_config_free(struct tls_config *config)
227 free((char *)config->ca_mem); 227 free((char *)config->ca_mem);
228 free((char *)config->ca_path); 228 free((char *)config->ca_path);
229 free((char *)config->ciphers); 229 free((char *)config->ciphers);
230 free(config->ocsp_staple);
230 231
231 free(config); 232 free(config);
232} 233}
@@ -641,3 +642,16 @@ tls_config_verify_client_optional(struct tls_config *config)
641{ 642{
642 config->verify_client = 2; 643 config->verify_client = 2;
643} 644}
645
646int
647tls_config_set_ocsp_staple_file(struct tls_config *config, const char *staple_file)
648{
649 return tls_config_load_file(&config->error, "OCSP", staple_file,
650 &config->ocsp_staple, &config->ocsp_staple_len);
651}
652
653int
654tls_config_set_ocsp_staple_mem(struct tls_config *config, char *staple, size_t len)
655{
656 return set_mem(&config->ocsp_staple, &config->ocsp_staple_len, staple, len);
657}
diff --git a/src/lib/libtls/tls_init.3 b/src/lib/libtls/tls_init.3
index 88195deb2e..a6ab619c19 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.77 2016/11/04 05:13:13 beck Exp $ 1.\" $OpenBSD: tls_init.3,v 1.78 2016/11/05 15:13:26 beck 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: November 4 2016 $ 17.Dd $Mdocdate: November 5 2016 $
18.Dt TLS_INIT 3 18.Dt TLS_INIT 3
19.Os 19.Os
20.Sh NAME 20.Sh NAME
@@ -39,6 +39,8 @@
39.Nm tls_config_set_key_mem , 39.Nm tls_config_set_key_mem ,
40.Nm tls_config_set_keypair_file , 40.Nm tls_config_set_keypair_file ,
41.Nm tls_config_set_keypair_mem , 41.Nm tls_config_set_keypair_mem ,
42.Nm tls_config_set_ocsp_staple_mem ,
43.Nm tls_config_set_ocsp_staple_file ,
42.Nm tls_config_set_protocols , 44.Nm tls_config_set_protocols ,
43.Nm tls_config_set_verify_depth , 45.Nm tls_config_set_verify_depth ,
44.Nm tls_config_prefer_ciphers_client , 46.Nm tls_config_prefer_ciphers_client ,
@@ -134,6 +136,10 @@
134.Fn tls_config_set_keypair_file "struct tls_config *config" "const char *cert_file" "const char *key_file" 136.Fn tls_config_set_keypair_file "struct tls_config *config" "const char *cert_file" "const char *key_file"
135.Ft "int" 137.Ft "int"
136.Fn tls_config_set_keypair_mem "struct tls_config *config" "const uint8_t *cert" "size_t cert_len" "const uint8_t *key" "size_t key_len" 138.Fn tls_config_set_keypair_mem "struct tls_config *config" "const uint8_t *cert" "size_t cert_len" "const uint8_t *key" "size_t key_len"
139.Ft "int"
140.Fn tls_config_set_ocsp_staple_mem "struct tls_config *config" "const char *staple" "size_t len"
141.Ft "int"
142.Fn tls_config_set_ocsp_staple_file "struct tls_config *config" "const char *staple_file
137.Ft "void" 143.Ft "void"
138.Fn tls_config_set_protocols "struct tls_config *config" "uint32_t protocols" 144.Fn tls_config_set_protocols "struct tls_config *config" "uint32_t protocols"
139.Ft "void" 145.Ft "void"
@@ -365,6 +371,14 @@ used as an alternative certificate for Server Name Indication (server only).
365adds an additional public certificate and private key from memory, 371adds an additional public certificate and private key from memory,
366used as an alternative certificate for Server Name Indication (server only). 372used as an alternative certificate for Server Name Indication (server only).
367.It 373.It
374.Fn tls_config_set_ocsp_staple_mem
375adds a DER encoded OCSP response to be stapled during the TLS handshake from
376memory.
377.It
378.Fn tls_config_set_ocsp_staple_file
379adds a DER encoded OCSP response to be stapled during the TLS handshake from
380the specified file.
381.It
368.Fn tls_config_set_alpn 382.Fn tls_config_set_alpn
369sets the ALPN protocols that are supported. 383sets the ALPN protocols that are supported.
370The alpn string is a comma separated list of protocols, in order of preference. 384The alpn string is a comma separated list of protocols, in order of preference.
diff --git a/src/lib/libtls/tls_internal.h b/src/lib/libtls/tls_internal.h
index 65b65371b2..1db186a05f 100644
--- a/src/lib/libtls/tls_internal.h
+++ b/src/lib/libtls/tls_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_internal.h,v 1.49 2016/11/05 14:50:05 beck Exp $ */ 1/* $OpenBSD: tls_internal.h,v 1.50 2016/11/05 15:13:26 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org> 3 * Copyright (c) 2014 Jeremie Courreges-Anglas <jca@openbsd.org>
4 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 4 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
@@ -67,6 +67,8 @@ struct tls_config {
67 int ecdhecurve; 67 int ecdhecurve;
68 struct tls_keypair *keypair; 68 struct tls_keypair *keypair;
69 int ocsp_require_stapling; 69 int ocsp_require_stapling;
70 char *ocsp_staple;
71 size_t ocsp_staple_len;
70 uint32_t protocols; 72 uint32_t protocols;
71 int verify_cert; 73 int verify_cert;
72 int verify_client; 74 int verify_client;
@@ -110,10 +112,6 @@ struct tls_ocsp {
110 /* responder location */ 112 /* responder location */
111 char *ocsp_url; 113 char *ocsp_url;
112 114
113 /* request blob */
114 uint8_t *request_data;
115 size_t request_size;
116
117 /* cert data, this struct does not own these */ 115 /* cert data, this struct does not own these */
118 X509 *main_cert; 116 X509 *main_cert;
119 STACK_OF(X509) *extra_certs; 117 STACK_OF(X509) *extra_certs;
@@ -208,6 +206,7 @@ int tls_conninfo_populate(struct tls *ctx);
208void tls_conninfo_free(struct tls_conninfo *conninfo); 206void tls_conninfo_free(struct tls_conninfo *conninfo);
209 207
210int tls_ocsp_verify_cb(SSL *ssl, void *arg); 208int tls_ocsp_verify_cb(SSL *ssl, void *arg);
209int tls_ocsp_stapling_cb(SSL *ssl, void *arg);
211void tls_ocsp_free(struct tls_ocsp *ctx); 210void tls_ocsp_free(struct tls_ocsp *ctx);
212struct tls_ocsp *tls_ocsp_setup_from_peer(struct tls *ctx); 211struct tls_ocsp *tls_ocsp_setup_from_peer(struct tls *ctx);
213 212
diff --git a/src/lib/libtls/tls_ocsp.c b/src/lib/libtls/tls_ocsp.c
index 2da88f4281..9ed60a2aa9 100644
--- a/src/lib/libtls/tls_ocsp.c
+++ b/src/lib/libtls/tls_ocsp.c
@@ -50,8 +50,6 @@ tls_ocsp_free(struct tls_ocsp *ocsp)
50 ocsp->ocsp_result = NULL; 50 ocsp->ocsp_result = NULL;
51 free(ocsp->ocsp_url); 51 free(ocsp->ocsp_url);
52 ocsp->ocsp_url = NULL; 52 ocsp->ocsp_url = NULL;
53 free(ocsp->request_data);
54 ocsp->request_data = NULL;
55 free(ocsp); 53 free(ocsp);
56} 54}
57 55
@@ -322,6 +320,38 @@ tls_ocsp_verify_cb(SSL *ssl, void *arg)
322 return (res == 0) ? 1 : 0; 320 return (res == 0) ? 1 : 0;
323} 321}
324 322
323
324/* Staple the OCSP information in ctx->ocsp to the server handshake. */
325int
326tls_ocsp_stapling_cb(SSL *ssl, void *arg)
327{
328 struct tls *ctx;
329 unsigned char *ocsp_staple = NULL;
330 int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
331
332 if ((ctx = SSL_get_app_data(ssl)) == NULL)
333 goto err;
334
335 if (ctx->config->ocsp_staple == NULL ||
336 ctx->config->ocsp_staple_len == 0)
337 return SSL_TLSEXT_ERR_NOACK;
338
339 if ((ocsp_staple = malloc(ctx->config->ocsp_staple_len)) == NULL)
340 goto err;
341
342 memcpy(ocsp_staple, ctx->config->ocsp_staple,
343 ctx->config->ocsp_staple_len);
344 if (SSL_set_tlsext_status_ocsp_resp(ctx->ssl_conn, ocsp_staple,
345 ctx->config->ocsp_staple_len) != 1)
346 goto err;
347
348 ret = SSL_TLSEXT_ERR_OK;
349 err:
350 if (ret != SSL_TLSEXT_ERR_OK)
351 free(ocsp_staple);
352 return ret;
353}
354
325/* 355/*
326 * Public API 356 * Public API
327 */ 357 */
diff --git a/src/lib/libtls/tls_server.c b/src/lib/libtls/tls_server.c
index e3b03e1301..a9a5902add 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.29 2016/11/04 19:01:29 jsing Exp $ */ 1/* $OpenBSD: tls_server.c,v 1.30 2016/11/05 15:13:26 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -48,6 +48,7 @@ tls_server_conn(struct tls *ctx)
48 return (NULL); 48 return (NULL);
49 49
50 conn_ctx->flags |= TLS_SERVER_CONN; 50 conn_ctx->flags |= TLS_SERVER_CONN;
51 conn_ctx->config = ctx->config;
51 52
52 return (conn_ctx); 53 return (conn_ctx);
53} 54}
@@ -213,6 +214,11 @@ tls_configure_server_ssl(struct tls *ctx, SSL_CTX **ssl_ctx,
213 if (ctx->config->ciphers_server == 1) 214 if (ctx->config->ciphers_server == 1)
214 SSL_CTX_set_options(*ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); 215 SSL_CTX_set_options(*ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
215 216
217 if (SSL_CTX_set_tlsext_status_cb(ctx->ssl_ctx, tls_ocsp_stapling_cb) != 1) {
218 tls_set_errorx(ctx, "failed to add OCSP stapling callback");
219 goto err;
220 }
221
216 /* 222 /*
217 * Set session ID context to a random value. We don't support 223 * Set session ID context to a random value. We don't support
218 * persistent caching of sessions so it is OK to set a temporary 224 * persistent caching of sessions so it is OK to set a temporary
diff --git a/src/usr.bin/nc/nc.1 b/src/usr.bin/nc/nc.1
index 8c7790f72a..2dda57af92 100644
--- a/src/usr.bin/nc/nc.1
+++ b/src/usr.bin/nc/nc.1
@@ -1,4 +1,4 @@
1.\" $OpenBSD: nc.1,v 1.76 2016/11/04 07:34:17 jmc Exp $ 1.\" $OpenBSD: nc.1,v 1.77 2016/11/05 15:13:26 beck Exp $
2.\" 2.\"
3.\" Copyright (c) 1996 David Sacerdote 3.\" Copyright (c) 1996 David Sacerdote
4.\" All rights reserved. 4.\" All rights reserved.
@@ -25,7 +25,7 @@
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\" 27.\"
28.Dd $Mdocdate: November 4 2016 $ 28.Dd $Mdocdate: November 5 2016 $
29.Dt NC 1 29.Dt NC 1
30.Os 30.Os
31.Sh NAME 31.Sh NAME
@@ -43,6 +43,7 @@
43.Op Fl M Ar ttl 43.Op Fl M Ar ttl
44.Op Fl m Ar minttl 44.Op Fl m Ar minttl
45.Op Fl O Ar length 45.Op Fl O Ar length
46.Op Fl o Ar staplefile
46.Op Fl P Ar proxy_username 47.Op Fl P Ar proxy_username
47.Op Fl p Ar source_port 48.Op Fl p Ar source_port
48.Op Fl R Ar CAfile 49.Op Fl R Ar CAfile
@@ -187,6 +188,12 @@ Do not do any DNS or service lookups on any specified addresses,
187hostnames or ports. 188hostnames or ports.
188.It Fl O Ar length 189.It Fl O Ar length
189Specifies the size of the TCP send buffer. 190Specifies the size of the TCP send buffer.
191.It Fl o Ar staplefile
192Specifies the filename from which to load data to be stapled
193during the TLS handshake.
194The file is expected to contain an OSCP response from an OCSP server in
195DER format.
196May only be used with TLS and when a certificate is being used.
190.It Fl P Ar proxy_username 197.It Fl P Ar proxy_username
191Specifies a username to present to a proxy server that requires authentication. 198Specifies a username to present to a proxy server that requires authentication.
192If no username is specified then authentication will not be attempted. 199If no username is specified then authentication will not be attempted.
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c
index b71c0426dc..4a841fb96d 100644
--- a/src/usr.bin/nc/netcat.c
+++ b/src/usr.bin/nc/netcat.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: netcat.c,v 1.167 2016/11/04 05:13:13 beck Exp $ */ 1/* $OpenBSD: netcat.c,v 1.168 2016/11/05 15:13:26 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
4 * Copyright (c) 2015 Bob Beck. All rights reserved. 4 * Copyright (c) 2015 Bob Beck. All rights reserved.
@@ -100,6 +100,7 @@ int rtableid = -1;
100int usetls; /* use TLS */ 100int usetls; /* use TLS */
101char *Cflag; /* Public cert file */ 101char *Cflag; /* Public cert file */
102char *Kflag; /* Private key file */ 102char *Kflag; /* Private key file */
103char *oflag; /* OCSP stapling file */
103char *Rflag = DEFAULT_CA_FILE; /* Root CA file */ 104char *Rflag = DEFAULT_CA_FILE; /* Root CA file */
104int tls_cachanged; /* Using non-default CA file */ 105int tls_cachanged; /* Using non-default CA file */
105int TLSopt; /* TLS options */ 106int TLSopt; /* TLS options */
@@ -163,7 +164,7 @@ main(int argc, char *argv[])
163 signal(SIGPIPE, SIG_IGN); 164 signal(SIGPIPE, SIG_IGN);
164 165
165 while ((ch = getopt(argc, argv, 166 while ((ch = getopt(argc, argv,
166 "46C:cDde:FH:hI:i:K:klM:m:NnO:P:p:R:rSs:T:tUuV:vw:X:x:z")) != -1) { 167 "46C:cDde:FH:hI:i:K:klM:m:NnO:o:P:p:R:rSs:T:tUuV:vw:X:x:z")) != -1) {
167 switch (ch) { 168 switch (ch) {
168 case '4': 169 case '4':
169 family = AF_INET; 170 family = AF_INET;
@@ -295,6 +296,9 @@ main(int argc, char *argv[])
295 errx(1, "TCP send window %s: %s", 296 errx(1, "TCP send window %s: %s",
296 errstr, optarg); 297 errstr, optarg);
297 break; 298 break;
299 case 'o':
300 oflag = optarg;
301 break;
298 case 'S': 302 case 'S':
299 Sflag = 1; 303 Sflag = 1;
300 break; 304 break;
@@ -380,6 +384,8 @@ main(int argc, char *argv[])
380 errx(1, "you must specify -c to use -C"); 384 errx(1, "you must specify -c to use -C");
381 if (Kflag && !usetls) 385 if (Kflag && !usetls)
382 errx(1, "you must specify -c to use -K"); 386 errx(1, "you must specify -c to use -K");
387 if (oflag && !Cflag)
388 errx(1, "you must specify -C to use -o");
383 if (tls_cachanged && !usetls) 389 if (tls_cachanged && !usetls)
384 errx(1, "you must specify -c to use -R"); 390 errx(1, "you must specify -c to use -R");
385 if (tls_expecthash && !usetls) 391 if (tls_expecthash && !usetls)
@@ -455,6 +461,8 @@ main(int argc, char *argv[])
455 errx(1, "%s", tls_config_error(tls_cfg)); 461 errx(1, "%s", tls_config_error(tls_cfg));
456 if (Kflag && tls_config_set_key_file(tls_cfg, Kflag) == -1) 462 if (Kflag && tls_config_set_key_file(tls_cfg, Kflag) == -1)
457 errx(1, "%s", tls_config_error(tls_cfg)); 463 errx(1, "%s", tls_config_error(tls_cfg));
464 if (oflag && tls_config_set_ocsp_staple_file(tls_cfg, oflag) == -1)
465 errx(1, "%s", tls_config_error(tls_cfg));
458 if (TLSopt & TLS_LEGACY) { 466 if (TLSopt & TLS_LEGACY) {
459 tls_config_set_protocols(tls_cfg, TLS_PROTOCOLS_ALL); 467 tls_config_set_protocols(tls_cfg, TLS_PROTOCOLS_ALL);
460 tls_config_set_ciphers(tls_cfg, "all"); 468 tls_config_set_ciphers(tls_cfg, "all");