cert: Fix verification of the 2nd certificate in chain
PATCH 1/5 cert: Fix verification of the 2nd certificate in chain
In the case that the trusted CA certificates are provided to l_certchain_verify() and one of them binary-matches the top CA certificate in the chain being verified, we'd skip linking it to the keyring and we'd link the next certificate directly. As a result that second certificate would be linked to an empty unrestricted keyring and would not be verified at all.
Instead add the top CA to the keyring and restrict it the same way we proceed when no trusted CAs are provided.
--- ell/cert.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-)
ell/cert.c +19 -14
@@ -454,6 +454,7 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,struct l_cert *cert;struct l_key *prev_key =NULL ;int verified =0 ; +bool ca_match;static char error_buf[200 ];if (unlikely (!chain || !chain->leaf))
@@ -464,6 +465,7 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,RETURN_ERROR ("Can't create verify keyring" ); cert = chain->ca; + ca_match =cert_is_in_set (cert, ca_certs);/* * For TLS compatibility the trusted root CA certificate is
@@ -477,19 +479,22 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,* already possess it in order to validate it in any case." * * The following is an optimization to skip verifying the root -* cert in the chain if it is identical to one of the trusted CA -* certificates. It also happens to work around a kernel issue -* preventing self-signed certificates missing the AKID -* extension from being linked to a keyring. +* cert in the chain if it is bitwise-identical to one of the +* trusted CA certificates. In that case we don't have to load +* all of the trusted certificates into the kernel, link them +* to @ca_ring or link @ca_ring to @verify_ring, instead we +* load the first certificate into @verify_ring before we set +* the restric mode on it, same as when no trusted CAs are +* provided. +* +* Note this happens to work around a kernel issue preventing +* self-signed certificates missing the optional AKID extension +* from being linked to a restricted keyring. That issue would +* have affected us if the trusted CA set included such +* certificate and the same certificate was at the root of +* the chain. */ -if (cert_is_in_set (cert, ca_certs)) { - verified++; - cert = cert->issued; -if (!cert) -return true; - - prev_key =cert_try_link (cert, verify_ring); - }else if (ca_certs) { +if (ca_certs && !ca_match) { ca_ring =cert_set_to_keyring (ca_certs, error_buf);if (!ca_ring) {if (error)
@@ -550,8 +555,8 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,"verified against trusted CA(s) and the " "following %i top certificates verified ok" , verified +1 , total, - ca_certs && verified ?"" :"not " , - verified ? verified - 1 : 0); + ca_certs && (ca_match || verified) ?"" : + "not ", verified ? verified - 1 : 0); }l_key_free (prev_key);
Signature · 1 line
2.20.1
PATCH 2/5 build: Generate test certs with no AKID / SKID extensions
--- Makefile.am | 29 +++++++++++++++++++++++++++-- unit/gencerts.cnf | 7 +++++++ 2 files changed, 34 insertions(+), 2 deletions(-)
Makefile.am +27 -2
@@ -209,12 +209,14 @@ cert_files = unit/cert-chain.pem \ unit/cert-client-key-v2-des.pem \ unit/cert-client-key-v2-des-ede3.pem \ unit/cert-client-key-v2-aes128.pem \ - unit/cert-client-key-v2-aes256.pem + unit/cert-client-key-v2-aes256.pem \ + unit/cert-no-keyid.pem cert_checks = unit/cert-intca \ unit/cert-entity-int \ unit/cert-server \ - unit/cert-client + unit/cert-client \ + unit/cert-no-keyid if MAINTAINER_MODE noinst_PROGRAMS += $(unit_tests) $(dbus_tests) $(cert_tests)
@@ -499,6 +501,29 @@ unit/cert-entity-int.pem: unit/cert-entity-int.csr unit/cert-intca.pem unit/genc unit/cert-entity-int: unit/cert-entity-int.pem unit/cert-chain.pem $(AM_V_GEN)openssl verify -CAfile $(builddir)/unit/cert-chain.pem $< +unit/cert-ca2.pem: unit/cert-ca-key.pem unit/gencerts.cnf + $(AM_V_GEN)openssl req -x509 -new -nodes -extensions ca_no_akid_ext \ + -config $(srcdir)/unit/gencerts.cnf \ + -subj '/O=International Union of Example Organizations/CN=Certificate issuer guy/emailAddress=ca-no-akid(a)mail.example' \ + -key $< -sha256 -days 10000 -out $@ + +unit/cert-no-keyid.csr: unit/cert-client-key.pem unit/gencerts.cnf + $(AM_V_GEN)openssl req -new \ + -config $(srcdir)/unit/gencerts.cnf \ + -subj '/O=Baz Example Organization/CN=Baz Example Organization/[email protected]' \ + -key $< -out $@ + +unit/cert-no-keyid.pem: unit/cert-no-keyid.csr unit/cert-ca2.pem unit/gencerts.cnf + $(AM_V_GEN)openssl x509 -req -extensions no_keyid_ext \ + -extfile $(srcdir)/unit/gencerts.cnf \ + -in $< -CA $(builddir)/unit/cert-ca2.pem \ + -CAkey $(builddir)/unit/cert-ca-key.pem \ + -CAserial $(builddir)/unit/cert-ca2.srl \ + -CAcreateserial -sha256 -days 10000 -out $@ $($(AM_V_P)_redirect_openssl) + +unit/cert-no-keyid: unit/cert-no-keyid.pem unit/cert-ca2.pem + $(AM_V_GEN)openssl verify -CAfile $(builddir)/unit/cert-ca2.pem $< + unit/key-plaintext.h: unit/plaintext.txt $(AM_V_GEN)xxd -i < $< > $@
unit/gencerts.cnf +7 -0
@@ -23,3 +23,10 @@ basicConstraints = CA:FALSE subjectKeyIdentifier = hash authorityKeyIdentifier = keyid:always,issuer:always subjectAltName = DNS:foo.int.example,DNS:foo.int.com + +[ ca_no_akid_ext ] +basicConstraints = CA:TRUE + +[ no_keyid_ext ] +basicConstraints = CA:FALSE +authorityKeyIdentifier = issuer:always
Signature · 1 line
2.20.1
PATCH 3/5 unit: Test l_certchain_verify with no-keyid certificates
In reply to Andrew ZaborowskiIn test-tls slightly refactor test_certificates() to avoid having so many variables and add new asserts to check that l_certchain_verify() can load a self-signed CA certificate with no SKID or AKID extension (the kernel has a problem with these certificates) and also that it can be looked up when verifying a leaf certificate pointing to that CA only by name+serial combination.
--- unit/test-tls.c | 81 +++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 29 deletions(-)
unit/test-tls.c +52 -29
@@ -230,9 +230,6 @@ static void test_certificates(const void *data)struct l_queue *wrongca;struct l_queue *twocas;struct l_certchain *chain; -struct l_certchain *chain2; -struct l_certchain *chain3; -struct l_certchain *chain4; cacert =l_pem_load_certificate_list (CERTDIR "cert-ca.pem" );assert (cacert && !l_queue_isempty (cacert));
@@ -251,46 +248,72 @@ static void test_certificates(const void *data)assert (l_certchain_verify (chain,NULL ,NULL ));assert (l_certchain_verify (chain, twocas,NULL )); - chain2 =l_pem_load_certificate_chain (CERTDIR "cert-chain.pem" ); -assert (chain2); +l_certchain_free (chain); + + chain =l_pem_load_certificate_chain (CERTDIR "cert-chain.pem" ); +assert (chain); -assert (!l_certchain_verify (chain2, wrongca,NULL )); -assert (l_certchain_verify (chain2, cacert,NULL )); -assert (l_certchain_verify (chain2,NULL ,NULL )); -assert (l_certchain_verify (chain2, twocas,NULL )); +assert (!l_certchain_verify (chain, wrongca,NULL )); +assert (l_certchain_verify (chain, cacert,NULL )); +assert (l_certchain_verify (chain,NULL ,NULL )); +assert (l_certchain_verify (chain, twocas,NULL )); + +l_certchain_free (chain); - chain3 =certchain_new_from_leaf ( + chain =certchain_new_from_leaf (load_cert_file (CERTDIR "cert-server.pem" )); -certchain_link_issuer (chain3, +certchain_link_issuer (chain,load_cert_file (CERTDIR "cert-entity-int.pem" )); -certchain_link_issuer (chain3, +certchain_link_issuer (chain,load_cert_file (CERTDIR "cert-intca.pem" )); -certchain_link_issuer (chain3, +certchain_link_issuer (chain,load_cert_file (CERTDIR "cert-ca.pem" )); -assert (chain3); +assert (chain); + +assert (!l_certchain_verify (chain, wrongca,NULL )); +assert (!l_certchain_verify (chain, cacert,NULL )); +assert (!l_certchain_verify (chain,NULL ,NULL )); +assert (!l_certchain_verify (chain, twocas,NULL )); -assert (!l_certchain_verify (chain3, wrongca,NULL )); -assert (!l_certchain_verify (chain3, cacert,NULL )); -assert (!l_certchain_verify (chain3,NULL ,NULL )); -assert (!l_certchain_verify (chain3, twocas,NULL )); +l_certchain_free (chain); - chain4 =certchain_new_from_leaf ( + chain =certchain_new_from_leaf (load_cert_file (CERTDIR "cert-entity-int.pem" )); -certchain_link_issuer (chain4, +certchain_link_issuer (chain,load_cert_file (CERTDIR "cert-intca.pem" )); -certchain_link_issuer (chain4, +certchain_link_issuer (chain,load_cert_file (CERTDIR "cert-ca.pem" )); -assert (chain4); +assert (chain); + +assert (!l_certchain_verify (chain, wrongca,NULL )); +assert (l_certchain_verify (chain, cacert,NULL )); +assert (l_certchain_verify (chain,NULL ,NULL )); +assert (l_certchain_verify (chain, twocas,NULL )); + +l_certchain_free (chain); +l_queue_destroy (cacert, (l_queue_destroy_func_t ) l_cert_free); + + cacert =l_pem_load_certificate_list (CERTDIR "cert-ca2.pem" ); +assert (cacert && !l_queue_isempty (cacert)); + + chain =certchain_new_from_leaf ( +load_cert_file (CERTDIR "cert-no-keyid.pem" )); +assert (chain); -assert (!l_certchain_verify (chain4, wrongca,NULL )); -assert (l_certchain_verify (chain4, cacert,NULL )); -assert (l_certchain_verify (chain4,NULL ,NULL )); -assert (l_certchain_verify (chain4, twocas,NULL )); +assert (!l_certchain_verify (chain, wrongca,NULL )); +assert (l_certchain_verify (chain, cacert,NULL )); +assert (l_certchain_verify (chain,NULL ,NULL )); +assert (!l_certchain_verify (chain, twocas,NULL )); + +certchain_link_issuer (chain, +load_cert_file (CERTDIR "cert-ca2.pem" )); + +assert (!l_certchain_verify (chain, wrongca,NULL )); +assert (l_certchain_verify (chain, cacert,NULL )); +assert (l_certchain_verify (chain,NULL ,NULL )); +assert (!l_certchain_verify (chain, twocas,NULL ));l_certchain_free (chain); -l_certchain_free (chain2); -l_certchain_free (chain3); -l_certchain_free (chain4);l_queue_destroy (cacert, (l_queue_destroy_func_t ) l_cert_free);l_queue_destroy (wrongca, (l_queue_destroy_func_t ) l_cert_free);l_queue_destroy (twocas, (l_queue_destroy_func_t ) l_cert_free);
Signature · 1 line
2.20.1
PATCH 4/5 cert: Relax trused CA matching in l_certchain_verify
In reply to Andrew ZaborowskiCheck if any of the certificates in the chain matched any of the trusted CA certificates (if given) and treat the chain as trusted even if the matching certificate was not the top CA in the chain. We also don't require that the trusted certificates or the root in the chain are self-signed.
This allows a user to pin a specific certificate to a server/client or trust a subtree of all of the certificates emitted by the root CA.
--- ell/cert.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-)
ell/cert.c +22 -8
@@ -454,7 +454,8 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,struct l_cert *cert;struct l_key *prev_key =NULL ;int verified =0 ; -bool ca_match; +int ca_match =0 ; +int i =0 ;static char error_buf[200 ];if (unlikely (!chain || !chain->leaf))
@@ -464,8 +465,13 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,if (!verify_ring)RETURN_ERROR ("Can't create verify keyring" ); +for (cert = chain->ca; cert; cert = cert->issued, i++) +if (cert_is_in_set (cert, ca_certs)) { + ca_match = i +1 ; +break ; + } + cert = chain->ca; - ca_match =cert_is_in_set (cert, ca_certs);/* * For TLS compatibility the trusted root CA certificate is
@@ -549,14 +555,22 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,if (!prev_key) {int total =0 ; +char str[100 ];for (cert = chain->ca; cert; cert = cert->issued, total++); -RETURN_ERROR ("Linking certificate %i / %i failed, root %s" -"verified against trusted CA(s) and the " -"following %i top certificates verified ok" , - verified +1 , total, - ca_certs && (ca_match || verified) ?"" : -"not " , verified ? verified -1 :0 ); + +if (ca_match) +snprintf (str,sizeof (str),"%i / %i matched a trusted " +"certificate, root not verified" , + ca_match, total); +else +snprintf (str,sizeof (str),"root %sverified against " +"trusted CA(s)" , + ca_certs && !ca_match && verified ?"" : +"not " ); + +RETURN_ERROR ("Linking certificate %i / %i failed, %s" , + verified +1 , total, str); }l_key_free (prev_key);
Signature · 1 line
2.20.1
PATCH 5/5 unit: Relax test-tls certificate chain checks
In reply to Andrew ZaborowskiMake the l_certchain_verify() failure tests pass after the previous commit.
--- unit/test-tls.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
unit/test-tls.c +7 -2
@@ -228,6 +228,7 @@ static void test_certificates(const void *data) {struct l_queue *cacert;struct l_queue *wrongca; +struct l_queue *wrongca2;struct l_queue *twocas;struct l_certchain *chain;
@@ -237,6 +238,9 @@ static void test_certificates(const void *data) wrongca =l_pem_load_certificate_list (CERTDIR "cert-intca.pem" );assert (wrongca && !l_queue_isempty (wrongca)); + wrongca2 =l_pem_load_certificate_list (CERTDIR "cert-server.pem" ); +assert (wrongca2 && !l_queue_isempty (wrongca2)); + twocas =l_pem_load_certificate_list (CERTDIR "cert-chain.pem" );assert (twocas);
@@ -253,7 +257,7 @@ static void test_certificates(const void *data) chain =l_pem_load_certificate_chain (CERTDIR "cert-chain.pem" );assert (chain); -assert (!l_certchain_verify (chain, wrongca,NULL )); +assert (!l_certchain_verify (chain, wrongca2,NULL ));assert (l_certchain_verify (chain, cacert,NULL ));assert (l_certchain_verify (chain,NULL ,NULL ));assert (l_certchain_verify (chain, twocas,NULL ));
@@ -285,7 +289,7 @@ static void test_certificates(const void *data) load_cert_file(CERTDIR "cert-ca.pem"));assert (chain); -assert (!l_certchain_verify (chain, wrongca,NULL )); +assert (!l_certchain_verify (chain, wrongca2,NULL ));assert (l_certchain_verify (chain, cacert,NULL ));assert (l_certchain_verify (chain,NULL ,NULL ));assert (l_certchain_verify (chain, twocas,NULL ));
@@ -316,6 +320,7 @@ static void test_certificates(const void *data)l_certchain_free (chain);l_queue_destroy (cacert, (l_queue_destroy_func_t ) l_cert_free);l_queue_destroy (wrongca, (l_queue_destroy_func_t ) l_cert_free); +l_queue_destroy (wrongca2, (l_queue_destroy_func_t ) l_cert_free);l_queue_destroy (twocas, (l_queue_destroy_func_t ) l_cert_free); }
Signature · 1 line
2.20.1
Hi Andrew,
Quoted 13 lines · Andrew Zaborowski
In the case that the trusted CA certificates are provided to
l_certchain_verify()and one of them binary-matches the top CA certificate in the chain being verified, we'd skip linking it to the keyring and we'd link the next certificate directly. As a result that second certificate would be linked to an empty unrestricted keyring and would not be verified at all.Instead add the top CA to the keyring and restrict it the same way we proceed when no trusted CAs are provided.
--- ell/cert.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-)
<snip>
Quoted 13 lines
ell/cert.c +5
-4@@ -477,19 +479,22 @@ LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,* already possess it in order to validate it in any case." * * The following is an optimization to skip verifying the root -* cert in the chain if it is identical to one of the trusted CA -* certificates. It also happens to work around a kernel issue -* preventing self-signed certificates missing the AKID -* extension from being linked to a keyring. +* cert in the chain if it is bitwise-identical to one of the +* trusted CA certificates. In that case we don't have to load +* all of the trusted certificates into the kernel, link them +* to @ca_ring or link @ca_ring to @verify_ring, instead we +* load the first certificate into @verify_ring before we set
ell/cert.c LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain, · line 496
Quoted 10 lines
ell/cert.c +9
-0+* the restric mode on it, same as when no trusted CAs are +* provided. +* +* Note this happens to work around a kernel issue preventing +* self-signed certificates missing the optional AKID extension +* from being linked to a restricted keyring. That issue would +* have affected us if the trusted CA set included such +* certificate and the same certificate was at the root of +* the chain. */
This comment makes things _much_ clearer now. I love it.
All applied, thanks.
Regards,
-Denis