Search in sources :

Example 1 with UntrustedIdentityException

use of org.whispersystems.libaxolotl.UntrustedIdentityException in project Conversations by siacs.

the class AxolotlService method buildSessionFromPEP.

private void buildSessionFromPEP(final AxolotlAddress address) {
    Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new session for " + address.toString());
    if (address.equals(getOwnAxolotlAddress())) {
        throw new AssertionError("We should NEVER build a session with ourselves. What happened here?!");
    }
    try {
        IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(Jid.fromString(address.getName()), address.getDeviceId());
        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
        mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {

            @Override
            public void onIqPacketReceived(Account account, IqPacket packet) {
                if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
                    fetchStatusMap.put(address, FetchStatus.TIMEOUT);
                } else if (packet.getType() == IqPacket.TYPE.RESULT) {
                    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
                    final IqParser parser = mXmppConnectionService.getIqParser();
                    final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
                    final PreKeyBundle bundle = parser.bundle(packet);
                    if (preKeyBundleList.isEmpty() || bundle == null) {
                        Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
                        fetchStatusMap.put(address, FetchStatus.ERROR);
                        finishBuildingSessionsFromPEP(address);
                        return;
                    }
                    Random random = new Random();
                    final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
                    if (preKey == null) {
                        //should never happen
                        fetchStatusMap.put(address, FetchStatus.ERROR);
                        finishBuildingSessionsFromPEP(address);
                        return;
                    }
                    final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(), preKey.getPreKeyId(), preKey.getPreKey(), bundle.getSignedPreKeyId(), bundle.getSignedPreKey(), bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
                    try {
                        SessionBuilder builder = new SessionBuilder(axolotlStore, address);
                        builder.process(preKeyBundle);
                        XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey());
                        sessions.put(address, session);
                        if (Config.X509_VERIFICATION) {
                            verifySessionWithPEP(session);
                        } else {
                            FingerprintStatus status = getFingerprintTrust(bundle.getIdentityKey().getFingerprint().replaceAll("\\s", ""));
                            FetchStatus fetchStatus;
                            if (status != null && status.isVerified()) {
                                fetchStatus = FetchStatus.SUCCESS_VERIFIED;
                            } else if (status != null && status.isTrusted()) {
                                fetchStatus = FetchStatus.SUCCESS_TRUSTED;
                            } else {
                                fetchStatus = FetchStatus.SUCCESS;
                            }
                            fetchStatusMap.put(address, fetchStatus);
                            finishBuildingSessionsFromPEP(address);
                        }
                    } catch (UntrustedIdentityException | InvalidKeyException e) {
                        Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": " + e.getClass().getName() + ", " + e.getMessage());
                        fetchStatusMap.put(address, FetchStatus.ERROR);
                        finishBuildingSessionsFromPEP(address);
                    }
                } else {
                    fetchStatusMap.put(address, FetchStatus.ERROR);
                    Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
                    finishBuildingSessionsFromPEP(address);
                }
            }
        });
    } catch (InvalidJidException e) {
        Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
    }
}
Also used : Account(eu.siacs.conversations.entities.Account) IqParser(eu.siacs.conversations.parser.IqParser) UntrustedIdentityException(org.whispersystems.libaxolotl.UntrustedIdentityException) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) SessionBuilder(org.whispersystems.libaxolotl.SessionBuilder) InvalidKeyException(org.whispersystems.libaxolotl.InvalidKeyException) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) PreKeyBundle(org.whispersystems.libaxolotl.state.PreKeyBundle) Random(java.util.Random)

Example 2 with UntrustedIdentityException

use of org.whispersystems.libaxolotl.UntrustedIdentityException in project Conversations by siacs.

the class XmppAxolotlSession method processReceiving.

@Nullable
public byte[] processReceiving(AxolotlKey encryptedKey) throws CryptoFailedException {
    byte[] plaintext;
    FingerprintStatus status = getTrust();
    if (!status.isCompromised()) {
        try {
            CiphertextMessage ciphertextMessage;
            try {
                ciphertextMessage = new PreKeyWhisperMessage(encryptedKey.key);
                Optional<Integer> optionalPreKeyId = ((PreKeyWhisperMessage) ciphertextMessage).getPreKeyId();
                IdentityKey identityKey = ((PreKeyWhisperMessage) ciphertextMessage).getIdentityKey();
                if (!optionalPreKeyId.isPresent()) {
                    throw new CryptoFailedException("PreKeyWhisperMessage did not contain a PreKeyId");
                }
                preKeyId = optionalPreKeyId.get();
                if (this.identityKey != null && !this.identityKey.equals(identityKey)) {
                    throw new CryptoFailedException("Received PreKeyWhisperMessage but preexisting identity key changed.");
                }
                this.identityKey = identityKey;
            } catch (InvalidVersionException | InvalidMessageException e) {
                ciphertextMessage = new WhisperMessage(encryptedKey.key);
            }
            if (ciphertextMessage instanceof PreKeyWhisperMessage) {
                plaintext = cipher.decrypt((PreKeyWhisperMessage) ciphertextMessage);
            } else {
                plaintext = cipher.decrypt((WhisperMessage) ciphertextMessage);
            }
        } catch (InvalidKeyException | LegacyMessageException | InvalidMessageException | DuplicateMessageException | NoSessionException | InvalidKeyIdException | UntrustedIdentityException e) {
            if (!(e instanceof DuplicateMessageException)) {
                e.printStackTrace();
            }
            throw new CryptoFailedException("Error decrypting WhisperMessage " + e.getClass().getSimpleName() + ": " + e.getMessage());
        }
        if (!status.isActive()) {
            setTrust(status.toActive());
        }
    } else {
        throw new CryptoFailedException("not encrypting omemo message from fingerprint " + getFingerprint() + " because it was marked as compromised");
    }
    return plaintext;
}
Also used : InvalidMessageException(org.whispersystems.libaxolotl.InvalidMessageException) IdentityKey(org.whispersystems.libaxolotl.IdentityKey) UntrustedIdentityException(org.whispersystems.libaxolotl.UntrustedIdentityException) PreKeyWhisperMessage(org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage) CiphertextMessage(org.whispersystems.libaxolotl.protocol.CiphertextMessage) InvalidVersionException(org.whispersystems.libaxolotl.InvalidVersionException) InvalidKeyException(org.whispersystems.libaxolotl.InvalidKeyException) NoSessionException(org.whispersystems.libaxolotl.NoSessionException) DuplicateMessageException(org.whispersystems.libaxolotl.DuplicateMessageException) InvalidKeyIdException(org.whispersystems.libaxolotl.InvalidKeyIdException) WhisperMessage(org.whispersystems.libaxolotl.protocol.WhisperMessage) PreKeyWhisperMessage(org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage) LegacyMessageException(org.whispersystems.libaxolotl.LegacyMessageException) Nullable(android.support.annotation.Nullable)

Aggregations

InvalidKeyException (org.whispersystems.libaxolotl.InvalidKeyException)2 UntrustedIdentityException (org.whispersystems.libaxolotl.UntrustedIdentityException)2 Nullable (android.support.annotation.Nullable)1 Account (eu.siacs.conversations.entities.Account)1 IqParser (eu.siacs.conversations.parser.IqParser)1 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)1 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)1 IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)1 Random (java.util.Random)1 DuplicateMessageException (org.whispersystems.libaxolotl.DuplicateMessageException)1 IdentityKey (org.whispersystems.libaxolotl.IdentityKey)1 InvalidKeyIdException (org.whispersystems.libaxolotl.InvalidKeyIdException)1 InvalidMessageException (org.whispersystems.libaxolotl.InvalidMessageException)1 InvalidVersionException (org.whispersystems.libaxolotl.InvalidVersionException)1 LegacyMessageException (org.whispersystems.libaxolotl.LegacyMessageException)1 NoSessionException (org.whispersystems.libaxolotl.NoSessionException)1 SessionBuilder (org.whispersystems.libaxolotl.SessionBuilder)1 CiphertextMessage (org.whispersystems.libaxolotl.protocol.CiphertextMessage)1 PreKeyWhisperMessage (org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage)1 WhisperMessage (org.whispersystems.libaxolotl.protocol.WhisperMessage)1