1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-30 00:02:06 +01:00

Fix KeyRingInfo.get*Algorithm(keyId)

This commit is contained in:
Paul Schaub 2022-01-15 02:43:59 +01:00
parent 9de196d6c5
commit e7f583c1af
3 changed files with 34 additions and 11 deletions

View file

@ -123,4 +123,26 @@ public abstract class KeyAccessor {
return signature;
}
}
public static class SubKey extends KeyAccessor {
public SubKey(KeyRingInfo info, SubkeyIdentifier key) {
super(info, key);
}
@Override
public @Nonnull PGPSignature getSignatureWithPreferences() {
PGPSignature signature;
if (key.getPrimaryKeyId() == key.getSubkeyId()) {
signature = info.getLatestDirectKeySelfSignature();
if (signature == null) {
signature = info.getLatestUserIdCertification(info.getPrimaryUserId());
}
} else {
signature = info.getCurrentSubkeyBindingSignature(key.getSubkeyId());
}
return signature;
}
}
}

View file

@ -975,7 +975,8 @@ public class KeyRingInfo {
}
public Set<HashAlgorithm> getPreferredHashAlgorithms(long keyId) {
return getKeyAccessor(null, keyId).getPreferredHashAlgorithms();
return new KeyAccessor.SubKey(this, new SubkeyIdentifier(keys, keyId))
.getPreferredHashAlgorithms();
}
public Set<SymmetricKeyAlgorithm> getPreferredSymmetricKeyAlgorithms() {
@ -987,7 +988,7 @@ public class KeyRingInfo {
}
public Set<SymmetricKeyAlgorithm> getPreferredSymmetricKeyAlgorithms(long keyId) {
return getKeyAccessor(null, keyId).getPreferredSymmetricKeyAlgorithms();
return new KeyAccessor.SubKey(this, new SubkeyIdentifier(keys, keyId)).getPreferredSymmetricKeyAlgorithms();
}
public Set<CompressionAlgorithm> getPreferredCompressionAlgorithms() {
@ -999,15 +1000,15 @@ public class KeyRingInfo {
}
public Set<CompressionAlgorithm> getPreferredCompressionAlgorithms(long keyId) {
return getKeyAccessor(null, keyId).getPreferredCompressionAlgorithms();
return new KeyAccessor.SubKey(this, new SubkeyIdentifier(keys, keyId)).getPreferredCompressionAlgorithms();
}
private KeyAccessor getKeyAccessor(@Nullable String userId, long keyID) {
if (getPublicKey(keyID) == null) {
throw new IllegalArgumentException("No subkey with key id " + Long.toHexString(keyID) + " found on this key.");
throw new NoSuchElementException("No subkey with key id " + Long.toHexString(keyID) + " found on this key.");
}
if (userId != null && !getUserIds().contains(userId)) {
throw new IllegalArgumentException("No user-id '" + userId + "' found on this key.");
throw new NoSuchElementException("No user-id '" + userId + "' found on this key.");
}
return userId == null ? new KeyAccessor.ViaKeyId(this, new SubkeyIdentifier(keys, keyID))
: new KeyAccessor.ViaUserId(this, new SubkeyIdentifier(keys, keyID), userId);

View file

@ -598,9 +598,9 @@ public class KeyRingInfoTest {
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
// Bob is an invalid userId
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob"));
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms(123L));
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms(123L));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms("Alice"));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(pkid));
@ -608,9 +608,9 @@ public class KeyRingInfoTest {
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(skid2));
// Bob is an invalid userId
assertThrows(IllegalArgumentException.class, () -> info.getPreferredCompressionAlgorithms("Bob"));
assertThrows(NoSuchElementException.class, () -> info.getPreferredCompressionAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(IllegalArgumentException.class, () -> info.getPreferredCompressionAlgorithms(123L));
assertThrows(NoSuchElementException.class, () -> info.getPreferredCompressionAlgorithms(123L));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms("Alice"));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(pkid));
@ -618,9 +618,9 @@ public class KeyRingInfoTest {
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(skid2));
// Bob is an invalid userId
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob"));
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms(123L));
assertThrows(NoSuchElementException.class, () -> info.getPreferredSymmetricKeyAlgorithms(123L));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms("Alice"));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms(pkid));