mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-22 20:12:07 +01:00
Merge pull request #397 from vanitasvitae/oxProviderImprov
Remove constructor dependency on XMPPConnection from OpenPgpProvider
This commit is contained in:
commit
6266f04682
8 changed files with 21 additions and 19 deletions
|
@ -122,7 +122,7 @@ public class OXSecretKeyBackupIntegrationTest extends AbstractOpenPgpIntegration
|
||||||
|
|
||||||
OpenPgpStore beforeStore = new FileBasedOpenPgpStore(beforePath);
|
OpenPgpStore beforeStore = new FileBasedOpenPgpStore(beforePath);
|
||||||
beforeStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
beforeStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
||||||
PainlessOpenPgpProvider beforeProvider = new PainlessOpenPgpProvider(aliceConnection, beforeStore);
|
PainlessOpenPgpProvider beforeProvider = new PainlessOpenPgpProvider(beforeStore);
|
||||||
openPgpManager = OpenPgpManager.getInstanceFor(aliceConnection);
|
openPgpManager = OpenPgpManager.getInstanceFor(aliceConnection);
|
||||||
openPgpManager.setOpenPgpProvider(beforeProvider);
|
openPgpManager.setOpenPgpProvider(beforeProvider);
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ public class OXSecretKeyBackupIntegrationTest extends AbstractOpenPgpIntegration
|
||||||
|
|
||||||
FileBasedOpenPgpStore afterStore = new FileBasedOpenPgpStore(afterPath);
|
FileBasedOpenPgpStore afterStore = new FileBasedOpenPgpStore(afterPath);
|
||||||
afterStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
afterStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
||||||
PainlessOpenPgpProvider afterProvider = new PainlessOpenPgpProvider(aliceConnection, afterStore);
|
PainlessOpenPgpProvider afterProvider = new PainlessOpenPgpProvider(afterStore);
|
||||||
openPgpManager.setOpenPgpProvider(afterProvider);
|
openPgpManager.setOpenPgpProvider(afterProvider);
|
||||||
|
|
||||||
OpenPgpV4Fingerprint fingerprint = openPgpManager.restoreSecretKeyServerBackup(new AskForBackupCodeCallback() {
|
OpenPgpV4Fingerprint fingerprint = openPgpManager.restoreSecretKeyServerBackup(new AskForBackupCodeCallback() {
|
||||||
|
|
|
@ -114,8 +114,8 @@ public class OXInstantMessagingIntegrationTest extends AbstractOpenPgpIntegratio
|
||||||
FileBasedOpenPgpStore bobStore = new FileBasedOpenPgpStore(bobStorePath);
|
FileBasedOpenPgpStore bobStore = new FileBasedOpenPgpStore(bobStorePath);
|
||||||
bobStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
bobStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
||||||
|
|
||||||
PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceConnection, aliceStore);
|
PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceStore);
|
||||||
PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobConnection, bobStore);
|
PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobStore);
|
||||||
|
|
||||||
aliceOpenPgp = OpenPgpManager.getInstanceFor(aliceConnection);
|
aliceOpenPgp = OpenPgpManager.getInstanceFor(aliceConnection);
|
||||||
bobOpenPgp = OpenPgpManager.getInstanceFor(bobConnection);
|
bobOpenPgp = OpenPgpManager.getInstanceFor(bobConnection);
|
||||||
|
|
|
@ -520,7 +520,7 @@ public final class OpenPgpManager extends Manager {
|
||||||
*/
|
*/
|
||||||
public OpenPgpMessage decryptOpenPgpElement(OpenPgpElement element, OpenPgpContact sender)
|
public OpenPgpMessage decryptOpenPgpElement(OpenPgpElement element, OpenPgpContact sender)
|
||||||
throws SmackException.NotLoggedInException, IOException, PGPException {
|
throws SmackException.NotLoggedInException, IOException, PGPException {
|
||||||
return provider.decryptAndOrVerify(element, getOpenPgpSelf(), sender);
|
return provider.decryptAndOrVerify(getAuthenticatedConnectionOrThrow(), element, getOpenPgpSelf(), sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void incomingChatMessageListener(final EntityBareJid from, final Message message, Chat chat) {
|
private void incomingChatMessageListener(final EntityBareJid from, final Message message, Chat chat) {
|
||||||
|
|
|
@ -19,6 +19,7 @@ package org.jivesoftware.smackx.ox.crypto;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
import org.jivesoftware.smackx.ox.OpenPgpContact;
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpMessage;
|
import org.jivesoftware.smackx.ox.OpenPgpMessage;
|
||||||
import org.jivesoftware.smackx.ox.OpenPgpSelf;
|
import org.jivesoftware.smackx.ox.OpenPgpSelf;
|
||||||
|
@ -96,12 +97,13 @@ public interface OpenPgpProvider {
|
||||||
* @param element signed and or encrypted {@link OpenPgpElement}.
|
* @param element signed and or encrypted {@link OpenPgpElement}.
|
||||||
* @param self our OpenPGP identity.
|
* @param self our OpenPGP identity.
|
||||||
* @param sender OpenPGP identity of the sender.
|
* @param sender OpenPGP identity of the sender.
|
||||||
|
* @param connection XMPP connection used to fetch any missing keys.
|
||||||
*
|
*
|
||||||
* @return decrypted message as {@link OpenPgpMessage}.
|
* @return decrypted message as {@link OpenPgpMessage}.
|
||||||
*
|
*
|
||||||
* @throws IOException IO is dangerous
|
* @throws IOException IO is dangerous
|
||||||
* @throws PGPException PGP is brittle
|
* @throws PGPException PGP is brittle
|
||||||
*/
|
*/
|
||||||
OpenPgpMessage decryptAndOrVerify(OpenPgpElement element, OpenPgpSelf self, OpenPgpContact sender)
|
OpenPgpMessage decryptAndOrVerify(XMPPConnection connection, OpenPgpElement element, OpenPgpSelf self, OpenPgpContact sender)
|
||||||
throws IOException, PGPException;
|
throws IOException, PGPException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,11 +51,9 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
||||||
|
|
||||||
private static final Logger LOGGER = Logger.getLogger(PainlessOpenPgpProvider.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(PainlessOpenPgpProvider.class.getName());
|
||||||
|
|
||||||
private final XMPPConnection connection;
|
|
||||||
private final OpenPgpStore store;
|
private final OpenPgpStore store;
|
||||||
|
|
||||||
public PainlessOpenPgpProvider(XMPPConnection connection, OpenPgpStore store) {
|
public PainlessOpenPgpProvider(OpenPgpStore store) {
|
||||||
this.connection = Objects.requireNonNull(connection);
|
|
||||||
this.store = Objects.requireNonNull(store);
|
this.store = Objects.requireNonNull(store);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +156,7 @@ public class PainlessOpenPgpProvider implements OpenPgpProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OpenPgpMessage decryptAndOrVerify(OpenPgpElement element, final OpenPgpSelf self, final OpenPgpContact sender) throws IOException, PGPException {
|
public OpenPgpMessage decryptAndOrVerify(XMPPConnection connection, OpenPgpElement element, final OpenPgpSelf self, final OpenPgpContact sender) throws IOException, PGPException {
|
||||||
ByteArrayOutputStream plainText = new ByteArrayOutputStream();
|
ByteArrayOutputStream plainText = new ByteArrayOutputStream();
|
||||||
InputStream cipherText = element.toInputStream();
|
InputStream cipherText = element.toInputStream();
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.jivesoftware.smack.DummyConnection;
|
import org.jivesoftware.smack.DummyConnection;
|
||||||
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||||
|
@ -82,8 +83,10 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite {
|
||||||
aliceStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
aliceStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
||||||
bobStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
bobStore.setKeyRingProtector(new UnprotectedKeysProtector());
|
||||||
|
|
||||||
PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(new DummyConnection(), aliceStore);
|
XMPPConnection bobConnection = new DummyConnection();
|
||||||
PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(new DummyConnection(), bobStore);
|
|
||||||
|
PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceStore);
|
||||||
|
PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobStore);
|
||||||
|
|
||||||
PGPKeyRing aliceKeys = aliceStore.generateKeyRing(alice);
|
PGPKeyRing aliceKeys = aliceStore.generateKeyRing(alice);
|
||||||
PGPKeyRing bobKeys = bobStore.generateKeyRing(bob);
|
PGPKeyRing bobKeys = bobStore.generateKeyRing(bob);
|
||||||
|
@ -134,7 +137,7 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite {
|
||||||
encrypted = aliceProvider.signAndEncrypt(signcryptElement, aliceSelf, Collections.singleton(bobForAlice));
|
encrypted = aliceProvider.signAndEncrypt(signcryptElement, aliceSelf, Collections.singleton(bobForAlice));
|
||||||
|
|
||||||
// Decrypt and Verify
|
// Decrypt and Verify
|
||||||
decrypted = bobProvider.decryptAndOrVerify(encrypted.getElement(), bobSelf, aliceForBob);
|
decrypted = bobProvider.decryptAndOrVerify(bobConnection, encrypted.getElement(), bobSelf, aliceForBob);
|
||||||
|
|
||||||
OpenPgpV4Fingerprint decryptionFingerprint = decrypted.getMetadata().getDecryptionFingerprint();
|
OpenPgpV4Fingerprint decryptionFingerprint = decrypted.getMetadata().getDecryptionFingerprint();
|
||||||
assertTrue(bobSelf.getSecretKeys().contains(decryptionFingerprint.getKeyId()));
|
assertTrue(bobSelf.getSecretKeys().contains(decryptionFingerprint.getKeyId()));
|
||||||
|
@ -154,7 +157,7 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite {
|
||||||
// Encrypt
|
// Encrypt
|
||||||
encrypted = aliceProvider.encrypt(cryptElement, aliceSelf, Collections.singleton(bobForAlice));
|
encrypted = aliceProvider.encrypt(cryptElement, aliceSelf, Collections.singleton(bobForAlice));
|
||||||
|
|
||||||
decrypted = bobProvider.decryptAndOrVerify(encrypted.getElement(), bobSelf, aliceForBob);
|
decrypted = bobProvider.decryptAndOrVerify(bobConnection, encrypted.getElement(), bobSelf, aliceForBob);
|
||||||
|
|
||||||
decryptionFingerprint = decrypted.getMetadata().getDecryptionFingerprint();
|
decryptionFingerprint = decrypted.getMetadata().getDecryptionFingerprint();
|
||||||
assertTrue(bobSelf.getSecretKeys().contains(decryptionFingerprint.getKeyId()));
|
assertTrue(bobSelf.getSecretKeys().contains(decryptionFingerprint.getKeyId()));
|
||||||
|
@ -174,7 +177,7 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite {
|
||||||
// Sign
|
// Sign
|
||||||
encrypted = aliceProvider.sign(signElement, aliceSelf);
|
encrypted = aliceProvider.sign(signElement, aliceSelf);
|
||||||
|
|
||||||
decrypted = bobProvider.decryptAndOrVerify(encrypted.getElement(), bobSelf, aliceForBob);
|
decrypted = bobProvider.decryptAndOrVerify(bobConnection, encrypted.getElement(), bobSelf, aliceForBob);
|
||||||
|
|
||||||
assertNull(decrypted.getMetadata().getDecryptionFingerprint());
|
assertNull(decrypted.getMetadata().getDecryptionFingerprint());
|
||||||
assertTrue(decrypted.getMetadata().getVerifiedSignatureKeyFingerprints().contains(aliceFingerprint));
|
assertTrue(decrypted.getMetadata().getVerifiedSignatureKeyFingerprints().contains(aliceFingerprint));
|
||||||
|
|
|
@ -27,7 +27,6 @@ import java.security.NoSuchProviderException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.jivesoftware.smack.DummyConnection;
|
|
||||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||||
|
|
||||||
import org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider;
|
import org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider;
|
||||||
|
@ -79,7 +78,7 @@ public class SecretKeyBackupHelperTest extends SmackTestSuite {
|
||||||
|
|
||||||
// Prepare store and provider and so on...
|
// Prepare store and provider and so on...
|
||||||
FileBasedOpenPgpStore store = new FileBasedOpenPgpStore(basePath);
|
FileBasedOpenPgpStore store = new FileBasedOpenPgpStore(basePath);
|
||||||
PainlessOpenPgpProvider provider = new PainlessOpenPgpProvider(new DummyConnection(), store);
|
PainlessOpenPgpProvider provider = new PainlessOpenPgpProvider(store);
|
||||||
|
|
||||||
// Generate and import key
|
// Generate and import key
|
||||||
PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:alice@wonderland.lit");
|
PGPKeyRing keyRing = PGPainless.generateKeyRing().simpleEcKeyRing("xmpp:alice@wonderland.lit");
|
||||||
|
|
|
@ -79,8 +79,8 @@ public class OXInstantMessagingManagerTest extends SmackTestSuite {
|
||||||
FileBasedOpenPgpStore aliceStore = new FileBasedOpenPgpStore(new File(basePath, "alice"));
|
FileBasedOpenPgpStore aliceStore = new FileBasedOpenPgpStore(new File(basePath, "alice"));
|
||||||
FileBasedOpenPgpStore bobStore = new FileBasedOpenPgpStore(new File(basePath, "bob"));
|
FileBasedOpenPgpStore bobStore = new FileBasedOpenPgpStore(new File(basePath, "bob"));
|
||||||
|
|
||||||
PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceCon, aliceStore);
|
PainlessOpenPgpProvider aliceProvider = new PainlessOpenPgpProvider(aliceStore);
|
||||||
PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobCon, bobStore);
|
PainlessOpenPgpProvider bobProvider = new PainlessOpenPgpProvider(bobStore);
|
||||||
|
|
||||||
OpenPgpManager aliceOpenPgp = OpenPgpManager.getInstanceFor(aliceCon);
|
OpenPgpManager aliceOpenPgp = OpenPgpManager.getInstanceFor(aliceCon);
|
||||||
OpenPgpManager bobOpenPgp = OpenPgpManager.getInstanceFor(bobCon);
|
OpenPgpManager bobOpenPgp = OpenPgpManager.getInstanceFor(bobCon);
|
||||||
|
|
Loading…
Reference in a new issue