Search in sources :

Example 1 with Verified

use of org.whispersystems.signalservice.internal.push.SignalServiceProtos.Verified in project libsignal-service-java by signalapp.

the class SignalServiceCipher method createSynchronizeMessage.

private SignalServiceSyncMessage createSynchronizeMessage(SignalServiceEnvelope envelope, SyncMessage content) throws InvalidMessageException {
    if (content.hasSent()) {
        SyncMessage.Sent sentContent = content.getSent();
        return SignalServiceSyncMessage.forSentTranscript(new SentTranscriptMessage(sentContent.getDestination(), sentContent.getTimestamp(), createSignalServiceMessage(envelope, sentContent.getMessage()), sentContent.getExpirationStartTimestamp()));
    }
    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()) {
            readMessages.add(new ReadMessage(read.getSender(), read.getTimestamp()));
        }
        return SignalServiceSyncMessage.forRead(readMessages);
    }
    if (content.hasVerified()) {
        try {
            Verified verified = content.getVerified();
            String destination = verified.getDestination();
            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 InvalidMessageException("Unknown state: " + verified.getState().getNumber());
            }
            return SignalServiceSyncMessage.forVerified(new VerifiedMessage(destination, identityKey, verifiedState, System.currentTimeMillis()));
        } catch (InvalidKeyException e) {
            throw new InvalidMessageException(e);
        }
    }
    return SignalServiceSyncMessage.empty();
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) IdentityKey(org.whispersystems.libsignal.IdentityKey) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) LinkedList(java.util.LinkedList) SyncMessage(org.whispersystems.signalservice.internal.push.SignalServiceProtos.SyncMessage) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) Verified(org.whispersystems.signalservice.internal.push.SignalServiceProtos.Verified) ReadMessage(org.whispersystems.signalservice.api.messages.multidevice.ReadMessage) RequestMessage(org.whispersystems.signalservice.api.messages.multidevice.RequestMessage) VerifiedState(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage.VerifiedState) SentTranscriptMessage(org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)

Example 2 with Verified

use of org.whispersystems.signalservice.internal.push.SignalServiceProtos.Verified 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)

Aggregations

LinkedList (java.util.LinkedList)2 IdentityKey (org.whispersystems.libsignal.IdentityKey)2 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 ReadMessage (org.whispersystems.signalservice.api.messages.multidevice.ReadMessage)2 RequestMessage (org.whispersystems.signalservice.api.messages.multidevice.RequestMessage)2 SentTranscriptMessage (org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage)2 SignalServiceSyncMessage (org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage)2 VerifiedMessage (org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)2 VerifiedState (org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage.VerifiedState)2 SyncMessage (org.whispersystems.signalservice.internal.push.SignalServiceProtos.SyncMessage)2 Verified (org.whispersystems.signalservice.internal.push.SignalServiceProtos.Verified)2 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ProtocolInvalidKeyException (org.signal.libsignal.metadata.ProtocolInvalidKeyException)1 ProtocolInvalidMessageException (org.signal.libsignal.metadata.ProtocolInvalidMessageException)1 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)1 BlockedListMessage (org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage)1 ConfigurationMessage (org.whispersystems.signalservice.api.messages.multidevice.ConfigurationMessage)1