Search in sources :

Example 26 with SignalProtocolAddress

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

the class SafetyNumberChangeRepository method trustOrVerifyChangedRecipientsAndResendInternal.

@WorkerThread
private TrustAndVerifyResult trustOrVerifyChangedRecipientsAndResendInternal(@NonNull List<ChangedRecipient> changedRecipients, @NonNull MessageRecord messageRecord) {
    if (changedRecipients.isEmpty()) {
        Log.d(TAG, "No changed recipients to process, will still process message record");
    }
    try (SignalSessionLock.Lock unused = ReentrantSessionLock.INSTANCE.acquire()) {
        for (ChangedRecipient changedRecipient : changedRecipients) {
            SignalProtocolAddress mismatchAddress = changedRecipient.getRecipient().requireServiceId().toProtocolAddress(SignalServiceAddress.DEFAULT_DEVICE_ID);
            Log.d(TAG, "Saving identity for: " + changedRecipient.getRecipient().getId() + " " + changedRecipient.getIdentityRecord().getIdentityKey().hashCode());
            SignalIdentityKeyStore.SaveResult result = ApplicationDependencies.getProtocolStore().aci().identities().saveIdentity(mismatchAddress, changedRecipient.getIdentityRecord().getIdentityKey(), true);
            Log.d(TAG, "Saving identity result: " + result);
            if (result == SignalIdentityKeyStore.SaveResult.NO_CHANGE) {
                Log.i(TAG, "Archiving sessions explicitly as they appear to be out of sync.");
                ApplicationDependencies.getProtocolStore().aci().sessions().archiveSession(changedRecipient.getRecipient().getId(), SignalServiceAddress.DEFAULT_DEVICE_ID);
                ApplicationDependencies.getProtocolStore().aci().sessions().archiveSiblingSessions(mismatchAddress);
                SignalDatabase.senderKeyShared().deleteAllFor(changedRecipient.getRecipient().getId());
            }
        }
    }
    if (messageRecord.isOutgoing()) {
        processOutgoingMessageRecord(changedRecipients, messageRecord);
    }
    return TrustAndVerifyResult.trustVerifyAndResend(changedRecipients, messageRecord);
}
Also used : SignalSessionLock(org.whispersystems.signalservice.api.SignalSessionLock) SignalIdentityKeyStore(org.thoughtcrime.securesms.crypto.storage.SignalIdentityKeyStore) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) WorkerThread(androidx.annotation.WorkerThread)

Example 27 with SignalProtocolAddress

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

the class SenderKeySharedDatabase method getSharedWith.

/**
 * Get the set of recipientIds that know about the distributionId in question.
 */
@NonNull
public Set<SignalProtocolAddress> getSharedWith(@NonNull DistributionId distributionId) {
    SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
    String query = DISTRIBUTION_ID + " = ?";
    String[] args = SqlUtil.buildArgs(distributionId);
    Set<SignalProtocolAddress> addresses = new HashSet<>();
    try (Cursor cursor = db.query(TABLE_NAME, new String[] { ADDRESS, DEVICE }, query, args, null, null, null)) {
        while (cursor.moveToNext()) {
            String address = CursorUtil.requireString(cursor, ADDRESS);
            int device = CursorUtil.requireInt(cursor, DEVICE);
            addresses.add(new SignalProtocolAddress(address, device));
        }
    }
    return addresses;
}
Also used : Cursor(android.database.Cursor) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) HashSet(java.util.HashSet) NonNull(androidx.annotation.NonNull)

Example 28 with SignalProtocolAddress

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

the class SenderKeySharedDatabase method deleteAllFor.

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

Example 29 with SignalProtocolAddress

use of org.whispersystems.libsignal.SignalProtocolAddress in project libsignal-service-java by signalapp.

the class SignalServiceMessageSender method getEncryptedMessage.

private OutgoingPushMessage getEncryptedMessage(PushServiceSocket socket, SignalServiceAddress recipient, Optional<UnidentifiedAccess> unidentifiedAccess, int deviceId, byte[] plaintext) throws IOException, InvalidKeyException, UntrustedIdentityException {
    SignalProtocolAddress signalProtocolAddress = new SignalProtocolAddress(recipient.getIdentifier(), deviceId);
    SignalServiceCipher cipher = new SignalServiceCipher(localAddress, store, 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());
                    SessionBuilder sessionBuilder = 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) UntrustedIdentityException(org.whispersystems.signalservice.api.crypto.UntrustedIdentityException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 30 with SignalProtocolAddress

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

the class TextSecureSessionStore method archiveAllSessions.

public void archiveAllSessions() {
    synchronized (LOCK) {
        List<SessionDatabase.SessionRow> sessions = SignalDatabase.sessions().getAll(accountId);
        for (SessionDatabase.SessionRow row : sessions) {
            row.getRecord().archiveCurrentState();
            storeSession(new SignalProtocolAddress(row.getAddress(), row.getDeviceId()), row.getRecord());
        }
    }
}
Also used : SessionDatabase(org.thoughtcrime.securesms.database.SessionDatabase) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

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