Skip to contentSkip to conversation

pem: Add pem_write_certificate_chain

PATCH In review Andrew Zaborowski 2 participants 4 messages Dec 17, 2020 – Dec 18, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 pem: Add pem_write_certificate_chain

---
 ell/pem-private.h |  5 +++++
 ell/pem.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
ell/pem-private.h +5 -0
index 6e9458d..10f918f 100644
@@ -27,6 +27,8 @@
 #define _GNU_SOURCE
 #include <sys/types.h>
 
+struct l_certchain;
+
 const char *pem_next(const void *buf, size_t buf_len, char **type_label,
 				size_t *base64_len,
 				const char **endp, bool strict);
@@ -37,3 +39,6 @@ struct l_key *pem_key_from_pkcs8_private_key_info(const uint8_t *der,
 struct l_key *pem_key_from_pkcs8_encrypted_private_key_info(const uint8_t *der,
 							size_t der_len,
 							const char *passphrase);
+
+int pem_write_certificate_chain(const struct l_certchain *cert,
+				const char *filename);
ell/pem.c +45 -0

51 lines not on this page · raw message

Signature · 1 line
2.27.0
#1 lore raw

PATCH 2/3 tls: Add l_tls_set_cert_dump_path

If this function is used to set a file path, l_tls will write the peer's certificate chain received in the TLS Certificate message if it is used (on the server side we may receive no client certificate). This can be used to diagnose 802.1x connection configurations for example.

---
 ell/ell.sym       |  1 +
 ell/tls-private.h |  1 +
 ell/tls.c         | 22 ++++++++++++++++++++++
 ell/tls.h         |  1 +
 4 files changed, 25 insertions(+)
ell/ell.sym +1 -0
index d94b585..c98bd8c 100644
@@ -498,6 +498,7 @@ global:
 	l_tls_set_domain_mask;
 	l_tls_alert_to_str;
 	l_tls_set_debug;
+	l_tls_set_cert_dump_path;
 	/* uintset */
 	l_uintset_new_from_range;
 	l_uintset_new;
ell/tls-private.h +1 -0
index 908c622..8d0540a 100644
@@ -205,6 +205,7 @@ struct l_tls {
 	l_tls_debug_cb_t debug_handler;
 	l_tls_destroy_cb_t debug_destroy;
 	void *debug_data;
+	char *cert_dump_path;
 	enum l_tls_version min_version;
 	enum l_tls_version max_version;
 
ell/tls.c +22 -0
index 1452909..4eaa66d 100644
@@ -37,6 +37,7 @@
 #include "random.h"
 #include "queue.h"
 #include "pem.h"
+#include "pem-private.h"
 #include "cert.h"
 #include "cert-private.h"
 #include "tls-private.h"
@@ -1942,6 +1943,19 @@ static void tls_handle_certificate(struct l_tls *tls,
 		goto done;
 	}
 
+	if (tls->cert_dump_path) {
+		int r = pem_write_certificate_chain(certchain,
+							tls->cert_dump_path);
+
+		if (r < 0)
+			TLS_DEBUG("Error %i (%s) writing the peer certchain "
+					"to %s",
+					-r, strerror(-r), tls->cert_dump_path);
+		else
+			TLS_DEBUG("Peer certchain written to %s",
+					tls->cert_dump_path);
+	}
+
 	/*
 	 * Validate the certificate chain's consistency and validate it
 	 * against our CAs if we have any.
@@ -2614,6 +2628,7 @@ LIB_EXPORT void l_tls_free(struct l_tls *tls)
 	l_tls_set_cacert(tls, NULL);
 	l_tls_set_auth_data(tls, NULL, NULL);
 	l_tls_set_domain_mask(tls, NULL);
+	l_tls_set_cert_dump_path(tls, NULL);
 
 	tls_reset_handshake(tls);
 	tls_cleanup_handshake(tls);
@@ -3104,3 +3119,10 @@ LIB_EXPORT bool l_tls_set_debug(struct l_tls *tls, l_tls_debug_cb_t function,
 
 	return true;
 }
+
+LIB_EXPORT bool l_tls_set_cert_dump_path(struct l_tls *tls, const char *path)
+{
+	l_free(tls->cert_dump_path);
+	tls->cert_dump_path = path ? l_strdup(path) : NULL;
+	return true;
+}
ell/tls.h +1 -0
index b67492b..683c54c 100644
@@ -133,6 +133,7 @@ bool l_tls_prf_get_bytes(struct l_tls *tls, bool use_master_secret,
 
 bool l_tls_set_debug(struct l_tls *tls, l_tls_debug_cb_t function,
 			void *user_data, l_tls_destroy_cb_t destroy);
+bool l_tls_set_cert_dump_path(struct l_tls *tls, const char *path);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.27.0
#2 lore raw

PATCH 3/3 examples: Use l_tls_set_cert_dump_path in https examples

In reply to Andrew Zaborowski
---
 examples/https-client-test.c |  9 ++++++++-
 examples/https-server-test.c | 18 ++++++++++++++----
 2 files changed, 22 insertions(+), 5 deletions(-)
examples/https-client-test.c +8 -1
index e17b271..b0c24b4 100644
@@ -190,9 +190,16 @@ int main(int argc, char *argv[])
 	tls = l_tls_new(false, https_new_data, https_tls_write,
 			https_tls_ready, https_tls_disconnected, NULL);
 
-	if (getenv("TLS_DEBUG"))
+	if (getenv("TLS_DEBUG")) {
+		char *str;
+
 		l_tls_set_debug(tls, https_tls_debug_cb, NULL, NULL);
 
+		str = l_strdup_printf("/tmp/ell-certchain-%s.pem", hostname);
+		l_tls_set_cert_dump_path(tls, str);
+		l_free(str);
+	}
+
 	if (argc >= 3) {
 		ca_cert = l_pem_load_certificate_list(argv[2]);
 		if (!ca_cert) {
examples/https-server-test.c +14 -4
index 8f3cb3c..6362722 100644
@@ -117,7 +117,9 @@ static void https_tls_debug_cb(const char *str, void *user_data)
 
 int main(int argc, char *argv[])
 {
-	struct sockaddr_in addr;
+	struct sockaddr_in addr = {};
+	struct sockaddr_in client_addr = {};
+	socklen_t client_addr_len = sizeof(client_addr);
 	int fd, listenfd;
 	bool auth_ok;
 	struct l_certchain *cert;
@@ -141,7 +143,6 @@ int main(int argc, char *argv[])
 	setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &(int) { 1 },
 			sizeof(int));
 
-	memset(&addr, 0, sizeof(addr));
 	addr.sin_family = AF_INET;
 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
 	addr.sin_port = htons(1234);
@@ -157,7 +158,8 @@ int main(int argc, char *argv[])
 
 	printf("Try https://localhost:1234/ now\n");
 
-	fd = accept(listenfd, NULL, NULL);
+	fd = accept(listenfd, (struct sockaddr *) &client_addr,
+			&client_addr_len);
 	close(listenfd);
 	if (fd == -1) {
 		fprintf(stderr, "accept: %s\n", strerror(errno));
@@ -196,9 +198,17 @@ int main(int argc, char *argv[])
 	tls = l_tls_new(true, https_new_data, https_tls_write,
 			https_tls_ready, https_tls_disconnected, NULL);
 
-	if (getenv("TLS_DEBUG"))
+	if (getenv("TLS_DEBUG")) {
+		char *str;
+
 		l_tls_set_debug(tls, https_tls_debug_cb, NULL, NULL);
 
+		str = l_strdup_printf("/tmp/ell-certchain-%s.pem",
+					inet_ntoa(client_addr.sin_addr));
+		l_tls_set_cert_dump_path(tls, str);
+		l_free(str);
+	}
+
 	auth_ok = l_tls_set_auth_data(tls, cert, priv_key) &&
 		(argc <= 4 || l_tls_set_cacert(tls, ca_cert)) &&
 		l_tls_start(tls);
Signature · 1 line
2.27.0
#3 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 5 lines · Andrew Zaborowski
---
  ell/pem-private.h |  5 +++++
  ell/pem.c         | 45 +++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 50 insertions(+)

All applied, thanks.

Regards,
-Denis