Search in sources :

Example 6 with CryptoFailedException

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

the class OmemoService method decryptStanza.

/**
 * Decrypt the OmemoElement inside the given Stanza and return it.
 * Return null if something goes wrong.
 *
 * @param stanza stanza
 * @param managerGuard authenticated OmemoManager
 * @return decrypted OmemoMessage or null
 *
 * @throws IOException if an I/O error occurred.
 */
OmemoMessage.Received decryptStanza(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 null;
        }
        OmemoMessage.Received decrypted = null;
        BareJid sender;
        try {
            MultiUserChat muc = getMuc(manager.getConnection(), stanza.getFrom());
            if (muc != null) {
                Occupant occupant = muc.getOccupant(stanza.getFrom().asEntityFullJidIfPossible());
                Jid occupantJid = occupant.getJid();
                if (occupantJid == null) {
                    LOGGER.log(Level.WARNING, "MUC message received, but there is no way to retrieve the senders Jid. " + stanza.getFrom());
                    return null;
                }
                sender = occupantJid.asBareJid();
                // try is for this
                decrypted = decryptMessage(managerGuard, sender, element);
            } else {
                sender = stanza.getFrom().asBareJid();
                // and this
                decrypted = decryptMessage(managerGuard, sender, element);
            }
            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);
        } 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);
            }
        }
        return decrypted;
    }
}
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 7 with CryptoFailedException

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

the class OmemoService method createRatchetUpdateElement.

/**
 * Create an empty OMEMO message, which is used to forward the ratchet of the recipient.
 * This message type is typically used to create stable sessions.
 * Note that trust decisions are ignored for the creation of this message.
 *
 * @param managerGuard Logged in OmemoManager
 * @param contactsDevice OmemoDevice of the contact
 * @return ratchet update message
 *
 * @throws NoSuchAlgorithmException if AES algorithms are not supported on this system.
 * @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.
 * @throws SmackException.NotConnectedException if the XMPP connection is not connected.
 * @throws CannotEstablishOmemoSessionException if session negotiation fails.
 * @throws IOException if an I/O error occurred.
 */
OmemoElement createRatchetUpdateElement(OmemoManager.LoggedInOmemoManager managerGuard, OmemoDevice contactsDevice) throws InterruptedException, SmackException.NoResponseException, CorruptedOmemoKeyException, SmackException.NotConnectedException, CannotEstablishOmemoSessionException, NoSuchAlgorithmException, CryptoFailedException, IOException {
    OmemoManager manager = managerGuard.get();
    OmemoDevice userDevice = manager.getOwnDevice();
    if (contactsDevice.equals(userDevice)) {
        throw new IllegalArgumentException("\"Thou shall not update thy own ratchet!\" - William Shakespeare");
    }
    // Establish session if necessary
    if (!hasSession(userDevice, contactsDevice)) {
        buildFreshSessionWithDevice(manager.getConnection(), userDevice, contactsDevice);
    }
    // Generate fresh AES key and IV
    byte[] messageKey = OmemoMessageBuilder.generateKey(KEYTYPE, KEYLENGTH);
    byte[] iv = OmemoMessageBuilder.generateIv();
    // Create message builder
    OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> builder;
    try {
        builder = new OmemoMessageBuilder<>(userDevice, gullibleTrustCallback, getOmemoRatchet(manager), messageKey, iv, null);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
        throw new CryptoFailedException(e);
    }
    // Add recipient
    try {
        builder.addRecipient(contactsDevice);
    } catch (UndecidedOmemoIdentityException | UntrustedOmemoIdentityException e) {
        throw new AssertionError("Gullible Trust Callback reported undecided or untrusted device, " + "even though it MUST NOT do that.");
    } catch (NoIdentityKeyException e) {
        throw new AssertionError("We MUST have an identityKey for " + contactsDevice + " since we built a session." + e);
    }
    return builder.finish();
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoIdentityKeyException(org.jivesoftware.smackx.omemo.exceptions.NoIdentityKeyException) CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) InvalidKeyException(java.security.InvalidKeyException) UntrustedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UntrustedOmemoIdentityException)

Example 8 with CryptoFailedException

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

the class OmemoService method encrypt.

/**
 * Encrypt a message with a messageKey and an IV and create an OmemoMessage from it.
 *
 * @param managerGuard authenticated OmemoManager
 * @param contactsDevices set of recipient OmemoDevices
 * @param messageKey AES key to encrypt the message
 * @param iv iv to be used with the messageKey
 * @return OmemoMessage object which contains the OmemoElement and some information.
 *
 * @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 UndecidedOmemoIdentityException if the list of recipient devices contains undecided devices
 * @throws CryptoFailedException if we are lacking some crypto primitives
 * @throws IOException if an I/O error occurred.
 */
private OmemoMessage.Sent encrypt(OmemoManager.LoggedInOmemoManager managerGuard, Set<OmemoDevice> contactsDevices, byte[] messageKey, byte[] iv, String message) throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, UndecidedOmemoIdentityException, CryptoFailedException, IOException {
    OmemoManager manager = managerGuard.get();
    OmemoDevice userDevice = manager.getOwnDevice();
    // Do not encrypt for our own device.
    removeOurDevice(userDevice, contactsDevices);
    buildMissingSessionsWithDevices(manager.getConnection(), userDevice, contactsDevices);
    Set<OmemoDevice> undecidedDevices = getUndecidedDevices(userDevice, manager.getTrustCallback(), contactsDevices);
    if (!undecidedDevices.isEmpty()) {
        throw new UndecidedOmemoIdentityException(undecidedDevices);
    }
    // Keep track of skipped devices
    HashMap<OmemoDevice, Throwable> skippedRecipients = new HashMap<>();
    OmemoMessageBuilder<T_IdKeyPair, T_IdKey, T_PreKey, T_SigPreKey, T_Sess, T_Addr, T_ECPub, T_Bundle, T_Ciph> builder;
    try {
        builder = new OmemoMessageBuilder<>(userDevice, manager.getTrustCallback(), getOmemoRatchet(managerGuard.get()), messageKey, iv, message);
    } catch (BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException e) {
        throw new CryptoFailedException(e);
    }
    for (OmemoDevice contactsDevice : contactsDevices) {
        // Build missing sessions
        if (!hasSession(userDevice, contactsDevice)) {
            try {
                buildFreshSessionWithDevice(manager.getConnection(), userDevice, contactsDevice);
            } catch (CorruptedOmemoKeyException | CannotEstablishOmemoSessionException e) {
                LOGGER.log(Level.WARNING, "Could not build session with " + contactsDevice + ".", e);
                skippedRecipients.put(contactsDevice, e);
                continue;
            }
        }
        int messageCounter = omemoStore.loadOmemoMessageCounter(userDevice, contactsDevice);
        // Ignore read-only devices
        if (OmemoConfiguration.getIgnoreReadOnlyDevices()) {
            boolean readOnly = messageCounter >= OmemoConfiguration.getMaxReadOnlyMessageCount();
            if (readOnly) {
                LOGGER.log(Level.FINE, "Device " + contactsDevice + " seems to be read-only (We sent " + messageCounter + " messages without getting a reply back (max allowed is " + OmemoConfiguration.getMaxReadOnlyMessageCount() + "). Ignoring the device.");
                skippedRecipients.put(contactsDevice, new ReadOnlyDeviceException(contactsDevice));
                // Skip this device and handle next device
                continue;
            }
        }
        // Add recipients
        try {
            builder.addRecipient(contactsDevice);
        } catch (NoIdentityKeyException | CorruptedOmemoKeyException e) {
            LOGGER.log(Level.WARNING, "Encryption failed for device " + contactsDevice + ".", e);
            skippedRecipients.put(contactsDevice, e);
        } catch (UndecidedOmemoIdentityException e) {
            throw new AssertionError("Recipients device seems to be undecided, even though we should have thrown" + " an exception earlier in that case. " + e);
        } catch (UntrustedOmemoIdentityException e) {
            LOGGER.log(Level.WARNING, "Device " + contactsDevice + " is untrusted. Message is not encrypted for it.");
            skippedRecipients.put(contactsDevice, e);
        }
        // Increment the message counter of the device
        omemoStore.storeOmemoMessageCounter(userDevice, contactsDevice, messageCounter + 1);
    }
    OmemoElement element = builder.finish();
    return new OmemoMessage.Sent(element, messageKey, iv, contactsDevices, skippedRecipients);
}
Also used : HashMap(java.util.HashMap) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CorruptedOmemoKeyException(org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) NoIdentityKeyException(org.jivesoftware.smackx.omemo.exceptions.NoIdentityKeyException) ReadOnlyDeviceException(org.jivesoftware.smackx.omemo.exceptions.ReadOnlyDeviceException) CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) InvalidKeyException(java.security.InvalidKeyException) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) UntrustedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UntrustedOmemoIdentityException) OmemoElement(org.jivesoftware.smackx.omemo.element.OmemoElement)

Example 9 with CryptoFailedException

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

the class OmemoService method createOmemoMessage.

/**
 * Create an OmemoMessage.
 *
 * @param managerGuard initialized OmemoManager
 * @param contactsDevices set of recipient devices
 * @param message message we want to send
 * @return encrypted OmemoMessage
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws UndecidedOmemoIdentityException if the list of recipient devices contains an undecided device.
 * @throws CryptoFailedException if we are lacking some cryptographic algorithms
 * @throws SmackException.NotConnectedException if the XMPP connection is not connected.
 * @throws SmackException.NoResponseException if there was no response from the remote entity.
 * @throws IOException if an I/O error occurred.
 */
OmemoMessage.Sent createOmemoMessage(OmemoManager.LoggedInOmemoManager managerGuard, Set<OmemoDevice> contactsDevices, String message) throws InterruptedException, UndecidedOmemoIdentityException, CryptoFailedException, SmackException.NotConnectedException, SmackException.NoResponseException, IOException {
    byte[] key, iv;
    iv = OmemoMessageBuilder.generateIv();
    try {
        key = OmemoMessageBuilder.generateKey(KEYTYPE, KEYLENGTH);
    } catch (NoSuchAlgorithmException e) {
        throw new CryptoFailedException(e);
    }
    return encrypt(managerGuard, contactsDevices, key, iv, message);
}
Also used : CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 10 with CryptoFailedException

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

the class SignalOmemoRatchet method doubleRatchetDecrypt.

@Override
public byte[] doubleRatchetDecrypt(OmemoDevice sender, byte[] encryptedKey) throws CorruptedOmemoKeyException, NoRawSessionException, CryptoFailedException, UntrustedOmemoIdentityException, IOException {
    SessionCipher cipher = getCipher(sender);
    byte[] decryptedKey;
    // Try to handle the message as a PreKeySignalMessage...
    try {
        PreKeySignalMessage preKeyMessage = new PreKeySignalMessage(encryptedKey);
        if (!preKeyMessage.getPreKeyId().isPresent()) {
            throw new CryptoFailedException("PreKeyMessage did not contain a preKeyId.");
        }
        IdentityKey messageIdentityKey = preKeyMessage.getIdentityKey();
        IdentityKey previousIdentityKey = store.loadOmemoIdentityKey(storeConnector.getOurDevice(), sender);
        if (previousIdentityKey != null && !previousIdentityKey.getFingerprint().equals(messageIdentityKey.getFingerprint())) {
            throw new UntrustedOmemoIdentityException(sender, store.keyUtil().getFingerprintOfIdentityKey(previousIdentityKey), store.keyUtil().getFingerprintOfIdentityKey(messageIdentityKey));
        }
        try {
            decryptedKey = cipher.decrypt(preKeyMessage);
        } catch (UntrustedIdentityException e) {
            throw new AssertionError("Signals trust management MUST be disabled.");
        } catch (LegacyMessageException | InvalidKeyException e) {
            throw new CryptoFailedException(e);
        } catch (InvalidKeyIdException e) {
            throw new NoRawSessionException(sender, e);
        } catch (DuplicateMessageException e) {
            LOGGER.log(Level.INFO, "Decryption of PreKeyMessage from " + sender + " failed, since the message has been decrypted before.");
            return null;
        }
    } catch (InvalidVersionException | InvalidMessageException noPreKeyMessage) {
        // ...if that fails, handle it as a SignalMessage
        try {
            SignalMessage message = new SignalMessage(encryptedKey);
            decryptedKey = getCipher(sender).decrypt(message);
        } catch (UntrustedIdentityException e) {
            throw new AssertionError("Signals trust management MUST be disabled.");
        } catch (InvalidMessageException | NoSessionException e) {
            throw new NoRawSessionException(sender, e);
        } catch (LegacyMessageException e) {
            throw new CryptoFailedException(e);
        } catch (DuplicateMessageException e1) {
            LOGGER.log(Level.INFO, "Decryption of SignalMessage from " + sender + " failed, since the message has been decrypted before.");
            return null;
        }
    }
    return decryptedKey;
}
Also used : CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) IdentityKey(org.whispersystems.libsignal.IdentityKey) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) SignalMessage(org.whispersystems.libsignal.protocol.SignalMessage) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) NoRawSessionException(org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) DuplicateMessageException(org.whispersystems.libsignal.DuplicateMessageException) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) LegacyMessageException(org.whispersystems.libsignal.LegacyMessageException) SessionCipher(org.whispersystems.libsignal.SessionCipher) UntrustedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UntrustedOmemoIdentityException)

Aggregations

CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)11 CorruptedOmemoKeyException (org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException)7 OmemoDevice (org.jivesoftware.smackx.omemo.internal.OmemoDevice)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 OmemoElement (org.jivesoftware.smackx.omemo.element.OmemoElement)5 CannotEstablishOmemoSessionException (org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException)5 NoRawSessionException (org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException)5 UntrustedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UntrustedOmemoIdentityException)4 MessageOrOmemoMessage (org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage)4 BareJid (org.jxmpp.jid.BareJid)4 EntityBareJid (org.jxmpp.jid.EntityBareJid)4 ArrayList (java.util.ArrayList)3 OmemoBundleElement (org.jivesoftware.smackx.omemo.element.OmemoBundleElement)3 UndecidedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException)3 NotALeafNodeException (org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2