mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-30 08:12:06 +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) {
|
if (keys.length != 0) {
|
||||||
List<PGPPublicKey> encryptionKeys = new ArrayList<>();
|
List<PGPPublicKey> encryptionKeys = new ArrayList<>();
|
||||||
for (PGPPublicKey k : keys) {
|
for (PGPPublicKey k : keys) {
|
||||||
if (encryptionKeySelector().accept(null, k)) {
|
if (encryptionKeySelector().accept(k)) {
|
||||||
encryptionKeys.add(k);
|
encryptionKeys.add(k);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Key " + k.getKeyID() + " is not a valid encryption key.");
|
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<>();
|
List<PGPPublicKey> encryptionKeys = new ArrayList<>();
|
||||||
for (PGPPublicKeyRing ring : keys) {
|
for (PGPPublicKeyRing ring : keys) {
|
||||||
for (PGPPublicKey k : ring) {
|
for (PGPPublicKey k : ring) {
|
||||||
if (encryptionKeySelector().accept(null, k)) {
|
if (encryptionKeySelector().accept(k)) {
|
||||||
encryptionKeys.add(k);
|
encryptionKeys.add(k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
for (PGPPublicKeyRingCollection collection : keys) {
|
for (PGPPublicKeyRingCollection collection : keys) {
|
||||||
for (PGPPublicKeyRing ring : collection) {
|
for (PGPPublicKeyRing ring : collection) {
|
||||||
for (PGPPublicKey k : ring) {
|
for (PGPPublicKey k : ring) {
|
||||||
if (encryptionKeySelector().accept(null, k)) {
|
if (encryptionKeySelector().accept(k)) {
|
||||||
encryptionKeys.add(k);
|
encryptionKeys.add(k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -151,7 +151,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
Set<PGPPublicKeyRing> acceptedSet = acceptedKeyRings.get(identifier);
|
Set<PGPPublicKeyRing> acceptedSet = acceptedKeyRings.get(identifier);
|
||||||
for (PGPPublicKeyRing ring : acceptedSet) {
|
for (PGPPublicKeyRing ring : acceptedSet) {
|
||||||
for (PGPPublicKey k : ring) {
|
for (PGPPublicKey k : ring) {
|
||||||
if (encryptionKeySelector().accept(null, k)) {
|
if (encryptionKeySelector().accept(k)) {
|
||||||
EncryptionBuilder.this.encryptionKeys.add(k);
|
EncryptionBuilder.this.encryptionKeys.add(k);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,7 +192,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
throw new IllegalArgumentException("Recipient list MUST NOT be empty.");
|
throw new IllegalArgumentException("Recipient list MUST NOT be empty.");
|
||||||
}
|
}
|
||||||
for (PGPPublicKey k : keys) {
|
for (PGPPublicKey k : keys) {
|
||||||
if (encryptionKeySelector().accept(null, k)) {
|
if (encryptionKeySelector().accept(k)) {
|
||||||
EncryptionBuilder.this.encryptionKeys.add(k);
|
EncryptionBuilder.this.encryptionKeys.add(k);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Key " + k.getKeyID() + " is not a valid encryption key.");
|
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 (PGPPublicKeyRing ring : keys) {
|
||||||
for (Iterator<PGPPublicKey> i = ring.getPublicKeys(); i.hasNext(); ) {
|
for (Iterator<PGPPublicKey> i = ring.getPublicKeys(); i.hasNext(); ) {
|
||||||
PGPPublicKey key = i.next();
|
PGPPublicKey key = i.next();
|
||||||
if (encryptionKeySelector().accept(null, key)) {
|
if (encryptionKeySelector().accept(key)) {
|
||||||
EncryptionBuilder.this.encryptionKeys.add(key);
|
EncryptionBuilder.this.encryptionKeys.add(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -222,7 +222,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
for (PGPPublicKeyRing ring : keys) {
|
for (PGPPublicKeyRing ring : keys) {
|
||||||
for (Iterator<PGPPublicKey> i = ring.getPublicKeys(); i.hasNext(); ) {
|
for (Iterator<PGPPublicKey> i = ring.getPublicKeys(); i.hasNext(); ) {
|
||||||
PGPPublicKey key = i.next();
|
PGPPublicKey key = i.next();
|
||||||
if (encryptionKeySelector().accept(null, key)) {
|
if (encryptionKeySelector().accept(key)) {
|
||||||
EncryptionBuilder.this.encryptionKeys.add(key);
|
EncryptionBuilder.this.encryptionKeys.add(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -243,7 +243,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
for (PGPPublicKeyRing k : acceptedSet) {
|
for (PGPPublicKeyRing k : acceptedSet) {
|
||||||
for (Iterator<PGPPublicKey> i = k.getPublicKeys(); i.hasNext(); ) {
|
for (Iterator<PGPPublicKey> i = k.getPublicKeys(); i.hasNext(); ) {
|
||||||
PGPPublicKey key = i.next();
|
PGPPublicKey key = i.next();
|
||||||
if (encryptionKeySelector().accept(null, key)) {
|
if (encryptionKeySelector().accept(key)) {
|
||||||
EncryptionBuilder.this.encryptionKeys.add(key);
|
EncryptionBuilder.this.encryptionKeys.add(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -320,7 +320,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
throw new IllegalArgumentException("Recipient list MUST NOT be empty.");
|
throw new IllegalArgumentException("Recipient list MUST NOT be empty.");
|
||||||
}
|
}
|
||||||
for (PGPSecretKey s : keys) {
|
for (PGPSecretKey s : keys) {
|
||||||
if (EncryptionBuilder.this.signingKeySelector().accept(null, s)) {
|
if (EncryptionBuilder.this.signingKeySelector().accept(s)) {
|
||||||
signingKeys.add(s);
|
signingKeys.add(s);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Key " + s.getKeyID() + " is not a valid signing key.");
|
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 (PGPSecretKeyRing key : keys) {
|
||||||
for (Iterator<PGPSecretKey> i = key.getSecretKeys(); i.hasNext(); ) {
|
for (Iterator<PGPSecretKey> i = key.getSecretKeys(); i.hasNext(); ) {
|
||||||
PGPSecretKey s = i.next();
|
PGPSecretKey s = i.next();
|
||||||
if (EncryptionBuilder.this.signingKeySelector().accept(null, s)) {
|
if (EncryptionBuilder.this.signingKeySelector().accept(s)) {
|
||||||
EncryptionBuilder.this.signingKeys.add(s);
|
EncryptionBuilder.this.signingKeys.add(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -362,7 +362,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
for (PGPSecretKeyRing k : acceptedSet) {
|
for (PGPSecretKeyRing k : acceptedSet) {
|
||||||
for (Iterator<PGPSecretKey> i = k.getSecretKeys(); i.hasNext(); ) {
|
for (Iterator<PGPSecretKey> i = k.getSecretKeys(); i.hasNext(); ) {
|
||||||
PGPSecretKey s = i.next();
|
PGPSecretKey s = i.next();
|
||||||
if (EncryptionBuilder.this.<O>signingKeySelector().accept(null, s)) {
|
if (EncryptionBuilder.this.<O>signingKeySelector().accept(s)) {
|
||||||
EncryptionBuilder.this.signingKeys.add(s);
|
EncryptionBuilder.this.signingKeys.add(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -424,15 +424,15 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
<O> PublicKeySelectionStrategy<O> encryptionKeySelector() {
|
PublicKeySelectionStrategy encryptionKeySelector() {
|
||||||
return new And.PubKeySelectionStrategy<>(
|
return new And.PubKeySelectionStrategy(
|
||||||
new NoRevocation.PubKeySelectionStrategy<>(),
|
new NoRevocation.PubKeySelectionStrategy(),
|
||||||
new EncryptionKeySelectionStrategy<>());
|
new EncryptionKeySelectionStrategy());
|
||||||
}
|
}
|
||||||
|
|
||||||
<O> SecretKeySelectionStrategy<O> signingKeySelector() {
|
SecretKeySelectionStrategy signingKeySelector() {
|
||||||
return new And.SecKeySelectionStrategy<>(
|
return new And.SecKeySelectionStrategy(
|
||||||
new NoRevocation.SecKeySelectionStrategy<>(),
|
new NoRevocation.SecKeySelectionStrategy(),
|
||||||
new SignatureKeySelectionStrategy<>());
|
new SignatureKeySelectionStrategy());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,24 +15,18 @@
|
||||||
*/
|
*/
|
||||||
package org.pgpainless.key.selection.key;
|
package org.pgpainless.key.selection.key;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
import org.pgpainless.util.MultiMap;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface that describes a selection strategy for OpenPGP keys.
|
* Interface that describes a selection strategy for OpenPGP keys.
|
||||||
* @param <K> Type of the Key
|
* @param <K> Type of the Key
|
||||||
* @param <R> Type of the PGPKeyRing
|
* @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);
|
boolean accept(K key);
|
||||||
|
|
||||||
Set<K> selectKeysFromKeyRing(O identifier, @Nonnull R ring);
|
|
||||||
|
|
||||||
MultiMap<O, K> selectKeysFromKeyRings(MultiMap<O, R> rings);
|
|
||||||
|
|
||||||
|
Set<K> selectKeysFromKeyRing(@Nonnull R ring);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,40 +15,26 @@
|
||||||
*/
|
*/
|
||||||
package org.pgpainless.key.selection.key;
|
package org.pgpainless.key.selection.key;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
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
|
* Key Selection Strategy which accepts {@link PGPPublicKey}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 PublicKeySelectionStrategy<O> implements KeySelectionStrategy<PGPPublicKey, PGPPublicKeyRing, O> {
|
public abstract class PublicKeySelectionStrategy implements KeySelectionStrategy<PGPPublicKey, PGPPublicKeyRing> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<PGPPublicKey> selectKeysFromKeyRing(O identifier, @Nonnull PGPPublicKeyRing ring) {
|
public Set<PGPPublicKey> selectKeysFromKeyRing(@Nonnull PGPPublicKeyRing ring) {
|
||||||
Set<PGPPublicKey> keys = new HashSet<>();
|
Set<PGPPublicKey> keys = new HashSet<>();
|
||||||
for (Iterator<PGPPublicKey> i = ring.getPublicKeys(); i.hasNext(); ) {
|
for (Iterator<PGPPublicKey> i = ring.getPublicKeys(); i.hasNext(); ) {
|
||||||
PGPPublicKey key = i.next();
|
PGPPublicKey key = i.next();
|
||||||
if (accept(identifier, key)) keys.add(key);
|
if (accept(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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,40 +15,27 @@
|
||||||
*/
|
*/
|
||||||
package org.pgpainless.key.selection.key;
|
package org.pgpainless.key.selection.key;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPSecretKey;
|
import org.bouncycastle.openpgp.PGPSecretKey;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
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
|
* 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
|
@Override
|
||||||
public Set<PGPSecretKey> selectKeysFromKeyRing(O identifier, @Nonnull PGPSecretKeyRing ring) {
|
public Set<PGPSecretKey> selectKeysFromKeyRing(@Nonnull PGPSecretKeyRing ring) {
|
||||||
Set<PGPSecretKey> keys = new HashSet<>();
|
Set<PGPSecretKey> keys = new HashSet<>();
|
||||||
for (Iterator<PGPSecretKey> i = ring.getSecretKeys(); i.hasNext(); ) {
|
for (Iterator<PGPSecretKey> i = ring.getSecretKeys(); i.hasNext(); ) {
|
||||||
PGPSecretKey key = i.next();
|
PGPSecretKey key = i.next();
|
||||||
if (accept(identifier, key)) keys.add(key);
|
if (accept(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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return keys;
|
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.
|
* 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
|
@Override
|
||||||
public boolean accept(O identifier, @Nonnull PGPPublicKey key) {
|
public boolean accept(@Nonnull PGPPublicKey key) {
|
||||||
return key.isEncryptionKey();
|
return key.isEncryptionKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,26 +29,22 @@ public class NoRevocation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Key Selection Strategy which only accepts {@link PGPPublicKey}s which have no revocation.
|
* 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
|
@Override
|
||||||
public boolean accept(O identifier, @Nonnull PGPPublicKey key) {
|
public boolean accept(@Nonnull PGPPublicKey key) {
|
||||||
return !key.hasRevocation();
|
return !key.hasRevocation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Key Selection Strategy which only accepts {@link PGPSecretKey}s which have no revocation.
|
* 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
|
@Override
|
||||||
public boolean accept(O identifier, @Nonnull PGPSecretKey key) {
|
public boolean accept(@Nonnull PGPSecretKey key) {
|
||||||
return !key.getPublicKey().hasRevocation();
|
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.
|
* 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
|
@Override
|
||||||
public boolean accept(O identifier, @Nonnull PGPSecretKey key) {
|
public boolean accept(@Nonnull PGPSecretKey key) {
|
||||||
return key.isSigningKey();
|
return key.isSigningKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,10 +31,16 @@ public class SignedByMasterKey {
|
||||||
|
|
||||||
private static final Logger LOGGER = Logger.getLogger(SignedByMasterKey.class.getName());
|
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
|
@Override
|
||||||
public boolean accept(PGPPublicKey masterKey, @Nonnull PGPPublicKey key) {
|
public boolean accept(@Nonnull PGPPublicKey key) {
|
||||||
// Same key -> accept
|
// Same key -> accept
|
||||||
if (Arrays.equals(masterKey.getFingerprint(), key.getFingerprint())) {
|
if (Arrays.equals(masterKey.getFingerprint(), key.getFingerprint())) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -15,6 +15,9 @@
|
||||||
*/
|
*/
|
||||||
package org.pgpainless.key.selection.key.util;
|
package org.pgpainless.key.selection.key.util;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||||
|
@ -24,37 +27,39 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy;
|
||||||
|
|
||||||
public class And {
|
public class And {
|
||||||
|
|
||||||
public static class PubKeySelectionStrategy<O> extends PublicKeySelectionStrategy<O> {
|
public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy {
|
||||||
|
|
||||||
private final PublicKeySelectionStrategy<O> left;
|
private final Set<PublicKeySelectionStrategy> strategies = new HashSet<>();
|
||||||
private final PublicKeySelectionStrategy<O> right;
|
|
||||||
|
|
||||||
public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy<O> left,
|
public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy... strategies) {
|
||||||
@Nonnull PublicKeySelectionStrategy<O> right) {
|
this.strategies.addAll(Arrays.asList(strategies));
|
||||||
this.left = left;
|
|
||||||
this.right = right;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(O identifier, PGPPublicKey key) {
|
public boolean accept(PGPPublicKey key) {
|
||||||
return left.accept(identifier, key) && right.accept(identifier, 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 Set<SecretKeySelectionStrategy> strategies = new HashSet<>();
|
||||||
private final SecretKeySelectionStrategy<O> right;
|
|
||||||
|
|
||||||
public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy<O> left,
|
public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy... strategies) {
|
||||||
@Nonnull SecretKeySelectionStrategy<O> right) {
|
this.strategies.addAll(Arrays.asList(strategies));
|
||||||
this.left = left;
|
|
||||||
this.right = right;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(O identifier, PGPSecretKey key) {
|
public boolean accept(PGPSecretKey key) {
|
||||||
return left.accept(identifier, key) && right.accept(identifier, 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;
|
package org.pgpainless.key.selection.key.util;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||||
|
@ -24,37 +27,39 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy;
|
||||||
|
|
||||||
public class Or {
|
public class Or {
|
||||||
|
|
||||||
public static class PubKeySelectionStrategy<O> extends PublicKeySelectionStrategy<O> {
|
public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy {
|
||||||
|
|
||||||
private final PublicKeySelectionStrategy<O> left;
|
private final Set<PublicKeySelectionStrategy> strategies = new HashSet<>();
|
||||||
private final PublicKeySelectionStrategy<O> right;
|
|
||||||
|
|
||||||
public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy<O> left,
|
public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy... strategies) {
|
||||||
@Nonnull PublicKeySelectionStrategy<O> right) {
|
this.strategies.addAll(Arrays.asList(strategies));
|
||||||
this.left = left;
|
|
||||||
this.right = right;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(O identifier, PGPPublicKey key) {
|
public boolean accept(PGPPublicKey key) {
|
||||||
return left.accept(identifier, key) || right.accept(identifier, 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 Set<SecretKeySelectionStrategy> strategies = new HashSet<>();
|
||||||
private final SecretKeySelectionStrategy<O> right;
|
|
||||||
|
|
||||||
public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy<O> left,
|
public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy... strategies) {
|
||||||
@Nonnull SecretKeySelectionStrategy<O> right) {
|
this.strategies.addAll(Arrays.asList(strategies));
|
||||||
this.left = left;
|
|
||||||
this.right = right;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(O identifier, PGPSecretKey key) {
|
public boolean accept(PGPSecretKey key) {
|
||||||
return left.accept(identifier, key) || right.accept(identifier, 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;
|
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 class Email {
|
||||||
|
|
||||||
public static class PubRingSelectionStrategy extends PartialUserId.PubRingSelectionStrategy {
|
public static class PubRingSelectionStrategy extends PartialUserId.PubRingSelectionStrategy {
|
||||||
|
|
||||||
@Override
|
public PubRingSelectionStrategy(String email) {
|
||||||
public boolean accept(@Nonnull String email, @Nonnull PGPPublicKey key) {
|
super(email.matches("^<.+>$") ? email : '<' + email + '>');
|
||||||
// Ensure, that email address is encapsulated in "<",">"
|
|
||||||
if (!email.matches("^<.+>$")) {
|
|
||||||
email = "<" + email + ">";
|
|
||||||
}
|
|
||||||
return super.accept(email, key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class SecRingSelectionStrategy extends PartialUserId.SecRingSelectionStrategy {
|
public static class SecRingSelectionStrategy extends PartialUserId.SecRingSelectionStrategy {
|
||||||
|
public SecRingSelectionStrategy(String email) {
|
||||||
@Override
|
super(email.matches("^<.+>$") ? email : '<' + email + '>');
|
||||||
public boolean accept(String email, PGPSecretKey key) {
|
|
||||||
// Ensure, that email address is encapsulated in "<",">"
|
|
||||||
if (!email.matches("^<.+>$")) {
|
|
||||||
email = "<" + email + ">";
|
|
||||||
}
|
|
||||||
return super.accept(email, key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,16 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy;
|
||||||
|
|
||||||
public class PartialUserId {
|
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
|
@Override
|
||||||
public boolean accept(String identifier, @Nonnull PGPPublicKey key) {
|
public boolean accept(@Nonnull PGPPublicKey key) {
|
||||||
for (Iterator<String> userIds = key.getUserIDs(); userIds.hasNext(); ) {
|
for (Iterator<String> userIds = key.getUserIDs(); userIds.hasNext(); ) {
|
||||||
String userId = userIds.next();
|
String userId = userIds.next();
|
||||||
if (userId.contains(identifier)) {
|
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
|
@Override
|
||||||
public boolean accept(String identifier, @Nonnull PGPSecretKey key) {
|
public boolean accept(@Nonnull PGPSecretKey key) {
|
||||||
for (Iterator<String> userIds = key.getUserIDs(); userIds.hasNext(); ) {
|
for (Iterator<String> userIds = key.getUserIDs(); userIds.hasNext(); ) {
|
||||||
String userId = userIds.next();
|
String userId = userIds.next();
|
||||||
if (userId.contains(identifier)) {
|
if (userId.contains(partialUserId)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,16 +151,16 @@ public class BCUtil {
|
||||||
throw new IllegalArgumentException("Given key is not a master key.");
|
throw new IllegalArgumentException("Given key is not a master key.");
|
||||||
}
|
}
|
||||||
// Only select keys which are signed by the master key and not revoked.
|
// Only select keys which are signed by the master key and not revoked.
|
||||||
PublicKeySelectionStrategy<PGPPublicKey> selector = new And.PubKeySelectionStrategy<>(
|
PublicKeySelectionStrategy selector = new And.PubKeySelectionStrategy(
|
||||||
new SignedByMasterKey.PubkeySelectionStrategy(),
|
new SignedByMasterKey.PubkeySelectionStrategy(masterKey),
|
||||||
new NoRevocation.PubKeySelectionStrategy<>());
|
new NoRevocation.PubKeySelectionStrategy());
|
||||||
|
|
||||||
PGPPublicKeyRing cleaned = ring;
|
PGPPublicKeyRing cleaned = ring;
|
||||||
|
|
||||||
Iterator<PGPPublicKey> publicKeys = ring.getPublicKeys();
|
Iterator<PGPPublicKey> publicKeys = ring.getPublicKeys();
|
||||||
while (publicKeys.hasNext()) {
|
while (publicKeys.hasNext()) {
|
||||||
PGPPublicKey publicKey = publicKeys.next();
|
PGPPublicKey publicKey = publicKeys.next();
|
||||||
if (!selector.accept(masterKey, publicKey)) {
|
if (!selector.accept(publicKey)) {
|
||||||
cleaned = PGPPublicKeyRing.removePublicKey(cleaned, publicKey);
|
cleaned = PGPPublicKeyRing.removePublicKey(cleaned, publicKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -182,16 +182,16 @@ public class BCUtil {
|
||||||
throw new IllegalArgumentException("Given key is not a master key.");
|
throw new IllegalArgumentException("Given key is not a master key.");
|
||||||
}
|
}
|
||||||
// Only select keys which are signed by the master key and not revoked.
|
// Only select keys which are signed by the master key and not revoked.
|
||||||
PublicKeySelectionStrategy<PGPPublicKey> selector = new And.PubKeySelectionStrategy<>(
|
PublicKeySelectionStrategy selector = new And.PubKeySelectionStrategy(
|
||||||
new SignedByMasterKey.PubkeySelectionStrategy(),
|
new SignedByMasterKey.PubkeySelectionStrategy(masterKey),
|
||||||
new NoRevocation.PubKeySelectionStrategy<>());
|
new NoRevocation.PubKeySelectionStrategy());
|
||||||
|
|
||||||
PGPSecretKeyRing cleaned = ring;
|
PGPSecretKeyRing cleaned = ring;
|
||||||
|
|
||||||
Iterator<PGPSecretKey> secretKeys = ring.getSecretKeys();
|
Iterator<PGPSecretKey> secretKeys = ring.getSecretKeys();
|
||||||
while (secretKeys.hasNext()) {
|
while (secretKeys.hasNext()) {
|
||||||
PGPSecretKey secretKey = secretKeys.next();
|
PGPSecretKey secretKey = secretKeys.next();
|
||||||
if (!selector.accept(masterKey, secretKey.getPublicKey())) {
|
if (!selector.accept(secretKey.getPublicKey())) {
|
||||||
cleaned = PGPSecretKeyRing.removeSecretKey(cleaned, secretKey);
|
cleaned = PGPSecretKeyRing.removeSecretKey(cleaned, secretKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,15 +29,14 @@ import org.pgpainless.key.selection.keyring.impl.Email;
|
||||||
|
|
||||||
public class EmailKeyRingSelectionStrategyTest {
|
public class EmailKeyRingSelectionStrategyTest {
|
||||||
|
|
||||||
Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy();
|
|
||||||
Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy();
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMatchingEmailUIDAcceptedOnPubKey() throws IOException {
|
public void testMatchingEmailUIDAcceptedOnPubKey() throws IOException {
|
||||||
String uid = "<emil@email.user>";
|
String uid = "<emil@email.user>";
|
||||||
PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey();
|
PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey();
|
||||||
|
|
||||||
assertTrue(pubKeySelectionStrategy.accept(uid, key));
|
Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(uid);
|
||||||
|
|
||||||
|
assertTrue(pubKeySelectionStrategy.accept(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -45,14 +44,19 @@ public class EmailKeyRingSelectionStrategyTest {
|
||||||
String uid = "emil@email.user";
|
String uid = "emil@email.user";
|
||||||
PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey();
|
PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey();
|
||||||
|
|
||||||
assertTrue(pubKeySelectionStrategy.accept(uid, key));
|
Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(uid);
|
||||||
|
|
||||||
|
assertTrue(pubKeySelectionStrategy.accept(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPubKeyWithDifferentUIDIsRejected() throws IOException {
|
public void testPubKeyWithDifferentUIDIsRejected() throws IOException {
|
||||||
String wrongUid = "emilia@email.user";
|
String wrongUid = "emilia@email.user";
|
||||||
PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey();
|
PGPPublicKey key = TestKeys.getEmilPublicKeyRing().getPublicKey();
|
||||||
assertFalse(pubKeySelectionStrategy.accept(wrongUid, key));
|
|
||||||
|
Email.PubRingSelectionStrategy pubKeySelectionStrategy = new Email.PubRingSelectionStrategy(wrongUid);
|
||||||
|
|
||||||
|
assertFalse(pubKeySelectionStrategy.accept(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -60,7 +64,9 @@ public class EmailKeyRingSelectionStrategyTest {
|
||||||
String uid = "<emil@email.user>";
|
String uid = "<emil@email.user>";
|
||||||
PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey();
|
PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey();
|
||||||
|
|
||||||
assertTrue(secKeySelectionStrategy.accept(uid, key));
|
Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(uid);
|
||||||
|
|
||||||
|
assertTrue(secKeySelectionStrategy.accept(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -68,13 +74,18 @@ public class EmailKeyRingSelectionStrategyTest {
|
||||||
String uid = "emil@email.user";
|
String uid = "emil@email.user";
|
||||||
PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey();
|
PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey();
|
||||||
|
|
||||||
assertTrue(secKeySelectionStrategy.accept(uid, key));
|
Email.SecRingSelectionStrategy secKeySelectionStrategy = new Email.SecRingSelectionStrategy(uid);
|
||||||
|
|
||||||
|
assertTrue(secKeySelectionStrategy.accept(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSecKeyWithDifferentUIDIsRejected() throws IOException, PGPException {
|
public void testSecKeyWithDifferentUIDIsRejected() throws IOException, PGPException {
|
||||||
String wrongUid = "emilia@email.user";
|
String wrongUid = "emilia@email.user";
|
||||||
PGPSecretKey key = TestKeys.getEmilSecretKeyRing().getSecretKey();
|
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