Search in sources :

Example 6 with OmemoFingerprint

use of org.jivesoftware.smackx.omemo.trust.OmemoFingerprint in project Smack by igniterealtime.

the class OmemoMessageBuilder method addRecipient.

/**
 * Add a new recipient device to the message.
 *
 * @param contactsDevice device of the recipient
 *
 * @throws NoIdentityKeyException if we have no identityKey of that device. Can be fixed by fetching and
 *                                processing the devices bundle.
 * @throws CorruptedOmemoKeyException if the identityKey of that device is corrupted.
 * @throws UndecidedOmemoIdentityException if the user hasn't yet decided whether to trust that device or not.
 * @throws UntrustedOmemoIdentityException if the user has decided not to trust that device.
 * @throws IOException if an I/O error occurred.
 */
public void addRecipient(OmemoDevice contactsDevice) throws NoIdentityKeyException, CorruptedOmemoKeyException, UndecidedOmemoIdentityException, UntrustedOmemoIdentityException, IOException {
    OmemoFingerprint fingerprint;
    fingerprint = OmemoService.getInstance().getOmemoStoreBackend().getFingerprint(userDevice, contactsDevice);
    switch(trustCallback.getTrust(contactsDevice, fingerprint)) {
        case undecided:
            throw new UndecidedOmemoIdentityException(contactsDevice);
        case trusted:
            CiphertextTuple encryptedKey = ratchet.doubleRatchetEncrypt(contactsDevice, messageKey);
            keys.add(new OmemoKeyElement(encryptedKey.getCiphertext(), contactsDevice.getDeviceId(), encryptedKey.isPreKeyMessage()));
            break;
        case untrusted:
            throw new UntrustedOmemoIdentityException(contactsDevice, fingerprint);
    }
}
Also used : UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) OmemoKeyElement(org.jivesoftware.smackx.omemo.element.OmemoKeyElement) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) UntrustedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UntrustedOmemoIdentityException) CiphertextTuple(org.jivesoftware.smackx.omemo.internal.CiphertextTuple)

Example 7 with OmemoFingerprint

use of org.jivesoftware.smackx.omemo.trust.OmemoFingerprint in project Smack by igniterealtime.

the class OmemoManager method getActiveFingerprints.

/**
 * Return all OmemoFingerprints of active devices of a contact.
 * TODO: Make more fail-safe
 *
 * @param contact contact
 * @return Map of all active devices of the contact and their fingerprints.
 *
 * @throws SmackException.NotLoggedInException if the XMPP connection is not authenticated.
 * @throws CorruptedOmemoKeyException if the OMEMO key is corrupted.
 * @throws CannotEstablishOmemoSessionException if no OMEMO session could be established.
 * @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 IOException if an I/O error occurred.
 */
public synchronized HashMap<OmemoDevice, OmemoFingerprint> getActiveFingerprints(BareJid contact) throws SmackException.NotLoggedInException, CorruptedOmemoKeyException, CannotEstablishOmemoSessionException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, IOException {
    if (getOwnJid() == null) {
        throw new SmackException.NotLoggedInException();
    }
    HashMap<OmemoDevice, OmemoFingerprint> fingerprints = new HashMap<>();
    OmemoCachedDeviceList deviceList = getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact);
    for (int id : deviceList.getActiveDevices()) {
        OmemoDevice device = new OmemoDevice(contact, id);
        OmemoFingerprint fingerprint = getFingerprint(device);
        if (fingerprint != null) {
            fingerprints.put(device, fingerprint);
        }
    }
    return fingerprints;
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) OmemoCachedDeviceList(org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint) StoreHint(org.jivesoftware.smackx.hints.element.StoreHint)

Example 8 with OmemoFingerprint

use of org.jivesoftware.smackx.omemo.trust.OmemoFingerprint in project Smack by igniterealtime.

the class OmemoClient method handleInput.

public void handleInput(String input) throws NotConnectedException, NotLoggedInException, InterruptedException, IOException {
    String[] com = input.split(" ", 3);
    switch(com[0]) {
        case "/omemo":
            if (com.length < 3) {
                print("Usage: /omemo <contact-jid> <message>");
                return;
            }
            BareJid recipient = JidCreate.bareFrom(com[1]);
            String body = com[2];
            MessageBuilder messageBuilder = connection.getStanzaFactory().buildMessageStanza();
            try {
                Message omemoMessage = omemoManager.encrypt(recipient, body).buildMessage(messageBuilder, recipient);
                connection.sendStanza(omemoMessage);
            } catch (UndecidedOmemoIdentityException e) {
                print("Undecided Identities!\n" + Arrays.toString(e.getUndecidedDevices().toArray()));
            } catch (CryptoFailedException | SmackException.NoResponseException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
            }
            break;
        case "/trust":
            print("Trust");
            if (com.length != 2) {
                print("Usage: /trust <contact-jid>");
            }
            BareJid contact = JidCreate.bareFrom(com[1]);
            HashMap<OmemoDevice, OmemoFingerprint> devices;
            try {
                devices = omemoManager.getActiveFingerprints(contact);
            } catch (CorruptedOmemoKeyException | CannotEstablishOmemoSessionException | SmackException.NoResponseException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
                return;
            }
            for (OmemoDevice d : devices.keySet()) {
                print("Trust (1) or distrust (2)?\n" + devices.get(d).blocksOf8Chars());
                if (Integer.parseInt(scanner.nextLine()) == 1) {
                    omemoManager.trustOmemoIdentity(d, devices.get(d));
                } else {
                    omemoManager.distrustOmemoIdentity(d, devices.get(d));
                }
            }
            print("Done.");
            break;
        case "/purge":
            try {
                omemoManager.purgeDeviceList();
                print("Purged.");
            } catch (XMPPException.XMPPErrorException | SmackException.NoResponseException | PubSubException.NotALeafNodeException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
            }
    }
}
Also used : CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) OmemoMessage(org.jivesoftware.smackx.omemo.OmemoMessage) Message(org.jivesoftware.smack.packet.Message) OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) CorruptedOmemoKeyException(org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)

Aggregations

OmemoFingerprint (org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)8 OmemoDevice (org.jivesoftware.smackx.omemo.internal.OmemoDevice)4 Test (org.junit.Test)3 UndecidedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException)2 HashMap (java.util.HashMap)1 WeakHashMap (java.util.WeakHashMap)1 Message (org.jivesoftware.smack.packet.Message)1 MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)1 Roster (org.jivesoftware.smack.roster.Roster)1 StoreHint (org.jivesoftware.smackx.hints.element.StoreHint)1 OmemoMessage (org.jivesoftware.smackx.omemo.OmemoMessage)1 OmemoKeyElement (org.jivesoftware.smackx.omemo.element.OmemoKeyElement)1 CannotEstablishOmemoSessionException (org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException)1 CorruptedOmemoKeyException (org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException)1 CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)1 NoIdentityKeyException (org.jivesoftware.smackx.omemo.exceptions.NoIdentityKeyException)1 UntrustedOmemoIdentityException (org.jivesoftware.smackx.omemo.exceptions.UntrustedOmemoIdentityException)1 CipherAndAuthTag (org.jivesoftware.smackx.omemo.internal.CipherAndAuthTag)1 CiphertextTuple (org.jivesoftware.smackx.omemo.internal.CiphertextTuple)1 OmemoCachedDeviceList (org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList)1