Skip to content

Commit

Permalink
Add pki nss-cert-find --subject and --issuer options
Browse files Browse the repository at this point in the history
  • Loading branch information
edewata committed Aug 26, 2024
1 parent 6f02dd9 commit 0d5eccb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/server-https-nss-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ jobs:
diff expected stderr
# the cert should not be stored
docker exec client pki nss-cert-find | tee output
docker exec client pki nss-cert-find --subject CN=pki.example.com | tee output
diff /dev/null output
Expand Down Expand Up @@ -225,6 +225,11 @@ jobs:
diff expected stderr
# the cert should not be stored
docker exec client pki nss-cert-find --subject CN=pki.example.com | tee output
diff /dev/null output
- name: Check PKI CLI with newly trusted server cert
run: |
# run PKI CLI and trust the cert
Expand Down Expand Up @@ -257,7 +262,7 @@ jobs:
diff expected stderr
# the cert should be stored and trusted
docker exec client pki nss-cert-find | tee output
docker exec client pki nss-cert-find --subject CN=pki.example.com | tee output
sed -i \
-e '/^ *Serial Number:/d' \
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/server-https-pkcs12-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ jobs:
diff expected stderr
# the cert should not be stored
docker exec client pki nss-cert-find | tee output
docker exec client pki nss-cert-find --subject CN=pki.example.com | tee output
diff /dev/null output
Expand Down Expand Up @@ -251,6 +251,11 @@ jobs:
diff expected stderr
# the cert should not be stored
docker exec client pki nss-cert-find --subject CN=pki.example.com | tee output
diff /dev/null output
- name: Check PKI CLI with newly trusted server cert
run: |
# run PKI CLI and trust the cert
Expand Down Expand Up @@ -282,7 +287,7 @@ jobs:
diff expected stderr
# the cert should be stored and trusted
docker exec client pki nss-cert-find | tee output
docker exec client pki nss-cert-find --subject CN=pki.example.com | tee output
sed -i \
-e '/^ *Serial Number:/d' \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
//
package com.netscape.cmstools.nss;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.dogtagpki.cli.CommandCLI;
import org.mozilla.jss.crypto.CryptoStore;
import org.mozilla.jss.crypto.CryptoToken;
Expand All @@ -31,25 +33,55 @@ public void printHelp() {
formatter.printHelp(getFullName() + " [OPTIONS...]", options);
}

public Collection<X509Certificate> findAllCerts() throws Exception {
@Override
public void createOptions() {
Option option = new Option(null, "subject", true, "Subject DN");
option.setArgName("DN");
options.addOption(option);

option = new Option(null, "issuer", true, "Issuer DN");
option.setArgName("DN");
options.addOption(option);
}

logger.info("Searching for all certs");
public Collection<X509Certificate> findCerts(
String subject,
String issuer
) throws Exception {

logger.info("Searching for certs");
String tokenName = getConfig().getTokenName();
CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName);
CryptoStore store = token.getCryptoStore();

return Arrays.asList(store.getCertificates());
List<X509Certificate> results = new ArrayList<>();
for (X509Certificate cert : store.getCertificates()) {

if (subject != null && !subject.equals(cert.getSubjectDN().toString())) {
continue;
}

if (issuer != null && !issuer.equals(cert.getIssuerDN().toString())) {
continue;
}

results.add(cert);
}
return results;
}

@Override
public void execute(CommandLine cmd) throws Exception {

String subject = cmd.getOptionValue("subject");
String issuer = cmd.getOptionValue("issuer");

MainCLI mainCLI = (MainCLI) getRoot();
mainCLI.init();

boolean first = true;

for (X509Certificate cert : findAllCerts()) {
for (X509Certificate cert : findCerts(subject, issuer)) {

if (first) {
first = false;
Expand Down

0 comments on commit 0d5eccb

Please sign in to comment.