famedlysdk/lib/src/utils/device_keys_list.dart

374 lines
12 KiB
Dart
Raw Normal View History

2020-02-04 13:41:13 +00:00
import 'dart:convert';
import 'package:canonical_json/canonical_json.dart';
import 'package:olm/olm.dart' as olm;
2020-02-04 13:41:13 +00:00
import '../../encryption.dart';
import '../../matrix_api.dart';
2020-02-04 13:41:13 +00:00
import '../client.dart';
2020-05-22 11:18:45 +00:00
import '../database/database.dart'
show DbUserDeviceKey, DbUserDeviceKeysKey, DbUserCrossSigningKey;
2020-05-15 18:40:17 +00:00
import '../event.dart';
import '../room.dart';
import '../user.dart';
2020-02-04 13:41:13 +00:00
enum UserVerifiedStatus { verified, unknown, unknownDevice }
2020-02-04 13:41:13 +00:00
class DeviceKeysList {
Client client;
2020-02-04 13:41:13 +00:00
String userId;
bool outdated = true;
Map<String, DeviceKeys> deviceKeys = {};
Map<String, CrossSigningKey> crossSigningKeys = {};
2020-02-04 13:41:13 +00:00
2020-06-06 12:28:18 +00:00
SignableKey getKey(String id) {
2020-05-25 13:30:53 +00:00
if (deviceKeys.containsKey(id)) {
return deviceKeys[id];
}
if (crossSigningKeys.containsKey(id)) {
return crossSigningKeys[id];
}
return null;
}
2020-05-30 11:55:09 +00:00
CrossSigningKey getCrossSigningKey(String type) => crossSigningKeys.values
.firstWhere((k) => k.usage.contains(type), orElse: () => null);
2020-05-25 13:30:53 +00:00
CrossSigningKey get masterKey => getCrossSigningKey('master');
CrossSigningKey get selfSigningKey => getCrossSigningKey('self_signing');
CrossSigningKey get userSigningKey => getCrossSigningKey('user_signing');
UserVerifiedStatus get verified {
if (masterKey == null) {
return UserVerifiedStatus.unknown;
}
if (masterKey.verified) {
for (final key in deviceKeys.values) {
if (!key.verified) {
return UserVerifiedStatus.unknownDevice;
}
}
return UserVerifiedStatus.verified;
}
return UserVerifiedStatus.unknown;
}
Future<KeyVerification> startVerification() async {
final roomId =
await User(userId, room: Room(client: client)).startDirectChat();
if (roomId == null) {
throw 'Unable to start new room';
}
final room = client.getRoomById(roomId) ?? Room(id: roomId, client: client);
2020-06-06 11:47:37 +00:00
final request = KeyVerification(
encryption: client.encryption, room: room, userId: userId);
await request.start();
// no need to add to the request client object. As we are doing a room
// verification request that'll happen automatically once we know the transaction id
return request;
}
2020-05-22 11:18:45 +00:00
DeviceKeysList.fromDb(
DbUserDeviceKey dbEntry,
List<DbUserDeviceKeysKey> childEntries,
List<DbUserCrossSigningKey> crossSigningEntries,
Client cl) {
client = cl;
2020-05-15 18:40:17 +00:00
userId = dbEntry.userId;
outdated = dbEntry.outdated;
deviceKeys = {};
for (final childEntry in childEntries) {
final entry = DeviceKeys.fromDb(childEntry, client);
2020-05-20 07:37:32 +00:00
if (entry.isValid) {
deviceKeys[childEntry.deviceId] = entry;
} else {
outdated = true;
}
2020-05-15 18:40:17 +00:00
}
for (final crossSigningEntry in crossSigningEntries) {
final entry = CrossSigningKey.fromDb(crossSigningEntry, client);
if (entry.isValid) {
crossSigningKeys[crossSigningEntry.publicKey] = entry;
} else {
outdated = true;
}
}
2020-05-15 18:40:17 +00:00
}
2020-06-15 08:26:50 +00:00
DeviceKeysList(this.userId, this.client);
2020-02-04 13:41:13 +00:00
}
2020-06-06 12:28:18 +00:00
abstract class SignableKey extends MatrixSignableKey {
Client client;
Map<String, dynamic> validSignatures;
bool _verified;
2020-02-04 13:41:13 +00:00
bool blocked;
String get ed25519Key => keys['ed25519:$identifier'];
2020-05-21 14:52:25 +00:00
bool get verified => (directVerified || crossVerified) && !blocked;
void setDirectVerified(bool v) {
_verified = v;
}
bool get directVerified => _verified;
2020-05-30 11:22:34 +00:00
bool get crossVerified => hasValidSignatureChain();
2020-05-27 15:37:14 +00:00
bool get signed => hasValidSignatureChain(verifiedOnly: false);
2020-06-06 12:28:18 +00:00
SignableKey.fromJson(Map<String, dynamic> json, Client cl)
: client = cl,
super.fromJson(json) {
_verified = false;
blocked = false;
}
2020-06-06 13:17:05 +00:00
MatrixSignableKey cloneForSigning() {
final newKey =
MatrixSignableKey.fromJson(Map<String, dynamic>.from(toJson()));
2020-06-06 13:43:18 +00:00
newKey.identifier = identifier;
2020-06-06 13:19:44 +00:00
newKey.signatures ??= <String, Map<String, String>>{};
2020-06-06 13:17:05 +00:00
newKey.signatures.clear();
return newKey;
}
2020-05-25 13:30:53 +00:00
String get signingContent {
2020-06-06 12:28:18 +00:00
final data = Map<String, dynamic>.from(super.toJson());
// some old data might have the custom verified and blocked keys
data.remove('verified');
data.remove('blocked');
// remove the keys not needed for signing
data.remove('unsigned');
data.remove('signatures');
return String.fromCharCodes(canonicalJson.encode(data));
}
bool _verifySignature(String pubKey, String signature) {
final olmutil = olm.Utility();
var valid = false;
try {
2020-05-25 13:30:53 +00:00
olmutil.ed25519_verify(pubKey, signingContent, signature);
valid = true;
} catch (_) {
// bad signature
valid = false;
} finally {
olmutil.free();
}
return valid;
}
bool hasValidSignatureChain({bool verifiedOnly = true, Set<String> visited}) {
2020-06-12 14:25:26 +00:00
if (!client.encryptionEnabled) {
return false;
}
2020-05-21 13:32:06 +00:00
visited ??= <String>{};
final setKey = '${userId};${identifier}';
if (visited.contains(setKey)) {
return false; // prevent recursion
}
visited.add(setKey);
for (final signatureEntries in signatures.entries) {
final otherUserId = signatureEntries.key;
2020-05-22 11:18:45 +00:00
if (!(signatureEntries.value is Map) ||
!client.userDeviceKeys.containsKey(otherUserId)) {
continue;
}
for (final signatureEntry in signatureEntries.value.entries) {
final fullKeyId = signatureEntry.key;
final signature = signatureEntry.value;
if (!(fullKeyId is String) || !(signature is String)) {
continue;
}
final keyId = fullKeyId.substring('ed25519:'.length);
2020-06-06 12:28:18 +00:00
SignableKey key;
if (client.userDeviceKeys[otherUserId].deviceKeys.containsKey(keyId)) {
key = client.userDeviceKeys[otherUserId].deviceKeys[keyId];
2020-05-22 11:18:45 +00:00
} else if (client.userDeviceKeys[otherUserId].crossSigningKeys
.containsKey(keyId)) {
key = client.userDeviceKeys[otherUserId].crossSigningKeys[keyId];
} else {
continue;
}
if (key.blocked) {
continue; // we can't be bothered about this keys signatures
}
var haveValidSignature = false;
var gotSignatureFromCache = false;
2020-05-22 11:18:45 +00:00
if (validSignatures != null &&
validSignatures.containsKey(otherUserId) &&
validSignatures[otherUserId].containsKey(fullKeyId)) {
if (validSignatures[otherUserId][fullKeyId] == true) {
haveValidSignature = true;
gotSignatureFromCache = true;
} else if (validSignatures[otherUserId][fullKeyId] == false) {
haveValidSignature = false;
gotSignatureFromCache = true;
}
}
if (!gotSignatureFromCache) {
// validate the signature manually
haveValidSignature = _verifySignature(key.ed25519Key, signature);
validSignatures ??= <String, dynamic>{};
if (!validSignatures.containsKey(otherUserId)) {
validSignatures[otherUserId] = <String, dynamic>{};
}
validSignatures[otherUserId][fullKeyId] = haveValidSignature;
}
if (!haveValidSignature) {
// no valid signature, this key is useless
continue;
}
2020-05-27 15:37:14 +00:00
if ((verifiedOnly && key.directVerified) ||
(key is CrossSigningKey &&
key.usage.contains('master') &&
2020-05-27 15:37:14 +00:00
key.directVerified &&
key.userId == client.userID)) {
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
2020-05-27 15:37:14 +00:00
final haveChain = key.hasValidSignatureChain(
verifiedOnly: verifiedOnly, visited: visited);
if (haveChain) {
return true;
}
}
}
return false;
}
2020-06-21 19:48:06 +00:00
void setVerified(bool newVerified, [bool sign = true]) {
_verified = newVerified;
2020-06-15 08:26:50 +00:00
if (newVerified &&
sign &&
2020-06-06 11:47:37 +00:00
client.encryptionEnabled &&
client.encryption.crossSigning.signable([this])) {
// sign the key!
2020-06-05 20:03:28 +00:00
client.encryption.crossSigning.sign([this]);
}
}
2020-05-25 13:30:53 +00:00
2020-05-29 07:06:36 +00:00
Future<void> setBlocked(bool newBlocked);
2020-05-25 13:30:53 +00:00
2020-06-06 12:28:18 +00:00
@override
Map<String, dynamic> toJson() {
2020-06-06 12:28:18 +00:00
final data = Map<String, dynamic>.from(super.toJson());
// some old data may have the verified and blocked keys which are unneeded now
data.remove('verified');
data.remove('blocked');
return data;
}
@override
String toString() => json.encode(toJson());
}
2020-06-06 12:28:18 +00:00
class CrossSigningKey extends SignableKey {
String get publicKey => identifier;
List<String> usage;
2020-05-22 11:18:45 +00:00
bool get isValid =>
userId != null && publicKey != null && keys != null && ed25519Key != null;
2020-05-25 13:30:53 +00:00
@override
2020-05-29 07:06:36 +00:00
Future<void> setVerified(bool newVerified, [bool sign = true]) {
super.setVerified(newVerified, sign);
2020-05-29 07:06:36 +00:00
return client.database?.setVerifiedUserCrossSigningKey(
2020-05-22 11:18:45 +00:00
newVerified, client.id, userId, publicKey);
}
2020-05-25 13:30:53 +00:00
@override
2020-05-29 07:06:36 +00:00
Future<void> setBlocked(bool newBlocked) {
blocked = newBlocked;
2020-05-29 07:06:36 +00:00
return client.database?.setBlockedUserCrossSigningKey(
2020-05-22 11:18:45 +00:00
newBlocked, client.id, userId, publicKey);
}
2020-06-06 12:28:18 +00:00
CrossSigningKey.fromMatrixCrossSigningKey(MatrixCrossSigningKey k, Client cl)
: super.fromJson(Map<String, dynamic>.from(k.toJson()), cl) {
final json = toJson();
2020-06-05 20:03:28 +00:00
identifier = k.publicKey;
2020-06-06 12:28:18 +00:00
usage = json['usage'].cast<String>();
2020-06-05 20:03:28 +00:00
}
2020-06-06 12:28:18 +00:00
CrossSigningKey.fromDb(DbUserCrossSigningKey dbEntry, Client cl)
: super.fromJson(Event.getMapFromPayload(dbEntry.content), cl) {
final json = toJson();
identifier = dbEntry.publicKey;
usage = json['usage'].cast<String>();
_verified = dbEntry.verified;
blocked = dbEntry.blocked;
}
2020-06-06 12:28:18 +00:00
CrossSigningKey.fromJson(Map<String, dynamic> json, Client cl)
: super.fromJson(Map<String, dynamic>.from(json), cl) {
final json = toJson();
usage = json['usage'].cast<String>();
2020-06-06 12:28:18 +00:00
if (keys != null && keys.isNotEmpty) {
identifier = keys.values.first;
}
}
}
2020-06-06 12:28:18 +00:00
class DeviceKeys extends SignableKey {
String get deviceId => identifier;
List<String> algorithms;
String get curve25519Key => keys['curve25519:$deviceId'];
2020-06-21 19:38:26 +00:00
String get deviceDisplayName =>
unsigned != null ? unsigned['device_display_name'] : null;
2020-05-22 10:12:18 +00:00
bool get isValid =>
userId != null &&
deviceId != null &&
keys != null &&
2020-05-22 10:12:18 +00:00
curve25519Key != null &&
ed25519Key != null;
2020-05-20 07:37:32 +00:00
2020-05-25 13:30:53 +00:00
@override
2020-05-29 07:06:36 +00:00
Future<void> setVerified(bool newVerified, [bool sign = true]) {
super.setVerified(newVerified, sign);
2020-06-05 20:03:28 +00:00
return client?.database
2020-05-22 10:12:18 +00:00
?.setVerifiedUserDeviceKey(newVerified, client.id, userId, deviceId);
2020-02-04 13:41:13 +00:00
}
2020-05-25 13:30:53 +00:00
@override
2020-05-29 07:06:36 +00:00
Future<void> setBlocked(bool newBlocked) {
2020-02-04 13:41:13 +00:00
blocked = newBlocked;
2020-06-05 20:03:28 +00:00
return client?.database
2020-05-22 10:12:18 +00:00
?.setBlockedUserDeviceKey(newBlocked, client.id, userId, deviceId);
2020-02-04 13:41:13 +00:00
}
2020-06-06 12:28:18 +00:00
DeviceKeys.fromMatrixDeviceKeys(MatrixDeviceKeys k, Client cl)
: super.fromJson(Map<String, dynamic>.from(k.toJson()), cl) {
final json = toJson();
2020-06-05 20:03:28 +00:00
identifier = k.deviceId;
2020-06-06 12:28:18 +00:00
algorithms = json['algorithms'].cast<String>();
2020-06-05 20:03:28 +00:00
}
2020-06-06 12:28:18 +00:00
DeviceKeys.fromDb(DbUserDeviceKeysKey dbEntry, Client cl)
: super.fromJson(Event.getMapFromPayload(dbEntry.content), cl) {
final json = toJson();
identifier = dbEntry.deviceId;
2020-06-06 12:28:18 +00:00
algorithms = json['algorithms'].cast<String>();
_verified = dbEntry.verified;
2020-05-15 18:40:17 +00:00
blocked = dbEntry.blocked;
}
2020-02-04 13:41:13 +00:00
2020-06-06 12:28:18 +00:00
DeviceKeys.fromJson(Map<String, dynamic> json, Client cl)
: super.fromJson(Map<String, dynamic>.from(json), cl) {
final json = toJson();
identifier = json['device_id'];
2020-02-04 13:41:13 +00:00
algorithms = json['algorithms'].cast<String>();
}
2020-05-22 11:22:28 +00:00
KeyVerification startVerification() {
2020-06-06 11:47:37 +00:00
final request = KeyVerification(
encryption: client.encryption, userId: userId, deviceId: deviceId);
2020-05-17 13:25:42 +00:00
request.start();
client.encryption.keyVerificationManager.addRequest(request);
2020-05-17 13:25:42 +00:00
return request;
}
2020-02-04 13:41:13 +00:00
}