Search in sources :

Example 76 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress 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)

Example 77 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class MultiDeviceGroupUpdateJob method onRun.

@Override
public void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    if (!TextSecurePreferences.isMultiDevice(context)) {
        Log.i(TAG, "Not multi device, aborting...");
        return;
    }
    if (SignalStore.account().isLinkedDevice()) {
        Log.i(TAG, "Not primary device, aborting...");
        return;
    }
    ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
    InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]);
    Uri uri = BlobProvider.getInstance().forData(inputStream, 0).withFileName("multidevice-group-update").createForSingleSessionOnDiskAsync(context, () -> Log.i(TAG, "Write successful."), e -> Log.w(TAG, "Error during write.", e));
    try (GroupDatabase.Reader reader = SignalDatabase.groups().getGroups()) {
        DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]));
        boolean hasData = false;
        GroupDatabase.GroupRecord record;
        while ((record = reader.getNext()) != null) {
            if (record.isV1Group()) {
                List<SignalServiceAddress> members = new LinkedList<>();
                List<Recipient> registeredMembers = RecipientUtil.getEligibleForSending(Recipient.resolvedList(record.getMembers()));
                for (Recipient member : registeredMembers) {
                    members.add(RecipientUtil.toSignalServiceAddress(context, member));
                }
                RecipientId recipientId = SignalDatabase.recipients().getOrInsertFromPossiblyMigratedGroupId(record.getId());
                Recipient recipient = Recipient.resolved(recipientId);
                Optional<Integer> expirationTimer = recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent();
                Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
                Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
                out.write(new DeviceGroup(record.getId().getDecodedId(), Optional.fromNullable(record.getTitle()), members, getAvatar(record.getRecipientId()), record.isActive(), expirationTimer, Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), recipient.isBlocked(), Optional.fromNullable(inboxPositions.get(recipientId)), archived.contains(recipientId)));
                hasData = true;
            }
        }
        out.close();
        if (hasData) {
            long length = BlobProvider.getInstance().calculateFileSize(context, uri);
            sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, uri), length);
        } else {
            Log.w(TAG, "No groups present for sync message. Sending an empty update.");
            sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), null, 0);
        }
    } finally {
        BlobProvider.getInstance().delete(context, uri);
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) InputStream(java.io.InputStream) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) DeviceGroup(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Uri(android.net.Uri) LinkedList(java.util.LinkedList) ParcelFileDescriptor(android.os.ParcelFileDescriptor) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) DeviceGroupsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream)

Example 78 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class NullMessageSendJob method onRun.

@Override
protected void onRun() throws Exception {
    Recipient recipient = Recipient.resolved(recipientId);
    if (recipient.isGroup()) {
        Log.w(TAG, "Groups are not supported!");
        return;
    }
    if (recipient.isUnregistered()) {
        Log.w(TAG, recipient.getId() + " not registered!");
    }
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    SignalServiceAddress address = RecipientUtil.toSignalServiceAddress(context, recipient);
    Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient);
    try {
        messageSender.sendNullMessage(address, unidentifiedAccess);
    } catch (UntrustedIdentityException e) {
        Log.w(TAG, "Unable to send null message.");
    }
}
Also used : UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair)

Example 79 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class DeviceContactsInputStream method read.

public DeviceContact read() throws IOException {
    int detailsLength = (int) readRawVarint32();
    if (detailsLength == -1) {
        return null;
    }
    byte[] detailsSerialized = new byte[(int) detailsLength];
    Util.readFully(in, detailsSerialized);
    SignalServiceProtos.ContactDetails details = SignalServiceProtos.ContactDetails.parseFrom(detailsSerialized);
    if (!SignalServiceAddress.isValidAddress(details.getUuid(), details.getNumber())) {
        throw new IOException("Missing contact address!");
    }
    SignalServiceAddress address = new SignalServiceAddress(ACI.parseOrThrow(details.getUuid()), details.getNumber());
    Optional<String> name = Optional.fromNullable(details.getName());
    Optional<SignalServiceAttachmentStream> avatar = Optional.absent();
    Optional<String> color = details.hasColor() ? Optional.of(details.getColor()) : Optional.<String>absent();
    Optional<VerifiedMessage> verified = Optional.absent();
    Optional<ProfileKey> profileKey = Optional.absent();
    boolean blocked = false;
    Optional<Integer> expireTimer = Optional.absent();
    Optional<Integer> inboxPosition = Optional.absent();
    boolean archived = false;
    if (details.hasAvatar()) {
        long avatarLength = details.getAvatar().getLength();
        InputStream avatarStream = new LimitedInputStream(in, avatarLength);
        String avatarContentType = details.getAvatar().getContentType();
        avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, false, false, null, null));
    }
    if (details.hasVerified()) {
        try {
            if (!SignalServiceAddress.isValidAddress(details.getVerified().getDestinationUuid(), details.getVerified().getDestinationE164())) {
                throw new InvalidMessageException("Missing Verified address!");
            }
            IdentityKey identityKey = new IdentityKey(details.getVerified().getIdentityKey().toByteArray(), 0);
            SignalServiceAddress destination = new SignalServiceAddress(ACI.parseOrThrow(details.getVerified().getDestinationUuid()), details.getVerified().getDestinationE164());
            VerifiedMessage.VerifiedState state;
            switch(details.getVerified().getState()) {
                case VERIFIED:
                    state = VerifiedMessage.VerifiedState.VERIFIED;
                    break;
                case UNVERIFIED:
                    state = VerifiedMessage.VerifiedState.UNVERIFIED;
                    break;
                case DEFAULT:
                    state = VerifiedMessage.VerifiedState.DEFAULT;
                    break;
                default:
                    throw new InvalidMessageException("Unknown state: " + details.getVerified().getState());
            }
            verified = Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
        } catch (InvalidKeyException | InvalidMessageException e) {
            Log.w(TAG, e);
            verified = Optional.absent();
        }
    }
    if (details.hasProfileKey()) {
        try {
            profileKey = Optional.fromNullable(new ProfileKey(details.getProfileKey().toByteArray()));
        } catch (InvalidInputException e) {
            Log.w(TAG, "Invalid profile key ignored", e);
        }
    }
    if (details.hasExpireTimer() && details.getExpireTimer() > 0) {
        expireTimer = Optional.of(details.getExpireTimer());
    }
    if (details.hasInboxPosition()) {
        inboxPosition = Optional.of(details.getInboxPosition());
    }
    blocked = details.getBlocked();
    archived = details.getArchived();
    return new DeviceContact(address, name, avatar, color, verified, profileKey, blocked, expireTimer, inboxPosition, archived);
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ProfileKey(org.signal.zkgroup.profiles.ProfileKey) SignalServiceAttachmentStream(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) InvalidInputException(org.signal.zkgroup.InvalidInputException) IdentityKey(org.whispersystems.libsignal.IdentityKey) InputStream(java.io.InputStream) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalServiceProtos(org.whispersystems.signalservice.internal.push.SignalServiceProtos)

Example 80 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class SignalServiceAddressProtobufSerializerTest method serialize_and_deserialize_both_address.

@Test
public void serialize_and_deserialize_both_address() {
    SignalServiceAddress address = new SignalServiceAddress(ServiceId.from(UUID.randomUUID()), Optional.of("+15552345678"));
    AddressProto addressProto = org.whispersystems.signalservice.internal.serialize.SignalServiceAddressProtobufSerializer.toProtobuf(address);
    SignalServiceAddress deserialized = org.whispersystems.signalservice.internal.serialize.SignalServiceAddressProtobufSerializer.fromProtobuf(addressProto);
    assertEquals(address, deserialized);
}
Also used : AddressProto(org.whispersystems.signalservice.internal.serialize.protos.AddressProto) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) Test(org.junit.Test)

Aggregations

SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)113 Recipient (org.thoughtcrime.securesms.recipients.Recipient)44 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)32 SendMessageResult (org.whispersystems.signalservice.api.messages.SendMessageResult)28 LinkedList (java.util.LinkedList)27 Optional (org.whispersystems.libsignal.util.guava.Optional)23 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)21 NotPushRegisteredException (org.thoughtcrime.securesms.net.NotPushRegisteredException)20 UnidentifiedAccessPair (org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair)20 NonNull (androidx.annotation.NonNull)18 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)18 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)16 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)15 List (java.util.List)14 ByteString (com.google.protobuf.ByteString)13 Nullable (androidx.annotation.Nullable)12 UntrustedIdentityException (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException)12 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)10