From 7b56cc79e687eeaf35880d1d73dee45147673148 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 10 Jul 2018 14:48:40 +0200 Subject: [PATCH] Integration tests are working! --- ...penPgpInstantMessagingIntegrationTest.java | 4 ++-- ...SecretKeyBackupRestoreIntegrationTest.java | 4 ++-- .../smackx/ox/OpenPgpContact.java | 3 ++- .../ox/crypto/PainlessOpenPgpProvider.java | 22 ++++++++++++++++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/ox/BasicOpenPgpInstantMessagingIntegrationTest.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/ox/BasicOpenPgpInstantMessagingIntegrationTest.java index 468701719..b306493d8 100644 --- a/smack-integration-test/src/main/java/org/jivesoftware/smackx/ox/BasicOpenPgpInstantMessagingIntegrationTest.java +++ b/smack-integration-test/src/main/java/org/jivesoftware/smackx/ox/BasicOpenPgpInstantMessagingIntegrationTest.java @@ -75,8 +75,8 @@ public class BasicOpenPgpInstantMessagingIntegrationTest extends AbstractOpenPgp FileBasedOpenPgpStore bobStore = new FileBasedOpenPgpStore(bobStorePath); bobStore.setKeyRingProtector(new UnprotectedKeysProtector()); - PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceStore); - PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobStore); + PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceConnection, aliceStore); + PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobConnection, bobStore); OpenPgpManager aliceOpenPgp = OpenPgpManager.getInstanceFor(aliceConnection); OpenPgpManager bobOpenPgp = OpenPgpManager.getInstanceFor(bobConnection); diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/ox/SecretKeyBackupRestoreIntegrationTest.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/ox/SecretKeyBackupRestoreIntegrationTest.java index eb00edea1..9d9d76240 100644 --- a/smack-integration-test/src/main/java/org/jivesoftware/smackx/ox/SecretKeyBackupRestoreIntegrationTest.java +++ b/smack-integration-test/src/main/java/org/jivesoftware/smackx/ox/SecretKeyBackupRestoreIntegrationTest.java @@ -100,7 +100,7 @@ public class SecretKeyBackupRestoreIntegrationTest extends AbstractOpenPgpIntegr OpenPgpStore beforeStore = new FileBasedOpenPgpStore(beforePath); beforeStore.setKeyRingProtector(new UnprotectedKeysProtector()); - PainlessOpenPgpProvider beforeProvider = new PainlessOpenPgpProvider(beforeStore); + PainlessOpenPgpProvider beforeProvider = new PainlessOpenPgpProvider(aliceConnection, beforeStore); OpenPgpManager openPgpManager = OpenPgpManager.getInstanceFor(aliceConnection); openPgpManager.setOpenPgpProvider(beforeProvider); @@ -130,7 +130,7 @@ public class SecretKeyBackupRestoreIntegrationTest extends AbstractOpenPgpIntegr FileBasedOpenPgpStore afterStore = new FileBasedOpenPgpStore(afterPath); afterStore.setKeyRingProtector(new UnprotectedKeysProtector()); - PainlessOpenPgpProvider afterProvider = new PainlessOpenPgpProvider(afterStore); + PainlessOpenPgpProvider afterProvider = new PainlessOpenPgpProvider(aliceConnection, afterStore); openPgpManager.setOpenPgpProvider(afterProvider); self = openPgpManager.getOpenPgpSelf(); diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpContact.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpContact.java index c7cb13f4f..013bf2f96 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpContact.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpContact.java @@ -17,6 +17,7 @@ package org.jivesoftware.smackx.ox; import java.io.IOException; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -73,7 +74,7 @@ public class OpenPgpContact { BareJidUserId.PubRingSelectionStrategy userIdFilter = new BareJidUserId.PubRingSelectionStrategy(); AnnouncedKeys.PubKeyRingSelectionStrategy announcedFilter = new AnnouncedKeys.PubKeyRingSelectionStrategy(); - for (PGPPublicKeyRing ring : anyKeys) { + for (PGPPublicKeyRing ring : (anyKeys != null ? anyKeys : Collections.emptyList())) { if (!userIdFilter.accept(getJid(), ring)) { LOGGER.log(Level.WARNING, "Ignore key " + Long.toHexString(ring.getPublicKey().getKeyID()) + diff --git a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java index f52276dab..7d03afea7 100644 --- a/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java +++ b/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/crypto/PainlessOpenPgpProvider.java @@ -21,7 +21,12 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.util.stringencoder.Base64; import org.jivesoftware.smackx.ox.OpenPgpContact; import org.jivesoftware.smackx.ox.OpenPgpMessage; @@ -31,6 +36,7 @@ import org.jivesoftware.smackx.ox.element.OpenPgpElement; import org.jivesoftware.smackx.ox.element.SignElement; import org.jivesoftware.smackx.ox.element.SigncryptElement; import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore; +import org.jivesoftware.smackx.pubsub.PubSubException; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; @@ -42,9 +48,13 @@ import org.pgpainless.pgpainless.encryption_signing.EncryptionStream; public class PainlessOpenPgpProvider implements OpenPgpProvider { + private static final Logger LOGGER = Logger.getLogger(PainlessOpenPgpProvider.class.getName()); + + private final XMPPConnection connection; private final OpenPgpStore store; - public PainlessOpenPgpProvider(OpenPgpStore store) { + public PainlessOpenPgpProvider(XMPPConnection connection, OpenPgpStore store) { + this.connection = connection; this.store = store; } @@ -137,6 +147,16 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider { ByteArrayOutputStream plainText = new ByteArrayOutputStream(); InputStream cipherText = element.toInputStream(); + PGPPublicKeyRingCollection announcedPublicKeys = sender.getAnnouncedPublicKeys(); + if (announcedPublicKeys == null) { + try { + sender.updateKeys(connection); + announcedPublicKeys = sender.getAnnouncedPublicKeys(); + } catch (InterruptedException | PubSubException.NotAPubSubNodeException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException | SmackException.NoResponseException | SmackException.NotConnectedException e) { + LOGGER.log(Level.WARNING, "Error fetching keys of " + sender.getJid(), e); + } + } + DecryptionStream cipherStream = PGPainless.createDecryptor().onInputStream(cipherText) .decryptWith(store.getKeyRingProtector(), self.getSecretKeys()) .verifyWith(sender.getAnnouncedPublicKeys())