Skip to content

Commit

Permalink
Update pki pkcs7-cert-import
Browse files Browse the repository at this point in the history
The pki pkcs7-cert-import has been updated to accept certs
specified via standard input.
  • Loading branch information
edewata committed May 16, 2024
1 parent d763942 commit b83487a
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.io.IOUtils;
import org.dogtagpki.cli.CommandCLI;
import org.mozilla.jss.netscape.security.pkcs.PKCS7;
import org.mozilla.jss.netscape.security.x509.CertificateChain;
Expand Down Expand Up @@ -88,31 +89,35 @@ public void execute(CommandLine cmd) throws Exception {
}

String inputFilename = cmd.getOptionValue("input-file");
if (inputFilename != null) {

logger.info("Importing certificates from " + inputFilename);
byte[] bytes = Files.readAllBytes(Paths.get(inputFilename));
byte[] bytes;
if (inputFilename == null) {
logger.info("Loading certificate from standard input");
bytes = IOUtils.toByteArray(System.in);

} else {
logger.info("Loading certificate from " + inputFilename);
bytes = Files.readAllBytes(Paths.get(inputFilename));
}

String inputFormat = cmd.getOptionValue("input-format", "PEM");
if ("PEM".equalsIgnoreCase(inputFormat)) {
String inputFormat = cmd.getOptionValue("input-format", "PEM");
if ("PEM".equalsIgnoreCase(inputFormat)) {

CertificateChain inputChain = CertificateChain.fromPEMString(new String(bytes));
for (X509Certificate cert : inputChain.getCertificates()) {
logger.info(" - " + cert.getSubjectDN());
}
CertificateChain inputChain = CertificateChain.fromPEMString(new String(bytes));
for (X509Certificate cert : inputChain.getCertificates()) {
logger.info(" - " + cert.getSubjectDN());
}

certChain.addCertificateChain(inputChain);
certChain.addCertificateChain(inputChain);

} else if ("DER".equalsIgnoreCase(inputFormat)) {
} else if ("DER".equalsIgnoreCase(inputFormat)) {

X509CertImpl cert = new X509CertImpl(bytes);
logger.info(" - " + cert.getSubjectDN());
X509CertImpl cert = new X509CertImpl(bytes);
logger.info(" - " + cert.getSubjectDN());

certChain.addCertificate(cert);
certChain.addCertificate(cert);

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

logger.info("Storing certificates into " + path);
Expand Down

0 comments on commit b83487a

Please sign in to comment.