Search in sources :

Example 6 with VerifiedMessage

use of org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage in project libsignal-service-java by signalapp.

the class SignalServiceCipher method createSynchronizeMessage.

private SignalServiceSyncMessage createSynchronizeMessage(Metadata metadata, SyncMessage content) throws ProtocolInvalidMessageException, ProtocolInvalidKeyException, UnsupportedDataMessageException {
    if (content.hasSent()) {
        SyncMessage.Sent sentContent = content.getSent();
        SignalServiceDataMessage dataMessage = createSignalServiceMessage(metadata, sentContent.getMessage());
        Optional<SignalServiceAddress> address = SignalServiceAddress.isValidAddress(sentContent.getDestinationUuid(), sentContent.getDestinationE164()) ? Optional.of(new SignalServiceAddress(UuidUtil.parseOrNull(sentContent.getDestinationUuid()), sentContent.getDestinationE164())) : Optional.<SignalServiceAddress>absent();
        Map<SignalServiceAddress, Boolean> unidentifiedStatuses = new HashMap<>();
        if (!address.isPresent() && !dataMessage.getGroupInfo().isPresent()) {
            throw new ProtocolInvalidMessageException(new InvalidMessageException("SyncMessage missing both destination and group ID!"), null, 0);
        }
        for (SyncMessage.Sent.UnidentifiedDeliveryStatus status : sentContent.getUnidentifiedStatusList()) {
            if (SignalServiceAddress.isValidAddress(status.getDestinationUuid(), status.getDestinationE164())) {
                SignalServiceAddress recipient = new SignalServiceAddress(UuidUtil.parseOrNull(status.getDestinationUuid()), status.getDestinationE164());
                unidentifiedStatuses.put(recipient, status.getUnidentified());
            } else {
                Log.w(TAG, "Encountered an invalid UnidentifiedDeliveryStatus in a SentTranscript! Ignoring.");
            }
        }
        return SignalServiceSyncMessage.forSentTranscript(new SentTranscriptMessage(address, sentContent.getTimestamp(), dataMessage, sentContent.getExpirationStartTimestamp(), unidentifiedStatuses, sentContent.getIsRecipientUpdate()));
    }
    if (content.hasRequest()) {
        return SignalServiceSyncMessage.forRequest(new RequestMessage(content.getRequest()));
    }
    if (content.getReadList().size() > 0) {
        List<ReadMessage> readMessages = new LinkedList<>();
        for (SyncMessage.Read read : content.getReadList()) {
            if (SignalServiceAddress.isValidAddress(read.getSenderUuid(), read.getSenderE164())) {
                SignalServiceAddress address = new SignalServiceAddress(UuidUtil.parseOrNull(read.getSenderUuid()), read.getSenderE164());
                readMessages.add(new ReadMessage(address, read.getTimestamp()));
            } else {
                Log.w(TAG, "Encountered an invalid ReadMessage! Ignoring.");
            }
        }
        return SignalServiceSyncMessage.forRead(readMessages);
    }
    if (content.hasViewOnceOpen()) {
        if (SignalServiceAddress.isValidAddress(content.getViewOnceOpen().getSenderUuid(), content.getViewOnceOpen().getSenderE164())) {
            SignalServiceAddress address = new SignalServiceAddress(UuidUtil.parseOrNull(content.getViewOnceOpen().getSenderUuid()), content.getViewOnceOpen().getSenderE164());
            ViewOnceOpenMessage timerRead = new ViewOnceOpenMessage(address, content.getViewOnceOpen().getTimestamp());
            return SignalServiceSyncMessage.forViewOnceOpen(timerRead);
        } else {
            throw new ProtocolInvalidMessageException(new InvalidMessageException("ViewOnceOpen message has no sender!"), null, 0);
        }
    }
    if (content.hasVerified()) {
        if (SignalServiceAddress.isValidAddress(content.getVerified().getDestinationUuid(), content.getVerified().getDestinationE164())) {
            try {
                Verified verified = content.getVerified();
                SignalServiceAddress destination = new SignalServiceAddress(UuidUtil.parseOrNull(verified.getDestinationUuid()), verified.getDestinationE164());
                IdentityKey identityKey = new IdentityKey(verified.getIdentityKey().toByteArray(), 0);
                VerifiedState verifiedState;
                if (verified.getState() == Verified.State.DEFAULT) {
                    verifiedState = VerifiedState.DEFAULT;
                } else if (verified.getState() == Verified.State.VERIFIED) {
                    verifiedState = VerifiedState.VERIFIED;
                } else if (verified.getState() == Verified.State.UNVERIFIED) {
                    verifiedState = VerifiedState.UNVERIFIED;
                } else {
                    throw new ProtocolInvalidMessageException(new InvalidMessageException("Unknown state: " + verified.getState().getNumber()), metadata.getSender().getIdentifier(), metadata.getSenderDevice());
                }
                return SignalServiceSyncMessage.forVerified(new VerifiedMessage(destination, identityKey, verifiedState, System.currentTimeMillis()));
            } catch (InvalidKeyException e) {
                throw new ProtocolInvalidKeyException(e, metadata.getSender().getIdentifier(), metadata.getSenderDevice());
            }
        } else {
            throw new ProtocolInvalidMessageException(new InvalidMessageException("Verified message has no sender!"), null, 0);
        }
    }
    if (content.getStickerPackOperationList().size() > 0) {
        List<StickerPackOperationMessage> operations = new LinkedList<>();
        for (SyncMessage.StickerPackOperation operation : content.getStickerPackOperationList()) {
            byte[] packId = operation.hasPackId() ? operation.getPackId().toByteArray() : null;
            byte[] packKey = operation.hasPackKey() ? operation.getPackKey().toByteArray() : null;
            StickerPackOperationMessage.Type type = null;
            if (operation.hasType()) {
                switch(operation.getType()) {
                    case INSTALL:
                        type = StickerPackOperationMessage.Type.INSTALL;
                        break;
                    case REMOVE:
                        type = StickerPackOperationMessage.Type.REMOVE;
                        break;
                }
            }
            operations.add(new StickerPackOperationMessage(packId, packKey, type));
        }
        return SignalServiceSyncMessage.forStickerPackOperations(operations);
    }
    if (content.hasBlocked()) {
        List<String> numbers = content.getBlocked().getNumbersList();
        List<String> uuids = content.getBlocked().getUuidsList();
        List<SignalServiceAddress> addresses = new ArrayList<>(numbers.size() + uuids.size());
        List<byte[]> groupIds = new ArrayList<>(content.getBlocked().getGroupIdsList().size());
        for (String e164 : numbers) {
            Optional<SignalServiceAddress> address = SignalServiceAddress.fromRaw(null, e164);
            if (address.isPresent()) {
                addresses.add(address.get());
            }
        }
        for (String uuid : uuids) {
            Optional<SignalServiceAddress> address = SignalServiceAddress.fromRaw(uuid, null);
            if (address.isPresent()) {
                addresses.add(address.get());
            }
        }
        for (ByteString groupId : content.getBlocked().getGroupIdsList()) {
            groupIds.add(groupId.toByteArray());
        }
        return SignalServiceSyncMessage.forBlocked(new BlockedListMessage(addresses, groupIds));
    }
    if (content.hasConfiguration()) {
        Boolean readReceipts = content.getConfiguration().hasReadReceipts() ? content.getConfiguration().getReadReceipts() : null;
        Boolean unidentifiedDeliveryIndicators = content.getConfiguration().hasUnidentifiedDeliveryIndicators() ? content.getConfiguration().getUnidentifiedDeliveryIndicators() : null;
        Boolean typingIndicators = content.getConfiguration().hasTypingIndicators() ? content.getConfiguration().getTypingIndicators() : null;
        Boolean linkPreviews = content.getConfiguration().hasLinkPreviews() ? content.getConfiguration().getLinkPreviews() : null;
        return SignalServiceSyncMessage.forConfiguration(new ConfigurationMessage(Optional.fromNullable(readReceipts), Optional.fromNullable(unidentifiedDeliveryIndicators), Optional.fromNullable(typingIndicators), Optional.fromNullable(linkPreviews)));
    }
    if (content.hasFetchLatest() && content.getFetchLatest().hasType()) {
        switch(content.getFetchLatest().getType()) {
            case LOCAL_PROFILE:
                return SignalServiceSyncMessage.forFetchLatest(SignalServiceSyncMessage.FetchType.LOCAL_PROFILE);
            case STORAGE_MANIFEST:
                return SignalServiceSyncMessage.forFetchLatest(SignalServiceSyncMessage.FetchType.STORAGE_MANIFEST);
        }
    }
    return SignalServiceSyncMessage.empty();
}
Also used : ProtocolInvalidMessageException(org.signal.libsignal.metadata.ProtocolInvalidMessageException) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ProtocolInvalidMessageException(org.signal.libsignal.metadata.ProtocolInvalidMessageException) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) BlockedListMessage(org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage) Verified(org.whispersystems.signalservice.internal.push.SignalServiceProtos.Verified) ViewOnceOpenMessage(org.whispersystems.signalservice.api.messages.multidevice.ViewOnceOpenMessage) RequestMessage(org.whispersystems.signalservice.api.messages.multidevice.RequestMessage) StickerPackOperationMessage(org.whispersystems.signalservice.api.messages.multidevice.StickerPackOperationMessage) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) ConfigurationMessage(org.whispersystems.signalservice.api.messages.multidevice.ConfigurationMessage) SentTranscriptMessage(org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage) IdentityKey(org.whispersystems.libsignal.IdentityKey) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ProtocolInvalidKeyException(org.signal.libsignal.metadata.ProtocolInvalidKeyException) LinkedList(java.util.LinkedList) SyncMessage(org.whispersystems.signalservice.internal.push.SignalServiceProtos.SyncMessage) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) ProtocolInvalidKeyException(org.signal.libsignal.metadata.ProtocolInvalidKeyException) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) ReadMessage(org.whispersystems.signalservice.api.messages.multidevice.ReadMessage) VerifiedState(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage.VerifiedState) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)

Example 7 with VerifiedMessage

use of org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage in project Signal-Android by signalapp.

the class MultiDeviceContactUpdateJob method generateSingleContactUpdate.

private void generateSingleContactUpdate(@NonNull RecipientId recipientId) throws IOException, UntrustedIdentityException, NetworkException {
    WriteDetails writeDetails = createTempFile();
    try {
        DeviceContactsOutputStream out = new DeviceContactsOutputStream(writeDetails.outputStream);
        Recipient recipient = Recipient.resolved(recipientId);
        if (recipient.getRegistered() == RecipientDatabase.RegisteredState.NOT_REGISTERED) {
            Log.w(TAG, recipientId + " not registered!");
            return;
        }
        Optional<IdentityRecord> identityRecord = ApplicationDependencies.getProtocolStore().aci().identities().getIdentityRecord(recipient.getId());
        Optional<VerifiedMessage> verifiedMessage = getVerifiedMessage(recipient, identityRecord);
        Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
        Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
        out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, recipient), Optional.fromNullable(recipient.isGroup() || recipient.isSystemContact() ? recipient.getDisplayName(context) : null), getAvatar(recipient.getId(), recipient.getContactUri()), Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), verifiedMessage, ProfileKeyUtil.profileKeyOptional(recipient.getProfileKey()), recipient.isBlocked(), recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent(), Optional.fromNullable(inboxPositions.get(recipientId)), archived.contains(recipientId)));
        out.close();
        long length = BlobProvider.getInstance().calculateFileSize(context, writeDetails.uri);
        sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, writeDetails.uri), length, false);
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
    } finally {
        BlobProvider.getInstance().delete(context, writeDetails.uri);
    }
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)

Example 8 with VerifiedMessage

use of org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage in project Signal-Android by WhisperSystems.

the class MultiDeviceContactUpdateJob method getVerifiedMessage.

private Optional<VerifiedMessage> getVerifiedMessage(Recipient recipient, Optional<IdentityRecord> identity) throws InvalidNumberException, IOException {
    if (!identity.isPresent())
        return Optional.absent();
    SignalServiceAddress destination = RecipientUtil.toSignalServiceAddress(context, recipient);
    IdentityKey identityKey = identity.get().getIdentityKey();
    VerifiedMessage.VerifiedState state;
    switch(identity.get().getVerifiedStatus()) {
        case VERIFIED:
            state = VerifiedMessage.VerifiedState.VERIFIED;
            break;
        case UNVERIFIED:
            state = VerifiedMessage.VerifiedState.UNVERIFIED;
            break;
        case DEFAULT:
            state = VerifiedMessage.VerifiedState.DEFAULT;
            break;
        default:
            throw new AssertionError("Unknown state: " + identity.get().getVerifiedStatus());
    }
    return Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)

Example 9 with VerifiedMessage

use of org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage in project Signal-Android by WhisperSystems.

the class MultiDeviceContactUpdateJob method generateSingleContactUpdate.

private void generateSingleContactUpdate(@NonNull RecipientId recipientId) throws IOException, UntrustedIdentityException, NetworkException {
    WriteDetails writeDetails = createTempFile();
    try {
        DeviceContactsOutputStream out = new DeviceContactsOutputStream(writeDetails.outputStream);
        Recipient recipient = Recipient.resolved(recipientId);
        if (recipient.getRegistered() == RecipientDatabase.RegisteredState.NOT_REGISTERED) {
            Log.w(TAG, recipientId + " not registered!");
            return;
        }
        Optional<IdentityRecord> identityRecord = ApplicationDependencies.getProtocolStore().aci().identities().getIdentityRecord(recipient.getId());
        Optional<VerifiedMessage> verifiedMessage = getVerifiedMessage(recipient, identityRecord);
        Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
        Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
        out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, recipient), Optional.fromNullable(recipient.isGroup() || recipient.isSystemContact() ? recipient.getDisplayName(context) : null), getAvatar(recipient.getId(), recipient.getContactUri()), Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), verifiedMessage, ProfileKeyUtil.profileKeyOptional(recipient.getProfileKey()), recipient.isBlocked(), recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent(), Optional.fromNullable(inboxPositions.get(recipientId)), archived.contains(recipientId)));
        out.close();
        long length = BlobProvider.getInstance().calculateFileSize(context, writeDetails.uri);
        sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, writeDetails.uri), length, false);
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
    } finally {
        BlobProvider.getInstance().delete(context, writeDetails.uri);
    }
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)

Example 10 with VerifiedMessage

use of org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage in project Signal-Android by WhisperSystems.

the class MultiDeviceVerifiedUpdateJob method onRun.

@Override
public void onRun() throws IOException, UntrustedIdentityException {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    try {
        if (!TextSecurePreferences.isMultiDevice(context)) {
            Log.i(TAG, "Not multi device...");
            return;
        }
        if (destination == null) {
            Log.w(TAG, "No destination...");
            return;
        }
        SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
        Recipient recipient = Recipient.resolved(destination);
        if (recipient.isUnregistered()) {
            Log.w(TAG, recipient.getId() + " not registered!");
            return;
        }
        VerifiedMessage.VerifiedState verifiedState = getVerifiedState(verifiedStatus);
        SignalServiceAddress verifiedAddress = RecipientUtil.toSignalServiceAddress(context, recipient);
        VerifiedMessage verifiedMessage = new VerifiedMessage(verifiedAddress, new IdentityKey(identityKey, 0), verifiedState, timestamp);
        messageSender.sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage), UnidentifiedAccessUtil.getAccessFor(context, recipient));
    } catch (InvalidKeyException e) {
        throw new IOException(e);
    }
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IOException(java.io.IOException) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Aggregations

VerifiedMessage (org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)14 IdentityKey (org.whispersystems.libsignal.IdentityKey)9 Recipient (org.thoughtcrime.securesms.recipients.Recipient)7 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)7 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)6 DeviceContact (org.whispersystems.signalservice.api.messages.multidevice.DeviceContact)5 DeviceContactsOutputStream (org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream)5 InvalidNumberException (org.whispersystems.signalservice.api.util.InvalidNumberException)5 LinkedList (java.util.LinkedList)4 IdentityRecord (org.thoughtcrime.securesms.database.model.IdentityRecord)4 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)4 ReadMessage (org.whispersystems.signalservice.api.messages.multidevice.ReadMessage)4 RequestMessage (org.whispersystems.signalservice.api.messages.multidevice.RequestMessage)4 SentTranscriptMessage (org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage)4 SignalServiceSyncMessage (org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage)4 ByteString (com.google.protobuf.ByteString)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ProtocolInvalidKeyException (org.signal.libsignal.metadata.ProtocolInvalidKeyException)3 BlockedListMessage (org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage)3