Implement List command, adapt changes from cert-d-java

This commit is contained in:
Paul Schaub 2022-08-12 15:04:54 +02:00
parent cd0150c4d9
commit 7a02ec865b
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
21 changed files with 151 additions and 44 deletions

View file

@ -10,8 +10,8 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.pgpainless.PGPainless;
import org.pgpainless.certificate_store.CertificateFactory;
import org.pgpainless.key.OpenPgpFingerprint;
import pgp.certificate.KeyMaterial;
import pgp.certificate.KeyMaterialMerger;
import pgp.certificate_store.certificate.KeyMaterial;
import pgp.certificate_store.certificate.KeyMaterialMerger;
import java.io.IOException;
import java.util.Iterator;

View file

@ -6,13 +6,15 @@ package pgp.cert_d.cli;
import org.pgpainless.certificate_store.PGPainlessCertD;
import pgp.cert_d.BaseDirectoryProvider;
import pgp.cert_d.exception.NotAStoreException;
import pgp.cert_d.cli.commands.Export;
import pgp.cert_d.cli.commands.Find;
import pgp.cert_d.cli.commands.Get;
import pgp.cert_d.cli.commands.Insert;
import pgp.cert_d.cli.commands.Import;
import pgp.cert_d.cli.commands.List;
import pgp.cert_d.cli.commands.Setup;
import pgp.cert_d.jdbc.sqlite.DatabaseSubkeyLookupFactory;
import pgp.certificate_store.exception.NotAStoreException;
import picocli.CommandLine;
import java.io.File;
@ -28,7 +30,8 @@ import java.sql.SQLException;
Import.class,
Get.class,
Setup.class,
List.class
List.class,
Find.class
}
)
public class PGPCertDCli {
@ -53,7 +56,7 @@ public class PGPCertDCli {
baseDirectory = BaseDirectoryProvider.getDefaultBaseDir();
}
PGPCertDCli.certificateDirectory = PGPainlessCertD.fileBased(baseDirectory);
PGPCertDCli.certificateDirectory = PGPainlessCertD.fileBased(baseDirectory, new DatabaseSubkeyLookupFactory());
}
public static void main(String[] args) {

View file

@ -9,7 +9,7 @@ import org.bouncycastle.util.io.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pgp.cert_d.cli.PGPCertDCli;
import pgp.certificate.Certificate;
import pgp.certificate_store.certificate.Certificate;
import picocli.CommandLine;
import java.io.IOException;

View file

@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.cert_d.cli.commands;
import org.pgpainless.key.OpenPgpFingerprint;
import pgp.cert_d.cli.PGPCertDCli;
import picocli.CommandLine;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Set;
import java.util.regex.Pattern;
@CommandLine.Command(name = "find",
resourceBundle = "msg_find")
public class Find implements Runnable {
private static final Pattern LONG_KEY_ID = Pattern.compile("^[0-9A-Fa-f]{16}$");
@CommandLine.Parameters(
paramLabel = "IDENTIFIER",
arity = "1")
String identifier;
@Override
public void run() {
if (identifier == null) {
throw new IllegalArgumentException("No subkey ID provided.");
}
identifier = identifier.trim();
long subkeyId = 0;
try {
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parse(identifier);
subkeyId = fingerprint.getKeyId();
} catch (IllegalArgumentException e) {
if (!LONG_KEY_ID.matcher(identifier).matches()) {
throw new IllegalArgumentException("Provided long key-id does not match expected format. " +
"A long key-id consists of 16 hexadecimal characters.");
}
subkeyId = new BigInteger(identifier, 16).longValue();
}
try {
Set<String> fingerprints = PGPCertDCli.getCertificateDirectory()
.getCertificateFingerprintsForSubkeyId(subkeyId);
for (String fingerprint : fingerprints) {
// CHECKSTYLE:OFF
System.out.println(fingerprint);
// CHECKSTYLE:ON
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -4,14 +4,15 @@
package pgp.cert_d.cli.commands;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.util.io.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pgp.cert_d.exception.BadDataException;
import pgp.cert_d.exception.BadNameException;
import pgp.cert_d.SpecialNames;
import pgp.cert_d.cli.PGPCertDCli;
import pgp.certificate.KeyMaterial;
import pgp.certificate_store.certificate.KeyMaterial;
import pgp.certificate_store.exception.BadDataException;
import pgp.certificate_store.exception.BadNameException;
import picocli.CommandLine;
import java.io.IOException;
@ -22,6 +23,9 @@ public class Get implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(Get.class);
@CommandLine.Option(names = {"-a", "--armor"})
boolean armor = false;
@CommandLine.Parameters(
paramLabel = "IDENTIFIER",
arity = "1"
@ -35,12 +39,20 @@ public class Get implements Runnable {
if (SpecialNames.lookupSpecialName(identifer) != null) {
record = PGPCertDCli.getCertificateDirectory().getBySpecialName(identifer);
} else {
record = PGPCertDCli.getCertificateDirectory().getByFingerprint(identifer);
record = PGPCertDCli.getCertificateDirectory().getByFingerprint(identifer.toLowerCase());
}
if (record == null) {
return;
}
Streams.pipeAll(record.getInputStream(), System.out);
if (armor) {
ArmoredOutputStream armorOut = new ArmoredOutputStream(System.out);
Streams.pipeAll(record.getInputStream(), armorOut);
armorOut.close();
} else {
Streams.pipeAll(record.getInputStream(), System.out);
}
} catch (IOException e) {
LOGGER.error("IO Error", e);
System.exit(-1);

View file

@ -10,10 +10,10 @@ import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.pgpainless.PGPainless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pgp.cert_d.exception.BadDataException;
import pgp.cert_d.cli.MergeCallbacks;
import pgp.cert_d.cli.PGPCertDCli;
import pgp.certificate.Certificate;
import pgp.certificate_store.certificate.Certificate;
import pgp.certificate_store.exception.BadDataException;
import picocli.CommandLine;
import java.io.ByteArrayInputStream;

View file

@ -6,10 +6,10 @@ package pgp.cert_d.cli.commands;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pgp.cert_d.exception.BadDataException;
import pgp.cert_d.cli.MergeCallbacks;
import pgp.cert_d.cli.PGPCertDCli;
import pgp.certificate.Certificate;
import pgp.certificate_store.certificate.Certificate;
import pgp.certificate_store.exception.BadDataException;
import picocli.CommandLine;
import java.io.IOException;

View file

@ -5,7 +5,7 @@
package pgp.cert_d.cli.commands;
import pgp.cert_d.cli.PGPCertDCli;
import pgp.certificate.Certificate;
import pgp.certificate_store.certificate.Certificate;
import picocli.CommandLine;
import java.util.Iterator;

View file

@ -15,9 +15,9 @@ import org.pgpainless.key.generation.type.eddsa.EdDSACurve;
import org.pgpainless.util.Passphrase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pgp.cert_d.exception.BadDataException;
import pgp.cert_d.cli.MergeCallbacks;
import pgp.cert_d.cli.PGPCertDCli;
import pgp.certificate_store.exception.BadDataException;
import picocli.CommandLine;
import java.io.ByteArrayInputStream;

View file

@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0
usage.header=Export all certificates in the store to Standard Output
armor=Wrap the output in ASCII armor
# Generic TODO: Remove when bumping picocli to 4.7.0
usage.synopsisHeading=Usage:\u0020

View file

@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0
usage.header=Exportiere alle gespeicherten Zertifikate zur Standardausgabe
armor=Verpacke the Ausgabe in ASCII Armor
# Generic TODO: Remove when bumping picocli to 4.7.0
usage.synopsisHeading=Aufruf:\u0020

View file

@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
#
# SPDX-License-Identifier: Apache-2.0
usage.header=Lookup primary certificate fingerprints by subkey ids or fingerprints
# Generic TODO: Remove when bumping picocli to 4.7.0
usage.synopsisHeading=Usage:\u0020
usage.commandListHeading = %nCommands:%n
usage.optionListHeading = %nOptions:%n
usage.footerHeading=Powered by picocli%n
store=Overwrite the default certificate directory path

View file

@ -0,0 +1,11 @@
# SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
#
# SPDX-License-Identifier: Apache-2.0
usage.header=Schlage primäre Fingerabdrücke von Zertifikaten per ID oder Fingerabdruck von Unterschlüsseln nach
# Generic TODO: Remove when bumping picocli to 4.7.0
usage.synopsisHeading=Aufruf:\u0020
usage.commandListHeading=%nBefehle:%n
usage.optionListHeading = %nOptionen:%n
usage.footerHeading=Powered by Picocli%n
store=Überschreibe den Standardpfad des Zertifikatsverzeichnisses

View file

@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
usage.header=Retrieve certificates from the store
IDENTIFIER[0]=Certificate identifier (fingerprint or special name)
armor=Wrap the output in ASCII armor
# Generic TODO: Remove when bumping picocli to 4.7.0
usage.synopsisHeading=Usage:\u0020

View file

@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
usage.header=Frage Zertifikate aus dem Speicher ab
IDENTIFIER[0]=Zertifikatskennung (Fingerabdruck oder Spezialname)
armor=Verpacke the Ausgabe in ASCII Armor
# Generic TODO: Remove when bumping picocli to 4.7.0
usage.synopsisHeading=Aufruf:\u0020