2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-01-11 13:11:14 +01:00
|
|
|
package org.pgpainless.symmetric_encryption;
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2020-11-13 16:31:59 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
2021-05-28 23:29:41 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2020-12-26 19:04:27 +01:00
|
|
|
import java.io.ByteArrayInputStream;
|
2018-06-13 17:26:48 +02:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
2020-12-26 19:04:27 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-05-28 23:29:41 +02:00
|
|
|
import java.util.Random;
|
2018-06-13 17:26:48 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2020-12-26 19:04:27 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
2021-07-31 20:40:31 +02:00
|
|
|
import org.bouncycastle.util.io.Streams;
|
2021-12-14 16:55:04 +01:00
|
|
|
import org.junit.jupiter.api.TestTemplate;
|
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
2021-12-14 16:56:29 +01:00
|
|
|
import org.pgpainless.util.TestAllImplementations;
|
2020-01-10 22:07:54 +01:00
|
|
|
import org.pgpainless.PGPainless;
|
2021-06-16 15:38:02 +02:00
|
|
|
import org.pgpainless.decryption_verification.ConsumerOptions;
|
2020-12-26 19:04:27 +01:00
|
|
|
import org.pgpainless.decryption_verification.DecryptionStream;
|
2021-10-19 18:13:23 +02:00
|
|
|
import org.pgpainless.decryption_verification.MissingKeyPassphraseStrategy;
|
2021-05-28 23:29:41 +02:00
|
|
|
import org.pgpainless.encryption_signing.EncryptionOptions;
|
2020-12-26 19:04:27 +01:00
|
|
|
import org.pgpainless.encryption_signing.EncryptionStream;
|
2021-05-28 23:29:41 +02:00
|
|
|
import org.pgpainless.encryption_signing.ProducerOptions;
|
|
|
|
import org.pgpainless.exception.MissingDecryptionMethodException;
|
2020-12-26 19:04:27 +01:00
|
|
|
import org.pgpainless.key.TestKeys;
|
|
|
|
import org.pgpainless.key.protection.KeyRingProtectionSettings;
|
|
|
|
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
|
|
|
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
|
|
|
import org.pgpainless.key.protection.passphrase_provider.SolitaryPassphraseProvider;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.util.Passphrase;
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2020-12-26 19:04:27 +01:00
|
|
|
/**
|
|
|
|
* Test parallel symmetric and public key encryption/decryption.
|
|
|
|
*/
|
2020-01-10 22:07:54 +01:00
|
|
|
public class SymmetricEncryptionTest {
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
public void encryptWithKeyAndPassphrase_DecryptWithKey() throws IOException, PGPException {
|
2020-12-26 19:04:27 +01:00
|
|
|
byte[] plaintext = "This is a secret message".getBytes(StandardCharsets.UTF_8);
|
|
|
|
ByteArrayInputStream plaintextIn = new ByteArrayInputStream(plaintext);
|
|
|
|
PGPPublicKeyRing encryptionKey = TestKeys.getCryptiePublicKeyRing();
|
|
|
|
Passphrase encryptionPassphrase = Passphrase.fromPassword("greenBeans");
|
2018-07-02 21:40:59 +02:00
|
|
|
|
2020-12-26 19:04:27 +01:00
|
|
|
ByteArrayOutputStream ciphertextOut = new ByteArrayOutputStream();
|
2021-06-16 15:38:02 +02:00
|
|
|
EncryptionStream encryptor = PGPainless.encryptAndOrSign()
|
|
|
|
.onOutputStream(ciphertextOut)
|
|
|
|
.withOptions(ProducerOptions.encrypt(
|
|
|
|
EncryptionOptions.encryptCommunications()
|
|
|
|
.addPassphrase(encryptionPassphrase)
|
|
|
|
.addRecipient(encryptionKey)
|
|
|
|
));
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2021-07-31 20:40:31 +02:00
|
|
|
Streams.pipeAll(plaintextIn, encryptor);
|
2020-12-26 19:04:27 +01:00
|
|
|
encryptor.close();
|
|
|
|
|
|
|
|
byte[] ciphertext = ciphertextOut.toByteArray();
|
|
|
|
|
|
|
|
// Test symmetric decryption
|
|
|
|
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
|
|
|
|
.onInputStream(new ByteArrayInputStream(ciphertext))
|
2021-06-16 15:38:02 +02:00
|
|
|
.withOptions(new ConsumerOptions()
|
|
|
|
.addDecryptionPassphrase(encryptionPassphrase));
|
2020-12-26 19:04:27 +01:00
|
|
|
|
|
|
|
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
|
|
|
|
|
2021-07-31 20:40:31 +02:00
|
|
|
Streams.pipeAll(decryptor, decrypted);
|
2020-12-26 19:04:27 +01:00
|
|
|
decryptor.close();
|
|
|
|
|
|
|
|
assertArrayEquals(plaintext, decrypted.toByteArray());
|
|
|
|
|
|
|
|
// Test public key decryption
|
|
|
|
PGPSecretKeyRingCollection decryptionKeys = TestKeys.getCryptieSecretKeyRingCollection();
|
|
|
|
SecretKeyRingProtector protector = new PasswordBasedSecretKeyRingProtector(
|
2023-06-20 14:05:41 +02:00
|
|
|
KeyRingProtectionSettings.saltedAndIterated(),
|
2020-12-26 19:04:27 +01:00
|
|
|
new SolitaryPassphraseProvider(Passphrase.fromPassword(TestKeys.CRYPTIE_PASSWORD)));
|
|
|
|
decryptor = PGPainless.decryptAndOrVerify()
|
|
|
|
.onInputStream(new ByteArrayInputStream(ciphertext))
|
2021-06-16 15:38:02 +02:00
|
|
|
.withOptions(new ConsumerOptions()
|
|
|
|
.addDecryptionKeys(decryptionKeys, protector));
|
2020-12-26 19:04:27 +01:00
|
|
|
|
|
|
|
decrypted = new ByteArrayOutputStream();
|
|
|
|
|
2021-07-31 20:40:31 +02:00
|
|
|
Streams.pipeAll(decryptor, decrypted);
|
2020-12-26 19:04:27 +01:00
|
|
|
decryptor.close();
|
|
|
|
|
|
|
|
assertArrayEquals(plaintext, decrypted.toByteArray());
|
2018-06-13 17:26:48 +02:00
|
|
|
}
|
2021-05-28 23:29:41 +02:00
|
|
|
|
2021-12-14 16:55:04 +01:00
|
|
|
@TestTemplate
|
2021-12-14 16:56:29 +01:00
|
|
|
@ExtendWith(TestAllImplementations.class)
|
2021-12-14 16:55:04 +01:00
|
|
|
public void testMismatchPassphraseFails() throws IOException, PGPException {
|
2021-05-28 23:29:41 +02:00
|
|
|
byte[] bytes = new byte[5000];
|
|
|
|
new Random().nextBytes(bytes);
|
|
|
|
|
|
|
|
ByteArrayOutputStream ciphertextOut = new ByteArrayOutputStream();
|
|
|
|
EncryptionStream encryptor = PGPainless.encryptAndOrSign().onOutputStream(ciphertextOut)
|
|
|
|
.withOptions(ProducerOptions.encrypt(
|
|
|
|
EncryptionOptions.encryptCommunications()
|
|
|
|
.addPassphrase(Passphrase.fromPassword("mellon"))));
|
|
|
|
|
2021-07-31 20:40:31 +02:00
|
|
|
Streams.pipeAll(new ByteArrayInputStream(bytes), encryptor);
|
2021-05-28 23:29:41 +02:00
|
|
|
encryptor.close();
|
|
|
|
|
|
|
|
assertThrows(MissingDecryptionMethodException.class, () -> PGPainless.decryptAndOrVerify()
|
|
|
|
.onInputStream(new ByteArrayInputStream(ciphertextOut.toByteArray()))
|
2021-06-16 15:38:02 +02:00
|
|
|
.withOptions(new ConsumerOptions()
|
2021-10-19 18:13:23 +02:00
|
|
|
.setMissingKeyPassphraseStrategy(MissingKeyPassphraseStrategy.THROW_EXCEPTION)
|
2021-06-16 15:38:02 +02:00
|
|
|
.addDecryptionPassphrase(Passphrase.fromPassword("meldir"))));
|
2021-05-28 23:29:41 +02:00
|
|
|
}
|
2018-06-13 17:26:48 +02:00
|
|
|
}
|