Merge branch 'krille/check-if-verified-by-any-master-key' into 'main'

feat: Check if a key is verified by any master key

See merge request famedly/company/frontend/famedlysdk!1173
This commit is contained in:
Mohammad Reza Moradi 2022-11-24 13:11:07 +00:00
commit 67e1b9b253
2 changed files with 36 additions and 15 deletions

View File

@ -160,10 +160,13 @@ class KeyVerification {
} }
Future<void> sendStart() async { Future<void> sendStart() async {
await send(EventTypes.KeyVerificationRequest, { await send(
'methods': knownVerificationMethods, EventTypes.KeyVerificationRequest,
if (room == null) 'timestamp': DateTime.now().millisecondsSinceEpoch, {
}); 'methods': knownVerificationMethods,
if (room == null) 'timestamp': DateTime.now().millisecondsSinceEpoch,
},
);
startedVerification = true; startedVerification = true;
setState(KeyVerificationState.waitingAccept); setState(KeyVerificationState.waitingAccept);
lastActivity = DateTime.now(); lastActivity = DateTime.now();
@ -604,7 +607,10 @@ class KeyVerification {
} }
} }
Future<void> send(String type, Map<String, dynamic> payload) async { Future<void> send(
String type,
Map<String, dynamic> payload,
) async {
makePayload(payload); makePayload(payload);
Logs().i('[Key Verification] Sending type $type: $payload'); Logs().i('[Key Verification] Sending type $type: $payload');
if (room != null) { if (room != null) {
@ -628,7 +634,17 @@ class KeyVerification {
EventTypes.KeyVerificationRequest, EventTypes.KeyVerificationRequest,
EventTypes.KeyVerificationCancel, EventTypes.KeyVerificationCancel,
}.contains(type)) { }.contains(type)) {
await client.sendToDevicesOfUserIds({userId}, type, payload); final deviceKeys = client.userDeviceKeys[userId]?.deviceKeys.values
.where((deviceKey) => deviceKey.hasValidSignatureChain(
verifiedByTheirMasterKey: true));
if (deviceKeys != null) {
await client.sendToDeviceEncrypted(
deviceKeys.toList(),
type,
payload,
);
}
} else { } else {
Logs().e( Logs().e(
'[Key Verification] Tried to broadcast and un-broadcastable type: $type'); '[Key Verification] Tried to broadcast and un-broadcastable type: $type');

View File

@ -93,7 +93,7 @@ class DeviceKeysList {
// verification request that'll happen automatically once we know the transaction id // verification request that'll happen automatically once we know the transaction id
return request; return request;
} else { } else {
// broadcast self-verification // start verification with verified devices
final request = KeyVerification( final request = KeyVerification(
encryption: encryption, userId: userId, deviceId: '*'); encryption: encryption, userId: userId, deviceId: '*');
await request.start(); await request.start();
@ -216,10 +216,14 @@ abstract class SignableKey extends MatrixSignableKey {
return valid; return valid;
} }
bool hasValidSignatureChain( bool hasValidSignatureChain({
{bool verifiedOnly = true, bool verifiedOnly = true,
Set<String>? visited, Set<String>? visited,
Set<String>? onlyValidateUserIds}) { Set<String>? onlyValidateUserIds,
/// Only check if this key is verified by their Master key.
bool verifiedByTheirMasterKey = false,
}) {
if (!client.encryptionEnabled) { if (!client.encryptionEnabled) {
return false; return false;
} }
@ -300,15 +304,16 @@ abstract class SignableKey extends MatrixSignableKey {
if ((verifiedOnly && key.directVerified) || if ((verifiedOnly && key.directVerified) ||
(key is CrossSigningKey && (key is CrossSigningKey &&
key.usage.contains('master') && key.usage.contains('master') &&
key.directVerified && (verifiedByTheirMasterKey ||
key.userId == client.userID)) { (key.directVerified && key.userId == client.userID)))) {
return true; // we verified this key and it is valid...all checks out! return true; // we verified this key and it is valid...all checks out!
} }
// or else we just recurse into that key and chack if it works out // or else we just recurse into that key and check if it works out
final haveChain = key.hasValidSignatureChain( final haveChain = key.hasValidSignatureChain(
verifiedOnly: verifiedOnly, verifiedOnly: verifiedOnly,
visited: visited_, visited: visited_,
onlyValidateUserIds: onlyValidateUserIds); onlyValidateUserIds: onlyValidateUserIds,
verifiedByTheirMasterKey: verifiedByTheirMasterKey);
if (haveChain) { if (haveChain) {
return true; return true;
} }