Search in sources :

Example 66 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Signal-Android by signalapp.

the class SignalServiceMessageSender method getEncryptedMessage.

private OutgoingPushMessage getEncryptedMessage(PushServiceSocket socket, SignalServiceAddress recipient, Optional<UnidentifiedAccess> unidentifiedAccess, int deviceId, EnvelopeContent plaintext) throws IOException, InvalidKeyException, UntrustedIdentityException {
    SignalProtocolAddress signalProtocolAddress = new SignalProtocolAddress(recipient.getIdentifier(), deviceId);
    SignalServiceCipher cipher = new SignalServiceCipher(localAddress, localDeviceId, store, sessionLock, null);
    if (!store.containsSession(signalProtocolAddress)) {
        try {
            List<PreKeyBundle> preKeys = socket.getPreKeys(recipient, unidentifiedAccess, deviceId);
            for (PreKeyBundle preKey : preKeys) {
                try {
                    SignalProtocolAddress preKeyAddress = new SignalProtocolAddress(recipient.getIdentifier(), preKey.getDeviceId());
                    SignalSessionBuilder sessionBuilder = new SignalSessionBuilder(sessionLock, new SessionBuilder(store, preKeyAddress));
                    sessionBuilder.process(preKey);
                } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
                    throw new UntrustedIdentityException("Untrusted identity key!", recipient.getIdentifier(), preKey.getIdentityKey());
                }
            }
            if (eventListener.isPresent()) {
                eventListener.get().onSecurityEvent(recipient);
            }
        } catch (InvalidKeyException e) {
            throw new IOException(e);
        }
    }
    try {
        return cipher.encrypt(signalProtocolAddress, unidentifiedAccess, plaintext);
    } catch (org.whispersystems.libsignal.UntrustedIdentityException e) {
        throw new UntrustedIdentityException("Untrusted on send", recipient.getIdentifier(), e.getUntrustedIdentity());
    }
}
Also used : PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) SignalGroupSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalGroupSessionBuilder) GroupSessionBuilder(org.whispersystems.libsignal.groups.GroupSessionBuilder) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) SignalSessionBuilder(org.whispersystems.signalservice.api.crypto.SignalSessionBuilder) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 67 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Signal-Android by signalapp.

the class SenderKeyDistributionSendJob method onRun.

@Override
protected void onRun() throws Exception {
    GroupDatabase groupDatabase = SignalDatabase.groups();
    if (!groupDatabase.isCurrentMember(groupId, recipientId)) {
        Log.w(TAG, recipientId + " is no longer a member of " + groupId + "! Not sending.");
        return;
    }
    Recipient recipient = Recipient.resolved(recipientId);
    if (recipient.getSenderKeyCapability() != Recipient.Capability.SUPPORTED) {
        Log.w(TAG, recipientId + " does not support sender key! Not sending.");
        return;
    }
    if (recipient.isUnregistered()) {
        Log.w(TAG, recipient.getId() + " not registered!");
        return;
    }
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    List<SignalServiceAddress> address = Collections.singletonList(RecipientUtil.toSignalServiceAddress(context, recipient));
    DistributionId distributionId = groupDatabase.getOrCreateDistributionId(groupId);
    SenderKeyDistributionMessage message = messageSender.getOrCreateNewGroupSession(distributionId);
    List<Optional<UnidentifiedAccessPair>> access = UnidentifiedAccessUtil.getAccessFor(context, Collections.singletonList(recipient));
    SendMessageResult result = messageSender.sendSenderKeyDistributionMessage(distributionId, address, access, message, groupId.getDecodedId()).get(0);
    if (result.isSuccess()) {
        List<SignalProtocolAddress> addresses = result.getSuccess().getDevices().stream().map(device -> recipient.requireServiceId().toProtocolAddress(device)).collect(Collectors.toList());
        ApplicationDependencies.getProtocolStore().aci().markSenderKeySharedWith(distributionId, addresses);
    }
}
Also used : SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) NonNull(androidx.annotation.NonNull) Data(org.thoughtcrime.securesms.jobmanager.Data) RecipientUtil(org.thoughtcrime.securesms.recipients.RecipientUtil) 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) 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) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) Collectors(java.util.stream.Collectors) Optional(org.whispersystems.libsignal.util.guava.Optional) TimeUnit(java.util.concurrent.TimeUnit) Log(org.signal.core.util.logging.Log) List(java.util.List) GroupId(org.thoughtcrime.securesms.groups.GroupId) Job(org.thoughtcrime.securesms.jobmanager.Job) Collections(java.util.Collections) Optional(org.whispersystems.libsignal.util.guava.Optional) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) DistributionId(org.whispersystems.signalservice.api.push.DistributionId) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) SenderKeyDistributionMessage(org.whispersystems.libsignal.protocol.SenderKeyDistributionMessage) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 68 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Signal-Android by signalapp.

the class SenderKeySharedDatabase method markAsShared.

/**
 * Mark that a distributionId has been shared with the provided recipients
 */
public void markAsShared(@NonNull DistributionId distributionId, @NonNull Collection<SignalProtocolAddress> addresses) {
    SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
    db.beginTransaction();
    try {
        for (SignalProtocolAddress address : addresses) {
            ContentValues values = new ContentValues();
            values.put(ADDRESS, address.getName());
            values.put(DEVICE, address.getDeviceId());
            values.put(DISTRIBUTION_ID, distributionId.toString());
            values.put(TIMESTAMP, System.currentTimeMillis());
            db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 69 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Signal-Android by signalapp.

the class SenderKeySharedDatabase method delete.

/**
 * Clear the shared statuses for all provided addresses.
 */
public void delete(@NonNull DistributionId distributionId, @NonNull Collection<SignalProtocolAddress> addresses) {
    SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
    String query = DISTRIBUTION_ID + " = ? AND " + ADDRESS + " = ? AND " + DEVICE + " = ?";
    db.beginTransaction();
    try {
        for (SignalProtocolAddress address : addresses) {
            db.delete(TABLE_NAME, query, SqlUtil.buildArgs(distributionId, address.getName(), address.getDeviceId()));
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
Also used : SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 70 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project Signal-Android by signalapp.

the class MessageContentProcessor method ratchetKeyMatches.

public static boolean ratchetKeyMatches(@NonNull Recipient recipient, int deviceId, @NonNull ECPublicKey ratchetKey) {
    SignalProtocolAddress address = recipient.resolve().requireServiceId().toProtocolAddress(deviceId);
    SessionRecord session = ApplicationDependencies.getProtocolStore().aci().loadSession(address);
    return session.currentRatchetKeyMatches(ratchetKey);
}
Also used : SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) SessionRecord(org.whispersystems.libsignal.state.SessionRecord)

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