summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbeck <>2017-04-05 03:19:22 +0000
committerbeck <>2017-04-05 03:19:22 +0000
commit2ffca9ef617ca25d3718111a126dbb0d580dd018 (patch)
tree3c6a03473ecfe3397dd6444eb474c3ac576ab36d /src
parent9b1685fcc9e7e4061dec174430e3e4a4d525dbe4 (diff)
downloadopenbsd-2ffca9ef617ca25d3718111a126dbb0d580dd018.tar.gz
openbsd-2ffca9ef617ca25d3718111a126dbb0d580dd018.tar.bz2
openbsd-2ffca9ef617ca25d3718111a126dbb0d580dd018.zip
Add tls_peer_cert_chain_pem - To retreive the peer certificate and chain
as PEM format. This allows for it to be used or examined with tools external to libtls bump minor ok jsing@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libtls/Symbols.list1
-rw-r--r--src/lib/libtls/shlib_version2
-rw-r--r--src/lib/libtls/tls.c5
-rw-r--r--src/lib/libtls/tls.h3
-rw-r--r--src/lib/libtls/tls_conninfo.c52
-rw-r--r--src/lib/libtls/tls_internal.h7
-rw-r--r--src/lib/libtls/tls_peer.c13
7 files changed, 77 insertions, 6 deletions
diff --git a/src/lib/libtls/Symbols.list b/src/lib/libtls/Symbols.list
index 98465dde27..248784a488 100644
--- a/src/lib/libtls/Symbols.list
+++ b/src/lib/libtls/Symbols.list
@@ -60,6 +60,7 @@ tls_handshake
60tls_init 60tls_init
61tls_load_file 61tls_load_file
62tls_ocsp_process_response 62tls_ocsp_process_response
63tls_peer_cert_chain_pem
63tls_peer_cert_contains_name 64tls_peer_cert_contains_name
64tls_peer_cert_hash 65tls_peer_cert_hash
65tls_peer_cert_issuer 66tls_peer_cert_issuer
diff --git a/src/lib/libtls/shlib_version b/src/lib/libtls/shlib_version
index 4c073ef03c..f0f244c56a 100644
--- a/src/lib/libtls/shlib_version
+++ b/src/lib/libtls/shlib_version
@@ -1,2 +1,2 @@
1major=15 1major=15
2minor=4 2minor=5
diff --git a/src/lib/libtls/tls.c b/src/lib/libtls/tls.c
index 419554818c..446f93430d 100644
--- a/src/lib/libtls/tls.c
+++ b/src/lib/libtls/tls.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls.c,v 1.60 2017/04/05 03:13:53 beck Exp $ */ 1/* $OpenBSD: tls.c,v 1.61 2017/04/05 03:19:22 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -528,6 +528,8 @@ tls_reset(struct tls *ctx)
528 ctx->ssl_conn = NULL; 528 ctx->ssl_conn = NULL;
529 ctx->ssl_ctx = NULL; 529 ctx->ssl_ctx = NULL;
530 ctx->ssl_peer_cert = NULL; 530 ctx->ssl_peer_cert = NULL;
531 /* X509 objects in chain are freed with the SSL */
532 ctx->ssl_peer_chain = NULL;
531 533
532 ctx->socket = -1; 534 ctx->socket = -1;
533 ctx->state = 0; 535 ctx->state = 0;
@@ -625,6 +627,7 @@ tls_handshake(struct tls *ctx)
625 627
626 if (rv == 0) { 628 if (rv == 0) {
627 ctx->ssl_peer_cert = SSL_get_peer_certificate(ctx->ssl_conn); 629 ctx->ssl_peer_cert = SSL_get_peer_certificate(ctx->ssl_conn);
630 ctx->ssl_peer_chain = SSL_get_peer_cert_chain(ctx->ssl_conn);
628 if (tls_conninfo_populate(ctx) == -1) 631 if (tls_conninfo_populate(ctx) == -1)
629 rv = -1; 632 rv = -1;
630 if (ctx->ocsp == NULL) 633 if (ctx->ocsp == NULL)
diff --git a/src/lib/libtls/tls.h b/src/lib/libtls/tls.h
index d9b2972e92..c9da8aa06e 100644
--- a/src/lib/libtls/tls.h
+++ b/src/lib/libtls/tls.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls.h,v 1.47 2017/01/31 16:18:57 beck Exp $ */ 1/* $OpenBSD: tls.h,v 1.48 2017/04/05 03:19:22 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -179,6 +179,7 @@ const char *tls_peer_cert_issuer(struct tls *_ctx);
179const char *tls_peer_cert_subject(struct tls *_ctx); 179const char *tls_peer_cert_subject(struct tls *_ctx);
180time_t tls_peer_cert_notbefore(struct tls *_ctx); 180time_t tls_peer_cert_notbefore(struct tls *_ctx);
181time_t tls_peer_cert_notafter(struct tls *_ctx); 181time_t tls_peer_cert_notafter(struct tls *_ctx);
182const uint8_t *tls_peer_cert_chain_pem(struct tls *_ctx, size_t *_len);
182 183
183const char *tls_conn_alpn_selected(struct tls *_ctx); 184const char *tls_conn_alpn_selected(struct tls *_ctx);
184const char *tls_conn_cipher(struct tls *_ctx); 185const char *tls_conn_cipher(struct tls *_ctx);
diff --git a/src/lib/libtls/tls_conninfo.c b/src/lib/libtls/tls_conninfo.c
index c4d23c308b..87660fa989 100644
--- a/src/lib/libtls/tls_conninfo.c
+++ b/src/lib/libtls/tls_conninfo.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_conninfo.c,v 1.14 2017/04/05 03:13:53 beck Exp $ */ 1/* $OpenBSD: tls_conninfo.c,v 1.15 2017/04/05 03:19:22 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 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
@@ -174,6 +174,49 @@ tls_conninfo_alpn_proto(struct tls *ctx)
174 return (0); 174 return (0);
175} 175}
176 176
177static int
178tls_conninfo_cert_pem(struct tls *ctx)
179{
180 int i, rv = -1;
181 BIO *membio = NULL;
182 BUF_MEM *bptr = NULL;
183
184 if (ctx->conninfo == NULL)
185 goto err;
186 if (ctx->ssl_peer_cert == NULL)
187 return 0;
188 if ((membio = BIO_new(BIO_s_mem()))== NULL)
189 goto err;
190
191 /*
192 * We have to write the peer cert out separately, because
193 * the certificate chain may or may not contain it.
194 */
195 if (!PEM_write_bio_X509(membio, ctx->ssl_peer_cert))
196 goto err;
197 for (i = 0; i < sk_X509_num(ctx->ssl_peer_chain); i++) {
198 X509 *chaincert = sk_X509_value(ctx->ssl_peer_chain, i);
199 if (chaincert != ctx->ssl_peer_cert &&
200 !PEM_write_bio_X509(membio, chaincert))
201 goto err;
202 }
203
204 BIO_get_mem_ptr(membio, &bptr);
205 free(ctx->conninfo->peer_cert);
206 ctx->conninfo->peer_cert_len = 0;
207 if ((ctx->conninfo->peer_cert = malloc(bptr->length)) == NULL)
208 goto err;
209 ctx->conninfo->peer_cert_len = bptr->length;
210 memcpy(ctx->conninfo->peer_cert, bptr->data,
211 ctx->conninfo->peer_cert_len);
212
213 /* BIO_free() will kill BUF_MEM - because we have not set BIO_NOCLOSE */
214 rv = 0;
215 err:
216 BIO_free(membio);
217 return rv;
218}
219
177int 220int
178tls_conninfo_populate(struct tls *ctx) 221tls_conninfo_populate(struct tls *ctx)
179{ 222{
@@ -210,6 +253,9 @@ tls_conninfo_populate(struct tls *ctx)
210 if (tls_get_peer_cert_info(ctx) == -1) 253 if (tls_get_peer_cert_info(ctx) == -1)
211 goto err; 254 goto err;
212 255
256 if (tls_conninfo_cert_pem(ctx) == -1)
257 goto err;
258
213 return (0); 259 return (0);
214 260
215 err: 261 err:
@@ -241,6 +287,10 @@ tls_conninfo_free(struct tls_conninfo *conninfo)
241 free(conninfo->subject); 287 free(conninfo->subject);
242 conninfo->subject = NULL; 288 conninfo->subject = NULL;
243 289
290 free(conninfo->peer_cert);
291 conninfo->peer_cert = NULL;
292 conninfo->peer_cert_len = 0;
293
244 free(conninfo); 294 free(conninfo);
245} 295}
246 296
diff --git a/src/lib/libtls/tls_internal.h b/src/lib/libtls/tls_internal.h
index b1d53c8fa3..5bbcadf804 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.54 2017/04/05 03:13:53 beck Exp $ */ 1/* $OpenBSD: tls_internal.h,v 1.55 2017/04/05 03:19:22 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>
@@ -110,6 +110,9 @@ struct tls_conninfo {
110 char *issuer; 110 char *issuer;
111 char *subject; 111 char *subject;
112 112
113 u_int8_t *peer_cert;
114 size_t peer_cert_len;
115
113 time_t notbefore; 116 time_t notbefore;
114 time_t notafter; 117 time_t notafter;
115}; 118};
@@ -166,6 +169,7 @@ struct tls {
166 struct tls_sni_ctx *sni_ctx; 169 struct tls_sni_ctx *sni_ctx;
167 170
168 X509 *ssl_peer_cert; 171 X509 *ssl_peer_cert;
172 STACK_OF(X509) *ssl_peer_chain;
169 173
170 struct tls_conninfo *conninfo; 174 struct tls_conninfo *conninfo;
171 175
@@ -237,6 +241,7 @@ struct tls_ocsp *tls_ocsp_setup_from_peer(struct tls *ctx);
237int tls_hex_string(const unsigned char *_in, size_t _inlen, char **_out, 241int tls_hex_string(const unsigned char *_in, size_t _inlen, char **_out,
238 size_t *_outlen); 242 size_t *_outlen);
239int tls_cert_hash(X509 *_cert, char **_hash); 243int tls_cert_hash(X509 *_cert, char **_hash);
244void tls_config_skip_private_key_check(struct tls_config *config);
240 245
241__END_HIDDEN_DECLS 246__END_HIDDEN_DECLS
242 247
diff --git a/src/lib/libtls/tls_peer.c b/src/lib/libtls/tls_peer.c
index 802a9c2780..1a9065dfb1 100644
--- a/src/lib/libtls/tls_peer.c
+++ b/src/lib/libtls/tls_peer.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_peer.c,v 1.6 2016/08/22 17:08:10 jsing Exp $ */ 1/* $OpenBSD: tls_peer.c,v 1.7 2017/04/05 03:19:22 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 * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
@@ -81,3 +81,14 @@ tls_peer_cert_notafter(struct tls *ctx)
81 return (ctx->conninfo->notafter); 81 return (ctx->conninfo->notafter);
82} 82}
83 83
84const uint8_t *
85tls_peer_cert_chain_pem(struct tls *ctx, size_t *size)
86{
87 if (ctx->ssl_peer_cert == NULL)
88 return (NULL);
89 if (ctx->conninfo == NULL)
90 return (NULL);
91 *size = ctx->conninfo->peer_cert_len;
92 return (ctx->conninfo->peer_cert);
93}
94