Search in sources :

Example 6 with PreKeyBundle

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

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 7 with PreKeyBundle

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

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

Example 8 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, Optional<UnidentifiedAccess> unidentifiedAccess, int deviceId, byte[] plaintext) throws IOException, InvalidKeyException, UntrustedIdentityException {
    SignalProtocolAddress signalProtocolAddress = new SignalProtocolAddress(recipient.getIdentifier(), deviceId);
    SignalServiceCipher cipher = new SignalServiceCipher(localAddress, store, 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());
                    SessionBuilder sessionBuilder = 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) 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 9 with PreKeyBundle

use of org.whispersystems.libsignal.state.PreKeyBundle in project Signal-Android 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));
        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 10 with PreKeyBundle

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

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)

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