Skip to content

Commit

Permalink
fix: workaround token rotation during cross-signing setup
Browse files Browse the repository at this point in the history
Signed-off-by: The one with the braid <[email protected]>
  • Loading branch information
TheOneWithTheBraid committed Feb 11, 2025
1 parent 83f4ec1 commit ff732a4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/encryption/cross_signing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class CrossSigning {
}
}

await client.ensureNotSoftLoggedOut();
await client.uploadCrossSigningSignatures(payload);
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/encryption/key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class KeyManager {
}
return sess; // nothing to do
}
await client.ensureNotSoftLoggedOut();
final session =
await client.database?.getInboundGroupSession(roomId, sessionId);
if (session == null) return null;
Expand Down Expand Up @@ -460,6 +461,7 @@ class KeyManager {
room.id,
sess.outboundGroupSession!.session_id(),
);
await client.ensureNotSoftLoggedOut();
// send out the key
await client.sendToDeviceEncryptedChunked(
devicesToReceive,
Expand Down Expand Up @@ -591,6 +593,7 @@ class KeyManager {
key: userID,
);
try {
await client.ensureNotSoftLoggedOut();
await client.sendToDeviceEncryptedChunked(
deviceKeys,
EventTypes.RoomKey,
Expand Down Expand Up @@ -659,6 +662,7 @@ class KeyManager {
.isBefore(_roomKeysVersionCacheDate!)) {
return _roomKeysVersionCache!;
}
await client.ensureNotSoftLoggedOut();
_roomKeysVersionCache = await client.getRoomKeysVersionCurrent();
_roomKeysVersionCacheDate = DateTime.now();
return _roomKeysVersionCache!;
Expand Down Expand Up @@ -725,6 +729,7 @@ class KeyManager {
/// while for older and big accounts.
Future<void> loadAllKeys() async {
final info = await getRoomKeysBackupInfo();
await client.ensureNotSoftLoggedOut();
final ret = await client.getRoomKeys(info.version);
await loadFromResponse(ret);
}
Expand All @@ -733,6 +738,7 @@ class KeyManager {
/// while for older and big rooms.
Future<void> loadAllKeysFromRoom(String roomId) async {
final info = await getRoomKeysBackupInfo();
await client.ensureNotSoftLoggedOut();
final ret = await client.getRoomKeysByRoomId(roomId, info.version);
final keys = RoomKeys.fromJson({
'rooms': {
Expand All @@ -748,6 +754,7 @@ class KeyManager {
/// and stores it.
Future<void> loadSingleKey(String roomId, String sessionId) async {
final info = await getRoomKeysBackupInfo();
await client.ensureNotSoftLoggedOut();
final ret =
await client.getRoomKeyBySessionId(roomId, sessionId, info.version);
final keys = RoomKeys.fromJson({
Expand Down Expand Up @@ -809,6 +816,7 @@ class KeyManager {
sessionId: sessionId,
);
final userList = await room.requestParticipants();
await client.ensureNotSoftLoggedOut();
await client.sendToDevicesOfUserIds(
userList.map<String>((u) => u.id).toSet(),
EventTypes.RoomKeyRequest,
Expand Down Expand Up @@ -916,6 +924,7 @@ class KeyManager {
await client.nativeImplementations.generateUploadKeys(args);
Logs().i('[Key Manager] Uploading ${dbSessions.length} room keys...');
// upload the payload...
await client.ensureNotSoftLoggedOut();
await client.putRoomKeys(info.version, roomKeys);
// and now finally mark all the keys as uploaded
// no need to optimze this, as we only run it so seldomly and almost never with many keys at once
Expand Down Expand Up @@ -1119,6 +1128,7 @@ class KeyManager {
final userData = data[device.userId] ??= {};
userData[device.deviceId!] = sendToDeviceMessage;
}
await client.ensureNotSoftLoggedOut();
await client.sendToDevice(
EventTypes.RoomKeyRequest,
client.generateUniqueTransactionId(),
Expand Down
9 changes: 9 additions & 0 deletions lib/encryption/ssss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class SSSS {
.key;

Future<void> setDefaultKeyId(String keyId) async {
await client.ensureNotSoftLoggedOut();
await client.setAccountData(
client.userID!,
EventTypes.SecretStorageDefaultKey,
Expand Down Expand Up @@ -264,6 +265,9 @@ class SSSS {
.firstWhere((keyId) => getKey(keyId) == null);

final accountDataTypeKeyId = EventTypes.secretStorageKey(keyId);

await client.ensureNotSoftLoggedOut();

// noooow we set the account data

await client.setAccountData(
Expand Down Expand Up @@ -395,6 +399,7 @@ class SSSS {
'ciphertext': encrypted.ciphertext,
'mac': encrypted.mac,
};
await client.ensureNotSoftLoggedOut();
// store the thing in your account data
await client.setAccountData(client.userID!, type, content);
final db = client.database;
Expand Down Expand Up @@ -434,6 +439,8 @@ class SSSS {
if (await getStored(type, keyId, key) != secret) {
throw Exception('Secrets do not match up!');
}

await client.ensureNotSoftLoggedOut();
// store the thing in your account data
await client.setAccountData(client.userID!, type, content);
if (cacheTypes.contains(type)) {
Expand Down Expand Up @@ -502,6 +509,7 @@ class SSSS {
devices: devices,
);
pendingShareRequests[requestId] = request;
await client.ensureNotSoftLoggedOut();
await client.sendToDeviceEncrypted(devices, EventTypes.SecretRequest, {
'action': 'request',
'requesting_device_id': client.deviceID,
Expand Down Expand Up @@ -565,6 +573,7 @@ class SSSS {
}
// okay, all checks out...time to share this secret!
Logs().i('[SSSS] Replying with secret for $type');
await client.ensureNotSoftLoggedOut();
await client.sendToDeviceEncrypted(
[device],
EventTypes.SecretSend,
Expand Down
6 changes: 6 additions & 0 deletions lib/encryption/utils/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ class Bootstrap {
// upload the keys!
state = BootstrapState.loading;
Logs().v('Upload device signing keys.');
await client.ensureNotSoftLoggedOut();
await client.uiaRequestBackground(
(AuthenticationData? auth) => client.uploadCrossSigningKeys(
masterKey: masterKey,
Expand All @@ -494,6 +495,7 @@ class Bootstrap {
}
}
if (newSsssKey != null) {
await client.ensureNotSoftLoggedOut();
final storeFutures = <Future<void>>[];
for (final entry in secretsToStore.entries) {
storeFutures.add(newSsssKey!.store(entry.key, entry.value));
Expand All @@ -510,6 +512,7 @@ class Bootstrap {
'ERROR: New master key does not match up!',
);
}
await client.ensureNotSoftLoggedOut();
Logs().v('Set own master key to verified...');
await client.userDeviceKeys[client.userID]!.masterKey!
.setVerified(true, false);
Expand All @@ -520,6 +523,7 @@ class Bootstrap {
client.userDeviceKeys[client.userID]!.deviceKeys[client.deviceID]!,
);
}
await client.ensureNotSoftLoggedOut();
Logs().v('Sign ourself...');
await encryption.crossSigning.sign(keysToSign);
} catch (e, s) {
Expand Down Expand Up @@ -570,6 +574,7 @@ class Bootstrap {
} finally {
keyObj.free();
}
await client.ensureNotSoftLoggedOut();
Logs().v('Create the new backup version...');
await client.postRoomKeysVersion(
BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2,
Expand All @@ -585,6 +590,7 @@ class Bootstrap {
);
await client.database?.markInboundGroupSessionsAsNeedingUpload();
Logs().v('And uploading keys...');
await client.ensureNotSoftLoggedOut();
await client.encryption?.keyManager.uploadInboundGroupSessions();
} catch (e, s) {
Logs().e('[Bootstrapping] Error setting up online key backup', e, s);
Expand Down
3 changes: 3 additions & 0 deletions lib/encryption/utils/key_verification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ class KeyVerification {
'code': 'm.accepted',
};
makePayload(cancelPayload);
await client.ensureNotSoftLoggedOut();
await client.sendToDeviceEncrypted(
devices,
EventTypes.KeyVerificationCancel,
Expand Down Expand Up @@ -957,6 +958,7 @@ class KeyVerification {
);

if (deviceKeys != null) {
await client.ensureNotSoftLoggedOut();
await client.sendToDeviceEncrypted(
deviceKeys.toList(),
type,
Expand All @@ -970,6 +972,7 @@ class KeyVerification {
}
} else {
if (client.userDeviceKeys[userId]?.deviceKeys[deviceId] != null) {
await client.ensureNotSoftLoggedOut();
await client.sendToDeviceEncrypted(
[client.userDeviceKeys[userId]!.deviceKeys[deviceId]!],
type,
Expand Down
1 change: 1 addition & 0 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3252,6 +3252,7 @@ class Client extends MatrixApi {
}

if (outdatedLists.isNotEmpty) {
await ensureNotSoftLoggedOut();
// Request the missing device key lists from the server.
final response = await queryKeys(outdatedLists, timeout: 10000);
if (!isLogged()) return;
Expand Down

0 comments on commit ff732a4

Please sign in to comment.