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

Fix API for accessing preferred algorithms

This commit is contained in:
Paul Schaub 2021-11-15 13:02:26 +01:00
parent 8a40cdeefb
commit 19b1a0238d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 52 additions and 60 deletions

View file

@ -165,7 +165,8 @@ public final class SigningOptions {
for (PGPPublicKey signingPubKey : signingPubKeys) {
PGPSecretKey signingSecKey = secretKey.getSecretKey(signingPubKey.getKeyID());
PGPPrivateKey signingSubkey = UnlockSecretKey.unlockSecretKey(signingSecKey, secretKeyDecryptor);
Set<HashAlgorithm> hashAlgorithms = keyRingInfo.getPreferredHashAlgorithms(userId, signingPubKey.getKeyID());
Set<HashAlgorithm> hashAlgorithms = userId != null ? keyRingInfo.getPreferredHashAlgorithms(userId)
: keyRingInfo.getPreferredHashAlgorithms(signingPubKey.getKeyID());
HashAlgorithm hashAlgorithm = negotiateHashAlgorithm(hashAlgorithms, PGPainless.getPolicy());
addSigningMethod(secretKey, signingSubkey, hashAlgorithm, signatureType, false);
}
@ -244,7 +245,8 @@ public final class SigningOptions {
for (PGPPublicKey signingPubKey : signingPubKeys) {
PGPSecretKey signingSecKey = secretKey.getSecretKey(signingPubKey.getKeyID());
PGPPrivateKey signingSubkey = signingSecKey.extractPrivateKey(secretKeyDecryptor.getDecryptor(signingPubKey.getKeyID()));
Set<HashAlgorithm> hashAlgorithms = keyRingInfo.getPreferredHashAlgorithms(userId, signingPubKey.getKeyID());
Set<HashAlgorithm> hashAlgorithms = userId != null ? keyRingInfo.getPreferredHashAlgorithms(userId)
: keyRingInfo.getPreferredHashAlgorithms(signingPubKey.getKeyID());
HashAlgorithm hashAlgorithm = negotiateHashAlgorithm(hashAlgorithms, PGPainless.getPolicy());
addSigningMethod(secretKey, signingSubkey, hashAlgorithm, signatureType, true);
}

View file

@ -18,7 +18,6 @@ import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -846,43 +845,40 @@ public class KeyRingInfo {
return signingKeys;
}
/**
* Return the (sorted) set of preferred hash algorithms of the given key.
*
* @param userId user-id. If this is non-null, the hash algorithms are being extracted from the user-id
* certification signature first.
* @param keyID if of the key in question
* @return hash algorithm preferences
*/
public Set<HashAlgorithm> getPreferredHashAlgorithms(@Nullable String userId, long keyID) {
KeyAccessor keyAccessor = getKeyAccessor(userId, keyID);
return keyAccessor.getPreferredHashAlgorithms();
public Set<HashAlgorithm> getPreferredHashAlgorithms() {
return getPreferredHashAlgorithms(getPrimaryUserId());
}
/**
* Return the (sorted) set of preferred symmetric encryption algorithms of the given key.
*
* @param userId user-id. If this is non-null, the symmetric encryption algorithms are being
* extracted from the user-id certification signature first.
* @param keyId if of the key in question
* @return symmetric encryption algorithm preferences
*/
public Set<SymmetricKeyAlgorithm> getPreferredSymmetricKeyAlgorithms(@Nullable String userId, long keyId) {
KeyAccessor keyAccessor = getKeyAccessor(userId, keyId);
return keyAccessor.getPreferredSymmetricKeyAlgorithms();
public Set<HashAlgorithm> getPreferredHashAlgorithms(String userId) {
return getKeyAccessor(userId, getKeyId()).getPreferredHashAlgorithms();
}
/**
* Return the (sorted) set of preferred compression algorithms of the given key.
*
* @param userId user-id. If this is non-null, the compression algorithms are being extracted from the user-id
* certification signature first.
* @param keyId if of the key in question
* @return compression algorithm preferences
*/
public Set<CompressionAlgorithm> getPreferredCompressionAlgorithms(@Nullable String userId, long keyId) {
KeyAccessor keyAccessor = getKeyAccessor(userId, keyId);
return keyAccessor.getPreferredCompressionAlgorithms();
public Set<HashAlgorithm> getPreferredHashAlgorithms(long keyId) {
return getKeyAccessor(null, keyId).getPreferredHashAlgorithms();
}
public Set<SymmetricKeyAlgorithm> getPreferredSymmetricKeyAlgorithms() {
return getPreferredSymmetricKeyAlgorithms(getPrimaryUserId());
}
public Set<SymmetricKeyAlgorithm> getPreferredSymmetricKeyAlgorithms(String userId) {
return getKeyAccessor(userId, getKeyId()).getPreferredSymmetricKeyAlgorithms();
}
public Set<SymmetricKeyAlgorithm> getPreferredSymmetricKeyAlgorithms(long keyId) {
return getKeyAccessor(null, keyId).getPreferredSymmetricKeyAlgorithms();
}
public Set<CompressionAlgorithm> getPreferredCompressionAlgorithms() {
return getPreferredCompressionAlgorithms(getPrimaryUserId());
}
public Set<CompressionAlgorithm> getPreferredCompressionAlgorithms(String userId) {
return getKeyAccessor(userId, getKeyId()).getPreferredCompressionAlgorithms();
}
public Set<CompressionAlgorithm> getPreferredCompressionAlgorithms(long keyId) {
return getKeyAccessor(null, keyId).getPreferredCompressionAlgorithms();
}
private KeyAccessor getKeyAccessor(@Nullable String userId, long keyID) {

View file

@ -605,40 +605,34 @@ public class KeyRingInfoTest {
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
// Bob is an invalid userId
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob", 0));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob", pkid));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms(null, 123L));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Alice", 123L));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms(123L));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms("Alice", pkid));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(null, pkid));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(null, skid1));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(null, skid2));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms("Alice"));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(pkid));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(skid1));
assertEquals(preferredHashAlgorithms, info.getPreferredHashAlgorithms(skid2));
// Bob is an invalid userId
assertThrows(IllegalArgumentException.class, () -> info.getPreferredCompressionAlgorithms("Bob", 0));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredCompressionAlgorithms("Bob", pkid));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredCompressionAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(IllegalArgumentException.class, () -> info.getPreferredCompressionAlgorithms(null, 123L));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredCompressionAlgorithms("Alice", 123L));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredCompressionAlgorithms(123L));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms("Alice", pkid));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(null, pkid));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(null, skid1));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(null, skid2));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms("Alice"));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(pkid));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(skid1));
assertEquals(preferredCompressionAlgorithms, info.getPreferredCompressionAlgorithms(skid2));
// Bob is an invalid userId
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob", 0));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob", pkid));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Bob"));
// 123 is an invalid keyid
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms(null, 123L));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms("Alice", 123L));
assertThrows(IllegalArgumentException.class, () -> info.getPreferredSymmetricKeyAlgorithms(123L));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms("Alice", pkid));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms(null, pkid));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms(null, skid1));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms(null, skid2));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms("Alice"));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms(pkid));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms(skid1));
assertEquals(preferredSymmetricAlgorithms, info.getPreferredSymmetricKeyAlgorithms(skid2));
}