2018-06-13 17:26:48 +02:00
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2018-06-07 18:12:13 +02:00
|
|
|
|
package de.vanitasvitae.crypto.pgpainless;
|
|
|
|
|
|
|
|
|
|
import static junit.framework.TestCase.assertTrue;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
2018-06-08 15:29:09 +02:00
|
|
|
|
import java.nio.charset.Charset;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.security.NoSuchProviderException;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collections;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-13 17:26:48 +02:00
|
|
|
|
import de.vanitasvitae.crypto.pgpainless.algorithm.PublicKeyAlgorithm;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
import de.vanitasvitae.crypto.pgpainless.decryption_verification.DecryptionStream;
|
|
|
|
|
import de.vanitasvitae.crypto.pgpainless.decryption_verification.PainlessResult;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
import de.vanitasvitae.crypto.pgpainless.key.SecretKeyRingProtector;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
import de.vanitasvitae.crypto.pgpainless.key.generation.type.length.RsaLength;
|
|
|
|
|
import de.vanitasvitae.crypto.pgpainless.util.BCUtil;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
|
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
|
|
|
import org.bouncycastle.util.io.Streams;
|
2018-06-10 17:12:44 +02:00
|
|
|
|
import org.junit.Ignore;
|
2018-06-07 18:12:13 +02:00
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
public class EncryptDecryptTest extends AbstractPGPainlessTest {
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-13 17:26:48 +02:00
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(EncryptDecryptTest.class.getName());
|
2018-06-10 17:12:44 +02:00
|
|
|
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-13 17:26:48 +02:00
|
|
|
|
private static final String testMessage = "Ah, Juliet, if the measure of thy joy\n" +
|
|
|
|
|
"Be heaped like mine, and that thy skill be more\n" +
|
|
|
|
|
"To blazon it, then sweeten with thy breath\n" +
|
|
|
|
|
"This neighbor air, and let rich music’s tongue\n" +
|
|
|
|
|
"Unfold the imagined happiness that both\n" +
|
|
|
|
|
"Receive in either by this dear encounter.";
|
|
|
|
|
|
|
|
|
|
public EncryptDecryptTest() {
|
|
|
|
|
LOGGER.log(Level.INFO, "Plain Length: " + testMessage.getBytes(UTF8).length);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
@Test
|
2018-06-10 17:55:51 +02:00
|
|
|
|
public void freshKeysRsaToRsaTest()
|
2018-06-10 17:12:44 +02:00
|
|
|
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
|
|
|
|
IOException {
|
2018-06-21 15:35:40 +02:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._4096);
|
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._4096);
|
2018-06-10 17:12:44 +02:00
|
|
|
|
|
2018-06-10 17:55:51 +02:00
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-06-10 17:55:51 +02:00
|
|
|
|
public void freshKeysEcToEcTest() throws IOException, PGPException, NoSuchAlgorithmException, NoSuchProviderException,
|
2018-06-10 17:12:44 +02:00
|
|
|
|
InvalidAlgorithmParameterException {
|
2018-06-21 15:35:40 +02:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
|
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
|
2018-06-10 17:12:44 +02:00
|
|
|
|
|
2018-06-10 17:55:51 +02:00
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-06-10 17:55:51 +02:00
|
|
|
|
public void freshKeysEcToRsaTest()
|
2018-06-10 17:12:44 +02:00
|
|
|
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
|
|
|
|
IOException {
|
2018-06-21 15:35:40 +02:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleEcKeyRing("romeo@montague.lit");
|
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleRsaKeyRing("juliet@capulet.lit", RsaLength._4096);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:55:51 +02:00
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
2018-06-10 17:12:44 +02:00
|
|
|
|
}
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
@Test
|
2018-06-10 17:55:51 +02:00
|
|
|
|
public void freshKeysRsaToEcTest()
|
2018-06-10 17:12:44 +02:00
|
|
|
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException,
|
|
|
|
|
IOException {
|
2018-06-21 15:35:40 +02:00
|
|
|
|
PGPSecretKeyRing sender = PGPainless.generateKeyRing().simpleRsaKeyRing("romeo@montague.lit", RsaLength._4096);
|
|
|
|
|
PGPSecretKeyRing recipient = PGPainless.generateKeyRing().simpleEcKeyRing("juliet@capulet.lit");
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:55:51 +02:00
|
|
|
|
encryptDecryptForSecretKeyRings(sender, recipient);
|
2018-06-10 17:12:44 +02:00
|
|
|
|
}
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
@Ignore
|
2018-06-10 17:55:51 +02:00
|
|
|
|
private void encryptDecryptForSecretKeyRings(PGPSecretKeyRing sender, PGPSecretKeyRing recipient)
|
2018-06-10 17:12:44 +02:00
|
|
|
|
throws PGPException,
|
|
|
|
|
IOException {
|
2018-06-10 17:55:51 +02:00
|
|
|
|
PGPPublicKeyRing recipientPub = BCUtil.publicKeyRingFromSecretKeyRing(recipient);
|
|
|
|
|
PGPPublicKeyRing senderPub = BCUtil.publicKeyRingFromSecretKeyRing(sender);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
SecretKeyRingProtector keyDecryptor = new UnprotectedKeysProtector();
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-21 15:35:40 +02:00
|
|
|
|
byte[] secretMessage = testMessage.getBytes(UTF8);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
ByteArrayOutputStream envelope = new ByteArrayOutputStream();
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
OutputStream encryptor = PGPainless.createEncryptor()
|
|
|
|
|
.onOutputStream(envelope)
|
2018-06-11 01:33:49 +02:00
|
|
|
|
.toRecipients(recipientPub)
|
2018-06-10 17:12:44 +02:00
|
|
|
|
.usingSecureAlgorithms()
|
2018-06-11 01:33:49 +02:00
|
|
|
|
.signWith(keyDecryptor, sender)
|
2018-06-10 17:12:44 +02:00
|
|
|
|
.noArmor();
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
|
|
|
|
|
encryptor.close();
|
|
|
|
|
byte[] encryptedSecretMessage = envelope.toByteArray();
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-13 17:26:48 +02:00
|
|
|
|
LOGGER.log(Level.INFO, "Sender: " + PublicKeyAlgorithm.fromId(sender.getPublicKey().getAlgorithm()) +
|
|
|
|
|
" Receiver: " + PublicKeyAlgorithm.fromId(recipient.getPublicKey().getAlgorithm()) +
|
|
|
|
|
" Encrypted Length: " + encryptedSecretMessage.length);
|
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
// Juliet trieth to comprehend Romeos words
|
2018-06-07 18:12:13 +02:00
|
|
|
|
|
2018-06-10 17:12:44 +02:00
|
|
|
|
ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage);
|
2018-06-11 01:33:49 +02:00
|
|
|
|
DecryptionStream decryptor = PGPainless.createDecryptor()
|
2018-06-10 17:12:44 +02:00
|
|
|
|
.onInputStream(envelopeIn)
|
2018-06-10 17:55:51 +02:00
|
|
|
|
.decryptWith(BCUtil.keyRingsToKeyRingCollection(recipient), keyDecryptor)
|
2018-06-21 15:35:40 +02:00
|
|
|
|
.verifyWith(Collections.singleton(senderPub.getPublicKey().getKeyID()),
|
|
|
|
|
BCUtil.keyRingsToKeyRingCollection(senderPub))
|
2018-06-07 18:12:13 +02:00
|
|
|
|
.ignoreMissingPublicKeys()
|
|
|
|
|
.build();
|
|
|
|
|
|
2018-06-21 15:35:40 +02:00
|
|
|
|
ByteArrayOutputStream decryptedSecretMessage = new ByteArrayOutputStream();
|
2018-06-10 17:12:44 +02:00
|
|
|
|
|
|
|
|
|
Streams.pipeAll(decryptor, decryptedSecretMessage);
|
2018-06-07 18:12:13 +02:00
|
|
|
|
decryptor.close();
|
|
|
|
|
|
2018-06-21 15:35:40 +02:00
|
|
|
|
assertTrue(Arrays.equals(secretMessage, decryptedSecretMessage.toByteArray()));
|
2018-06-11 01:33:49 +02:00
|
|
|
|
PainlessResult result = decryptor.getResult();
|
2018-06-10 17:55:51 +02:00
|
|
|
|
assertTrue(result.containsVerifiedSignatureFrom(senderPub));
|
|
|
|
|
assertTrue(result.isIntegrityProtected());
|
|
|
|
|
assertTrue(result.isSigned());
|
|
|
|
|
assertTrue(result.isEncrypted());
|
|
|
|
|
assertTrue(result.isVerified());
|
2018-06-07 18:12:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|