A new CLI has been added to update the certificate trust flags in
PKCS #12 file which will be useful to import OpenSSL certificates.

--
Endi S. Dewata
>From e14c04c9b62fff9e99eab286fd4b560eee6ed7ee Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <[email protected]>
Date: Tue, 14 Jun 2016 02:00:16 +0200
Subject: [PATCH] Added pki pkcs12-cert-mod command.

A new CLI has been added to update the certificate trust flags in
PKCS #12 file which will be useful to import OpenSSL certificates.
---
 .../netscape/cmstools/pkcs12/PKCS12CertCLI.java    |   1 +
 .../netscape/cmstools/pkcs12/PKCS12CertModCLI.java | 174 +++++++++++++++++++++
 2 files changed, 175 insertions(+)
 create mode 100644 base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java

diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertCLI.java
index fe7092c00ef3e4ee202d47b479ec22caea5f36c1..920cff8fc6028473d9cefa58958f0a6322eca14b 100644
--- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertCLI.java
@@ -37,6 +37,7 @@ public class PKCS12CertCLI extends CLI {
         addModule(new PKCS12CertAddCLI(this));
         addModule(new PKCS12CertExportCLI(this));
         addModule(new PKCS12CertFindCLI(this));
+        addModule(new PKCS12CertModCLI(this));
         addModule(new PKCS12CertRemoveCLI(this));
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java
new file mode 100644
index 0000000000000000000000000000000000000000..a0db0f766c7f8ad93aba7a678100d25f484df460
--- /dev/null
+++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java
@@ -0,0 +1,174 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2016 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cmstools.pkcs12;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.util.Collection;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.ParseException;
+import org.mozilla.jss.util.Password;
+
+import com.netscape.cmstools.cli.CLI;
+import com.netscape.cmstools.cli.MainCLI;
+
+import netscape.security.pkcs.PKCS12;
+import netscape.security.pkcs.PKCS12CertInfo;
+import netscape.security.pkcs.PKCS12Util;
+
+/**
+ * @author Endi S. Dewata
+ */
+public class PKCS12CertModCLI extends CLI {
+
+    public PKCS12CertModCLI(PKCS12CertCLI certCLI) {
+        super("mod", "Modify certificate in PKCS #12 file", certCLI);
+
+        createOptions();
+    }
+
+    public void printHelp() {
+        formatter.printHelp(getFullName() + " <nickname> [OPTIONS...]", options);
+    }
+
+    public void createOptions() {
+        Option option = new Option(null, "pkcs12-file", true, "PKCS #12 file");
+        option.setArgName("path");
+        options.addOption(option);
+
+        option = new Option(null, "pkcs12-password", true, "PKCS #12 password");
+        option.setArgName("password");
+        options.addOption(option);
+
+        option = new Option(null, "pkcs12-password-file", true, "PKCS #12 password file");
+        option.setArgName("path");
+        options.addOption(option);
+
+        option = new Option(null, "trust-flags", true, "Certificate trust flags");
+        option.setArgName("flags");
+        options.addOption(option);
+
+        options.addOption("v", "verbose", false, "Run in verbose mode.");
+        options.addOption(null, "debug", false, "Run in debug mode.");
+        options.addOption(null, "help", false, "Show help message.");
+    }
+
+    public void execute(String[] args) throws Exception {
+
+        CommandLine cmd = null;
+
+        try {
+            cmd = parser.parse(options, args);
+        } catch (ParseException e) {
+            System.err.println("Error: " + e.getMessage());
+            printHelp();
+            System.exit(-1);
+        }
+
+        if (cmd.hasOption("help")) {
+            printHelp();
+            System.exit(0);
+        }
+
+        if (cmd.hasOption("verbose")) {
+            Logger.getLogger("org.dogtagpki").setLevel(Level.INFO);
+            Logger.getLogger("com.netscape").setLevel(Level.INFO);
+            Logger.getLogger("netscape").setLevel(Level.INFO);
+
+        } else if (cmd.hasOption("debug")) {
+            Logger.getLogger("org.dogtagpki").setLevel(Level.FINE);
+            Logger.getLogger("com.netscape").setLevel(Level.FINE);
+            Logger.getLogger("netscape").setLevel(Level.FINE);
+        }
+
+        String[] cmdArgs = cmd.getArgs();
+
+        if (cmdArgs.length == 0) {
+            System.err.println("Error: Missing certificate nickname.");
+            printHelp();
+            System.exit(-1);
+        }
+
+        String nickname = cmdArgs[0];
+
+        String filename = cmd.getOptionValue("pkcs12-file");
+
+        if (filename == null) {
+            System.err.println("Error: Missing PKCS #12 file.");
+            printHelp();
+            System.exit(-1);
+        }
+
+        String passwordString = cmd.getOptionValue("pkcs12-password");
+
+        if (passwordString == null) {
+
+            String passwordFile = cmd.getOptionValue("pkcs12-password-file");
+            if (passwordFile != null) {
+                try (BufferedReader in = new BufferedReader(new FileReader(passwordFile))) {
+                    passwordString = in.readLine();
+                }
+            }
+        }
+
+        if (passwordString == null) {
+            System.err.println("Error: Missing PKCS #12 password.");
+            printHelp();
+            System.exit(-1);
+        }
+
+        Password password = new Password(passwordString.toCharArray());
+
+        String trustFlags = cmd.getOptionValue("trust-flags");
+
+        if (trustFlags == null) {
+            System.err.println("Error: Missing trust flags.");
+            printHelp();
+            System.exit(-1);
+        }
+
+        try {
+            PKCS12Util util = new PKCS12Util();
+            PKCS12 pkcs12 = util.loadFromFile(filename, password);
+            Collection<PKCS12CertInfo> certInfos = pkcs12.getCertInfosByNickname(nickname);
+
+            if (trustFlags.equals("")) { // remove trust flags
+                for (PKCS12CertInfo certInfo : certInfos) {
+                    certInfo.setTrustFlags(null);
+                }
+
+            } else { // set trust flags
+                for (PKCS12CertInfo certInfo : certInfos) {
+                    certInfo.setTrustFlags(trustFlags);
+                }
+            }
+
+            util.storeIntoFile(pkcs12, filename, password);
+
+        } finally {
+            password.clear();
+        }
+
+        MainCLI.printMessage("Updated certificate \"" + nickname + "\"");
+    }
+}
-- 
2.5.5

_______________________________________________
Pki-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/pki-devel

Reply via email to