Skip to content

Commit

Permalink
Update pki pkcs7-export
Browse files Browse the repository at this point in the history
The pki pkcs7-export has been modified to support redirecting
the output to the standard output and to provide an option to
specify the output format.
  • Loading branch information
edewata committed Aug 10, 2023
1 parent e620ae7 commit 12fbe56
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
package com.netscape.cmstools.pkcs7;

import java.io.PrintWriter;
import java.io.FileOutputStream;
import java.security.cert.X509Certificate;

import javax.net.ssl.KeyManagerFactory;
Expand Down Expand Up @@ -43,6 +43,10 @@ public void createOptions() {
Option option = new Option(null, "pkcs7", true, "PKCS #7 file");
option.setArgName("path");
options.addOption(option);

option = new Option(null, "format", true, "PKCS #7 format: PEM (default), DER");
option.setArgName("format");
options.addOption(option);
}

@Override
Expand All @@ -56,9 +60,7 @@ public void execute(CommandLine cmd) throws Exception {
String nickname = cmdArgs[0];

String filename = cmd.getOptionValue("pkcs7");
if (filename == null) {
throw new Exception("Missing PKCS #7 file");
}
String format = cmd.getOptionValue("format", "PEM").toUpperCase();

MainCLI mainCLI = (MainCLI) getRoot();
mainCLI.init();
Expand All @@ -75,15 +77,30 @@ public void execute(CommandLine cmd) throws Exception {
CertificateChain certChain = new CertificateChain(certs);
certChain.sort();

logger.info("Storing certificate chain into " + filename);
PKCS7 pkcs7 = certChain.toPKCS7();

for (X509Certificate cert : certChain.getCertificates()) {
logger.info("- " + cert.getSubjectDN());
}

try (PrintWriter out = new PrintWriter(filename)) {
out.print(pkcs7.toPEMString());
PKCS7 pkcs7 = certChain.toPKCS7();
byte[] bytes;

if (format.equals("PEM")) {
bytes = pkcs7.toPEMString().getBytes();

} else if (format.equals("DER")) {
bytes = pkcs7.getBytes();

} else {
throw new Exception("Unsupported format: " + format);
}

if (filename == null) {
System.out.write(bytes);

} else {
try (FileOutputStream fos = new FileOutputStream(filename)) {
fos.write(bytes);
}
}
}
}

0 comments on commit 12fbe56

Please sign in to comment.