Skip to content

Commit

Permalink
Migrated to null-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfj committed Dec 8, 2020
1 parent 2010361 commit e5c87ad
Show file tree
Hide file tree
Showing 37 changed files with 633 additions and 625 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.5.0-null-safety.0
* Ported to null-safety without any breaking changes.

# 0.2.2
* Increased Flutter SDK constraint to `>=1.24.0-10.2.pre` (current beta),
because API version breakage in dynamic linking API for Dart SDK.
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ packages:
name: ffi
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
version: "0.2.0-nullsafety.1"
file:
dependency: transitive
description:
Expand Down Expand Up @@ -443,7 +443,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.2.2"
version: "0.5.0-null-safety.0"
webdriver:
dependency: transitive
description:
Expand Down
16 changes: 8 additions & 8 deletions lib/src/boringssl/bytestring.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import 'lookup/lookup.dart';
/// };
/// ```
class CBS extends Struct {
Pointer<Bytes> data;
external Pointer<Bytes> data;

@IntPtr()
int len;
external int len;
}

/// CBS_init sets cbs to point to data. It does not take ownership of data.
Expand Down Expand Up @@ -69,21 +69,21 @@ final CBS_init = resolve(Sym.CBS_init)
/// };
/// ```
class CBB extends Struct {
Pointer<Void> base;
external Pointer<Void> base;

Pointer<CBB> child;
external Pointer<CBB> child;

@IntPtr()
int offset;
external int offset;

@Uint8()
int pending_len_len;
external int pending_len_len;

@Int8()
int pending_is_asn1;
external int pending_is_asn1;

@Int8()
int is_top_level;
external int is_top_level;
}

/// CBB_zero sets an uninitialised cbb to the zero state. It must be initialised
Expand Down
7 changes: 4 additions & 3 deletions lib/src/boringssl/lookup/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// ignore_for_file: non_constant_identifier_names

import 'dart:io' show Platform, Directory, File;
import 'dart:ffi';
import 'symbols.generated.dart';
Expand All @@ -38,7 +40,7 @@ String get libraryFileName {
/// and initialize it with [initialize_dart_dl].
///
/// Returns `null` if it could not be found.
Pointer<Void> Function(Sym) lookupLibraryInDotDartTool() {
Pointer<Void> Function(Sym)? lookupLibraryInDotDartTool() {
final dotDartTool = _findDotDartTool();
if (dotDartTool == null) {
return null;
Expand All @@ -51,7 +53,6 @@ Pointer<Void> Function(Sym) lookupLibraryInDotDartTool() {
final library = DynamicLibrary.open(libraryFile.path);

// Try to lookup the 'webcrypto_lookup_symbol' symbol
// ignore: non_constant_identifier_names
final webcrypto_lookup_symbol = library
.lookup<NativeFunction<Pointer<Void> Function(Int32)>>(
'webcrypto_lookup_symbol',
Expand Down Expand Up @@ -89,7 +90,7 @@ void initialize_dart_dl(Pointer<Void> Function(Sym) lookup) {
}

/// Find the `.dart_tool/` folder, returns `null` if unable to find it.
Uri _findDotDartTool() {
Uri? _findDotDartTool() {
// HACK: Because 'dart:isolate' is unavailable in Flutter we have no means
// by which we can find the location of the package_config.json file.
// Which we need, because the binary library created by:
Expand Down
8 changes: 3 additions & 5 deletions lib/src/impl_ffi/impl_ffi.aes_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ Uint8List _aesImportRawKey(List<int> keyData) {

Uint8List _aesImportJwkKey(
Map<String, dynamic> jwk, {
@required String expectedJwkAlgSuffix,
required String expectedJwkAlgSuffix,
}) {
assert(expectedJwkAlgSuffix != null);
ArgumentError.checkNotNull(jwk, 'jwk');

final k = JsonWebKey.fromJson(jwk);
Expand All @@ -43,7 +42,7 @@ Uint8List _aesImportJwkKey(
checkJwk(k.k != null, 'k', 'must be present');
checkJwk(k.use == null || k.use == 'enc', 'use', 'must be "enc", if present');

final keyData = _jwkDecodeBase64UrlNoPadding(k.k, 'k');
final keyData = _jwkDecodeBase64UrlNoPadding(k.k!, 'k');
if (keyData.length == 24) {
// 192-bit AES is intentionally unsupported, see https://crbug.com/533699
// If not supported in Chrome, there is not reason to support it in Dart.
Expand All @@ -66,9 +65,8 @@ Uint8List _aesImportJwkKey(

Map<String, dynamic> _aesExportJwkKey(
List<int> keyData, {
@required String jwkAlgSuffix,
required String jwkAlgSuffix,
}) {
assert(jwkAlgSuffix != null);
assert(keyData.length == 16 || keyData.length == 32);
final algPrefix = keyData.length == 16 ? 'A128' : 'A256';

Expand Down
3 changes: 1 addition & 2 deletions lib/src/impl_ffi/impl_ffi.aesctr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ Future<AesCtrSecretKey> aesCtr_importJsonWebKey(
Future<AesCtrSecretKey> aesCtr_generateKey(int length) async =>
_AesCtrSecretKey(_aesGenerateKey(length));

BigInt _parseBigEndian(List<int> data, [int bitLength]) {
assert(data != null);
BigInt _parseBigEndian(List<int> data, [int? bitLength]) {
bitLength ??= data.length * 8;
assert(bitLength <= data.length * 8);

Expand Down
26 changes: 12 additions & 14 deletions lib/src/impl_ffi/impl_ffi.aesgcm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
List<int> key,
List<int> data,
List<int> iv,
List<int> additionalData,
List<int>? additionalData,
int tagLength,
bool isEncrypt,
) async {
ArgumentError.checkNotNull(data, 'data');
final additionalData_ = additionalData ??= <int>[];
if (isEncrypt && data.length > (1 << 39) - 256) {
// More than this is not allowed by Web crypto spec, we shall honor that.
throw _OperationError('data may not be more than 2^39 - 256 bytes');
}
tagLength ??= 128;
if (tagLength != 32 &&
tagLength != 64 &&
tagLength != 96 &&
Expand All @@ -51,7 +50,6 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
tagLength != 128) {
throw _OperationError('tagLength must be 32, 64, 96, 104, 112, 120 or 128');
}
additionalData ??= [];

// TODO: Check iv length is less than EVP_AEAD_nonce_length
// More importantly, add some test cases covering this, also consider
Expand Down Expand Up @@ -88,8 +86,8 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
iv.length,
scope.dataAsPointer(data),
data.length,
scope.dataAsPointer(additionalData),
additionalData.length,
scope.dataAsPointer(additionalData_),
additionalData_.length,
));
}).sublist(0, outLen.value);
} else {
Expand All @@ -104,8 +102,8 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
iv.length,
scope.dataAsPointer(data),
data.length,
scope.dataAsPointer(additionalData),
additionalData.length,
scope.dataAsPointer(additionalData_),
additionalData_.length,
));
}).sublist(0, outLen.value);
}
Expand All @@ -122,31 +120,31 @@ class _AesGcmSecretKey implements AesGcmSecretKey {
Future<Uint8List> decryptBytes(
List<int> data,
List<int> iv, {
List<int> additionalData,
int tagLength = 128,
List<int>? additionalData,
int? tagLength = 128,
}) async =>
_aesGcmEncryptDecrypt(
_key,
data,
iv,
additionalData,
tagLength,
tagLength ?? 128,
false,
);

@override
Future<Uint8List> encryptBytes(
List<int> data,
List<int> iv, {
List<int> additionalData,
int tagLength = 128,
List<int>? additionalData,
int? tagLength = 128,
}) async =>
_aesGcmEncryptDecrypt(
_key,
data,
iv,
additionalData,
tagLength,
tagLength ?? 128,
true,
);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/impl_ffi/impl_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ class _KeyPair<S, T> implements KeyPair<S, T> {
@override
final T publicKey;

_KeyPair({this.privateKey, this.publicKey});
_KeyPair({required this.privateKey, required this.publicKey});
}
37 changes: 19 additions & 18 deletions lib/src/impl_ffi/impl_ffi.ec_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ int _ecCurveToNID(EllipticCurve curve) {

/// Get [EllipticCurve] from matching BoringSSL `ssl.NID_...`.
EllipticCurve _ecCurveFromNID(int nid) {
assert(nid != null);

if (nid == ssl.NID_X9_62_prime256v1) {
return EllipticCurve.p256;
}
Expand Down Expand Up @@ -126,17 +124,22 @@ ffi.Pointer<ssl.EVP_PKEY> _importSpkiEcPublicKey(
ffi.Pointer<ssl.EVP_PKEY> _importJwkEcPrivateOrPublicKey(
JsonWebKey jwk,
EllipticCurve curve, {
@required bool isPrivateKey,
@required String expectedUse,
String expectedAlg, // may be null, if 'alg' property isn't validated (ECDH)
required bool isPrivateKey,
required String expectedUse,
String? expectedAlg, // may be null, if 'alg' property isn't validated (ECDH)
}) {
assert(isPrivateKey != null);
assert(expectedUse != null);

_checkData(
jwk.kty == 'EC',
message: 'expected a elliptic-curve key, JWK property "kty" must be "EC"',
);
_checkData(
jwk.x != null,
message: 'expected a elliptic-curve key, JWK property "x" to be present',
);
_checkData(
jwk.y != null,
message: 'expected a elliptic-curve key, JWK property "y" to be present',
);
if (isPrivateKey) {
_checkData(
jwk.d != null,
Expand Down Expand Up @@ -193,15 +196,15 @@ ffi.Pointer<ssl.EVP_PKEY> _importJwkEcPrivateOrPublicKey(
_checkDataIsOne(
ssl.EC_KEY_set_public_key_affine_coordinates(
ec,
decodeParam(jwk.x, 'x'),
decodeParam(jwk.y, 'y'),
decodeParam(jwk.x!, 'x'),
decodeParam(jwk.y!, 'y'),
),
fallback: 'invalid EC key',
);

if (isPrivateKey) {
_checkDataIsOne(
ssl.EC_KEY_set_private_key(ec, decodeParam(jwk.d, 'd')),
ssl.EC_KEY_set_private_key(ec, decodeParam(jwk.d!, 'd')),
fallback: 'invalid EC key',
);
}
Expand Down Expand Up @@ -262,7 +265,7 @@ Uint8List _exportRawEcPublicKey(ffi.Pointer<ssl.EVP_PKEY> key) {
final scope = _Scope();
try {
final ec = ssl.EVP_PKEY_get1_EC_KEY(key);
_checkOp(ec.address != null, fallback: 'internal key type invariant error');
_checkOp(ec.address != 0, fallback: 'internal key type invariant error');
scope.defer(() => ssl.EC_KEY_free(ec));

return _withOutCBB((cbb) {
Expand All @@ -283,11 +286,9 @@ Uint8List _exportRawEcPublicKey(ffi.Pointer<ssl.EVP_PKEY> key) {

Map<String, dynamic> _exportJwkEcPrivateOrPublicKey(
ffi.Pointer<ssl.EVP_PKEY> key, {
@required bool isPrivateKey,
String jwkUse,
required bool isPrivateKey,
String? jwkUse,
}) {
assert(isPrivateKey != null);

final scope = _Scope();
try {
final ec = ssl.EVP_PKEY_get1_EC_KEY(key);
Expand Down Expand Up @@ -318,7 +319,7 @@ Map<String, dynamic> _exportJwkEcPrivateOrPublicKey(
_checkOpIsOne(ssl.BN_bn2bin_padded(p, paramSize, y));
});

Uint8List dAsBytes;
Uint8List? dAsBytes;
if (isPrivateKey) {
final d = ssl.EC_KEY_get0_private_key(ec);
dAsBytes = _withOutPointer(paramSize, (ffi.Pointer<ssl.Bytes> p) {
Expand All @@ -332,7 +333,7 @@ Map<String, dynamic> _exportJwkEcPrivateOrPublicKey(
crv: _ecCurveToJwkCrv(curve),
x: _jwkEncodeBase64UrlNoPadding(xAsBytes),
y: _jwkEncodeBase64UrlNoPadding(yAsBytes),
d: isPrivateKey ? _jwkEncodeBase64UrlNoPadding(dAsBytes) : null,
d: dAsBytes != null ? _jwkEncodeBase64UrlNoPadding(dAsBytes) : null,
).toJson();
} finally {
scope.release();
Expand Down
4 changes: 1 addition & 3 deletions lib/src/impl_ffi/impl_ffi.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ class _EcdhPrivateKey implements EcdhPrivateKey {

final scope = _Scope();
try {
final _publicKey = publicKey as _EcdhPublicKey;

final pubEcKey = ssl.EVP_PKEY_get1_EC_KEY(_publicKey._key);
final pubEcKey = ssl.EVP_PKEY_get1_EC_KEY(publicKey._key);
_checkOp(pubEcKey.address != 0, fallback: 'not an ec key');
scope.defer(() => ssl.EC_KEY_free(pubEcKey));

Expand Down
4 changes: 2 additions & 2 deletions lib/src/impl_ffi/impl_ffi.ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ Uint8List _convertEcdsaDerSignatureToWebCryptoSignature(
/// Returns `null` if the [signature] is invalid and should be rejected.
///
/// See also: https://chromium.googlesource.com/chromium/src/+/43d62c50b705f88c67b14539e91fd8fd017f70c4/components/webcrypto/algorithms/ecdsa.cc#111
Uint8List _convertEcdsaWebCryptoSignatureToDerSignature(
Uint8List? _convertEcdsaWebCryptoSignatureToDerSignature(
ffi.Pointer<ssl.EVP_PKEY> key,
Uint8List signature,
List<int> signature,
) {
final scope = _Scope();
try {
Expand Down
Loading

0 comments on commit e5c87ad

Please sign in to comment.