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