]> WPIA git - gigi.git/blobdiff - util-testing/org/cacert/gigi/util/SimpleSigner.java
fix: Avoid NPE if the CA's files cannot be listed
[gigi.git] / util-testing / org / cacert / gigi / util / SimpleSigner.java
index 296eb99e5bb73a9f3c88697bc206b24d7e7bbacb..1cc9e9eeae6e8e7851e540113a6cc1da1fda9ce8 100644 (file)
@@ -152,6 +152,17 @@ public class SimpleSigner {
         runner.start();
     }
 
+    public static void ping() {
+        synchronized (SimpleSigner.class) {
+            SimpleSigner.class.notifyAll();
+            try {
+                SimpleSigner.class.wait(2000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
     private synchronized static void work() {
         try {
             gencrl();
@@ -166,6 +177,7 @@ public class SimpleSigner {
                 signCertificates();
                 revokeCertificates();
 
+                SimpleSigner.class.notifyAll();
                 SimpleSigner.class.wait(5000);
             } catch (IOException e) {
                 e.printStackTrace();
@@ -199,7 +211,8 @@ public class SimpleSigner {
             return;
         }
         String[] call = new String[] {
-                "openssl", "ca",//
+                "openssl",
+                "ca",//
                 "-cert",
                 "../unassured.crt",//
                 "-keyfile",
@@ -219,8 +232,6 @@ public class SimpleSigner {
         }
     }
 
-    private static int counter = 0;
-
     private static void signCertificates() throws SQLException {
         GigiResultSet rs = readyCerts.executeQuery();
 
@@ -282,7 +293,6 @@ public class SimpleSigner {
                 try (FileInputStream inStream = new FileInputStream("signer/profiles/" + s)) {
                     caP.load(inStream);
                 }
-                String ca = caP.getProperty("ca") + "_2015_1";
 
                 HashMap<String, String> subj = new HashMap<>();
                 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT name, value FROM `certAvas` WHERE `certId`=?")) {
@@ -317,7 +327,20 @@ public class SimpleSigner {
                     PKCS10 p10 = new PKCS10(PEM.decode("(NEW )?CERTIFICATE REQUEST", new String(data, "UTF-8")));
                     pk = p10.getSubjectPublicKeyInfo();
                 }
-                PrivateKey i = loadOpensslKey(new File("signer/ca/" + ca + "/ca.key"));
+                String ca = caP.getProperty("ca") + "_2015_1";
+                File parent = new File("signer/ca");
+                File[] caFiles = parent.listFiles();
+                if (null == caFiles) {
+                    caFiles = new File[0];
+                }
+                for (File f : caFiles) {
+                    if (f.getName().startsWith(caP.getProperty("ca"))) {
+                        ca = f.getName();
+                        break;
+                    }
+                }
+                File caKey = new File(parent, ca + "/ca.key");
+                PrivateKey i = loadOpensslKey(caKey);
 
                 X509Certificate root = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(new FileInputStream("signer/ca/" + ca + "/ca.crt"));
                 byte[] cert = generateCert(pk, i, subj, root.getSubjectX500Principal(), altnames, fromDate, toDate, Digest.valueOf(rs.getString("md").toUpperCase()), caP.getProperty("eku"));
@@ -476,7 +499,7 @@ public class SimpleSigner {
     private static byte[] generateKU() throws IOException {
         try (DerOutputStream dos = new DerOutputStream()) {
             dos.putBitString(new byte[] {
-                (byte) 0b10101000
+                    (byte) 0b10101000
             });
             return dos.toByteArray();
         }