Search in sources :

Example 1 with CannotEstablishOmemoSessionException

use of org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException in project Smack by igniterealtime.

the class OmemoService method onOmemoMessageStanzaReceived.

@Override
public void onOmemoMessageStanzaReceived(Stanza stanza, OmemoManager.LoggedInOmemoManager managerGuard) throws IOException {
    OmemoManager manager = managerGuard.get();
    // Avoid the ratchet being manipulated and the bundle being published multiple times simultaneously
    synchronized (manager) {
        OmemoDevice userDevice = manager.getOwnDevice();
        OmemoElement element = (OmemoElement) stanza.getExtensionElement(OmemoElement.NAME_ENCRYPTED, OmemoElement_VAxolotl.NAMESPACE);
        if (element == null) {
            return;
        }
        OmemoMessage.Received decrypted;
        BareJid sender;
        try {
            MultiUserChat muc = getMuc(manager.getConnection(), stanza.getFrom());
            if (muc != null) {
                Occupant occupant = muc.getOccupant(stanza.getFrom().asEntityFullJidIfPossible());
                if (occupant == null) {
                    LOGGER.log(Level.WARNING, "Cannot decrypt OMEMO MUC message; MUC Occupant is null.");
                    return;
                }
                Jid occupantJid = occupant.getJid();
                if (occupantJid == null) {
                    LOGGER.log(Level.WARNING, "Cannot decrypt OMEMO MUC message; Senders Jid is null. " + stanza.getFrom());
                    return;
                }
                sender = occupantJid.asBareJid();
                // try is for this
                decrypted = decryptMessage(managerGuard, sender, element);
                manager.notifyOmemoMucMessageReceived(muc, stanza, decrypted);
            } else {
                sender = stanza.getFrom().asBareJid();
                // and this
                decrypted = decryptMessage(managerGuard, sender, element);
                manager.notifyOmemoMessageReceived(stanza, decrypted);
            }
            if (decrypted.isPreKeyMessage() && OmemoConfiguration.getCompleteSessionWithEmptyMessage()) {
                LOGGER.log(Level.FINE, "Received a preKeyMessage from " + decrypted.getSenderDevice() + ".\n" + "Complete the session by sending an empty response message.");
                try {
                    sendRatchetUpdate(managerGuard, decrypted.getSenderDevice());
                } catch (CannotEstablishOmemoSessionException e) {
                    throw new AssertionError("Since we successfully received a message, we MUST be able to " + "establish a session. " + e);
                } catch (NoSuchAlgorithmException | InterruptedException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
                    LOGGER.log(Level.WARNING, "Cannot send a ratchet update message.", e);
                }
            }
        } catch (NoRawSessionException e) {
            OmemoDevice device = e.getDeviceWithoutSession();
            LOGGER.log(Level.WARNING, "No raw session found for contact " + device + ". ", e);
            if (OmemoConfiguration.getRepairBrokenSessionsWithPreKeyMessages()) {
                repairBrokenSessionWithPreKeyMessage(managerGuard, device);
            }
        } catch (CorruptedOmemoKeyException | CryptoFailedException e) {
            LOGGER.log(Level.WARNING, "Could not decrypt incoming message: ", e);
        }
        // Upload fresh bundle.
        if (getOmemoStoreBackend().loadOmemoPreKeys(userDevice).size() < OmemoConstants.PRE_KEY_COUNT_PER_BUNDLE) {
            LOGGER.log(Level.FINE, "We used up a preKey. Upload a fresh bundle.");
            try {
                getOmemoStoreBackend().replenishKeys(userDevice);
                OmemoBundleElement bundleElement = getOmemoStoreBackend().packOmemoBundle(userDevice);
                publishBundle(manager.getConnection(), userDevice, bundleElement);
            } catch (CorruptedOmemoKeyException | InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | NotALeafNodeException e) {
                LOGGER.log(Level.WARNING, "Could not republish replenished bundle.", e);
            }
        }
    }
}
Also used : CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) EntityBareJid(org.jxmpp.jid.EntityBareJid) Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) NotALeafNodeException(org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException) EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) Occupant(org.jivesoftware.smackx.muc.Occupant) NoRawSessionException(org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException) MessageOrOmemoMessage(org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) OmemoBundleElement(org.jivesoftware.smackx.omemo.element.OmemoBundleElement) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) CorruptedOmemoKeyException(org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException) OmemoElement(org.jivesoftware.smackx.omemo.element.OmemoElement)

Example 2 with CannotEstablishOmemoSessionException

use of org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException in project Smack by igniterealtime.

the class OmemoService method onOmemoCarbonCopyReceived.

@Override
public void onOmemoCarbonCopyReceived(CarbonExtension.Direction direction, Message carbonCopy, Message wrappingMessage, OmemoManager.LoggedInOmemoManager managerGuard) throws IOException {
    OmemoManager manager = managerGuard.get();
    // Avoid the ratchet being manipulated and the bundle being published multiple times simultaneously
    synchronized (manager) {
        OmemoDevice userDevice = manager.getOwnDevice();
        OmemoElement element = (OmemoElement) carbonCopy.getExtensionElement(OmemoElement.NAME_ENCRYPTED, OmemoElement_VAxolotl.NAMESPACE);
        if (element == null) {
            return;
        }
        OmemoMessage.Received decrypted;
        BareJid sender = carbonCopy.getFrom().asBareJid();
        try {
            decrypted = decryptMessage(managerGuard, sender, element);
            manager.notifyOmemoCarbonCopyReceived(direction, carbonCopy, wrappingMessage, decrypted);
            if (decrypted.isPreKeyMessage() && OmemoConfiguration.getCompleteSessionWithEmptyMessage()) {
                LOGGER.log(Level.FINE, "Received a preKeyMessage in a carbon copy from " + decrypted.getSenderDevice() + ".\n" + "Complete the session by sending an empty response message.");
                try {
                    sendRatchetUpdate(managerGuard, decrypted.getSenderDevice());
                } catch (CannotEstablishOmemoSessionException e) {
                    throw new AssertionError("Since we successfully received a message, we MUST be able to " + "establish a session. " + e);
                } catch (NoSuchAlgorithmException | InterruptedException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
                    LOGGER.log(Level.WARNING, "Cannot send a ratchet update message.", e);
                }
            }
        } catch (NoRawSessionException e) {
            OmemoDevice device = e.getDeviceWithoutSession();
            LOGGER.log(Level.WARNING, "No raw session found for contact " + device + ". ", e);
            if (OmemoConfiguration.getRepairBrokenSessionsWithPreKeyMessages()) {
                repairBrokenSessionWithPreKeyMessage(managerGuard, device);
            }
        } catch (CorruptedOmemoKeyException | CryptoFailedException e) {
            LOGGER.log(Level.WARNING, "Could not decrypt incoming carbon copy: ", e);
        }
        // Upload fresh bundle.
        if (getOmemoStoreBackend().loadOmemoPreKeys(userDevice).size() < OmemoConstants.PRE_KEY_COUNT_PER_BUNDLE) {
            LOGGER.log(Level.FINE, "We used up a preKey. Upload a fresh bundle.");
            try {
                getOmemoStoreBackend().replenishKeys(userDevice);
                OmemoBundleElement bundleElement = getOmemoStoreBackend().packOmemoBundle(userDevice);
                publishBundle(manager.getConnection(), userDevice, bundleElement);
            } catch (CorruptedOmemoKeyException | InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException | NotALeafNodeException e) {
                LOGGER.log(Level.WARNING, "Could not republish replenished bundle.", e);
            }
        }
    }
}
Also used : CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) NotALeafNodeException(org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException) EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) NoRawSessionException(org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException) MessageOrOmemoMessage(org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) OmemoBundleElement(org.jivesoftware.smackx.omemo.element.OmemoBundleElement) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) CorruptedOmemoKeyException(org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException) OmemoElement(org.jivesoftware.smackx.omemo.element.OmemoElement)

Example 3 with CannotEstablishOmemoSessionException

use of org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException in project Smack by igniterealtime.

the class OmemoService method buildFreshSessionWithDevice.

/**
 * Fetch the bundle of a contact and build a fresh OMEMO session with the contacts device.
 * Note that this builds a fresh session, regardless if we have had a session before or not.
 *
 * @param connection authenticated XMPP connection
 * @param userDevice our OmemoDevice
 * @param contactsDevice OmemoDevice of a contact.
 *
 * @throws CannotEstablishOmemoSessionException if we cannot establish a session (because of missing bundle etc.)
 * @throws SmackException.NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException.NoResponseException if there was no response from the remote entity.
 * @throws CorruptedOmemoKeyException if our IdentityKeyPair is corrupted.
 */
void buildFreshSessionWithDevice(XMPPConnection connection, OmemoDevice userDevice, OmemoDevice contactsDevice) throws CannotEstablishOmemoSessionException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, CorruptedOmemoKeyException {
    if (contactsDevice.equals(userDevice)) {
        // Do not build a session with yourself.
        return;
    }
    OmemoBundleElement bundleElement;
    try {
        bundleElement = fetchBundle(connection, contactsDevice);
    } catch (XMPPException.XMPPErrorException | PubSubException.NotALeafNodeException | PubSubException.NotAPubSubNodeException e) {
        throw new CannotEstablishOmemoSessionException(contactsDevice, e);
    }
    // Select random Bundle
    HashMap<Integer, T_Bundle> bundlesList = getOmemoStoreBackend().keyUtil().BUNDLE.bundles(bundleElement, contactsDevice);
    int randomIndex = new Random().nextInt(bundlesList.size());
    T_Bundle randomPreKeyBundle = new ArrayList<>(bundlesList.values()).get(randomIndex);
    // build the session
    OmemoManager omemoManager = OmemoManager.getInstanceFor(connection, userDevice.getDeviceId());
    processBundle(omemoManager, randomPreKeyBundle, contactsDevice);
}
Also used : NotALeafNodeException(org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException) OmemoBundleElement(org.jivesoftware.smackx.omemo.element.OmemoBundleElement) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) Random(java.util.Random)

Example 4 with CannotEstablishOmemoSessionException

use of org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException in project Smack by igniterealtime.

the class OmemoExceptionsTest method cannotEstablishOmemoSessionExceptionTest.

@Test
public void cannotEstablishOmemoSessionExceptionTest() throws XmppStringprepException {
    OmemoDevice alice1 = new OmemoDevice(JidCreate.bareFrom("alice@server.tld"), 1234);
    OmemoDevice alice2 = new OmemoDevice(JidCreate.bareFrom("alice@server.tld"), 2345);
    OmemoDevice bob = new OmemoDevice(JidCreate.bareFrom("bob@server.tld"), 5678);
    CannotEstablishOmemoSessionException c = new CannotEstablishOmemoSessionException(alice1, null);
    assertEquals(1, c.getFailures().size());
    assertTrue(c.getFailures().containsKey(alice1.getJid()));
    c.addSuccess(alice2);
    assertFalse(c.requiresThrowing());
    c.addFailures(new CannotEstablishOmemoSessionException(bob, null));
    assertTrue(c.requiresThrowing());
    assertEquals(1, c.getSuccesses().size());
    assertEquals(2, c.getFailures().size());
    c.getSuccesses().remove(alice2.getJid());
    c.addFailures(new CannotEstablishOmemoSessionException(alice2, null));
    assertEquals(2, c.getFailures().size());
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) Test(org.junit.Test)

Example 5 with CannotEstablishOmemoSessionException

use of org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException in project Zom-Android by zom.

the class Omemo method getFingerprints.

public ArrayList<String> getFingerprints(BareJid jid, boolean autoload) throws CorruptedOmemoKeyException, SmackException, XMPPException.XMPPErrorException, InterruptedException {
    try {
        mOmemoManager.buildSessionsWith(jid);
    } catch (CannotEstablishOmemoSessionException e) {
        debug(TAG, "couldn't establish omemo session: " + e);
    }
    CachedDeviceList list = mOmemoStore.loadCachedDeviceList(mOmemoManager, jid);
    if (list == null) {
        list = new CachedDeviceList();
    }
    ArrayList<String> fps = new ArrayList<>();
    for (int id : list.getActiveDevices()) {
        OmemoDevice d = new OmemoDevice(jid, id);
        IdentityKey idk = (IdentityKey) mOmemoStore.loadOmemoIdentityKey(mOmemoManager, d);
        if (idk == null) {
            System.out.println("No identityKey for " + d);
        } else {
            fps.add(mOmemoStore.keyUtil().getFingerprint(idk).toString());
        }
    }
    return fps;
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) ArrayList(java.util.ArrayList) CachedDeviceList(org.jivesoftware.smackx.omemo.internal.CachedDeviceList) OmemoFingerprint(org.jivesoftware.smackx.omemo.OmemoFingerprint) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException)

Aggregations

CannotEstablishOmemoSessionException (org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException)9 OmemoDevice (org.jivesoftware.smackx.omemo.internal.OmemoDevice)8 CorruptedOmemoKeyException (org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException)6 CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 OmemoBundleElement (org.jivesoftware.smackx.omemo.element.OmemoBundleElement)4 OmemoElement (org.jivesoftware.smackx.omemo.element.OmemoElement)4 NotALeafNodeException (org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException)4 BareJid (org.jxmpp.jid.BareJid)4 EntityBareJid (org.jxmpp.jid.EntityBareJid)4 NoRawSessionException (org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException)3 OmemoFingerprint (org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)3 MessageOrOmemoMessage (org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage)3 MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)2 Occupant (org.jivesoftware.smackx.muc.Occupant)2 UndecidedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException)2 Jid (org.jxmpp.jid.Jid)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 ArrayList (java.util.ArrayList)1