Search in sources :

Example 6 with SessionBuilder

use of org.whispersystems.libsignal.SessionBuilder in project Signal-Android by WhisperSystems.

the class SignalServiceMessageSender method handleMismatchedDevices.

private void handleMismatchedDevices(PushServiceSocket socket, SignalServiceAddress recipient, MismatchedDevices mismatchedDevices) throws IOException, UntrustedIdentityException {
    try {
        Log.w(TAG, "[handleMismatchedDevices] Address: " + recipient.getIdentifier() + ", ExtraDevices: " + mismatchedDevices.getExtraDevices() + ", MissingDevices: " + mismatchedDevices.getMissingDevices());
        archiveSessions(recipient, mismatchedDevices.getExtraDevices());
        for (int missingDeviceId : mismatchedDevices.getMissingDevices()) {
            PreKeyBundle preKey = socket.getPreKey(recipient, missingDeviceId);
            try {
                SignalSessionBuilder sessionBuilder = new SignalSessionBuilder(sessionLock, new SessionBuilder(store, new SignalProtocolAddress(recipient.getIdentifier(), missingDeviceId)));
                sessionBuilder.process(preKey);
            } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
                throw new UntrustedIdentityException("Untrusted identity key!", recipient.getIdentifier(), preKey.getIdentityKey());
            }
        }
    } catch (InvalidKeyException e) {
        throw new IOException(e);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) SignalGroupSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalGroupSessionBuilder) GroupSessionBuilder(org.whispersystems.libsignal.groups.GroupSessionBuilder) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 7 with SessionBuilder

use of org.whispersystems.libsignal.SessionBuilder in project Pix-Art-Messenger by kriztan.

the class AxolotlService method buildSessionFromPEP.

private void buildSessionFromPEP(final SignalProtocolAddress 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(CryptoHelper.bytesToHex(bundle.getIdentityKey().getPublicKey().serialize()));
                            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(de.pixart.messenger.entities.Account) IqParser(de.pixart.messenger.parser.IqParser) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) OnIqPacketReceived(de.pixart.messenger.xmpp.OnIqPacketReceived) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket) PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) Random(java.util.Random)

Example 8 with SessionBuilder

use of org.whispersystems.libsignal.SessionBuilder in project Signal-Android by signalapp.

the class SignalServiceMessageSender method handleMismatchedDevices.

private void handleMismatchedDevices(PushServiceSocket socket, SignalServiceAddress recipient, MismatchedDevices mismatchedDevices) throws IOException, UntrustedIdentityException {
    try {
        Log.w(TAG, "[handleMismatchedDevices] Address: " + recipient.getIdentifier() + ", ExtraDevices: " + mismatchedDevices.getExtraDevices() + ", MissingDevices: " + mismatchedDevices.getMissingDevices());
        archiveSessions(recipient, mismatchedDevices.getExtraDevices());
        for (int missingDeviceId : mismatchedDevices.getMissingDevices()) {
            PreKeyBundle preKey = socket.getPreKey(recipient, missingDeviceId);
            try {
                SignalSessionBuilder sessionBuilder = new SignalSessionBuilder(sessionLock, new SessionBuilder(store, new SignalProtocolAddress(recipient.getIdentifier(), missingDeviceId)));
                sessionBuilder.process(preKey);
            } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
                throw new UntrustedIdentityException("Untrusted identity key!", recipient.getIdentifier(), preKey.getIdentityKey());
            }
        }
    } catch (InvalidKeyException e) {
        throw new IOException(e);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) SignalGroupSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalGroupSessionBuilder) GroupSessionBuilder(org.whispersystems.libsignal.groups.GroupSessionBuilder) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 9 with SessionBuilder

use of org.whispersystems.libsignal.SessionBuilder in project Signal-Android by signalapp.

the class SignalServiceMessageSender method getEncryptedMessage.

private OutgoingPushMessage getEncryptedMessage(PushServiceSocket socket, SignalServiceAddress recipient, Optional<UnidentifiedAccess> unidentifiedAccess, int deviceId, EnvelopeContent plaintext) throws IOException, InvalidKeyException, UntrustedIdentityException {
    SignalProtocolAddress signalProtocolAddress = new SignalProtocolAddress(recipient.getIdentifier(), deviceId);
    SignalServiceCipher cipher = new SignalServiceCipher(localAddress, localDeviceId, store, sessionLock, null);
    if (!store.containsSession(signalProtocolAddress)) {
        try {
            List<PreKeyBundle> preKeys = socket.getPreKeys(recipient, unidentifiedAccess, deviceId);
            for (PreKeyBundle preKey : preKeys) {
                try {
                    SignalProtocolAddress preKeyAddress = new SignalProtocolAddress(recipient.getIdentifier(), preKey.getDeviceId());
                    SignalSessionBuilder sessionBuilder = new SignalSessionBuilder(sessionLock, new SessionBuilder(store, preKeyAddress));
                    sessionBuilder.process(preKey);
                } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
                    throw new UntrustedIdentityException("Untrusted identity key!", recipient.getIdentifier(), preKey.getIdentityKey());
                }
            }
            if (eventListener.isPresent()) {
                eventListener.get().onSecurityEvent(recipient);
            }
        } catch (InvalidKeyException e) {
            throw new IOException(e);
        }
    }
    try {
        return cipher.encrypt(signalProtocolAddress, unidentifiedAccess, plaintext);
    } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
        throw new UntrustedIdentityException("Untrusted on send", recipient.getIdentifier(), e.getUntrustedIdentity());
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) SignalGroupSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalGroupSessionBuilder) GroupSessionBuilder(org.whispersystems.libsignal.groups.GroupSessionBuilder) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 10 with SessionBuilder

use of org.whispersystems.libsignal.SessionBuilder in project libsignal-service-java by signalapp.

the class SignalServiceMessageSender method handleMismatchedDevices.

private void handleMismatchedDevices(PushServiceSocket socket, SignalServiceAddress recipient, MismatchedDevices mismatchedDevices) throws IOException, UntrustedIdentityException {
    try {
        for (int extraDeviceId : mismatchedDevices.getExtraDevices()) {
            if (recipient.getUuid().isPresent()) {
                store.deleteSession(new SignalProtocolAddress(recipient.getUuid().get().toString(), extraDeviceId));
            }
            if (recipient.getNumber().isPresent()) {
                store.deleteSession(new SignalProtocolAddress(recipient.getNumber().get(), extraDeviceId));
            }
        }
        for (int missingDeviceId : mismatchedDevices.getMissingDevices()) {
            PreKeyBundle preKey = socket.getPreKey(recipient, missingDeviceId);
            try {
                SessionBuilder sessionBuilder = new SessionBuilder(store, new SignalProtocolAddress(recipient.getIdentifier(), missingDeviceId));
                sessionBuilder.process(preKey);
            } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
                throw new UntrustedIdentityException("Untrusted identity key!", recipient.getIdentifier(), preKey.getIdentityKey());
            }
        }
    } catch (InvalidKeyException e) {
        throw new IOException(e);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Aggregations

SessionBuilder (org.whispersystems.libsignal.SessionBuilder)10 PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)9 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)8 IOException (java.io.IOException)7 SignalProtocolAddress (org.whispersystems.libsignal.SignalProtocolAddress)7 UntrustedIdentityException (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException)7 GroupSessionBuilder (org.whispersystems.libsignal.groups.GroupSessionBuilder)4 SignalGroupSessionBuilder (org.whispersystems.signalservice.api.crypto.SignalGroupSessionBuilder)4 SignalServiceCipher (org.whispersystems.signalservice.api.crypto.SignalServiceCipher)4 SignalSessionBuilder (org.whispersystems.signalservice.api.crypto.SignalSessionBuilder)4 Random (java.util.Random)2 UntrustedIdentityException (org.whispersystems.libsignal.UntrustedIdentityException)2 ContentHint (org.whispersystems.signalservice.api.crypto.ContentHint)2 ImmutableList (com.google.common.collect.ImmutableList)1 Account (de.pixart.messenger.entities.Account)1 IqParser (de.pixart.messenger.parser.IqParser)1 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)1 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)1 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)1 IqParser (eu.siacs.conversations.parser.IqParser)1