From 19b1a0238d4fd7c57b5231b7f20817e4117dcded Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 15 Nov 2021 13:02:26 +0100 Subject: [PATCH] Fix API for accessing preferred algorithms --- .../encryption_signing/SigningOptions.java | 6 +- .../org/pgpainless/key/info/KeyRingInfo.java | 64 +++++++++---------- .../pgpainless/key/info/KeyRingInfoTest.java | 42 ++++++------ 3 files changed, 52 insertions(+), 60 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/SigningOptions.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/SigningOptions.java index c1714c89..39962724 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/SigningOptions.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/SigningOptions.java @@ -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 hashAlgorithms = keyRingInfo.getPreferredHashAlgorithms(userId, signingPubKey.getKeyID()); + Set 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 hashAlgorithms = keyRingInfo.getPreferredHashAlgorithms(userId, signingPubKey.getKeyID()); + Set hashAlgorithms = userId != null ? keyRingInfo.getPreferredHashAlgorithms(userId) + : keyRingInfo.getPreferredHashAlgorithms(signingPubKey.getKeyID()); HashAlgorithm hashAlgorithm = negotiateHashAlgorithm(hashAlgorithms, PGPainless.getPolicy()); addSigningMethod(secretKey, signingSubkey, hashAlgorithm, signatureType, true); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java b/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java index 66c7f041..f8711037 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/info/KeyRingInfo.java @@ -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 getPreferredHashAlgorithms(@Nullable String userId, long keyID) { - KeyAccessor keyAccessor = getKeyAccessor(userId, keyID); - return keyAccessor.getPreferredHashAlgorithms(); + public Set 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 getPreferredSymmetricKeyAlgorithms(@Nullable String userId, long keyId) { - KeyAccessor keyAccessor = getKeyAccessor(userId, keyId); - return keyAccessor.getPreferredSymmetricKeyAlgorithms(); + public Set 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 getPreferredCompressionAlgorithms(@Nullable String userId, long keyId) { - KeyAccessor keyAccessor = getKeyAccessor(userId, keyId); - return keyAccessor.getPreferredCompressionAlgorithms(); + public Set getPreferredHashAlgorithms(long keyId) { + return getKeyAccessor(null, keyId).getPreferredHashAlgorithms(); + } + + public Set getPreferredSymmetricKeyAlgorithms() { + return getPreferredSymmetricKeyAlgorithms(getPrimaryUserId()); + } + + public Set getPreferredSymmetricKeyAlgorithms(String userId) { + return getKeyAccessor(userId, getKeyId()).getPreferredSymmetricKeyAlgorithms(); + } + + public Set getPreferredSymmetricKeyAlgorithms(long keyId) { + return getKeyAccessor(null, keyId).getPreferredSymmetricKeyAlgorithms(); + } + + public Set getPreferredCompressionAlgorithms() { + return getPreferredCompressionAlgorithms(getPrimaryUserId()); + } + + public Set getPreferredCompressionAlgorithms(String userId) { + return getKeyAccessor(userId, getKeyId()).getPreferredCompressionAlgorithms(); + } + + public Set getPreferredCompressionAlgorithms(long keyId) { + return getKeyAccessor(null, keyId).getPreferredCompressionAlgorithms(); } private KeyAccessor getKeyAccessor(@Nullable String userId, long keyID) { diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/info/KeyRingInfoTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/info/KeyRingInfoTest.java index 2cf030e9..39e13edd 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/info/KeyRingInfoTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/info/KeyRingInfoTest.java @@ -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)); }