settings: Fix a format string
PATCH 1/7 settings: Fix a format string
The %d conersion specifier is for integers.
--- ell/settings.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
ell/settings.c +1 -1
@@ -1068,7 +1068,7 @@ LIB_EXPORT bool l_settings_set_double(struct l_settings *settings, { L_AUTO_FREE_VAR(char *, buf); - buf =l_strdup_printf ("%d" , in); + buf =l_strdup_printf ("%f" , in);return l_settings_set_value (settings, group_name, key, buf); }
Signature · 1 line
2.19.1
PATCH 2/7 Add format string checking to a few declarations
Add the gcc attribute(format(printf)) to function declarations where this may be useful.
--- ell/dbus.h | 3 ++- ell/string.h | 3 ++- ell/util.h | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-)
ell/dbus.h +2 -1
@@ -118,7 +118,8 @@ struct l_dbus_message *l_dbus_message_new_error_valist(struct l_dbus_message *l_dbus_message_new_error (struct l_dbus_message *method_call,const char *name, -const char *format, ...); +const char *format, ...) + __attribute__((format (printf,3 ,4 )));struct l_dbus_message *l_dbus_message_ref (struct l_dbus_message *message);void l_dbus_message_unref (struct l_dbus_message *message);
ell/string.h +2 -1
@@ -42,7 +42,8 @@ struct l_string *l_string_append_fixed(struct l_string *dest, const char *src,void l_string_append_vprintf (struct l_string *dest,const char *format,va_list args); -void l_string_append_printf (struct l_string *dest,const char *format, ...); +void l_string_append_printf (struct l_string *dest,const char *format, ...) + __attribute__((format (printf,2 ,3 )));struct l_string *l_string_truncate (struct l_string *string,size_t new_size);
ell/util.h +4 -2
@@ -258,7 +258,8 @@ static inline void auto_free(void *a)char *l_strdup (const char *str);char *l_strndup (const char *str,size_t max); -char *l_strdup_printf (const char *format, ...); +char *l_strdup_printf (const char *format, ...) + __attribute__((format (printf,1 ,2 )));char *l_strdup_vprintf (const char *format,va_list args);size_t l_strlcpy (char * dst,const char *src,size_t len);
@@ -280,7 +281,8 @@ void l_util_hexdumpv(bool in, const struct iovec *iov, size_t n_iov,l_util_hexdump_func_t function, void *user_data);void l_util_debug (l_util_hexdump_func_t function,void *user_data, -const char *format, ...); +const char *format, ...) + __attribute__((format (printf,3 ,4 )));const char *l_util_get_debugfs_path (void );
Signature · 1 line
2.19.1
PATCH 3/7 tls: Add debug logging API
In reply to Andrew ZaborowskiAdd a debug logging API similar to that in dbus.h and genl.h. A lot of messages are added to report on the negotiation progress (Handshake, Alert and ChangeCipherSpec messages) and all the possible error situations. Especially configuration errors -- the most common errors -- should be clear when looking at the logs.
No Application Data messages (plaintext) or raw TLS record contents (encrypted) are logged because this can be easily done by the user code and could add a flood of messages that would make it actually harder to solve TLS issues.
--- ell/tls-private.h | 24 +++ ell/tls-record.c | 66 ++++--- ell/tls.c | 427 +++++++++++++++++++++++++++++++++++----------- ell/tls.h | 6 + 4 files changed, 399 insertions(+), 124 deletions(-)
ell/tls-private.h +24 -0
@@ -48,6 +48,7 @@ struct tls_hash_algorithm {uint8_t tls_id;enum l_checksum_type l_id;size_t length; +const char *name; }; typedef bool (*tls_get_hash_t)(struct l_tls *tls, uint8_t tls_id,
@@ -89,6 +90,7 @@ struct tls_cipher_suite {struct tls_compression_method {int id; +const char *name; };enum tls_handshake_state {
@@ -136,6 +138,9 @@ struct l_tls {l_tls_ready_cb_t ready_handle;l_tls_disconnect_cb_t disconnected;void *user_data; +l_tls_debug_cb_t debug_handler; +l_tls_destroy_cb_t debug_destroy; +void *debug_data;char *ca_cert_path;char *cert_path;
@@ -254,3 +259,22 @@ void tls_prf_get_bytes(struct l_tls *tls,const char *label, const void *seed, size_t seed_len, uint8_t *buf, size_t len); + +#define TLS_DEBUG(fmt, args...) \ + l_util_debug(tls->debug_handler, tls->debug_data, "%s:%i " fmt, \ + __func__, __LINE__, ## args) +#define TLS_SET_STATE (new_state) \ + do { \ + TLS_DEBUG("New state %s", \ + tls_handshake_state_to_str(new_state)); \ + tls->state = new_state; \ + } while (0) +#define TLS_DISCONNECT(desc, local_desc, fmt, args...) \ + do { \ + TLS_DEBUG("Disconnect desc=%s local-desc=%s reason=" fmt,\ + l_tls_alert_to_str(desc), \ + l_tls_alert_to_str(local_desc), ## args);\ + tls_disconnect(tls, desc, local_desc); \ + } while (0) + +const char *tls_handshake_state_to_str (enum tls_handshake_state state);
ell/tls-record.c +42 -24
161 lines not on this page · raw message
ell/tls.c +327 -100
985 lines not on this page · raw message
ell/tls.h +6 -0
@@ -60,6 +60,9 @@ typedef void (*l_tls_write_cb_t)(const uint8_t *data, size_t len,typedef void (*l_tls_ready_cb_t )(const char *peer_identity,void *user_data);typedef void (*l_tls_disconnect_cb_t )(enum l_tls_alert_desc reason,bool remote,void *user_data); +typedef void (*l_tls_debug_cb_t )(const char *str,void *user_data); +typedef void (*l_tls_destroy_cb_t )(void *user_data); +/* * app_data_handler gets called with newly received decrypted data.
@@ -110,6 +113,9 @@ bool l_tls_prf_get_bytes(struct l_tls *tls, size_t hash_len, bool use_master_secret, const char *label, uint8_t *buf, size_t len); +bool l_tls_set_debug (struct l_tls *tls,l_tls_debug_cb_t function, +void *user_data,l_tls_destroy_cb_t destroy); + #ifdef __cplusplus }#endif
Signature · 1 line
2.19.1
PATCH 4/7 pem: Add l_pem_load_certificate_list utility
In reply to Andrew Zaborowskil_pem_load_certificate_list loads all the certificates in a file with multiple concatenated PEM structures. This could also be done previously by calling l_pem_load_file with consecutive index values until it returned an error, this is a faster way to load all of the certificates.
--- ell/pem.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- ell/pem.h | 8 ++++++ 2 files changed, 88 insertions(+), 3 deletions(-)
ell/pem.c +80 -3
@@ -39,6 +39,7 @@#include "asn1-private.h" #include "pkcs5-private.h" #include "cipher.h" +#include "queue.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, +size_t *consumed) {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 (consumed) + *consumed = eol +1 - buf; +return data; }
@@ -178,7 +183,7 @@ 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,
@@ -205,7 +210,7 @@ LIB_EXPORT uint8_t *l_pem_load_file(const char *filename, int index,return NULL ; } - result =pem_load_buffer (data, st.st_size, index, type_label, len); + result =pem_load_buffer (data, st.st_size, index, type_label, len,NULL );munmap (data, st.st_size);close (fd);
@@ -233,6 +238,78 @@ LIB_EXPORT uint8_t *l_pem_load_certificate(const char *filename, size_t *len)return content; } +static void pem_element_free (void *data) +{ +struct l_pem_list_element *element = data; + +l_free (element->content); +l_free (element); +} + +LIB_EXPORT struct l_queue *l_pem_load_certificate_list(const char *filename) +{ +int fd; +struct stat st; +uint8_t *data; +size_t start =0 ; +struct l_queue *result =NULL ; + + fd =open (filename,O_RDONLY ); +if (fd <0 ) +return NULL ; + +if (fstat (fd, &st) <0 ) { +close (fd); + +return NULL ; + } + + data =mmap (NULL , st.st_size,PROT_READ ,MAP_SHARED , fd,0 ); +if (data ==MAP_FAILED ) { +close (fd); + +return NULL ; + } + +while (1 ) { +uint8_t *certificate; +size_t len, consumed; +char *label; +struct l_pem_list_element *element; + + certificate =pem_load_buffer (data + start, st.st_size - start,0 , + &label, &len, &consumed); +if (!certificate) + goto done; + +if (strcmp (label,"CERTIFICATE" )) { +l_free (certificate); +break ; + } + +l_free (label); + element =l_malloc (sizeof (struct l_pem_list_element )); + element->content = certificate; + element->len = len; + +if (!result) + result =l_queue_new (); + +l_queue_push_tail (result, element); + start += consumed; + } + +/* Error occured */ +l_queue_destroy (result, pem_element_free); + result =NULL ; + +done: +munmap (data, st.st_size); +close (fd); + +return result; +} +/** * l_pem_load_private_key * @filename: path string to the PEM file to load
ell/pem.h +8 -0
@@ -27,12 +27,20 @@extern "C" {#endif +struct l_queue ; + +struct l_pem_list_element { +uint8_t *content; +size_t len; +}; +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_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 5/7 tls: Support loading multiple root CAs
In reply to Andrew ZaborowskiIf a file with multiple concatenated certificates is supplied to set_auth_data, 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 in which case our network config files break while wpa_supplicant configs keep working.
Also simplify tls_cert_verify_certchain slightly.
--- ell/tls-private.h | 3 +- ell/tls.c | 137 ++++++++++++++++++++++++++++++++-------------- ell/tls.h | 2 +- 3 files changed, 100 insertions(+), 42 deletions(-)
ell/tls-private.h +2 -1
@@ -240,6 +240,7 @@ enum tls_cert_key_type { };struct tls_cert *tls_cert_load_file (const char *filename); +struct l_queue *tls_cert_list_load_file (const char *filename);int tls_cert_from_certificate_list (const void *data,size_t len,struct tls_cert **out_certchain);
@@ -247,7 +248,7 @@ 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); +struct l_queue *ca_certs);void tls_cert_free_certchain (struct tls_cert *cert);
ell/tls.c +97 -40
220 lines not on this page · raw message
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 certificate(s) */ void l_tls_set_cacert (struct l_tls *tls,const char *ca_cert_path);/*
Signature · 1 line
2.19.1
PATCH 6/7 tools: Update tls_cert_verify_certchain parameters
In reply to Andrew Zaborowski--- tools/certchain-verify.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
tools/certchain-verify.c +7 -7
@@ -87,7 +87,7 @@ static void usage(const char *bin)printf ("%s - TLS certificate chain verification utility\n\n" , bin);printf ("Usage: %s [options] <ca_cert file> <raw certificates file>\n" -" <ca_cert file> - local CA Certificate to validate against\n" +" <ca_cert file> - local CA Certificate(s) to validate against\n" " <raw certificates file> - Certificates obtained from PCAP\n" " --help\n\n" , bin); }
@@ -96,7 +96,7 @@ int main(int argc, char *argv[]) {int status =EXIT_FAILURE ;struct tls_cert *certchain; -struct tls_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 =tls_cert_list_load_file (argv[1 ]); +if (!ca_certs) { +fprintf (stderr,"Unable to load CA certifiates\n" ); goto free_certchain; } -if (!tls_cert_verify_certchain (certchain, ca_cert)) { +if (!tls_cert_verify_certchain (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_free (ca_cert); +l_queue_destroy (ca_certs, l_free); free_certchain:tls_cert_free_certchain (certchain); done:
Signature · 1 line
2.19.1
PATCH 7/7 unit: Update tls_cert_verify_certchain parameters
In reply to Andrew Zaborowski--- unit/test-tls.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
unit/test-tls.c +6 -6
@@ -214,16 +214,16 @@ 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_queue *cacert; +struct l_queue *wrongca; cert =tls_cert_load_file (CERTDIR "cert-server.pem" );assert (cert); - cacert =tls_cert_load_file (CERTDIR "cert-ca.pem" ); + cacert =tls_cert_list_load_file (CERTDIR "cert-ca.pem" );assert (cacert); - wrongca =tls_cert_load_file (CERTDIR "cert-intca.pem" ); + wrongca =tls_cert_list_load_file (CERTDIR "cert-intca.pem" );assert (wrongca);assert (!tls_cert_verify_certchain (cert, wrongca));
@@ -233,8 +233,8 @@ static void test_certificates(const void *data)assert (tls_cert_verify_certchain (cert,NULL ));l_free (cert); -l_free (cacert); -l_free (wrongca); +l_queue_destroy (cacert, l_free); +l_queue_destroy (wrongca, l_free); }struct tls_conn_test {
Signature · 1 line
2.19.1