Adopt latest changes from pgp-cert-d and add armor headers for get command

This commit is contained in:
Paul Schaub 2022-08-24 13:24:05 +02:00
parent 7b6162682e
commit 33d93cc39a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 25 additions and 86 deletions

View file

@ -5,7 +5,10 @@
package pgp.cert_d.cli.commands; package pgp.cert_d.cli.commands;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.pgpainless.PGPainless;
import org.pgpainless.util.ArmorUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import pgp.cert_d.SpecialNames; import pgp.cert_d.SpecialNames;
@ -46,7 +49,8 @@ public class Get implements Runnable {
} }
if (armor) { if (armor) {
ArmoredOutputStream armorOut = new ArmoredOutputStream(System.out); PGPKeyRing keyRing = PGPainless.readKeyRing().keyRing(record.getInputStream());
ArmoredOutputStream armorOut = ArmorUtils.toAsciiArmoredStream(keyRing, System.out);
Streams.pipeAll(record.getInputStream(), armorOut); Streams.pipeAll(record.getInputStream(), armorOut);
armorOut.close(); armorOut.close();
} else { } else {

View file

@ -6,54 +6,26 @@ package org.pgpainless.certificate_store;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.util.encoders.Base64;
import org.pgpainless.key.OpenPgpFingerprint; import org.pgpainless.key.OpenPgpFingerprint;
import pgp.certificate_store.certificate.Certificate; import pgp.certificate_store.certificate.Certificate;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
public class CertificateFactory { public class CertificateFactory {
public static Certificate certificateFromPublicKeyRing(PGPPublicKeyRing publicKeyRing) { public static Certificate certificateFromPublicKeyRing(PGPPublicKeyRing publicKeyRing, Long tag)
return new Certificate() { throws IOException {
@Override byte[] bytes = publicKeyRing.getEncoded();
public String getFingerprint() { String fingerprint = OpenPgpFingerprint.of(publicKeyRing).toString().toLowerCase();
return OpenPgpFingerprint.of(publicKeyRing).toString().toLowerCase(); List<Long> subkeyIds = new ArrayList<>();
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(publicKeyRing.getEncoded());
}
@Override
public String getTag() throws IOException {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new AssertionError("No MessageDigest for SHA-256 instantiated, although BC is on the classpath: " + e.getMessage());
}
digest.update(publicKeyRing.getEncoded());
return Base64.toBase64String(digest.digest());
}
@Override
public List<Long> getSubkeyIds() throws IOException {
List<Long> keyIds = new ArrayList<>();
Iterator<PGPPublicKey> keys = publicKeyRing.getPublicKeys(); Iterator<PGPPublicKey> keys = publicKeyRing.getPublicKeys();
while (keys.hasNext()) { while (keys.hasNext()) {
keyIds.add(keys.next().getKeyID()); subkeyIds.add(keys.next().getKeyID());
} }
return keyIds;
} return new Certificate(bytes, fingerprint, subkeyIds, tag);
};
} }
} }

View file

@ -6,55 +6,18 @@ package org.pgpainless.certificate_store;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.util.encoders.Base64;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import pgp.certificate_store.certificate.Certificate; import pgp.certificate_store.certificate.Certificate;
import pgp.certificate_store.certificate.Key; import pgp.certificate_store.certificate.Key;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
public class KeyFactory { public class KeyFactory {
public static Key keyFromSecretKeyRing(PGPSecretKeyRing secretKeyRing) { public static Key keyFromSecretKeyRing(PGPSecretKeyRing secretKeyRing, Long tag) throws IOException {
byte[] bytes = secretKeyRing.getEncoded();
return new Key() { PGPPublicKeyRing publicKeyRing = PGPainless.extractCertificate(secretKeyRing);
@Override Certificate certificate = CertificateFactory.certificateFromPublicKeyRing(publicKeyRing, tag);
public String getFingerprint() { return new Key(bytes, certificate, tag);
return getCertificate().getFingerprint();
}
@Override
public Certificate getCertificate() {
PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeyRing);
return CertificateFactory.certificateFromPublicKeyRing(publicKeys);
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(secretKeyRing.getEncoded());
}
@Override
public String getTag() throws IOException {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new AssertionError("No MessageDigest for SHA-256 instantiated, although BC is on the classpath: " + e.getMessage());
}
digest.update(secretKeyRing.getEncoded());
return Base64.toBase64String(digest.digest());
}
@Override
public List<Long> getSubkeyIds() throws IOException {
return getCertificate().getSubkeyIds();
}
};
} }
} }

View file

@ -18,12 +18,12 @@ import java.io.InputStream;
public class KeyMaterialReader implements KeyMaterialReaderBackend { public class KeyMaterialReader implements KeyMaterialReaderBackend {
@Override @Override
public KeyMaterial read(InputStream data) throws IOException, BadDataException { public KeyMaterial read(InputStream data, Long tag) throws IOException, BadDataException {
PGPKeyRing keyMaterial = PGPainless.readKeyRing().keyRing(data); PGPKeyRing keyMaterial = PGPainless.readKeyRing().keyRing(data);
if (keyMaterial instanceof PGPSecretKeyRing) { if (keyMaterial instanceof PGPSecretKeyRing) {
return KeyFactory.keyFromSecretKeyRing((PGPSecretKeyRing) keyMaterial); return KeyFactory.keyFromSecretKeyRing((PGPSecretKeyRing) keyMaterial, tag);
} else if (keyMaterial instanceof PGPPublicKeyRing) { } else if (keyMaterial instanceof PGPPublicKeyRing) {
return CertificateFactory.certificateFromPublicKeyRing((PGPPublicKeyRing) keyMaterial); return CertificateFactory.certificateFromPublicKeyRing((PGPPublicKeyRing) keyMaterial, tag);
} else { } else {
throw new BadDataException(); throw new BadDataException();
} }

View file

@ -81,9 +81,9 @@ public class MergeCallbacks {
printOutDifferences(existingKeyRing, mergedKeyRing); printOutDifferences(existingKeyRing, mergedKeyRing);
if (mergedKeyRing instanceof PGPPublicKeyRing) { if (mergedKeyRing instanceof PGPPublicKeyRing) {
return CertificateFactory.certificateFromPublicKeyRing((PGPPublicKeyRing) mergedKeyRing); return CertificateFactory.certificateFromPublicKeyRing((PGPPublicKeyRing) mergedKeyRing, null);
} else { } else {
return KeyFactory.keyFromSecretKeyRing((PGPSecretKeyRing) mergedKeyRing); return KeyFactory.keyFromSecretKeyRing((PGPSecretKeyRing) mergedKeyRing, null);
} }
} catch (PGPException e) { } catch (PGPException e) {