format and some analyze

This commit is contained in:
Sorunome 2020-06-06 13:47:37 +02:00
parent 060a772bfa
commit 4154c7d0eb
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
9 changed files with 87 additions and 65 deletions

View file

@ -152,7 +152,8 @@ class CrossSigning {
if (key is CrossSigningKey) {
if (key.usage.contains('master')) {
// okay, we'll sign our own master key
final signature = encryption.olmManager.signString(key.signingContent);
final signature =
encryption.olmManager.signString(key.signingContent);
addSignature(
key,
client
@ -172,8 +173,8 @@ class CrossSigning {
}
} else if (key is CrossSigningKey && key.usage.contains('master')) {
// we are signing someone elses master key
userSigningKey ??=
base64.decode(await encryption.ssss.getCached(USER_SIGNING_KEY) ?? '');
userSigningKey ??= base64
.decode(await encryption.ssss.getCached(USER_SIGNING_KEY) ?? '');
if (userSigningKey.isNotEmpty) {
final signature = _sign(key.signingContent, userSigningKey);
addSignature(key, client.userDeviceKeys[client.userID].userSigningKey,

View file

@ -93,7 +93,11 @@ class Encryption {
if (update.type == 'ephemeral') {
return;
}
if (update.eventType.startsWith('m.key.verification.') || (update.eventType == 'm.room.message' && (update.content['content']['msgtype'] is String) && update.content['content']['msgtype'].startsWith('m.key.verification.'))) {
if (update.eventType.startsWith('m.key.verification.') ||
(update.eventType == 'm.room.message' &&
(update.content['content']['msgtype'] is String) &&
update.content['content']['msgtype']
.startsWith('m.key.verification.'))) {
// "just" key verification, no need to do this in sync
unawaited(keyVerificationManager.handleEventUpdate(update));
}

View file

@ -321,7 +321,8 @@ class KeyManager {
if (!(payload['rooms'] is Map)) {
return;
}
final privateKey = base64.decode(await encryption.ssss.getCached(MEGOLM_KEY));
final privateKey =
base64.decode(await encryption.ssss.getCached(MEGOLM_KEY));
final decryption = olm.PkDecryption();
final info = await getRoomKeysInfo();
String backupPubKey;
@ -373,7 +374,9 @@ class KeyManager {
if (decrypted != null) {
decrypted['session_id'] = sessionId;
decrypted['room_id'] = roomId;
setInboundGroupSession(roomId, sessionId, decrypted['sender_key'], decrypted, forwarded: true);
setInboundGroupSession(
roomId, sessionId, decrypted['sender_key'], decrypted,
forwarded: true);
}
}
}
@ -403,7 +406,8 @@ class KeyManager {
/// Request a certain key from another device
Future<void> request(Room room, String sessionId, String senderKey) async {
// let's first check our online key backup store thingy...
var hadPreviously = getInboundGroupSession(room.id, sessionId, senderKey) != null;
var hadPreviously =
getInboundGroupSession(room.id, sessionId, senderKey) != null;
try {
await loadSingleKey(room.id, sessionId);
} catch (err, stacktrace) {
@ -411,7 +415,8 @@ class KeyManager {
print(err.toString());
print(stacktrace);
}
if (!hadPreviously && getInboundGroupSession(room.id, sessionId, senderKey) != null) {
if (!hadPreviously &&
getInboundGroupSession(room.id, sessionId, senderKey) != null) {
return; // we managed to load the session from online backup, no need to care about it now
}
// while we just send the to-device event to '*', we still need to save the

View file

@ -29,7 +29,7 @@ class KeyVerificationManager {
final Map<String, KeyVerification> _requests = {};
Future<void> cleanup() async {
Set<String> entriesToDispose = <String>{};
final Set entriesToDispose = <String>{};
for (final entry in _requests.entries) {
var dispose = entry.value.canceled ||
entry.value.state == KeyVerificationState.done ||
@ -55,7 +55,8 @@ class KeyVerificationManager {
}
Future<void> handleToDeviceEvent(ToDeviceEvent event) async {
if (!event.type.startsWith('m.key.verification') || client.verificationMethods.isEmpty) {
if (!event.type.startsWith('m.key.verification') ||
client.verificationMethods.isEmpty) {
return;
}
// we have key verification going on!
@ -84,7 +85,9 @@ class KeyVerificationManager {
final type = event['type'].startsWith('m.key.verification.')
? event['type']
: event['content']['msgtype'];
if (type == null || !type.startsWith('m.key.verification.') || client.verificationMethods.isEmpty) {
if (type == null ||
!type.startsWith('m.key.verification.') ||
client.verificationMethods.isEmpty) {
return;
}
if (type == 'm.key.verification.request') {
@ -97,7 +100,7 @@ class KeyVerificationManager {
if (_requests.containsKey(transactionId)) {
final req = _requests[transactionId];
if (event['sender'] != client.userID) {
req.handlePayload(type, event['content'], event['event_id']);
await req.handlePayload(type, event['content'], event['event_id']);
} else if (req.userId == client.userID && req.deviceId == null) {
// okay, maybe another of our devices answered
await req.handlePayload(type, event['content'], event['event_id']);
@ -108,12 +111,12 @@ class KeyVerificationManager {
}
}
} else if (event['sender'] != client.userID) {
final room =
client.getRoomById(update.roomID) ?? Room(id: update.roomID, client: client);
final newKeyRequest =
KeyVerification(encryption: encryption, userId: event['sender'], room: room);
await newKeyRequest
.handlePayload(type, event['content'], event['event_id']);
final room = client.getRoomById(update.roomID) ??
Room(id: update.roomID, client: client);
final newKeyRequest = KeyVerification(
encryption: encryption, userId: event['sender'], room: room);
await newKeyRequest.handlePayload(
type, event['content'], event['event_id']);
if (newKeyRequest.state != KeyVerificationState.askAccept) {
// something went wrong, let's just dispose the request
newKeyRequest.dispose();

View file

@ -177,7 +177,10 @@ class SSSS {
}
// check if it is still valid
final keys = keyIdsFromType(type);
if (keys.contains(ret.keyId) && client.accountData[type].content['encrypted'][ret.keyId]['ciphertext'] == ret.ciphertext) {
if (keys.contains(ret.keyId) &&
client.accountData[type].content['encrypted'][ret.keyId]
['ciphertext'] ==
ret.ciphertext) {
return ret.content;
}
return null;
@ -200,7 +203,8 @@ class SSSS {
final decrypted = decryptAes(encryptInfo, key, type);
if (CACHE_TYPES.contains(type) && client.database != null) {
// cache the thing
await client.database.storeSSSSCache(client.id, type, keyId, enc['ciphertext'], decrypted);
await client.database
.storeSSSSCache(client.id, type, keyId, enc['ciphertext'], decrypted);
}
return decrypted;
}
@ -224,7 +228,8 @@ class SSSS {
);
if (CACHE_TYPES.contains(type) && client.database != null) {
// cache the thing
await client.database.storeSSSSCache(client.id, type, keyId, encrypted.ciphertext, secret);
await client.database
.storeSSSSCache(client.id, type, keyId, encrypted.ciphertext, secret);
}
}
@ -352,9 +357,10 @@ class SSSS {
if (client.database != null) {
final keyId = keyIdFromType(request.type);
if (keyId != null) {
final ciphertext = client.accountData[request.type].content['encrypted'][keyId]['ciphertext'];
await client.database
.storeSSSSCache(client.id, request.type, keyId, ciphertext, secret);
final ciphertext = client.accountData[request.type]
.content['encrypted'][keyId]['ciphertext'];
await client.database.storeSSSSCache(
client.id, request.type, keyId, ciphertext, secret);
}
}
}

View file

@ -41,8 +41,8 @@ class KeysQueryResponse {
),
)
: null;
masterKeys = json['master_keys'] != null ?
(json['master_keys'] as Map).map(
masterKeys = json['master_keys'] != null
? (json['master_keys'] as Map).map(
(k, v) => MapEntry(
k,
MatrixCrossSigningKey.fromJson(v),
@ -50,8 +50,8 @@ class KeysQueryResponse {
)
: null;
selfSigningKeys = json['self_signing_keys'] != null ?
(json['self_signing_keys'] as Map).map(
selfSigningKeys = json['self_signing_keys'] != null
? (json['self_signing_keys'] as Map).map(
(k, v) => MapEntry(
k,
MatrixCrossSigningKey.fromJson(v),
@ -59,8 +59,8 @@ class KeysQueryResponse {
)
: null;
userSigningKeys = json['user_signing_keys'] != null ?
(json['user_signing_keys'] as Map).map(
userSigningKeys = json['user_signing_keys'] != null
? (json['user_signing_keys'] as Map).map(
(k, v) => MapEntry(
k,
MatrixCrossSigningKey.fromJson(v),

View file

@ -1160,7 +1160,8 @@ class Client {
final deviceId = rawDeviceKeyEntry.key;
// Set the new device key for this device
final entry = DeviceKeys.fromMatrixDeviceKeys(rawDeviceKeyEntry.value, this);
final entry =
DeviceKeys.fromMatrixDeviceKeys(rawDeviceKeyEntry.value, this);
if (entry.isValid) {
// is this a new key or the same one as an old one?
// better store an update - the signatures might have changed!
@ -1231,7 +1232,8 @@ class Client {
if (!userDeviceKeys.containsKey(userId)) {
_userDeviceKeys[userId] = DeviceKeysList(userId);
}
final oldKeys = Map<String, CrossSigningKey>.from(_userDeviceKeys[userId].crossSigningKeys);
final oldKeys = Map<String, CrossSigningKey>.from(
_userDeviceKeys[userId].crossSigningKeys);
_userDeviceKeys[userId].crossSigningKeys = {};
// add the types we aren't handling atm back
for (final oldEntry in oldKeys.entries) {
@ -1240,8 +1242,8 @@ class Client {
oldEntry.value;
}
}
final entry =
CrossSigningKey.fromMatrixCrossSigningKey(crossSigningKeyListEntry.value, this);
final entry = CrossSigningKey.fromMatrixCrossSigningKey(
crossSigningKeyListEntry.value, this);
if (entry.isValid) {
final publicKey = entry.publicKey;
if (!oldKeys.containsKey(publicKey) ||

View file

@ -60,7 +60,8 @@ class DeviceKeysList {
throw 'Unable to start new room';
}
final room = client.getRoomById(roomId) ?? Room(id: roomId, client: client);
final request = KeyVerification(encryption: client.encryption, room: room, userId: userId);
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
@ -224,7 +225,9 @@ abstract class SignedKey {
Future<void> setVerified(bool newVerified, [bool sign = true]) {
_verified = newVerified;
if (sign && client.encryptionEnabled && client.encryption.crossSigning.signable([this])) {
if (sign &&
client.encryptionEnabled &&
client.encryption.crossSigning.signable([this])) {
// sign the key!
client.encryption.crossSigning.sign([this]);
}
@ -266,13 +269,16 @@ class CrossSigningKey extends SignedKey {
newBlocked, client.id, userId, publicKey);
}
CrossSigningKey.fromMatrixCrossSigningKey(MatrixCrossSigningKey k, Client cl) {
CrossSigningKey.fromMatrixCrossSigningKey(
MatrixCrossSigningKey k, Client cl) {
client = cl;
content = Map<String, dynamic>.from(k.toJson());
userId = k.userId;
identifier = k.publicKey;
usage = content['usage'].cast<String>();
keys = content['keys'] != null ? Map<String, String>.from(content['keys']) : null;
keys = content['keys'] != null
? Map<String, String>.from(content['keys'])
: null;
signatures = content['signatures'] != null
? Map<String, dynamic>.from(content['signatures'])
: null;
@ -397,8 +403,8 @@ class DeviceKeys extends SignedKey {
}
KeyVerification startVerification() {
final request =
KeyVerification(encryption: client.encryption, userId: userId, deviceId: deviceId);
final request = KeyVerification(
encryption: client.encryption, userId: userId, deviceId: deviceId);
request.start();
client.encryption.keyVerificationManager.addRequest(request);

View file

@ -42,15 +42,10 @@ void main() {
},
'unsigned': {'device_display_name': "Alice's mobile phone"},
};
var rawListJson = <String, dynamic>{
'user_id': '@alice:example.com',
'outdated': true,
'device_keys': {'JLAFKJWSCS': rawJson},
};
final key = DeviceKeys.fromJson(rawJson, null);
key.setVerified(false, false);
key.setBlocked(true);
await key.setVerified(false, false);
await key.setBlocked(true);
expect(json.encode(key.toJson()), json.encode(rawJson));
expect(key.directVerified, false);
expect(key.blocked, true);