Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions lib/src/boringssl/bindings/generated_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,27 @@ import 'dart:ffi' as ffi;
class WebCrypto {
/// Holds the symbol lookup function.
final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
_lookup;
_lookup;

/// The symbols are looked up in [dynamicLibrary].
WebCrypto(ffi.DynamicLibrary dynamicLibrary)
: _lookup = dynamicLibrary.lookup;
: _lookup = dynamicLibrary.lookup;

/// The symbols are looked up with [lookup].
WebCrypto.fromLookup(
ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
lookup)
: _lookup = lookup;
ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName) lookup,
) : _lookup = lookup;

/// Function to lookup BoringSSL symbols based on index in the Sym enum.
/// See src/symbols.yaml for details.
ffi.Pointer<ffi.Void> webcrypto_lookup_symbol(
int index,
) {
return _webcrypto_lookup_symbol(
index,
);
ffi.Pointer<ffi.Void> webcrypto_lookup_symbol(int index) {
return _webcrypto_lookup_symbol(index);
}

late final _webcrypto_lookup_symbolPtr =
_lookup<ffi.NativeFunction<ffi.Pointer<ffi.Void> Function(ffi.Int32)>>(
'webcrypto_lookup_symbol');
'webcrypto_lookup_symbol',
);
late final _webcrypto_lookup_symbol = _webcrypto_lookup_symbolPtr
.asFunction<ffi.Pointer<ffi.Void> Function(int)>();
}
2 changes: 1 addition & 1 deletion lib/src/boringssl/lookup/lookup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final Pointer<T> Function<T extends NativeType>(String symbolName) lookup = () {
}();

final Pointer<T> Function<T extends NativeType>(String symbolName)
_cachedLookup = lookup;
_cachedLookup = lookup;

/// Gives access to BoringSSL symbols.
final BoringSsl ssl = BoringSsl.fromLookup(_cachedLookup);
Expand Down
12 changes: 7 additions & 5 deletions lib/src/boringssl/lookup/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ String get libraryFileName {
///
/// Returns `null` if it could not be found.
Pointer<T> Function<T extends NativeType>(String symbolName)?
lookupLibraryInDotDartTool() {
lookupLibraryInDotDartTool() {
final dotDartTool = _findDotDartTool();
if (dotDartTool == null) {
return null;
Expand Down Expand Up @@ -78,8 +78,9 @@ Uri? _findDotDartTool() {

// Traverse up until we see a `.dart_tool/package_config.json` file.
do {
if (File.fromUri(root.resolve('.dart_tool/package_config.json'))
.existsSync()) {
if (File.fromUri(
root.resolve('.dart_tool/package_config.json'),
).existsSync()) {
return root.resolve('.dart_tool/');
}
} while (root != (root = root.resolve('..')));
Expand All @@ -90,8 +91,9 @@ Uri? _findDotDartTool() {

// Traverse up until we see a `.dart_tool/package_config.json` file.
do {
if (File.fromUri(root.resolve('.dart_tool/package_config.json'))
.existsSync()) {
if (File.fromUri(
root.resolve('.dart_tool/package_config.json'),
).existsSync()) {
return root.resolve('.dart_tool/');
}
} while (root != (root = root.resolve('..')));
Expand Down
193 changes: 58 additions & 135 deletions lib/src/crypto_subtle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ extension type JSSubtleCrypto(JSObject _) implements JSObject {
);

/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
external JSPromise<JSArrayBuffer> digest(
String algorithm,
JSTypedArray data,
);
external JSPromise<JSArrayBuffer> digest(String algorithm, JSTypedArray data);

/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits
external JSPromise<JSArrayBuffer> deriveBits(
Expand Down Expand Up @@ -151,10 +148,7 @@ extension type JSSubtleCrypto(JSObject _) implements JSObject {

/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
@JS('exportKey')
external JSPromise<JSArrayBuffer> exportKey(
String format,
JSCryptoKey key,
);
external JSPromise<JSArrayBuffer> exportKey(String format, JSCryptoKey key);

/// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
@JS('exportKey')
Expand Down Expand Up @@ -283,25 +277,24 @@ class Algorithm {
TypedData? salt,
TypedData? info,
int? iterations,
}) =>
Algorithm(
name: this.name ?? name,
modulusLength: this.modulusLength ?? modulusLength,
publicExponent: this.publicExponent ?? publicExponent,
hash: this.hash ?? hash,
saltLength: this.saltLength ?? saltLength,
label: this.label ?? label,
namedCurve: this.namedCurve ?? namedCurve,
public: this.public ?? public,
counter: this.counter ?? counter,
length: this.length ?? length,
iv: this.iv ?? iv,
additionalData: this.additionalData ?? additionalData,
tagLength: this.tagLength ?? tagLength,
salt: this.salt ?? salt,
info: this.info ?? info,
iterations: this.iterations ?? iterations,
);
}) => Algorithm(
name: this.name ?? name,
modulusLength: this.modulusLength ?? modulusLength,
publicExponent: this.publicExponent ?? publicExponent,
hash: this.hash ?? hash,
saltLength: this.saltLength ?? saltLength,
label: this.label ?? label,
namedCurve: this.namedCurve ?? namedCurve,
public: this.public ?? public,
counter: this.counter ?? counter,
length: this.length ?? length,
iv: this.iv ?? iv,
additionalData: this.additionalData ?? additionalData,
tagLength: this.tagLength ?? tagLength,
salt: this.salt ?? salt,
info: this.info ?? info,
iterations: this.iterations ?? iterations,
);
}

extension type JSJsonWebKey(JSObject _) implements JSObject {
Expand Down Expand Up @@ -390,11 +383,7 @@ TypedData getRandomValues(TypedData array) {
array.setAll(0, dartArray);
}
} else {
throw ArgumentError.value(
array,
'array',
'Unsupported TypedData type',
);
throw ArgumentError.value(array, 'array', 'Unsupported TypedData type');
}

return array;
Expand All @@ -406,11 +395,7 @@ Future<ByteBuffer> decrypt(
Uint8List data,
) async {
final value = await window.crypto.subtle
.decrypt(
algorithm.toJS,
key,
data.toJS,
)
.decrypt(algorithm.toJS, key, data.toJS)
.toDart;

return value.toDart;
Expand All @@ -422,40 +407,20 @@ Future<ByteBuffer> encrypt(
Uint8List data,
) async {
final value = await window.crypto.subtle
.encrypt(
algorithm.toJS,
key,
data.toJS,
)
.encrypt(algorithm.toJS, key, data.toJS)
.toDart;

return value.toDart;
}

Future<ByteBuffer> exportKey(
String format,
JSCryptoKey key,
) async {
final value = await window.crypto.subtle
.exportKey(
format,
key,
)
.toDart;
Future<ByteBuffer> exportKey(String format, JSCryptoKey key) async {
final value = await window.crypto.subtle.exportKey(format, key).toDart;

return value.toDart;
}

Future<JsonWebKey> exportJsonWebKey(
String format,
JSCryptoKey key,
) async {
final value = await window.crypto.subtle
.exportJsonWebKey(
format,
key,
)
.toDart;
Future<JsonWebKey> exportJsonWebKey(String format, JSCryptoKey key) async {
final value = await window.crypto.subtle.exportJsonWebKey(format, key).toDart;

return value.toDart;
}
Expand All @@ -466,11 +431,7 @@ Future<JSCryptoKey> generateKey(
List<String> usages,
) async {
final value = await window.crypto.subtle
.generateCryptoKey(
algorithm.toJS,
extractable,
usages.toJS,
)
.generateCryptoKey(algorithm.toJS, extractable, usages.toJS)
.toDart;

return value;
Expand All @@ -482,26 +443,14 @@ Future<JSCryptoKeyPair> generateKeyPair(
List<String> usages,
) async {
final value = await window.crypto.subtle
.generateCryptoKeyPair(
algorithm.toJS,
extractable,
usages.toJS,
)
.generateCryptoKeyPair(algorithm.toJS, extractable, usages.toJS)
.toDart;

return value;
}

Future<ByteBuffer> digest(
String algorithm,
Uint8List data,
) async {
final value = await window.crypto.subtle
.digest(
algorithm,
data.toJS,
)
.toDart;
Future<ByteBuffer> digest(String algorithm, Uint8List data) async {
final value = await window.crypto.subtle.digest(algorithm, data.toJS).toDart;

return value.toDart;
}
Expand All @@ -514,13 +463,7 @@ Future<JSCryptoKey> importKey(
List<String> usages,
) async {
final value = await window.crypto.subtle
.importKey(
format,
keyData.toJS,
algorithm.toJS,
extractable,
usages.toJS,
)
.importKey(format, keyData.toJS, algorithm.toJS, extractable, usages.toJS)
.toDart;

return value;
Expand Down Expand Up @@ -552,11 +495,7 @@ Future<ByteBuffer> sign(
Uint8List data,
) async {
final value = await window.crypto.subtle
.sign(
algorithm.toJS,
key,
data.toJS,
)
.sign(algorithm.toJS, key, data.toJS)
.toDart;

return value.toDart;
Expand All @@ -569,12 +508,7 @@ Future<bool> verify(
Uint8List data,
) async {
final value = await window.crypto.subtle
.verify(
algorithm.toJS,
key,
signature.toJS,
data.toJS,
)
.verify(algorithm.toJS, key, signature.toJS, data.toJS)
.toDart;

return value.toDart;
Expand All @@ -586,21 +520,16 @@ Future<ByteBuffer> deriveBits(
int length,
) async {
final value = await window.crypto.subtle
.deriveBits(
algorithm.toJS,
key,
length,
)
.deriveBits(algorithm.toJS, key, length)
.toDart;

return value.toDart;
}

extension ListExtension on List<String> {
@visibleForTesting
JSArray<JSString> get toJS => <JSString>[
for (final value in this) value.toJS,
].toJS;
JSArray<JSString> get toJS =>
<JSString>[for (final value in this) value.toJS].toJS;
}

extension AlgorithmExtension on Algorithm {
Expand Down Expand Up @@ -687,33 +616,27 @@ extension on JsonWebKey {

extension on JSJsonWebKey {
JsonWebKey get toDart => JsonWebKey(
kty: kty,
use: use,
key_ops: keyOps?.toDart.map((e) => e.toDart).toList(),
alg: alg,
ext: ext,
crv: crv,
x: x,
y: y,
d: d,
n: n,
e: e,
p: p,
q: q,
dp: dp,
dq: dq,
qi: qi,
oth: oth?.toDart
.map(
(e) => RsaOtherPrimesInfo(
r: e.r,
d: e.d,
t: e.t,
),
)
.toList(),
k: k,
);
kty: kty,
use: use,
key_ops: keyOps?.toDart.map((e) => e.toDart).toList(),
alg: alg,
ext: ext,
crv: crv,
x: x,
y: y,
d: d,
n: n,
e: e,
p: p,
q: q,
dp: dp,
dq: dq,
qi: qi,
oth: oth?.toDart
.map((e) => RsaOtherPrimesInfo(r: e.r, d: e.d, t: e.t))
.toList(),
k: k,
);
}

// TODO: crypto.subtle.unwrapKey
Expand Down
Loading
Loading