Skip to contentSkip to conversation

asn1: Add length check in asn1_parse_definite_length

PATCH In review Andrew Zaborowski 2 participants 8 messages Nov 19, 2018 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/7 asn1: Add length check in asn1_parse_definite_length

I'm going to be using asn1_parse_definite_length outside of the local functions so add the missing length validation.

---
 ell/asn1-private.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
ell/asn1-private.h +7 -1
index 4ba17ac..0c5897a 100644
@@ -46,8 +46,14 @@ static inline int asn1_parse_definite_length(const uint8_t **buf,
 	int n;
 	size_t result = 0;
 
-	(*len)--;
+	/* Decrease the buffer length left */
+	if ((*len)-- < 1)
+		return -1;
 
+	/*
+	 * If short form length, move the pointer to start of data and
+	 * return the data length.
+	 */
 	if (!(**buf & 0x80))
 		return *(*buf)++;
 
Signature · 1 line
2.19.1
#1 lore raw

PATCH 2/7 cert: Add cert.c for certificate types and routines

Create a new file for all the generic X.509 certificate utilities so far living in tls.c and make l_cert and l_certchain public objects. In this patch we only add the contructors and destructors. Since there's not going to be any way to add a certificate to multiple certchains we add the prev/next pointers for the doubly-linked list implementation directly to the l_cert structure. The private certchain constructor is going to be used by pem file loaders in pem.c and in tls_parse_certificate_list.

---
 Makefile.am        |   7 +-
 ell/cert-private.h |  24 ++++++
 ell/cert.c         | 199 +++++++++++++++++++++++++++++++++++++++++++++
 ell/cert.h         |  49 +++++++++++
 ell/ell.h          |   1 +
 ell/ell.sym        |   4 +
 6 files changed, 282 insertions(+), 2 deletions(-)
 create mode 100644 ell/cert-private.h
 create mode 100644 ell/cert.c
 create mode 100644 ell/cert.h
Makefile.am +5 -2
index 0a8d0f6..d115360 100644
@@ -48,7 +48,8 @@ pkginclude_HEADERS = ell/ell.h \
 			ell/file.h \
 			ell/dir.h \
 			ell/net.h \
-			ell/dhcp.h
+			ell/dhcp.h \
+			ell/cert.h
 
 lib_LTLIBRARIES = ell/libell.la
 
@@ -109,7 +110,9 @@ ell_libell_la_SOURCES = $(linux_headers) \
 			ell/dhcp-private.h \
 			ell/dhcp.c \
 			ell/dhcp-transport.c \
-			ell/dhcp-lease.c
+			ell/dhcp-lease.c \
+			ell/cert.c \
+			ell/cert-private.h
 
 ell_libell_la_LDFLAGS = -no-undefined \
 			-Wl,--version-script=$(top_srcdir)/ell/ell.sym \
ell/cert-private.h new file +24 -0
new file mode 100644
index 0000000..f89e216
@@ -0,0 +1,24 @@
+/*
+ *
+ *  Embedded Linux library
+ *
+ *  Copyright (C) 2018  Intel Corporation. All rights reserved.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+struct l_certchain *certchain_new_from_leaf(struct l_cert *leaf);
+void certchain_link_issuer(struct l_certchain *chain, struct l_cert *issuer);
ell/cert.c new file +199 -0

199 lines not on this page · raw message

ell/cert.h new file +49 -0

49 lines not on this page · raw message

ell/ell.h +1 -0
index ba1928d..4b0eac4 100644
@@ -56,3 +56,4 @@
 #include <ell/dbus-service.h>
 #include <ell/dbus-client.h>
 #include <ell/dhcp.h>
+#include <ell/cert.h>
ell/ell.sym +4 -0
index c21fb21..0e6f8a0 100644
@@ -432,6 +432,10 @@ global:
 	l_uuid_is_valid;
 	l_uuid_get_version;
 	l_uuid_to_string;
+	/* cert */
+	l_cert_new_from_der;
+	l_cert_free;
+	l_certchain_free;
 local:
 	*;
 };
Signature · 1 line
2.19.1
#2 lore raw

PATCH 3/7 cert: Add l_cert and l_certchain accessor functions

In reply to Andrew Zaborowski
---
 ell/cert.c  | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 ell/cert.h  | 15 ++++++++++
 ell/ell.sym |  7 +++++
 3 files changed, 108 insertions(+)
ell/cert.c +86 -0
index 141362f..7d0c0b5 100644
@@ -146,6 +146,52 @@ LIB_EXPORT void l_cert_free(struct l_cert *cert)
 	l_free(cert);
 }
 
+LIB_EXPORT const uint8_t *l_cert_get_der_data(struct l_cert *cert,
+						size_t *out_len)
+{
+	if (unlikely(!cert))
+		return NULL;
+
+	*out_len = cert->asn1_len;
+	return cert->asn1;
+}
+
+LIB_EXPORT const uint8_t *l_cert_get_dn(struct l_cert *cert, size_t *out_len)
+{
+	if (unlikely(!cert))
+		return NULL;
+
+	return asn1_der_find_elem_by_path(cert->asn1, cert->asn1_len,
+						ASN1_ID_OID, out_len,
+						X509_CERTIFICATE_POS,
+						X509_TBSCERTIFICATE_POS,
+						X509_TBSCERT_SUBJECT_DN_POS,
+						-1);
+}
+
+LIB_EXPORT enum l_cert_key_type l_cert_get_pubkey_type(struct l_cert *cert)
+{
+	if (unlikely(!cert))
+		return L_CERT_KEY_UNKNOWN;
+
+	return cert->pubkey_type;
+}
+
+/*
+ * Note: Returns a new l_key object to be freed by the caller.
+ */
+LIB_EXPORT struct l_key *l_cert_get_pubkey(struct l_cert *cert)
+{
+	if (unlikely(!cert))
+		return NULL;
+
+	/* Use kernel's ASN.1 certificate parser to find the key data for us */
+	if (cert->pubkey_type == L_CERT_KEY_RSA)
+		return l_key_new(L_KEY_RSA, cert->asn1, cert->asn1_len);
+
+	return NULL;
+}
+
 /*
  * Note: takes ownership of the certificate.  The certificate is
  * assumed to be new and not linked into any certchain object.
@@ -197,3 +243,43 @@ LIB_EXPORT void l_certchain_free(struct l_certchain *chain)
 
 	l_free(chain);
 }
+
+LIB_EXPORT struct l_cert *l_certchain_get_leaf(struct l_certchain *chain)
+{
+	if (unlikely(!chain))
+		return NULL;
+
+	return chain->leaf;
+}
+
+LIB_EXPORT bool l_certchain_foreach_from_leaf(struct l_certchain *chain,
+						l_cert_foreach_cb_t cb,
+						void *user_data)
+{
+	struct l_cert *cert;
+
+	if (unlikely(!chain))
+		return false;
+
+	for (cert = chain->leaf; cert; cert = cert->issuer)
+		if (cb(cert, user_data))
+			return true;
+
+	return false;
+}
+
+LIB_EXPORT bool l_certchain_foreach_from_ca(struct l_certchain *chain,
+						l_cert_foreach_cb_t cb,
+						void *user_data)
+{
+	struct l_cert *cert;
+
+	if (unlikely(!chain))
+		return false;
+
+	for (cert = chain->ca; cert; cert = cert->issued)
+		if (cb(cert, user_data))
+			return true;
+
+	return false;
+}
ell/cert.h +15 -0
index aafc0a9..81a8b43 100644
@@ -37,11 +37,26 @@ enum l_cert_key_type {
 	L_CERT_KEY_UNKNOWN,
 };
 
+typedef bool (*l_cert_foreach_cb_t)(struct l_cert *cert, void *user_data);
+
 struct l_cert *l_cert_new_from_der(const uint8_t *buf, size_t buf_len);
 void l_cert_free(struct l_cert *cert);
 
+const uint8_t *l_cert_get_der_data(struct l_cert *cert, size_t *out_len);
+const uint8_t *l_cert_get_dn(struct l_cert *cert, size_t *out_len);
+enum l_cert_key_type l_cert_get_pubkey_type(struct l_cert *cert);
+struct l_key *l_cert_get_pubkey(struct l_cert *cert);
+
 void l_certchain_free(struct l_certchain *chain);
 
+struct l_cert *l_certchain_get_leaf(struct l_certchain *chain);
+bool l_certchain_foreach_from_leaf(struct l_certchain *chain,
+					l_cert_foreach_cb_t cb,
+					void *user_data);
+bool l_certchain_foreach_from_ca(struct l_certchain *chain,
+					l_cert_foreach_cb_t cb,
+					void *user_data);
+
 #ifdef __cplusplus
 }
 #endif
ell/ell.sym +7 -0
index 0e6f8a0..414df53 100644
@@ -435,7 +435,14 @@ global:
 	/* cert */
 	l_cert_new_from_der;
 	l_cert_free;
+	l_cert_get_der_data;
+	l_cert_get_dn;
+	l_cert_get_pubkey_type;
+	l_cert_get_pubkey;
 	l_certchain_free;
+	l_certchain_get_leaf;
+	l_certchain_foreach_from_leaf;
+	l_certchain_foreach_from_ca;
 local:
 	*;
 };
Signature · 1 line
2.19.1
#3 lore raw

PATCH 4/7 tls: Move certchain utilities to cert.c

In reply to Andrew Zaborowski

Move and adapt tls_cert_verify_certchain and tls_cert_find_certchain to the new cert.c classes and drop tls_cert_free_certchain and tls_cert_get_pubkey_type because these functions are already provided by cert.c. For clarity there's no attempt to improve any of the functions here but further certchain_verify work is pending.

---
 ell/cert.c        | 134 ++++++++++++++++++++++++++++++++++++++
 ell/cert.h        |   3 +
 ell/ell.sym       |   2 +
 ell/tls-private.h |  10 ---
 ell/tls.c         | 161 ----------------------------------------------
 5 files changed, 139 insertions(+), 171 deletions(-)
ell/cert.c +134 -0
index 7d0c0b5..8ce9da0 100644
@@ -283,3 +283,137 @@ LIB_EXPORT bool l_certchain_foreach_from_ca(struct l_certchain *chain,
 
 	return false;
 }
+
+LIB_EXPORT bool l_certchain_find(struct l_certchain *chain,
+					struct l_cert *ca_cert)
+{
+	if (unlikely(!chain || !chain->leaf))
+		return false;
+
+	/* Nothing to do if no CA certificates supplied */
+	if (!ca_cert)
+		return true;
+
+	/*
+	 * Also nothing to do if the user already supplied a working
+	 * certificate chain.
+	 */
+	if (l_certchain_verify(chain, ca_cert))
+		return true;
+
+	/* Actual search for a chain to the CA cert is unimplemented, fail */
+	return false;
+}
+
+static void cert_key_cleanup(struct l_key **p)
+{
+	l_key_free_norevoke(*p);
+}
+
+static bool certchain_verify_with_keyring(struct l_cert *cert,
+						struct l_keyring *ring,
+						struct l_cert *root,
+						struct l_keyring *trusted)
+{
+	if (!cert)
+		return true;
+
+	if (cert->pubkey_type != L_CERT_KEY_RSA)
+		return false;
+
+	/*
+	 * RFC5246 7.4.2:
+	 * "Because certificate validation requires that root keys be
+	 * distributed independently, the self-signed certificate that
+	 * specifies the root certificate authority MAY be omitted from
+	 * the chain, under the assumption that the remote end must
+	 * already possess it in order to validate it in any case."
+	 */
+	if (!cert->issuer && root && cert->asn1_len == root->asn1_len &&
+			!memcmp(cert->asn1, root->asn1, root->asn1_len))
+		return true;
+
+	if (certchain_verify_with_keyring(cert->issuer, ring, root, trusted)) {
+		L_AUTO_CLEANUP_VAR(struct l_key *, key, cert_key_cleanup);
+
+		key = l_key_new(L_KEY_RSA, cert->asn1, cert->asn1_len);
+		if (!key)
+			return false;
+
+		if (!l_keyring_link(ring, key))
+			return false;
+
+		if (trusted || cert->issuer)
+			return true;
+
+		/*
+		 * If execution reaches this point, it's known that:
+		 *  * No trusted root key was supplied, so the chain is only
+		 *    being checked against its own root
+		 *  * The keyring 'ring' is not restricted yet
+		 *  * The chain's root cert was just linked in to the
+		 *    previously empty keyring 'ring'.
+		 *
+		 * By restricting 'ring' now, the rest of the certs in
+		 * the chain will have their signature validated using 'key'
+		 * as the root.
+		 */
+		return l_keyring_restrict(ring,	L_KEYRING_RESTRICT_ASYM_CHAIN,
+						trusted);
+	}
+
+	return false;
+}
+
+static void cert_keyring_cleanup(struct l_keyring **p)
+{
+	l_keyring_free(*p);
+}
+
+LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,
+					struct l_cert *ca_cert)
+{
+	L_AUTO_CLEANUP_VAR(struct l_keyring *, ca_ring,
+				cert_keyring_cleanup) = NULL;
+	L_AUTO_CLEANUP_VAR(struct l_keyring *, verify_ring,
+				cert_keyring_cleanup) = NULL;
+
+	if (unlikely(!chain || !chain->leaf))
+		return false;
+
+	if (ca_cert && ca_cert->pubkey_type != L_CERT_KEY_RSA)
+		return false;
+
+	if (ca_cert) {
+		L_AUTO_CLEANUP_VAR(struct l_key *, ca_key, cert_key_cleanup);
+		ca_key = NULL;
+
+		ca_ring = l_keyring_new();
+		if (!ca_ring)
+			return false;
+
+		ca_key = l_cert_get_pubkey(ca_cert);
+		if (!ca_key || !l_keyring_link(ca_ring, ca_key))
+			return false;
+	}
+
+	verify_ring = l_keyring_new();
+	if (!verify_ring)
+		return false;
+
+	/*
+	 * If a CA cert was supplied, restrict verify_ring now so
+	 * everything else in certchain is validated against the CA.
+	 * Otherwise, verify_ring will be restricted after the root of
+	 * certchain is added to verify_ring by
+	 * cert_verify_with_keyring().
+	 */
+	if (ca_ring && !l_keyring_restrict(verify_ring,
+						L_KEYRING_RESTRICT_ASYM_CHAIN,
+						ca_ring)) {
+		return false;
+	}
+
+	return certchain_verify_with_keyring(chain->leaf, verify_ring, ca_cert,
+						ca_ring);
+}
ell/cert.h +3 -0
index 81a8b43..915ad5f 100644
@@ -57,6 +57,9 @@ bool l_certchain_foreach_from_ca(struct l_certchain *chain,
 					l_cert_foreach_cb_t cb,
 					void *user_data);
 
+bool l_certchain_find(struct l_certchain *chain, struct l_cert *ca_cert);
+bool l_certchain_verify(struct l_certchain *chain, struct l_cert *ca_cert);
+
 #ifdef __cplusplus
 }
 #endif
ell/ell.sym +2 -0
index 414df53..c2aafee 100644
@@ -443,6 +443,8 @@ global:
 	l_certchain_get_leaf;
 	l_certchain_foreach_from_leaf;
 	l_certchain_foreach_from_ca;
+	l_certchain_find;
+	l_certchain_verify;
 local:
 	*;
 };
ell/tls-private.h +0 -10
index 0d8592a..7dfbd6c 100644
@@ -255,16 +255,6 @@ struct tls_cert *tls_cert_load_file(const char *filename);
 int tls_cert_from_certificate_list(const void *data, size_t len,
 					struct tls_cert **out_certchain);
 
-bool tls_cert_find_certchain(struct tls_cert *cert,
-				const char *cacert_filename);
-
-bool tls_cert_verify_certchain(struct tls_cert *certchain,
-				struct tls_cert *ca_cert);
-
-void tls_cert_free_certchain(struct tls_cert *cert);
-
-enum tls_cert_key_type tls_cert_get_pubkey_type(struct tls_cert *cert);
-
 #define TLS_DEBUG(fmt, args...)	\
 	l_util_debug(tls->debug_handler, tls->debug_data, "%s:%i " fmt,	\
 			__func__, __LINE__, ## args)
ell/tls.c +0 -161

167 lines not on this page · raw message

Signature · 1 line
2.19.1
#4 lore raw

PATCH 5/7 tls: Update l_tls to use the l_cert type, drop local code

In reply to Andrew Zaborowski

Drop the remaining struct tls_cert code and use the l_cert type for all the certificate operations. tls_cert_load_file is temporarily left in tls.c but my idea is to replace it with two new pem.c functions, although test-key.c will still need it. tls_cert_from_certificate_list is only renamed to tls_parse_certificate_list as it is TLS-specific.

---
 ell/tls-private.h |  29 ++----
 ell/tls-record.c  |   1 +
 ell/tls.c         | 244 +++++++++++++++++++++-------------------------
 3 files changed, 119 insertions(+), 155 deletions(-)
ell/tls-private.h +7 -22
index 7dfbd6c..9c909e1 100644
@@ -28,8 +28,6 @@
 #define TLS_VERSION	TLS_V12
 #define TLS_MIN_VERSION	TLS_V10
 
-struct tls_cert;
-
 enum tls_cipher_type {
 	TLS_CIPHER_STREAM,
 	TLS_CIPHER_BLOCK,
@@ -65,7 +63,7 @@ struct tls_key_exchange_algorithm {
 
 	bool certificate_check;
 
-	bool (*validate_cert_key_type)(struct tls_cert *cert);
+	bool (*validate_cert_key_type)(struct l_cert *cert);
 
 	bool (*send_client_key_exchange)(struct l_tls *tls);
 	void (*handle_client_key_exchange)(struct l_tls *tls,
@@ -147,8 +145,8 @@ struct l_tls {
 	l_tls_destroy_cb_t debug_destroy;
 	void *debug_data;
 
-	char *ca_cert_path;
-	char *cert_path;
+	struct l_cert *ca_cert;
+	struct l_certchain *cert;
 	struct l_key *priv_key;
 	size_t priv_key_size;
 
@@ -174,7 +172,7 @@ struct l_tls {
 	uint16_t negotiated_version;
 	bool cert_requested, cert_sent;
 	bool peer_authenticated;
-	struct tls_cert *peer_cert;
+	struct l_cert *peer_cert;
 	struct l_key *peer_pubkey;
 	size_t peer_pubkey_size;
 	enum handshake_hash_type signature_hash;
@@ -238,22 +236,9 @@ void tls_tx_record(struct l_tls *tls, enum tls_content_type type,
 bool tls_handle_message(struct l_tls *tls, const uint8_t *message,
 			int len, enum tls_content_type type, uint16_t version);
 
-/* X509 Certificates and Certificate Chains */
-
-struct tls_cert {
-	size_t size;
-	struct tls_cert *issuer;
-	uint8_t asn1[0];
-};
-
-enum tls_cert_key_type {
-	TLS_CERT_KEY_RSA,
-	TLS_CERT_KEY_UNKNOWN,
-};
-
-struct tls_cert *tls_cert_load_file(const char *filename);
-int tls_cert_from_certificate_list(const void *data, size_t len,
-					struct tls_cert **out_certchain);
+struct l_cert *tls_cert_load_file(const char *filename);
+int tls_parse_certificate_list(const void *data, size_t len,
+				struct l_certchain **out_certchain);
 
 #define TLS_DEBUG(fmt, args...)	\
 	l_util_debug(tls->debug_handler, tls->debug_data, "%s:%i " fmt,	\
ell/tls-record.c +1 -0
index a8ff696..73be1a4 100644
@@ -29,6 +29,7 @@
 #include "tls.h"
 #include "checksum.h"
 #include "cipher.h"
+#include "cert.h"
 #include "tls-private.h"
 #include "random.h"
 
ell/tls.c +111 -133

445 lines not on this page · raw message

Signature · 1 line
2.19.1
#5 lore raw

PATCH 6/7 tools: Update certchain-verify to new certificate API

In reply to Andrew Zaborowski
---
 tools/certchain-verify.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
tools/certchain-verify.c +7 -7
index 1800f68..99adb17 100644
@@ -35,7 +35,7 @@
 #include <ell/ell.h>
 #include "ell/tls-private.h"
 
-static int load_cert_chain(const char *file, struct tls_cert **certchain)
+static int load_cert_chain(const char *file, struct l_certchain **certchain)
 {
 	int fd;
 	struct stat st;
@@ -70,7 +70,7 @@ static int load_cert_chain(const char *file, struct tls_cert **certchain)
 		goto close_file;
 	}
 
-	err = tls_cert_from_certificate_list(data, st.st_size, certchain);
+	err = tls_parse_certificate_list(data, st.st_size, certchain);
 	if (err < 0)
 		fprintf(stderr, "Could not parse certificate list: %s\n",
 						strerror(-err));
@@ -95,8 +95,8 @@ static void usage(const char *bin)
 int main(int argc, char *argv[])
 {
 	int status = EXIT_FAILURE;
-	struct tls_cert *certchain;
-	struct tls_cert *ca_cert;
+	struct l_certchain *certchain;
+	struct l_cert *ca_cert;
 	int err;
 
 	if (argc != 3) {
@@ -122,7 +122,7 @@ int main(int argc, char *argv[])
 		goto free_certchain;
 	}
 
-	if (!tls_cert_verify_certchain(certchain, ca_cert)) {
+	if (!l_certchain_verify(certchain, ca_cert)) {
 		fprintf(stderr, "Verification failed\n");
 		goto free_cacert;
 	}
@@ -131,9 +131,9 @@ int main(int argc, char *argv[])
 	status = EXIT_SUCCESS;
 
 free_cacert:
-	l_free(ca_cert);
+	l_cert_free(ca_cert);
 free_certchain:
-	tls_cert_free_certchain(certchain);
+	l_certchain_free(certchain);
 done:
 	return status;
 }
Signature · 1 line
2.19.1
#6 lore raw

PATCH 7/7 unit: Update tests to new certificate API

In reply to Andrew Zaborowski
---
 unit/test-key.c | 76 ++++++++++++++++++++++---------------------------
 unit/test-tls.c | 27 +++++++++---------
 2 files changed, 48 insertions(+), 55 deletions(-)
unit/test-key.c +34 -42
index afe4103..ba27e19 100644
@@ -28,6 +28,8 @@
 
 #include <ell/ell.h>
 
+#include "ell/tls-private.h"
+
 #define KEY1_STR "This key has exactly _32_ bytes!"
 #define KEY1_LEN (strlen(KEY1_STR))
 #define KEY2_STR "This key is longer than 32 bytes, just to be different."
@@ -394,23 +396,20 @@ static void test_trusted_keyring(const void *data)
 {
 	struct l_keyring *ring;
 	struct l_keyring *trust;
-	uint8_t *cacert;
-	size_t cacertlen;
-	uint8_t *cert;
-	size_t certlen;
+	struct l_cert *cacert;
+	struct l_cert *cert;
 	struct l_key *cakey;
 	struct l_key *key;
 	bool success;
 
-	cacert = l_pem_load_certificate(CERTDIR "cert-ca.pem", &cacertlen);
+	cacert = tls_cert_load_file(CERTDIR "cert-ca.pem");
 	assert(cacert);
-	cert = l_pem_load_certificate(CERTDIR "cert-server.pem",
-					&certlen);
+	cert = tls_cert_load_file(CERTDIR "cert-server.pem");
 	assert(cert);
 
-	cakey = l_key_new(L_KEY_RSA, cacert, cacertlen);
+	cakey = l_cert_get_pubkey(cacert);
 	assert(cakey);
-	key = l_key_new(L_KEY_RSA, cert, certlen);
+	key = l_cert_get_pubkey(cert);
 	assert(key);
 
 	trust = l_keyring_new();
@@ -431,39 +430,34 @@ static void test_trusted_keyring(const void *data)
 	l_keyring_free(ring);
 	l_key_free(cakey);
 	l_key_free(key);
-	l_free(cacert);
-	l_free(cert);
+	l_cert_free(cacert);
+	l_cert_free(cert);
 }
 
 static void test_trust_chain(const void *data)
 {
 	struct l_keyring *ring;
 	struct l_keyring *trust;
-	uint8_t *cacert;
-	size_t cacertlen;
-	uint8_t *intcert;
-	size_t intcertlen;
-	uint8_t *cert;
-	size_t certlen;
+	struct l_cert *cacert;
+	struct l_cert *intcert;
+	struct l_cert *cert;
 	struct l_key *cakey;
 	struct l_key *intkey;
 	struct l_key *key;
 	bool success;
 
-	cacert = l_pem_load_certificate(CERTDIR "cert-ca.pem", &cacertlen);
+	cacert = tls_cert_load_file(CERTDIR "cert-ca.pem");
 	assert(cacert);
-	intcert = l_pem_load_certificate(CERTDIR "cert-intca.pem",
-						&intcertlen);
+	intcert = tls_cert_load_file(CERTDIR "cert-intca.pem");
 	assert(intcert);
-	cert = l_pem_load_certificate(CERTDIR "cert-entity-int.pem",
-					&certlen);
+	cert = tls_cert_load_file(CERTDIR "cert-entity-int.pem");
 	assert(cert);
 
-	cakey = l_key_new(L_KEY_RSA, cacert, cacertlen);
+	cakey = l_cert_get_pubkey(cacert);
 	assert(cakey);
-	intkey = l_key_new(L_KEY_RSA, intcert, intcertlen);
+	intkey = l_cert_get_pubkey(intcert);
 	assert(intkey);
-	key = l_key_new(L_KEY_RSA, cert, certlen);
+	key = l_cert_get_pubkey(cert);
 	assert(key);
 
 	trust = l_keyring_new();
@@ -492,17 +486,16 @@ static void test_trust_chain(const void *data)
 	l_key_free(cakey);
 	l_key_free(intkey);
 	l_key_free(key);
-	l_free(cacert);
-	l_free(intcert);
-	l_free(cert);
+	l_cert_free(cacert);
+	l_cert_free(intcert);
+	l_cert_free(cert);
 }
 
 static void test_key_crypto(const void *data)
 {
-	uint8_t *cert;
-	size_t certlen;
-	uint8_t *pubcert;
-	size_t pubcertlen;
+	uint8_t *privkey;
+	size_t privkeylen;
+	struct l_cert *cert;
 	struct l_key *key;
 	struct l_key *pubkey;
 	bool is_public;
@@ -514,18 +507,17 @@ static void test_key_crypto(const void *data)
 	int hash = L_CHECKSUM_NONE;
 	int rsa = L_KEY_RSA_PKCS1_V1_5;
 
-	cert = l_pem_load_private_key(CERTDIR "cert-client-key-pkcs8.pem",
-					NULL, NULL, &certlen);
+	privkey = l_pem_load_private_key(CERTDIR "cert-client-key-pkcs8.pem",
+						NULL, NULL, &privkeylen);
+	assert(privkey);
+	cert = tls_cert_load_file(CERTDIR "cert-client.pem");
 	assert(cert);
-	pubcert = l_pem_load_certificate(CERTDIR "cert-client.pem",
-						&pubcertlen);
-	assert(pubcert);
 
-	key = l_key_new(L_KEY_RSA, cert, certlen);
-	assert(key);
-	pubkey = l_key_new(L_KEY_RSA, pubcert, pubcertlen);
+	pubkey = l_cert_get_pubkey(cert);
 	assert(pubkey);
 
+	key = l_key_new(L_KEY_RSA, privkey, privkeylen);
+	assert(key);
 	success = l_key_get_info(key, rsa, hash, &keybits, &is_public);
 	assert(success);
 	assert(keybits == 2048);
@@ -606,8 +598,8 @@ static void test_key_crypto(const void *data)
 
 	l_key_free(key);
 	l_key_free(pubkey);
-	l_free(cert);
-	l_free(pubcert);
+	l_free(privkey);
+	l_cert_free(cert);
 }
 
 int main(int argc, char *argv[])
unit/test-tls.c +14 -13
index 4ac7162..a3a8905 100644
@@ -29,9 +29,8 @@
 
 #include <ell/ell.h>
 
-#include "ell/cipher.h"
-#include "ell/checksum.h"
 #include "ell/tls-private.h"
+#include "ell/cert-private.h"
 
 static void test_tls10_prf(const void *data)
 {
@@ -213,12 +212,14 @@ static void test_tls12_prf(const void *data)
 
 static void test_certificates(const void *data)
 {
-	struct tls_cert *cert;
-	struct tls_cert *cacert;
-	struct tls_cert *wrongca;
+	struct l_cert *cert;
+	struct l_cert *cacert;
+	struct l_cert *wrongca;
+	struct l_certchain *chain;
 
 	cert = tls_cert_load_file(CERTDIR "cert-server.pem");
 	assert(cert);
+	chain = certchain_new_from_leaf(cert);
 
 	cacert = tls_cert_load_file(CERTDIR "cert-ca.pem");
 	assert(cacert);
@@ -226,15 +227,15 @@ static void test_certificates(const void *data)
 	wrongca = tls_cert_load_file(CERTDIR "cert-intca.pem");
 	assert(wrongca);
 
-	assert(!tls_cert_verify_certchain(cert, wrongca));
+	assert(!l_certchain_verify(chain, wrongca));
 
-	assert(tls_cert_verify_certchain(cert, cacert));
+	assert(l_certchain_verify(chain, cacert));
 
-	assert(tls_cert_verify_certchain(cert, NULL));
+	assert(l_certchain_verify(chain, NULL));
 
-	l_free(cert);
-	l_free(cacert);
-	l_free(wrongca);
+	l_certchain_free(chain);
+	l_cert_free(cacert);
+	l_cert_free(wrongca);
 }
 
 struct tls_conn_test {
@@ -418,8 +419,8 @@ static void test_tls_test(const void *data)
 					test->client_key_path,
 					test->client_key_passphrase);
 	assert(auth_ok);
-	l_tls_set_cacert(s[0].tls, test->server_ca_cert_path);
-	l_tls_set_cacert(s[1].tls, test->client_ca_cert_path);
+	assert(l_tls_set_cacert(s[0].tls, test->server_ca_cert_path));
+	assert(l_tls_set_cacert(s[1].tls, test->client_ca_cert_path));
 
 	while (1) {
 		if (s[0].raw_buf_len) {
Signature · 1 line
2.19.1
#7 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 6 lines · Andrew Zaborowski

I'm going to be using asn1_parse_definite_length outside of the local functions so add the missing length validation.

---
  ell/asn1-private.h | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)

All 7 applied, thanks.

Regards,
-Denis