>From 2cae54b97c1a315fd9c898e6ed06a45f430d980f Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Mon, 1 Aug 2016 22:35:32 +0200
Subject: [PATCH] Fixed PKCS #12 import for cloning.

To fix cloning issue in IPA the security_database.py has been
modified to import all certificates and keys in the PKCS #12 file
before the PKI server is started. Since the PKCS #12 generated by
IPA may not contain the certificate trust flags, the script will
also reset the trust flags on the imported certificates (i.e.
CT,C,C for CA certificate and u,u,Pu for audit certificate).

The ConfigurationUtils.restoreCertsFromP12() is now redundant and
it should be removed in the future, but for now it has been
modified to set the same trust flags on imported certificates.

The CryptoUtil.importCertificateChain() has also been modified to
set the same trust flags on imported certificates.

https://fedorahosted.org/pki/ticket/2424
---
 .../cms/servlet/csadmin/ConfigurationUtils.java    |  9 +++-
 .../deployment/scriptlets/security_databases.py    | 13 ++++-
 .../com/netscape/cmsutil/crypto/CryptoUtil.java    | 60 ++++++++++++----------
 3 files changed, 51 insertions(+), 31 deletions(-)

diff --git a/base/server/cms/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java b/base/server/cms/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
index fe65bb855a24702c2248d03059716d4246d70d0a..905a5bb662be23297a2e24125421cb1a11064a3d 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
@@ -834,7 +834,8 @@ public class ConfigurationUtils {
             BadPaddingException, NotInitializedException, NicknameConflictException, UserCertConflictException,
             NoSuchItemOnTokenException, InvalidBERException, IOException {
 
-        // TODO: refactor into a PKCS #12 utility class
+        // TODO: The PKCS #12 file is already imported in security_database.py.
+        // This method should be removed.
 
         byte b[] = new byte[1000000];
         FileInputStream fis = new FileInputStream(p12File);
@@ -1109,10 +1110,14 @@ public class ConfigurationUtils {
                 InternalCertificate icert = (InternalCertificate) xcert;
 
                 if (isCASigningCert) {
-                    // we need to change the trust attribute to CT
+                    // set trust flags to CT,C,C
                     icert.setSSLTrust(InternalCertificate.TRUSTED_CA
                             | InternalCertificate.TRUSTED_CLIENT_CA
                             | InternalCertificate.VALID_CA);
+                    icert.setEmailTrust(InternalCertificate.TRUSTED_CA
+                            | InternalCertificate.VALID_CA);
+                    icert.setObjectSigningTrust(InternalCertificate.TRUSTED_CA
+                            | InternalCertificate.VALID_CA);
 
                 } else if (isAuditSigningCert(name)) {
                     icert.setObjectSigningTrust(InternalCertificate.USER
diff --git a/base/server/python/pki/server/deployment/scriptlets/security_databases.py b/base/server/python/pki/server/deployment/scriptlets/security_databases.py
index 75b50e9260b2005fd3eb40b344465a9c963f8ae2..1a90a32e7ec02d8d0af435596f84f35653396975 100644
--- a/base/server/python/pki/server/deployment/scriptlets/security_databases.py
+++ b/base/server/python/pki/server/deployment/scriptlets/security_databases.py
@@ -161,8 +161,17 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
 
             nssdb.import_pkcs12(
                 pkcs12_file=pki_clone_pkcs12_path,
-                pkcs12_password=pki_clone_pkcs12_password,
-                no_user_certs=True)
+                pkcs12_password=pki_clone_pkcs12_password)
+
+            # Set certificate trust flags
+            if subsystem.type == 'CA':
+                nssdb.modify_cert(
+                    nickname=deployer.mdict['pki_ca_signing_nickname'],
+                    trust_attributes='CTu,Cu,Cu')
+
+            nssdb.modify_cert(
+                nickname=deployer.mdict['pki_audit_signing_nickname'],
+                trust_attributes='u,u,Pu')
 
             print('Imported certificates in %s:' % deployer.mdict['pki_database_path'])
 
diff --git a/base/util/src/com/netscape/cmsutil/crypto/CryptoUtil.java b/base/util/src/com/netscape/cmsutil/crypto/CryptoUtil.java
index 9cabdc5cc459afc0ab2b60450d29fa14e384f58d..b02c363e2af150cb4c74d394dd00d6d8650b0c58 100644
--- a/base/util/src/com/netscape/cmsutil/crypto/CryptoUtil.java
+++ b/base/util/src/com/netscape/cmsutil/crypto/CryptoUtil.java
@@ -47,33 +47,6 @@ import java.util.Random;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
-import netscape.security.pkcs.PKCS10;
-import netscape.security.pkcs.PKCS10Attribute;
-import netscape.security.pkcs.PKCS10Attributes;
-import netscape.security.pkcs.PKCS7;
-import netscape.security.pkcs.PKCS9Attribute;
-import netscape.security.util.BigInt;
-import netscape.security.util.DerInputStream;
-import netscape.security.util.DerOutputStream;
-import netscape.security.util.DerValue;
-import netscape.security.util.ObjectIdentifier;
-import netscape.security.x509.AlgorithmId;
-import netscape.security.x509.CertificateAlgorithmId;
-import netscape.security.x509.CertificateChain;
-import netscape.security.x509.CertificateExtensions;
-import netscape.security.x509.CertificateIssuerName;
-import netscape.security.x509.CertificateSerialNumber;
-import netscape.security.x509.CertificateSubjectName;
-import netscape.security.x509.CertificateValidity;
-import netscape.security.x509.CertificateVersion;
-import netscape.security.x509.CertificateX509Key;
-import netscape.security.x509.Extensions;
-import netscape.security.x509.X500Name;
-import netscape.security.x509.X500Signer;
-import netscape.security.x509.X509CertImpl;
-import netscape.security.x509.X509CertInfo;
-import netscape.security.x509.X509Key;
-
 import org.mozilla.jss.CryptoManager;
 import org.mozilla.jss.CryptoManager.NotInitializedException;
 import org.mozilla.jss.NoSuchTokenException;
@@ -132,6 +105,33 @@ import org.mozilla.jss.util.Password;
 import com.netscape.cmsutil.util.Cert;
 import com.netscape.cmsutil.util.Utils;
 
+import netscape.security.pkcs.PKCS10;
+import netscape.security.pkcs.PKCS10Attribute;
+import netscape.security.pkcs.PKCS10Attributes;
+import netscape.security.pkcs.PKCS7;
+import netscape.security.pkcs.PKCS9Attribute;
+import netscape.security.util.BigInt;
+import netscape.security.util.DerInputStream;
+import netscape.security.util.DerOutputStream;
+import netscape.security.util.DerValue;
+import netscape.security.util.ObjectIdentifier;
+import netscape.security.x509.AlgorithmId;
+import netscape.security.x509.CertificateAlgorithmId;
+import netscape.security.x509.CertificateChain;
+import netscape.security.x509.CertificateExtensions;
+import netscape.security.x509.CertificateIssuerName;
+import netscape.security.x509.CertificateSerialNumber;
+import netscape.security.x509.CertificateSubjectName;
+import netscape.security.x509.CertificateValidity;
+import netscape.security.x509.CertificateVersion;
+import netscape.security.x509.CertificateX509Key;
+import netscape.security.x509.Extensions;
+import netscape.security.x509.X500Name;
+import netscape.security.x509.X500Signer;
+import netscape.security.x509.X509CertImpl;
+import netscape.security.x509.X509CertInfo;
+import netscape.security.x509.X509Key;
+
 @SuppressWarnings("serial")
 public class CryptoUtil {
 
@@ -1164,10 +1164,16 @@ public class CryptoUtil {
         if (certchains != null) {
             cert = certchains[certchains.length - 1];
         }
+
+        // set trust flags to CT,C,C
         InternalCertificate icert = (InternalCertificate) cert;
         icert.setSSLTrust(InternalCertificate.TRUSTED_CA
                                     | InternalCertificate.TRUSTED_CLIENT_CA
                                     | InternalCertificate.VALID_CA);
+        icert.setEmailTrust(InternalCertificate.TRUSTED_CA
+                | InternalCertificate.VALID_CA);
+        icert.setObjectSigningTrust(InternalCertificate.TRUSTED_CA
+                | InternalCertificate.VALID_CA);
     }
 
     public static SEQUENCE parseCRMFMsgs(byte cert_request[])
-- 
2.5.5

