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;
}
}
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);
}
}
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;
}
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);
}
Aggregations