Search in sources :

Example 76 with SignalProtocolAddress

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

the class SignalServiceCipher method decrypt.

private Plaintext decrypt(SignalServiceEnvelope envelope, byte[] ciphertext) throws InvalidMetadataMessageException, InvalidMetadataVersionException, ProtocolDuplicateMessageException, ProtocolUntrustedIdentityException, ProtocolLegacyMessageException, ProtocolInvalidKeyException, ProtocolInvalidVersionException, ProtocolInvalidMessageException, ProtocolInvalidKeyIdException, ProtocolNoSessionException, SelfSendException {
    try {
        byte[] paddedMessage;
        Metadata metadata;
        int sessionVersion;
        if (!envelope.hasSource() && !envelope.isUnidentifiedSender()) {
            throw new ProtocolInvalidMessageException(new InvalidMessageException("Non-UD envelope is missing a source!"), null, 0);
        }
        if (envelope.isPreKeySignalMessage()) {
            SignalProtocolAddress sourceAddress = getPreferredProtocolAddress(signalProtocolStore, envelope.getSourceAddress(), envelope.getSourceDevice());
            SessionCipher sessionCipher = new SessionCipher(signalProtocolStore, sourceAddress);
            paddedMessage = sessionCipher.decrypt(new PreKeySignalMessage(ciphertext));
            metadata = new Metadata(envelope.getSourceAddress(), envelope.getSourceDevice(), envelope.getTimestamp(), false);
            sessionVersion = sessionCipher.getSessionVersion();
        } else if (envelope.isSignalMessage()) {
            SignalProtocolAddress sourceAddress = getPreferredProtocolAddress(signalProtocolStore, envelope.getSourceAddress(), envelope.getSourceDevice());
            SessionCipher sessionCipher = new SessionCipher(signalProtocolStore, sourceAddress);
            paddedMessage = sessionCipher.decrypt(new SignalMessage(ciphertext));
            metadata = new Metadata(envelope.getSourceAddress(), envelope.getSourceDevice(), envelope.getTimestamp(), false);
            sessionVersion = sessionCipher.getSessionVersion();
        } else if (envelope.isUnidentifiedSender()) {
            SealedSessionCipher sealedSessionCipher = new SealedSessionCipher(signalProtocolStore, localAddress.getUuid().orNull(), localAddress.getNumber().orNull(), 1);
            DecryptionResult result = sealedSessionCipher.decrypt(certificateValidator, ciphertext, envelope.getServerTimestamp());
            SignalServiceAddress resultAddress = new SignalServiceAddress(UuidUtil.parse(result.getSenderUuid().orNull()), result.getSenderE164());
            SignalProtocolAddress protocolAddress = getPreferredProtocolAddress(signalProtocolStore, resultAddress, result.getDeviceId());
            paddedMessage = result.getPaddedMessage();
            metadata = new Metadata(resultAddress, result.getDeviceId(), envelope.getTimestamp(), true);
            sessionVersion = sealedSessionCipher.getSessionVersion(protocolAddress);
        } else {
            throw new InvalidMetadataMessageException("Unknown type: " + envelope.getType());
        }
        PushTransportDetails transportDetails = new PushTransportDetails(sessionVersion);
        byte[] data = transportDetails.getStrippedPaddingMessageBody(paddedMessage);
        return new Plaintext(metadata, data);
    } catch (DuplicateMessageException e) {
        throw new ProtocolDuplicateMessageException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (LegacyMessageException e) {
        throw new ProtocolLegacyMessageException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (InvalidMessageException e) {
        throw new ProtocolInvalidMessageException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (InvalidKeyIdException e) {
        throw new ProtocolInvalidKeyIdException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (InvalidKeyException e) {
        throw new ProtocolInvalidKeyException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (UntrustedIdentityException e) {
        throw new ProtocolUntrustedIdentityException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (InvalidVersionException e) {
        throw new ProtocolInvalidVersionException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (NoSessionException e) {
        throw new ProtocolNoSessionException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    }
}
Also used : ProtocolInvalidMessageException(org.signal.libsignal.metadata.ProtocolInvalidMessageException) SealedSessionCipher(org.signal.libsignal.metadata.SealedSessionCipher) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ProtocolInvalidMessageException(org.signal.libsignal.metadata.ProtocolInvalidMessageException) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) ProtocolUntrustedIdentityException(org.signal.libsignal.metadata.ProtocolUntrustedIdentityException) ProtocolUntrustedIdentityException(org.signal.libsignal.metadata.ProtocolUntrustedIdentityException) ProtocolInvalidVersionException(org.signal.libsignal.metadata.ProtocolInvalidVersionException) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) ProtocolInvalidVersionException(org.signal.libsignal.metadata.ProtocolInvalidVersionException) ProtocolInvalidKeyIdException(org.signal.libsignal.metadata.ProtocolInvalidKeyIdException) NoSessionException(org.whispersystems.libsignal.NoSessionException) ProtocolNoSessionException(org.signal.libsignal.metadata.ProtocolNoSessionException) ProtocolDuplicateMessageException(org.signal.libsignal.metadata.ProtocolDuplicateMessageException) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) PushTransportDetails(org.whispersystems.signalservice.internal.push.PushTransportDetails) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) ProtocolLegacyMessageException(org.signal.libsignal.metadata.ProtocolLegacyMessageException) SessionCipher(org.whispersystems.libsignal.SessionCipher) SealedSessionCipher(org.signal.libsignal.metadata.SealedSessionCipher) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) ProtocolNoSessionException(org.signal.libsignal.metadata.ProtocolNoSessionException) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) SignalMessage(org.whispersystems.libsignal.protocol.SignalMessage) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ProtocolInvalidKeyException(org.signal.libsignal.metadata.ProtocolInvalidKeyException) InvalidMetadataMessageException(org.signal.libsignal.metadata.InvalidMetadataMessageException) ProtocolInvalidKeyException(org.signal.libsignal.metadata.ProtocolInvalidKeyException) DuplicateMessageException(org.whispersystems.libsignal.DuplicateMessageException) ProtocolDuplicateMessageException(org.signal.libsignal.metadata.ProtocolDuplicateMessageException) DecryptionResult(org.signal.libsignal.metadata.SealedSessionCipher.DecryptionResult) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) ProtocolInvalidKeyIdException(org.signal.libsignal.metadata.ProtocolInvalidKeyIdException) LegacyMessageException(org.whispersystems.libsignal.LegacyMessageException) ProtocolLegacyMessageException(org.signal.libsignal.metadata.ProtocolLegacyMessageException)

Example 77 with SignalProtocolAddress

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

SignalProtocolAddress (org.whispersystems.libsignal.SignalProtocolAddress)77 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)14 PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)11 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)11 ArrayList (java.util.ArrayList)10 Recipient (org.thoughtcrime.securesms.recipients.Recipient)10 IOException (java.io.IOException)9 SessionBuilder (org.whispersystems.libsignal.SessionBuilder)9 Optional (org.whispersystems.libsignal.util.guava.Optional)9 UntrustedIdentityException (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 List (java.util.List)8 ContentHint (org.whispersystems.signalservice.api.crypto.ContentHint)8 DistributionId (org.whispersystems.signalservice.api.push.DistributionId)8 NonNull (androidx.annotation.NonNull)6 LinkedList (java.util.LinkedList)6 Collectors (java.util.stream.Collectors)6 SenderKeyDistributionMessage (org.whispersystems.libsignal.protocol.SenderKeyDistributionMessage)6 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)6