match gpg behaviour for loading user public keyrings

This commit is contained in:
Craig Raw 2024-02-22 13:50:47 +02:00
parent 9656603930
commit f455d94a01

View file

@ -132,21 +132,27 @@ public class PGPUtils {
return null; return null;
} }
private static PGPPublicKeyRingCollection getUserKeyRingCollection() throws IOException { private static PGPPublicKeyRingCollection getUserKeyRingCollection() {
try { try {
File gnupgPubRing = getGnuPGPubRing(); File gnupgHome = getGnuPGHome();
if(gnupgPubRing != null) { if(gnupgHome.exists()) {
if(gnupgPubRing.getName().equals(PUBRING_GPG)) { File kbxPubRing = new File(gnupgHome, PUBRING_KBX);
return PGPainless.readKeyRing().publicKeyRingCollection(new FileInputStream(gnupgPubRing)); if(kbxPubRing.exists()) {
} else if(gnupgPubRing.getName().equals(PUBRING_KBX)) { BcKeyBox bcKeyBox = new BcKeyBox(new FileInputStream(kbxPubRing));
BcKeyBox bcKeyBox = new BcKeyBox(new FileInputStream(gnupgPubRing));
List<PGPPublicKeyRing> rings = new ArrayList<>(); List<PGPPublicKeyRing> rings = new ArrayList<>();
for(KeyBlob keyBlob : bcKeyBox.getKeyBlobs()) { for(KeyBlob keyBlob : bcKeyBox.getKeyBlobs()) {
if(keyBlob instanceof PublicKeyRingBlob publicKeyRingBlob) { if(keyBlob instanceof PublicKeyRingBlob publicKeyRingBlob) {
rings.add(publicKeyRingBlob.getPGPPublicKeyRing()); rings.add(publicKeyRingBlob.getPGPPublicKeyRing());
} }
} }
return new PGPPublicKeyRingCollection(rings); if(!rings.isEmpty()) {
return new PGPPublicKeyRingCollection(rings);
}
}
File gpgPubRing = new File(gnupgHome, PUBRING_GPG);
if(gpgPubRing.exists()) {
return PGPainless.readKeyRing().publicKeyRingCollection(new FileInputStream(gpgPubRing));
} }
} }
} catch(Exception e) { } catch(Exception e) {
@ -156,23 +162,6 @@ public class PGPUtils {
return null; return null;
} }
private static File getGnuPGPubRing() {
File gnupgHome = getGnuPGHome();
if(gnupgHome.exists()) {
File gpgPubRing = new File(gnupgHome, PUBRING_GPG);
if(gpgPubRing.exists()) {
return gpgPubRing;
}
File kbxPubRing = new File(gnupgHome, PUBRING_KBX);
if(kbxPubRing.exists()) {
return kbxPubRing;
}
}
return null;
}
private static File getGnuPGHome() { private static File getGnuPGHome() {
String gnupgHome = System.getenv("GNUPGHOME"); String gnupgHome = System.getenv("GNUPGHOME");
if(gnupgHome != null && !gnupgHome.isEmpty()) { if(gnupgHome != null && !gnupgHome.isEmpty()) {