Skip to content

Commit

Permalink
Merge pull request #29 from jezell/main
Browse files Browse the repository at this point in the history
update to package:web
  • Loading branch information
cloudwebrtc authored Apr 8, 2024
2 parents d7945ee + 6990ea8 commit 6b2411b
Show file tree
Hide file tree
Showing 24 changed files with 885 additions and 771 deletions.
20 changes: 10 additions & 10 deletions lib/src/e2ee.worker/crypto.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:async';
import 'dart:html' as html;
import 'dart:js_util' as jsutil;
import 'dart:typed_data';

import 'package:js/js.dart';
import 'package:web/web.dart' as web;

@JS('Promise')
class Promise<T> {
Expand All @@ -18,14 +18,14 @@ class Algorithm {
@JS('crypto.subtle.encrypt')
external Promise<ByteBuffer> encrypt(
dynamic algorithm,
html.CryptoKey key,
web.CryptoKey key,
ByteBuffer data,
);

@JS('crypto.subtle.decrypt')
external Promise<ByteBuffer> decrypt(
dynamic algorithm,
html.CryptoKey key,
web.CryptoKey key,
ByteBuffer data,
);

Expand All @@ -52,7 +52,7 @@ ByteBuffer jsArrayBufferFrom(List<int> data) {
}

@JS('crypto.subtle.importKey')
external Promise<html.CryptoKey> importKey(
external Promise<web.CryptoKey> importKey(
String format,
ByteBuffer keyData,
dynamic algorithm,
Expand All @@ -63,28 +63,28 @@ external Promise<html.CryptoKey> importKey(
@JS('crypto.subtle.exportKey')
external Promise<ByteBuffer> exportKey(
String format,
html.CryptoKey key,
web.CryptoKey key,
);

@JS('crypto.subtle.deriveKey')
external Promise<html.CryptoKey> deriveKey(
external Promise<web.CryptoKey> deriveKey(
dynamic algorithm,
html.CryptoKey baseKey,
web.CryptoKey baseKey,
dynamic derivedKeyAlgorithm,
bool extractable,
List<String> keyUsages);

@JS('crypto.subtle.deriveBits')
external Promise<ByteBuffer> deriveBits(
dynamic algorithm,
html.CryptoKey baseKey,
web.CryptoKey baseKey,
int length,
);

Future<html.CryptoKey> impportKeyFromRawData(List<int> secretKeyData,
Future<web.CryptoKey> impportKeyFromRawData(List<int> secretKeyData,
{required String webCryptoAlgorithm,
required List<String> keyUsages}) async {
return jsutil.promiseToFuture<html.CryptoKey>(importKey(
return jsutil.promiseToFuture<web.CryptoKey>(importKey(
'raw',
jsArrayBufferFrom(secretKeyData),
jsutil.jsify({'name': webCryptoAlgorithm}),
Expand Down
10 changes: 6 additions & 4 deletions lib/src/e2ee.worker/e2ee.cryptor.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'dart:async';
import 'dart:html';
import 'dart:js';
import 'dart:js_interop';
import 'dart:js_util' as jsutil;
import 'dart:math';
import 'dart:typed_data';

import 'package:dart_webrtc/src/rtc_transform_stream.dart';
import 'package:web/web.dart' as web;

import 'crypto.dart' as crypto;
import 'e2ee.keyhandler.dart';
import 'e2ee.logger.dart';
Expand Down Expand Up @@ -136,7 +138,7 @@ class FrameCryptor {
bool _enabled = false;
CryptorError lastError = CryptorError.kNew;
int currentKeyIndex = 0;
final DedicatedWorkerGlobalScope worker;
final web.DedicatedWorkerGlobalScope worker;
SifGuard sifGuard = SifGuard();

void setParticipant(String identity, ParticipantKeyHandler keys) {
Expand Down Expand Up @@ -219,7 +221,7 @@ class FrameCryptor {
}

void postMessage(Object message) {
worker.postMessage(message);
worker.postMessage(message.jsify());
}

Future<void> setupTransform({
Expand Down Expand Up @@ -480,7 +482,7 @@ class FrameCryptor {
));

if (currentkeySet != initialKeySet) {
logger.warning(
logger.fine(
'ratchetKey: decryption ok, reset state to kKeyRatcheted');
await keyHandler.setKeySetFromMaterial(
currentkeySet, initialKeyIndex);
Expand Down
21 changes: 11 additions & 10 deletions lib/src/e2ee.worker/e2ee.keyhandler.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:async';
import 'dart:html';
import 'dart:js_util' as jsutil;
import 'dart:typed_data';

import 'package:web/web.dart' as web;

import 'crypto.dart' as crypto;
import 'e2ee.logger.dart';
import 'e2ee.utils.dart';
Expand All @@ -29,7 +30,7 @@ class KeyOptions {

class KeyProvider {
KeyProvider(this.worker, this.id, this.keyProviderOptions);
final DedicatedWorkerGlobalScope worker;
final web.DedicatedWorkerGlobalScope worker;
final String id;
final KeyOptions keyProviderOptions;
var participantKeys = <String, ParticipantKeyHandler>{};
Expand Down Expand Up @@ -80,8 +81,8 @@ const KEYRING_SIZE = 16;

class KeySet {
KeySet(this.material, this.encryptionKey);
CryptoKey material;
CryptoKey encryptionKey;
web.CryptoKey material;
web.CryptoKey encryptionKey;
}

class ParticipantKeyHandler {
Expand All @@ -100,7 +101,7 @@ class ParticipantKeyHandler {

final KeyOptions keyOptions;

final DedicatedWorkerGlobalScope worker;
final web.DedicatedWorkerGlobalScope worker;

final String participantIdentity;

Expand Down Expand Up @@ -157,8 +158,8 @@ class ParticipantKeyHandler {
return newKey;
}

Future<CryptoKey> ratchetMaterial(
CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
Future<web.CryptoKey> ratchetMaterial(
web.CryptoKey currentMaterial, ByteBuffer newKeyBuffer) async {
var newMaterial = await jsutil.promiseToFuture(crypto.importKey(
'raw',
newKeyBuffer,
Expand Down Expand Up @@ -194,14 +195,14 @@ class ParticipantKeyHandler {

/// Derives a set of keys from the master key.
/// See https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.1
Future<KeySet> deriveKeys(CryptoKey material, Uint8List salt) async {
Future<KeySet> deriveKeys(web.CryptoKey material, Uint8List salt) async {
var algorithmOptions =
getAlgoOptions((material.algorithm as crypto.Algorithm).name, salt);

// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#HKDF
// https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
var encryptionKey =
await jsutil.promiseToFuture<CryptoKey>(crypto.deriveKey(
await jsutil.promiseToFuture<web.CryptoKey>(crypto.deriveKey(
jsutil.jsify(algorithmOptions),
material,
jsutil.jsify({'name': 'AES-GCM', 'length': 128}),
Expand All @@ -215,7 +216,7 @@ class ParticipantKeyHandler {
/// Ratchets a key. See
/// https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
Future<Uint8List> ratchet(CryptoKey material, Uint8List salt) async {
Future<Uint8List> ratchet(web.CryptoKey material, Uint8List salt) async {
var algorithmOptions = getAlgoOptions('PBKDF2', salt);

// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits
Expand Down
12 changes: 7 additions & 5 deletions lib/src/e2ee.worker/e2ee.utils.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:html';
import 'dart:js' as js;
import 'dart:typed_data';

import 'package:js/js_util.dart';
import 'package:web/web.dart' as web;

import 'crypto.dart' as crypto;

bool isE2EESupported() {
Expand All @@ -17,10 +19,10 @@ bool isInsertableStreamSupported() {
js.context['RTCRtpSender']['prototype']['createEncodedStreams'] != null;
}

Future<CryptoKey> importKey(
Future<web.CryptoKey> importKey(
Uint8List keyBytes, String algorithm, String usage) {
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
return promiseToFuture<CryptoKey>(crypto.importKey(
return promiseToFuture<web.CryptoKey>(crypto.importKey(
'raw',
crypto.jsArrayBufferFrom(keyBytes),
js.JsObject.jsify({'name': algorithm}),
Expand All @@ -29,10 +31,10 @@ Future<CryptoKey> importKey(
));
}

Future<CryptoKey> createKeyMaterialFromString(
Future<web.CryptoKey> createKeyMaterialFromString(
Uint8List keyBytes, String algorithm, String usage) {
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
return promiseToFuture<CryptoKey>(crypto.importKey(
return promiseToFuture<web.CryptoKey>(crypto.importKey(
'raw',
crypto.jsArrayBufferFrom(keyBytes),
js.JsObject.jsify({'name': 'PBKDF2'}),
Expand Down
Loading

0 comments on commit 6b2411b

Please sign in to comment.