Search in sources :

Example 1 with UnidentifiedAccessPair

use of org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair in project Signal-Android by WhisperSystems.

the class AutomaticSessionResetJob method sendNullMessage.

private void sendNullMessage() throws IOException {
    Recipient recipient = Recipient.resolved(recipientId);
    if (recipient.isUnregistered()) {
        Log.w(TAG, recipient.getId() + " not registered!");
        return;
    }
    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 2 with UnidentifiedAccessPair

use of org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair in project Signal-Android by WhisperSystems.

the class UnidentifiedAccessUtil method getAccessForSync.

public static Optional<UnidentifiedAccessPair> getAccessForSync(@NonNull Context context) {
    try {
        byte[] ourUnidentifiedAccessKey = UnidentifiedAccess.deriveAccessKeyFrom(ProfileKeyUtil.getSelfProfileKey());
        byte[] ourUnidentifiedAccessCertificate = getUnidentifiedAccessCertificate(Recipient.self());
        if (TextSecurePreferences.isUniversalUnidentifiedAccess(context)) {
            ourUnidentifiedAccessKey = UNRESTRICTED_KEY;
        }
        if (ourUnidentifiedAccessCertificate != null) {
            return Optional.of(new UnidentifiedAccessPair(new UnidentifiedAccess(ourUnidentifiedAccessKey, ourUnidentifiedAccessCertificate), new UnidentifiedAccess(ourUnidentifiedAccessKey, ourUnidentifiedAccessCertificate)));
        }
        return Optional.absent();
    } catch (InvalidCertificateException e) {
        Log.w(TAG, e);
        return Optional.absent();
    }
}
Also used : UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) InvalidCertificateException(org.signal.libsignal.metadata.certificate.InvalidCertificateException) UnidentifiedAccess(org.whispersystems.signalservice.api.crypto.UnidentifiedAccess)

Example 3 with UnidentifiedAccessPair

use of org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair in project Signal-Android by WhisperSystems.

the class UnidentifiedAccessUtil method getAccessFor.

@WorkerThread
public static List<Optional<UnidentifiedAccessPair>> getAccessFor(@NonNull Context context, @NonNull List<Recipient> recipients, boolean log) {
    byte[] ourUnidentifiedAccessKey = UnidentifiedAccess.deriveAccessKeyFrom(ProfileKeyUtil.getSelfProfileKey());
    if (TextSecurePreferences.isUniversalUnidentifiedAccess(context)) {
        ourUnidentifiedAccessKey = UNRESTRICTED_KEY;
    }
    List<Optional<UnidentifiedAccessPair>> access = new ArrayList<>(recipients.size());
    Map<CertificateType, Integer> typeCounts = new HashMap<>();
    for (Recipient recipient : recipients) {
        byte[] theirUnidentifiedAccessKey = getTargetUnidentifiedAccessKey(recipient);
        CertificateType certificateType = getUnidentifiedAccessCertificateType(recipient);
        byte[] ourUnidentifiedAccessCertificate = SignalStore.certificateValues().getUnidentifiedAccessCertificate(certificateType);
        int typeCount = Util.getOrDefault(typeCounts, certificateType, 0);
        typeCount++;
        typeCounts.put(certificateType, typeCount);
        if (theirUnidentifiedAccessKey != null && ourUnidentifiedAccessCertificate != null) {
            try {
                access.add(Optional.of(new UnidentifiedAccessPair(new UnidentifiedAccess(theirUnidentifiedAccessKey, ourUnidentifiedAccessCertificate), new UnidentifiedAccess(ourUnidentifiedAccessKey, ourUnidentifiedAccessCertificate))));
            } catch (InvalidCertificateException e) {
                Log.w(TAG, e);
                access.add(Optional.absent());
            }
        } else {
            access.add(Optional.absent());
        }
    }
    int unidentifiedCount = Stream.of(access).filter(Optional::isPresent).toList().size();
    int otherCount = access.size() - unidentifiedCount;
    if (log) {
        Log.i(TAG, "Unidentified: " + unidentifiedCount + ", Other: " + otherCount + ". Types: " + typeCounts);
    }
    return access;
}
Also used : Optional(org.whispersystems.libsignal.util.guava.Optional) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Recipient(org.thoughtcrime.securesms.recipients.Recipient) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) UnidentifiedAccess(org.whispersystems.signalservice.api.crypto.UnidentifiedAccess) CertificateType(org.thoughtcrime.securesms.keyvalue.CertificateType) InvalidCertificateException(org.signal.libsignal.metadata.certificate.InvalidCertificateException) WorkerThread(androidx.annotation.WorkerThread)

Example 4 with UnidentifiedAccessPair

use of org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair in project Signal-Android by WhisperSystems.

the class ResendMessageJob method onRun.

@Override
protected void onRun() throws Exception {
    if (SignalStore.internalValues().delayResends()) {
        Log.w(TAG, "Delaying resend by 10 sec because of an internal preference.");
        ThreadUtil.sleep(10000);
    }
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    Recipient recipient = Recipient.resolved(recipientId);
    if (recipient.isUnregistered()) {
        Log.w(TAG, recipient.getId() + " is unregistered!");
        return;
    }
    SignalServiceAddress address = RecipientUtil.toSignalServiceAddress(context, recipient);
    Optional<UnidentifiedAccessPair> access = UnidentifiedAccessUtil.getAccessFor(context, recipient);
    Content contentToSend = content;
    if (distributionId != null) {
        Optional<GroupRecord> groupRecord = SignalDatabase.groups().getGroupByDistributionId(distributionId);
        if (!groupRecord.isPresent()) {
            Log.w(TAG, "Could not find a matching group for the distributionId! Skipping message send.");
            return;
        } else if (!groupRecord.get().getMembers().contains(recipientId)) {
            Log.w(TAG, "The target user is no longer in the group! Skipping message send.");
            return;
        }
        SenderKeyDistributionMessage senderKeyDistributionMessage = messageSender.getOrCreateNewGroupSession(distributionId);
        ByteString distributionBytes = ByteString.copyFrom(senderKeyDistributionMessage.serialize());
        contentToSend = contentToSend.toBuilder().setSenderKeyDistributionMessage(distributionBytes).build();
    }
    SendMessageResult result = messageSender.resendContent(address, access, sentTimestamp, contentToSend, contentHint, Optional.fromNullable(groupId).transform(GroupId::getDecodedId));
    if (result.isSuccess() && distributionId != null) {
        List<SignalProtocolAddress> addresses = result.getSuccess().getDevices().stream().map(device -> recipient.requireServiceId().toProtocolAddress(device)).collect(Collectors.toList());
        ApplicationDependencies.getProtocolStore().aci().markSenderKeySharedWith(distributionId, addresses);
    }
}
Also used : SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) ContentHint(org.whispersystems.signalservice.api.crypto.ContentHint) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) NonNull(androidx.annotation.NonNull) Data(org.thoughtcrime.securesms.jobmanager.Data) RecipientUtil(org.thoughtcrime.securesms.recipients.RecipientUtil) GroupRecord(org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) Recipient(org.thoughtcrime.securesms.recipients.Recipient) DistributionId(org.whispersystems.signalservice.api.push.DistributionId) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) SenderKeyDistributionMessage(org.whispersystems.libsignal.protocol.SenderKeyDistributionMessage) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint) UnidentifiedAccessUtil(org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil) ThreadUtil(org.signal.core.util.ThreadUtil) Collectors(java.util.stream.Collectors) Optional(org.whispersystems.libsignal.util.guava.Optional) ByteString(com.google.protobuf.ByteString) TimeUnit(java.util.concurrent.TimeUnit) Log(org.signal.core.util.logging.Log) List(java.util.List) Nullable(androidx.annotation.Nullable) GroupId(org.thoughtcrime.securesms.groups.GroupId) Content(org.whispersystems.signalservice.internal.push.SignalServiceProtos.Content) Job(org.thoughtcrime.securesms.jobmanager.Job) ByteString(com.google.protobuf.ByteString) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) GroupRecord(org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) Content(org.whispersystems.signalservice.internal.push.SignalServiceProtos.Content) SenderKeyDistributionMessage(org.whispersystems.libsignal.protocol.SenderKeyDistributionMessage) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 5 with UnidentifiedAccessPair

use of org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair in project Signal-Android by WhisperSystems.

the class SendRetryReceiptJob method onRun.

@Override
protected void onRun() throws Exception {
    Recipient recipient = Recipient.resolved(recipientId);
    if (recipient.isUnregistered()) {
        Log.w(TAG, recipient.getId() + " not registered!");
        return;
    }
    SignalServiceAddress address = RecipientUtil.toSignalServiceAddress(context, recipient);
    Optional<UnidentifiedAccessPair> access = UnidentifiedAccessUtil.getAccessFor(context, recipient);
    Optional<byte[]> group = groupId.transform(GroupId::getDecodedId);
    Log.i(TAG, "Sending retry receipt for " + errorMessage.getTimestamp() + " to " + recipientId + ", device: " + errorMessage.getDeviceId());
    ApplicationDependencies.getSignalServiceMessageSender().sendRetryReceipt(address, access, group, errorMessage);
}
Also used : Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) GroupId(org.thoughtcrime.securesms.groups.GroupId)

Aggregations

UnidentifiedAccessPair (org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair)24 Recipient (org.thoughtcrime.securesms.recipients.Recipient)20 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)20 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)16 SendMessageResult (org.whispersystems.signalservice.api.messages.SendMessageResult)14 Optional (org.whispersystems.libsignal.util.guava.Optional)10 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)10 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 GroupId (org.thoughtcrime.securesms.groups.GroupId)8 UnidentifiedAccess (org.whispersystems.signalservice.api.crypto.UnidentifiedAccess)8 UntrustedIdentityException (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException)8 DistributionId (org.whispersystems.signalservice.api.push.DistributionId)8 NonNull (androidx.annotation.NonNull)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 TimeUnit (java.util.concurrent.TimeUnit)6 Log (org.signal.core.util.logging.Log)6 UnidentifiedAccessUtil (org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil)6 SignalDatabase (org.thoughtcrime.securesms.database.SignalDatabase)6