diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/ArmorImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/ArmorImpl.java index daee3a9b..421dc7a7 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/ArmorImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/ArmorImpl.java @@ -18,21 +18,26 @@ import sop.enums.ArmorLabel; import sop.exception.SOPGPException; import sop.operation.Armor; +import javax.annotation.Nonnull; + /** * Implementation of the
armoroperation using PGPainless. */ public class ArmorImpl implements Armor { + @Nonnull @Override - public Armor label(ArmorLabel label) throws SOPGPException.UnsupportedOption { + @Deprecated + public Armor label(@Nonnull ArmorLabel label) throws SOPGPException.UnsupportedOption { throw new SOPGPException.UnsupportedOption("Setting custom Armor labels not supported."); } + @Nonnull @Override - public Ready data(InputStream data) throws SOPGPException.BadData { + public Ready data(@Nonnull InputStream data) throws SOPGPException.BadData { return new Ready() { @Override - public void writeTo(OutputStream outputStream) throws IOException { + public void writeTo(@Nonnull OutputStream outputStream) throws IOException { // By buffering the output stream, we can improve performance drastically BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/ChangeKeyPasswordImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/ChangeKeyPasswordImpl.java index 95377b12..56613720 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/ChangeKeyPasswordImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/ChangeKeyPasswordImpl.java @@ -24,32 +24,38 @@ import sop.Ready; import sop.exception.SOPGPException; import sop.operation.ChangeKeyPassword; +import javax.annotation.Nonnull; + public class ChangeKeyPasswordImpl implements ChangeKeyPassword { private final MatchMakingSecretKeyRingProtector oldProtector = new MatchMakingSecretKeyRingProtector(); private Passphrase newPassphrase = Passphrase.emptyPassphrase(); private boolean armor = true; + @Nonnull @Override public ChangeKeyPassword noArmor() { armor = false; return this; } + @Nonnull @Override - public ChangeKeyPassword oldKeyPassphrase(String oldPassphrase) { + public ChangeKeyPassword oldKeyPassphrase(@Nonnull String oldPassphrase) { oldProtector.addPassphrase(Passphrase.fromPassword(oldPassphrase)); return this; } + @Nonnull @Override - public ChangeKeyPassword newKeyPassphrase(String newPassphrase) { + public ChangeKeyPassword newKeyPassphrase(@Nonnull String newPassphrase) { this.newPassphrase = Passphrase.fromPassword(newPassphrase); return this; } + @Nonnull @Override - public Ready keys(InputStream inputStream) throws SOPGPException.KeyIsProtected { + public Ready keys(@Nonnull InputStream inputStream) throws SOPGPException.KeyIsProtected { SecretKeyRingProtector newProtector = SecretKeyRingProtector.unlockAnyKeyWith(newPassphrase); PGPSecretKeyRingCollection secretKeyRingCollection; try { @@ -76,7 +82,7 @@ public class ChangeKeyPasswordImpl implements ChangeKeyPassword { final PGPSecretKeyRingCollection changedSecretKeyCollection = new PGPSecretKeyRingCollection(updatedSecretKeys); return new Ready() { @Override - public void writeTo(OutputStream outputStream) throws IOException { + public void writeTo(@Nonnull OutputStream outputStream) throws IOException { if (armor) { ArmoredOutputStream armorOut = ArmoredOutputStreamFactory.get(outputStream); changedSecretKeyCollection.encode(armorOut); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/DearmorImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/DearmorImpl.java index 29483437..5fd0a03b 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/DearmorImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/DearmorImpl.java @@ -15,13 +15,16 @@ import sop.Ready; import sop.exception.SOPGPException; import sop.operation.Dearmor; +import javax.annotation.Nonnull; + /** * Implementation of the
dearmoroperation using PGPainless. */ public class DearmorImpl implements Dearmor { + @Nonnull @Override - public Ready data(InputStream data) { + public Ready data(@Nonnull InputStream data) { InputStream decoder; try { decoder = PGPUtil.getDecoderStream(data); @@ -31,7 +34,7 @@ public class DearmorImpl implements Dearmor { return new Ready() { @Override - public void writeTo(OutputStream outputStream) throws IOException { + public void writeTo(@Nonnull OutputStream outputStream) throws IOException { BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); Streams.pipeAll(decoder, bufferedOutputStream); bufferedOutputStream.flush(); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/DecryptImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/DecryptImpl.java index d15713ca..bc5081d7 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/DecryptImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/DecryptImpl.java @@ -7,7 +7,6 @@ package org.pgpainless.sop; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -33,6 +32,9 @@ import sop.SessionKey; import sop.Verification; import sop.exception.SOPGPException; import sop.operation.Decrypt; +import sop.util.UTF8Util; + +import javax.annotation.Nonnull; /** * Implementation of the
decryptoperation using PGPainless. @@ -42,20 +44,23 @@ public class DecryptImpl implements Decrypt { private final ConsumerOptions consumerOptions = ConsumerOptions.get(); private final MatchMakingSecretKeyRingProtector protector = new MatchMakingSecretKeyRingProtector(); + @Nonnull @Override - public DecryptImpl verifyNotBefore(Date timestamp) throws SOPGPException.UnsupportedOption { + public DecryptImpl verifyNotBefore(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption { consumerOptions.verifyNotBefore(timestamp); return this; } + @Nonnull @Override - public DecryptImpl verifyNotAfter(Date timestamp) throws SOPGPException.UnsupportedOption { + public DecryptImpl verifyNotAfter(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption { consumerOptions.verifyNotAfter(timestamp); return this; } + @Nonnull @Override - public DecryptImpl verifyWithCert(InputStream certIn) throws SOPGPException.BadData, IOException { + public DecryptImpl verifyWithCert(@Nonnull InputStream certIn) throws SOPGPException.BadData, IOException { PGPPublicKeyRingCollection certs = KeyReader.readPublicKeys(certIn, true); if (certs != null) { consumerOptions.addVerificationCerts(certs); @@ -63,8 +68,9 @@ public class DecryptImpl implements Decrypt { return this; } + @Nonnull @Override - public DecryptImpl withSessionKey(SessionKey sessionKey) throws SOPGPException.UnsupportedOption { + public DecryptImpl withSessionKey(@Nonnull SessionKey sessionKey) throws SOPGPException.UnsupportedOption { consumerOptions.setSessionKey( new org.pgpainless.util.SessionKey( SymmetricKeyAlgorithm.requireFromId(sessionKey.getAlgorithm()), @@ -72,8 +78,9 @@ public class DecryptImpl implements Decrypt { return this; } + @Nonnull @Override - public DecryptImpl withPassword(String password) { + public DecryptImpl withPassword(@Nonnull String password) { consumerOptions.addDecryptionPassphrase(Passphrase.fromPassword(password)); String withoutTrailingWhitespace = removeTrailingWhitespace(password); if (!password.equals(withoutTrailingWhitespace)) { @@ -91,8 +98,9 @@ public class DecryptImpl implements Decrypt { return passphrase.substring(0, i); } + @Nonnull @Override - public DecryptImpl withKey(InputStream keyIn) throws SOPGPException.BadData, IOException, SOPGPException.UnsupportedAsymmetricAlgo { + public DecryptImpl withKey(@Nonnull InputStream keyIn) throws SOPGPException.BadData, IOException, SOPGPException.UnsupportedAsymmetricAlgo { PGPSecretKeyRingCollection secretKeyCollection = KeyReader.readSecretKeys(keyIn, true); for (PGPSecretKeyRing key : secretKeyCollection) { @@ -102,15 +110,17 @@ public class DecryptImpl implements Decrypt { return this; } + @Nonnull @Override - public Decrypt withKeyPassword(byte[] password) { - String string = new String(password, Charset.forName("UTF8")); + public Decrypt withKeyPassword(@Nonnull byte[] password) { + String string = new String(password, UTF8Util.UTF8); protector.addPassphrase(Passphrase.fromPassword(string)); return this; } + @Nonnull @Override - public ReadyWithResult
signoperation using PGPainless. @@ -42,7 +44,7 @@ import sop.operation.DetachedSign; public class DetachedSignImpl implements DetachedSign { private boolean armor = true; - private SignAs mode = SignAs.Binary; + private SignAs mode = SignAs.binary; private final SigningOptions signingOptions = SigningOptions.get(); private final MatchMakingSecretKeyRingProtector protector = new MatchMakingSecretKeyRingProtector(); private final List
verifyoperation using PGPainless. */ @@ -31,26 +33,30 @@ public class DetachedVerifyImpl implements DetachedVerify { private final ConsumerOptions options = ConsumerOptions.get(); @Override - public DetachedVerify notBefore(Date timestamp) throws SOPGPException.UnsupportedOption { + @Nonnull + public DetachedVerify notBefore(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption { options.verifyNotBefore(timestamp); return this; } @Override - public DetachedVerify notAfter(Date timestamp) throws SOPGPException.UnsupportedOption { + @Nonnull + public DetachedVerify notAfter(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption { options.verifyNotAfter(timestamp); return this; } @Override - public DetachedVerify cert(InputStream cert) throws SOPGPException.BadData, IOException { + @Nonnull + public DetachedVerify cert(@Nonnull InputStream cert) throws SOPGPException.BadData, IOException { PGPPublicKeyRingCollection certificates = KeyReader.readPublicKeys(cert, true); options.addVerificationCerts(certificates); return this; } @Override - public DetachedVerifyImpl signatures(InputStream signatures) throws SOPGPException.BadData { + @Nonnull + public DetachedVerifyImpl signatures(@Nonnull InputStream signatures) throws SOPGPException.BadData { try { options.addVerificationOfDetachedSignatures(signatures); } catch (IOException | PGPException e) { @@ -60,7 +66,8 @@ public class DetachedVerifyImpl implements DetachedVerify { } @Override - public List
encryptoperation using PGPainless. @@ -52,23 +55,26 @@ public class EncryptImpl implements Encrypt { private final Set
extract-certoperation using PGPainless. */ @@ -27,13 +29,15 @@ public class ExtractCertImpl implements ExtractCert { private boolean armor = true; @Override + @Nonnull public ExtractCert noArmor() { armor = false; return this; } @Override - public Ready key(InputStream keyInputStream) throws IOException, SOPGPException.BadData { + @Nonnull + public Ready key(@Nonnull InputStream keyInputStream) throws IOException, SOPGPException.BadData { PGPSecretKeyRingCollection keys = KeyReader.readSecretKeys(keyInputStream, true); List
generate-keyoperation using PGPainless. */ @@ -48,25 +50,29 @@ public class GenerateKeyImpl implements GenerateKey { private String profile = CURVE25519_PROFILE.getName(); @Override + @Nonnull public GenerateKey noArmor() { this.armor = false; return this; } @Override - public GenerateKey userId(String userId) { + @Nonnull + public GenerateKey userId(@Nonnull String userId) { this.userIds.add(userId); return this; } @Override - public GenerateKey withKeyPassword(String password) { + @Nonnull + public GenerateKey withKeyPassword(@Nonnull String password) { this.passphrase = Passphrase.fromPassword(password); return this; } @Override - public GenerateKey profile(String profileName) { + @Nonnull + public GenerateKey profile(@Nonnull String profileName) { // Sanitize the profile name to make sure we support the given profile for (Profile profile : SUPPORTED_PROFILES) { if (profile.getName().equals(profileName)) { @@ -81,18 +87,20 @@ public class GenerateKeyImpl implements GenerateKey { } @Override + @Nonnull public GenerateKey signingOnly() { signingOnly = true; return this; } @Override + @Nonnull public Ready generate() throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo { try { final PGPSecretKeyRing key = generateKeyWithProfile(profile, userIds, passphrase, signingOnly); return new Ready() { @Override - public void writeTo(OutputStream outputStream) throws IOException { + public void writeTo(@Nonnull OutputStream outputStream) throws IOException { if (armor) { ArmoredOutputStream armoredOutputStream = ArmorUtils.toAsciiArmoredStream(key, outputStream); key.encode(armoredOutputStream); diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/InlineDetachImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/InlineDetachImpl.java index da6e0917..85c06d25 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/InlineDetachImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/InlineDetachImpl.java @@ -30,6 +30,8 @@ import sop.Signatures; import sop.exception.SOPGPException; import sop.operation.InlineDetach; +import javax.annotation.Nonnull; + /** * Implementation of the
inline-detachoperation using PGPainless. */ @@ -38,20 +40,22 @@ public class InlineDetachImpl implements InlineDetach { private boolean armor = true; @Override + @Nonnull public InlineDetach noArmor() { this.armor = false; return this; } @Override - public ReadyWithResult
inline-signoperation using PGPainless. @@ -41,19 +43,22 @@ public class InlineSignImpl implements InlineSign { private final List
inline-verifyoperation using PGPainless. */ @@ -34,29 +36,33 @@ public class InlineVerifyImpl implements InlineVerify { private final ConsumerOptions options = ConsumerOptions.get(); @Override - public InlineVerify notBefore(Date timestamp) throws SOPGPException.UnsupportedOption { + @Nonnull + public InlineVerify notBefore(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption { options.verifyNotBefore(timestamp); return this; } @Override - public InlineVerify notAfter(Date timestamp) throws SOPGPException.UnsupportedOption { + @Nonnull + public InlineVerify notAfter(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption { options.verifyNotAfter(timestamp); return this; } @Override - public InlineVerify cert(InputStream cert) throws SOPGPException.BadData, IOException { + @Nonnull + public InlineVerify cert(@Nonnull InputStream cert) throws SOPGPException.BadData, IOException { PGPPublicKeyRingCollection certificates = KeyReader.readPublicKeys(cert, true); options.addVerificationCerts(certificates); return this; } @Override - public ReadyWithResult
list-profilesoperation using PGPainless. * @@ -17,10 +19,8 @@ import sop.operation.ListProfiles; public class ListProfilesImpl implements ListProfiles { @Override - public List
sopAPI using PGPainless. *
{@code @@ -35,86 +37,103 @@ public class SOPImpl implements SOP { } @Override + @Nonnull public Version version() { return new VersionImpl(); } @Override + @Nonnull public GenerateKey generateKey() { return new GenerateKeyImpl(); } @Override + @Nonnull public ExtractCert extractCert() { return new ExtractCertImpl(); } @Override + @Nonnull public DetachedSign sign() { return detachedSign(); } @Override + @Nonnull public DetachedSign detachedSign() { return new DetachedSignImpl(); } @Override + @Nonnull public InlineSign inlineSign() { return new InlineSignImpl(); } @Override + @Nonnull public DetachedVerify verify() { return detachedVerify(); } @Override + @Nonnull public DetachedVerify detachedVerify() { return new DetachedVerifyImpl(); } @Override + @Nonnull public InlineVerify inlineVerify() { return new InlineVerifyImpl(); } @Override + @Nonnull public Encrypt encrypt() { return new EncryptImpl(); } @Override + @Nonnull public Decrypt decrypt() { return new DecryptImpl(); } @Override + @Nonnull public Armor armor() { return new ArmorImpl(); } @Override + @Nonnull public Dearmor dearmor() { return new DearmorImpl(); } @Override + @Nonnull public ListProfiles listProfiles() { return new ListProfilesImpl(); } @Override + @Nonnull public RevokeKey revokeKey() { return new RevokeKeyImpl(); } @Override + @Nonnull public ChangeKeyPassword changeKeyPassword() { return new ChangeKeyPasswordImpl(); } @Override + @Nonnull public InlineDetach inlineDetach() { return new InlineDetachImpl(); } diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/VersionImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/VersionImpl.java index 488d95b3..424f8d19 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/VersionImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/VersionImpl.java @@ -12,6 +12,8 @@ import java.util.Properties; import org.bouncycastle.jce.provider.BouncyCastleProvider; import sop.operation.Version; +import javax.annotation.Nonnull; + /** * Implementation of theversionoperation using PGPainless. */ @@ -21,11 +23,13 @@ public class VersionImpl implements Version { private static final int SOP_VERSION = 7; @Override + @Nonnull public String getName() { return "PGPainless-SOP"; } @Override + @Nonnull public String getVersion() { // See https://stackoverflow.com/a/50119235 String version; @@ -44,11 +48,13 @@ public class VersionImpl implements Version { } @Override + @Nonnull public String getBackendVersion() { return "PGPainless " + getVersion(); } @Override + @Nonnull public String getExtendedVersion() { double bcVersion = new BouncyCastleProvider().getVersion(); String FORMAT_VERSION = String.format("%02d", SOP_VERSION);