pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0156-server-certinstall-support-PKINIT.patch

483b06
From 96afd05dda2ce502994b6c9ceae819d79d96a666 Mon Sep 17 00:00:00 2001
483b06
From: Jan Cholasta <jcholast@redhat.com>
483b06
Date: Wed, 3 May 2017 06:18:05 +0000
483b06
Subject: [PATCH] server certinstall: support PKINIT
483b06
483b06
Allow replacing the KDC certificate.
483b06
483b06
https://pagure.io/freeipa/issue/6831
483b06
483b06
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
483b06
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
483b06
---
483b06
 install/tools/man/ipa-server-certinstall.1  |  5 ++-
483b06
 ipaserver/install/ipa_server_certinstall.py | 70 +++++++++++++++++++++++++++--
483b06
 2 files changed, 70 insertions(+), 5 deletions(-)
483b06
483b06
diff --git a/install/tools/man/ipa-server-certinstall.1 b/install/tools/man/ipa-server-certinstall.1
483b06
index d23bbd490e2b0454b8fb908e22f33c7a611c8874..35cd8c6c711119d7c782c6a89ac78b4894cec073 100644
483b06
--- a/install/tools/man/ipa-server-certinstall.1
483b06
+++ b/install/tools/man/ipa-server-certinstall.1
483b06
@@ -22,7 +22,7 @@ ipa\-server\-certinstall \- Install new SSL server certificates
483b06
 .SH "SYNOPSIS"
483b06
 ipa\-server\-certinstall [\fIOPTION\fR]... FILE...
483b06
 .SH "DESCRIPTION"
483b06
-Replace the current SSL Directory and/or Apache server certificate(s) with the certificate in the specified files. The files are accepted in PEM and DER certificate, PKCS#7 certificate chain, PKCS#8 and raw private key and PKCS#12 formats.
483b06
+Replace the current Directory server SSL certificate, Apache server SSL certificate and/or Kerberos KDC certificate with the certificate in the specified files. The files are accepted in PEM and DER certificate, PKCS#7 certificate chain, PKCS#8 and raw private key and PKCS#12 formats.
483b06
 
483b06
 PKCS#12 is a file format used to safely transport SSL certificates and public/private keypairs.
483b06
 
483b06
@@ -37,6 +37,9 @@ Install the certificate on the Directory Server
483b06
 \fB\-w\fR, \fB\-\-http\fR
483b06
 Install the certificate in the Apache Web Server
483b06
 .TP
483b06
+\fB\-k\fR, \fB\-\-kdc\fR
483b06
+Install the certificate in the Kerberos KDC
483b06
+.TP
483b06
 \fB\-\-pin\fR=\fIPIN\fR
483b06
 The password to unlock the private key
483b06
 .TP
483b06
diff --git a/ipaserver/install/ipa_server_certinstall.py b/ipaserver/install/ipa_server_certinstall.py
483b06
index 9f2cd9573a156949ae979e7b69fbd23adaf2feb8..a14a84f188c62170c8ac11f823ebba60609e4cc7 100644
483b06
--- a/ipaserver/install/ipa_server_certinstall.py
483b06
+++ b/ipaserver/install/ipa_server_certinstall.py
483b06
@@ -21,12 +21,17 @@
483b06
 import os
483b06
 import os.path
483b06
 import pwd
483b06
+import tempfile
483b06
 import optparse  # pylint: disable=deprecated-module
483b06
 
483b06
+from ipalib import x509
483b06
+from ipalib.install import certmonger
483b06
 from ipaplatform.constants import constants
483b06
 from ipaplatform.paths import paths
483b06
 from ipapython import admintool
483b06
-from ipapython.certdb import get_ca_nickname, NSSDatabase
483b06
+from ipapython.certdb import (get_ca_nickname,
483b06
+                              NSSDatabase,
483b06
+                              verify_kdc_cert_validity)
483b06
 from ipapython.dn import DN
483b06
 from ipalib import api, errors
483b06
 from ipaserver.install import certs, dsinstance, installutils
483b06
@@ -35,7 +40,7 @@ from ipaserver.install import certs, dsinstance, installutils
483b06
 class ServerCertInstall(admintool.AdminTool):
483b06
     command_name = 'ipa-server-certinstall'
483b06
 
483b06
-    usage = "%prog <-d|-w> [options] <file> ..."
483b06
+    usage = "%prog <-d|-w|-k> [options] <file> ..."
483b06
 
483b06
     description = "Install new SSL server certificates."
483b06
 
483b06
@@ -52,6 +57,10 @@ class ServerCertInstall(admintool.AdminTool):
483b06
             dest="http", action="store_true", default=False,
483b06
             help="install certificate for the http server")
483b06
         parser.add_option(
483b06
+            "-k", "--kdc",
483b06
+            dest="kdc", action="store_true", default=False,
483b06
+            help="install PKINIT certificate for the KDC")
483b06
+        parser.add_option(
483b06
             "--pin",
483b06
             dest="pin", metavar="PIN", sensitive=True,
483b06
             help="The password of the PKCS#12 file")
483b06
@@ -73,8 +82,9 @@ class ServerCertInstall(admintool.AdminTool):
483b06
 
483b06
         installutils.check_server_configuration()
483b06
 
483b06
-        if not self.options.dirsrv and not self.options.http:
483b06
-            self.option_parser.error("you must specify dirsrv and/or http")
483b06
+        if not any((self.options.dirsrv, self.options.http, self.options.kdc)):
483b06
+            self.option_parser.error(
483b06
+                "you must specify dirsrv, http and/or kdc")
483b06
 
483b06
         if not self.args:
483b06
             self.option_parser.error("you must provide certificate filename")
483b06
@@ -108,6 +118,9 @@ class ServerCertInstall(admintool.AdminTool):
483b06
         if self.options.http:
483b06
             self.install_http_cert()
483b06
 
483b06
+        if self.options.kdc:
483b06
+            self.install_kdc_cert()
483b06
+
483b06
         api.Backend.ldap2.disconnect()
483b06
 
483b06
     def install_dirsrv_cert(self):
483b06
@@ -161,6 +174,55 @@ class ServerCertInstall(admintool.AdminTool):
483b06
         os.chown(os.path.join(dirname, 'key3.db'), 0, pent.pw_gid)
483b06
         os.chown(os.path.join(dirname, 'secmod.db'), 0, pent.pw_gid)
483b06
 
483b06
+    def install_kdc_cert(self):
483b06
+        ca_cert_file = paths.CA_BUNDLE_PEM
483b06
+        pkcs12_file, pin, ca_cert = installutils.load_pkcs12(
483b06
+            cert_files=self.args,
483b06
+            key_password=self.options.pin,
483b06
+            key_nickname=self.options.cert_name,
483b06
+            ca_cert_files=[ca_cert_file],
483b06
+            realm_name=api.env.realm)
483b06
+
483b06
+        cdb = certs.CertDB(api.env.realm, nssdir=paths.IPA_NSSDB_DIR)
483b06
+
483b06
+        # Check that the ca_cert is known and trusted
483b06
+        with tempfile.NamedTemporaryFile() as temp:
483b06
+            certs.install_pem_from_p12(pkcs12_file.name, pin, temp.name)
483b06
+
483b06
+            kdc_cert = x509.load_certificate_from_file(temp.name)
483b06
+            ca_certs = x509.load_certificate_list_from_file(ca_cert_file)
483b06
+
483b06
+            try:
483b06
+                verify_kdc_cert_validity(kdc_cert, ca_certs, api.env.realm)
483b06
+            except ValueError as e:
483b06
+                raise admintool.ScriptError(
483b06
+                    "Peer's certificate issuer is not trusted (%s). "
483b06
+                    "Please run ipa-cacert-manage install and ipa-certupdate "
483b06
+                    "to install the CA certificate." % str(e))
483b06
+
483b06
+        try:
483b06
+            ca_enabled = api.Command.ca_is_enabled()['result']
483b06
+            if ca_enabled:
483b06
+                certmonger.stop_tracking(certfile=paths.KDC_CERT)
483b06
+
483b06
+            certs.install_pem_from_p12(pkcs12_file.name, pin, paths.KDC_CERT)
483b06
+            certs.install_key_from_p12(pkcs12_file.name, pin, paths.KDC_KEY)
483b06
+
483b06
+            if ca_enabled:
483b06
+                # Start tracking only if the cert was issued by IPA CA
483b06
+                # Retrieve IPA CA
483b06
+                ipa_ca_cert = cdb.get_cert_from_db(
483b06
+                    get_ca_nickname(api.env.realm),
483b06
+                    pem=False)
483b06
+                # And compare with the CA which signed this certificate
483b06
+                if ca_cert == ipa_ca_cert:
483b06
+                    certmonger.start_tracking(
483b06
+                        (paths.KDC_CERT, paths.KDC_KEY),
483b06
+                        storage='FILE',
483b06
+                        profile='KDCs_PKINIT_Certs')
483b06
+        except RuntimeError as e:
483b06
+            raise admintool.ScriptError(str(e))
483b06
+
483b06
     def check_chain(self, pkcs12_filename, pkcs12_pin, nssdb):
483b06
         # create a temp nssdb
483b06
         with NSSDatabase() as tempnssdb:
483b06
-- 
483b06
2.9.4
483b06