diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/generation/StupidAlgorithmPreferenceEncryptionTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/generation/StupidAlgorithmPreferenceEncryptionTest.java new file mode 100644 index 00000000..42aa0856 --- /dev/null +++ b/pgpainless-core/src/test/java/org/pgpainless/key/generation/StupidAlgorithmPreferenceEncryptionTest.java @@ -0,0 +1,106 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.key.generation; + +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPPublicKeyRing; +import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.junit.jupiter.api.Test; +import org.pgpainless.PGPainless; +import org.pgpainless.encryption_signing.EncryptionOptions; +import org.pgpainless.encryption_signing.EncryptionResult; +import org.pgpainless.encryption_signing.EncryptionStream; +import org.pgpainless.encryption_signing.ProducerOptions; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class StupidAlgorithmPreferenceEncryptionTest { + + // RSA key with symmetric algorithm preference "NULL" (unencrypted). + private static final String STUPID_KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" + + "Version: PGPainless\n" + + "Comment: 6CA6 C4F3 CF01 CAC9 C954 0BD6 5BDA 78ED C479 A8E9\n" + + "Comment: This is Stupid\n" + + "\n" + + "lQcYBGLRTLUBEADg2W4x7HgoBDJS7qYCqheyY/J/SNKJ7AvPTTd8V7S9rxZ6kZlo\n" + + "556q1+KJA+/jWJ3LFvuNCyTW/utQxxLYylDFTaI4KJ5MWWLtvlph+VjolI/+u8B6\n" + + "qSeZl72uXiEFW8vnQfBliXOyWCudLHh4cRj/DgxEbL3Cm9FXH0/XYRKweSPRhOkX\n" + + "Mh/veiTe7a6RcwGOi0DNcRth5g4MggspgHQmNcHq2XGeOOp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ENAqAYRQLJR4iHPSjcRFz\n" + + "WFv0UlYdnSNZfY0vroIMxnM1w4XJVg==\n" + + "=4plS\n" + + "-----END PGP PRIVATE KEY BLOCK-----\n"; + + @Test + public void testEncryptionIsNotUnencrypted() throws PGPException, IOException { + PGPSecretKeyRing stupidKey = PGPainless.readKeyRing().secretKeyRing(STUPID_KEY); + PGPPublicKeyRing certificate = PGPainless.extractCertificate(stupidKey); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + EncryptionStream encryptionStream = PGPainless.encryptAndOrSign() + .onOutputStream(out) + .withOptions(ProducerOptions.encrypt( + new EncryptionOptions().addRecipient(certificate) + )); + + encryptionStream.write("Hello".getBytes(StandardCharsets.UTF_8)); + encryptionStream.close(); + + EncryptionResult metadata = encryptionStream.getResult(); + assertTrue(metadata.isEncryptedFor(certificate)); + assertEquals(PGPainless.getPolicy().getSymmetricKeyEncryptionAlgorithmPolicy().getDefaultSymmetricKeyAlgorithm(), + metadata.getEncryptionAlgorithm()); + } +}