From d869bc8c8bc495e01fce6436a802a7c9f258d6c7 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 5 Aug 2018 12:22:49 +0200 Subject: [PATCH 1/4] Add @SuppressWarnings("deprecation") to LengthTest --- pgpainless-core/src/test/java/org/pgpainless/LengthTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pgpainless-core/src/test/java/org/pgpainless/LengthTest.java b/pgpainless-core/src/test/java/org/pgpainless/LengthTest.java index 7b4dd2c2..92ff9f20 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/LengthTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/LengthTest.java @@ -59,7 +59,9 @@ public class LengthTest extends AbstractPGPainlessTest { throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, IOException { LOGGER.log(Level.INFO, "\nRSA-2048 -> RSA-2048"); + @SuppressWarnings("deprecation") PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048); + @SuppressWarnings("deprecation") PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048); encryptDecryptForSecretKeyRings(sender, recipient); } @@ -78,6 +80,7 @@ public class LengthTest extends AbstractPGPainlessTest { public void rsaEc() throws PGPException, IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException { LOGGER.log(Level.INFO, "\nRSA-2048 -> EC"); + @SuppressWarnings("deprecation") PGPKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("simplejid@server.tld", RsaLength._2048); PGPKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("otherjid@other.srv"); encryptDecryptForSecretKeyRings(sender, recipient); @@ -89,6 +92,7 @@ public class LengthTest extends AbstractPGPainlessTest { IOException { LOGGER.log(Level.INFO, "\nEC -> RSA-2048"); PGPKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("simplejid@server.tld"); + @SuppressWarnings("deprecation") PGPKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("otherjid@other.srv", RsaLength._2048); encryptDecryptForSecretKeyRings(sender, recipient); } From cab513773d4f4594624a7d9ed52b8863e9e86c76 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 5 Aug 2018 12:23:09 +0200 Subject: [PATCH 2/4] Add @SuppressWarnings("deprecation") to BCUtilTest --- pgpainless-core/src/test/java/org/pgpainless/BCUtilTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/pgpainless-core/src/test/java/org/pgpainless/BCUtilTest.java b/pgpainless-core/src/test/java/org/pgpainless/BCUtilTest.java index 3e8dea69..587b7362 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/BCUtilTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/BCUtilTest.java @@ -106,6 +106,7 @@ public class BCUtilTest extends AbstractPGPainlessTest { @Test public void removeUnsignedKeysTest() throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { + @SuppressWarnings("deprecation") PGPKeyRing alice = PGPainless.generateKeyRing().simpleRsaKeyRing("alice@wonderland.lit", RsaLength._1024); PGPKeyRing mallory = PGPainless.generateKeyRing().simpleEcKeyRing("mallory@mall.ory"); From b6bb395e4f9caced2b1486fdcb7c483d3e0d3b32 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 5 Aug 2018 12:23:24 +0200 Subject: [PATCH 3/4] Add missing @Override to EncryptionBuilder --- .../org/pgpainless/encryption_signing/EncryptionBuilder.java | 1 + 1 file changed, 1 insertion(+) 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 58b3417b..ef11df65 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 @@ -193,6 +193,7 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { return this; } + @Override public WithAlgorithms andToSelf(@Nonnull PublicKeyRingSelectionStrategy ringSelectionStrategy, @Nonnull MultiMap keys) { if (keys.isEmpty()) { From cfab8a744d236a568516d205f22ae9352a4d7749 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 5 Aug 2018 12:23:54 +0200 Subject: [PATCH 4/4] Properly close InputStream in SymmetricEncryptorDecryptor Although it is possibly not strict required, it is always a good idea to close streams even if the conrete implementation does not cause resource leaks when not closed. After all, the implementation could change at some point in the future. --- .../SymmetricEncryptorDecryptor.java | 65 +++++++++++-------- 1 file changed, 37 insertions(+), 28 deletions(-) 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 87ec4404..c5505156 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 @@ -102,43 +102,52 @@ public class SymmetricEncryptorDecryptor { */ public static byte[] symmetricallyDecrypt(@Nonnull byte[] data, @Nonnull Passphrase password) throws IOException, PGPException { - InputStream in = new BufferedInputStream(new ByteArrayInputStream(data)); - in = PGPUtil.getDecoderStream(in); + PGPPBEEncryptedData pbe; + ByteArrayOutputStream outputStream = null; + BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(data)); + InputStream in = PGPUtil.getDecoderStream(bis); - BcPGPObjectFactory pgpF = new BcPGPObjectFactory(in); - PGPEncryptedDataList enc; - Object o = pgpF.nextObject(); + try { + BcPGPObjectFactory pgpF = new BcPGPObjectFactory(in); + PGPEncryptedDataList enc; + Object o = pgpF.nextObject(); - if (o instanceof PGPEncryptedDataList) { - enc = (PGPEncryptedDataList) o; - } else { - enc = (PGPEncryptedDataList) pgpF.nextObject(); - } + if (o instanceof PGPEncryptedDataList) { + enc = (PGPEncryptedDataList) o; + } else { + enc = (PGPEncryptedDataList) pgpF.nextObject(); + } - PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0); + pbe = (PGPPBEEncryptedData) enc.get(0); - InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory( - password.getChars(), new BcPGPDigestCalculatorProvider())); + InputStream clear = pbe.getDataStream( + new BcPBEDataDecryptorFactory(password.getChars(), new BcPGPDigestCalculatorProvider())); + BcPGPObjectFactory pgpFact = new BcPGPObjectFactory(clear); - BcPGPObjectFactory pgpFact = new BcPGPObjectFactory(clear); - - o = pgpFact.nextObject(); - if (o instanceof PGPCompressedData) { - PGPCompressedData cData = (PGPCompressedData) o; - pgpFact = new BcPGPObjectFactory(cData.getDataStream()); o = pgpFact.nextObject(); + if (o instanceof PGPCompressedData) { + PGPCompressedData cData = (PGPCompressedData) o; + pgpFact = new BcPGPObjectFactory(cData.getDataStream()); + o = pgpFact.nextObject(); + } + + PGPLiteralData ld = (PGPLiteralData) o; + InputStream unc = ld.getInputStream(); + + try { + outputStream = new ByteArrayOutputStream(); + + Streams.pipeAll(unc, outputStream); + } finally { + if (outputStream != null) { + outputStream.close(); + } + } + } finally { + in.close(); } - PGPLiteralData ld = (PGPLiteralData) o; - InputStream unc = ld.getInputStream(); - - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - - Streams.pipeAll(unc, outputStream); - - outputStream.close(); - if (pbe.isIntegrityProtected()) { if (!pbe.verify()) { throw new PGPException("Integrity check failed.");