Search in sources :

Example 6 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.

the class OmemoService method decryptMessage.

/**
 * Decrypt an OMEMO message.
 *
 * @param managerGuard authenticated OmemoManager.
 * @param senderJid BareJid of the sender.
 * @param omemoElement omemoElement.
 * @return decrypted OmemoMessage object.
 *
 * @throws CorruptedOmemoKeyException if the identityKey of the sender is damaged.
 * @throws CryptoFailedException if decryption fails.
 * @throws NoRawSessionException if we have no session with the device and it sent a normal (non-preKey) message.
 * @throws IOException if an I/O error occurred.
 */
OmemoMessage.Received decryptMessage(OmemoManager.LoggedInOmemoManager managerGuard, BareJid senderJid, OmemoElement omemoElement) throws CorruptedOmemoKeyException, CryptoFailedException, NoRawSessionException, IOException {
    OmemoManager manager = managerGuard.get();
    int senderId = omemoElement.getHeader().getSid();
    OmemoDevice senderDevice = new OmemoDevice(senderJid, senderId);
    CipherAndAuthTag cipherAndAuthTag = getOmemoRatchet(manager).retrieveMessageKeyAndAuthTag(senderDevice, omemoElement);
    // Retrieve senders fingerprint.
    OmemoFingerprint senderFingerprint;
    try {
        senderFingerprint = getOmemoStoreBackend().getFingerprint(manager.getOwnDevice(), senderDevice);
    } catch (NoIdentityKeyException e) {
        throw new AssertionError("Cannot retrieve OmemoFingerprint of sender although decryption was successful: " + e);
    }
    // Reset the message counter.
    omemoStore.storeOmemoMessageCounter(manager.getOwnDevice(), senderDevice, 0);
    if (omemoElement.isMessageElement()) {
        // Use symmetric message key to decrypt message payload.
        String plaintext = OmemoRatchet.decryptMessageElement(omemoElement, cipherAndAuthTag);
        return new OmemoMessage.Received(omemoElement, cipherAndAuthTag.getKey(), cipherAndAuthTag.getIv(), plaintext, senderFingerprint, senderDevice, cipherAndAuthTag.wasPreKeyEncrypted());
    } else {
        // KeyTransportMessages don't require decryption of the payload.
        return new OmemoMessage.Received(omemoElement, cipherAndAuthTag.getKey(), cipherAndAuthTag.getIv(), null, senderFingerprint, senderDevice, cipherAndAuthTag.wasPreKeyEncrypted());
    }
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) CipherAndAuthTag(org.jivesoftware.smackx.omemo.internal.CipherAndAuthTag) NoIdentityKeyException(org.jivesoftware.smackx.omemo.exceptions.NoIdentityKeyException) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)

Example 7 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.

the class OmemoManager method getDevicesOf.

/**
 * Return a set of all OMEMO capable devices of a contact.
 * Note, that this method does not explicitly refresh the device list of the contact, so it might be outdated.
 *
 * @see #requestDeviceListUpdateFor(BareJid)
 *
 * @param contact contact we want to get a set of device of.
 * @return set of known devices of that contact.
 *
 * @throws IOException if an I/O error occurred.
 */
public Set<OmemoDevice> getDevicesOf(BareJid contact) throws IOException {
    OmemoCachedDeviceList list = getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact);
    HashSet<OmemoDevice> devices = new HashSet<>();
    for (int deviceId : list.getActiveDevices()) {
        devices.add(new OmemoDevice(contact, deviceId));
    }
    return devices;
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) StoreHint(org.jivesoftware.smackx.hints.element.StoreHint) HashSet(java.util.HashSet)

Example 8 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.

the class FileBasedOmemoStore method loadAllRawSessionsOf.

@Override
public HashMap<Integer, T_Sess> loadAllRawSessionsOf(OmemoDevice userDevice, BareJid contact) throws IOException {
    File contactsDirectory = hierarchy.getContactsDir(userDevice, contact);
    HashMap<Integer, T_Sess> sessions = new HashMap<>();
    String[] devices = contactsDirectory.list();
    for (String deviceId : devices != null ? devices : new String[0]) {
        int id;
        try {
            id = Integer.parseInt(deviceId);
        } catch (NumberFormatException e) {
            continue;
        }
        OmemoDevice device = new OmemoDevice(contact, id);
        File session = hierarchy.getContactsSessionPath(userDevice, device);
        byte[] bytes = readBytes(session);
        if (bytes != null) {
            try {
                T_Sess s = keyUtil().rawSessionFromBytes(bytes);
                sessions.put(id, s);
            } catch (IOException e) {
                LOGGER.log(Level.WARNING, "Could not deserialize raw session.", e);
            }
        }
    }
    return sessions;
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) HashMap(java.util.HashMap) IOException(java.io.IOException) File(java.io.File)

Example 9 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.

the class FileBasedOmemoStore method removeAllRawSessionsOf.

@Override
public void removeAllRawSessionsOf(OmemoDevice userDevice, BareJid contact) {
    File contactsDirectory = hierarchy.getContactsDir(userDevice, contact);
    String[] devices = contactsDirectory.list();
    for (String deviceId : devices != null ? devices : new String[0]) {
        int id = Integer.parseInt(deviceId);
        OmemoDevice device = new OmemoDevice(contact, id);
        File session = hierarchy.getContactsSessionPath(userDevice, device);
        if (!session.delete()) {
            LOGGER.log(Level.WARNING, "Deleting raw OMEMO session " + session.getAbsolutePath() + "failed.");
        }
    }
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) File(java.io.File)

Example 10 with OmemoDevice

use of org.jivesoftware.smackx.omemo.internal.OmemoDevice in project Smack by igniterealtime.

the class OmemoStore method mergeCachedDeviceList.

/**
 * Merge the received OmemoDeviceListElement with the one we already have. If we had none, the received one is saved.
 *
 * @param userDevice our OmemoDevice.
 * @param contact Contact we received the list from.
 * @param list    List we received.
 *
 * @throws IOException if an I/O error occurred.
 */
OmemoCachedDeviceList mergeCachedDeviceList(OmemoDevice userDevice, BareJid contact, OmemoDeviceListElement list) throws IOException {
    OmemoCachedDeviceList cached = loadCachedDeviceList(userDevice, contact);
    if (cached == null) {
        cached = new OmemoCachedDeviceList();
    }
    if (list == null) {
        return cached;
    }
    for (int devId : list.getDeviceIds()) {
        if (!cached.contains(devId)) {
            setDateOfLastDeviceIdPublication(userDevice, new OmemoDevice(contact, devId), new Date());
        }
    }
    cached.merge(list.getDeviceIds());
    storeCachedDeviceList(userDevice, contact, cached);
    return cached;
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) Date(java.util.Date)

Aggregations

OmemoDevice (org.jivesoftware.smackx.omemo.internal.OmemoDevice)29 CannotEstablishOmemoSessionException (org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException)8 OmemoFingerprint (org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)8 CorruptedOmemoKeyException (org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException)6 CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)6 Test (org.junit.Test)6 BareJid (org.jxmpp.jid.BareJid)6 EntityBareJid (org.jxmpp.jid.EntityBareJid)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashMap (java.util.HashMap)4 OmemoBundleElement (org.jivesoftware.smackx.omemo.element.OmemoBundleElement)4 OmemoElement (org.jivesoftware.smackx.omemo.element.OmemoElement)4 UndecidedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException)4 OmemoCachedDeviceList (org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 OmemoFingerprint (org.jivesoftware.smackx.omemo.OmemoFingerprint)3 NoIdentityKeyException (org.jivesoftware.smackx.omemo.exceptions.NoIdentityKeyException)3 NoRawSessionException (org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException)3