From: Apollon Oikonomopoulos Date: Mon, 3 Sep 2018 15:54:10 +0300 Subject: verify: warn about weak cert keys or signing algos Extend x509.VerifyX509Certificate() to also check certificates for weak keys or signing algorithms. Rename _VerifyCertificateInner() to _VerifyX509CertificateValidity() to better match what it does, and add a new _VerifyX509CertificateStrength() function that checks: - whether the public key's length is smaller than constants.RSA_KEY_BITS - whether the certificate is signed using a known-weak signature algorithm Apart from cluster verify, VerifyX509Certificate() is also called in a number of places as a pre-flight check with expiration warnings disabled, where every non-empty response is treated as a hard error. In order not to break these uses, we need to change VerifyX509Certificate()'s API to make strength checks optional. Also we refactor the error handling logic to return multiple error XOR warning message that originate from different checks. Signed-off-by: Apollon Oikonomopoulos --- lib/cmdlib/backup.py | 2 +- lib/cmdlib/instance_create.py | 2 +- lib/utils/security.py | 2 +- lib/utils/x509.py | 61 ++++++++++++++++++++++-- src/Ganeti/Constants.hs | 8 ++++ test/py/legacy/ganeti.utils.x509_unittest.py | 69 ++++++++++++++++++++++------ 6 files changed, 122 insertions(+), 22 deletions(-) diff --git a/lib/cmdlib/backup.py b/lib/cmdlib/backup.py index 14527e6..a3d6c50 100644 --- a/lib/cmdlib/backup.py +++ b/lib/cmdlib/backup.py @@ -252,7 +252,7 @@ class LUBackupExport(LogicalUnit): raise errors.OpPrereqError("Unable to load destination X509 CA (%s)" % (err, ), errors.ECODE_INVAL) - (errcode, msg) = utils.VerifyX509Certificate(cert, None, None) + (errcode, msg) = utils.VerifyX509Certificate(cert, None, None, False) if errcode is not None: raise errors.OpPrereqError("Invalid destination X509 CA (%s)" % (msg, ), errors.ECODE_INVAL) diff --git a/lib/cmdlib/instance_create.py b/lib/cmdlib/instance_create.py index 576672a..704225e 100644 --- a/lib/cmdlib/instance_create.py +++ b/lib/cmdlib/instance_create.py @@ -313,7 +313,7 @@ class LUInstanceCreate(LogicalUnit): raise errors.OpPrereqError("Unable to load source X509 CA (%s)" % (err, ), errors.ECODE_INVAL) - (errcode, msg) = utils.VerifyX509Certificate(cert, None, None) + (errcode, msg) = utils.VerifyX509Certificate(cert, None, None, False) if errcode is not None: raise errors.OpPrereqError("Invalid source X509 CA (%s)" % (msg, ), errors.ECODE_INVAL) diff --git a/lib/utils/security.py b/lib/utils/security.py index ad31d04..57a4cb8 100644 --- a/lib/utils/security.py +++ b/lib/utils/security.py @@ -120,7 +120,7 @@ def VerifyCertificate(filename): (errcode, msg) = \ x509.VerifyX509Certificate(cert, constants.SSL_CERT_EXPIRATION_WARN, - constants.SSL_CERT_EXPIRATION_ERROR) + constants.SSL_CERT_EXPIRATION_ERROR, True) if msg: fnamemsg = "While verifying %s: %s" % (filename, msg) diff --git a/lib/utils/x509.py b/lib/utils/x509.py index a7634b3..22d5704 100644 --- a/lib/utils/x509.py +++ b/lib/utils/x509.py @@ -34,7 +34,9 @@ import calendar import datetime import errno +import itertools import logging +import operator import re import time @@ -111,8 +113,8 @@ def GetX509CertValidity(cert): return (not_before, not_after) -def _VerifyCertificateInner(expired, not_before, not_after, now, - warn_days, error_days): +def _VerifyX509CertificateValidity(expired, not_before, not_after, now, + warn_days, error_days): """Verifies certificate validity. @type expired: bool @@ -162,7 +164,34 @@ def _VerifyCertificateInner(expired, not_before, not_after, now, return (None, None) -def VerifyX509Certificate(cert, warn_days, error_days): +def _VerifyX509CertificateStrength(cert): + """Verifies the strength of a certificate + + @type sig_algo: string + @param sig_algo: Name of the algorithm used to sign the certificate + @type key_bits: int + @param key_bits: Length of the public/private key in bits + """ + sig_algo = cert.get_signature_algorithm() + key_bits = cert.get_pubkey().bits() + + warnings = [] + if sig_algo in constants.X509_WEAK_SIGNATURE_ALGORITHMS: + warnings.append("weak signature algorithm '%s'," + " consider running gnt-cluster renew-crypto" % sig_algo) + + if key_bits < constants.RSA_KEY_BITS: + warnings.append("weak public/private keypair: %d bits," + " should be at least %d bits," + " consider running gnt-cluster renew-crypto" % + (key_bits, constants.RSA_KEY_BITS)) + + if warnings: + return (CERT_WARNING, ", ".join(warnings)) + return (None, None) + + +def VerifyX509Certificate(cert, warn_days, error_days, check_strength): """Verifies a certificate for LUClusterVerify. @type cert: OpenSSL.crypto.X509 @@ -171,6 +200,8 @@ def VerifyX509Certificate(cert, warn_days, error_days): @param warn_days: How many days before expiration a warning should be reported @type error_days: number or None @param error_days: How many days before expiration an error should be reported + @type check_strength: bool + @param check_strength: Whether to check the certificate's strength """ # Depending on the pyOpenSSL version, this can just return (None, None) @@ -178,8 +209,28 @@ def VerifyX509Certificate(cert, warn_days, error_days): now = time.time() + constants.NODE_MAX_CLOCK_SKEW - return _VerifyCertificateInner(cert.has_expired(), not_before, not_after, - now, warn_days, error_days) + checks = [] + checks.append(_VerifyX509CertificateValidity(cert.has_expired(), not_before, + not_after, now, warn_days, + error_days)) + + if check_strength: + checks.append(_VerifyX509CertificateStrength(cert)) + + + keyfunc = operator.itemgetter(0) + + # Drop non-error results + checks = [c for c in checks if keyfunc(c) is not None] + + if not checks: + return (None, None) + + # Return all errors of the most severe level + for level, errors in itertools.groupby(sorted(checks, key=keyfunc, + reverse=True), + keyfunc): + return (level, ", ".join(e[1] for e in errors)) def SignX509Certificate(cert, key, salt): diff --git a/src/Ganeti/Constants.hs b/src/Ganeti/Constants.hs index f788ee3..bbfc012 100644 --- a/src/Ganeti/Constants.hs +++ b/src/Ganeti/Constants.hs @@ -614,6 +614,14 @@ x509CertSignatureHeader = "X-Ganeti-Signature" x509CertSignDigest :: String x509CertSignDigest = "SHA256" +-- | Known-weak certificate signature algorithms +x509SignatureAlgoSha1 :: String +x509SignatureAlgoSha1 = "sha1WithRSAEncryption" + +x509WeakSignatureAlgorithms :: FrozenSet String +x509WeakSignatureAlgorithms = ConstantUtils.mkSet [x509SignatureAlgoSha1] + + -- * Import/export daemon mode iemExport :: String diff --git a/test/py/legacy/ganeti.utils.x509_unittest.py b/test/py/legacy/ganeti.utils.x509_unittest.py index 43dd762..e4e3bd4 100755 --- a/test/py/legacy/ganeti.utils.x509_unittest.py +++ b/test/py/legacy/ganeti.utils.x509_unittest.py @@ -160,9 +160,13 @@ class TestCertVerification(testutils.GanetiTestCase): testutils.GanetiTestCase.setUp(self) self.tmpdir = tempfile.mkdtemp() + self.orig_rsa_key_bits = constants.RSA_KEY_BITS + self.orig_x509_weak_signature_algorithms = constants.X509_WEAK_SIGNATURE_ALGORITHMS def tearDown(self): shutil.rmtree(self.tmpdir) + constants.RSA_KEY_BITS = self.orig_rsa_key_bits + constants.X509_WEAK_SIGNATURE_ALGORITHMS = self.orig_x509_weak_signature_algorithms def testVerifyCertificate(self): cert_pem = testutils.ReadTestData("cert1.pem") @@ -170,7 +174,7 @@ class TestCertVerification(testutils.GanetiTestCase): cert_pem) # Not checking return value as this certificate is expired - utils.VerifyX509Certificate(cert, 30, 7) + utils.VerifyX509Certificate(cert, 30, 7, True) @staticmethod def _GenCert(key, before, validity): @@ -199,50 +203,87 @@ class TestCertVerification(testutils.GanetiTestCase): # few lines take more than NODE_MAX_CLOCK_SKEW / 2 for before in [-1, 0, SKEW // 4, SKEW // 2]: cert = self._GenCert(key, before, validity) - result = utils.VerifyX509Certificate(cert, 1, 2) + result = utils.VerifyX509Certificate(cert, 1, 2, True) self.assertEqual(result, (None, None)) # skew too great, not accepting certs for before in [SKEW * 2, SKEW * 10]: cert = self._GenCert(key, before, validity) - (status, msg) = utils.VerifyX509Certificate(cert, 1, 2) + (status, msg) = utils.VerifyX509Certificate(cert, 1, 2, True) self.assertEqual(status, utils.CERT_WARNING) self.assertTrue(msg.startswith("Certificate not yet valid")) + def testKeyStrength(self): + # Create private and public key + key = OpenSSL.crypto.PKey() + key.generate_key(OpenSSL.crypto.TYPE_RSA, constants.RSA_KEY_BITS) + + validity = 7 * 86400 + cert = self._GenCert(key, 0, validity) + + result = utils.VerifyX509Certificate(cert, None, None, True) + self.assertEqual(result, (None, None)) + + constants.RSA_KEY_BITS *= 2 + status, msg = utils.VerifyX509Certificate(cert, None, None, True) + self.assertTrue(status == utils.CERT_WARNING) + self.assertTrue(msg.startswith("weak public/private key")) + constants.RSA_KEY_BITS = self.orig_rsa_key_bits + + def testSigningAlgoStrength(self): + # Create private and public key + key = OpenSSL.crypto.PKey() + key.generate_key(OpenSSL.crypto.TYPE_RSA, constants.RSA_KEY_BITS) + + validity = 7 * 86400 + cert = self._GenCert(key, 0, validity) + + result = utils.VerifyX509Certificate(cert, None, None, True) + self.assertEqual(result, (None, None)) + + signing_algo = cert.get_signature_algorithm() + + constants.X509_WEAK_SIGNATURE_ALGORITHMS = \ + constants.X509_WEAK_SIGNATURE_ALGORITHMS.union([signing_algo]) + status, msg = utils.VerifyX509Certificate(cert, None, None, True) + self.assertTrue(status == utils.CERT_WARNING) + self.assertTrue(msg.startswith("weak signature algorithm")) + constants.X509_WEAK_SIGNATURE_ALGORITHMS = self.orig_x509_weak_signature_algorithms + -class TestVerifyCertificateInner(unittest.TestCase): +class TestVerifyX509CertificateValidity(unittest.TestCase): def test(self): - vci = utils.x509._VerifyCertificateInner + vcv = utils.x509._VerifyX509CertificateValidity # Valid - self.assertEqual(vci(False, 1263916313, 1298476313, 1266940313, 30, 7), + self.assertEqual(vcv(False, 1263916313, 1298476313, 1266940313, 30, 7), (None, None)) # Not yet valid - (errcode, msg) = vci(False, 1266507600, 1267544400, 1266075600, 30, 7) + (errcode, msg) = vcv(False, 1266507600, 1267544400, 1266075600, 30, 7) self.assertEqual(errcode, utils.CERT_WARNING) # Expiring soon - (errcode, msg) = vci(False, 1266507600, 1267544400, 1266939600, 30, 7) + (errcode, msg) = vcv(False, 1266507600, 1267544400, 1266939600, 30, 7) self.assertEqual(errcode, utils.CERT_ERROR) - (errcode, msg) = vci(False, 1266507600, 1267544400, 1266939600, 30, 1) + (errcode, msg) = vcv(False, 1266507600, 1267544400, 1266939600, 30, 1) self.assertEqual(errcode, utils.CERT_WARNING) - (errcode, msg) = vci(False, 1266507600, None, 1266939600, 30, 7) + (errcode, msg) = vcv(False, 1266507600, None, 1266939600, 30, 7) self.assertEqual(errcode, None) # Expired - (errcode, msg) = vci(True, 1266507600, 1267544400, 1266939600, 30, 7) + (errcode, msg) = vcv(True, 1266507600, 1267544400, 1266939600, 30, 7) self.assertEqual(errcode, utils.CERT_ERROR) - (errcode, msg) = vci(True, None, 1267544400, 1266939600, 30, 7) + (errcode, msg) = vcv(True, None, 1267544400, 1266939600, 30, 7) self.assertEqual(errcode, utils.CERT_ERROR) - (errcode, msg) = vci(True, 1266507600, None, 1266939600, 30, 7) + (errcode, msg) = vcv(True, 1266507600, None, 1266939600, 30, 7) self.assertEqual(errcode, utils.CERT_ERROR) - (errcode, msg) = vci(True, None, None, 1266939600, 30, 7) + (errcode, msg) = vcv(True, None, None, 1266939600, 30, 7) self.assertEqual(errcode, utils.CERT_ERROR)