mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-10-31 17:45:58 +01:00
Get rid of generics in selection strategies
This commit is contained in:
parent
e53a21ff77
commit
83bd157a78
14 changed files with 144 additions and 162 deletions
|
@ -80,7 +80,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
|||
if (keys.length != 0) {
|
||||
List<PGPPublicKey> 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<PGPPublicKey> 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<PGPPublicKeyRing> 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<PGPPublicKey> 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<PGPPublicKey> 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<PGPPublicKey> 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<PGPSecretKey> 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<PGPSecretKey> i = k.getSecretKeys(); i.hasNext(); ) {
|
||||
PGPSecretKey s = i.next();
|
||||
if (EncryptionBuilder.this.<O>signingKeySelector().accept(null, s)) {
|
||||
if (EncryptionBuilder.this.<O>signingKeySelector().accept(s)) {
|
||||
EncryptionBuilder.this.signingKeys.add(s);
|
||||
}
|
||||
}
|
||||
|
@ -424,15 +424,15 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
|||
}
|
||||
}
|
||||
|
||||
<O> PublicKeySelectionStrategy<O> encryptionKeySelector() {
|
||||
return new And.PubKeySelectionStrategy<>(
|
||||
new NoRevocation.PubKeySelectionStrategy<>(),
|
||||
new EncryptionKeySelectionStrategy<>());
|
||||
PublicKeySelectionStrategy encryptionKeySelector() {
|
||||
return new And.PubKeySelectionStrategy(
|
||||
new NoRevocation.PubKeySelectionStrategy(),
|
||||
new EncryptionKeySelectionStrategy());
|
||||
}
|
||||
|
||||
<O> SecretKeySelectionStrategy<O> signingKeySelector() {
|
||||
return new And.SecKeySelectionStrategy<>(
|
||||
new NoRevocation.SecKeySelectionStrategy<>(),
|
||||
new SignatureKeySelectionStrategy<>());
|
||||
SecretKeySelectionStrategy signingKeySelector() {
|
||||
return new And.SecKeySelectionStrategy(
|
||||
new NoRevocation.SecKeySelectionStrategy(),
|
||||
new SignatureKeySelectionStrategy());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <K> Type of the Key
|
||||
* @param <R> Type of the PGPKeyRing
|
||||
* @param <O> Type that describes the owner of this key
|
||||
*/
|
||||
public interface KeySelectionStrategy<K, R, O> {
|
||||
public interface KeySelectionStrategy<K, R> {
|
||||
|
||||
boolean accept(O identifier, K key);
|
||||
|
||||
Set<K> selectKeysFromKeyRing(O identifier, @Nonnull R ring);
|
||||
|
||||
MultiMap<O, K> selectKeysFromKeyRings(MultiMap<O, R> rings);
|
||||
boolean accept(K key);
|
||||
|
||||
Set<K> selectKeysFromKeyRing(@Nonnull R ring);
|
||||
}
|
||||
|
|
|
@ -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 <O> Type that describes the owner of the key.
|
||||
* {@link #accept(Object)}.
|
||||
*/
|
||||
public abstract class PublicKeySelectionStrategy<O> implements KeySelectionStrategy<PGPPublicKey, PGPPublicKeyRing, O> {
|
||||
public abstract class PublicKeySelectionStrategy implements KeySelectionStrategy<PGPPublicKey, PGPPublicKeyRing> {
|
||||
|
||||
@Override
|
||||
public Set<PGPPublicKey> selectKeysFromKeyRing(O identifier, @Nonnull PGPPublicKeyRing ring) {
|
||||
public Set<PGPPublicKey> selectKeysFromKeyRing(@Nonnull PGPPublicKeyRing ring) {
|
||||
Set<PGPPublicKey> keys = new HashSet<>();
|
||||
for (Iterator<PGPPublicKey> i = ring.getPublicKeys(); i.hasNext(); ) {
|
||||
PGPPublicKey key = i.next();
|
||||
if (accept(identifier, key)) keys.add(key);
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiMap<O, PGPPublicKey> selectKeysFromKeyRings(@Nonnull MultiMap<O, PGPPublicKeyRing> keyRings) {
|
||||
MultiMap<O, PGPPublicKey> 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;
|
||||
}
|
||||
|
|
|
@ -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 <O> Type that describes the owner of the key.
|
||||
*/
|
||||
public abstract class SecretKeySelectionStrategy<O> implements KeySelectionStrategy<PGPSecretKey, PGPSecretKeyRing, O> {
|
||||
public abstract class SecretKeySelectionStrategy implements KeySelectionStrategy<PGPSecretKey, PGPSecretKeyRing> {
|
||||
|
||||
@Override
|
||||
public Set<PGPSecretKey> selectKeysFromKeyRing(O identifier, @Nonnull PGPSecretKeyRing ring) {
|
||||
public Set<PGPSecretKey> selectKeysFromKeyRing(@Nonnull PGPSecretKeyRing ring) {
|
||||
Set<PGPSecretKey> keys = new HashSet<>();
|
||||
for (Iterator<PGPSecretKey> i = ring.getSecretKeys(); i.hasNext(); ) {
|
||||
PGPSecretKey key = i.next();
|
||||
if (accept(identifier, key)) keys.add(key);
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiMap<O, PGPSecretKey> selectKeysFromKeyRings(@Nonnull MultiMap<O, PGPSecretKeyRing> keyRings) {
|
||||
MultiMap<O, PGPSecretKey> 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;
|
||||
}
|
||||
|
|
|
@ -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 <O> Type that describes the owner of the key (not used for decision).
|
||||
*/
|
||||
public class EncryptionKeySelectionStrategy<O> extends PublicKeySelectionStrategy<O> {
|
||||
public class EncryptionKeySelectionStrategy extends PublicKeySelectionStrategy {
|
||||
|
||||
@Override
|
||||
public boolean accept(O identifier, @Nonnull PGPPublicKey key) {
|
||||
public boolean accept(@Nonnull PGPPublicKey key) {
|
||||
return key.isEncryptionKey();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,26 +29,22 @@ public class NoRevocation {
|
|||
|
||||
/**
|
||||
* Key Selection Strategy which only accepts {@link PGPPublicKey}s which have no revocation.
|
||||
*
|
||||
* @param <O> Type that describes the owner of this key (not used for this decision).
|
||||
*/
|
||||
public static class PubKeySelectionStrategy<O> extends PublicKeySelectionStrategy<O> {
|
||||
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 <O> Type that describes the owner of this key (not used for this decision).
|
||||
*/
|
||||
public static class SecKeySelectionStrategy<O> extends SecretKeySelectionStrategy<O> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 <O> Type that describes the owner of the key (not used for this decision).
|
||||
*/
|
||||
public class SignatureKeySelectionStrategy<O> extends SecretKeySelectionStrategy<O> {
|
||||
public class SignatureKeySelectionStrategy extends SecretKeySelectionStrategy {
|
||||
|
||||
@Override
|
||||
public boolean accept(O identifier, @Nonnull PGPSecretKey key) {
|
||||
public boolean accept(@Nonnull PGPSecretKey key) {
|
||||
return key.isSigningKey();
|
||||
}
|
||||
|
||||
|
|
|
@ -31,10 +31,16 @@ public class SignedByMasterKey {
|
|||
|
||||
private static final Logger LOGGER = Logger.getLogger(SignedByMasterKey.class.getName());
|
||||
|
||||
public static class PubkeySelectionStrategy extends PublicKeySelectionStrategy<PGPPublicKey> {
|
||||
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;
|
||||
|
|
|
@ -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<O> extends PublicKeySelectionStrategy<O> {
|
||||
public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy {
|
||||
|
||||
private final PublicKeySelectionStrategy<O> left;
|
||||
private final PublicKeySelectionStrategy<O> right;
|
||||
private final Set<PublicKeySelectionStrategy> strategies = new HashSet<>();
|
||||
|
||||
public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy<O> left,
|
||||
@Nonnull PublicKeySelectionStrategy<O> 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<O> extends SecretKeySelectionStrategy<O> {
|
||||
public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy {
|
||||
|
||||
private final SecretKeySelectionStrategy<O> left;
|
||||
private final SecretKeySelectionStrategy<O> right;
|
||||
private final Set<SecretKeySelectionStrategy> strategies = new HashSet<>();
|
||||
|
||||
public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy<O> left,
|
||||
@Nonnull SecretKeySelectionStrategy<O> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<O> extends PublicKeySelectionStrategy<O> {
|
||||
public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy {
|
||||
|
||||
private final PublicKeySelectionStrategy<O> left;
|
||||
private final PublicKeySelectionStrategy<O> right;
|
||||
private final Set<PublicKeySelectionStrategy> strategies = new HashSet<>();
|
||||
|
||||
public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy<O> left,
|
||||
@Nonnull PublicKeySelectionStrategy<O> 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<O> extends SecretKeySelectionStrategy<O> {
|
||||
public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy {
|
||||
|
||||
private final SecretKeySelectionStrategy<O> left;
|
||||
private final SecretKeySelectionStrategy<O> right;
|
||||
private final Set<SecretKeySelectionStrategy> strategies = new HashSet<>();
|
||||
|
||||
public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy<O> left,
|
||||
@Nonnull SecretKeySelectionStrategy<O> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 + '>');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,10 +25,16 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy;
|
|||
|
||||
public class PartialUserId {
|
||||
|
||||
public static class PubRingSelectionStrategy extends PublicKeySelectionStrategy<String> {
|
||||
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<String> 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<String> {
|
||||
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<String> userIds = key.getUserIDs(); userIds.hasNext(); ) {
|
||||
String userId = userIds.next();
|
||||
if (userId.contains(identifier)) {
|
||||
if (userId.contains(partialUserId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<PGPPublicKey> 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<PGPPublicKey> 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<PGPPublicKey> 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<PGPSecretKey> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 = "<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
|
||||
|
@ -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 = "<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
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue