|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:html'; |
| 3 | +import 'dart:js_util' as jsutil; |
| 4 | +import 'dart:typed_data'; |
| 5 | + |
| 6 | +import 'crypto.dart' as crypto; |
| 7 | +import 'e2ee.logger.dart'; |
| 8 | +import 'e2ee.utils.dart'; |
| 9 | + |
| 10 | +class KeyOptions { |
| 11 | + KeyOptions({ |
| 12 | + required this.sharedKey, |
| 13 | + required this.ratchetSalt, |
| 14 | + required this.ratchetWindowSize, |
| 15 | + this.uncryptedMagicBytes, |
| 16 | + this.failureTolerance = -1, |
| 17 | + }); |
| 18 | + bool sharedKey; |
| 19 | + Uint8List ratchetSalt; |
| 20 | + int ratchetWindowSize = 0; |
| 21 | + int failureTolerance; |
| 22 | + Uint8List? uncryptedMagicBytes; |
| 23 | + |
| 24 | + @override |
| 25 | + String toString() { |
| 26 | + return 'KeyOptions{sharedKey: $sharedKey, ratchetWindowSize: $ratchetWindowSize, failureTolerance: $failureTolerance, uncryptedMagicBytes: $uncryptedMagicBytes, ratchetSalt: $ratchetSalt}'; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +const KEYRING_SIZE = 16; |
| 31 | + |
| 32 | +class KeySet { |
| 33 | + KeySet(this.material, this.encryptionKey); |
| 34 | + CryptoKey material; |
| 35 | + CryptoKey encryptionKey; |
| 36 | +} |
| 37 | + |
| 38 | +class ParticipantKeyHandler { |
| 39 | + ParticipantKeyHandler({ |
| 40 | + required this.worker, |
| 41 | + required this.keyOptions, |
| 42 | + required this.participantIdentity, |
| 43 | + }); |
| 44 | + int currentKeyIndex = 0; |
| 45 | + |
| 46 | + List<KeySet?> cryptoKeyRing = List.filled(KEYRING_SIZE, null); |
| 47 | + |
| 48 | + bool _hasValidKey = false; |
| 49 | + |
| 50 | + bool get hasValidKey => _hasValidKey; |
| 51 | + |
| 52 | + final KeyOptions keyOptions; |
| 53 | + |
| 54 | + final DedicatedWorkerGlobalScope worker; |
| 55 | + |
| 56 | + final String participantIdentity; |
| 57 | + |
| 58 | + int _decryptionFailureCount = 0; |
| 59 | + |
| 60 | + void decryptionFailure() { |
| 61 | + if (keyOptions.failureTolerance < 0) { |
| 62 | + return; |
| 63 | + } |
| 64 | + _decryptionFailureCount += 1; |
| 65 | + |
| 66 | + if (_decryptionFailureCount > keyOptions.failureTolerance) { |
| 67 | + logger.warning('key for $participantIdentity is being marked as invalid'); |
| 68 | + _hasValidKey = false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + void decryptionSuccess() { |
| 73 | + resetKeyStatus(); |
| 74 | + } |
| 75 | + |
| 76 | + /// Call this after user initiated ratchet or a new key has been set in order |
| 77 | + /// to make sure to mark potentially invalid keys as valid again |
| 78 | + void resetKeyStatus() { |
| 79 | + _decryptionFailureCount = 0; |
| 80 | + _hasValidKey = true; |
| 81 | + } |
| 82 | + |
| 83 | + Future<Uint8List?> exportKey(int? keyIndex) async { |
| 84 | + var currentMaterial = getKeySet(keyIndex)?.material; |
| 85 | + if (currentMaterial == null) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + try { |
| 89 | + var key = await jsutil.promiseToFuture<ByteBuffer>( |
| 90 | + crypto.exportKey('raw', currentMaterial)); |
| 91 | + return key.asUint8List(); |
| 92 | + } catch (e) { |
| 93 | + logger.warning('exportKey: $e'); |
| 94 | + return null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + Future<Uint8List?> ratchetKey(int? keyIndex) async { |
| 99 | + var currentMaterial = getKeySet(keyIndex)?.material; |
| 100 | + if (currentMaterial == null) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + var newKey = await ratchet(currentMaterial, keyOptions.ratchetSalt); |
| 104 | + var newMaterial = await ratchetMaterial( |
| 105 | + currentMaterial, crypto.jsArrayBufferFrom(newKey)); |
| 106 | + var newKeySet = await deriveKeys(newMaterial, keyOptions.ratchetSalt); |
| 107 | + await setKeySetFromMaterial(newKeySet, keyIndex ?? currentKeyIndex); |
| 108 | + return newKey; |
| 109 | + } |
| 110 | + |
| 111 | + Future<CryptoKey> ratchetMaterial( |
| 112 | + CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async { |
| 113 | + var newMaterial = await jsutil.promiseToFuture(crypto.importKey( |
| 114 | + 'raw', |
| 115 | + newKeyBuffer, |
| 116 | + (currentMaterial.algorithm as crypto.Algorithm).name, |
| 117 | + false, |
| 118 | + ['deriveBits', 'deriveKey'], |
| 119 | + )); |
| 120 | + return newMaterial; |
| 121 | + } |
| 122 | + |
| 123 | + KeySet? getKeySet(int? keyIndex) { |
| 124 | + return cryptoKeyRing[keyIndex ?? currentKeyIndex]; |
| 125 | + } |
| 126 | + |
| 127 | + Future<void> setKey(Uint8List key, {int keyIndex = 0}) async { |
| 128 | + var keyMaterial = await crypto.impportKeyFromRawData(key, |
| 129 | + webCryptoAlgorithm: 'PBKDF2', keyUsages: ['deriveBits', 'deriveKey']); |
| 130 | + var keySet = await deriveKeys( |
| 131 | + keyMaterial, |
| 132 | + keyOptions.ratchetSalt, |
| 133 | + ); |
| 134 | + await setKeySetFromMaterial(keySet, keyIndex); |
| 135 | + resetKeyStatus(); |
| 136 | + } |
| 137 | + |
| 138 | + Future<void> setKeySetFromMaterial(KeySet keySet, int keyIndex) async { |
| 139 | + logger.config('setKeySetFromMaterial: set new key, index: $keyIndex'); |
| 140 | + if (keyIndex >= 0) { |
| 141 | + currentKeyIndex = keyIndex % cryptoKeyRing.length; |
| 142 | + } |
| 143 | + cryptoKeyRing[currentKeyIndex] = keySet; |
| 144 | + } |
| 145 | + |
| 146 | + /// Derives a set of keys from the master key. |
| 147 | + /// See https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.1 |
| 148 | + Future<KeySet> deriveKeys(CryptoKey material, Uint8List salt) async { |
| 149 | + var algorithmOptions = |
| 150 | + getAlgoOptions((material.algorithm as crypto.Algorithm).name, salt); |
| 151 | + |
| 152 | + // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#HKDF |
| 153 | + // https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams |
| 154 | + var encryptionKey = |
| 155 | + await jsutil.promiseToFuture<CryptoKey>(crypto.deriveKey( |
| 156 | + jsutil.jsify(algorithmOptions), |
| 157 | + material, |
| 158 | + jsutil.jsify({'name': 'AES-GCM', 'length': 128}), |
| 159 | + false, |
| 160 | + ['encrypt', 'decrypt'], |
| 161 | + )); |
| 162 | + |
| 163 | + return KeySet(material, encryptionKey); |
| 164 | + } |
| 165 | + |
| 166 | + /// Ratchets a key. See |
| 167 | + /// https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1 |
| 168 | +
|
| 169 | + Future<Uint8List> ratchet(CryptoKey material, Uint8List salt) async { |
| 170 | + var algorithmOptions = getAlgoOptions('PBKDF2', salt); |
| 171 | + |
| 172 | + // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits |
| 173 | + var newKey = await jsutil.promiseToFuture<ByteBuffer>( |
| 174 | + crypto.deriveBits(jsutil.jsify(algorithmOptions), material, 256)); |
| 175 | + return newKey.asUint8List(); |
| 176 | + } |
| 177 | +} |
0 commit comments