diff options
author | Endi S. Dewata <edewata@redhat.com> | 2016-06-14 02:00:16 +0200 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2016-06-16 04:22:03 +0200 |
commit | 61e5bff2bc89edf96c90d8ff063529af213afcbe (patch) | |
tree | 176a7de4034fd13ece2f19305efcb7810d1328b1 /base/java-tools/src/com/netscape/cmstools/pkcs12 | |
parent | 82e738db05be6ea844ad89bce31879960953d5e4 (diff) | |
download | pki-61e5bff2bc89edf96c90d8ff063529af213afcbe.tar.gz pki-61e5bff2bc89edf96c90d8ff063529af213afcbe.tar.xz pki-61e5bff2bc89edf96c90d8ff063529af213afcbe.zip |
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.
Diffstat (limited to 'base/java-tools/src/com/netscape/cmstools/pkcs12')
-rw-r--r-- | base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertCLI.java | 1 | ||||
-rw-r--r-- | base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java | 174 |
2 files changed, 175 insertions, 0 deletions
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 fe7092c00..920cff8fc 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 000000000..a0db0f766 --- /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 + "\""); + } +} |