1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-29 15:52:08 +01:00

Annotate EncryptionOptions methods with @Nonnull

This commit is contained in:
Paul Schaub 2023-05-03 14:38:52 +02:00
parent 304350fe5c
commit 64c6d7a904
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 23 additions and 17 deletions

View file

@ -75,7 +75,7 @@ public class EncryptionOptions {
this(EncryptionPurpose.ANY); this(EncryptionPurpose.ANY);
} }
public EncryptionOptions(EncryptionPurpose purpose) { public EncryptionOptions(@Nonnull EncryptionPurpose purpose) {
this.purpose = purpose; this.purpose = purpose;
} }
@ -118,7 +118,7 @@ public class EncryptionOptions {
* @param keys keys * @param keys keys
* @return this * @return this
*/ */
public EncryptionOptions addRecipients(Iterable<PGPPublicKeyRing> keys) { public EncryptionOptions addRecipients(@Nonnull Iterable<PGPPublicKeyRing> keys) {
if (!keys.iterator().hasNext()) { if (!keys.iterator().hasNext()) {
throw new IllegalArgumentException("Set of recipient keys cannot be empty."); throw new IllegalArgumentException("Set of recipient keys cannot be empty.");
} }
@ -154,7 +154,7 @@ public class EncryptionOptions {
* @param userId user id * @param userId user id
* @return this * @return this
*/ */
public EncryptionOptions addRecipient(PGPPublicKeyRing key, String userId) { public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key, @Nonnull CharSequence userId) {
return addRecipient(key, userId, encryptionKeySelector); return addRecipient(key, userId, encryptionKeySelector);
} }
@ -167,11 +167,13 @@ public class EncryptionOptions {
* @param encryptionKeySelectionStrategy strategy to select one or more encryption subkeys to encrypt to * @param encryptionKeySelectionStrategy strategy to select one or more encryption subkeys to encrypt to
* @return this * @return this
*/ */
public EncryptionOptions addRecipient(PGPPublicKeyRing key, String userId, EncryptionKeySelector encryptionKeySelectionStrategy) { public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key,
@Nonnull CharSequence userId,
@Nonnull EncryptionKeySelector encryptionKeySelectionStrategy) {
KeyRingInfo info = new KeyRingInfo(key, new Date()); KeyRingInfo info = new KeyRingInfo(key, new Date());
List<PGPPublicKey> encryptionSubkeys = encryptionKeySelectionStrategy List<PGPPublicKey> encryptionSubkeys = encryptionKeySelectionStrategy
.selectEncryptionSubkeys(info.getEncryptionSubkeys(userId, purpose)); .selectEncryptionSubkeys(info.getEncryptionSubkeys(userId.toString(), purpose));
if (encryptionSubkeys.isEmpty()) { if (encryptionSubkeys.isEmpty()) {
throw new KeyException.UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(key)); throw new KeyException.UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(key));
} }
@ -179,7 +181,7 @@ public class EncryptionOptions {
for (PGPPublicKey encryptionSubkey : encryptionSubkeys) { for (PGPPublicKey encryptionSubkey : encryptionSubkeys) {
SubkeyIdentifier keyId = new SubkeyIdentifier(key, encryptionSubkey.getKeyID()); SubkeyIdentifier keyId = new SubkeyIdentifier(key, encryptionSubkey.getKeyID());
keyRingInfo.put(keyId, info); keyRingInfo.put(keyId, info);
keyViews.put(keyId, new KeyAccessor.ViaUserId(info, keyId, userId)); keyViews.put(keyId, new KeyAccessor.ViaUserId(info, keyId, userId.toString()));
addRecipientKey(key, encryptionSubkey); addRecipientKey(key, encryptionSubkey);
} }
@ -192,7 +194,7 @@ public class EncryptionOptions {
* @param key key ring * @param key key ring
* @return this * @return this
*/ */
public EncryptionOptions addRecipient(PGPPublicKeyRing key) { public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key) {
return addRecipient(key, encryptionKeySelector); return addRecipient(key, encryptionKeySelector);
} }
@ -203,7 +205,8 @@ public class EncryptionOptions {
* @param encryptionKeySelectionStrategy strategy used to select one or multiple encryption subkeys. * @param encryptionKeySelectionStrategy strategy used to select one or multiple encryption subkeys.
* @return this * @return this
*/ */
public EncryptionOptions addRecipient(PGPPublicKeyRing key, EncryptionKeySelector encryptionKeySelectionStrategy) { public EncryptionOptions addRecipient(@Nonnull PGPPublicKeyRing key,
@Nonnull EncryptionKeySelector encryptionKeySelectionStrategy) {
Date evaluationDate = new Date(); Date evaluationDate = new Date();
KeyRingInfo info; KeyRingInfo info;
info = new KeyRingInfo(key, evaluationDate); info = new KeyRingInfo(key, evaluationDate);
@ -234,7 +237,8 @@ public class EncryptionOptions {
return this; return this;
} }
private void addRecipientKey(PGPPublicKeyRing keyRing, PGPPublicKey key) { private void addRecipientKey(@Nonnull PGPPublicKeyRing keyRing,
@Nonnull PGPPublicKey key) {
encryptionKeys.add(new SubkeyIdentifier(keyRing, key.getKeyID())); encryptionKeys.add(new SubkeyIdentifier(keyRing, key.getKeyID()));
PGPKeyEncryptionMethodGenerator encryptionMethod = ImplementationFactory PGPKeyEncryptionMethodGenerator encryptionMethod = ImplementationFactory
.getInstance().getPublicKeyKeyEncryptionMethodGenerator(key); .getInstance().getPublicKeyKeyEncryptionMethodGenerator(key);
@ -247,7 +251,7 @@ public class EncryptionOptions {
* @param passphrase passphrase * @param passphrase passphrase
* @return this * @return this
*/ */
public EncryptionOptions addPassphrase(Passphrase passphrase) { public EncryptionOptions addPassphrase(@Nonnull Passphrase passphrase) {
if (passphrase.isEmpty()) { if (passphrase.isEmpty()) {
throw new IllegalArgumentException("Passphrase must not be empty."); throw new IllegalArgumentException("Passphrase must not be empty.");
} }
@ -267,7 +271,7 @@ public class EncryptionOptions {
* @param encryptionMethod encryption method * @param encryptionMethod encryption method
* @return this * @return this
*/ */
public EncryptionOptions addEncryptionMethod(PGPKeyEncryptionMethodGenerator encryptionMethod) { public EncryptionOptions addEncryptionMethod(@Nonnull PGPKeyEncryptionMethodGenerator encryptionMethod) {
encryptionMethods.add(encryptionMethod); encryptionMethods.add(encryptionMethod);
return this; return this;
} }
@ -303,7 +307,7 @@ public class EncryptionOptions {
* @param encryptionAlgorithm encryption algorithm override * @param encryptionAlgorithm encryption algorithm override
* @return this * @return this
*/ */
public EncryptionOptions overrideEncryptionAlgorithm(SymmetricKeyAlgorithm encryptionAlgorithm) { public EncryptionOptions overrideEncryptionAlgorithm(@Nonnull SymmetricKeyAlgorithm encryptionAlgorithm) {
if (encryptionAlgorithm == SymmetricKeyAlgorithm.NULL) { if (encryptionAlgorithm == SymmetricKeyAlgorithm.NULL) {
throw new IllegalArgumentException("Plaintext encryption can only be used to denote unencrypted secret keys."); throw new IllegalArgumentException("Plaintext encryption can only be used to denote unencrypted secret keys.");
} }
@ -322,7 +326,7 @@ public class EncryptionOptions {
} }
public interface EncryptionKeySelector { public interface EncryptionKeySelector {
List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys); List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys);
} }
/** /**
@ -333,7 +337,7 @@ public class EncryptionOptions {
public static EncryptionKeySelector encryptToFirstSubkey() { public static EncryptionKeySelector encryptToFirstSubkey() {
return new EncryptionKeySelector() { return new EncryptionKeySelector() {
@Override @Override
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) { public List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys) {
return encryptionCapableKeys.isEmpty() ? Collections.emptyList() : Collections.singletonList(encryptionCapableKeys.get(0)); return encryptionCapableKeys.isEmpty() ? Collections.emptyList() : Collections.singletonList(encryptionCapableKeys.get(0));
} }
}; };
@ -347,7 +351,7 @@ public class EncryptionOptions {
public static EncryptionKeySelector encryptToAllCapableSubkeys() { public static EncryptionKeySelector encryptToAllCapableSubkeys() {
return new EncryptionKeySelector() { return new EncryptionKeySelector() {
@Override @Override
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) { public List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys) {
return encryptionCapableKeys; return encryptionCapableKeys;
} }
}; };

View file

@ -36,6 +36,8 @@ import org.pgpainless.key.generation.type.xdh.XDHSpec;
import org.pgpainless.key.util.KeyRingUtils; import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
import javax.annotation.Nonnull;
public class EncryptionOptionsTest { public class EncryptionOptionsTest {
private static PGPSecretKeyRing secretKeys; private static PGPSecretKeyRing secretKeys;
@ -149,7 +151,7 @@ public class EncryptionOptionsTest {
assertThrows(KeyException.UnacceptableEncryptionKeyException.class, assertThrows(KeyException.UnacceptableEncryptionKeyException.class,
() -> options.addRecipient(publicKeys, new EncryptionOptions.EncryptionKeySelector() { () -> options.addRecipient(publicKeys, new EncryptionOptions.EncryptionKeySelector() {
@Override @Override
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) { public List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys) {
return Collections.emptyList(); return Collections.emptyList();
} }
})); }));
@ -157,7 +159,7 @@ public class EncryptionOptionsTest {
assertThrows(KeyException.UnacceptableEncryptionKeyException.class, assertThrows(KeyException.UnacceptableEncryptionKeyException.class,
() -> options.addRecipient(publicKeys, "test@pgpainless.org", new EncryptionOptions.EncryptionKeySelector() { () -> options.addRecipient(publicKeys, "test@pgpainless.org", new EncryptionOptions.EncryptionKeySelector() {
@Override @Override
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) { public List<PGPPublicKey> selectEncryptionSubkeys(@Nonnull List<PGPPublicKey> encryptionCapableKeys) {
return Collections.emptyList(); return Collections.emptyList();
} }
})); }));