Search in sources :

Example 11 with PreKeyBundle

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

the class SignalServiceMessageSender method getEncryptedMessage.

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

Example 12 with PreKeyBundle

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

the class PushServiceSocket method getPreKey.

public PreKeyBundle getPreKey(SignalServiceAddress destination, int deviceId) throws IOException {
    try {
        String path = String.format(PREKEY_DEVICE_PATH, destination.getIdentifier(), String.valueOf(deviceId));
        String responseText = makeServiceRequest(path, "GET", null);
        PreKeyResponse response = JsonUtil.fromJson(responseText, PreKeyResponse.class);
        if (response.getDevices() == null || response.getDevices().size() < 1)
            throw new IOException("Empty prekey list");
        PreKeyResponseItem device = response.getDevices().get(0);
        ECPublicKey preKey = null;
        ECPublicKey signedPreKey = null;
        byte[] signedPreKeySignature = null;
        int preKeyId = -1;
        int signedPreKeyId = -1;
        if (device.getPreKey() != null) {
            preKeyId = device.getPreKey().getKeyId();
            preKey = device.getPreKey().getPublicKey();
        }
        if (device.getSignedPreKey() != null) {
            signedPreKeyId = device.getSignedPreKey().getKeyId();
            signedPreKey = device.getSignedPreKey().getPublicKey();
            signedPreKeySignature = device.getSignedPreKey().getSignature();
        }
        return new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), preKeyId, preKey, signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey());
    } catch (NotFoundException nfe) {
        throw new UnregisteredUserException(destination.getIdentifier(), nfe);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) IOException(java.io.IOException)

Example 13 with PreKeyBundle

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

the class PushServiceSocket method getPreKeys.

public List<PreKeyBundle> getPreKeys(SignalServiceAddress destination, Optional<UnidentifiedAccess> unidentifiedAccess, int deviceIdInteger) throws IOException {
    try {
        String deviceId = String.valueOf(deviceIdInteger);
        if (deviceId.equals("1"))
            deviceId = "*";
        String path = String.format(PREKEY_DEVICE_PATH, destination.getIdentifier(), deviceId);
        Log.d(TAG, "Fetching prekeys for " + destination.getIdentifier() + "." + deviceId + ", i.e. GET " + path);
        String responseText = makeServiceRequest(path, "GET", null, NO_HEADERS, unidentifiedAccess);
        PreKeyResponse response = JsonUtil.fromJson(responseText, PreKeyResponse.class);
        List<PreKeyBundle> bundles = new LinkedList<>();
        for (PreKeyResponseItem device : response.getDevices()) {
            ECPublicKey preKey = null;
            ECPublicKey signedPreKey = null;
            byte[] signedPreKeySignature = null;
            int preKeyId = -1;
            int signedPreKeyId = -1;
            if (device.getSignedPreKey() != null) {
                signedPreKey = device.getSignedPreKey().getPublicKey();
                signedPreKeyId = device.getSignedPreKey().getKeyId();
                signedPreKeySignature = device.getSignedPreKey().getSignature();
            }
            if (device.getPreKey() != null) {
                preKeyId = device.getPreKey().getKeyId();
                preKey = device.getPreKey().getPublicKey();
            }
            bundles.add(new PreKeyBundle(device.getRegistrationId(), device.getDeviceId(), preKeyId, preKey, signedPreKeyId, signedPreKey, signedPreKeySignature, response.getIdentityKey()));
        }
        return bundles;
    } catch (NotFoundException nfe) {
        throw new UnregisteredUserException(destination.getIdentifier(), nfe);
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) GroupNotFoundException(org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) GroupsV2AuthorizationString(org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString) LinkedList(java.util.LinkedList)

Example 14 with PreKeyBundle

use of org.whispersystems.libsignal.state.PreKeyBundle 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 15 with PreKeyBundle

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

the class IqParser method preKeys.

public List<PreKeyBundle> preKeys(final IqPacket preKeys) {
    List<PreKeyBundle> bundles = new ArrayList<>();
    Map<Integer, ECPublicKey> preKeyPublics = preKeyPublics(preKeys);
    if (preKeyPublics != null) {
        for (Integer preKeyId : preKeyPublics.keySet()) {
            ECPublicKey preKeyPublic = preKeyPublics.get(preKeyId);
            bundles.add(new PreKeyBundle(0, 0, preKeyId, preKeyPublic, 0, null, null, null));
        }
    }
    return bundles;
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) ArrayList(java.util.ArrayList)

Aggregations

PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)22 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)11 IOException (java.io.IOException)10 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)10 SessionBuilder (org.whispersystems.libsignal.SessionBuilder)9 SignalProtocolAddress (org.whispersystems.libsignal.SignalProtocolAddress)7 UntrustedIdentityException (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException)7 NotFoundException (org.whispersystems.signalservice.api.push.exceptions.NotFoundException)7 UnregisteredUserException (org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException)7 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)4 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 GroupsV2AuthorizationString (org.whispersystems.signalservice.api.groupsv2.GroupsV2AuthorizationString)4 GroupNotFoundException (org.whispersystems.signalservice.internal.push.exceptions.GroupNotFoundException)4 Element (eu.siacs.conversations.xml.Element)3 List (java.util.List)3 ImmutableList (com.google.common.collect.ImmutableList)2