483b06
From 5301e86fccfde6ab444a2c600a412487318fbd13 Mon Sep 17 00:00:00 2001
483b06
From: Jan Cholasta <jcholast@redhat.com>
483b06
Date: Wed, 3 May 2017 06:14:27 +0000
483b06
Subject: [PATCH] server install: fix KDC certificate validation in CA-less
483b06
483b06
Verify that the provided certificate has the extended key usage and subject
483b06
alternative name required for KDC.
483b06
483b06
https://pagure.io/freeipa/issue/6831
483b06
https://pagure.io/freeipa/issue/6869
483b06
483b06
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
483b06
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
483b06
---
483b06
 ipapython/certdb.py                        | 42 ++++++++++++++++++++++++++++++
483b06
 ipaserver/install/installutils.py          | 24 +++++++++++------
483b06
 ipaserver/install/server/install.py        | 11 ++++++--
483b06
 ipaserver/install/server/replicainstall.py | 11 ++++++--
483b06
 4 files changed, 76 insertions(+), 12 deletions(-)
483b06
483b06
diff --git a/ipapython/certdb.py b/ipapython/certdb.py
483b06
index 1ee2603653452577476cf413e6af951cd29c273e..114c58340253141706afa461ecaf87797562ca1d 100644
483b06
--- a/ipapython/certdb.py
483b06
+++ b/ipapython/certdb.py
483b06
@@ -24,14 +24,17 @@ import pwd
483b06
 import grp
483b06
 import re
483b06
 import tempfile
483b06
+from tempfile import NamedTemporaryFile
483b06
 import shutil
483b06
 import base64
483b06
 from cryptography.hazmat.primitives import serialization
483b06
+import cryptography.x509
483b06
 from nss import nss
483b06
 from nss.error import NSPRError
483b06
 
483b06
 from ipapython.dn import DN
483b06
 from ipapython.ipa_log_manager import root_logger
483b06
+from ipapython.kerberos import Principal
483b06
 from ipapython import ipautil
483b06
 from ipalib import x509     # pylint: disable=ipa-forbidden-import
483b06
 
483b06
@@ -182,6 +185,38 @@ def unparse_trust_flags(trust_flags):
483b06
     return trust_flags
483b06
 
483b06
 
483b06
+def verify_kdc_cert_validity(kdc_cert, ca_certs, realm):
483b06
+    pem_kdc_cert = kdc_cert.public_bytes(serialization.Encoding.PEM)
483b06
+    pem_ca_certs = '\n'.join(
483b06
+        cert.public_bytes(serialization.Encoding.PEM) for cert in ca_certs)
483b06
+
483b06
+    with NamedTemporaryFile() as kdc_file, NamedTemporaryFile() as ca_file:
483b06
+        kdc_file.write(pem_kdc_cert)
483b06
+        kdc_file.flush()
483b06
+        ca_file.write(pem_ca_certs)
483b06
+        ca_file.flush()
483b06
+
483b06
+        try:
483b06
+            ipautil.run(
483b06
+                [OPENSSL, 'verify', '-CAfile', ca_file.name, kdc_file.name])
483b06
+            eku = kdc_cert.extensions.get_extension_for_class(
483b06
+                cryptography.x509.ExtendedKeyUsage)
483b06
+            list(eku.value).index(
483b06
+                cryptography.x509.ObjectIdentifier(x509.EKU_PKINIT_KDC))
483b06
+        except (ipautil.CalledProcessError,
483b06
+                cryptography.x509.ExtensionNotFound,
483b06
+                ValueError):
483b06
+            raise ValueError("invalid for a KDC")
483b06
+
483b06
+        principal = str(Principal(['krbtgt', realm], realm))
483b06
+        gns = x509.process_othernames(x509.get_san_general_names(kdc_cert))
483b06
+        for gn in gns:
483b06
+            if isinstance(gn, x509.KRB5PrincipalName) and gn.name == principal:
483b06
+                break
483b06
+        else:
483b06
+            raise ValueError("invalid for realm %s" % realm)
483b06
+
483b06
+
483b06
 class NSSDatabase(object):
483b06
     """A general-purpose wrapper around a NSS cert database
483b06
 
483b06
@@ -707,3 +742,10 @@ class NSSDatabase(object):
483b06
         finally:
483b06
             del certdb, cert
483b06
             nss.nss_shutdown()
483b06
+
483b06
+    def verify_kdc_cert_validity(self, nickname, realm):
483b06
+        nicknames = self.get_trust_chain(nickname)
483b06
+        certs = [self.get_cert(nickname) for nickname in nicknames]
483b06
+        certs = [x509.load_certificate(cert, x509.DER) for cert in certs]
483b06
+
483b06
+        verify_kdc_cert_validity(certs[-1], certs[:-1], realm)
483b06
diff --git a/ipaserver/install/installutils.py b/ipaserver/install/installutils.py
483b06
index 5bce9894780bd920db11196b925492a7fe8f22d0..d2283af20485fd5d66bfd3cc49059d08d1802575 100644
483b06
--- a/ipaserver/install/installutils.py
483b06
+++ b/ipaserver/install/installutils.py
483b06
@@ -1001,7 +1001,7 @@ def handle_error(error, log_file_name=None):
483b06
 
483b06
 
483b06
 def load_pkcs12(cert_files, key_password, key_nickname, ca_cert_files,
483b06
-                host_name):
483b06
+                host_name=None, realm_name=None):
483b06
     """
483b06
     Load and verify server certificate and private key from multiple files
483b06
 
483b06
@@ -1066,13 +1066,21 @@ def load_pkcs12(cert_files, key_password, key_nickname, ca_cert_files,
483b06
                     "CA certificate %s in %s is not valid: %s" %
483b06
                     (subject, ", ".join(cert_files), e))
483b06
 
483b06
-        # Check server validity
483b06
-        try:
483b06
-            nssdb.verify_server_cert_validity(key_nickname, host_name)
483b06
-        except ValueError as e:
483b06
-            raise ScriptError(
483b06
-                "The server certificate in %s is not valid: %s" %
483b06
-                (", ".join(cert_files), e))
483b06
+        if host_name is not None:
483b06
+            try:
483b06
+                nssdb.verify_server_cert_validity(key_nickname, host_name)
483b06
+            except ValueError as e:
483b06
+                raise ScriptError(
483b06
+                    "The server certificate in %s is not valid: %s" %
483b06
+                    (", ".join(cert_files), e))
483b06
+
483b06
+        if realm_name is not None:
483b06
+            try:
483b06
+                nssdb.verify_kdc_cert_validity(key_nickname, realm_name)
483b06
+            except ValueError as e:
483b06
+                raise ScriptError(
483b06
+                    "The KDC certificate in %s is not valid: %s" %
483b06
+                    (", ".join(cert_files), e))
483b06
 
483b06
         out_file = tempfile.NamedTemporaryFile()
483b06
         out_password = ipautil.ipa_generate_password()
483b06
diff --git a/ipaserver/install/server/install.py b/ipaserver/install/server/install.py
483b06
index c1bdce6c8459dfeabd0096d105e535ec4ee56a2a..03380b8d0e9150224b014a1a174d7ea81ccdcf00 100644
483b06
--- a/ipaserver/install/server/install.py
483b06
+++ b/ipaserver/install/server/install.py
483b06
@@ -520,12 +520,12 @@ def install_check(installer):
483b06
             if options.pkinit_pin is None:
483b06
                 raise ScriptError(
483b06
                     "Kerberos KDC private key unlock password required")
483b06
-        pkinit_pkcs12_file, pkinit_pin, _pkinit_ca_cert = load_pkcs12(
483b06
+        pkinit_pkcs12_file, pkinit_pin, pkinit_ca_cert = load_pkcs12(
483b06
             cert_files=options.pkinit_cert_files,
483b06
             key_password=options.pkinit_pin,
483b06
             key_nickname=options.pkinit_cert_name,
483b06
             ca_cert_files=options.ca_cert_files,
483b06
-            host_name=host_name)
483b06
+            realm_name=realm_name)
483b06
         pkinit_pkcs12_info = (pkinit_pkcs12_file.name, pkinit_pin)
483b06
 
483b06
     if (options.http_cert_files and options.dirsrv_cert_files and
483b06
@@ -534,6 +534,13 @@ def install_check(installer):
483b06
             "Apache Server SSL certificate and Directory Server SSL "
483b06
             "certificate are not signed by the same CA certificate")
483b06
 
483b06
+    if (options.http_cert_files and
483b06
+            options.pkinit_cert_files and
483b06
+            http_ca_cert != pkinit_ca_cert):
483b06
+        raise ScriptError(
483b06
+            "Apache Server SSL certificate and PKINIT KDC "
483b06
+            "certificate are not signed by the same CA certificate")
483b06
+
483b06
     if not options.dm_password:
483b06
         dm_password = read_dm_password()
483b06
 
483b06
diff --git a/ipaserver/install/server/replicainstall.py b/ipaserver/install/server/replicainstall.py
483b06
index 66d7ba44645aed69b12f0e5ea14f5080492fe5ef..6f71f0b51812943fea3fb1c576a0174c739a070b 100644
483b06
--- a/ipaserver/install/server/replicainstall.py
483b06
+++ b/ipaserver/install/server/replicainstall.py
483b06
@@ -1069,12 +1069,12 @@ def promote_check(installer):
483b06
             if options.pkinit_pin is None:
483b06
                 raise ScriptError(
483b06
                     "Kerberos KDC private key unlock password required")
483b06
-        pkinit_pkcs12_file, pkinit_pin, _pkinit_ca_cert = load_pkcs12(
483b06
+        pkinit_pkcs12_file, pkinit_pin, pkinit_ca_cert = load_pkcs12(
483b06
             cert_files=options.pkinit_cert_files,
483b06
             key_password=options.pkinit_pin,
483b06
             key_nickname=options.pkinit_cert_name,
483b06
             ca_cert_files=options.ca_cert_files,
483b06
-            host_name=config.host_name)
483b06
+            realm_name=config.realm_name)
483b06
         pkinit_pkcs12_info = (pkinit_pkcs12_file.name, pkinit_pin)
483b06
 
483b06
     if (options.http_cert_files and options.dirsrv_cert_files and
483b06
@@ -1083,6 +1083,13 @@ def promote_check(installer):
483b06
                            "Server SSL certificate are not signed by the same"
483b06
                            " CA certificate")
483b06
 
483b06
+    if (options.http_cert_files and
483b06
+            options.pkinit_cert_files and
483b06
+            http_ca_cert != pkinit_ca_cert):
483b06
+        raise RuntimeError("Apache Server SSL certificate and PKINIT KDC "
483b06
+                           "certificate are not signed by the same CA "
483b06
+                           "certificate")
483b06
+
483b06
     installutils.verify_fqdn(config.host_name, options.no_host_dns)
483b06
     installutils.verify_fqdn(config.master_host_name, options.no_host_dns)
483b06
 
483b06
-- 
483b06
2.9.4
483b06