crypto: misc fixes and improvements to cert handling
PATCH 4/7 crypto: only verify CA certs in chain of trust
In reply to Daniel P. BerrangéFrom: Henry Kleynhans <[email protected]>
The CA file provided to qemu may contain CA certificates which do not form part of the chain of trust for the specific certificate we are sanity checking.
This patch changes the sanity checking from validating every CA certificate to only checking the CA certificates which are part of the chain of trust (issuer chain). Other certificates are ignored.
--- crypto/tlscredsx509.c | 57 ++++++++++++++++++++++++--- tests/unit/test-crypto-tlscredsx509.c | 25 +++++++++++- 2 files changed, 75 insertions(+), 7 deletions(-)
crypto/tlscredsx509.c +51 -6
@@ -307,6 +307,51 @@ qcrypto_tls_creds_check_cert(QCryptoTLSCredsX509 *creds,return 0 ; } +static int +qcrypto_tls_creds_check_authority_chain (QCryptoTLSCredsX509 *creds, +gnutls_x509_crt_t cert, +gnutls_x509_crt_t *cacerts, +unsigned int ncacerts, +const char *cacertFile, +bool isServer, +bool isCA, +Error **errp) +{ +gnutls_x509_crt_t *cert_to_check = &cert; +int checking_issuer =1 ; +int retval =0 ; + +while (checking_issuer) { + checking_issuer =0 ; + +if (gnutls_x509_crt_check_issuer (*cert_to_check, + *cert_to_check)) { +/* +* The cert is self-signed indicating we have +* reached the root of trust. +*/ +return qcrypto_tls_creds_check_cert ( + creds, *cert_to_check, cacertFile, + isServer, isCA, errp); + } +for (int i =0 ; i < ncacerts; i++) { +if (gnutls_x509_crt_check_issuer (*cert_to_check, + cacerts[i])) { + retval =qcrypto_tls_creds_check_cert ( + creds, cacerts[i], cacertFile, + isServer, isCA, errp); +if (retval <0 ) { +return retval; + } + cert_to_check = &cacerts[i]; + checking_issuer =1 ; +break ; + } + } + } + +return -1 ; +} static int qcrypto_tls_creds_check_cert_pair(gnutls_x509_crt_t cert,
@@ -491,12 +536,12 @@ qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds, goto cleanup; } -for (i =0 ; i < ncacerts; i++) { -if (qcrypto_tls_creds_check_cert (creds, - cacerts[i], cacertFile, - isServer, true, errp) <0 ) { - goto cleanup; - } +if (cert && +qcrypto_tls_creds_check_authority_chain (creds, cert, + cacerts, ncacerts, + cacertFile, isServer, + true, errp) <0 ) { + goto cleanup; } if (cert && ncacerts &&
tests/unit/test-crypto-tlscredsx509.c +24 -1
@@ -589,6 +589,12 @@ int main(int argc, char **argv) true, true,GNUTLS_KEY_KEY_CERT_SIGN , false, false,NULL ,NULL , 0, 0); +TLS_CERT_REQ (cacertlevel1creq_invalid, cacertrootreq, +"UK" ,"qemu level 1c invalid" ,NULL ,NULL ,NULL ,NULL , + true, true, true, + true, true,GNUTLS_KEY_KEY_CERT_SIGN , + false, false,NULL ,NULL , +360 ,400 ); TLS_CERT_REQ(cacertlevel2areq, cacertlevel1areq, "UK", "qemu level 2a", NULL, NULL, NULL, NULL, true, true, true,
@@ -617,16 +623,32 @@ int main(int argc, char **argv) cacertlevel2areq.crt, }; +test_tls_write_cert_chain (WORKDIR "cacertchain-ctx.pem" , certchain,G_N_ELEMENTS (certchain)); +gnutls_x509_crt_t certchain_with_invalid[] = { + cacertrootreq.crt, + cacertlevel1areq.crt, + cacertlevel1breq.crt, + cacertlevel1creq_invalid.crt, + cacertlevel2areq.crt, + }; + +test_tls_write_cert_chain (WORKDIR "cacertchain-with-invalid-ctx.pem" , + certchain_with_invalid, +G_N_ELEMENTS (certchain_with_invalid)); +TLS_TEST_REG (chain1, true,WORKDIR "cacertchain-ctx.pem" , servercertlevel3areq.filename, false);TLS_TEST_REG (chain2, false,WORKDIR "cacertchain-ctx.pem" , clientcertlevel2breq.filename, false); +TLS_TEST_REG (certchainwithexpiredcert, false, +WORKDIR "cacertchain-with-invalid-ctx.pem" , + clientcertlevel2breq.filename, false);/* Some missing certs - first two are fatal, the last * is ok
@@ -640,7 +662,6 @@ int main(int argc, char **argv)TLS_TEST_REG (missingclient, false, cacert1req.filename,"clientcertdoesnotexist.pem" , false); - ret =g_test_run ();test_tls_discard_cert (&cacertreq);
@@ -694,10 +715,12 @@ int main(int argc, char **argv)test_tls_discard_cert (&cacertrootreq);test_tls_discard_cert (&cacertlevel1areq);test_tls_discard_cert (&cacertlevel1breq); +test_tls_discard_cert (&cacertlevel1creq_invalid);test_tls_discard_cert (&cacertlevel2areq);test_tls_discard_cert (&servercertlevel3areq);test_tls_discard_cert (&clientcertlevel2breq);unlink (WORKDIR "cacertchain-ctx.pem" ); +unlink (WORKDIR "cacertchain-with-invalid-ctx.pem" );test_tls_cleanup (KEYFILE );rmdir (WORKDIR );
Signature · 1 line
2.49.0
PATCH 6/7 crypto: fix error reporting in cert chain checks
In reply to Daniel P. BerrangéThe loop that checks the CA certificate chain can fail to report an error message if one of the certs in the chain has an issuer than is not present in the chain. In this case, the outer loop 'while (checking_issuer)' will terminate after failing to find the issuer, and no error message will be reported.
--- crypto/tlscredsx509.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-)
crypto/tlscredsx509.c +23 -12
@@ -318,11 +318,11 @@ qcrypto_tls_creds_check_authority_chain(QCryptoTLSCredsX509 *creds, Error **errp) {gnutls_x509_crt_t cert_to_check = cert; -int checking_issuer =1 ; -int retval =0 ; +gnutls_datum_t dn = {}; +int rv; -while (checking_issuer) { - checking_issuer =0 ; +for (;;) { +gnutls_x509_crt_t cert_issuer =NULL ;if (gnutls_x509_crt_check_issuer (cert_to_check, cert_to_check)) {
@@ -337,19 +337,30 @@ qcrypto_tls_creds_check_authority_chain(QCryptoTLSCredsX509 *creds,for (int i =0 ; i < ncacerts; i++) {if (gnutls_x509_crt_check_issuer (cert_to_check, cacerts[i])) { - retval =qcrypto_tls_creds_check_cert ( - creds, cacerts[i], cacertFile, - isServer, isCA, errp); -if (retval <0 ) { -return retval; - } - cert_to_check = cacerts[i]; - checking_issuer =1 ; + cert_issuer = cacerts[i];break ; } } +if (!cert_issuer) { +break ; + } + +if (qcrypto_tls_creds_check_cert (creds, cert_issuer, cacertFile, + isServer, isCA, errp) <0 ) { +return -1 ; + } + + cert_to_check = cert_issuer; } + rv =gnutls_x509_crt_get_dn2 (cert_to_check, &dn); +if (rv <0 ) { +error_setg (errp,"Unable to fetch cert DN: %s" , +gnutls_strerror (rv)); +return -1 ; + } +error_setg (errp,"Cert '%s' has no issuer in CA chain" , dn.data); +gnutls_free (dn.data);return -1 ; }
Signature · 1 line
2.49.0
PATCH 1/7 crypto: stop requiring "key encipherment" usage in x509 certs
In reply to Daniel P. BerrangéThis usage flag was deprecated by RFC8813, such that it is forbidden to be present for certs using ECDSA/ECDH algorithms, and in TLS 1.3 is conceptually obsolete.
As such many valid certs will no longer have this key usage flag set, and QEMU should not be rejecting them, as this prevents use of otherwise valid & desirable algorithms.
--- crypto/tlscredsx509.c | 10 +------- docs/system/tls.rst | 13 +++------- tests/unit/crypto-tls-x509-helpers.h | 6 ++--- tests/unit/test-crypto-tlscredsx509.c | 36 +++++++++++++-------------- tests/unit/test-crypto-tlssession.c | 14 +++++------ tests/unit/test-io-channel-tls.c | 4 +-- 6 files changed, 34 insertions(+), 49 deletions(-)
crypto/tlscredsx509.c +1 -9
@@ -144,7 +144,7 @@ qcrypto_tls_creds_check_cert_key_usage(QCryptoTLSCredsX509 *creds,if (status <0 ) {if (status ==GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE ) { usage = isCA ?GNUTLS_KEY_KEY_CERT_SIGN : -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT ; +GNUTLS_KEY_DIGITAL_SIGNATURE ; }else { error_setg(errp, "Unable to query certificate %s key usage: %s",
@@ -171,14 +171,6 @@ qcrypto_tls_creds_check_cert_key_usage(QCryptoTLSCredsX509 *creds,return -1 ; } } -if (!(usage &GNUTLS_KEY_KEY_ENCIPHERMENT )) { -if (critical) { -error_setg (errp, -"Certificate %s usage does not permit key " -"encipherment" , certFile); -return -1 ; - } - } }return 0 ;
docs/system/tls.rst +4 -9
@@ -118,7 +118,6 @@ information for each server, and use it to issue server certificates. ip_address = 2620:0:cafe::87 ip_address = 2001:24::92 tls_www_server - encryption_key signing_key EOF # certtool --generate-privkey > server-hostNNN-key.pem
@@ -134,9 +133,8 @@ the subject alt name extension data. The ``tls_www_server`` keyword is the key purpose extension to indicate this certificate is intended for usage in a web server. Although QEMU network services are not in fact HTTP servers (except for VNC websockets), setting this key purpose is -still recommended. The ``encryption_key`` and ``signing_key`` keyword is -the key usage extension to indicate this certificate is intended for -usage in the data session. +still recommended. The ``signing_key`` keyword is the key usage extension +to indicate this certificate is intended for usage in the data session. The ``server-hostNNN-key.pem`` and ``server-hostNNN-cert.pem`` files should now be securely copied to the server for which they were
@@ -171,7 +169,6 @@ certificates. organization = Name of your organization cn = hostNNN.foo.example.com tls_www_client - encryption_key signing_key EOF # certtool --generate-privkey > client-hostNNN-key.pem
@@ -187,9 +184,8 @@ the ``dns_name`` and ``ip_address`` fields are not included. The ``tls_www_client`` keyword is the key purpose extension to indicate this certificate is intended for usage in a web client. Although QEMU network clients are not in fact HTTP clients, setting this key purpose is still -recommended. The ``encryption_key`` and ``signing_key`` keyword is the -key usage extension to indicate this certificate is intended for usage -in the data session. +recommended. The ``signing_key`` keyword is the key usage extension to +indicate this certificate is intended for usage in the data session. The ``client-hostNNN-key.pem`` and ``client-hostNNN-cert.pem`` files should now be securely copied to the client for which they were
@@ -222,7 +218,6 @@ client and server instructions in one. ip_address = 2001:24::92 tls_www_server tls_www_client - encryption_key signing_key EOF # certtool --generate-privkey > both-hostNNN-key.pem
tests/unit/crypto-tls-x509-helpers.h +2 -4
@@ -148,8 +148,7 @@ void test_tls_cleanup(const char *keyfile); .basicConstraintsIsCA = false, \ .keyUsageEnable = true, \ .keyUsageCritical = true, \ - .keyUsageValue = \ - GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT, \ + .keyUsageValue = GNUTLS_KEY_DIGITAL_SIGNATURE, \ .keyPurposeEnable = true, \ .keyPurposeCritical = true, \ .keyPurposeOID1 = GNUTLS_KP_TLS_WWW_CLIENT, \
@@ -168,8 +167,7 @@ void test_tls_cleanup(const char *keyfile); .basicConstraintsIsCA = false, \ .keyUsageEnable = true, \ .keyUsageCritical = true, \ - .keyUsageValue = \ - GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT, \ + .keyUsageValue = GNUTLS_KEY_DIGITAL_SIGNATURE, \ .keyPurposeEnable = true, \ .keyPurposeCritical = true, \ .keyPurposeOID1 = GNUTLS_KP_TLS_WWW_SERVER, \
tests/unit/test-crypto-tlscredsx509.c +18 -18
@@ -166,14 +166,14 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);TLS_CERT_REQ (clientcertreq, cacertreq,"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_CLIENT ,NULL ,0 ,0 );
@@ -196,7 +196,7 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);
@@ -211,7 +211,7 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);
@@ -226,7 +226,7 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);
@@ -250,7 +250,7 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);/* no-basic */
@@ -264,7 +264,7 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);/* Key usage:dig-sig:critical */
@@ -278,7 +278,7 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);
@@ -303,7 +303,7 @@ int main(int argc, char **argv)"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT | +GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_CERT_SIGN , false, false,NULL ,NULL , 0, 0);
@@ -406,7 +406,7 @@ int main(int argc, char **argv)"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT | +GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_CERT_SIGN , false, false,NULL ,NULL , 0, 0);
@@ -508,21 +508,21 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);TLS_CERT_REQ (servercertexp1req, cacertreq,"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL ,0 ,-1 );TLS_CERT_REQ (clientcertexp1req, cacertreq,"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_CLIENT ,NULL ,0 ,-1 );
@@ -546,21 +546,21 @@ int main(int argc, char **argv)"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);TLS_CERT_REQ (servercertnew1req, cacertreq,"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL ,1 ,2 );TLS_CERT_REQ (clientcertnew1req, cacertreq,"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_CLIENT ,NULL ,1 ,2 );
@@ -599,14 +599,14 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);TLS_CERT_REQ (clientcertlevel2breq, cacertlevel1breq,"UK" ,"qemu client level 2b" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_CLIENT ,NULL ,0 ,0 );
tests/unit/test-crypto-tlssession.c +7 -7
@@ -472,14 +472,14 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);TLS_CERT_REQ (clientcertreq, cacertreq,"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_CLIENT ,NULL ,0 ,0 );
@@ -487,7 +487,7 @@ int main(int argc, char **argv)"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_CLIENT ,NULL , 0, 0);
@@ -506,7 +506,7 @@ int main(int argc, char **argv)"192.168.122.1" ,"fec0::dead:beaf" , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);/* This intentionally doesn't replicate */
@@ -515,7 +515,7 @@ int main(int argc, char **argv)"192.168.122.1" ,"fec0::dead:beaf" , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);
@@ -619,14 +619,14 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);TLS_CERT_REQ (clientcertlevel2breq, cacertlevel1breq,"UK" ,"qemu client level 2b" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_CLIENT ,NULL ,0 ,0 );
tests/unit/test-io-channel-tls.c +2 -2
@@ -302,14 +302,14 @@ int main(int argc, char **argv)"UK" ,"qemu.org" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_SERVER ,NULL , 0, 0);TLS_CERT_REQ (clientcertreq, cacertreq,"UK" ,"qemu" ,NULL ,NULL ,NULL ,NULL , true, true, false, true, true, -GNUTLS_KEY_DIGITAL_SIGNATURE |GNUTLS_KEY_KEY_ENCIPHERMENT , +GNUTLS_KEY_DIGITAL_SIGNATURE , true, true,GNUTLS_KP_TLS_WWW_CLIENT ,NULL ,0 ,0 );
Signature · 1 line
2.49.0
PATCH 5/7 crypto: remove extraneous pointer usage in gnutls certs
In reply to Daniel P. BerrangéThe 'gnutls_x509_crt_t' type is already a pointer, not a struct, so the extra level of pointer indirection is not needed.
--- crypto/tlscredsx509.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
crypto/tlscredsx509.c +6 -6
@@ -317,25 +317,25 @@ qcrypto_tls_creds_check_authority_chain(QCryptoTLSCredsX509 *creds, bool isCA, Error **errp) { - gnutls_x509_crt_t *cert_to_check = &cert; + gnutls_x509_crt_t cert_to_check = cert; int checking_issuer = 1; int retval = 0; while (checking_issuer) { checking_issuer = 0; - if (gnutls_x509_crt_check_issuer(*cert_to_check, - *cert_to_check)) { + if (gnutls_x509_crt_check_issuer(cert_to_check, + cert_to_check)) { /* * The cert is self-signed indicating we have * reached the root of trust. */ return qcrypto_tls_creds_check_cert( - creds, *cert_to_check, cacertFile, + creds, cert_to_check, cacertFile, isServer, isCA, errp); } for (int i = 0; i < ncacerts; i++) { - if (gnutls_x509_crt_check_issuer(*cert_to_check, + if (gnutls_x509_crt_check_issuer(cert_to_check, cacerts[i])) { retval = qcrypto_tls_creds_check_cert( creds, cacerts[i], cacertFile,
@@ -343,7 +343,7 @@ qcrypto_tls_creds_check_authority_chain(QCryptoTLSCredsX509 *creds,if (retval <0 ) {return retval; } - cert_to_check = &cacerts[i]; + cert_to_check = cacerts[i]; checking_issuer =1 ;break ; }
Signature · 1 line
2.49.0
PATCH 2/7 crypto: switch to newer gnutls API for distinguished name
In reply to Daniel P. BerrangéThe new API automatically allocates the right amount of memory to hold the distinguished name, avoiding the need to loop and realloc.
--- crypto/tlssession.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
crypto/tlssession.c +3 -9
@@ -373,20 +373,14 @@ qcrypto_tls_session_check_certificate(QCryptoTLSSession *session, }if (i ==0 ) { -size_t dnameSize =1024 ; - session->peername =g_malloc (dnameSize); - requery: - ret =gnutls_x509_crt_get_dn (cert, session->peername, &dnameSize); +gnutls_datum_t dname = {}; + ret =gnutls_x509_crt_get_dn2 (cert, &dname);if (ret <0 ) { -if (ret ==GNUTLS_E_SHORT_MEMORY_BUFFER ) { - session->peername =g_realloc (session->peername, - dnameSize); - goto requery; - }error_setg (errp,"Cannot get client distinguished name: %s" ,gnutls_strerror (ret)); goto error; } + session->peername = (char *)g_steal_pointer (&dname.data);if (session->authzid) {bool allow;
Signature · 1 line
2.49.0
cover crypto: misc fixes and improvements to cert handling
This series includes three patches that were posted a fairly long time ago. They are borderline between a feature request and a bug fix, but I'm classing them more bug fix, since they addressing issues with cert acceptance that we really should not have had.
The patches by Henry had outstanding comments from myself, and I've chosen to simply fix them in two followup commits of my own now to get this over the line.
The patch from "matoro" was not accepted because they were contributed under a github alias. With our change to have a more relaxed interpretation of the DCO allowing any "known identity", we can now accept this patch. It had some conflicts with Henry's patch which I've fixed up.
Then there is one other small bug fix and one improvement to use a newer gnutls API.
Daniel P. Berrangé (4):
crypto: stop requiring "key encipherment" usage in x509 certs
crypto: switch to newer gnutls API for distinguished name
crypto: remove extraneous pointer usage in gnutls certs
crypto: fix error reporting in cert chain checks
Henry Kleynhans (2):
crypto: load all certificates in X509 CA file
crypto: only verify CA certs in chain of trust
matoro (1):
crypto: allow client/server cert chains
crypto/tlscredsx509.c | 236 +++++++++++++++----------- crypto/tlssession.c | 12 +- docs/system/tls.rst | 13 +- tests/unit/crypto-tls-x509-helpers.h | 6 +- tests/unit/test-crypto-tlscredsx509.c | 138 ++++++++++++--- tests/unit/test-crypto-tlssession.c | 14 +- tests/unit/test-io-channel-tls.c | 4 +- 7 files changed, 270 insertions(+), 153 deletions(-)
Signature · 1 line
2.49.0
PATCH 3/7 crypto: load all certificates in X509 CA file
From: Henry Kleynhans <[email protected]>
Some CA files may contain multiple intermediaries and roots of trust. These may not fit into the hard-coded limit of 16.
Extend the validation code to allocate enough space to load all of the certificates present in the CA file and ensure they are cleaned up.
--- crypto/tlscredsx509.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
crypto/tlscredsx509.c +11 -12
@@ -418,9 +418,8 @@ qcrypto_tls_creds_load_cert(QCryptoTLSCredsX509 *creds,static int qcrypto_tls_creds_load_ca_cert_list (QCryptoTLSCredsX509 *creds,const char *certFile, -gnutls_x509_crt_t *certs, -unsigned int certMax, -size_t *ncerts, +gnutls_x509_crt_t **certs, +unsigned int *ncerts,Error **errp) {gnutls_datum_t data;
@@ -441,20 +440,18 @@ qcrypto_tls_creds_load_ca_cert_list(QCryptoTLSCredsX509 *creds, data.data = (unsigned char *)buf; data.size =strlen (buf); -if (gnutls_x509_crt_list_import (certs, &certMax, &data, -GNUTLS_X509_FMT_PEM ,0 ) <0 ) { +if (gnutls_x509_crt_list_import2 (certs, ncerts, &data, +GNUTLS_X509_FMT_PEM ,0 ) <0 ) {error_setg (errp,"Unable to import CA certificate list %s" , certFile);return -1 ; } - *ncerts = certMax;return 0 ; } -#define MAX_CERTS 16 static int qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds, bool isServer,
@@ -463,12 +460,11 @@ qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds, Error **errp) {gnutls_x509_crt_t cert =NULL ; -gnutls_x509_crt_t cacerts[MAX_CERTS ]; -size_t ncacerts =0 ; +gnutls_x509_crt_t *cacerts =NULL ; +unsigned int ncacerts =0 ;size_t i;int ret =-1 ; -memset (cacerts,0 ,sizeof (cacerts));if (certFile &&access (certFile,R_OK ) ==0 ) { cert = qcrypto_tls_creds_load_cert(creds,
@@ -480,8 +476,9 @@ qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds, }if (access (cacertFile,R_OK ) ==0 ) {if (qcrypto_tls_creds_load_ca_cert_list (creds, - cacertFile, cacerts, -MAX_CERTS , &ncacerts, + cacertFile, + &cacerts, + &ncacerts, errp) <0 ) { goto cleanup; }
@@ -518,6 +515,8 @@ qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds,for (i =0 ; i < ncacerts; i++) {gnutls_x509_crt_deinit (cacerts[i]); } +gnutls_free (cacerts); +return ret; }
Signature · 1 line
2.49.0
PATCH 7/7 crypto: allow client/server cert chains
In reply to Daniel P. BerrangéFrom: matoro <[email protected]>
The existing implementation assumes that client/server certificates are single individual certificates. If using publicly-issued certificates, or internal CAs that use an intermediate issuer, this is unlikely to be the case, and they will instead be certificate chains. While this can be worked around by moving the intermediate certificates to the CA certificate, which DOES currently support multiple certificates, this instead allows the issued certificate chains to be used as-is, without requiring the overhead of shuffling certificates around.
Corresponding libvirt change is available here:
https://gitlab.com/libvirt/libvirt/-/merge_requests/222
--- crypto/tlscredsx509.c | 157 ++++++++++++-------------- tests/unit/test-crypto-tlscredsx509.c | 77 +++++++++++++ 2 files changed, 147 insertions(+), 87 deletions(-)
crypto/tlscredsx509.c +70 -87
@@ -309,7 +309,8 @@ qcrypto_tls_creds_check_cert(QCryptoTLSCredsX509 *creds, static int qcrypto_tls_creds_check_authority_chain(QCryptoTLSCredsX509 *creds, - gnutls_x509_crt_t cert, + gnutls_x509_crt_t *certs, + unsigned int ncerts, gnutls_x509_crt_t *cacerts, unsigned int ncacerts, const char *cacertFile,
@@ -317,10 +318,33 @@ qcrypto_tls_creds_check_authority_chain(QCryptoTLSCredsX509 *creds,bool isCA, Error **errp) { - gnutls_x509_crt_t cert_to_check = cert; -gnutls_datum_t dn = {}; + gnutls_x509_crt_t cert_to_check = certs[ncerts - 1]; +gnutls_datum_t dn = {}, dnissuer = {};int rv; +for (int i =0 ; i < (ncerts -1 ); i++) { +if (!gnutls_x509_crt_check_issuer (certs[i], certs[i +1 ])) { + rv =gnutls_x509_crt_get_dn2 (certs[i], &dn); +if (rv <0 ) { +error_setg (errp,"Unable to fetch cert DN: %s" , +gnutls_strerror (rv)); +return -1 ; + } + rv =gnutls_x509_crt_get_dn2 (certs[i +1 ], &dnissuer); +if (rv <0 ) { +gnutls_free (dn.data); +error_setg (errp,"Unable to fetch cert DN: %s" , +gnutls_strerror (rv)); +return -1 ; + } +error_setg (errp,"Cert '%s' does not match issuer of cert '%s'" , + dnissuer.data, dn.data); +gnutls_free (dn.data); +gnutls_free (dnissuer.data); +return -1 ; + } + } +for (;;) {gnutls_x509_crt_t cert_issuer =NULL ;
@@ -365,7 +389,8 @@ qcrypto_tls_creds_check_authority_chain(QCryptoTLSCredsX509 *creds, } static int -qcrypto_tls_creds_check_cert_pair(gnutls_x509_crt_t cert, +qcrypto_tls_creds_check_cert_pair(gnutls_x509_crt_t *certs, + size_t ncerts, const char *certFile, gnutls_x509_crt_t *cacerts, size_t ncacerts,
@@ -375,7 +400,7 @@ qcrypto_tls_creds_check_cert_pair(gnutls_x509_crt_t cert, {unsigned int status; -if (gnutls_x509_crt_list_verify (&cert,1 , +if (gnutls_x509_crt_list_verify (certs, ncerts, cacerts, ncacerts,NULL ,0 ,0 , &status) <0 ) {
@@ -417,66 +442,14 @@ qcrypto_tls_creds_check_cert_pair(gnutls_x509_crt_t cert, } -static gnutls_x509_crt_t -qcrypto_tls_creds_load_cert (QCryptoTLSCredsX509 *creds, -const char *certFile, -bool isServer, -Error **errp) -{ -gnutls_datum_t data; -gnutls_x509_crt_t cert =NULL ; - g_autofree char *buf = NULL; -gsize buflen; -GError *gerr =NULL ; -int ret =-1 ; -int err; - -trace_qcrypto_tls_creds_x509_load_cert (creds, isServer, certFile); - - err =gnutls_x509_crt_init (&cert); -if (err <0 ) { -error_setg (errp,"Unable to initialize certificate: %s" , -gnutls_strerror (err)); - goto cleanup; - } - -if (!g_file_get_contents (certFile, &buf, &buflen, &gerr)) { -error_setg (errp,"Cannot load CA cert list %s: %s" , - certFile, gerr->message); -g_error_free (gerr); - goto cleanup; - } - - data.data = (unsigned char *)buf; - data.size =strlen (buf); - - err =gnutls_x509_crt_import (cert, &data,GNUTLS_X509_FMT_PEM ); -if (err <0 ) { -error_setg (errp, isServer ? -"Unable to import server certificate %s: %s" : -"Unable to import client certificate %s: %s" , - certFile, -gnutls_strerror (err)); - goto cleanup; - } - - ret =0 ; - - cleanup: -if (ret !=0 ) { -gnutls_x509_crt_deinit (cert); - cert =NULL ; - } -return cert; -} - -static int -qcrypto_tls_creds_load_ca_cert_list (QCryptoTLSCredsX509 *creds, -const char *certFile, -gnutls_x509_crt_t **certs, -unsigned int *ncerts, -Error **errp) +qcrypto_tls_creds_load_cert_list (QCryptoTLSCredsX509 *creds, +const char *certFile, +gnutls_x509_crt_t **certs, +unsigned int *ncerts, +bool isServer, +bool isCA, +Error **errp) {gnutls_datum_t data; g_autofree char *buf = NULL;
@@ -499,7 +472,9 @@ qcrypto_tls_creds_load_ca_cert_list(QCryptoTLSCredsX509 *creds,if (gnutls_x509_crt_list_import2 (certs, ncerts, &data,GNUTLS_X509_FMT_PEM ,0 ) <0 ) {error_setg (errp, -"Unable to import CA certificate list %s" , + isCA ?"Unable to import CA certificate list %s" : + (isServer ?"Unable to import server certificate %s" : +"Unable to import client certificate %s" ), certFile);return -1 ; }
@@ -515,7 +490,8 @@ qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds,const char *certFile, Error **errp) { - gnutls_x509_crt_t cert = NULL; + gnutls_x509_crt_t *certs = NULL; +unsigned int ncerts =0 ;gnutls_x509_crt_t *cacerts =NULL ;unsigned int ncacerts =0 ;size_t i;
@@ -523,41 +499,48 @@ qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds,if (certFile &&access (certFile,R_OK ) ==0 ) { - cert =qcrypto_tls_creds_load_cert (creds, - certFile, isServer, - errp); -if (!cert) { +if (qcrypto_tls_creds_load_cert_list (creds, + certFile, + &certs, + &ncerts, + isServer, + false, + errp) <0 ) { goto cleanup; } }if (access (cacertFile,R_OK ) ==0 ) { -if (qcrypto_tls_creds_load_ca_cert_list (creds, - cacertFile, - &cacerts, - &ncacerts, - errp) <0 ) { +if (qcrypto_tls_creds_load_cert_list (creds, + cacertFile, + &cacerts, + &ncacerts, + isServer, + true, + errp) <0 ) { goto cleanup; } } -if (cert && -qcrypto_tls_creds_check_cert (creds, - cert, certFile, isServer, - false, errp) <0 ) { - goto cleanup; +for (i =0 ; i < ncerts; i++) { +if (qcrypto_tls_creds_check_cert (creds, + certs[i], certFile, + isServer, (i !=0 ), errp) <0 ) { + goto cleanup; + } } -if (cert && -qcrypto_tls_creds_check_authority_chain (creds, cert, +if (ncerts && +qcrypto_tls_creds_check_authority_chain (creds, + certs, ncerts, cacerts, ncacerts, cacertFile, isServer, true, errp) <0 ) { goto cleanup; } -if (cert && ncacerts && -qcrypto_tls_creds_check_cert_pair (cert, certFile, cacerts, - ncacerts, cacertFile, +if (ncerts && ncacerts && +qcrypto_tls_creds_check_cert_pair (certs, ncerts, certFile, + cacerts, ncacerts, cacertFile, isServer, errp) <0 ) { goto cleanup; }
@@ -565,8 +548,8 @@ qcrypto_tls_creds_x509_sanity_check(QCryptoTLSCredsX509 *creds, ret =0 ; cleanup: -if (cert) { -gnutls_x509_crt_deinit (cert); +for (i =0 ; i < ncerts; i++) { +gnutls_x509_crt_deinit (certs[i]); }for (i =0 ; i < ncacerts; i++) {gnutls_x509_crt_deinit (cacerts[i]);
tests/unit/test-crypto-tlscredsx509.c +77 -0
103 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH 5/7] crypto: remove extraneous pointer usage in gnutls certs
In reply to Daniel P. BerrangéQuoted 7 lines · Daniel P. Berrangé
The '
gnutls_x509_crt_t' type is already a pointer, not a struct, so the extra level of pointer indirection is not needed.Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/tlscredsx509.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
Re: [PATCH 7/7] crypto: allow client/server cert chains
In reply to Daniel P. BerrangéQuoted 1 line · Daniel P. Berrangé
From: matoro <[email protected]>
Should we use <[email protected]> here?
Quoted 21 lines
The existing implementation assumes that client/server certificates are single individual certificates. If using publicly-issued certificates, or internal CAs that use an intermediate issuer, this is unlikely to be the case, and they will instead be certificate chains. While this can be worked around by moving the intermediate certificates to the CA certificate, which DOES currently support multiple certificates, this instead allows the issued certificate chains to be used as-is, without requiring the overhead of shuffling certificates around.
Corresponding libvirt change is available here:
https://gitlab.com/libvirt/libvirt/-/merge_requests/222Reviewed-by Daniel P. Berrangé <[email protected]>Signed-off-by matoro <[email protected]>[DB: adapted for code conflicts with multi-CA patch]Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/tlscredsx509.c | 157 ++++++++++++-------------- tests/unit/test-crypto-tlscredsx509.c | 77 +++++++++++++ 2 files changed, 147 insertions(+), 87 deletions(-)
Re: [PATCH 7/7] crypto: allow client/server cert chains
Quoted 4 lines · Philippe Mathieu-Daudé
Quoted 1 line · Daniel P. Berrangé
From: matoro <[email protected]>
Should we use <[email protected]> here?
I generally don't like to change the git metadata that a user submits with unless it is clearly broken, which I don't think is the case here.
Quoted 14 lines
Quoted 14 lines
The existing implementation assumes that client/server certificates are single individual certificates. If using publicly-issued certificates, or internal CAs that use an intermediate issuer, this is unlikely to be the case, and they will instead be certificate chains. While this can be worked around by moving the intermediate certificates to the CA certificate, which DOES currently support multiple certificates, this instead allows the issued certificate chains to be used as-is, without requiring the overhead of shuffling certificates around.
Corresponding libvirt change is available here:
https://gitlab.com/libvirt/libvirt/-/merge_requests/222
Quoted 9 lines
Quoted 8 lines
Reviewed-by Daniel P. Berrangé <[email protected]>Signed-off-by matoro <[email protected]>[DB: adapted for code conflicts with multi-CA patch]Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/tlscredsx509.c | 157 ++++++++++++-------------- tests/unit/test-crypto-tlscredsx509.c | 77 +++++++++++++ 2 files changed, 147 insertions(+), 87 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 7/7] crypto: allow client/server cert chains
Quoted 9 lines · Daniel P. Berrangé
Quoted 4 lines · Philippe Mathieu-Daudé
Quoted 1 line · Daniel P. Berrangé
From: matoro <[email protected]>
Should we use <[email protected]> here?
I generally don't like to change the git metadata that a user submits with unless it is clearly broken, which I don't think is the case here.
I find confusing to have a distinct email for author ...
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
The existing implementation assumes that client/server certificates are single individual certificates. If using publicly-issued certificates, or internal CAs that use an intermediate issuer, this is unlikely to be the case, and they will instead be certificate chains. While this can be worked around by moving the intermediate certificates to the CA certificate, which DOES currently support multiple certificates, this instead allows the issued certificate chains to be used as-is, without requiring the overhead of shuffling certificates around.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Corresponding libvirt change is available here:
https://gitlab.com/libvirt/libvirt/-/merge_requests/222Reviewed-by Daniel P. Berrangé <[email protected]>Signed-off-by matoro <[email protected]>
... and the S-o-b. Anyway this isn't the first case, so I don't mind at all.
Quoted 10 lines
Quoted 7 lines
Quoted 6 lines
[DB: adapted for code conflicts with multi-CA patch]Signed-off-by Daniel P. Berrangé <[email protected]>--- crypto/tlscredsx509.c | 157 ++++++++++++-------------- tests/unit/test-crypto-tlscredsx509.c | 77 +++++++++++++ 2 files changed, 147 insertions(+), 87 deletions(-)With regards,
Daniel