From 60835ed008586f85a22737d0161cb026f2dbffec Mon Sep 17 00:00:00 2001 From: Endi Sukma Dewata Date: Sat, 18 Aug 2012 02:30:44 -0500 Subject: Moved REST CLI into pki-tools. The pki-client.jar has been split and merged into pki-certsrv.jar and pki-tools.jar. The REST client classes are now packaged in com.netscape.certsrv. packages. The REST CLI classes are now packaged in com.netscape.cmstools. packages. The "pki" script has been moved into pki-tools RPM package. Ticket #215 --- .../src/com/netscape/cmstools/cert/CertCLI.java | 152 ++++++++ .../com/netscape/cmstools/cert/CertFindCLI.java | 388 +++++++++++++++++++++ .../com/netscape/cmstools/cert/CertHoldCLI.java | 118 +++++++ .../netscape/cmstools/cert/CertReleaseHoldCLI.java | 109 ++++++ .../cmstools/cert/CertRequestApproveCLI.java | 69 ++++ .../cmstools/cert/CertRequestReviewCLI.java | 103 ++++++ .../cmstools/cert/CertRequestSubmitCLI.java | 85 +++++ .../com/netscape/cmstools/cert/CertRevokeCLI.java | 164 +++++++++ .../com/netscape/cmstools/cert/CertShowCLI.java | 95 +++++ 9 files changed, 1283 insertions(+) create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertRequestApproveCLI.java create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java create mode 100644 base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java (limited to 'base/java-tools/src/com/netscape/cmstools/cert') diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java new file mode 100644 index 000000000..6857b689e --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java @@ -0,0 +1,152 @@ +// --- 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) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.cert; + +import java.util.Arrays; + +import org.apache.commons.lang.StringUtils; +import org.jboss.resteasy.plugins.providers.atom.Link; + +import com.netscape.certsrv.cert.CertClient; +import com.netscape.certsrv.cert.CertData; +import com.netscape.certsrv.cert.CertDataInfo; +import com.netscape.certsrv.cert.CertRequestInfo; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class CertCLI extends CLI { + + public MainCLI parent; + public CertClient client; + + public CertCLI(MainCLI parent) { + super("cert", "Certificate management commands"); + this.parent = parent; + + addModule(new CertFindCLI(this)); + addModule(new CertShowCLI(this)); + + addModule(new CertRevokeCLI(this)); + addModule(new CertHoldCLI(this)); + addModule(new CertReleaseHoldCLI(this)); + addModule(new CertRequestSubmitCLI(this)); + addModule(new CertRequestReviewCLI(this)); + addModule(new CertRequestApproveCLI(this)); + } + + public void printHelp() { + + System.out.println("Commands:"); + + int leftPadding = 1; + int rightPadding = 25; + + for (CLI module : modules.values()) { + String label = name + "-" + module.getName(); + + int padding = rightPadding - leftPadding - label.length(); + if (padding < 1) + padding = 1; + + System.out.print(StringUtils.repeat(" ", leftPadding)); + System.out.print(label); + System.out.print(StringUtils.repeat(" ", padding)); + System.out.println(module.getDescription()); + } + } + + public void execute(String[] args) throws Exception { + + client = new CertClient(parent.config); + client.setVerbose(verbose); + + if (args.length == 0) { + printHelp(); + System.exit(1); + } + + String command = args[0]; + String[] commandArgs = Arrays.copyOfRange(args, 1, args.length); + + if (command == null) { + printHelp(); + System.exit(1); + } + + CLI module = getModule(command); + if (module != null) { + module.execute(commandArgs); + + } else { + System.err.println("Error: Invalid command \"" + command + "\""); + printHelp(); + System.exit(1); + } + } + + public static void printCertInfo(CertDataInfo info) { + System.out.println(" Serial Number: "+info.getID().toHexString()); + System.out.println(" Subject DN: "+info.getSubjectDN()); + System.out.println(" Status: "+info.getStatus()); + + Link link = info.getLink(); + if (verbose && link != null) { + System.out.println(" Link: " + link.getHref()); + } + } + + public static void printCertData( + CertData certData, + boolean showPrettyPrint, + boolean showEncoded) { + + System.out.println(" Serial Number: " + certData.getSerialNumber().toHexString()); + System.out.println(" Issuer: " + certData.getIssuerDN()); + System.out.println(" Subject: " + certData.getSubjectDN()); + System.out.println(" Status: " + certData.getStatus()); + System.out.println(" Not Before: " + certData.getNotBefore()); + System.out.println(" Not After: " + certData.getNotAfter()); + + Link link = certData.getLink(); + if (verbose && link != null) { + System.out.println(" Link: " + link.getHref()); + } + + String prettyPrint = certData.getPrettyPrint(); + if (showPrettyPrint && prettyPrint != null) { + System.out.println(); + System.out.println(prettyPrint); + } + + String encoded = certData.getEncoded(); + if (showEncoded && encoded != null) { + System.out.println(); + System.out.println(encoded); + } + } + + public static void printCertRequestInfo(CertRequestInfo info) { + System.out.println(" Request ID: " + info.getRequestId()); + System.out.println(" Status: " + info.getRequestStatus()); + System.out.println(" Type: " + info.getRequestType()); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java new file mode 100644 index 000000000..397df3f93 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java @@ -0,0 +1,388 @@ +// --- 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) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.cert; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +import javax.xml.bind.JAXBException; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.ParseException; + +import com.netscape.certsrv.base.PKIException; +import com.netscape.certsrv.cert.CertDataInfo; +import com.netscape.certsrv.cert.CertDataInfos; +import com.netscape.certsrv.cert.CertSearchRequest; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class CertFindCLI extends CLI { + + public CertCLI parent; + + public CertFindCLI(CertCLI parent) { + super("find", "Find certificates"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " [OPTIONS...]", options); + } + + public void execute(String[] args) { + + addOptions(); + + CommandLine cmd = null; + CertSearchRequest searchData = 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(-1); + } + + String fileName = null; + + if (cmd.hasOption("input")) { + fileName = cmd.getOptionValue("input"); + if (fileName == null || fileName.length() < 1) { + System.err.println("Error: No file name specified."); + printHelp(); + System.exit(-1); + } + } + if (fileName != null) { + FileReader reader = null; + try { + reader = new FileReader(fileName); + searchData = CertSearchRequest.valueOf(reader); + } catch (FileNotFoundException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(-1); + } catch (JAXBException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(-1); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } else { + searchData = new CertSearchRequest(); + searchData.setSerialNumberRangeInUse(true); + } + String s = cmd.getOptionValue("start"); + Integer start = s == null ? null : Integer.valueOf(s); + + s = cmd.getOptionValue("size"); + Integer size = s == null ? null : Integer.valueOf(s); + + addSearchAttribute(cmd, searchData); + CertDataInfos certs = null; + try { + certs = parent.client.findCerts(searchData, start, size); + } catch (PKIException e) { + System.err.println("Error: Cannot list certificates. " + e.getMessage()); + System.exit(-1); + } + if (certs.getCertInfos() == null || certs.getCertInfos().isEmpty()) { + MainCLI.printMessage("No matches found."); + System.exit(-1); + } + MainCLI.printMessage(certs.getCertInfos().size() + " certificate(s) matched"); + + boolean first = true; + + for (CertDataInfo cert : certs.getCertInfos()) { + if (first) { + first = false; + } else { + System.out.println(); + } + + CertCLI.printCertInfo(cert); + } + + MainCLI.printMessage("Number of entries returned " + certs.getCertInfos().size()); + } + + public void addOptions() { + + Option option = null; + + //pagination options + option = new Option(null, "start", true, "Page start"); + option.setArgName("start"); + options.addOption(option); + + option = new Option(null, "size", true, "Page size"); + option.setArgName("size"); + options.addOption(option); + + //help + options.addOption(null, "help", false, "Show help options"); + + //file input + option = new Option(null, "input", true, "File containing the search constraints"); + option.setArgName("file path"); + options.addOption(option); + + //serialNumberinUse + option = new Option(null, "minSerialNumber", true, "Minimum serial number"); + option.setArgName("serial number"); + options.addOption(option); + option = new Option(null, "maxSerialNumber", true, "Maximum serial number"); + option.setArgName("serial number"); + options.addOption(option); + + //subjectNameinUse + option = new Option(null, "name", true, "Subject's common name"); + option.setArgName("name"); + options.addOption(option); + option = new Option(null, "email", true, "Subject's email address"); + option.setArgName("email"); + options.addOption(option); + option = new Option(null, "uid", true, "Subject's userid"); + option.setArgName("user id"); + options.addOption(option); + option = new Option(null, "org", true, "Subject's organization"); + option.setArgName("name"); + options.addOption(option); + option = new Option(null, "orgUnit", true, "Subject's organization unit"); + option.setArgName("name"); + options.addOption(option); + option = new Option(null, "locality", true, "Subject's locality"); + option.setArgName("name"); + options.addOption(option); + option = new Option(null, "state", true, "Subject's state"); + option.setArgName("name"); + options.addOption(option); + option = new Option(null, "country", true, "Subject's country"); + option.setArgName("name"); + options.addOption(option); + options.addOption(null, "matchExactly", false, "Match exactly with the details provided"); + + //revokedByInUse + option = new Option(null, "revokedBy", true, "Certificate revoked by"); + option.setArgName("user id"); + options.addOption(option); + + //revocationPeriod + option = new Option(null, "revokedOnFrom", true, "Revoked on or after this date"); + option.setArgName("date"); + options.addOption(option); + option = new Option(null, "revokedOnTo", true, "Revoked on or before this date"); + option.setArgName("date"); + options.addOption(option); + + //revocationReason + option = new Option(null, "revocationReason", true, "Reason for revocation"); + option.setArgName("reason"); + options.addOption(option); + + //issuedBy + option = new Option(null, "issuedBy", true, "Issued by"); + option.setArgName("user id"); + options.addOption(option); + + //issuedFor(period) + option = new Option(null, "issuedOn", true, "Date issued"); + option.setArgName("date"); + options.addOption(option); + + //certTypeinUse + option = new Option(null, "certTypeSubEmailCA", true, "Certifiate type: Subject Email CA"); + option.setArgName("on|off"); + options.addOption(option); + option = new Option(null, "certTypeSubSSLCA", true, "Certificate type: Subject SSL CA"); + option.setArgName("on|off"); + options.addOption(option); + option = new Option(null, "certTypeSecureEmail", true, "Certifiate Type: Secure Email"); + option.setArgName("on|off"); + options.addOption(option); + option = new Option(null, "certTypeSSLClient", true, "Certifiate Type: SSL Client"); + option.setArgName("on|off"); + options.addOption(option); + option = new Option(null, "certTypeSSLServer", true, "Certifiate Type: SSL Server"); + option.setArgName("on|off"); + options.addOption(option); + + //validationNotBeforeInUse + option = new Option(null, "validNotBeforeFrom", true, "Valid not before start date"); + option.setArgName("date"); + options.addOption(option); + option = new Option(null, "validNotBeforeTo", true, "Valid not before end date"); + option.setArgName("date"); + options.addOption(option); + + //validityNotAfterinUse + option = new Option(null, "validNotAfterFrom", true, "Valid not after start date"); + option.setArgName("date"); + options.addOption(option); + option = new Option(null, "validNotAfterTo", true, "Valid not after end date"); + option.setArgName("date"); + options.addOption(option); + + //validityLengthinUse + option = new Option(null, "validityOperation", true, "Validity operation: \"<=\" or \">=\""); + option.setArgName("operation"); + options.addOption(option); + option = new Option(null, "validityCount", true, "Validity count"); + option.setArgName("count"); + options.addOption(option); + option = new Option(null, "validityUnit", true, "Validity unit"); + option.setArgName("milliseconds"); + options.addOption(option); + } + + public void addSearchAttribute(CommandLine cmd, CertSearchRequest csd) { + if (cmd.hasOption("minSerialNumber")) { + csd.setSerialNumberRangeInUse(true); + csd.setSerialFrom(cmd.getOptionValue("minSerialNumber")); + } + if (cmd.hasOption("maxSerialNumber")) { + csd.setSerialNumberRangeInUse(true); + csd.setSerialTo(cmd.getOptionValue("maxSerialNumber")); + } + if (cmd.hasOption("name")) { + csd.setSubjectInUse(true); + csd.setCommonName(cmd.getOptionValue("name")); + } + if (cmd.hasOption("email")) { + csd.setSubjectInUse(true); + csd.setEmail(cmd.getOptionValue("email")); + } + if (cmd.hasOption("uid")) { + csd.setSubjectInUse(true); + csd.setUserID(cmd.getOptionValue("uid")); + } + if (cmd.hasOption("org")) { + csd.setSubjectInUse(true); + csd.setOrg(cmd.getOptionValue("org")); + } + if (cmd.hasOption("orgUnit")) { + csd.setSubjectInUse(true); + csd.setOrgUnit(cmd.getOptionValue("orgUnit")); + } + if (cmd.hasOption("locality")) { + csd.setSubjectInUse(true); + csd.setLocality(cmd.getOptionValue("locality")); + } + if (cmd.hasOption("state")) { + csd.setSubjectInUse(true); + csd.setState(cmd.getOptionValue("state")); + } + if (cmd.hasOption("country")) { + csd.setSubjectInUse(true); + csd.setCountry(cmd.getOptionValue("country")); + } + if (cmd.hasOption("matchExactly")) { + csd.setMatchExactly(true); + } + if (cmd.hasOption("revokedBy")) { + csd.setRevokedByInUse(true); + csd.setRevokedBy(cmd.getOptionValue("revokedBy")); + } + if (cmd.hasOption("revokedOnFrom")) { + csd.setRevokedOnInUse(true); + csd.setRevokedOnFrom(cmd.getOptionValue("revokedOnFrom")); + } + if (cmd.hasOption("revokedOnTo")) { + csd.setRevokedOnInUse(true); + csd.setRevokedOnTo(cmd.getOptionValue("revokedOnTo")); + } + if (cmd.hasOption("revocationReason")) { + csd.setRevocationReasonInUse(true); + csd.setRevocationReason(cmd.getOptionValue("revocationReason")); + } + if (cmd.hasOption("issuedBy")) { + csd.setIssuedByInUse(true); + csd.setIssuedBy(cmd.getOptionValue("issuedBy")); + } + if (cmd.hasOption("issuedOn")) { + csd.setIssuedOnInUse(true); + csd.setIssuedOnFrom(cmd.getOptionValue("issuedOn")); + } + if (cmd.hasOption("certTypeSubEmailCA")) { + csd.setCertTypeInUse(true); + csd.setCertTypeSubEmailCA(cmd.getOptionValue("certTypeSubEmailCA")); + } + if (cmd.hasOption("certTypeSubSSLCA")) { + csd.setCertTypeInUse(true); + csd.setCertTypeSubSSLCA(cmd.getOptionValue("certTypeSubSSLCA")); + } + if (cmd.hasOption("certTypeSecureEmail")) { + csd.setCertTypeInUse(true); + csd.setCertTypeSecureEmail(cmd.getOptionValue("certTypeSecureEmail")); + } + if (cmd.hasOption("certTypeSSLClient")) { + csd.setCertTypeInUse(true); + csd.setCertTypeSSLClient(cmd.getOptionValue("certTypeSSLCllient")); + } + if (cmd.hasOption("certTypeSSLServer")) { + csd.setCertTypeInUse(true); + csd.setCertTypeSSLServer(cmd.getOptionValue("certTypeSSLServer")); + } + if (cmd.hasOption("validNotBeforeFrom")) { + csd.setValidNotBeforeInUse(true); + csd.setValidNotBeforeFrom(cmd.getOptionValue("validNotBeforeFrom")); + } + if (cmd.hasOption("validNotBeforeTo")) { + csd.setValidNotBeforeInUse(true); + csd.setValidNotBeforeTo(cmd.getOptionValue("validNotBeforeTo")); + } + if (cmd.hasOption("validNotAfterFrom")) { + csd.setValidNotAfterInUse(true); + csd.setValidNotAfterFrom(cmd.getOptionValue("validNotAfterFrom")); + } + if (cmd.hasOption("validNotAfterTo")) { + csd.setValidNotAfterInUse(true); + csd.setValidNotAfterTo(cmd.getOptionValue("validNotAfterTo")); + } + if (cmd.hasOption("validityOperation")) { + csd.setValidityLengthInUse(true); + csd.setValidityOperation(cmd.getOptionValue("validityOperation")); + } + if (cmd.hasOption("validityCount")) { + csd.setValidityLengthInUse(true); + csd.setValidityCount(cmd.getOptionValue("validityCount")); + } + if (cmd.hasOption("validityUnit")) { + csd.setValidityLengthInUse(true); + csd.setValidityUnit(cmd.getOptionValue("validityUnit")); + } + + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java new file mode 100644 index 000000000..b5c604a86 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java @@ -0,0 +1,118 @@ +// --- 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) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.cert; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import netscape.security.x509.RevocationReason; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.cert.CertData; +import com.netscape.certsrv.cert.CertRequestInfo; +import com.netscape.certsrv.cert.CertRevokeRequest; +import com.netscape.certsrv.dbs.certdb.CertId; +import com.netscape.certsrv.request.RequestStatus; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class CertHoldCLI extends CLI { + + public CertCLI parent; + + public CertHoldCLI(CertCLI parent) { + super("hold", "Place certificate on-hold"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "comments", true, "Comments"); + option.setArgName("comments"); + options.addOption(option); + + options.addOption(null, "force", false, "Force"); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + CertId certID = new CertId(cmdArgs[0]); + + if (!cmd.hasOption("force")) { + + CertData certData = parent.client.getCert(certID); + + System.out.println("Placing certificate on-hold:"); + + CertCLI.printCertData(certData, false, false); + + System.out.print("Are you sure (Y/N)? "); + System.out.flush(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String line = reader.readLine(); + if (!line.equalsIgnoreCase("Y")) { + System.exit(1); + } + } + + CertRevokeRequest request = new CertRevokeRequest(); + request.setReason(RevocationReason.CERTIFICATE_HOLD); + request.setComments(cmd.getOptionValue("comments")); + + CertRequestInfo certRequestInfo = parent.client.revokeCert(certID, request); + + if (verbose) { + CertCLI.printCertRequestInfo(certRequestInfo); + } + + if (certRequestInfo.getRequestStatus() == RequestStatus.COMPLETE) { + MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" on-hold"); + CertData certData = parent.client.getCert(certID); + CertCLI.printCertData(certData, false, false); + + } else { + MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": " + certRequestInfo.getRequestStatus()); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java new file mode 100644 index 000000000..184976f1e --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java @@ -0,0 +1,109 @@ +// --- 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) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.cert; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import org.apache.commons.cli.CommandLine; + +import com.netscape.certsrv.cert.CertData; +import com.netscape.certsrv.cert.CertRequestInfo; +import com.netscape.certsrv.cert.CertUnrevokeRequest; +import com.netscape.certsrv.dbs.certdb.CertId; +import com.netscape.certsrv.request.RequestStatus; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class CertReleaseHoldCLI extends CLI { + + public CertCLI parent; + + public CertReleaseHoldCLI(CertCLI parent) { + super("release-hold", "Place certificate off-hold"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + options.addOption(null, "force", false, "Force"); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + CertId certID = new CertId(cmdArgs[0]); + + if (!cmd.hasOption("force")) { + + CertData certData = parent.client.getCert(certID); + + System.out.println("Placing certificate off-hold:"); + + CertCLI.printCertData(certData, false, false); + + System.out.print("Are you sure (Y/N)? "); + System.out.flush(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String line = reader.readLine(); + if (!line.equalsIgnoreCase("Y")) { + System.exit(1); + } + } + + CertUnrevokeRequest request = new CertUnrevokeRequest(); + + CertRequestInfo certRequestInfo = parent.client.unrevokeCert(certID, request); + + if (verbose) { + CertCLI.printCertRequestInfo(certRequestInfo); + } + + if (certRequestInfo.getRequestStatus() == RequestStatus.COMPLETE) { + MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" off-hold"); + CertData certData = parent.client.getCert(certID); + CertCLI.printCertData(certData, false, false); + + } else { + MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": " + certRequestInfo.getRequestStatus()); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestApproveCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestApproveCLI.java new file mode 100644 index 000000000..98a3a2b4f --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestApproveCLI.java @@ -0,0 +1,69 @@ +package com.netscape.cmstools.cert; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.ParseException; + +import com.netscape.certsrv.base.PKIException; +import com.netscape.certsrv.cert.CertReviewResponse; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class CertRequestApproveCLI extends CLI { + CertCLI parent; + + public CertRequestApproveCLI(CertCLI parent) { + super("request-approve", "Approve certificate request"); + this.parent = parent; + } + + @Override + public void execute(String[] args) { + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + } catch (ParseException e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(-1); + } + + String[] cLineArgs = cmd.getArgs(); + + if (cLineArgs.length < 1) { + System.err.println("Error: No file name specified."); + printHelp(); + System.exit(-1); + } + CertReviewResponse reviewInfo = null; + try { + JAXBContext context = JAXBContext.newInstance(CertReviewResponse.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + FileInputStream fis = new FileInputStream(cLineArgs[0].trim()); + reviewInfo = (CertReviewResponse) unmarshaller.unmarshal(fis); + parent.client.approveRequest(reviewInfo.getRequestId(), reviewInfo); + } catch (PKIException e) { + System.err.println(e.getMessage()); + System.exit(-1); + } catch (JAXBException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(-1); + } catch (FileNotFoundException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(-1); + } + MainCLI.printMessage("Approved certificate request " + reviewInfo.getRequestId().toString()); + } + + @Override + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " ", options); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java new file mode 100644 index 000000000..682314b25 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java @@ -0,0 +1,103 @@ +package com.netscape.cmstools.cert; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.ParseException; + +import com.netscape.certsrv.base.PKIException; +import com.netscape.certsrv.cert.CertReviewResponse; +import com.netscape.certsrv.request.RequestId; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class CertRequestReviewCLI extends CLI { + + CertCLI parent; + + public CertRequestReviewCLI(CertCLI parent) { + super("request-review", "Review certificate request"); + this.parent = parent; + } + + @Override + public void execute(String[] args) { + CommandLine cmd = null; + + Option output = new Option(null, "output", true, "Output Filename"); + options.addOption(output); + + try { + cmd = parser.parse(options, args); + } catch (ParseException e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(-1); + } + + String[] cLineArgs = cmd.getArgs(); + + if (cLineArgs.length < 1) { + System.err.println("Error: No request id specified."); + printHelp(); + System.exit(-1); + } + String filename = null; + if (cmd.hasOption("output")) { + filename = cmd.getOptionValue("output"); + } else { + System.err.println("No output option specified."); + printHelp(); + System.exit(-1); + } + + if (filename == null || filename.trim().length() == 0) { + System.err.println("Specify the filename to write the request information"); + printHelp(); + System.exit(-1); + } + + RequestId reqId = null; + try { + reqId = new RequestId(cLineArgs[0]); + } catch (NumberFormatException e) { + System.err.println("Error: Invalid RequestID: " + cLineArgs[0]); + System.exit(-1); + } + + CertReviewResponse reviewInfo = null; + try { + reviewInfo = parent.client.reviewRequest(reqId); + } catch (PKIException e) { + System.err.println(e.getMessage()); + System.exit(-1); + } + + try { + JAXBContext context = JAXBContext.newInstance(CertReviewResponse.class); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + + FileOutputStream stream = new FileOutputStream(filename); + + marshaller.marshal(reviewInfo, stream); + MainCLI.printMessage("Downloaded certificate request " + cLineArgs[0]); + } catch (JAXBException e) { + System.err.println("Cannot write to the file. " + e); + } catch (FileNotFoundException e) { + System.err.println("File not found at " + filename); + } + + } + + @Override + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " ", options); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java new file mode 100644 index 000000000..3562d48f0 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java @@ -0,0 +1,85 @@ +package com.netscape.cmstools.cert; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.Collection; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.ParseException; + +import com.netscape.certsrv.cert.CertEnrollmentRequest; +import com.netscape.certsrv.cert.CertRequestInfo; +import com.netscape.certsrv.cert.CertRequestInfos; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +public class CertRequestSubmitCLI extends CLI { + + CertCLI parent; + + public CertRequestSubmitCLI(CertCLI parent) { + super("request-submit", "Submit certificate request"); + this.parent = parent; + } + + @Override + public void execute(String[] args) { + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + } catch (ParseException e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(-1); + } + + String[] cLineArgs = cmd.getArgs(); + + if (cLineArgs.length < 1) { + System.err.println("Error: No filename specified."); + printHelp(); + System.exit(-1); + } + + CertEnrollmentRequest erd = null; + + try { + erd = getEnrollmentRequest(cLineArgs[0]); + CertRequestInfos cri = parent.client.enrollRequest(erd); + MainCLI.printMessage("Submitted certificate request"); + printRequestInformation(cri); + } catch (FileNotFoundException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(-1); + } catch (JAXBException e) { + System.err.println("Error: " + e.getMessage()); + System.exit(-1); + } + } + + private CertEnrollmentRequest getEnrollmentRequest(String fileName) throws JAXBException, FileNotFoundException { + CertEnrollmentRequest erd = null; + JAXBContext context = JAXBContext.newInstance(CertEnrollmentRequest.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + FileInputStream fis = new FileInputStream(fileName); + erd = (CertEnrollmentRequest) unmarshaller.unmarshal(fis); + return erd; + } + + private void printRequestInformation(CertRequestInfos cri) { + Collection allRequests = cri.getRequests(); + for (CertRequestInfo x : allRequests) { + CertCLI.printCertRequestInfo(x); + } + System.out.println(); + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " ", options); + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java new file mode 100644 index 000000000..d8a4d5295 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java @@ -0,0 +1,164 @@ +// --- 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) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.cert; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import netscape.security.x509.RevocationReason; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.cert.CertData; +import com.netscape.certsrv.cert.CertRequestInfo; +import com.netscape.certsrv.cert.CertRevokeRequest; +import com.netscape.certsrv.dbs.certdb.CertId; +import com.netscape.certsrv.request.RequestStatus; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class CertRevokeCLI extends CLI { + + public CertCLI parent; + + public CertRevokeCLI(CertCLI parent) { + super("revoke", "Revoke certificate"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + StringBuilder sb = new StringBuilder(); + + for (RevocationReason reason : RevocationReason.INSTANCES) { + if (sb.length() > 0) { + sb.append(", "); + } + sb.append(reason); + if (reason == RevocationReason.UNSPECIFIED) { + sb.append(" (default)"); + } + } + + Option option = new Option(null, "reason", true, "Revocation reason: " + sb); + option.setArgName("reason"); + options.addOption(option); + + option = new Option(null, "comments", true, "Comments"); + option.setArgName("comments"); + options.addOption(option); + + options.addOption(null, "ca", false, "CA signing certificate"); + options.addOption(null, "force", false, "Force"); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + CertId certID = new CertId(cmdArgs[0]); + + String string = cmd.getOptionValue("reason", RevocationReason.UNSPECIFIED.toString()); + RevocationReason reason = RevocationReason.valueOf(string); + + if (reason == null) { + System.err.println("Error: Invalid revocation reason: "+string); + printHelp(); + System.exit(1); + return; + } + + if (!cmd.hasOption("force")) { + + CertData certData = parent.client.getCert(certID); + + if (reason == RevocationReason.CERTIFICATE_HOLD) { + System.out.println("Placing certificate on-hold:"); + } else if (reason == RevocationReason.REMOVE_FROM_CRL) { + System.out.println("Placing certificate off-hold:"); + } else { + System.out.println("Revoking certificate:"); + } + + CertCLI.printCertData(certData, false, false); + + System.out.print("Are you sure (Y/N)? "); + System.out.flush(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String line = reader.readLine(); + if (!line.equalsIgnoreCase("Y")) { + System.exit(1); + } + } + + CertRevokeRequest request = new CertRevokeRequest(); + request.setReason(reason); + request.setComments(cmd.getOptionValue("comments")); + + CertRequestInfo certRequestInfo; + + if (cmd.hasOption("ca")) { + certRequestInfo = parent.client.revokeCACert(certID, request); + } else { + certRequestInfo = parent.client.revokeCert(certID, request); + } + + if (verbose) { + CertCLI.printCertRequestInfo(certRequestInfo); + } + + if (certRequestInfo.getRequestStatus() == RequestStatus.COMPLETE) { + if (reason == RevocationReason.CERTIFICATE_HOLD) { + MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" on-hold"); + } else if (reason == RevocationReason.REMOVE_FROM_CRL) { + MainCLI.printMessage("Placed certificate \"" + certID.toHexString() + "\" off-hold"); + } else { + MainCLI.printMessage("Revoked certificate \"" + certID.toHexString() + "\""); + } + + CertData certData = parent.client.getCert(certID); + CertCLI.printCertData(certData, false, false); + + } else { + MainCLI.printMessage("Request \"" + certRequestInfo.getRequestId() + "\": " + certRequestInfo.getRequestStatus()); + } + } +} diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java new file mode 100644 index 000000000..798514814 --- /dev/null +++ b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java @@ -0,0 +1,95 @@ +// --- 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) 2012 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +package com.netscape.cmstools.cert; + +import java.io.FileWriter; +import java.io.PrintWriter; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import com.netscape.certsrv.cert.CertData; +import com.netscape.certsrv.dbs.certdb.CertId; +import com.netscape.cmstools.cli.CLI; +import com.netscape.cmstools.cli.MainCLI; + +/** + * @author Endi S. Dewata + */ +public class CertShowCLI extends CLI { + + public CertCLI parent; + + public CertShowCLI(CertCLI parent) { + super("show", "Show certificate"); + this.parent = parent; + } + + public void printHelp() { + formatter.printHelp(parent.name + "-" + name + " [OPTIONS...]", options); + } + + public void execute(String[] args) throws Exception { + + Option option = new Option(null, "output", true, "Output file"); + option.setArgName("file"); + options.addOption(option); + + options.addOption(null, "pretty", false, "Pretty print"); + options.addOption(null, "encoded", false, "Base-64 encoded"); + + CommandLine cmd = null; + + try { + cmd = parser.parse(options, args); + + } catch (Exception e) { + System.err.println("Error: " + e.getMessage()); + printHelp(); + System.exit(1); + } + + boolean showPrettyPrint = cmd.hasOption("pretty"); + boolean showEncoded = cmd.hasOption("encoded"); + + String[] cmdArgs = cmd.getArgs(); + + if (cmdArgs.length != 1) { + printHelp(); + System.exit(1); + } + + CertId certID = new CertId(cmdArgs[0]); + String file = cmd.getOptionValue("output"); + + CertData certData = parent.client.getCert(certID); + + String encoded = certData.getEncoded(); + if (encoded != null && file != null) { + // store cert to file + PrintWriter out = new PrintWriter(new FileWriter(file)); + out.print(encoded); + out.close(); + } + + MainCLI.printMessage("Certificate \"" + certID.toHexString() + "\""); + + CertCLI.printCertData(certData, showPrettyPrint, showEncoded); + } +} -- cgit