Beispiel #1
0
 def install_external_pkinit_certs(self):
     certs.install_pem_from_p12(self.pkcs12_info[0], self.pkcs12_info[1],
                                paths.KDC_CERT)
     certs.install_key_from_p12(self.pkcs12_info[0], self.pkcs12_info[1],
                                paths.KDC_KEY)
     self._install_pkinit_ca_bundle()
     self.pkinit_enable()
Beispiel #2
0
    def migrate_to_mod_ssl(self):
        """For upgrades only, migrate from mod_nss to mod_ssl"""
        db = certs.CertDB(api.env.realm, nssdir=paths.HTTPD_ALIAS_DIR)
        nickname = self.get_mod_nss_nickname()
        with tempfile.NamedTemporaryFile() as temp:
            pk12_password = ipautil.ipa_generate_password()
            pk12_pwdfile = ipautil.write_tmp_file(pk12_password)
            db.export_pkcs12(temp.name, pk12_pwdfile.name, nickname)
            certs.install_pem_from_p12(temp.name, pk12_password,
                                       paths.HTTPD_CERT_FILE)
            certs.install_key_from_p12(temp.name, pk12_password,
                                       paths.HTTPD_KEY_FILE)

        self.backup_ssl_conf()
        self.configure_mod_ssl_certs()
        self.set_mod_ssl_protocol()
        self.set_mod_ssl_logdir()
        self.__add_include()

        self.cert = x509.load_certificate_from_file(paths.HTTPD_CERT_FILE)

        if self.ca_is_configured:
            db.untrack_server_cert(nickname)
            self.start_tracking_certificates()

        # remove nickname and CA certs from NSS db

        self.disable_nss_conf()
Beispiel #3
0
 def install_external_pkinit_certs(self):
     certs.install_pem_from_p12(self.pkcs12_info[0],
                                self.pkcs12_info[1],
                                paths.KDC_CERT)
     certs.install_key_from_p12(self.pkcs12_info[0],
                                self.pkcs12_info[1],
                                paths.KDC_KEY)
     self._install_pkinit_ca_bundle()
     self.pkinit_enable()
Beispiel #4
0
 def install_external_pkinit_certs(self):
     certs.install_pem_from_p12(self.pkcs12_info[0], self.pkcs12_info[1],
                                paths.KDC_CERT)
     # The KDC cert needs to be readable by everyone
     os.chmod(paths.KDC_CERT, 0o644)
     certs.install_key_from_p12(self.pkcs12_info[0], self.pkcs12_info[1],
                                paths.KDC_KEY)
     self._install_pkinit_ca_bundle()
     self.pkinit_enable()
    def setup_pkinit(self):
        if self.pkcs12_info:
            certs.install_pem_from_p12(self.pkcs12_info[0],
                                       self.pkcs12_info[1],
                                       paths.KDC_CERT)
            certs.install_key_from_p12(self.pkcs12_info[0],
                                       self.pkcs12_info[1],
                                       paths.KDC_KEY)
        else:
            subject = str(DN(('cn', self.fqdn), self.subject_base))
            krbtgt = "krbtgt/" + self.realm + "@" + self.realm
            certpath = (paths.KDC_CERT, paths.KDC_KEY)

            try:
                prev_helper = None
                if self.master_fqdn is None:
                    ca_args = [
                        paths.CERTMONGER_DOGTAG_SUBMIT,
                        '--ee-url', 'https://%s:8443/ca/ee/ca' % self.fqdn,
                        '--certfile', paths.RA_AGENT_PEM,
                        '--keyfile', paths.RA_AGENT_KEY,
                        '--cafile', paths.IPA_CA_CRT,
                        '--agent-submit'
                    ]
                    helper = " ".join(ca_args)
                    prev_helper = certmonger.modify_ca_helper('IPA', helper)
                else:
                    self._wait_for_replica_kdc_entry()

                certmonger.request_and_wait_for_cert(
                    certpath,
                    subject,
                    krbtgt,
                    dns=self.fqdn,
                    storage='FILE',
                    profile='KDCs_PKINIT_Certs')
            except dbus.DBusException as e:
                # if the certificate is already tracked, ignore the error
                name = e.get_dbus_name()
                if name != 'org.fedorahosted.certmonger.duplicate':
                    root_logger.error("Failed to initiate the request: %s", e)
                return
            finally:
                if prev_helper is not None:
                    certmonger.modify_ca_helper('IPA', prev_helper)

        # Finally copy the cacert in the krb directory so we don't
        # have any selinux issues with the file context
        shutil.copyfile(paths.IPA_CA_CRT, paths.CACERT_PEM)

        try:
            self.restart()
        except Exception:
            root_logger.critical("krb5kdc service failed to restart")
            raise
    def install_kdc_cert(self):
        ca_cert_file = paths.CA_BUNDLE_PEM
        pkcs12_file, pin, ca_cert = installutils.load_pkcs12(
            cert_files=self.args,
            key_password=self.options.pin,
            key_nickname=self.options.cert_name,
            ca_cert_files=[ca_cert_file],
            realm_name=api.env.realm)

        cdb = certs.CertDB(api.env.realm, nssdir=paths.IPA_NSSDB_DIR)

        # Check that the ca_cert is known and trusted
        with tempfile.NamedTemporaryFile() as temp:
            certs.install_pem_from_p12(pkcs12_file.name, pin, temp.name)

            kdc_cert = x509.load_certificate_from_file(temp.name)
            ca_certs = x509.load_certificate_list_from_file(ca_cert_file)

            try:
                verify_kdc_cert_validity(kdc_cert, ca_certs, api.env.realm)
            except ValueError as e:
                raise admintool.ScriptError(
                    "Peer's certificate issuer is not trusted (%s). "
                    "Please run ipa-cacert-manage install and ipa-certupdate "
                    "to install the CA certificate." % str(e))

        try:
            ca_enabled = api.Command.ca_is_enabled()['result']
            if ca_enabled:
                certmonger.stop_tracking(certfile=paths.KDC_CERT)

            certs.install_pem_from_p12(pkcs12_file.name, pin, paths.KDC_CERT)
            certs.install_key_from_p12(pkcs12_file.name, pin, paths.KDC_KEY)

            if ca_enabled:
                # Start tracking only if the cert was issued by IPA CA
                # Retrieve IPA CA
                ipa_ca_cert = cdb.get_cert_from_db(
                    get_ca_nickname(api.env.realm),
                    pem=False)
                # And compare with the CA which signed this certificate
                if ca_cert == ipa_ca_cert:
                    certmonger.start_tracking(
                        (paths.KDC_CERT, paths.KDC_KEY),
                        storage='FILE',
                        profile='KDCs_PKINIT_Certs')
        except RuntimeError as e:
            raise admintool.ScriptError(str(e))

        krb = krbinstance.KrbInstance()
        krb.init_info(
            realm_name=api.env.realm,
            host_name=api.env.host,
        )
        krb.pkinit_enable()
Beispiel #7
0
    def install_kdc_cert(self):
        ca_cert_file = paths.CA_BUNDLE_PEM
        pkcs12_file, pin, ca_cert = installutils.load_pkcs12(
            cert_files=self.args,
            key_password=self.options.pin,
            key_nickname=self.options.cert_name,
            ca_cert_files=[ca_cert_file],
            realm_name=api.env.realm)

        cdb = certs.CertDB(api.env.realm, nssdir=paths.IPA_NSSDB_DIR)

        # Check that the ca_cert is known and trusted
        with tempfile.NamedTemporaryFile() as temp:
            certs.install_pem_from_p12(pkcs12_file.name, pin, temp.name)

            kdc_cert = x509.load_certificate_from_file(temp.name)
            ca_certs = x509.load_certificate_list_from_file(ca_cert_file)

            try:
                verify_kdc_cert_validity(kdc_cert, ca_certs, api.env.realm)
            except ValueError as e:
                raise admintool.ScriptError(
                    "Peer's certificate issuer is not trusted (%s). "
                    "Please run ipa-cacert-manage install and ipa-certupdate "
                    "to install the CA certificate." % str(e))

        try:
            ca_enabled = api.Command.ca_is_enabled()['result']
            if ca_enabled:
                certmonger.stop_tracking(certfile=paths.KDC_CERT)

            certs.install_pem_from_p12(pkcs12_file.name, pin, paths.KDC_CERT)
            certs.install_key_from_p12(pkcs12_file.name, pin, paths.KDC_KEY)

            if ca_enabled:
                # Start tracking only if the cert was issued by IPA CA
                # Retrieve IPA CA
                ipa_ca_cert = cdb.get_cert_from_db(get_ca_nickname(
                    api.env.realm),
                                                   pem=False)
                # And compare with the CA which signed this certificate
                if ca_cert == ipa_ca_cert:
                    certmonger.start_tracking((paths.KDC_CERT, paths.KDC_KEY),
                                              storage='FILE',
                                              profile='KDCs_PKINIT_Certs')
        except RuntimeError as e:
            raise admintool.ScriptError(str(e))

        krb = krbinstance.KrbInstance()
        krb.init_info(
            realm_name=api.env.realm,
            host_name=api.env.host,
        )
        krb.pkinit_enable()
Beispiel #8
0
    def migrate_to_mod_ssl(self):
        """For upgrades only, migrate from mod_nss to mod_ssl"""
        db = certs.CertDB(api.env.realm, nssdir=paths.HTTPD_ALIAS_DIR)
        nickname = self.get_mod_nss_nickname()
        with tempfile.NamedTemporaryFile() as temp:
            pk12_password = ipautil.ipa_generate_password()
            pk12_pwdfile = ipautil.write_tmp_file(pk12_password)
            db.export_pkcs12(temp.name, pk12_pwdfile.name, nickname)
            certs.install_pem_from_p12(temp.name,
                                       pk12_password,
                                       paths.HTTPD_CERT_FILE)

            passwd_fname = paths.HTTPD_PASSWD_FILE_FMT.format(
                            host=api.env.host)
            with open(passwd_fname, 'wb') as passwd_file:
                os.fchmod(passwd_file.fileno(), 0o600)
                passwd_file.write(
                    ipautil.ipa_generate_password().encode('utf-8'))

            certs.install_key_from_p12(temp.name,
                                       pk12_password,
                                       paths.HTTPD_KEY_FILE,
                                       out_passwd_fname=passwd_fname)

        self.backup_ssl_conf()
        self.configure_mod_ssl_certs()
        self.set_mod_ssl_protocol()
        self.set_mod_ssl_logdir()
        self.__add_include()

        self.cert = x509.load_certificate_from_file(paths.HTTPD_CERT_FILE)

        if self.ca_is_configured:
            db.untrack_server_cert(nickname)
            self.start_tracking_certificates()

        # remove nickname and CA certs from NSS db

        self.disable_nss_conf()
Beispiel #9
0
    def load_pkcs12(self, ca_chain_fname=paths.IPA_CA_CRT, **kwargs):
        # Note that the "installutils.load_pkcs12" is quite a complex function
        # which performs some checking based on its kwargs:
        #       host_name performs NSSDatabase.verify_server_cert_validity()
        #       realm performs NSSDatabase.verify_kdc_cert_validity()
        pkcs12_file, pin, ca_cert = installutils.load_pkcs12(
            cert_files=self.args,
            key_password=self.options.pin,
            key_nickname=self.options.cert_name,
            ca_cert_files=[ca_chain_fname],
            **kwargs)

        # Check that the ca_cert is known and trusted
        with tempfile.NamedTemporaryFile() as temp:
            certs.install_pem_from_p12(pkcs12_file.name, pin, temp.name)
            cert = x509.load_certificate_from_file(temp.name)

        with tempfile.NamedTemporaryFile("rb") as temp:
            certs.install_key_from_p12(pkcs12_file.name, pin, temp.name)
            key = x509.load_pem_private_key(
                temp.read(), None, backend=x509.default_backend())

        return cert, key, ca_cert
    def load_pkcs12(self, ca_chain_fname=paths.IPA_CA_CRT, **kwargs):
        # Note that the "installutils.load_pkcs12" is quite a complex function
        # which performs some checking based on its kwargs:
        #       host_name performs NSSDatabase.verify_server_cert_validity()
        #       realm performs NSSDatabase.verify_kdc_cert_validity()
        pkcs12_file, pin, ca_cert = installutils.load_pkcs12(
            cert_files=self.args,
            key_password=self.options.pin,
            key_nickname=self.options.cert_name,
            ca_cert_files=[ca_chain_fname],
            **kwargs)

        # Check that the ca_cert is known and trusted
        with tempfile.NamedTemporaryFile() as temp:
            certs.install_pem_from_p12(pkcs12_file.name, pin, temp.name)
            cert = x509.load_certificate_from_file(temp.name)

        with tempfile.NamedTemporaryFile("rb") as temp:
            certs.install_key_from_p12(pkcs12_file.name, pin, temp.name)
            key = x509.load_pem_private_key(
                temp.read(), None, backend=x509.default_backend())

        return cert, key, ca_cert