Search in sources :

Example 1 with IdentityStoreRecord

use of org.thoughtcrime.securesms.database.model.IdentityStoreRecord in project Signal-Android by WhisperSystems.

the class SignalBaseIdentityKeyStore method saveIdentity.

@NonNull
public SaveResult saveIdentity(SignalProtocolAddress address, IdentityKey identityKey, boolean nonBlockingApproval) {
    synchronized (LOCK) {
        IdentityStoreRecord identityRecord = cache.get(address.getName());
        RecipientId recipientId = RecipientId.fromExternalPush(address.getName());
        if (identityRecord == null) {
            Log.i(TAG, "Saving new identity...");
            cache.save(address.getName(), recipientId, identityKey, VerifiedStatus.DEFAULT, true, System.currentTimeMillis(), nonBlockingApproval);
            return SaveResult.NEW;
        }
        if (!identityRecord.getIdentityKey().equals(identityKey)) {
            Log.i(TAG, "Replacing existing identity... Existing: " + identityRecord.getIdentityKey().hashCode() + " New: " + identityKey.hashCode());
            VerifiedStatus verifiedStatus;
            if (identityRecord.getVerifiedStatus() == VerifiedStatus.VERIFIED || identityRecord.getVerifiedStatus() == VerifiedStatus.UNVERIFIED) {
                verifiedStatus = VerifiedStatus.UNVERIFIED;
            } else {
                verifiedStatus = VerifiedStatus.DEFAULT;
            }
            cache.save(address.getName(), recipientId, identityKey, verifiedStatus, false, System.currentTimeMillis(), nonBlockingApproval);
            IdentityUtil.markIdentityUpdate(context, recipientId);
            ApplicationDependencies.getProtocolStore().aci().sessions().archiveSiblingSessions(address);
            SignalDatabase.senderKeyShared().deleteAllFor(recipientId);
            return SaveResult.UPDATE;
        }
        if (isNonBlockingApprovalRequired(identityRecord)) {
            Log.i(TAG, "Setting approval status...");
            cache.setApproval(address.getName(), recipientId, identityRecord, nonBlockingApproval);
            return SaveResult.NON_BLOCKING_APPROVAL_REQUIRED;
        }
        return SaveResult.NO_CHANGE;
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) VerifiedStatus(org.thoughtcrime.securesms.database.IdentityDatabase.VerifiedStatus) IdentityStoreRecord(org.thoughtcrime.securesms.database.model.IdentityStoreRecord) NonNull(androidx.annotation.NonNull)

Example 2 with IdentityStoreRecord

use of org.thoughtcrime.securesms.database.model.IdentityStoreRecord in project Signal-Android by WhisperSystems.

the class SignalBaseIdentityKeyStore method isTrustedIdentity.

public boolean isTrustedIdentity(SignalProtocolAddress address, IdentityKey identityKey, IdentityKeyStore.Direction direction) {
    Recipient self = Recipient.self();
    boolean isSelf = address.getName().equals(self.requireServiceId().toString()) || address.getName().equals(self.requireE164());
    if (isSelf) {
        return identityKey.equals(SignalStore.account().getAciIdentityKey().getPublicKey());
    }
    IdentityStoreRecord record = cache.get(address.getName());
    switch(direction) {
        case SENDING:
            return isTrustedForSending(identityKey, record);
        case RECEIVING:
            return true;
        default:
            throw new AssertionError("Unknown direction: " + direction);
    }
}
Also used : Recipient(org.thoughtcrime.securesms.recipients.Recipient) IdentityStoreRecord(org.thoughtcrime.securesms.database.model.IdentityStoreRecord)

Example 3 with IdentityStoreRecord

use of org.thoughtcrime.securesms.database.model.IdentityStoreRecord in project Signal-Android by WhisperSystems.

the class IdentityDatabase method getIdentityStoreRecord.

@Nullable
public IdentityStoreRecord getIdentityStoreRecord(@NonNull String addressName) {
    SQLiteDatabase database = databaseHelper.getSignalReadableDatabase();
    String query = ADDRESS + " = ?";
    String[] args = SqlUtil.buildArgs(addressName);
    try (Cursor cursor = database.query(TABLE_NAME, null, query, args, null, null, null)) {
        if (cursor.moveToFirst()) {
            String serializedIdentity = CursorUtil.requireString(cursor, IDENTITY_KEY);
            long timestamp = CursorUtil.requireLong(cursor, TIMESTAMP);
            int verifiedStatus = CursorUtil.requireInt(cursor, VERIFIED);
            boolean nonblockingApproval = CursorUtil.requireBoolean(cursor, NONBLOCKING_APPROVAL);
            boolean firstUse = CursorUtil.requireBoolean(cursor, FIRST_USE);
            return new IdentityStoreRecord(addressName, new IdentityKey(Base64.decode(serializedIdentity), 0), VerifiedStatus.forState(verifiedStatus), firstUse, timestamp, nonblockingApproval);
        } else if (UuidUtil.isUuid(addressName)) {
            if (SignalDatabase.recipients().containsPhoneOrUuid(addressName)) {
                Recipient recipient = Recipient.external(context, addressName);
                if (recipient.hasE164() && !UuidUtil.isUuid(recipient.requireE164())) {
                    Log.i(TAG, "Could not find identity for UUID. Attempting E164.");
                    return getIdentityStoreRecord(recipient.requireE164());
                } else {
                    Log.i(TAG, "Could not find identity for UUID, and our recipient doesn't have an E164.");
                }
            } else {
                Log.i(TAG, "Could not find identity for UUID, and we don't have a recipient.");
            }
        } else {
            Log.i(TAG, "Could not find identity for E164 either.");
        }
    } catch (InvalidKeyException | IOException e) {
        throw new AssertionError(e);
    }
    return null;
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) IdentityStoreRecord(org.thoughtcrime.securesms.database.model.IdentityStoreRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException) Cursor(android.database.Cursor) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) Nullable(androidx.annotation.Nullable)

Example 4 with IdentityStoreRecord

use of org.thoughtcrime.securesms.database.model.IdentityStoreRecord in project Signal-Android by WhisperSystems.

the class SignalBaseIdentityKeyStore method getIdentityRecords.

@NonNull
public IdentityRecordList getIdentityRecords(@NonNull List<Recipient> recipients) {
    List<String> addressNames = recipients.stream().filter(Recipient::hasServiceId).map(Recipient::requireServiceId).map(ServiceId::toString).collect(Collectors.toList());
    if (addressNames.isEmpty()) {
        return IdentityRecordList.EMPTY;
    }
    List<IdentityRecord> records = new ArrayList<>(recipients.size());
    for (Recipient recipient : recipients) {
        if (recipient.hasServiceId()) {
            IdentityStoreRecord record = cache.get(recipient.requireServiceId().toString());
            if (record != null) {
                records.add(record.toIdentityRecord(recipient.getId()));
            }
        } else {
            Log.w(TAG, "[getIdentityRecords] No serviceId for " + recipient.getId());
        }
    }
    return new IdentityRecordList(records);
}
Also used : IdentityRecordList(org.thoughtcrime.securesms.database.identity.IdentityRecordList) ArrayList(java.util.ArrayList) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IdentityStoreRecord(org.thoughtcrime.securesms.database.model.IdentityStoreRecord) NonNull(androidx.annotation.NonNull)

Aggregations

IdentityStoreRecord (org.thoughtcrime.securesms.database.model.IdentityStoreRecord)4 Recipient (org.thoughtcrime.securesms.recipients.Recipient)3 NonNull (androidx.annotation.NonNull)2 Cursor (android.database.Cursor)1 Nullable (androidx.annotation.Nullable)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 VerifiedStatus (org.thoughtcrime.securesms.database.IdentityDatabase.VerifiedStatus)1 IdentityRecordList (org.thoughtcrime.securesms.database.identity.IdentityRecordList)1 IdentityRecord (org.thoughtcrime.securesms.database.model.IdentityRecord)1 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)1 IdentityKey (org.whispersystems.libsignal.IdentityKey)1 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)1