summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbeck <>2015-09-11 12:56:55 +0000
committerbeck <>2015-09-11 12:56:55 +0000
commit68048eefb0353c6cb686c092af50ba61fca67874 (patch)
tree6080d52d99050fc94d1bd00cdb70fd607c96d262 /src
parent656ef1e2adb75d20a81b8763a2088b2c8e6c3058 (diff)
downloadopenbsd-68048eefb0353c6cb686c092af50ba61fca67874.tar.gz
openbsd-68048eefb0353c6cb686c092af50ba61fca67874.tar.bz2
openbsd-68048eefb0353c6cb686c092af50ba61fca67874.zip
add tls_peer functions for checking names and issuers of peer certificates.
ok jsing@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libtls/Makefile4
-rw-r--r--src/lib/libtls/tls.h7
-rw-r--r--src/lib/libtls/tls_client.c4
-rw-r--r--src/lib/libtls/tls_init.331
-rw-r--r--src/lib/libtls/tls_internal.h4
-rw-r--r--src/lib/libtls/tls_peer.c50
-rw-r--r--src/lib/libtls/tls_verify.c8
7 files changed, 95 insertions, 13 deletions
diff --git a/src/lib/libtls/Makefile b/src/lib/libtls/Makefile
index 0e3329589e..2e03e247e4 100644
--- a/src/lib/libtls/Makefile
+++ b/src/lib/libtls/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.15 2015/09/11 11:28:01 jsing Exp $ 1# $OpenBSD: Makefile,v 1.16 2015/09/11 12:56:55 beck Exp $
2 2
3CFLAGS+= -Wall -Werror -Wimplicit 3CFLAGS+= -Wall -Werror -Wimplicit
4CFLAGS+= -DLIBRESSL_INTERNAL 4CFLAGS+= -DLIBRESSL_INTERNAL
@@ -46,6 +46,8 @@ MLINKS+=tls_init.3 tls_config_insecure_noverifyname.3
46MLINKS+=tls_init.3 tls_config_verify.3 46MLINKS+=tls_init.3 tls_config_verify.3
47MLINKS+=tls_init.3 tls_config_verify_client.3 47MLINKS+=tls_init.3 tls_config_verify_client.3
48MLINKS+=tls_init.3 tls_config_verify_client_optional.3 48MLINKS+=tls_init.3 tls_config_verify_client_optional.3
49MLINKS+=tls_init.3 tls_peer_cert_provided.3
50MLINKS+=tls_init.3 tls_peer_cert_contains_name.3
49MLINKS+=tls_init.3 tls_load_file.3 51MLINKS+=tls_init.3 tls_load_file.3
50MLINKS+=tls_init.3 tls_client.3 52MLINKS+=tls_init.3 tls_client.3
51MLINKS+=tls_init.3 tls_server.3 53MLINKS+=tls_init.3 tls_server.3
diff --git a/src/lib/libtls/tls.h b/src/lib/libtls/tls.h
index 3cb24f0ee5..3d1682e2b6 100644
--- a/src/lib/libtls/tls.h
+++ b/src/lib/libtls/tls.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls.h,v 1.19 2015/09/11 11:28:01 jsing Exp $ */ 1/* $OpenBSD: tls.h,v 1.20 2015/09/11 12:56:55 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -76,6 +76,11 @@ void tls_config_verify(struct tls_config *_config);
76void tls_config_verify_client(struct tls_config *_config); 76void tls_config_verify_client(struct tls_config *_config);
77void tls_config_verify_client_optional(struct tls_config *_config); 77void tls_config_verify_client_optional(struct tls_config *_config);
78 78
79int tls_peer_cert_provided(struct tls *ctx);
80int tls_peer_cert_contains_name(struct tls *ctx, const char *name);
81int tls_peer_cert_issuer(struct tls *ctx, char **name);
82int tls_peer_cert_subject(struct tls *ctx, char **subject);
83
79void tls_config_clear_keys(struct tls_config *_config); 84void tls_config_clear_keys(struct tls_config *_config);
80int tls_config_parse_protocols(uint32_t *_protocols, const char *_protostr); 85int tls_config_parse_protocols(uint32_t *_protocols, const char *_protostr);
81 86
diff --git a/src/lib/libtls/tls_client.c b/src/lib/libtls/tls_client.c
index fb7f3a6f75..2aca519f8b 100644
--- a/src/lib/libtls/tls_client.c
+++ b/src/lib/libtls/tls_client.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_client.c,v 1.26 2015/09/10 10:14:20 jsing Exp $ */ 1/* $OpenBSD: tls_client.c,v 1.27 2015/09/11 12:56:55 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -262,7 +262,7 @@ tls_handshake_client(struct tls *ctx)
262 tls_set_errorx(ctx, "no server certificate"); 262 tls_set_errorx(ctx, "no server certificate");
263 goto err; 263 goto err;
264 } 264 }
265 if ((rv = tls_check_servername(ctx, cert, 265 if ((rv = tls_check_name(ctx, cert,
266 ctx->servername)) != 0) { 266 ctx->servername)) != 0) {
267 if (rv != -2) 267 if (rv != -2)
268 tls_set_errorx(ctx, "name `%s' not present in" 268 tls_set_errorx(ctx, "name `%s' not present in"
diff --git a/src/lib/libtls/tls_init.3 b/src/lib/libtls/tls_init.3
index 6197817f6f..4066713603 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.37 2015/09/11 09:02:10 beck Exp $ 1.\" $OpenBSD: tls_init.3,v 1.38 2015/09/11 12:56:55 beck Exp $
2.\" 2.\"
3.\" Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> 3.\" Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
4.\" 4.\"
@@ -112,6 +112,10 @@
112.Fn tls_config_verify_client "struct tls_config *config" 112.Fn tls_config_verify_client "struct tls_config *config"
113.Ft "void" 113.Ft "void"
114.Fn tls_config_verify_client_optional "struct tls_config *config" 114.Fn tls_config_verify_client_optional "struct tls_config *config"
115.Ft "int"
116.Fn tls_peer_cert_provided "struct tls *ctx"
117.Ft "int"
118.Fn tls_peer_cert_contains_name "struct tls *ctx" "const char *name"
115.Ft "uint8_t *" 119.Ft "uint8_t *"
116.Fn tls_load_file "const char *file" "size_t *len" "char *password" 120.Fn tls_load_file "const char *file" "size_t *len" "char *password"
117.Ft "struct tls *" 121.Ft "struct tls *"
@@ -354,6 +358,24 @@ enables client certificate verification, requiring the client to send
354a certificate. 358a certificate.
355.Em (Server) 359.Em (Server)
356.It 360.It
361.Fn tls_peer_cert_provided
362checks if the peer of
363.Ar ctx
364has provided a certificate.
365.Fn tls_peer_cert_provided
366will only succeed after the handshake is complete.
367.Em (Server and client)
368.It
369.Fn tls_peer_cert_constains_name
370checks if the peer of a tls
371.Ar ctx
372 has povided a certificate that contains a
373SAN or CN that matches
374.Ar name
375.Fn tls_peer_cert_contains_name
376will only succeed after the handshake is complete.
377.Em (Server and client)
378.It
357.Fn tls_config_verify_client_opional 379.Fn tls_config_verify_client_opional
358enables client certificate verification, without requiring the client 380enables client certificate verification, without requiring the client
359to send a certificate. 381to send a certificate.
@@ -455,7 +477,12 @@ or
455.Fn tls_connect_servername . 477.Fn tls_connect_servername .
456.El 478.El
457.Sh RETURN VALUES 479.Sh RETURN VALUES
458Functions that return 480The
481.Fn tls_peer_cert_provided
482and
483.Fn tls_peer_cert_contain_name
484functions return 1 if the check succeeds, and 0 if it does not.
485All other functions that return
459.Vt int 486.Vt int
460or 487or
461.Vt ssize_t 488.Vt ssize_t
diff --git a/src/lib/libtls/tls_internal.h b/src/lib/libtls/tls_internal.h
index b514847cfe..34af0fb48a 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.19 2015/09/11 09:24:54 jsing Exp $ */ 1/* $OpenBSD: tls_internal.h,v 1.20 2015/09/11 12:56:55 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>
@@ -73,7 +73,7 @@ struct tls {
73struct tls *tls_new(void); 73struct tls *tls_new(void);
74struct tls *tls_server_conn(struct tls *ctx); 74struct tls *tls_server_conn(struct tls *ctx);
75 75
76int tls_check_servername(struct tls *ctx, X509 *cert, const char *servername); 76int tls_check_name(struct tls *ctx, X509 *cert, const char *servername);
77int tls_configure_keypair(struct tls *ctx, int); 77int tls_configure_keypair(struct tls *ctx, int);
78int tls_configure_server(struct tls *ctx); 78int tls_configure_server(struct tls *ctx);
79int tls_configure_ssl(struct tls *ctx); 79int tls_configure_ssl(struct tls *ctx);
diff --git a/src/lib/libtls/tls_peer.c b/src/lib/libtls/tls_peer.c
index e2a2c7b141..39a9d90cab 100644
--- a/src/lib/libtls/tls_peer.c
+++ b/src/lib/libtls/tls_peer.c
@@ -1,6 +1,7 @@
1/* $OpenBSD: tls_peer.c,v 1.1 2015/09/11 11:28:01 jsing Exp $ */ 1/* $OpenBSD: tls_peer.c,v 1.2 2015/09/11 12:56:55 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2015 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2015 Joel Sing <jsing@openbsd.org>
4 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
4 * 5 *
5 * Permission to use, copy, modify, and distribute this software for any 6 * 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 * purpose with or without fee is hereby granted, provided that the above
@@ -85,3 +86,50 @@ err:
85 86
86 return (rv); 87 return (rv);
87} 88}
89
90int
91tls_peer_cert_provided(struct tls *ctx)
92{
93 return (ctx->ssl_peer_cert != NULL);
94}
95
96int
97tls_peer_cert_contains_name(struct tls *ctx, const char *name)
98{
99 if (ctx->ssl_peer_cert == NULL)
100 return (0);
101
102 return (tls_check_name(ctx, ctx->ssl_peer_cert, name) == 0);
103}
104
105int
106tls_peer_cert_issuer(struct tls *ctx, char **issuer)
107{
108 X509_NAME *name = NULL;
109
110 *issuer = NULL;
111 if (ctx->ssl_peer_cert != NULL)
112 return (-1);
113 if ((name = X509_get_issuer_name(ctx->ssl_peer_cert)) == NULL)
114 return (-1);
115 *issuer = X509_NAME_oneline(name, 0, 0);
116 if (*issuer == NULL)
117 return (-1);
118 return (0);
119}
120
121int
122tls_peer_cert_subject(struct tls *ctx, char **subject)
123{
124 X509_NAME *name = NULL;
125
126 *subject = NULL;
127 if (ctx->ssl_peer_cert == NULL)
128 return (-1);
129 if ((name = X509_get_subject_name(ctx->ssl_peer_cert)) == NULL)
130 return (-1);
131 *subject = X509_NAME_oneline(name, 0, 0);
132 if (*subject == NULL)
133 return (-1);
134 return (0);
135}
diff --git a/src/lib/libtls/tls_verify.c b/src/lib/libtls/tls_verify.c
index 7fd0b3d545..c6f29c897d 100644
--- a/src/lib/libtls/tls_verify.c
+++ b/src/lib/libtls/tls_verify.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_verify.c,v 1.11 2015/09/09 19:49:07 jsing Exp $ */ 1/* $OpenBSD: tls_verify.c,v 1.12 2015/09/11 12:56:55 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 * 4 *
@@ -246,13 +246,13 @@ tls_check_common_name(struct tls *ctx, X509 *cert, const char *name)
246} 246}
247 247
248int 248int
249tls_check_servername(struct tls *ctx, X509 *cert, const char *servername) 249tls_check_name(struct tls *ctx, X509 *cert, const char *name)
250{ 250{
251 int rv; 251 int rv;
252 252
253 rv = tls_check_subject_altname(ctx, cert, servername); 253 rv = tls_check_subject_altname(ctx, cert, name);
254 if (rv == 0 || rv == -2) 254 if (rv == 0 || rv == -2)
255 return rv; 255 return rv;
256 256
257 return tls_check_common_name(ctx, cert, servername); 257 return tls_check_common_name(ctx, cert, name);
258} 258}