From b89d3562ac37edd20202bdd348582159d4084b81 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 31 Jul 2018 20:09:16 +0200 Subject: [PATCH] Add @Nullable, @Nonnull annotations through findbugs --- pgpainless-core/build.gradle | 3 + .../main/java/org/pgpainless/PGPainless.java | 9 +- .../DecryptionBuilder.java | 13 +-- .../DecryptionBuilderInterface.java | 11 ++- .../DecryptionStream.java | 8 +- .../DecryptionStreamFactory.java | 26 +++--- .../MissingPublicKeyCallback.java | 4 +- .../SignatureVerifyingInputStream.java | 9 +- .../encryption_signing/EncryptionBuilder.java | 43 +++++---- .../EncryptionBuilderInterface.java | 39 ++++---- .../encryption_signing/EncryptionStream.java | 46 ++------- .../exception/PublicKeyNotFoundException.java | 38 -------- .../pgpainless/key/OpenPgpV4Fingerprint.java | 20 ++-- .../key/collection/KeyRingCollection.java | 20 ++-- .../pgpainless/key/collection/PGPKeyRing.java | 30 +++--- .../key/generation/KeyRingBuilder.java | 15 +-- .../generation/KeyRingBuilderInterface.java | 11 ++- .../pgpainless/key/generation/KeySpec.java | 11 ++- .../key/generation/KeySpecBuilder.java | 15 +-- .../generation/KeySpecBuilderInterface.java | 12 ++- .../pgpainless/key/generation/type/ECDH.java | 3 +- .../pgpainless/key/generation/type/ECDSA.java | 6 +- .../key/generation/type/ElGamal_ENCRYPT.java | 4 +- .../key/generation/type/ElGamal_GENERAL.java | 5 +- .../key/generation/type/RSA_ENCRYPT.java | 4 +- .../key/generation/type/RSA_GENERAL.java | 5 +- .../key/generation/type/RSA_SIGN.java | 4 +- .../generation/type/curve/EllipticCurve.java | 4 +- .../pgpainless/key/parsing/KeyRingReader.java | 56 +++++++---- .../protection/KeyRingProtectionSettings.java | 11 ++- .../PassphraseMapKeyRingProtector.java | 21 +++-- .../PasswordBasedSecretKeyRingProtector.java | 7 +- .../SecretKeyPassphraseProvider.java | 10 +- .../protection/SecretKeyRingProtector.java | 8 +- .../protection/UnprotectedKeysProtector.java | 5 + .../selection/key/KeySelectionStrategy.java | 3 +- .../key/PublicKeySelectionStrategy.java | 5 +- .../key/SecretKeySelectionStrategy.java | 5 +- .../key/selection/key/impl/And.java | 10 +- .../impl/EncryptionKeySelectionStrategy.java | 4 +- .../key/selection/key/impl/NoRevocation.java | 6 +- .../pgpainless/key/selection/key/impl/Or.java | 10 +- .../impl/SignatureKeySelectionStrategy.java | 4 +- .../selection/key/impl/SignedByMasterKey.java | 3 +- .../PublicKeyRingSelectionStrategy.java | 5 +- .../SecretKeyRingSelectionStrategy.java | 5 +- .../key/selection/keyring/impl/Email.java | 4 +- .../selection/keyring/impl/PartialUserId.java | 5 +- .../SymmetricEncryptorDecryptor.java | 14 +-- .../main/java/org/pgpainless/util/BCUtil.java | 93 ++++++------------- .../org/pgpainless/util/KeyRingSubKeyFix.java | 17 ++-- .../java/org/pgpainless/util/MultiMap.java | 13 +-- .../java/org/pgpainless/util/Passphrase.java | 15 ++- 53 files changed, 387 insertions(+), 370 deletions(-) delete mode 100644 pgpainless-core/src/main/java/org/pgpainless/exception/PublicKeyNotFoundException.java diff --git a/pgpainless-core/build.gradle b/pgpainless-core/build.gradle index 54ef2009..f343d4c4 100644 --- a/pgpainless-core/build.gradle +++ b/pgpainless-core/build.gradle @@ -7,4 +7,7 @@ dependencies { compile 'org.bouncycastle:bcprov-jdk15on:1.60' //*/ compile 'org.bouncycastle:bcpg-jdk15on:1.60' + + // https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305 + compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2' } diff --git a/pgpainless-core/src/main/java/org/pgpainless/PGPainless.java b/pgpainless-core/src/main/java/org/pgpainless/PGPainless.java index 306b3119..a3c4d24c 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/PGPainless.java +++ b/pgpainless-core/src/main/java/org/pgpainless/PGPainless.java @@ -15,6 +15,9 @@ */ package org.pgpainless; +import javax.annotation.Nonnull; +import java.io.IOException; + import org.bouncycastle.openpgp.PGPException; import org.pgpainless.algorithm.CompressionAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm; @@ -27,8 +30,6 @@ import org.pgpainless.key.parsing.KeyRingReader; import org.pgpainless.symmetric_encryption.SymmetricEncryptorDecryptor; import org.pgpainless.util.Passphrase; -import java.io.IOException; - public class PGPainless { /** @@ -73,7 +74,7 @@ public class PGPainless { * @throws IOException IO is dangerous. * @throws PGPException PGP is brittle. */ - public static byte[] encryptWithPassword(byte[] data, Passphrase password, SymmetricKeyAlgorithm algorithm) throws IOException, PGPException { + public static byte[] encryptWithPassword(@Nonnull byte[] data, @Nonnull Passphrase password, @Nonnull SymmetricKeyAlgorithm algorithm) throws IOException, PGPException { return SymmetricEncryptorDecryptor.symmetricallyEncrypt(data, password, algorithm, CompressionAlgorithm.UNCOMPRESSED); } @@ -88,7 +89,7 @@ public class PGPainless { * @throws IOException IO is dangerous. * @throws PGPException PGP is brittle. */ - public static byte[] decryptWithPassword(byte[] data, Passphrase password) throws IOException, PGPException { + public static byte[] decryptWithPassword(@Nonnull byte[] data, @Nonnull Passphrase password) throws IOException, PGPException { return SymmetricEncryptorDecryptor.symmetricallyDecrypt(data, password); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java index 827c2d81..5e9cf9ea 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java @@ -15,6 +15,7 @@ */ package org.pgpainless.decryption_verification; +import javax.annotation.Nonnull; import java.io.IOException; import java.io.InputStream; import java.util.HashSet; @@ -45,7 +46,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface { class DecryptWithImpl implements DecryptWith { @Override - public VerifyWith decryptWith(SecretKeyRingProtector decryptor, PGPSecretKeyRingCollection secretKeyRings) { + public VerifyWith decryptWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRingCollection secretKeyRings) { DecryptionBuilder.this.decryptionKeys = secretKeyRings; DecryptionBuilder.this.decryptionKeyDecryptor = decryptor; return new VerifyWithImpl(); @@ -62,7 +63,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface { class VerifyWithImpl implements VerifyWith { @Override - public HandleMissingPublicKeys verifyWith(PGPPublicKeyRingCollection publicKeyRingCollection) { + public HandleMissingPublicKeys verifyWith(@Nonnull PGPPublicKeyRingCollection publicKeyRingCollection) { Set publicKeyRings = new HashSet<>(); for (Iterator i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) { publicKeyRings.add(i.next()); @@ -71,8 +72,8 @@ public class DecryptionBuilder implements DecryptionBuilderInterface { } @Override - public HandleMissingPublicKeys verifyWith(Set trustedKeyIds, - PGPPublicKeyRingCollection publicKeyRingCollection) { + public HandleMissingPublicKeys verifyWith(@Nonnull Set trustedKeyIds, + @Nonnull PGPPublicKeyRingCollection publicKeyRingCollection) { Set publicKeyRings = new HashSet<>(); for (Iterator i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) { PGPPublicKeyRing p = i.next(); @@ -85,7 +86,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface { } @Override - public HandleMissingPublicKeys verifyWith(Set publicKeyRings) { + public HandleMissingPublicKeys verifyWith(@Nonnull Set publicKeyRings) { DecryptionBuilder.this.verificationKeys = publicKeyRings; return new HandleMissingPublicKeysImpl(); } @@ -100,7 +101,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface { class HandleMissingPublicKeysImpl implements HandleMissingPublicKeys { @Override - public Build handleMissingPublicKeysWith(MissingPublicKeyCallback callback) { + public Build handleMissingPublicKeysWith(@Nonnull MissingPublicKeyCallback callback) { DecryptionBuilder.this.missingPublicKeyCallback = callback; return new BuildImpl(); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilderInterface.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilderInterface.java index 14d3030f..0cd096be 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilderInterface.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilderInterface.java @@ -15,6 +15,7 @@ */ package org.pgpainless.decryption_verification; +import javax.annotation.Nonnull; import java.io.IOException; import java.io.InputStream; import java.util.Set; @@ -32,7 +33,7 @@ public interface DecryptionBuilderInterface { interface DecryptWith { - VerifyWith decryptWith(SecretKeyRingProtector decryptor, PGPSecretKeyRingCollection secretKeyRings); + VerifyWith decryptWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRingCollection secretKeyRings); VerifyWith doNotDecrypt(); @@ -40,11 +41,11 @@ public interface DecryptionBuilderInterface { interface VerifyWith { - HandleMissingPublicKeys verifyWith(PGPPublicKeyRingCollection publicKeyRings); + HandleMissingPublicKeys verifyWith(@Nonnull PGPPublicKeyRingCollection publicKeyRings); - HandleMissingPublicKeys verifyWith(Set trustedFingerprints, PGPPublicKeyRingCollection publicKeyRings); + HandleMissingPublicKeys verifyWith(@Nonnull Set trustedFingerprints, @Nonnull PGPPublicKeyRingCollection publicKeyRings); - HandleMissingPublicKeys verifyWith(Set publicKeyRings); + HandleMissingPublicKeys verifyWith(@Nonnull Set publicKeyRings); Build doNotVerify(); @@ -52,7 +53,7 @@ public interface DecryptionBuilderInterface { interface HandleMissingPublicKeys { - Build handleMissingPublicKeysWith(MissingPublicKeyCallback callback); + Build handleMissingPublicKeysWith(@Nonnull MissingPublicKeyCallback callback); Build ignoreMissingPublicKeys(); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStream.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStream.java index 53eea2d0..2bad3724 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStream.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStream.java @@ -15,6 +15,7 @@ */ package org.pgpainless.decryption_verification; +import javax.annotation.Nonnull; import java.io.IOException; import java.io.InputStream; @@ -24,12 +25,7 @@ public class DecryptionStream extends InputStream { private final OpenPgpMetadata.Builder resultBuilder; private boolean isClosed = false; - DecryptionStream(InputStream wrapped, OpenPgpMetadata.Builder resultBuilder) { - - if (wrapped == null) { - throw new NullPointerException("Wrapped InputStream MUST NOT be null!"); - } - + DecryptionStream(@Nonnull InputStream wrapped, @Nonnull OpenPgpMetadata.Builder resultBuilder) { this.inputStream = wrapped; this.resultBuilder = resultBuilder; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStreamFactory.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStreamFactory.java index 29fe55a6..3c439a43 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStreamFactory.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStreamFactory.java @@ -15,6 +15,8 @@ */ package org.pgpainless.decryption_verification; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.util.Collections; @@ -66,21 +68,21 @@ public final class DecryptionStreamFactory { private final KeyFingerPrintCalculator fingerCalc = new BcKeyFingerprintCalculator(); private final Map verifiableOnePassSignatures = new HashMap<>(); - private DecryptionStreamFactory(PGPSecretKeyRingCollection decryptionKeys, - SecretKeyRingProtector decryptor, - Set verificationKeys, - MissingPublicKeyCallback missingPublicKeyCallback) { + private DecryptionStreamFactory(@Nullable PGPSecretKeyRingCollection decryptionKeys, + @Nullable SecretKeyRingProtector decryptor, + @Nullable Set verificationKeys, + @Nullable MissingPublicKeyCallback missingPublicKeyCallback) { this.decryptionKeys = decryptionKeys; this.decryptionKeyDecryptor = decryptor; this.verificationKeys.addAll(verificationKeys != null ? verificationKeys : Collections.emptyList()); this.missingPublicKeyCallback = missingPublicKeyCallback; } - public static DecryptionStream create(InputStream inputStream, - PGPSecretKeyRingCollection decryptionKeys, - SecretKeyRingProtector decryptor, - Set verificationKeys, - MissingPublicKeyCallback missingPublicKeyCallback) + public static DecryptionStream create(@Nonnull InputStream inputStream, + @Nullable PGPSecretKeyRingCollection decryptionKeys, + @Nullable SecretKeyRingProtector decryptor, + @Nullable Set verificationKeys, + @Nullable MissingPublicKeyCallback missingPublicKeyCallback) throws IOException, PGPException { DecryptionStreamFactory factory = new DecryptionStreamFactory(decryptionKeys, @@ -94,7 +96,7 @@ public final class DecryptionStreamFactory { return new DecryptionStream(factory.wrap(objectFactory), factory.resultBuilder); } - private InputStream wrap(PGPObjectFactory objectFactory) throws IOException, PGPException { + private InputStream wrap(@Nonnull PGPObjectFactory objectFactory) throws IOException, PGPException { Object pgpObj; while ((pgpObj = objectFactory.nextObject()) != null) { @@ -142,7 +144,7 @@ public final class DecryptionStreamFactory { throw new PGPException("No Literal Data Packet found"); } - private InputStream decrypt(PGPEncryptedDataList encryptedDataList) + private InputStream decrypt(@Nonnull PGPEncryptedDataList encryptedDataList) throws PGPException { Iterator iterator = encryptedDataList.getEncryptedDataObjects(); if (!iterator.hasNext()) { @@ -190,7 +192,7 @@ public final class DecryptionStreamFactory { return decryptionStream; } - private void initOnePassSignatures(PGPOnePassSignatureList onePassSignatureList) throws PGPException { + private void initOnePassSignatures(@Nonnull PGPOnePassSignatureList onePassSignatureList) throws PGPException { Iterator iterator = onePassSignatureList.iterator(); if (!iterator.hasNext()) { throw new PGPException("Verification failed - No OnePassSignatures found"); diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/MissingPublicKeyCallback.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/MissingPublicKeyCallback.java index ed52dfa4..9ca3bfa3 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/MissingPublicKeyCallback.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/MissingPublicKeyCallback.java @@ -15,6 +15,8 @@ */ package org.pgpainless.decryption_verification; +import javax.annotation.Nonnull; + import org.bouncycastle.openpgp.PGPPublicKey; public interface MissingPublicKeyCallback { @@ -29,6 +31,6 @@ public interface MissingPublicKeyCallback { * * @return the key or null */ - PGPPublicKey onMissingPublicKeyEncountered(Long keyId); + PGPPublicKey onMissingPublicKeyEncountered(@Nonnull Long keyId); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java index 13077094..312d92fa 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java @@ -15,6 +15,7 @@ */ package org.pgpainless.decryption_verification; +import javax.annotation.Nonnull; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; @@ -41,10 +42,10 @@ public class SignatureVerifyingInputStream extends FilterInputStream { private boolean validated = false; - protected SignatureVerifyingInputStream(InputStream inputStream, - PGPObjectFactory objectFactory, - Map onePassSignatures, - OpenPgpMetadata.Builder resultBuilder) { + protected SignatureVerifyingInputStream(@Nonnull InputStream inputStream, + @Nonnull PGPObjectFactory objectFactory, + @Nonnull Map onePassSignatures, + @Nonnull OpenPgpMetadata.Builder resultBuilder) { super(inputStream); this.objectFactory = objectFactory; this.resultBuilder = resultBuilder; 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 b8502821..ae5665c0 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 @@ -15,6 +15,7 @@ */ package org.pgpainless.encryption_signing; +import javax.annotation.Nonnull; import java.io.IOException; import java.io.OutputStream; import java.util.HashSet; @@ -55,7 +56,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { private boolean asciiArmor = false; @Override - public ToRecipients onOutputStream(OutputStream outputStream) { + public ToRecipients onOutputStream(@Nonnull OutputStream outputStream) { this.outputStream = outputStream; return new ToRecipientsImpl(); } @@ -63,7 +64,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { class ToRecipientsImpl implements ToRecipients { @Override - public WithAlgorithms toRecipients(PGPPublicKey... keys) { + public WithAlgorithms toRecipients(@Nonnull PGPPublicKey... keys) { for (PGPPublicKey k : keys) { if (encryptionKeySelector().accept(null, k)) { EncryptionBuilder.this.encryptionKeys.add(k); @@ -80,7 +81,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public WithAlgorithms toRecipients(PGPPublicKeyRing... keys) { + public WithAlgorithms toRecipients(@Nonnull PGPPublicKeyRing... keys) { for (PGPPublicKeyRing ring : keys) { for (PGPPublicKey k : ring) { if (encryptionKeySelector().accept(null, k)) { @@ -97,7 +98,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public WithAlgorithms toRecipients(PGPPublicKeyRingCollection... keys) { + public WithAlgorithms toRecipients(@Nonnull PGPPublicKeyRingCollection... keys) { for (PGPPublicKeyRingCollection collection : keys) { for (PGPPublicKeyRing ring : collection) { for (PGPPublicKey k : ring) { @@ -116,8 +117,8 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public WithAlgorithms toRecipients(PublicKeyRingSelectionStrategy ringSelectionStrategy, - MultiMap keys) { + public WithAlgorithms toRecipients(@Nonnull PublicKeyRingSelectionStrategy ringSelectionStrategy, + @Nonnull MultiMap keys) { if (keys.isEmpty()) { throw new IllegalArgumentException("Recipient map MUST NOT be empty."); } @@ -149,7 +150,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { class WithAlgorithmsImpl implements WithAlgorithms { @Override - public WithAlgorithms andToSelf(PGPPublicKey... keys) { + public WithAlgorithms andToSelf(@Nonnull PGPPublicKey... keys) { if (keys.length == 0) { throw new IllegalArgumentException("Recipient list MUST NOT be empty."); } @@ -164,7 +165,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public WithAlgorithms andToSelf(PGPPublicKeyRing... keys) { + public WithAlgorithms andToSelf(@Nonnull PGPPublicKeyRing... keys) { if (keys.length == 0) { throw new IllegalArgumentException("Recipient list MUST NOT be empty."); } @@ -180,7 +181,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public WithAlgorithms andToSelf(PGPPublicKeyRingCollection keys) { + public WithAlgorithms andToSelf(@Nonnull PGPPublicKeyRingCollection keys) { for (PGPPublicKeyRing ring : keys) { for (Iterator i = ring.getPublicKeys(); i.hasNext(); ) { PGPPublicKey key = i.next(); @@ -192,8 +193,8 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { return this; } - public WithAlgorithms andToSelf(PublicKeyRingSelectionStrategy ringSelectionStrategy, - MultiMap keys) { + public WithAlgorithms andToSelf(@Nonnull PublicKeyRingSelectionStrategy ringSelectionStrategy, + @Nonnull MultiMap keys) { if (keys.isEmpty()) { throw new IllegalArgumentException("Recipient list MUST NOT be empty."); } @@ -214,9 +215,9 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public SignWith usingAlgorithms(SymmetricKeyAlgorithm symmetricKeyAlgorithm, - HashAlgorithm hashAlgorithm, - CompressionAlgorithm compressionAlgorithm) { + public SignWith usingAlgorithms(@Nonnull SymmetricKeyAlgorithm symmetricKeyAlgorithm, + @Nonnull HashAlgorithm hashAlgorithm, + @Nonnull CompressionAlgorithm compressionAlgorithm) { EncryptionBuilder.this.symmetricKeyAlgorithm = symmetricKeyAlgorithm; EncryptionBuilder.this.hashAlgorithm = hashAlgorithm; @@ -238,7 +239,8 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { class SignWithImpl implements SignWith { @Override - public Armor signWith(SecretKeyRingProtector decryptor, PGPSecretKey... keys) { + public Armor signWith(@Nonnull SecretKeyRingProtector decryptor, + @Nonnull PGPSecretKey... keys) { if (keys.length == 0) { throw new IllegalArgumentException("Recipient list MUST NOT be empty."); } @@ -254,7 +256,8 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public Armor signWith(SecretKeyRingProtector decryptor, PGPSecretKeyRing... keys) { + public Armor signWith(@Nonnull SecretKeyRingProtector decryptor, + @Nonnull PGPSecretKeyRing... keys) { if (keys.length == 0) { throw new IllegalArgumentException("Recipient list MUST NOT be empty."); } @@ -271,9 +274,9 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { } @Override - public Armor signWith(SecretKeyRingSelectionStrategy ringSelectionStrategy, - SecretKeyRingProtector decryptor, - MultiMap keys) { + public Armor signWith(@Nonnull SecretKeyRingSelectionStrategy ringSelectionStrategy, + @Nonnull SecretKeyRingProtector decryptor, + @Nonnull MultiMap keys) { if (keys.isEmpty()) { throw new IllegalArgumentException("Recipient list MUST NOT be empty."); } @@ -320,7 +323,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { privateKeys.add(secretKey.extractPrivateKey(signingKeysDecryptor.getDecryptor(secretKey.getKeyID()))); } - return EncryptionStream.create( + return new EncryptionStream( EncryptionBuilder.this.outputStream, EncryptionBuilder.this.encryptionKeys, privateKeys, diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilderInterface.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilderInterface.java index c6cff4b0..e0f79a36 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilderInterface.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionBuilderInterface.java @@ -15,6 +15,7 @@ */ package org.pgpainless.encryption_signing; +import javax.annotation.Nonnull; import java.io.IOException; import java.io.OutputStream; @@ -36,18 +37,18 @@ import org.pgpainless.util.MultiMap; public interface EncryptionBuilderInterface { - ToRecipients onOutputStream(OutputStream outputStream); + ToRecipients onOutputStream(@Nonnull OutputStream outputStream); interface ToRecipients { - WithAlgorithms toRecipients(PGPPublicKey... keys); + WithAlgorithms toRecipients(@Nonnull PGPPublicKey... keys); - WithAlgorithms toRecipients(PGPPublicKeyRing... keys); + WithAlgorithms toRecipients(@Nonnull PGPPublicKeyRing... keys); - WithAlgorithms toRecipients(PGPPublicKeyRingCollection... keys); + WithAlgorithms toRecipients(@Nonnull PGPPublicKeyRingCollection... keys); - WithAlgorithms toRecipients(PublicKeyRingSelectionStrategy selectionStrategy, - MultiMap keys); + WithAlgorithms toRecipients(@Nonnull PublicKeyRingSelectionStrategy selectionStrategy, + @Nonnull MultiMap keys); SignWith doNotEncrypt(); @@ -55,18 +56,18 @@ public interface EncryptionBuilderInterface { interface WithAlgorithms { - WithAlgorithms andToSelf(PGPPublicKey... keys); + WithAlgorithms andToSelf(@Nonnull PGPPublicKey... keys); - WithAlgorithms andToSelf(PGPPublicKeyRing... keys); + WithAlgorithms andToSelf(@Nonnull PGPPublicKeyRing... keys); - WithAlgorithms andToSelf(PGPPublicKeyRingCollection keys); + WithAlgorithms andToSelf(@Nonnull PGPPublicKeyRingCollection keys); - WithAlgorithms andToSelf(PublicKeyRingSelectionStrategy selectionStrategy, - MultiMap keys); + WithAlgorithms andToSelf(@Nonnull PublicKeyRingSelectionStrategy selectionStrategy, + @Nonnull MultiMap keys); - SignWith usingAlgorithms(SymmetricKeyAlgorithm symmetricKeyAlgorithm, - HashAlgorithm hashAlgorithm, - CompressionAlgorithm compressionAlgorithm); + SignWith usingAlgorithms(@Nonnull SymmetricKeyAlgorithm symmetricKeyAlgorithm, + @Nonnull HashAlgorithm hashAlgorithm, + @Nonnull CompressionAlgorithm compressionAlgorithm); SignWith usingSecureAlgorithms(); @@ -74,13 +75,13 @@ public interface EncryptionBuilderInterface { interface SignWith { - Armor signWith(SecretKeyRingProtector decryptor, PGPSecretKey... keys); + Armor signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKey... keys); - Armor signWith(SecretKeyRingProtector decryptor, PGPSecretKeyRing... keyRings); + Armor signWith(@Nonnull SecretKeyRingProtector decryptor, @Nonnull PGPSecretKeyRing... keyRings); - Armor signWith(SecretKeyRingSelectionStrategy selectionStrategy, - SecretKeyRingProtector decryptor, - MultiMap keys) + Armor signWith(@Nonnull SecretKeyRingSelectionStrategy selectionStrategy, + @Nonnull SecretKeyRingProtector decryptor, + @Nonnull MultiMap keys) throws SecretKeyNotFoundException; Armor doNotSign(); diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java index 9ac947f5..d2c7b1d1 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java @@ -15,6 +15,7 @@ */ package org.pgpainless.encryption_signing; +import javax.annotation.Nonnull; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; @@ -75,13 +76,13 @@ public final class EncryptionStream extends OutputStream { private PGPLiteralDataGenerator literalDataGenerator; private OutputStream literalDataStream; - private EncryptionStream(OutputStream targetOutputStream, - Set encryptionKeys, - Set signingKeys, - SymmetricKeyAlgorithm symmetricKeyAlgorithm, - HashAlgorithm hashAlgorithm, - CompressionAlgorithm compressionAlgorithm, - boolean asciiArmor) + EncryptionStream(@Nonnull OutputStream targetOutputStream, + @Nonnull Set encryptionKeys, + @Nonnull Set signingKeys, + @Nonnull SymmetricKeyAlgorithm symmetricKeyAlgorithm, + @Nonnull HashAlgorithm hashAlgorithm, + @Nonnull CompressionAlgorithm compressionAlgorithm, + boolean asciiArmor) throws IOException, PGPException { // Currently outermost Stream @@ -163,31 +164,6 @@ public final class EncryptionStream extends OutputStream { signingKeyIds, Collections.emptySet()); } - static EncryptionStream create(OutputStream outputStream, - Set encryptionKeys, - Set signingKeys, - SymmetricKeyAlgorithm symmetricKeyAlgorithm, - HashAlgorithm hashAlgorithm, - CompressionAlgorithm compressionAlgorithm, - boolean asciiArmor) - throws IOException, PGPException { - - requireNonNull(outputStream, "targetOutputStream"); - requireNonNull(encryptionKeys, "encryptionKeys"); - requireNonNull(signingKeys, "signingKeys"); - requireNonNull(symmetricKeyAlgorithm, "symmetricKeyAlgorithm"); - requireNonNull(hashAlgorithm, "hashAlgorithm"); - requireNonNull(compressionAlgorithm, "compressionAlgorithm"); - - return new EncryptionStream(outputStream, - encryptionKeys, - signingKeys, - symmetricKeyAlgorithm, - hashAlgorithm, - compressionAlgorithm, - asciiArmor); - } - @Override public void write(int data) throws IOException { literalDataStream.write(data); @@ -253,12 +229,6 @@ public final class EncryptionStream extends OutputStream { } } - private static void requireNonNull(Object o, String name) { - if (o == null) { - throw new IllegalArgumentException("Argument '" + name + "' MUST NOT be null."); - } - } - public OpenPgpMetadata getResult() { return result; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/exception/PublicKeyNotFoundException.java b/pgpainless-core/src/main/java/org/pgpainless/exception/PublicKeyNotFoundException.java deleted file mode 100644 index 469f7596..00000000 --- a/pgpainless-core/src/main/java/org/pgpainless/exception/PublicKeyNotFoundException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2018 Paul Schaub. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.pgpainless.exception; - -import org.bouncycastle.openpgp.PGPException; - -public class PublicKeyNotFoundException extends Exception { - - private static final long serialVersionUID = 1L; - - private long keyId; - - public PublicKeyNotFoundException(long keyId) { - super("No PGPPublicKey with id " + Long.toHexString(keyId) + " (" + keyId + ") found."); - this.keyId = keyId; - } - - public PublicKeyNotFoundException(PGPException e) { - - } - - public long getKeyId() { - return keyId; - } -} diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/OpenPgpV4Fingerprint.java b/pgpainless-core/src/main/java/org/pgpainless/key/OpenPgpV4Fingerprint.java index 92984793..f051d086 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/OpenPgpV4Fingerprint.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/OpenPgpV4Fingerprint.java @@ -15,6 +15,7 @@ */ package org.pgpainless.key; +import javax.annotation.Nonnull; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.charset.Charset; @@ -39,10 +40,7 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable * @param fingerprint hexadecimal representation of the fingerprint. */ - public OpenPgpV4Fingerprint(String fingerprint) { - if (fingerprint == null) { - throw new NullPointerException("Fingerprint MUST NOT be null."); - } + public OpenPgpV4Fingerprint(@Nonnull String fingerprint) { String fp = fingerprint.trim().toUpperCase(); if (!isValid(fp)) { throw new IllegalArgumentException("Fingerprint " + fingerprint + @@ -51,26 +49,26 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable 1) { + throw new IllegalArgumentException("s2kCount cannot be less than 1."); + } this.s2kCount = s2kCount; } - public SymmetricKeyAlgorithm getEncryptionAlgorithm() { + public @Nonnull SymmetricKeyAlgorithm getEncryptionAlgorithm() { return encryptionAlgorithm; } - public HashAlgorithm getHashAlgorithm() { + public @Nonnull HashAlgorithm getHashAlgorithm() { return hashAlgorithm; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/protection/PassphraseMapKeyRingProtector.java b/pgpainless-core/src/main/java/org/pgpainless/key/protection/PassphraseMapKeyRingProtector.java index b4059dd5..481f30c4 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/protection/PassphraseMapKeyRingProtector.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/protection/PassphraseMapKeyRingProtector.java @@ -15,6 +15,8 @@ */ package org.pgpainless.key.protection; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; @@ -34,9 +36,9 @@ public class PassphraseMapKeyRingProtector implements SecretKeyRingProtector, Se private final SecretKeyRingProtector protector; private final SecretKeyPassphraseProvider provider; - public PassphraseMapKeyRingProtector(Map passphrases, - KeyRingProtectionSettings protectionSettings, - SecretKeyPassphraseProvider missingPassphraseCallback) { + public PassphraseMapKeyRingProtector(@Nonnull Map passphrases, + @Nonnull KeyRingProtectionSettings protectionSettings, + @Nullable SecretKeyPassphraseProvider missingPassphraseCallback) { this.cache.putAll(passphrases); this.protector = new PasswordBasedSecretKeyRingProtector(protectionSettings, this); this.provider = missingPassphraseCallback; @@ -48,7 +50,7 @@ public class PassphraseMapKeyRingProtector implements SecretKeyRingProtector, Se * @param keyId id of the key * @param passphrase passphrase */ - public void addPassphrase(Long keyId, Passphrase passphrase) { + public void addPassphrase(@Nonnull Long keyId, @Nullable Passphrase passphrase) { this.cache.put(keyId, passphrase); } @@ -58,14 +60,15 @@ public class PassphraseMapKeyRingProtector implements SecretKeyRingProtector, Se * * @param keyId id of the key */ - public void forgetPassphrase(Long keyId) { + public void forgetPassphrase(@Nonnull Long keyId) { Passphrase passphrase = cache.get(keyId); passphrase.clear(); cache.remove(keyId); } @Override - public Passphrase getPassphraseFor(Long keyId) { + @Nullable + public Passphrase getPassphraseFor(@Nonnull Long keyId) { Passphrase passphrase = cache.get(keyId); if (passphrase == null || !passphrase.isValid()) { passphrase = provider.getPassphraseFor(keyId); @@ -77,12 +80,14 @@ public class PassphraseMapKeyRingProtector implements SecretKeyRingProtector, Se } @Override - public PBESecretKeyDecryptor getDecryptor(Long keyId) { + @Nullable + public PBESecretKeyDecryptor getDecryptor(@Nonnull Long keyId) { return protector.getDecryptor(keyId); } @Override - public PBESecretKeyEncryptor getEncryptor(Long keyId) throws PGPException { + @Nullable + public PBESecretKeyEncryptor getEncryptor(@Nonnull Long keyId) throws PGPException { return protector.getEncryptor(keyId); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/protection/PasswordBasedSecretKeyRingProtector.java b/pgpainless-core/src/main/java/org/pgpainless/key/protection/PasswordBasedSecretKeyRingProtector.java index f28c6e16..4fbc2145 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/protection/PasswordBasedSecretKeyRingProtector.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/protection/PasswordBasedSecretKeyRingProtector.java @@ -15,6 +15,9 @@ */ package org.pgpainless.key.protection; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; @@ -43,12 +46,13 @@ public class PasswordBasedSecretKeyRingProtector implements SecretKeyRingProtect * @param settings S2K settings etc. * @param passphraseProvider provider which provides passphrases. */ - public PasswordBasedSecretKeyRingProtector(KeyRingProtectionSettings settings, SecretKeyPassphraseProvider passphraseProvider) { + public PasswordBasedSecretKeyRingProtector(@Nonnull KeyRingProtectionSettings settings, @Nonnull SecretKeyPassphraseProvider passphraseProvider) { this.protectionSettings = settings; this.passphraseProvider = passphraseProvider; } @Override + @Nullable public PBESecretKeyDecryptor getDecryptor(Long keyId) { Passphrase passphrase = passphraseProvider.getPassphraseFor(keyId); return new BcPBESecretKeyDecryptorBuilder(calculatorProvider) @@ -56,6 +60,7 @@ public class PasswordBasedSecretKeyRingProtector implements SecretKeyRingProtect } @Override + @Nullable public PBESecretKeyEncryptor getEncryptor(Long keyId) throws PGPException { Passphrase passphrase = passphraseProvider.getPassphraseFor(keyId); return new BcPBESecretKeyEncryptorBuilder( diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/protection/SecretKeyPassphraseProvider.java b/pgpainless-core/src/main/java/org/pgpainless/key/protection/SecretKeyPassphraseProvider.java index 1577a325..4c878024 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/protection/SecretKeyPassphraseProvider.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/protection/SecretKeyPassphraseProvider.java @@ -15,6 +15,8 @@ */ package org.pgpainless.key.protection; +import javax.annotation.Nullable; + import org.pgpainless.util.Passphrase; /** @@ -23,10 +25,12 @@ import org.pgpainless.util.Passphrase; public interface SecretKeyPassphraseProvider { /** - * Return a passphrase for the given key. + * Return a passphrase for the given key. If no record has been found, return null. + * Note: In case of an unprotected secret key, this method must may not return null, but a {@link Passphrase} with + * a content of null. * * @param keyId id of the key - * @return passphrase + * @return passphrase or null, if no passphrase record has been found. */ - Passphrase getPassphraseFor(Long keyId); + @Nullable Passphrase getPassphraseFor(Long keyId); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/protection/SecretKeyRingProtector.java b/pgpainless-core/src/main/java/org/pgpainless/key/protection/SecretKeyRingProtector.java index 6f0d7ae9..83672913 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/protection/SecretKeyRingProtector.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/protection/SecretKeyRingProtector.java @@ -15,6 +15,8 @@ */ package org.pgpainless.key.protection; +import javax.annotation.Nullable; + import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; @@ -23,19 +25,21 @@ public interface SecretKeyRingProtector { /** * Return a decryptor for the key of id {@code keyId}. + * This method returns null if the key is unprotected. * * @param keyId id of the key * @return decryptor for the key */ - PBESecretKeyDecryptor getDecryptor(Long keyId); + @Nullable PBESecretKeyDecryptor getDecryptor(Long keyId); /** * Return an encryptor for the key of id {@code keyId}. + * This method returns null if the key is unprotected. * * @param keyId id of the key * @return encryptor for the key * @throws PGPException if the encryptor cannot be created for some reason */ - PBESecretKeyEncryptor getEncryptor(Long keyId) throws PGPException; + @Nullable PBESecretKeyEncryptor getEncryptor(Long keyId) throws PGPException; } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/protection/UnprotectedKeysProtector.java b/pgpainless-core/src/main/java/org/pgpainless/key/protection/UnprotectedKeysProtector.java index 304eb004..fdac82da 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/protection/UnprotectedKeysProtector.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/protection/UnprotectedKeysProtector.java @@ -15,6 +15,8 @@ */ package org.pgpainless.key.protection; +import javax.annotation.Nullable; + import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; @@ -22,12 +24,15 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor; * Implementation of the {@link SecretKeyRingProtector} which assumes that all handled keys are not password protected. */ public class UnprotectedKeysProtector implements SecretKeyRingProtector { + @Override + @Nullable public PBESecretKeyDecryptor getDecryptor(Long keyId) { return null; } @Override + @Nullable public PBESecretKeyEncryptor getEncryptor(Long keyId) { return null; } 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 0674c9d7..d71fd049 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,6 +15,7 @@ */ package org.pgpainless.key.selection.key; +import javax.annotation.Nonnull; import java.util.Set; import org.pgpainless.util.MultiMap; @@ -30,7 +31,7 @@ public interface KeySelectionStrategy { boolean accept(O identifier, K key); - Set selectKeysFromKeyRing(O identifier, R ring); + Set selectKeysFromKeyRing(O identifier, @Nonnull R ring); MultiMap selectKeysFromKeyRings(MultiMap rings); 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 13d8376d..76af04f3 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,6 +15,7 @@ */ package org.pgpainless.key.selection.key; +import javax.annotation.Nonnull; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -32,7 +33,7 @@ import org.pgpainless.util.MultiMap; public abstract class PublicKeySelectionStrategy implements KeySelectionStrategy { @Override - public Set selectKeysFromKeyRing(O identifier, PGPPublicKeyRing ring) { + public Set selectKeysFromKeyRing(O identifier, @Nonnull PGPPublicKeyRing ring) { Set keys = new HashSet<>(); for (Iterator i = ring.getPublicKeys(); i.hasNext(); ) { PGPPublicKey key = i.next(); @@ -42,7 +43,7 @@ public abstract class PublicKeySelectionStrategy implements KeySelectionStrat } @Override - public MultiMap selectKeysFromKeyRings(MultiMap keyRings) { + public MultiMap selectKeysFromKeyRings(@Nonnull MultiMap keyRings) { MultiMap keys = new MultiMap<>(); for (O identifier : keyRings.keySet()) { for (PGPPublicKeyRing ring : keyRings.get(identifier)) { 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 e034d907..5f569862 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,6 +15,7 @@ */ package org.pgpainless.key.selection.key; +import javax.annotation.Nonnull; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -32,7 +33,7 @@ import org.pgpainless.util.MultiMap; public abstract class SecretKeySelectionStrategy implements KeySelectionStrategy { @Override - public Set selectKeysFromKeyRing(O identifier, PGPSecretKeyRing ring) { + public Set selectKeysFromKeyRing(O identifier, @Nonnull PGPSecretKeyRing ring) { Set keys = new HashSet<>(); for (Iterator i = ring.getSecretKeys(); i.hasNext(); ) { PGPSecretKey key = i.next(); @@ -42,7 +43,7 @@ public abstract class SecretKeySelectionStrategy implements KeySelectionStrat } @Override - public MultiMap selectKeysFromKeyRings(MultiMap keyRings) { + public MultiMap selectKeysFromKeyRings(@Nonnull MultiMap keyRings) { MultiMap keys = new MultiMap<>(); for (O identifier : keyRings.keySet()) { for (PGPSecretKeyRing ring : keyRings.get(identifier)) { diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/And.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/And.java index 231f8400..662deaa3 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/And.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/And.java @@ -15,6 +15,8 @@ */ package org.pgpainless.key.selection.key.impl; +import javax.annotation.Nonnull; + import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPSecretKey; import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; @@ -27,8 +29,8 @@ public class And { private final PublicKeySelectionStrategy left; private final PublicKeySelectionStrategy right; - public PubKeySelectionStrategy(PublicKeySelectionStrategy left, - PublicKeySelectionStrategy right) { + public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy left, + @Nonnull PublicKeySelectionStrategy right) { this.left = left; this.right = right; } @@ -44,8 +46,8 @@ public class And { private final SecretKeySelectionStrategy left; private final SecretKeySelectionStrategy right; - public SecKeySelectionStrategy(SecretKeySelectionStrategy left, - SecretKeySelectionStrategy right) { + public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy left, + @Nonnull SecretKeySelectionStrategy right) { this.left = left; this.right = right; } 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 c4277a2e..876edd20 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 @@ -15,6 +15,8 @@ */ package org.pgpainless.key.selection.key.impl; +import javax.annotation.Nonnull; + import org.bouncycastle.openpgp.PGPPublicKey; import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; @@ -26,7 +28,7 @@ import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; public class EncryptionKeySelectionStrategy extends PublicKeySelectionStrategy { @Override - public boolean accept(O identifier, PGPPublicKey key) { + public boolean accept(O identifier, @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 58c2e26d..c7e882ca 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 @@ -15,6 +15,8 @@ */ package org.pgpainless.key.selection.key.impl; +import javax.annotation.Nonnull; + import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPSecretKey; import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; @@ -33,7 +35,7 @@ public class NoRevocation { public static class PubKeySelectionStrategy extends PublicKeySelectionStrategy { @Override - public boolean accept(O identifier, PGPPublicKey key) { + public boolean accept(O identifier, @Nonnull PGPPublicKey key) { return !key.hasRevocation(); } } @@ -46,7 +48,7 @@ public class NoRevocation { public static class SecKeySelectionStrategy extends SecretKeySelectionStrategy { @Override - public boolean accept(O identifier, PGPSecretKey key) { + public boolean accept(O identifier, @Nonnull PGPSecretKey key) { return !key.getPublicKey().hasRevocation(); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/Or.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/Or.java index 32f110ce..55e263e3 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/Or.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/key/impl/Or.java @@ -15,6 +15,8 @@ */ package org.pgpainless.key.selection.key.impl; +import javax.annotation.Nonnull; + import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPSecretKey; import org.pgpainless.key.selection.key.PublicKeySelectionStrategy; @@ -27,8 +29,8 @@ public class Or { private final PublicKeySelectionStrategy left; private final PublicKeySelectionStrategy right; - public PubKeySelectionStrategy(PublicKeySelectionStrategy left, - PublicKeySelectionStrategy right) { + public PubKeySelectionStrategy(@Nonnull PublicKeySelectionStrategy left, + @Nonnull PublicKeySelectionStrategy right) { this.left = left; this.right = right; } @@ -44,8 +46,8 @@ public class Or { private final SecretKeySelectionStrategy left; private final SecretKeySelectionStrategy right; - public SecKeySelectionStrategy(SecretKeySelectionStrategy left, - SecretKeySelectionStrategy right) { + public SecKeySelectionStrategy(@Nonnull SecretKeySelectionStrategy left, + @Nonnull SecretKeySelectionStrategy right) { this.left = left; this.right = right; } 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 a68dfe68..51275e4f 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 @@ -15,6 +15,8 @@ */ package org.pgpainless.key.selection.key.impl; +import javax.annotation.Nonnull; + import org.bouncycastle.openpgp.PGPSecretKey; import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; @@ -26,7 +28,7 @@ import org.pgpainless.key.selection.key.SecretKeySelectionStrategy; public class SignatureKeySelectionStrategy extends SecretKeySelectionStrategy { @Override - public boolean accept(O identifier, PGPSecretKey key) { + public boolean accept(O identifier, @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 d20a0a04..e3e6a134 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 @@ -15,6 +15,7 @@ */ package org.pgpainless.key.selection.key.impl; +import javax.annotation.Nonnull; import java.util.Arrays; import java.util.Iterator; import java.util.logging.Level; @@ -33,7 +34,7 @@ public class SignedByMasterKey { public static class PubkeySelectionStrategy extends PublicKeySelectionStrategy { @Override - public boolean accept(PGPPublicKey masterKey, PGPPublicKey key) { + public boolean accept(PGPPublicKey masterKey, @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/keyring/PublicKeyRingSelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/PublicKeyRingSelectionStrategy.java index 427ec042..75ac536f 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/PublicKeyRingSelectionStrategy.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/PublicKeyRingSelectionStrategy.java @@ -15,6 +15,7 @@ */ package org.pgpainless.key.selection.keyring; +import javax.annotation.Nonnull; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -26,7 +27,7 @@ import org.pgpainless.util.MultiMap; public abstract class PublicKeyRingSelectionStrategy implements KeyRingSelectionStrategy { @Override - public Set selectKeyRingsFromCollection(O identifier, PGPPublicKeyRingCollection keyRingCollection) { + public Set selectKeyRingsFromCollection(@Nonnull O identifier, @Nonnull PGPPublicKeyRingCollection keyRingCollection) { Set accepted = new HashSet<>(); for (Iterator i = keyRingCollection.getKeyRings(); i.hasNext(); ) { PGPPublicKeyRing ring = i.next(); @@ -36,7 +37,7 @@ public abstract class PublicKeyRingSelectionStrategy implements KeyRingSelect } @Override - public MultiMap selectKeyRingsFromCollections(MultiMap keyRingCollections) { + public MultiMap selectKeyRingsFromCollections(@Nonnull MultiMap keyRingCollections) { MultiMap keyRings = new MultiMap<>(); for (O identifier : keyRingCollections.keySet()) { for (PGPPublicKeyRingCollection collection : keyRingCollections.get(identifier)) { diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/SecretKeyRingSelectionStrategy.java b/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/SecretKeyRingSelectionStrategy.java index 20c9dd58..51475652 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/SecretKeyRingSelectionStrategy.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/selection/keyring/SecretKeyRingSelectionStrategy.java @@ -15,6 +15,7 @@ */ package org.pgpainless.key.selection.keyring; +import javax.annotation.Nonnull; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -25,7 +26,7 @@ import org.pgpainless.util.MultiMap; public abstract class SecretKeyRingSelectionStrategy implements KeyRingSelectionStrategy { @Override - public Set selectKeyRingsFromCollection(O identifier, PGPSecretKeyRingCollection keyRingCollection) { + public Set selectKeyRingsFromCollection(O identifier, @Nonnull PGPSecretKeyRingCollection keyRingCollection) { Set accepted = new HashSet<>(); for (Iterator i = keyRingCollection.getKeyRings(); i.hasNext(); ) { PGPSecretKeyRing ring = i.next(); @@ -35,7 +36,7 @@ public abstract class SecretKeyRingSelectionStrategy implements KeyRingSelect } @Override - public MultiMap selectKeyRingsFromCollections(MultiMap keyRingCollections) { + public MultiMap selectKeyRingsFromCollections(@Nonnull MultiMap keyRingCollections) { MultiMap keyRings = new MultiMap<>(); for (O identifier : keyRingCollections.keySet()) { for (PGPSecretKeyRingCollection collection : keyRingCollections.get(identifier)) { 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 968980d2..2a33df1b 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,6 +15,8 @@ */ package org.pgpainless.key.selection.keyring.impl; +import javax.annotation.Nonnull; + import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPSecretKey; @@ -23,7 +25,7 @@ public class Email { public static class PubRingSelectionStrategy extends PartialUserId.PubRingSelectionStrategy { @Override - public boolean accept(String email, PGPPublicKey key) { + public boolean accept(@Nonnull String email, @Nonnull PGPPublicKey key) { // Ensure, that email address is encapsulated in "<",">" if (!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 6e733cc1..9e67a8dd 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 @@ -15,6 +15,7 @@ */ package org.pgpainless.key.selection.keyring.impl; +import javax.annotation.Nonnull; import java.util.Iterator; import org.bouncycastle.openpgp.PGPPublicKey; @@ -27,7 +28,7 @@ public class PartialUserId { public static class PubRingSelectionStrategy extends PublicKeySelectionStrategy { @Override - public boolean accept(String identifier, PGPPublicKey key) { + public boolean accept(String identifier, @Nonnull PGPPublicKey key) { for (Iterator userIds = key.getUserIDs(); userIds.hasNext(); ) { String userId = userIds.next(); if (userId.contains(identifier)) { @@ -41,7 +42,7 @@ public class PartialUserId { public static class SecRingSelectionStrategy extends SecretKeySelectionStrategy { @Override - public boolean accept(String identifier, PGPSecretKey key) { + public boolean accept(String identifier, @Nonnull PGPSecretKey key) { for (Iterator userIds = key.getUserIDs(); userIds.hasNext(); ) { String userId = (String) userIds.next(); if (userId.contains(identifier)) { diff --git a/pgpainless-core/src/main/java/org/pgpainless/symmetric_encryption/SymmetricEncryptorDecryptor.java b/pgpainless-core/src/main/java/org/pgpainless/symmetric_encryption/SymmetricEncryptorDecryptor.java index 9e70ec36..87ec4404 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/symmetric_encryption/SymmetricEncryptorDecryptor.java +++ b/pgpainless-core/src/main/java/org/pgpainless/symmetric_encryption/SymmetricEncryptorDecryptor.java @@ -15,6 +15,7 @@ */ package org.pgpainless.symmetric_encryption; +import javax.annotation.Nonnull; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -62,10 +63,10 @@ public class SymmetricEncryptorDecryptor { * @throws IOException IO is dangerous * @throws PGPException OpenPGP is brittle */ - public static byte[] symmetricallyEncrypt(byte[] data, - Passphrase password, - SymmetricKeyAlgorithm encryptionAlgorithm, - CompressionAlgorithm compressionAlgorithm) + public static byte[] symmetricallyEncrypt(@Nonnull byte[] data, + @Nonnull Passphrase password, + @Nonnull SymmetricKeyAlgorithm encryptionAlgorithm, + @Nonnull CompressionAlgorithm compressionAlgorithm) throws IOException, PGPException { byte[] compressedData = compress(data, compressionAlgorithm.getAlgorithmId()); @@ -99,7 +100,8 @@ public class SymmetricEncryptorDecryptor { * @throws IOException IO is dangerous * @throws PGPException OpenPGP is brittle */ - public static byte[] symmetricallyDecrypt(byte[] data, Passphrase password) throws IOException, PGPException { + public static byte[] symmetricallyDecrypt(@Nonnull byte[] data, @Nonnull Passphrase password) + throws IOException, PGPException { InputStream in = new BufferedInputStream(new ByteArrayInputStream(data)); in = PGPUtil.getDecoderStream(in); @@ -156,7 +158,7 @@ public class SymmetricEncryptorDecryptor { * @return compressed data * @throws IOException IO is dangerous */ - private static byte[] compress(byte[] clearData, int algorithm) throws IOException { + private static byte[] compress(@Nonnull byte[] clearData, int algorithm) throws IOException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(algorithm); OutputStream cos = comData.open(bOut); 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 8434b1ab..a022bfcc 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/BCUtil.java @@ -15,6 +15,7 @@ */ package org.pgpainless.util; +import javax.annotation.Nonnull; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -52,17 +53,17 @@ public class BCUtil { /* PGPXxxKeyRing -> PGPXxxKeyRingCollection */ - public static PGPPublicKeyRingCollection keyRingsToKeyRingCollection(PGPPublicKeyRing... rings) + public static PGPPublicKeyRingCollection keyRingsToKeyRingCollection(@Nonnull PGPPublicKeyRing... rings) throws IOException, PGPException { return new PGPPublicKeyRingCollection(Arrays.asList(rings)); } - public static PGPSecretKeyRingCollection keyRingsToKeyRingCollection(PGPSecretKeyRing... rings) + public static PGPSecretKeyRingCollection keyRingsToKeyRingCollection(@Nonnull PGPSecretKeyRing... rings) throws IOException, PGPException { return new PGPSecretKeyRingCollection(Arrays.asList(rings)); } - public static PGPPublicKeyRing publicKeyRingFromSecretKeyRing(PGPSecretKeyRing secretKeys) + public static PGPPublicKeyRing publicKeyRingFromSecretKeyRing(@Nonnull PGPSecretKeyRing secretKeys) throws PGPException, IOException { PGPSecretKeyRing fixedSecretKeys = KeyRingSubKeyFix.repairSubkeyPackets(secretKeys, null, null); @@ -81,7 +82,8 @@ public class BCUtil { PGPXxxKeyRingCollection -> PGPXxxKeyRing */ - public static PGPSecretKeyRing getKeyRingFromCollection(PGPSecretKeyRingCollection collection, Long id) + public static PGPSecretKeyRing getKeyRingFromCollection(@Nonnull PGPSecretKeyRingCollection collection, + @Nonnull Long id) throws PGPException { PGPSecretKeyRing uncleanedRing = collection.getSecretKeyRing(id); @@ -104,27 +106,32 @@ public class BCUtil { return cleanedRing; } - public static PGPPublicKeyRing getKeyRingFromCollection(PGPPublicKeyRingCollection collection, Long id) + public static PGPPublicKeyRing getKeyRingFromCollection(@Nonnull PGPPublicKeyRingCollection collection, + @Nonnull Long id) throws PGPException { PGPPublicKey key = collection.getPublicKey(id); return removeUnassociatedKeysFromKeyRing(collection.getPublicKeyRing(id), key); } - public static InputStream getPgpDecoderInputStream(byte[] bytes) throws IOException { + public static InputStream getPgpDecoderInputStream(@Nonnull byte[] bytes) + throws IOException { return getPgpDecoderInputStream(new ByteArrayInputStream(bytes)); } - public static InputStream getPgpDecoderInputStream(InputStream inputStream) throws IOException { + public static InputStream getPgpDecoderInputStream(@Nonnull InputStream inputStream) + throws IOException { return PGPUtil.getDecoderStream(inputStream); } - public static byte[] getDecodedBytes(byte[] bytes) throws IOException { + public static byte[] getDecodedBytes(@Nonnull byte[] bytes) + throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); Streams.pipeAll(getPgpDecoderInputStream(bytes), buffer); return buffer.toByteArray(); } - public static byte[] getDecodedBytes(InputStream inputStream) throws IOException { + public static byte[] getDecodedBytes(@Nonnull InputStream inputStream) + throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); Streams.pipeAll(inputStream, buffer); return getDecodedBytes(buffer.toByteArray()); @@ -138,7 +145,8 @@ public class BCUtil { * @param masterKey master key * @return "cleaned" key ring */ - public static PGPPublicKeyRing removeUnassociatedKeysFromKeyRing(PGPPublicKeyRing ring, PGPPublicKey masterKey) { + public static PGPPublicKeyRing removeUnassociatedKeysFromKeyRing(@Nonnull PGPPublicKeyRing ring, + @Nonnull PGPPublicKey masterKey) { if (!masterKey.isMasterKey()) { throw new IllegalArgumentException("Given key is not a master key."); } @@ -168,7 +176,8 @@ public class BCUtil { * @param masterKey master key * @return "cleaned" key ring */ - public static PGPSecretKeyRing removeUnassociatedKeysFromKeyRing(PGPSecretKeyRing ring, PGPPublicKey masterKey) { + public static PGPSecretKeyRing removeUnassociatedKeysFromKeyRing(@Nonnull PGPSecretKeyRing ring, + @Nonnull PGPPublicKey masterKey) { if (!masterKey.isMasterKey()) { throw new IllegalArgumentException("Given key is not a master key."); } @@ -196,7 +205,7 @@ public class BCUtil { * @param ring key ring * @return master key */ - public static PGPPublicKey getMasterKeyFrom(PGPPublicKeyRing ring) { + public static PGPPublicKey getMasterKeyFrom(@Nonnull PGPPublicKeyRing ring) { Iterator it = ring.getPublicKeys(); while (it.hasNext()) { PGPPublicKey k = it.next(); @@ -208,7 +217,7 @@ public class BCUtil { return null; } - public static PGPPublicKey getMasterKeyFrom(PGPKeyRing ring) { + public static PGPPublicKey getMasterKeyFrom(@Nonnull PGPKeyRing ring) { Iterator it = ring.getPublicKeys(); while (it.hasNext()) { PGPPublicKey k = (PGPPublicKey) it.next(); @@ -220,7 +229,7 @@ public class BCUtil { return null; } - public static Set signingKeyIds(PGPSecretKeyRing ring) { + public static Set signingKeyIds(@Nonnull PGPSecretKeyRing ring) { Set ids = new HashSet<>(); Iterator it = ring.getPublicKeys(); while (it.hasNext()) { @@ -261,61 +270,13 @@ public class BCUtil { return ids; } - public static boolean keyRingContainsKeyWithId(PGPPublicKeyRing ring, long keyId) { + public static boolean keyRingContainsKeyWithId(@Nonnull PGPPublicKeyRing ring, + long keyId) { return ring.getPublicKey(keyId) != null; } - public static boolean keyRingContainsKeyWithId(PGPSecretKeyRing ring, long keyId) { + public static boolean keyRingContainsKeyWithId(@Nonnull PGPSecretKeyRing ring, + long keyId) { return ring.getSecretKey(keyId) != null; } - - /* - public static PGPKeyRing merge(PGPKeyRing one, PGPKeyRing other) { - - PGPPublicKey masterOne = getMasterKeyFrom(one); - if (masterOne == null) { - throw new IllegalArgumentException("First PGPKeyRing has no master key"); - } - - PGPPublicKey masterOther = getMasterKeyFrom(other); - if (masterOther == null) { - throw new IllegalArgumentException("Other PGPKeyRing has no master key"); - } - - if (masterOne.getKeyID() != masterOther.getKeyID() || - Arrays.equals(masterOne.getFingerprint(), masterOther.getFingerprint())) { - throw new IllegalArgumentException("Keys are not the same."); - } - - PGPKeyRing merged = one; - - boolean mergedIsSecret = (merged instanceof PGPSecretKeyRing); - boolean otherIsSecret = (other instanceof PGPSecretKeyRing); - - for (Iterator it = other.getPublicKeys(); it.hasNext(); ) { - - PGPPublicKey nextPublicKey = (PGPPublicKey) it.next(); - PGPPublicKey pendant = merged.getPublicKey(nextPublicKey.getKeyID()); - - if (pendant == null) { - if (mergedIsSecret && otherIsSecret) { - // Add secret key - PGPSecretKey secretKey = ((PGPSecretKeyRing) other).getSecretKey(nextPublicKey.getKeyID()); - merged = PGPSecretKeyRing.insertSecretKey((PGPSecretKeyRing) merged, secretKey); - } else { - if (mergedIsSecret) { - PGPSecretKeyRing mergedAsSecret = (PGPSecretKeyRing) merged; - PGPSecretKey secretKey = mergedAsSecret.getSecretKey(nextPublicKey.getKeyID()); - if (secretKey == null) { - PGPPublicKeyRing mergedAsPublic = publicKeyRingFromSecretKeyRing((PGPSecretKeyRing) merged); - mergedAsPublic = PGPPublicKeyRing.insertPublicKey(mergedAsPublic, nextPublicKey); - mergedAsSecret = PGPSecretKeyRing.replacePublicKeys(mergedAsSecret, mergedAsPublic); - merged = mergedAsSecret; - } - } - } - } - } - } - */ } diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/KeyRingSubKeyFix.java b/pgpainless-core/src/main/java/org/pgpainless/util/KeyRingSubKeyFix.java index a226b55f..6927ec29 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/KeyRingSubKeyFix.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/KeyRingSubKeyFix.java @@ -15,6 +15,8 @@ */ package org.pgpainless.util; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Iterator; @@ -56,9 +58,9 @@ public class KeyRingSubKeyFix { * * @throws PGPException in case we cannot dismantle or reassemble the key. */ - public static PGPSecretKeyRing repairSubkeyPackets(PGPSecretKeyRing secretKeys, - PBESecretKeyDecryptor decryptor, - PBESecretKeyEncryptor encryptor) + public static PGPSecretKeyRing repairSubkeyPackets(@Nonnull PGPSecretKeyRing secretKeys, + @Nullable PBESecretKeyDecryptor decryptor, + @Nullable PBESecretKeyEncryptor encryptor) throws PGPException { PGPDigestCalculator calculator = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1); @@ -68,15 +70,14 @@ public class KeyRingSubKeyFix { try { while (secretKeyIterator.hasNext()) { - PGPSecretKey key = secretKeyIterator.next(); + PGPSecretKey secSubKey = secretKeyIterator.next(); - if (key.isMasterKey()) { - LOGGER.log(Level.INFO, Long.toHexString(key.getKeyID()) + " is master key. Skip."); - _secretKeys.add(key); + if (secSubKey.isMasterKey()) { + LOGGER.log(Level.INFO, Long.toHexString(secSubKey.getKeyID()) + " is master key. Skip."); + _secretKeys.add(secSubKey); continue; } - PGPSecretKey secSubKey = key; PGPPublicKey pubSubKey = secSubKey.getPublicKey(); // check for public key packet type diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java b/pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java index 178822f7..ee5e70f8 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/MultiMap.java @@ -15,6 +15,7 @@ */ package org.pgpainless.util; +import javax.annotation.Nonnull; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -29,14 +30,14 @@ public class MultiMap { map = new HashMap<>(); } - public MultiMap(MultiMap other) { + public MultiMap(@Nonnull MultiMap other) { this.map = new HashMap<>(); for (K k : other.map.keySet()) { map.put(k, new HashSet<>(other.map.get(k))); } } - public MultiMap(Map> content) { + public MultiMap(@Nonnull Map> content) { this.map = new HashMap<>(content); } @@ -48,18 +49,18 @@ public class MultiMap { return map.isEmpty(); } - public boolean containsKey(Object o) { + public boolean containsKey(K o) { return map.containsKey(o); } - public boolean containsValue(Object o) { + public boolean containsValue(V o) { for (Set values : map.values()) { if (values.contains(o)) return true; } return false; } - public Set get(Object o) { + public Set get(K o) { return map.get(o); } @@ -78,7 +79,7 @@ public class MultiMap { } } - public void remove(Object o) { + public void remove(K o) { for (Set values : map.values()) { values.remove(o); } diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/Passphrase.java b/pgpainless-core/src/main/java/org/pgpainless/util/Passphrase.java index 71706457..10fddf8c 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/Passphrase.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/Passphrase.java @@ -15,6 +15,7 @@ */ package org.pgpainless.util; +import javax.annotation.Nullable; import java.util.Arrays; public class Passphrase { @@ -29,7 +30,7 @@ public class Passphrase { * * @param chars may be null for empty passwords. */ - public Passphrase(char[] chars) { + public Passphrase(@Nullable char[] chars) { this.chars = chars; } @@ -58,12 +59,13 @@ public class Passphrase { /** * Return a copy of the underlying char array. + * A return value of {@code null} represents no password. * * @return passphrase chars. * * @throws IllegalStateException in case the password has been cleared at this point. */ - public char[] getChars() { + public @Nullable char[] getChars() { synchronized (lock) { if (!valid) { throw new IllegalStateException("Passphrase has been cleared."); @@ -89,4 +91,13 @@ public class Passphrase { return valid; } } + + /** + * Represents a {@link Passphrase} instance that represents no password. + * + * @return empty passphrase + */ + public static Passphrase emptyPassphrase() { + return new Passphrase(null); + } }