From 3976b0f1e22461e08fcd339f41a3488b48af1756 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 30 Aug 2022 11:55:17 +0200 Subject: [PATCH] refactor: Avoid using private types in public api --- lib/encryption/key_manager.dart | 16 ++++++++-------- lib/encryption/ssss.dart | 22 ++++++++++++---------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/encryption/key_manager.dart b/lib/encryption/key_manager.dart index 68dbeb99..786a76b5 100644 --- a/lib/encryption/key_manager.dart +++ b/lib/encryption/key_manager.dart @@ -771,7 +771,7 @@ class KeyManager { } final args = GenerateUploadKeysArgs( pubkey: backupPubKey, - dbSessions: <_DbInboundGroupSessionBundle>[], + dbSessions: [], userId: userID, ); // we need to calculate verified beforehand, as else we pass a closure to an isolate @@ -781,7 +781,7 @@ class KeyManager { for (final dbSession in dbSessions) { final device = client.getUserDeviceKeysByCurve25519Key(dbSession.senderKey); - args.dbSessions.add(_DbInboundGroupSessionBundle( + args.dbSessions.add(DbInboundGroupSessionBundle( dbSession: dbSession, verified: device?.verified ?? false, )); @@ -1113,12 +1113,12 @@ RoomKeys generateUploadKeysImplementation(GenerateUploadKeysArgs args) { } } -class _DbInboundGroupSessionBundle { - _DbInboundGroupSessionBundle( +class DbInboundGroupSessionBundle { + DbInboundGroupSessionBundle( {required this.dbSession, required this.verified}); - factory _DbInboundGroupSessionBundle.fromJson(Map json) => - _DbInboundGroupSessionBundle( + factory DbInboundGroupSessionBundle.fromJson(Map json) => + DbInboundGroupSessionBundle( dbSession: StoredInboundGroupSession.fromJson(Map.from(json['dbSession'])), verified: json['verified'], @@ -1140,7 +1140,7 @@ class GenerateUploadKeysArgs { GenerateUploadKeysArgs( pubkey: json['pubkey'], dbSessions: (json['dbSessions'] as Iterable) - .map((e) => _DbInboundGroupSessionBundle.fromJson(e)) + .map((e) => DbInboundGroupSessionBundle.fromJson(e)) .toList(), userId: json['userId'], ); @@ -1152,6 +1152,6 @@ class GenerateUploadKeysArgs { }; String pubkey; - List<_DbInboundGroupSessionBundle> dbSessions; + List dbSessions; String userId; } diff --git a/lib/encryption/ssss.dart b/lib/encryption/ssss.dart index b8828d2c..1a4b5732 100644 --- a/lib/encryption/ssss.dart +++ b/lib/encryption/ssss.dart @@ -68,7 +68,7 @@ class SSSS { _cache.clear(); } - static _DerivedKeys deriveKeys(Uint8List key, String name) { + static DerivedKeys deriveKeys(Uint8List key, String name) { final zerosalt = Uint8List(8); final prk = Hmac(sha256, zerosalt).convert(key); final b = Uint8List(1); @@ -77,12 +77,13 @@ class SSSS { b[0] = 2; final hmacKey = Hmac(sha256, prk.bytes).convert(aesKey.bytes + utf8.encode(name) + b); - return _DerivedKeys( + return DerivedKeys( aesKey: Uint8List.fromList(aesKey.bytes), hmacKey: Uint8List.fromList(hmacKey.bytes)); } - static Future<_Encrypted> encryptAes(String data, Uint8List key, String name, + static Future encryptAes( + String data, Uint8List key, String name, [String? ivStr]) async { Uint8List iv; if (ivStr != null) { @@ -100,14 +101,14 @@ class SSSS { final hmac = Hmac(sha256, keys.hmacKey).convert(ciphertext); - return _Encrypted( + return EncryptedContent( iv: base64.encode(iv), ciphertext: base64.encode(ciphertext), mac: base64.encode(hmac.bytes)); } static Future decryptAes( - _Encrypted data, Uint8List key, String name) async { + EncryptedContent data, Uint8List key, String name) async { final keys = deriveKeys(key, name); final cipher = base64decodeUnpadded(data.ciphertext); final hmac = base64 @@ -318,7 +319,7 @@ class SSSS { throw Exception('Wrong / unknown key'); } final enc = secretInfo.content['encrypted'][keyId]; - final encryptInfo = _Encrypted( + final encryptInfo = EncryptedContent( iv: enc['iv'], ciphertext: enc['ciphertext'], mac: enc['mac']); final decrypted = await decryptAes(encryptInfo, key, type); final db = client.database; @@ -604,19 +605,20 @@ class _ShareRequest { : start = DateTime.now(); } -class _Encrypted { +class EncryptedContent { final String iv; final String ciphertext; final String mac; - _Encrypted({required this.iv, required this.ciphertext, required this.mac}); + EncryptedContent( + {required this.iv, required this.ciphertext, required this.mac}); } -class _DerivedKeys { +class DerivedKeys { final Uint8List aesKey; final Uint8List hmacKey; - _DerivedKeys({required this.aesKey, required this.hmacKey}); + DerivedKeys({required this.aesKey, required this.hmacKey}); } class OpenSSSS {