pem: New certificate loading functions
PATCH 1/16 pem: New certificate loading functions
Add l_pem_load_certificate_chain and l_pem_load_certificate_list that load certificate chains and lists of certificates such as those generated by openssl and return an l_certchain and an l_queue respectively.
--- ell/pem.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++-------- ell/pem.h | 5 ++ 2 files changed, 123 insertions(+), 19 deletions(-)
ell/pem.c +118 -19
@@ -39,6 +39,7 @@#include "asn1-private.h" #include "pkcs5-private.h" #include "cipher.h" +#include "cert-private.h" #define PEM_START_BOUNDARY "-----BEGIN "#define PEM_END_BOUNDARY "-----END "
@@ -119,7 +120,8 @@ static bool is_end_boundary(const uint8_t *buf, size_t buf_len, }static uint8_t *pem_load_buffer (const uint8_t *buf,size_t buf_len,int index, -char **type_label,size_t *len) +char **type_label,size_t *len, +const uint8_t **endp) {const uint8_t *base64_data =NULL , *label =NULL , *eol;uint8_t *data;
@@ -152,6 +154,9 @@ static uint8_t *pem_load_buffer(const uint8_t *buf, size_t buf_len, int index, *type_label =l_strndup ((const char *) label, label_len); +if (endp) + *endp = eol +1 ; +return data; }
@@ -171,6 +176,10 @@ static uint8_t *pem_load_buffer(const uint8_t *buf, size_t buf_len, int index, } } +/* If we found no label signal EOF rather than parse error */ +if (!base64_data && endp) + *endp =NULL ; +return NULL ; }
@@ -178,38 +187,58 @@ LIB_EXPORT uint8_t *l_pem_load_buffer(const uint8_t *buf, size_t buf_len, int index, char **type_label, size_t *out_len) { - return pem_load_buffer(buf, buf_len, index, type_label, out_len); + return pem_load_buffer(buf, buf_len, index, type_label, out_len, NULL); } -LIB_EXPORT uint8_t *l_pem_load_file(const char *filename, int index, -char **type_label,size_t *len) -{ +struct pem_file_info {int fd;struct stat st; -uint8_t *data, *result; +uint8_t *data; +}; - fd =open (filename,O_RDONLY ); -if (fd <0 ) -return NULL ; +static int pem_file_open (struct pem_file_info *info,const char *filename) +{ + info->fd =open (filename,O_RDONLY ); +if (info->fd <0 ) +return -errno; -if (fstat (fd, &st) <0 ) { -close (fd); +if (fstat (info->fd, &info->st) <0 ) { +int r = -errno; -return NULL ; +close (info->fd); +return r; } - data =mmap (NULL , st.st_size,PROT_READ ,MAP_SHARED , fd,0 ); -if (data ==MAP_FAILED ) { -close (fd); + info->data =mmap (NULL , info->st.st_size, +PROT_READ ,MAP_SHARED , info->fd,0 ); +if (info->data ==MAP_FAILED ) { +int r = -errno; -return NULL ; +close (info->fd); +return r; } - result =pem_load_buffer (data, st.st_size, index, type_label, len); +return 0 ; +} + +static void pem_file_close (struct pem_file_info *info) +{ +munmap (info->data, info->st.st_size); +close (info->fd); +} + +LIB_EXPORT uint8_t *l_pem_load_file(const char *filename, int index, +char **type_label,size_t *len) +{ +struct pem_file_info file; +uint8_t *result; -munmap (data, st.st_size); -close (fd); +if (pem_file_open (&file, filename) <0 ) +return NULL ; + result =pem_load_buffer (file.data, file.st.st_size, index, + type_label, len,NULL ); +pem_file_close (&file);return result; }
@@ -233,6 +262,76 @@ LIB_EXPORT uint8_t *l_pem_load_certificate(const char *filename, size_t *len)return content; } +LIB_EXPORT struct l_certchain *l_pem_load_certificate_chain( +const char *filename) +{ +struct l_queue *list =l_pem_load_certificate_list (filename); +struct l_certchain *chain; + +if (!list) +return NULL ; + + chain =certchain_new_from_leaf (l_queue_pop_head (list)); + +while (!l_queue_isempty (list)) +certchain_link_issuer (chain,l_queue_pop_head (list)); + +l_queue_destroy (list,NULL ); +return chain; +} + +LIB_EXPORT struct l_queue *l_pem_load_certificate_list(const char *filename) +{ +struct pem_file_info file; +const uint8_t *ptr, *end; +struct l_queue *list =NULL ; + +if (pem_file_open (&file, filename) <0 ) +return NULL ; + + ptr = file.data; + end = file.data + file.st.st_size; + +while (ptr && ptr < end) { +uint8_t *der; +size_t der_len; +char *label; +struct l_cert *cert; + + der =pem_load_buffer (ptr, end - ptr,0 , &label, &der_len, &ptr); + +if (!der ||strcmp (label,"CERTIFICATE" )) { +l_free (der); +l_free (label); + +if (!ptr)/* EOF */ +break ; +else + goto error; + } + +l_free (label); + cert =l_cert_new_from_der (der, der_len); +l_free (der); + +if (!cert) + goto error; + +if (!list) + list =l_queue_new (); + +l_queue_push_tail (list, cert); + } + +pem_file_close (&file); +return list; + +error: +l_queue_destroy (list, (l_queue_destroy_func_t ) l_cert_free); +pem_file_close (&file); +return NULL ; +} +/** * l_pem_load_private_key * @filename: path string to the PEM file to load
ell/pem.h +5 -0
@@ -27,12 +27,17 @@extern "C" {#endif +#include <ell/cert.h> +#include <ell/queue.h> +uint8_t *l_pem_load_buffer (const uint8_t *buf,size_t buf_len,int index,char **type_label,size_t *out_len);uint8_t *l_pem_load_file (const char *filename,int index,char **type_label,size_t *len);uint8_t *l_pem_load_certificate (const char *filename,size_t *len); +struct l_certchain *l_pem_load_certificate_chain (const char *filename); +struct l_queue *l_pem_load_certificate_list (const char *filename);uint8_t *l_pem_load_private_key (const char *filename,const char *passphrase,bool *encrypted,size_t *len);
Signature · 1 line
2.19.1
PATCH 2/16 cert: Accept lists of CA certificates instead of one
In l_certchain_find and l_certchain_verify accept an l_queue with potentially more than one CA certifcate so the user doesn't need to call these functions multiple times if multiple CAs are configured.
--- ell/cert.c | 91 +++++++++++++++++++++++++++++++++++------------------- ell/cert.h | 6 ++-- 2 files changed, 63 insertions(+), 34 deletions(-)
ell/cert.c +59 -32
@@ -285,20 +285,20 @@ LIB_EXPORT bool l_certchain_foreach_from_ca(struct l_certchain *chain, } LIB_EXPORT bool l_certchain_find(struct l_certchain *chain, -struct l_cert *ca_cert) +struct l_queue *ca_certs) {if (unlikely (!chain || !chain->leaf))return false;/* Nothing to do if no CA certificates supplied */ -if (!ca_cert) +if (!ca_certs)return true;/* * Also nothing to do if the user already supplied a working * certificate chain. */ -if (l_certchain_verify (chain, ca_cert)) +if (l_certchain_verify (chain, ca_certs))return true;/* Actual search for a chain to the CA cert is unimplemented, fail */
@@ -312,7 +312,7 @@ static void cert_key_cleanup(struct l_key **p)static bool certchain_verify_with_keyring (struct l_cert *cert,struct l_keyring *ring, -struct l_cert *root, +struct l_queue *roots,struct l_keyring *trusted) {if (!cert)
@@ -328,12 +328,30 @@ static bool certchain_verify_with_keyring(struct l_cert *cert,* 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." +* +* This is an optimization to skip adding the root cert if +* it's already in the trusted keyring. It also happens to +* work around a kernel issue preventing self-signed certificates +* missing the AKID extension from being linked. +* +* If 'roots' were supplied we assume the keyring is already +* restricted. */ -if (!cert->issuer && root && cert->asn1_len == root->asn1_len && - !memcmp (cert->asn1, root->asn1, root->asn1_len)) -return true; +if (!cert->issuer && roots) { +const struct l_queue_entry *entry; + +for (entry =l_queue_get_entries (roots); entry; + entry = entry->next) { +struct l_cert *root = entry->data; + +if (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)) { +if (certchain_verify_with_keyring (cert->issuer, ring, roots, trusted)) { L_AUTO_CLEANUP_VAR(struct l_key *, key, cert_key_cleanup); key =l_key_new (L_KEY_RSA , cert->asn1, cert->asn1_len);
@@ -371,7 +389,7 @@ static void cert_keyring_cleanup(struct l_keyring **p) } LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain, -struct l_cert *ca_cert) +struct l_queue *ca_certs) { L_AUTO_CLEANUP_VAR(struct l_keyring *, ca_ring, cert_keyring_cleanup) = NULL;
@@ -381,39 +399,48 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,if (unlikely (!chain || !chain->leaf))return false; -if (ca_cert && ca_cert->pubkey_type !=L_CERT_KEY_RSA ) + verify_ring =l_keyring_new (); +if (!verify_ring)return false; -if (ca_cert) { - L_AUTO_CLEANUP_VAR(struct l_key *, ca_key, cert_key_cleanup); - ca_key =NULL ; +/* +* If CA cert(s) were supplied, restrict verify_ring now so +* everything else in certchain is validated against the CA(s). +* Otherwise, verify_ring will be restricted after the root of +* certchain is added to verify_ring by +* certchain_verify_with_keyring(). +*/ +if (ca_certs) { +const struct l_queue_entry *entry; 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; - } +for (entry =l_queue_get_entries (ca_certs); entry; + entry = entry->next) { +struct l_cert *ca = entry->data; +struct l_key *ca_key; - verify_ring =l_keyring_new (); -if (!verify_ring) -return false; +if (ca->pubkey_type !=L_CERT_KEY_RSA ) +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; + ca_key =l_cert_get_pubkey (ca); +if (!ca_key) +return false; + +if (!l_keyring_link (ca_ring, ca_key)) { +l_key_free (ca_key); +return false; + } + } + +if (!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, +return certchain_verify_with_keyring (chain->leaf, verify_ring, ca_certs, ca_ring); }
ell/cert.h +4 -2
@@ -29,6 +29,8 @@ extern "C" {#include <stddef.h> +#include <ell/queue.h> +struct l_cert ;struct l_certchain ;
@@ -57,8 +59,8 @@ 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); +bool l_certchain_find (struct l_certchain *chain,struct l_queue *ca_certs); +bool l_certchain_verify (struct l_certchain *chain,struct l_queue *ca_certs); #ifdef __cplusplus }
Signature · 1 line
2.19.1
PATCH 3/16 tls: Support loading multiple root CAs
In reply to Andrew ZaborowskiIf a file with multiple concatenated certificates is supplied to set_cecert, load all of them and verify received certificate chains aginst any of them instead of just the first one. This is also what wpa_supplicant does for files supplied with the ca_cert= setting and it's actually useful. Some enterprise CAs have multiple root certificates and provision clients with a file that contains all of them concatenated and it does happen that they switch from a certificate chain using one root to a new chain with a different root for their wifi APs in which case our network config files break while wpa_supplicant configs keep working.
--- ell/tls-private.h | 2 +- ell/tls.c | 35 ++++++++++++++++++----------------- ell/tls.h | 2 +- 3 files changed, 20 insertions(+), 19 deletions(-)
ell/tls-private.h +1 -1
@@ -145,7 +145,7 @@ struct l_tls {l_tls_destroy_cb_t debug_destroy;void *debug_data; -struct l_cert *ca_cert; +struct l_queue *ca_certs;struct l_certchain *cert;struct l_key *priv_key;size_t priv_key_size;
ell/tls.c +18 -17
@@ -959,7 +959,7 @@ static bool tls_send_certificate(struct l_tls *tls)return false; } -if (tls->cert && !l_certchain_find (tls->cert, tls->ca_cert)) { +if (tls->cert && !l_certchain_find (tls->cert, tls->ca_certs)) {TLS_DISCONNECT (TLS_ALERT_INTERNAL_ERROR ,TLS_ALERT_UNKNOWN_CA ,"Can't find certificate chain to local " "CA cert" );
@@ -1657,14 +1657,14 @@ static void tls_handle_client_hello(struct l_tls *tls,/* TODO: don't bother if configured to not authenticate client */ if (tls->pending.cipher_suite->key_xchg->certificate_check && - tls->ca_cert) + tls->ca_certs)if (!tls_send_certificate_request (tls))return ;tls_send_server_hello_done (tls);if (tls->pending.cipher_suite->key_xchg->certificate_check && - tls->ca_cert) + tls->ca_certs)TLS_SET_STATE (TLS_HANDSHAKE_WAIT_CERTIFICATE );else TLS_SET_STATE (TLS_HANDSHAKE_WAIT_KEY_EXCHANGE );
@@ -1824,13 +1824,13 @@ static void tls_handle_certificate(struct l_tls *tls,/* * Validate the certificate chain's consistency and validate it -* against our CA if we have any. +* against our CAs if we have any. */ -if (!l_certchain_verify (certchain, tls->ca_cert)) { +if (!l_certchain_verify (certchain, tls->ca_certs)) {TLS_DISCONNECT (TLS_ALERT_BAD_CERT ,0 ,"Peer certchain verification failed " -"consistency check%s" , - tls->ca_cert ?" or against local CA cert" :"" ); +"consistency check%s" , tls->ca_certs ? +" or against local CA certs" :"" ); goto done; }
@@ -2139,10 +2139,10 @@ static void tls_handle_certificate_verify(struct l_tls *tls,* - If we received an (expected) Certificate Verify, we must have * sent a Certificate Request. * - If we sent a Certificate Request that's because -* tls->ca_cert is non-NULL. -* - If tls->ca_cert is non-NULL then tls_handle_certificate +* tls->ca_certs is non-NULL. +* - If tls->ca_certs is non-NULL then tls_handle_certificate * will have checked the whole certificate chain to be valid and -* additionally trusted by our CA if known. +* additionally trusted by our CAs if known. * - Additionally cipher_suite->key_xchg->verify has just confirmed * that the peer owns the end-entity certificate because it was * able to sign the contents of the handshake messages and that
@@ -2352,7 +2352,7 @@ static void tls_handle_handshake(struct l_tls *tls, int type,/* * On the client, the server's certificate is only now * verified, based on the following logic: -* - tls->ca_cert is non-NULL so tls_handle_certificate +* - tls->ca_certs is non-NULL so tls_handle_certificate * (always called on the client) must have veritifed the * server's certificate chain to be valid and additionally * trusted by our CA.
@@ -2365,7 +2365,7 @@ static void tls_handle_handshake(struct l_tls *tls, int type,* message (either should be enough). */ if (!tls->server && tls->cipher_suite[0 ]->key_xchg-> - certificate_check && tls->ca_cert) + certificate_check && tls->ca_certs) tls->peer_authenticated = true;tls_finished (tls);
@@ -2598,9 +2598,10 @@ LIB_EXPORT bool l_tls_set_cacert(struct l_tls *tls, const char *ca_cert_path) {TLS_DEBUG ("ca-cert-path=%s" , ca_cert_path); -if (tls->ca_cert) { -l_cert_free (tls->ca_cert); - tls->ca_cert =NULL ; +if (tls->ca_certs) { +l_queue_destroy (tls->ca_certs, + (l_queue_destroy_func_t ) l_cert_free); + tls->ca_certs =NULL ; }if (ca_cert_path) {
@@ -2610,8 +2611,8 @@ LIB_EXPORT bool l_tls_set_cacert(struct l_tls *tls, const char *ca_cert_path)return false; } - tls->ca_cert =tls_cert_load_file (ca_cert_path); -if (!tls->ca_cert) { + tls->ca_certs =l_pem_load_certificate_list (ca_cert_path); +if (!tls->ca_certs) {TLS_DEBUG ("Error loading %s" , ca_cert_path);return false; }
ell/tls.h +1 -1
@@ -86,7 +86,7 @@ void l_tls_write(struct l_tls *tls, const uint8_t *data, size_t len);/* Submit TLS payload from underlying transport to be decrypted */ void l_tls_handle_rx (struct l_tls *tls,const uint8_t *data,size_t len); -/* If peer is to be authenticated, supply the CA certificate */ +/* If peer is to be authenticated, supply the CA certificates */ bool l_tls_set_cacert (struct l_tls *tls,const char *ca_cert_path);/*
Signature · 1 line
2.19.1
PATCH 4/16 tls: Load local cert using l_pem_load_certificate_chain
In reply to Andrew ZaborowskiThis will allow to load the whole chain instead of just the first certificate in the file, if a certificate chain file is supplied although that's optional. The certificate chain file is just a concatenation of PEM structures and the first one is the end-entity certificate so it is backwards compatible. If a CA certificate is set with l_tls_set_cacert it need to also be the last certificate in the file or the issuer of the last certificate.
Until now things worked because the certificate supplied can be directly signed by the supplied CA certificate and the two certificates then form the full chain but longer chains need to also be supported.
With this tls_cert_load_file is only used by the unit tests. tls_cert_load_file is the only user of <ell/cert-private.h> in tls.c and also the only user of l_pem_load_certificate.
--- ell/tls.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
ell/tls.c +2 -5
@@ -2640,14 +2640,11 @@ LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path, }if (cert_path) { -struct l_cert *cert =tls_cert_load_file (cert_path); - -if (!cert) { + tls->cert =l_pem_load_certificate_chain (cert_path); +if (!tls->cert) {TLS_DEBUG ("Error loading %s" , cert_path);return false; } - - tls->cert =certchain_new_from_leaf (cert); }if (priv_key_path) {
Signature · 1 line
2.19.1
PATCH 5/16 pem: Return an l_key from l_pem_load_private_key
In reply to Andrew ZaborowskiChange l_pem_load_private_key to directly return the l_key object instead of the byte buffer (and length) so there's no need to call the l_key constructor seprately.
--- ell/pem.c | 36 ++++++++++++++++++++++-------------- ell/pem.h | 6 ++++-- ell/tls.c | 18 +++++------------- 3 files changed, 31 insertions(+), 29 deletions(-)
ell/pem.c +22 -14
@@ -337,7 +337,6 @@ error:* @filename: path string to the PEM file to load * @passphrase: private key encryption passphrase or NULL for unencrypted * @encrypted: receives indication whether the file was encrypted if non-NULL -* @len: receives the length of the returned buffer * * Load the PEM encoded RSA Private Key file at @filename. If it is an * encrypted private key and @passphrase was non-NULL, the file is
@@ -346,20 +345,22 @@ error:* success case and on error when NULL is returned. This can be used to * check if a passphrase is required without prior information. * -* Returns: Buffer containing raw DER data for the private key or NULL, to -* be freed with l_free. +* Returns: An l_key object to be freed with an l_key_free* function, +* or NULL. **/ -LIB_EXPORT uint8_t *l_pem_load_private_key(const char *filename, +LIB_EXPORT struct l_key *l_pem_load_private_key(const char *filename,const char *passphrase, -bool *encrypted,size_t *len) +bool *encrypted) {uint8_t *content;char *label; +size_t len; +struct l_key *pkey =NULL ;if (encrypted) *encrypted = false; - content =l_pem_load_file (filename,0 , &label, len); + content =l_pem_load_file (filename,0 , &label, &len);if (!content)return NULL ;
@@ -388,7 +389,7 @@ LIB_EXPORT uint8_t *l_pem_load_private_key(const char *filename, goto err;/* Technically this is BER, not limited to DER */ - key_info =asn1_der_find_elem (content, *len,0 , &tag, + key_info =asn1_der_find_elem (content, len,0 , &tag, &key_info_len);if (!key_info || tag !=ASN1_ID_SEQUENCE ) goto err;
@@ -404,7 +405,7 @@ LIB_EXPORT uint8_t *l_pem_load_private_key(const char *filename, (data_len & 7) != 0) goto err; -if (asn1_der_find_elem (content, *len,2 , &tag, &tmp_len)) +if (asn1_der_find_elem (content, len,2 , &tag, &tmp_len)) goto err; alg =pkcs5_cipher_from_alg_id (alg_id, alg_id_len, passphrase);
@@ -420,8 +421,10 @@ LIB_EXPORT uint8_t *l_pem_load_private_key(const char *filename, }l_cipher_free (alg); +memset (content,0 , len);l_free (content); content = decrypted; + len = data_len;/* * Strip padding as defined in RFC8018 (for PKCS#5 v1) or
@@ -436,7 +439,7 @@ LIB_EXPORT uint8_t *l_pem_load_private_key(const char *filename,if (content[data_len -1 - i] != content[data_len -1 ]) goto err; - *len = data_len - content[data_len -1 ]; + len = data_len - content[data_len -1 ]; goto done; }
@@ -449,12 +452,17 @@ LIB_EXPORT uint8_t *l_pem_load_private_key(const char *filename,*/ /* Label not known */ -err: -l_free (content); - content =NULL ; + goto err; done: -l_free (label); + pkey =l_key_new (L_KEY_RSA , content, len); -return content; +err: +if (content) { +memset (content,0 , len); +l_free (content); + } + +l_free (label); +return pkey; }
ell/pem.h +4 -2
@@ -27,6 +27,7 @@extern "C" {#endif +#include <ell/key.h> #include <ell/cert.h> #include <ell/queue.h>
@@ -39,8 +40,9 @@ uint8_t *l_pem_load_certificate(const char *filename, size_t *len);struct l_certchain *l_pem_load_certificate_chain (const char *filename);struct l_queue *l_pem_load_certificate_list (const char *filename); -uint8_t *l_pem_load_private_key (const char *filename,const char *passphrase, -bool *encrypted,size_t *len); +struct l_key *l_pem_load_private_key (const char *filename, +const char *passphrase, +bool *encrypted); #ifdef __cplusplus }
ell/tls.c +5 -13
@@ -2648,23 +2648,15 @@ LIB_EXPORT bool l_tls_set_auth_data(struct l_tls *tls, const char *cert_path, } if (priv_key_path) { - uint8_t *priv_key; bool is_public = true; - priv_key = l_pem_load_private_key(priv_key_path, + tls->priv_key = l_pem_load_private_key(priv_key_path, priv_key_passphrase, - NULL, - &tls->priv_key_size); - TLS_DEBUG("l_pem_load_private_key returned %p, size %zi", - priv_key, tls->priv_key_size); - - if (!priv_key) + NULL); + if (!tls->priv_key) { + TLS_DEBUG("Error loading %s", priv_key_path); return false; - - tls->priv_key = l_key_new(L_KEY_RSA, priv_key, - tls->priv_key_size); - memset(priv_key, 0, tls->priv_key_size); - l_free(priv_key); + } if (!l_key_get_info(tls->priv_key, L_KEY_RSA_PKCS1_V1_5, L_CHECKSUM_NONE, &tls->priv_key_size,
Signature · 1 line
2.19.1
PATCH 6/16 tls: Check cipher suites against ceritficate key type
In reply to Andrew ZaborowskiCheck that the key exchange mechanism in the cipher suite is compatible with the supplied certificate if any, in tls_cipher_suite_is_compatible.
--- ell/tls.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
ell/tls.c +13 -1
@@ -519,6 +519,7 @@ static bool tls_cipher_suite_is_compatible(struct l_tls *tls, const char **error) {static char error_buf[200 ]; +struct l_cert *leaf;if (suite->encryption && suite->encryption->cipher_type ==TLS_CIPHER_AEAD ) {
@@ -591,7 +592,18 @@ static bool tls_cipher_suite_is_compatible(struct l_tls *tls,return false; } -/* TODO: Check suite->key_xchg->validate_cert_key_type(tls->cert) */ + leaf =l_certchain_get_leaf (tls->cert); +if (leaf && !suite->key_xchg->validate_cert_key_type (leaf)) { +if (error) { + *error = error_buf; +snprintf (error_buf,sizeof (error_buf), +"Local certificate has key type " +"incompatible with cipher suite %s's " +"key xchg mechanism" , suite->name); + } + +return false; + }return true; }
Signature · 1 line
2.19.1
PATCH 7/16 asn1: Implement asn1_write_definite_length
In reply to Andrew Zaborowski--- ell/asn1-private.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
ell/asn1-private.h +16 -0
@@ -68,6 +68,22 @@ static inline int asn1_parse_definite_length(const uint8_t **buf,return result; } +static inline void asn1_write_definite_length (uint8_t **buf,size_t len) +{ +int n; + +if (len <0x80 ) { + *(*buf)++ = len; +return ; + } + +for (n =1 ; len >> (n *8 ); n++); + *(*buf)++ =0x80 | n; + +while (n--) + *(*buf)++ = len >> (n *8 ); +} +/* Return index'th element in a DER SEQUENCE */ static inline const uint8_t *asn1_der_find_elem(const uint8_t *buf, size_t len_in, int index,
Signature · 1 line
2.19.1
PATCH 8/16 cert: Fix l_cert_get_dn
In reply to Andrew ZaborowskiThe DistinguishedName type is a SEQUENCE, this was a copy&paste error.
--- ell/cert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
ell/cert.c +1 -1
@@ -162,7 +162,7 @@ LIB_EXPORT const uint8_t *l_cert_get_dn(struct l_cert *cert, size_t *out_len)return NULL ; return asn1_der_find_elem_by_path(cert->asn1, cert->asn1_len, - ASN1_ID_OID, out_len, + ASN1_ID_SEQUENCE, out_len, X509_CERTIFICATE_POS, X509_TBSCERTIFICATE_POS, X509_TBSCERT_SUBJECT_DN_POS,
Signature · 1 line
2.19.1
PATCH 9/16 tls: Send the DistinguishedName in Certificate Request
In reply to Andrew ZaborowskiNow that we have l_cert_get_dn we can fill in the DistinguishedNames fields in the Certificate Request TLS message.
--- ell/tls.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-)
ell/tls.c +35 -3
@@ -39,6 +39,7 @@#include "cert-private.h" #include "tls-private.h" #include "key.h" +#include "asn1-private.h" void tls10_prf(const void *secret, size_t secret_len, const char *label,
@@ -1050,10 +1051,22 @@ static struct tls_signature_hash_algorithms tls_signature_hash_pref[] = {static bool tls_send_certificate_request (struct l_tls *tls) {uint8_t *buf, *ptr, *dn_ptr, *signature_hash_ptr; +const struct l_queue_entry *entry;unsigned int i; +size_t dn_total =0 ; + +for (entry =l_queue_get_entries (tls->ca_certs); entry; + entry = entry->next) { +struct l_cert *ca_cert = entry->data; +size_t dn_size; + +if (l_cert_get_dn (ca_cert, &dn_size)) + dn_total +=10 + dn_size; + } buf =l_malloc (128 +L_ARRAY_SIZE (tls_cert_type_pref) + -2 *L_ARRAY_SIZE (tls_signature_hash_pref)); +2 *L_ARRAY_SIZE (tls_signature_hash_pref) + + dn_total); ptr = buf +TLS_HANDSHAKE_HEADER_SIZE ;/* Fill in the Certificate Request body */
@@ -1086,8 +1099,27 @@ static bool tls_send_certificate_request(struct l_tls *tls) } dn_ptr = ptr; - ptr +=2 ;/* Leave space for sizes */ -l_put_be16 (0 , dn_ptr);/* DistinguishedNames size */ + ptr +=2 ;/* Leave space for the total DN size */ + +for (entry =l_queue_get_entries (tls->ca_certs); entry; + entry = entry->next) { +struct l_cert *ca_cert = entry->data; +size_t dn_size; +const uint8_t *dn =l_cert_get_dn (ca_cert, &dn_size); +uint8_t *cur_dn_ptr = ptr; + +if (!dn) +continue ; + + ptr +=2 ;/* Leave space for current DN size */ + *ptr++ =ASN1_ID_SEQUENCE ;/* DER outer SEQUENCE tag */ +asn1_write_definite_length (&ptr, dn_size);/* length */ +memcpy (ptr, dn, dn_size);/* value */ + ptr += dn_size; +l_put_be16 (ptr - cur_dn_ptr -2 , cur_dn_ptr); + } + +l_put_be16 (ptr - dn_ptr -2 , dn_ptr);/* DistinguishedNames size */ tls_tx_handshake (tls,TLS_CERTIFICATE_REQUEST , buf, ptr - buf);
Signature · 1 line
2.19.1
PATCH 10/16 tls: Flush remaining decoded messages on Alert
In reply to Andrew ZaborowskiWe previously started flushing the received raw records that we still had queued when one of the records being decoded generates an Alert message. But if there were multiple plaintext messages within one record, they'd still be processed after one message had generated an error, so also flush these too. This caused further Alert messages to be sent if the TLS state machine couldn't make sense of the new messages after it had reset its state due to the initial Alert.
Also simplify the record_flush logic in l_tls_handle_rx slightly.
--- ell/tls-record.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
ell/tls-record.c +4 -3
@@ -334,6 +334,9 @@ static bool tls_handle_plaintext(struct l_tls *tls, const uint8_t *plaintext, tls->message_buf_len =0 ; +if (tls->record_flush) +break ; +continue ; }
@@ -609,10 +612,8 @@ LIB_EXPORT void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data, tls->record_buf_len =0 ; need_len =5 ; -if (tls->record_flush) { - tls->record_flush = false; +if (tls->record_flush)break ; - } }if (!len)
Signature · 1 line
2.19.1
PATCH 11/16 examples: Ensure SIGPIPE is ignore in https examples
In reply to Andrew ZaborowskiPotential SIGPIPEs on a write to a socket that's been shut down would kill the example without letting it print any explanation to user.
--- examples/https-client-test.c | 2 +- examples/https-server-test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
examples/https-client-test.c +1 -1
@@ -86,7 +86,7 @@ static void https_tls_write(const uint8_t *data, size_t len, void *user_data)int r;while (len) { - r =write (l_io_get_fd (io), data, len); + r =send (l_io_get_fd (io), data, len,MSG_NOSIGNAL );if (r <0 ) {l_main_quit ();break ;
examples/https-server-test.c +1 -1
@@ -89,7 +89,7 @@ static void https_tls_write(const uint8_t *data, size_t len, void *user_data)int r;while (len) { - r =write (l_io_get_fd (io), data, len); + r =send (l_io_get_fd (io), data, len,MSG_NOSIGNAL );if (r <0 ) {l_main_quit ();break ;
Signature · 1 line
2.19.1
PATCH 12/16 examples: Fix check in https-server-test
In reply to Andrew Zaborowski--- examples/https-server-test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
examples/https-server-test.c +1 -1
@@ -174,7 +174,7 @@ int main(int argc, char *argv[]) auth_ok =l_tls_set_auth_data (tls, argv[1 ], argv[2 ], argv[3 ]);l_tls_set_cacert (tls, argc >4 ? argv[4 ] :NULL ); -if (tls && !auth_ok) +if (tls && auth_ok)l_main_run ();l_io_destroy (io);
Signature · 1 line
2.19.1
PATCH 13/16 examples: Check l_tls_set_cacert return value in https examples
In reply to Andrew Zaborowski--- examples/https-client-test.c | 9 ++++----- examples/https-server-test.c | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-)
examples/https-client-test.c +4 -5
@@ -124,7 +124,7 @@ int main(int argc, char *argv[])struct in_addr **addr_list;struct sockaddr_in addr;int fd; -bool auth_ok = true; +bool auth_ok;if (argc !=2 && argc !=3 && argc !=6 ) { printf("Usage: %s <https-host-name> [<ca-cert-path> "
@@ -180,10 +180,9 @@ int main(int argc, char *argv[])if (getenv ("TLS_DEBUG" ))l_tls_set_debug (tls, https_tls_debug_cb,NULL ,NULL ); -if (argc >2 ) -l_tls_set_cacert (tls, argv[2 ]); -if (argc >5 ) - auth_ok =l_tls_set_auth_data (tls, argv[3 ], argv[4 ], argv[5 ]); + auth_ok = (argc <=2 ||l_tls_set_cacert (tls, argv[2 ])) && + (argc <=5 || +l_tls_set_auth_data (tls, argv[3 ], argv[4 ], argv[5 ]));if (tls && auth_ok)l_main_run ();
examples/https-server-test.c +2 -2
@@ -171,8 +171,8 @@ int main(int argc, char *argv[])if (getenv ("TLS_DEBUG" ))l_tls_set_debug (tls, https_tls_debug_cb,NULL ,NULL ); - auth_ok =l_tls_set_auth_data (tls, argv[1 ], argv[2 ], argv[3 ]); -l_tls_set_cacert (tls, argc >4 ? argv[4 ] :NULL ); + auth_ok =l_tls_set_auth_data (tls, argv[1 ], argv[2 ], argv[3 ]) && + (argc <=4 ||l_tls_set_cacert (tls, argv[4 ]));if (tls && auth_ok)l_main_run ();
Signature · 1 line
2.19.1
PATCH 14/16 examples: Improve messages printed by https examples
In reply to Andrew ZaborowskiMake sure we print something in all situations that cause the end of the main loop. Print the "page served" message before l_tls_close to not imply that we sent the page after the TLS alert message. Use printf consistently to avoid potential confusion is messages were buffered separately by l_info.
--- examples/https-client-test.c | 4 +++- examples/https-server-test.c | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-)
examples/https-client-test.c +3 -1
@@ -115,7 +115,7 @@ static void https_tls_ready(const char *peer_identity, void *user_data)static void https_tls_debug_cb (const char *str,void *user_data) { -l_info ("%s" , str); +printf ("%s\n" , str); }int main (int argc,char *argv[])
@@ -186,6 +186,8 @@ int main(int argc, char *argv[])if (tls && auth_ok)l_main_run (); +else +printf ("TLS setup failed\n" );l_io_destroy (io);l_tls_free (tls);
examples/https-server-test.c +9 -4
@@ -52,9 +52,11 @@ static bool https_io_read(struct l_io *io, void *user_data)int l; l =read (l_io_get_fd (io), buf,sizeof (buf)); -if (l ==0 ) +if (l ==0 ) { +if (!served) +printf ("EOF before serving a page\n" );l_main_quit (); -else if (l >0 ) + }else if (l >0 )l_tls_handle_rx (tls, buf, l);return true;
@@ -78,9 +80,9 @@ static void https_new_data(const uint8_t *data, size_t len, void *user_data)if (len >=4 && !memcmp (data + len -4 ,"\r\n\r\n" ,4 )) {l_tls_write (tls, (void *) reply,strlen (reply)); -l_tls_close (tls); served = true;printf ("Hello world page served\n" ); +l_tls_close (tls); } }
@@ -91,6 +93,7 @@ static void https_tls_write(const uint8_t *data, size_t len, void *user_data)while (len) { r =send (l_io_get_fd (io), data, len,MSG_NOSIGNAL );if (r <0 ) { +printf ("send error\n" );l_main_quit ();break ; }
@@ -109,7 +112,7 @@ static void https_tls_ready(const char *peer_identity, void *user_data)static void https_tls_debug_cb (const char *str,void *user_data) { -l_info ("%s" , str); +printf ("%s\n" , str); }int main (int argc,char *argv[])
@@ -176,6 +179,8 @@ int main(int argc, char *argv[])if (tls && auth_ok)l_main_run (); +else +printf ("TLS setup failed\n" );l_io_destroy (io);l_tls_free (tls);
Signature · 1 line
2.19.1
PATCH 15/16 tools: Update certchain-verify's cert API usage
In reply to Andrew Zaborowski--- tools/certchain-verify.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
tools/certchain-verify.c +6 -6
@@ -96,7 +96,7 @@ int main(int argc, char *argv[]) {int status =EXIT_FAILURE ;struct l_certchain *certchain; -struct l_cert *ca_cert; +struct l_queue *ca_certs;int err;if (argc !=3 ) {
@@ -116,13 +116,13 @@ int main(int argc, char *argv[]) goto done; } - ca_cert =tls_cert_load_file (argv[1 ]); -if (!ca_cert) { -fprintf (stderr,"Unable to load CA certifiate\n" ); + ca_certs =l_pem_load_certificate_list (argv[1 ]); +if (!ca_certs) { +fprintf (stderr,"Unable to load CA certifiates\n" ); goto free_certchain; } -if (!l_certchain_verify (certchain, ca_cert)) { +if (!l_certchain_verify (certchain, ca_certs)) {fprintf (stderr,"Verification failed\n" ); goto free_cacert; }
@@ -131,7 +131,7 @@ int main(int argc, char *argv[]) status =EXIT_SUCCESS ; free_cacert: -l_cert_free (ca_cert); +l_queue_destroy (ca_certs, (l_queue_destroy_func_t ) l_cert_free); free_certchain:l_certchain_free (certchain); done:
Signature · 1 line
2.19.1
PATCH 16/16 unit: Update tests usage of certificate API
In reply to Andrew Zaborowski--- unit/test-key.c | 34 ++++++++++++---------------- unit/test-pem.c | 60 +++++++++++++++++++++++++++++-------------------- unit/test-tls.c | 16 ++++++------- 3 files changed, 58 insertions(+), 52 deletions(-)
unit/test-key.c +14 -20
@@ -493,10 +493,8 @@ static void test_trust_chain(const void *data)static void test_key_crypto (const void *data) { -uint8_t *privkey; -size_t privkeylen;struct l_cert *cert; -struct l_key *key; +struct l_key *privkey;struct l_key *pubkey;bool is_public;size_t keybits;
@@ -507,23 +505,21 @@ static void test_key_crypto(const void *data)int hash =L_CHECKSUM_NONE ;int rsa =L_KEY_RSA_PKCS1_V1_5 ; - 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); - pubkey =l_cert_get_pubkey (cert);assert (pubkey); +l_cert_free (cert); - key =l_key_new (L_KEY_RSA , privkey, privkeylen); -assert (key); - success =l_key_get_info (key, rsa, hash, &keybits, &is_public); + privkey =l_pem_load_private_key (CERTDIR "cert-client-key-pkcs8.pem" , +NULL ,NULL ); +assert (privkey); + success =l_key_get_info (privkey, rsa, hash, &keybits, &is_public);assert (success);assert (keybits ==2048 );assert (!is_public); - success =l_key_get_info (key, rsa,L_CHECKSUM_NONE , &keybits, + success =l_key_get_info (privkey, rsa,L_CHECKSUM_NONE , &keybits, &is_public);assert (success);assert (keybits ==2048 );
@@ -546,14 +542,14 @@ static void test_key_crypto(const void *data) sizeof(ciphertext), sizeof(decrypted));assert (len <0 ); - len =l_key_decrypt (key, rsa, hash, ciphertext, decrypted, + len =l_key_decrypt (privkey, rsa, hash, ciphertext, decrypted,sizeof (ciphertext),sizeof (decrypted));assert (len == (ssize_t )strlen (plaintext));assert (strcmp (plaintext, (char *)decrypted) ==0 );/* Decrypt reference ciphertext */ memset (decrypted,0 ,sizeof (decrypted)); - len =l_key_decrypt (key, rsa, hash, reference_ciphertext, decrypted, + len =l_key_decrypt (privkey, rsa, hash, reference_ciphertext, decrypted,sizeof (reference_ciphertext),sizeof (decrypted));assert (len == (ssize_t )strlen (plaintext));
@@ -563,7 +559,7 @@ static void test_key_crypto(const void *data)memset (decrypted,0 ,sizeof (decrypted));memcpy (ciphertext, reference_ciphertext,sizeof (ciphertext)); ciphertext[0 ] = ciphertext[0 ] ^ (uint8_t )0xFF ; - len =l_key_decrypt (key, rsa, hash, ciphertext, decrypted, + len =l_key_decrypt (privkey, rsa, hash, ciphertext, decrypted,sizeof (ciphertext),sizeof (decrypted));assert (len <0 );
@@ -573,7 +569,7 @@ static void test_key_crypto(const void *data) strlen(plaintext), sizeof(ciphertext));assert (len <0 ); - len =l_key_sign (key, rsa, hash, plaintext, ciphertext, + len =l_key_sign (privkey, rsa, hash, plaintext, ciphertext,strlen (plaintext),sizeof (ciphertext));assert (len ==sizeof (ciphertext));
@@ -581,7 +577,7 @@ static void test_key_crypto(const void *data) strlen(plaintext), sizeof(ciphertext));assert (success); - success =l_key_verify (key, rsa, hash, plaintext, ciphertext, + success =l_key_verify (privkey, rsa, hash, plaintext, ciphertext,strlen (plaintext),sizeof (ciphertext));assert (success);
@@ -592,14 +588,12 @@ static void test_key_crypto(const void *data)/* Corrupt signature */ ciphertext[42 ] = ciphertext[52 ] ^ (uint8_t )0xFF ; - success =l_key_verify (key, rsa, hash, plaintext, ciphertext, + success =l_key_verify (privkey, rsa, hash, plaintext, ciphertext,strlen (plaintext),sizeof (ciphertext));assert (!success); -l_key_free (key); +l_key_free (privkey);l_key_free (pubkey); -l_free (privkey); -l_cert_free (cert); }int main (int argc,char *argv[])
unit/test-pem.c +36 -24
79 lines not on this page · raw message
unit/test-tls.c +8 -8
@@ -213,19 +213,19 @@ static void test_tls12_prf(const void *data)static void test_certificates (const void *data) {struct l_cert *cert; -struct l_cert *cacert; -struct l_cert *wrongca; +struct l_queue *cacert; +struct l_queue *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); + cacert =l_pem_load_certificate_list (CERTDIR "cert-ca.pem" ); +assert (cacert && !l_queue_isempty (cacert)); - wrongca =tls_cert_load_file (CERTDIR "cert-intca.pem" ); -assert (wrongca); + wrongca =l_pem_load_certificate_list (CERTDIR "cert-intca.pem" ); +assert (wrongca && !l_queue_isempty (wrongca));assert (!l_certchain_verify (chain, wrongca));
@@ -234,8 +234,8 @@ static void test_certificates(const void *data)assert (l_certchain_verify (chain,NULL ));l_certchain_free (chain); -l_cert_free (cacert); -l_cert_free (wrongca); +l_queue_destroy (cacert, (l_queue_destroy_func_t ) l_cert_free); +l_queue_destroy (wrongca, (l_queue_destroy_func_t ) l_cert_free); }struct tls_conn_test {
Signature · 1 line
2.19.1
Hi Andrew,
Quoted 9 lines · Andrew Zaborowski
Add
l_pem_load_certificate_chainandl_pem_load_certificate_listthat load certificate chains and lists of certificates such as those generated by openssl and return anl_certchainand anl_queuerespectively.--- ell/pem.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++-------- ell/pem.h | 5 ++ 2 files changed, 123 insertions(+), 19 deletions(-)
All applied, thanks.
Regards,
-Denis
Re: [PATCH 02/16] cert: Accept lists of CA certificates instead of one
In reply to Andrew ZaborowskiHi Andrew,
Quoted 21 lines
ell/cert.c +10
-11-/* -* 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; + ca_key =l_cert_get_pubkey (ca); +if (!ca_key) +return false; + +if (!l_keyring_link (ca_ring, ca_key)) { +l_key_free (ca_key); +return false; + } + } +
ell/cert.c LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain, · line 446
Quoted 10 lines
ell/cert.c +5
-1+if (!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, +return certchain_verify_with_keyring (chain->leaf, verify_ring, ca_certs, ca_ring); }
So can we switch to a non-recursive version now?
Regards,
-Denis