From e23cf88082b6bce535fd7fc7939f29b26ce08669 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 19 Jun 2018 16:41:02 +0200 Subject: [PATCH] Add test --- .../bouncycastle/PainlessOpenPgpProvider.java | 5 +- .../FileBasedPainlessOpenPgpStoreTest.java | 67 +++++++++++++++++++ .../smackx/ox/OXInstantMessagingManager.java | 17 +++-- .../jivesoftware/smackx/ox/OpenPgpStore.java | 2 + 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/FileBasedPainlessOpenPgpStoreTest.java diff --git a/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/PainlessOpenPgpProvider.java b/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/PainlessOpenPgpProvider.java index 4893aa718..803a70860 100644 --- a/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/PainlessOpenPgpProvider.java +++ b/smack-openpgp-bouncycastle/src/main/java/org/jivesoftware/smackx/ox/bouncycastle/PainlessOpenPgpProvider.java @@ -239,12 +239,11 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { } } - return decryptImpl(bytes, secretKeyRings, protector, trustedKeyIds, trustedKeys); + return decryptImpl(bytes, secretKeyRings, protector, trustedKeys); } DecryptedBytesAndMetadata decryptImpl(byte[] bytes, PGPSecretKeyRingCollection decryptionKeys, SecretKeyRingProtector protector, - Set trustedKeyIds, Set verificationKeys) throws SmackOpenPgpException, IOException { @@ -255,7 +254,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { fromEncrypted = PGPainless.createDecryptor() .onInputStream(encryptedBytes) .decryptWith(decryptionKeys, protector) - .verifyWith(trustedKeyIds, verificationKeys) + .verifyWith(verificationKeys) .ignoreMissingPublicKeys() .build(); } catch (IOException | PGPException e) { diff --git a/smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/FileBasedPainlessOpenPgpStoreTest.java b/smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/FileBasedPainlessOpenPgpStoreTest.java new file mode 100644 index 000000000..d2b98a8be --- /dev/null +++ b/smack-openpgp-bouncycastle/src/test/java/org/jivesoftware/smackx/ox/bouncycastle/FileBasedPainlessOpenPgpStoreTest.java @@ -0,0 +1,67 @@ +package org.jivesoftware.smackx.ox.bouncycastle; + +import static junit.framework.TestCase.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Security; +import java.util.Arrays; +import java.util.Collections; + +import org.jivesoftware.smack.test.util.SmackTestSuite; + +import de.vanitasvitae.crypto.pgpainless.PGPainless; +import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector; +import de.vanitasvitae.crypto.pgpainless.key.generation.type.length.RsaLength; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; +import org.junit.Test; +import org.jxmpp.jid.BareJid; +import org.jxmpp.jid.impl.JidCreate; +import org.jxmpp.stringprep.XmppStringprepException; + +public class FileBasedPainlessOpenPgpStoreTest extends SmackTestSuite { + + private static final File basePath; + private static final BareJid alice; + private static final BareJid bob; + + static { + String userHome = System.getProperty("user.home"); + if (userHome != null) { + File f = new File(userHome); + basePath = new File(f, ".config/smack-integration-test/ox/painless_ox_store"); + } else { + basePath = new File("painless_ox_store"); + } + + try { + alice = JidCreate.bareFrom("alice@wonderland.lit"); + bob = JidCreate.bareFrom("bob@builder.tv"); + } catch (XmppStringprepException e) { + throw new AssertionError(e); + } + + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + public void storeSecretKeyRingsTest() + throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, + IOException { + FileBasedPainlessOpenPgpStore store = new FileBasedPainlessOpenPgpStore(basePath, new UnprotectedKeysProtector()); + + PGPSecretKeyRing secretKey = PGPainless.generateKeyRing().simpleRsaKeyRing("xmpp:" + alice.toString(), RsaLength._3072); + PGPSecretKeyRingCollection saving = new PGPSecretKeyRingCollection(Collections.singleton(secretKey)); + store.storeSecretKeyRing(alice, saving); + + PGPSecretKeyRingCollection restored = store.getSecretKeyRings(alice); + + assertTrue(Arrays.equals(saving.getEncoded(), restored.getEncoded())); + } +} diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OXInstantMessagingManager.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OXInstantMessagingManager.java index 9370dfd70..ec5fc6c8b 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OXInstantMessagingManager.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OXInstantMessagingManager.java @@ -78,10 +78,12 @@ public final class OXInstantMessagingManager extends Manager { public static OXInstantMessagingManager getInstanceFor(XMPPConnection connection) { OXInstantMessagingManager manager = INSTANCES.get(connection); + if (manager == null) { manager = new OXInstantMessagingManager(connection); INSTANCES.put(connection, manager); } + return manager; } @@ -123,10 +125,17 @@ public final class OXInstantMessagingManager extends Manager { throws SmackOpenPgpException, InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { - OpenPgpFingerprints theirKeys = openPgpManager.determineContactsKeys(jid); - OpenPgpFingerprints ourKeys = openPgpManager.determineContactsKeys(connection().getUser().asBareJid()); - Chat chat = chatManager.chatWith(jid); - return new OpenPgpEncryptedChat(openPgpManager.getOpenPgpProvider(), chat, ourKeys, theirKeys); + OpenPgpEncryptedChat encryptedChat = chats.get(jid); + + if (encryptedChat == null) { + OpenPgpFingerprints theirKeys = openPgpManager.determineContactsKeys(jid); + OpenPgpFingerprints ourKeys = openPgpManager.determineContactsKeys(connection().getUser().asBareJid()); + Chat chat = chatManager.chatWith(jid); + encryptedChat = new OpenPgpEncryptedChat(openPgpManager.getOpenPgpProvider(), chat, ourKeys, theirKeys); + chats.put(jid, encryptedChat); + } + + return encryptedChat; } public boolean addOpenPgpEncryptedMessageListener(OpenPgpEncryptedMessageListener listener) { diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpStore.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpStore.java index 28d311c74..6ea5bd6d4 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpStore.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpStore.java @@ -53,6 +53,8 @@ public interface OpenPgpStore { * * @param owner owner. * @return set of fingerprints of available OpenPGP key pairs master keys. + * + * @throws SmackOpenPgpException */ Set getAvailableKeyPairFingerprints(BareJid owner) throws SmackOpenPgpException;