diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java index b0f6d022..6ca02604 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilder.java @@ -80,7 +80,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { if (keys.length != 0) { List encryptionKeys = new ArrayList<>(); for (PGPPublicKey k : keys) { - if (encryptionKeySelector().accept(null, k)) { + if (encryptionKeySelector().accept(k)) { encryptionKeys.add(k); } else { throw new IllegalArgumentException("Key " + k.getKeyID() + " is not a valid encryption key."); @@ -102,7 +102,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { List encryptionKeys = new ArrayList<>(); for (PGPPublicKeyRing ring : keys) { for (PGPPublicKey k : ring) { - if (encryptionKeySelector().accept(null, k)) { + if (encryptionKeySelector().accept(k)) { encryptionKeys.add(k); } } @@ -123,7 +123,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { for (PGPPublicKeyRingCollection collection : keys) { for (PGPPublicKeyRing ring : collection) { for (PGPPublicKey k : ring) { - if (encryptionKeySelector().accept(null, k)) { + if (encryptionKeySelector().accept(k)) { encryptionKeys.add(k); } } @@ -151,7 +151,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { Set acceptedSet = acceptedKeyRings.get(identifier); for (PGPPublicKeyRing ring : acceptedSet) { for (PGPPublicKey k : ring) { - if (encryptionKeySelector().accept(null, k)) { + if (encryptionKeySelector().accept(k)) { EncryptionBuilder.this.encryptionKeys.add(k); } } @@ -192,7 +192,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { throw new IllegalArgumentException("Recipient list MUST NOT be empty."); } for (PGPPublicKey k : keys) { - if (encryptionKeySelector().accept(null, k)) { + if (encryptionKeySelector().accept(k)) { EncryptionBuilder.this.encryptionKeys.add(k); } else { throw new IllegalArgumentException("Key " + k.getKeyID() + " is not a valid encryption key."); @@ -209,7 +209,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { for (PGPPublicKeyRing ring : keys) { for (Iterator i = ring.getPublicKeys(); i.hasNext(); ) { PGPPublicKey key = i.next(); - if (encryptionKeySelector().accept(null, key)) { + if (encryptionKeySelector().accept(key)) { EncryptionBuilder.this.encryptionKeys.add(key); } } @@ -222,7 +222,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { for (PGPPublicKeyRing ring : keys) { for (Iterator i = ring.getPublicKeys(); i.hasNext(); ) { PGPPublicKey key = i.next(); - if (encryptionKeySelector().accept(null, key)) { + if (encryptionKeySelector().accept(key)) { EncryptionBuilder.this.encryptionKeys.add(key); } } @@ -243,7 +243,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { for (PGPPublicKeyRing k : acceptedSet) { for (Iterator i = k.getPublicKeys(); i.hasNext(); ) { PGPPublicKey key = i.next(); - if (encryptionKeySelector().accept(null, key)) { + if (encryptionKeySelector().accept(key)) { EncryptionBuilder.this.encryptionKeys.add(key); } } @@ -320,7 +320,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { throw new IllegalArgumentException("Recipient list MUST NOT be empty."); } for (PGPSecretKey s : keys) { - if (EncryptionBuilder.this.signingKeySelector().accept(null, s)) { + if (EncryptionBuilder.this.signingKeySelector().accept(s)) { signingKeys.add(s); } else { throw new IllegalArgumentException("Key " + s.getKeyID() + " is not a valid signing key."); @@ -339,7 +339,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { for (PGPSecretKeyRing key : keys) { for (Iterator i = key.getSecretKeys(); i.hasNext(); ) { PGPSecretKey s = i.next(); - if (EncryptionBuilder.this.signingKeySelector().accept(null, s)) { + if (EncryptionBuilder.this.signingKeySelector().accept(s)) { EncryptionBuilder.this.signingKeys.add(s); } } @@ -362,7 +362,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { for (PGPSecretKeyRing k : acceptedSet) { for (Iterator i = k.getSecretKeys(); i.hasNext(); ) { PGPSecretKey s = i.next(); - if (EncryptionBuilder.this.signingKeySelector().accept(null, s)) { + if (EncryptionBuilder.this.signingKeySelector().accept(s)) { EncryptionBuilder.this.signingKeys.add(s); } } @@ -424,15 +424,15 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } } - PublicKeySelectionStrategy encryptionKeySelector() { - return new And.PubKeySelectionStrategy<>( - new NoRevocation.PubKeySelectionStrategy<>(), - new EncryptionKeySelectionStrategy<>()); + PublicKeySelectionStrategy encryptionKeySelector() { + return new And.PubKeySelectionStrategy( + new NoRevocation.PubKeySelectionStrategy(), + new EncryptionKeySelectionStrategy()); } - SecretKeySelectionStrategy signingKeySelector() { - return new And.SecKeySelectionStrategy<>( - new NoRevocation.SecKeySelectionStrategy<>(), - new SignatureKeySelectionStrategy<>()); + SecretKeySelectionStrategy signingKeySelector() { + return new And.SecKeySelectionStrategy( + new NoRevocation.SecKeySelectionStrategy(), + new SignatureKeySelectionStrategy()); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/KeySelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/KeySelectionStrategy.java index d71fd049..d32777e9 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/KeySelectionStrategy.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/KeySelectionStrategy.java @@ -15,24 +15,18 @@ */ package org.pgpainless.key.selection.key; -import javax.annotation.Nonnull; import java.util.Set; - -import org.pgpainless.util.MultiMap; +import javax.annotation.Nonnull; /** * Interface that describes a selection strategy for OpenPGP keys. * @param Type of the Key * @param Type of the PGPKeyRing - * @param Type that describes the owner of this key */ -public interface KeySelectionStrategy { +public interface KeySelectionStrategy { - boolean accept(O identifier, K key); - - Set selectKeysFromKeyRing(O identifier, @Nonnull R ring); - - MultiMap selectKeysFromKeyRings(MultiMap rings); + boolean accept(K key); + Set selectKeysFromKeyRing(@Nonnull R ring); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/PublicKeySelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/PublicKeySelectionStrategy.java index 76af04f3..9393142e 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/PublicKeySelectionStrategy.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/PublicKeySelectionStrategy.java @@ -15,40 +15,26 @@ */ package org.pgpainless.key.selection.key; -import javax.annotation.Nonnull; import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import javax.annotation.Nonnull; import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; -import org.pgpainless.util.MultiMap; /** * Key Selection Strategy which accepts {@link PGPPublicKey}s that are accepted by the abstract method - * {@link #accept(Object, Object)}. - * - * @param Type that describes the owner of the key. + * {@link #accept(Object)}. */ -public abstract class PublicKeySelectionStrategy implements KeySelectionStrategy { +public abstract class PublicKeySelectionStrategy implements KeySelectionStrategy { @Override - public Set selectKeysFromKeyRing(O identifier, @Nonnull PGPPublicKeyRing ring) { + public Set selectKeysFromKeyRing(@Nonnull PGPPublicKeyRing ring) { Set keys = new HashSet<>(); for (Iterator i = ring.getPublicKeys(); i.hasNext(); ) { PGPPublicKey key = i.next(); - if (accept(identifier, key)) keys.add(key); - } - return keys; - } - - @Override - public MultiMap selectKeysFromKeyRings(@Nonnull MultiMap keyRings) { - MultiMap keys = new MultiMap<>(); - for (O identifier : keyRings.keySet()) { - for (PGPPublicKeyRing ring : keyRings.get(identifier)) { - keys.put(identifier, selectKeysFromKeyRing(identifier, ring)); - } + if (accept(key)) keys.add(key); } return keys; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/SecretKeySelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/SecretKeySelectionStrategy.java index 5f569862..d48e0082 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/SecretKeySelectionStrategy.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/SecretKeySelectionStrategy.java @@ -15,40 +15,27 @@ */ package org.pgpainless.key.selection.key; -import javax.annotation.Nonnull; import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import javax.annotation.Nonnull; import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKeyRing; -import org.pgpainless.util.MultiMap; /** * Key Selection Strategy which accepts {@link PGPSecretKey}s that are accepted by the abstract method - * {@link #accept(Object, Object)}. + * {@link #accept(Object)}. * - * @param Type that describes the owner of the key. */ -public abstract class SecretKeySelectionStrategy implements KeySelectionStrategy { +public abstract class SecretKeySelectionStrategy implements KeySelectionStrategy { @Override - public Set selectKeysFromKeyRing(O identifier, @Nonnull PGPSecretKeyRing ring) { + public Set selectKeysFromKeyRing(@Nonnull PGPSecretKeyRing ring) { Set keys = new HashSet<>(); for (Iterator i = ring.getSecretKeys(); i.hasNext(); ) { PGPSecretKey key = i.next(); - if (accept(identifier, key)) keys.add(key); - } - return keys; - } - - @Override - public MultiMap selectKeysFromKeyRings(@Nonnull MultiMap keyRings) { - MultiMap keys = new MultiMap<>(); - for (O identifier : keyRings.keySet()) { - for (PGPSecretKeyRing ring : keyRings.get(identifier)) { - keys.put(identifier, selectKeysFromKeyRing(identifier, ring)); - } + if (accept(key)) keys.add(key); } return keys; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/EncryptionKeySelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/EncryptionKeySelectionStrategy.java index 876edd20..a861516a 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/EncryptionKeySelectionStrategy.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/EncryptionKeySelectionStrategy.java @@ -22,13 +22,11 @@ import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; /** * Key Selection Strategy that only accepts {@link PGPPublicKey}s which are capable of encryption. - * - * @param Type that describes the owner of the key (not used for decision). */ -public class EncryptionKeySelectionStrategy extends PublicKeySelectionStrategy { +public class EncryptionKeySelectionStrategy extends PublicKeySelectionStrategy { @Override - public boolean accept(O identifier, @Nonnull PGPPublicKey key) { + public boolean accept(@Nonnull PGPPublicKey key) { return key.isEncryptionKey(); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/NoRevocation.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/NoRevocation.java index c7e882ca..38224d7b 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/NoRevocation.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/NoRevocation.java @@ -29,26 +29,22 @@ public class NoRevocation { /** * Key Selection Strategy which only accepts {@link PGPPublicKey}s which have no revocation. - * - * @param Type that describes the owner of this key (not used for this decision). */ - public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy { + public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy { @Override - public boolean accept(O identifier, @Nonnull PGPPublicKey key) { + public boolean accept(@Nonnull PGPPublicKey key) { return !key.hasRevocation(); } } /** * Key Selection Strategy which only accepts {@link PGPSecretKey}s which have no revocation. - * - * @param Type that describes the owner of this key (not used for this decision). */ - public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy { + public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy { @Override - public boolean accept(O identifier, @Nonnull PGPSecretKey key) { + public boolean accept(@Nonnull PGPSecretKey key) { return !key.getPublicKey().hasRevocation(); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/SignatureKeySelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/SignatureKeySelectionStrategy.java index 51275e4f..41246327 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/SignatureKeySelectionStrategy.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/SignatureKeySelectionStrategy.java @@ -22,13 +22,11 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; /** * Key Selection Strategy that only accepts {@link PGPSecretKey}s which are capable of signing. - * - * @param Type that describes the owner of the key (not used for this decision). */ -public class SignatureKeySelectionStrategy extends SecretKeySelectionStrategy { +public class SignatureKeySelectionStrategy extends SecretKeySelectionStrategy { @Override - public boolean accept(O identifier, @Nonnull PGPSecretKey key) { + public boolean accept(@Nonnull PGPSecretKey key) { return key.isSigningKey(); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/SignedByMasterKey.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/SignedByMasterKey.java index e3e6a134..c0f524a8 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/SignedByMasterKey.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/SignedByMasterKey.java @@ -31,10 +31,16 @@ public class SignedByMasterKey { private static final Logger LOGGER = Logger.getLogger(SignedByMasterKey.class.getName()); - public static class PubkeySelectionStrategy extends PublicKeySelectionStrategy { + public static class PubkeySelectionStrategy extends PublicKeySelectionStrategy { + + private final PGPPublicKey masterKey; + + public PubkeySelectionStrategy(PGPPublicKey masterKey) { + this.masterKey = masterKey; + } @Override - public boolean accept(PGPPublicKey masterKey, @Nonnull PGPPublicKey key) { + public boolean accept(@Nonnull PGPPublicKey key) { // Same key -> accept if (Arrays.equals(masterKey.getFingerprint(), key.getFingerprint())) { return true; diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/util/And.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/util/And.java index c494efcd..3128aecb 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/util/And.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/util/And.java @@ -15,6 +15,9 @@ */ package org.pgpainless.key.selection.key.util; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import javax.annotation.Nonnull; import org.bouncycastle.openpgp.PGPPublicKey; @@ -24,37 +27,39 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; public class And { - public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy { + public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy { - private final PublicKeySelectionStrategy left; - private final PublicKeySelectionStrategy right; + private final Set strategies = new HashSet<>(); - public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy left, - @Nonnull PublicKeySelectionStrategy right) { - this.left = left; - this.right = right; + public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy... strategies) { + this.strategies.addAll(Arrays.asList(strategies)); } @Override - public boolean accept(O identifier, PGPPublicKey key) { - return left.accept(identifier, key) && right.accept(identifier, key); + public boolean accept(PGPPublicKey key) { + boolean accept = true; + for (PublicKeySelectionStrategy strategy : strategies) { + accept &= strategy.accept(key); + } + return accept; } } - public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy { + public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy { - private final SecretKeySelectionStrategy left; - private final SecretKeySelectionStrategy right; + private final Set strategies = new HashSet<>(); - public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy left, - @Nonnull SecretKeySelectionStrategy right) { - this.left = left; - this.right = right; + public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy... strategies) { + this.strategies.addAll(Arrays.asList(strategies)); } @Override - public boolean accept(O identifier, PGPSecretKey key) { - return left.accept(identifier, key) && right.accept(identifier, key); + public boolean accept(PGPSecretKey key) { + boolean accept = true; + for (SecretKeySelectionStrategy strategy : strategies) { + accept &= strategy.accept(key); + } + return accept; } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/util/Or.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/util/Or.java index a7b73208..bbd8043c 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/util/Or.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/util/Or.java @@ -15,6 +15,9 @@ */ package org.pgpainless.key.selection.key.util; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import javax.annotation.Nonnull; import org.bouncycastle.openpgp.PGPPublicKey; @@ -24,37 +27,39 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; public class Or { - public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy { + public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy { - private final PublicKeySelectionStrategy left; - private final PublicKeySelectionStrategy right; + private final Set strategies = new HashSet<>(); - public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy left, - @Nonnull PublicKeySelectionStrategy right) { - this.left = left; - this.right = right; + public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy... strategies) { + this.strategies.addAll(Arrays.asList(strategies)); } @Override - public boolean accept(O identifier, PGPPublicKey key) { - return left.accept(identifier, key) || right.accept(identifier, key); + public boolean accept(PGPPublicKey key) { + boolean accept = false; + for (PublicKeySelectionStrategy strategy : strategies) { + accept |= strategy.accept(key); + } + return accept; } } - public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy { + public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy { - private final SecretKeySelectionStrategy left; - private final SecretKeySelectionStrategy right; + private final Set strategies = new HashSet<>(); - public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy left, - @Nonnull SecretKeySelectionStrategy right) { - this.left = left; - this.right = right; + public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy... strategies) { + this.strategies.addAll(Arrays.asList(strategies)); } @Override - public boolean accept(O identifier, PGPSecretKey key) { - return left.accept(identifier, key) || right.accept(identifier, key); + public boolean accept(PGPSecretKey key) { + boolean accept = false; + for (SecretKeySelectionStrategy strategy : strategies) { + accept |= strategy.accept(key); + } + return accept; } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/impl/Email.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/impl/Email.java index 2a33df1b..e23da2a8 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/impl/Email.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/impl/Email.java @@ -15,34 +15,18 @@ */ package org.pgpainless.key.selection.keyring.impl; -import javax.annotation.Nonnull; - -import org.bouncycastle.openpgp.PGPPublicKey; -import org.bouncycastle.openpgp.PGPSecretKey; - public class Email { public static class PubRingSelectionStrategy extends PartialUserId.PubRingSelectionStrategy { - @Override - public boolean accept(@Nonnull String email, @Nonnull PGPPublicKey key) { - // Ensure, that email address is encapsulated in "<",">" - if (!email.matches("^<.+>$")) { - email = "<" + email + ">"; - } - return super.accept(email, key); + public PubRingSelectionStrategy(String email) { + super(email.matches("^<.+>$") ? email : '<' + email + '>'); } } public static class SecRingSelectionStrategy extends PartialUserId.SecRingSelectionStrategy { - - @Override - public boolean accept(String email, PGPSecretKey key) { - // Ensure, that email address is encapsulated in "<",">" - if (!email.matches("^<.+>$")) { - email = "<" + email + ">"; - } - return super.accept(email, key); + public SecRingSelectionStrategy(String email) { + super(email.matches("^<.+>$") ? email : '<' + email + '>'); } } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/impl/PartialUserId.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/impl/PartialUserId.java index 2efdcd98..f8057c88 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/impl/PartialUserId.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/impl/PartialUserId.java @@ -25,10 +25,16 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; public class PartialUserId { - public static class PubRingSelectionStrategy extends PublicKeySelectionStrategy { + public static class PubRingSelectionStrategy extends PublicKeySelectionStrategy { + + protected final String identifier; + + public PubRingSelectionStrategy(String identifier) { + this.identifier = identifier; + } @Override - public boolean accept(String identifier, @Nonnull PGPPublicKey key) { + public boolean accept(@Nonnull PGPPublicKey key) { for (Iterator userIds = key.getUserIDs(); userIds.hasNext(); ) { String userId = userIds.next(); if (userId.contains(identifier)) { @@ -39,13 +45,19 @@ public class PartialUserId { } } - public static class SecRingSelectionStrategy extends SecretKeySelectionStrategy { + public static class SecRingSelectionStrategy extends SecretKeySelectionStrategy { + + protected final String partialUserId; + + public SecRingSelectionStrategy(String partialUserId) { + this.partialUserId = partialUserId; + } @Override - public boolean accept(String identifier, @Nonnull PGPSecretKey key) { + public boolean accept(@Nonnull PGPSecretKey key) { for (Iterator userIds = key.getUserIDs(); userIds.hasNext(); ) { String userId = userIds.next(); - if (userId.contains(identifier)) { + if (userId.contains(partialUserId)) { return true; } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java b/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java index b68f63b3..a86d8e9b 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java @@ -151,16 +151,16 @@ public class BCUtil { throw new IllegalArgumentException("Given key is not a master key."); } // Only select keys which are signed by the master key and not revoked. - PublicKeySelectionStrategy selector = new And.PubKeySelectionStrategy<>( - new SignedByMasterKey.PubkeySelectionStrategy(), - new NoRevocation.PubKeySelectionStrategy<>()); + PublicKeySelectionStrategy selector = new And.PubKeySelectionStrategy( + new SignedByMasterKey.PubkeySelectionStrategy(masterKey), + new NoRevocation.PubKeySelectionStrategy()); PGPPublicKeyRing cleaned = ring; Iterator publicKeys = ring.getPublicKeys(); while (publicKeys.hasNext()) { PGPPublicKey publicKey = publicKeys.next(); - if (!selector.accept(masterKey, publicKey)) { + if (!selector.accept(publicKey)) { cleaned = PGPPublicKeyRing.removePublicKey(cleaned, publicKey); } } @@ -182,16 +182,16 @@ public class BCUtil { throw new IllegalArgumentException("Given key is not a master key."); } // Only select keys which are signed by the master key and not revoked. - PublicKeySelectionStrategy selector = new And.PubKeySelectionStrategy<>( - new SignedByMasterKey.PubkeySelectionStrategy(), - new NoRevocation.PubKeySelectionStrategy<>()); + PublicKeySelectionStrategy selector = new And.PubKeySelectionStrategy( + new SignedByMasterKey.PubkeySelectionStrategy(masterKey), + new NoRevocation.PubKeySelectionStrategy()); PGPSecretKeyRing cleaned = ring; Iterator secretKeys = ring.getSecretKeys(); while (secretKeys.hasNext()) { PGPSecretKey secretKey = secretKeys.next(); - if (!selector.accept(masterKey, secretKey.getPublicKey())) { + if (!selector.accept(secretKey.getPublicKey())) { cleaned = PGPSecretKeyRing.removeSecretKey(cleaned, secretKey); } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/selection/keyring/EmailKeyRingSelectionStrategyTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/selection/keyring/EmailKeyRingSelectionStrategyTest.java index 8d9c398f..8dc91d17 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/selection/keyring/EmailKeyRingSelectionStrategyTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/selection/keyring/EmailKeyRingSelectionStrategyTest.java @@ -29,15 +29,14 @@ import org.pgpainless.key.selection.keyring.impl.Email; public class EmailKeyRingSelectionStrategyTest { - Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(); - Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(); - @Test public void testMatchingEmailUIDAcceptedOnPubKey() throws IOException { String uid = ""; PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey(); - assertTrue(pubKeySelectionStrategy.accept(uid, key)); + Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(uid); + + assertTrue(pubKeySelectionStrategy.accept(key)); } @Test @@ -45,14 +44,19 @@ public class EmailKeyRingSelectionStrategyTest { String uid = "emil@email.user"; PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey(); - assertTrue(pubKeySelectionStrategy.accept(uid, key)); + Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(uid); + + assertTrue(pubKeySelectionStrategy.accept(key)); } @Test public void testPubKeyWithDifferentUIDIsRejected() throws IOException { String wrongUid = "emilia@email.user"; PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey(); - assertFalse(pubKeySelectionStrategy.accept(wrongUid, key)); + + Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(wrongUid); + + assertFalse(pubKeySelectionStrategy.accept(key)); } @Test @@ -60,7 +64,9 @@ public class EmailKeyRingSelectionStrategyTest { String uid = ""; PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey(); - assertTrue(secKeySelectionStrategy.accept(uid, key)); + Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(uid); + + assertTrue(secKeySelectionStrategy.accept(key)); } @Test @@ -68,13 +74,18 @@ public class EmailKeyRingSelectionStrategyTest { String uid = "emil@email.user"; PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey(); - assertTrue(secKeySelectionStrategy.accept(uid, key)); + Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(uid); + + assertTrue(secKeySelectionStrategy.accept(key)); } @Test public void testSecKeyWithDifferentUIDIsRejected() throws IOException, PGPException { String wrongUid = "emilia@email.user"; PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey(); - assertFalse(secKeySelectionStrategy.accept(wrongUid, key)); + + Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(wrongUid); + + assertFalse(secKeySelectionStrategy.accept(key)); } }