From 583dfce7c078d0c6959481fd0622e8b785cd6d24 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 8 May 2018 18:06:30 +0200 Subject: [PATCH] Include PGPainless and create encryption, decryption test --- smack-openpgp-bouncycastle/build.gradle | 4 +- .../ox/bouncycastle/BasicEncryptionTest.java | 110 ++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/BasicEncryptionTest.java diff --git a/smack-openpgp-bouncycastle/build.gradle b/smack-openpgp-bouncycastle/build.gradle index a30b82889..988ae4d77 100644 --- a/smack-openpgp-bouncycastle/build.gradle +++ b/smack-openpgp-bouncycastle/build.gradle @@ -6,7 +6,9 @@ Smack API for OpenPGP using Bouncycastle.""" dependencies { compile project(':smack-core') compile project(':smack-openpgp') - compile 'org.bouncycastle:bcpg-jdk15on:1.56' + compile 'org.bouncycastle:bcpg-jdk15on:1.59' + compile 'de.vanitasvitae.pgpainless:pgpainless:2.1.0' testCompile project(path: ":smack-core", configuration: "testRuntime") testCompile project(path: ":smack-core", configuration: "archives") + testCompile project(path: ":smack-openpgp", configuration: "testRuntime") } diff --git a/smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/BasicEncryptionTest.java b/smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/BasicEncryptionTest.java new file mode 100644 index 000000000..3c5e9d7d1 --- /dev/null +++ b/smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/BasicEncryptionTest.java @@ -0,0 +1,110 @@ +/** + * + * 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. + */ +package org.jivesoftware.smackx.ox.bouncycastle; + +import static junit.framework.TestCase.assertTrue; +import static org.jivesoftware.smackx.ox.TestKeys.JULIET_UID; +import static org.jivesoftware.smackx.ox.TestKeys.ROMEO_UID; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SignatureException; +import java.util.Arrays; +import java.util.Date; + +import org.jivesoftware.smack.test.util.SmackTestSuite; +import org.jivesoftware.smackx.ox.TestKeys; + +import name.neuhalfen.projects.crypto.bouncycastle.openpgp.BouncyGPG; +import name.neuhalfen.projects.crypto.bouncycastle.openpgp.algorithms.DefaultPGPAlgorithmSuites; +import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.KeyringConfigCallbacks; +import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.callbacks.Xep0373KeySelectionStrategy; +import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.InMemoryKeyring; +import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.KeyringConfig; +import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.keyrings.KeyringConfigs; +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.util.io.Streams; +import org.junit.Test; + +public class BasicEncryptionTest extends SmackTestSuite { + + private static final Charset UTF8 = Charset.forName("UTF-8"); + private final KeyringConfig keyringJuliet; + private final KeyringConfig keyringRomeo; + + public BasicEncryptionTest() throws IOException, PGPException { + super(); + + // Prepare Juliets keyring + keyringJuliet = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys()); + ((InMemoryKeyring) keyringJuliet).addSecretKey(TestKeys.JULIET_PRIV.getBytes(UTF8)); + ((InMemoryKeyring) keyringJuliet).addPublicKey(TestKeys.JULIET_PUB.getBytes(UTF8)); + ((InMemoryKeyring) keyringJuliet).addPublicKey(TestKeys.ROMEO_PUB.getBytes(UTF8)); + + // Prepare Romeos keyring + keyringRomeo = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys()); + ((InMemoryKeyring) keyringRomeo).addSecretKey(TestKeys.ROMEO_PRIV.getBytes(UTF8)); + ((InMemoryKeyring) keyringRomeo).addPublicKey(TestKeys.ROMEO_PUB.getBytes(UTF8)); + ((InMemoryKeyring) keyringRomeo).addPublicKey(TestKeys.JULIET_PUB.getBytes(UTF8)); + } + + @Test + public void encryptionTest() + throws IOException, PGPException, NoSuchAlgorithmException, SignatureException, NoSuchProviderException { + + + ByteArrayOutputStream result = new ByteArrayOutputStream(); + + byte[] message = "Hello World!!!!".getBytes(UTF8); + + // Encrypt + OutputStream out = BouncyGPG.encryptToStream() + .withConfig(keyringJuliet) + .withKeySelectionStrategy(new Xep0373KeySelectionStrategy(new Date())) + .withAlgorithms(DefaultPGPAlgorithmSuites.defaultSuiteForGnuPG()) + .toRecipients(ROMEO_UID, JULIET_UID) + .andSignWith(JULIET_UID) + .binaryOutput() + .andWriteTo(result); + + out.write(message); + out.close(); + + byte[] encrypted = result.toByteArray(); + + // Decrypt + ByteArrayInputStream encIn = new ByteArrayInputStream(encrypted); + InputStream in = BouncyGPG.decryptAndVerifyStream() + .withConfig(keyringRomeo) + .withKeySelectionStrategy(new Xep0373KeySelectionStrategy(new Date())) + .andRequireSignatureFromAllKeys(JULIET_UID) + .fromEncryptedInputStream(encIn); + + ByteArrayOutputStream decrypted = new ByteArrayOutputStream(); + Streams.pipeAll(in, decrypted); + + byte[] message2 = decrypted.toByteArray(); + + assertTrue(Arrays.equals(message, message2)); + } +}