Search in sources :

Example 21 with PreKeyBundle

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

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));
        if (destination.getRelay().isPresent()) {
            path = path + "?relay=" + destination.getRelay().get();
        }
        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) NotFoundException(org.whispersystems.signalservice.api.push.exceptions.NotFoundException) IOException(java.io.IOException)

Example 22 with PreKeyBundle

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

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