Skip to content

Commit e5c87ad

Browse files
committed
Migrated to null-safety
1 parent 2010361 commit e5c87ad

37 files changed

+633
-625
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.5.0-null-safety.0
2+
* Ported to null-safety without any breaking changes.
3+
14
# 0.2.2
25
* Increased Flutter SDK constraint to `>=1.24.0-10.2.pre` (current beta),
36
because API version breakage in dynamic linking API for Dart SDK.

example/pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ packages:
119119
name: ffi
120120
url: "https://pub.dartlang.org"
121121
source: hosted
122-
version: "0.1.3"
122+
version: "0.2.0-nullsafety.1"
123123
file:
124124
dependency: transitive
125125
description:
@@ -443,7 +443,7 @@ packages:
443443
path: ".."
444444
relative: true
445445
source: path
446-
version: "0.2.2"
446+
version: "0.5.0-null-safety.0"
447447
webdriver:
448448
dependency: transitive
449449
description:

lib/src/boringssl/bytestring.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import 'lookup/lookup.dart';
3333
/// };
3434
/// ```
3535
class CBS extends Struct {
36-
Pointer<Bytes> data;
36+
external Pointer<Bytes> data;
3737

3838
@IntPtr()
39-
int len;
39+
external int len;
4040
}
4141

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

74-
Pointer<CBB> child;
74+
external Pointer<CBB> child;
7575

7676
@IntPtr()
77-
int offset;
77+
external int offset;
7878

7979
@Uint8()
80-
int pending_len_len;
80+
external int pending_len_len;
8181

8282
@Int8()
83-
int pending_is_asn1;
83+
external int pending_is_asn1;
8484

8585
@Int8()
86-
int is_top_level;
86+
external int is_top_level;
8787
}
8888

8989
/// CBB_zero sets an uninitialised cbb to the zero state. It must be initialised

lib/src/boringssl/lookup/utils.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// ignore_for_file: non_constant_identifier_names
16+
1517
import 'dart:io' show Platform, Directory, File;
1618
import 'dart:ffi';
1719
import 'symbols.generated.dart';
@@ -38,7 +40,7 @@ String get libraryFileName {
3840
/// and initialize it with [initialize_dart_dl].
3941
///
4042
/// Returns `null` if it could not be found.
41-
Pointer<Void> Function(Sym) lookupLibraryInDotDartTool() {
43+
Pointer<Void> Function(Sym)? lookupLibraryInDotDartTool() {
4244
final dotDartTool = _findDotDartTool();
4345
if (dotDartTool == null) {
4446
return null;
@@ -51,7 +53,6 @@ Pointer<Void> Function(Sym) lookupLibraryInDotDartTool() {
5153
final library = DynamicLibrary.open(libraryFile.path);
5254

5355
// Try to lookup the 'webcrypto_lookup_symbol' symbol
54-
// ignore: non_constant_identifier_names
5556
final webcrypto_lookup_symbol = library
5657
.lookup<NativeFunction<Pointer<Void> Function(Int32)>>(
5758
'webcrypto_lookup_symbol',
@@ -89,7 +90,7 @@ void initialize_dart_dl(Pointer<Void> Function(Sym) lookup) {
8990
}
9091

9192
/// Find the `.dart_tool/` folder, returns `null` if unable to find it.
92-
Uri _findDotDartTool() {
93+
Uri? _findDotDartTool() {
9394
// HACK: Because 'dart:isolate' is unavailable in Flutter we have no means
9495
// by which we can find the location of the package_config.json file.
9596
// Which we need, because the binary library created by:

lib/src/impl_ffi/impl_ffi.aes_common.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ Uint8List _aesImportRawKey(List<int> keyData) {
2929

3030
Uint8List _aesImportJwkKey(
3131
Map<String, dynamic> jwk, {
32-
@required String expectedJwkAlgSuffix,
32+
required String expectedJwkAlgSuffix,
3333
}) {
34-
assert(expectedJwkAlgSuffix != null);
3534
ArgumentError.checkNotNull(jwk, 'jwk');
3635

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

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

6766
Map<String, dynamic> _aesExportJwkKey(
6867
List<int> keyData, {
69-
@required String jwkAlgSuffix,
68+
required String jwkAlgSuffix,
7069
}) {
71-
assert(jwkAlgSuffix != null);
7270
assert(keyData.length == 16 || keyData.length == 32);
7371
final algPrefix = keyData.length == 16 ? 'A128' : 'A256';
7472

lib/src/impl_ffi/impl_ffi.aesctr.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ Future<AesCtrSecretKey> aesCtr_importJsonWebKey(
2828
Future<AesCtrSecretKey> aesCtr_generateKey(int length) async =>
2929
_AesCtrSecretKey(_aesGenerateKey(length));
3030

31-
BigInt _parseBigEndian(List<int> data, [int bitLength]) {
32-
assert(data != null);
31+
BigInt _parseBigEndian(List<int> data, [int? bitLength]) {
3332
bitLength ??= data.length * 8;
3433
assert(bitLength <= data.length * 8);
3534

lib/src/impl_ffi/impl_ffi.aesgcm.dart

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
3232
List<int> key,
3333
List<int> data,
3434
List<int> iv,
35-
List<int> additionalData,
35+
List<int>? additionalData,
3636
int tagLength,
3737
bool isEncrypt,
3838
) async {
39-
ArgumentError.checkNotNull(data, 'data');
39+
final additionalData_ = additionalData ??= <int>[];
4040
if (isEncrypt && data.length > (1 << 39) - 256) {
4141
// More than this is not allowed by Web crypto spec, we shall honor that.
4242
throw _OperationError('data may not be more than 2^39 - 256 bytes');
4343
}
44-
tagLength ??= 128;
4544
if (tagLength != 32 &&
4645
tagLength != 64 &&
4746
tagLength != 96 &&
@@ -51,7 +50,6 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
5150
tagLength != 128) {
5251
throw _OperationError('tagLength must be 32, 64, 96, 104, 112, 120 or 128');
5352
}
54-
additionalData ??= [];
5553

5654
// TODO: Check iv length is less than EVP_AEAD_nonce_length
5755
// More importantly, add some test cases covering this, also consider
@@ -88,8 +86,8 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
8886
iv.length,
8987
scope.dataAsPointer(data),
9088
data.length,
91-
scope.dataAsPointer(additionalData),
92-
additionalData.length,
89+
scope.dataAsPointer(additionalData_),
90+
additionalData_.length,
9391
));
9492
}).sublist(0, outLen.value);
9593
} else {
@@ -104,8 +102,8 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
104102
iv.length,
105103
scope.dataAsPointer(data),
106104
data.length,
107-
scope.dataAsPointer(additionalData),
108-
additionalData.length,
105+
scope.dataAsPointer(additionalData_),
106+
additionalData_.length,
109107
));
110108
}).sublist(0, outLen.value);
111109
}
@@ -122,31 +120,31 @@ class _AesGcmSecretKey implements AesGcmSecretKey {
122120
Future<Uint8List> decryptBytes(
123121
List<int> data,
124122
List<int> iv, {
125-
List<int> additionalData,
126-
int tagLength = 128,
123+
List<int>? additionalData,
124+
int? tagLength = 128,
127125
}) async =>
128126
_aesGcmEncryptDecrypt(
129127
_key,
130128
data,
131129
iv,
132130
additionalData,
133-
tagLength,
131+
tagLength ?? 128,
134132
false,
135133
);
136134

137135
@override
138136
Future<Uint8List> encryptBytes(
139137
List<int> data,
140138
List<int> iv, {
141-
List<int> additionalData,
142-
int tagLength = 128,
139+
List<int>? additionalData,
140+
int? tagLength = 128,
143141
}) async =>
144142
_aesGcmEncryptDecrypt(
145143
_key,
146144
data,
147145
iv,
148146
additionalData,
149-
tagLength,
147+
tagLength ?? 128,
150148
true,
151149
);
152150

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ class _KeyPair<S, T> implements KeyPair<S, T> {
6464
@override
6565
final T publicKey;
6666

67-
_KeyPair({this.privateKey, this.publicKey});
67+
_KeyPair({required this.privateKey, required this.publicKey});
6868
}

lib/src/impl_ffi/impl_ffi.ec_common.dart

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ int _ecCurveToNID(EllipticCurve curve) {
3333

3434
/// Get [EllipticCurve] from matching BoringSSL `ssl.NID_...`.
3535
EllipticCurve _ecCurveFromNID(int nid) {
36-
assert(nid != null);
37-
3836
if (nid == ssl.NID_X9_62_prime256v1) {
3937
return EllipticCurve.p256;
4038
}
@@ -126,17 +124,22 @@ ffi.Pointer<ssl.EVP_PKEY> _importSpkiEcPublicKey(
126124
ffi.Pointer<ssl.EVP_PKEY> _importJwkEcPrivateOrPublicKey(
127125
JsonWebKey jwk,
128126
EllipticCurve curve, {
129-
@required bool isPrivateKey,
130-
@required String expectedUse,
131-
String expectedAlg, // may be null, if 'alg' property isn't validated (ECDH)
127+
required bool isPrivateKey,
128+
required String expectedUse,
129+
String? expectedAlg, // may be null, if 'alg' property isn't validated (ECDH)
132130
}) {
133-
assert(isPrivateKey != null);
134-
assert(expectedUse != null);
135-
136131
_checkData(
137132
jwk.kty == 'EC',
138133
message: 'expected a elliptic-curve key, JWK property "kty" must be "EC"',
139134
);
135+
_checkData(
136+
jwk.x != null,
137+
message: 'expected a elliptic-curve key, JWK property "x" to be present',
138+
);
139+
_checkData(
140+
jwk.y != null,
141+
message: 'expected a elliptic-curve key, JWK property "y" to be present',
142+
);
140143
if (isPrivateKey) {
141144
_checkData(
142145
jwk.d != null,
@@ -193,15 +196,15 @@ ffi.Pointer<ssl.EVP_PKEY> _importJwkEcPrivateOrPublicKey(
193196
_checkDataIsOne(
194197
ssl.EC_KEY_set_public_key_affine_coordinates(
195198
ec,
196-
decodeParam(jwk.x, 'x'),
197-
decodeParam(jwk.y, 'y'),
199+
decodeParam(jwk.x!, 'x'),
200+
decodeParam(jwk.y!, 'y'),
198201
),
199202
fallback: 'invalid EC key',
200203
);
201204

202205
if (isPrivateKey) {
203206
_checkDataIsOne(
204-
ssl.EC_KEY_set_private_key(ec, decodeParam(jwk.d, 'd')),
207+
ssl.EC_KEY_set_private_key(ec, decodeParam(jwk.d!, 'd')),
205208
fallback: 'invalid EC key',
206209
);
207210
}
@@ -262,7 +265,7 @@ Uint8List _exportRawEcPublicKey(ffi.Pointer<ssl.EVP_PKEY> key) {
262265
final scope = _Scope();
263266
try {
264267
final ec = ssl.EVP_PKEY_get1_EC_KEY(key);
265-
_checkOp(ec.address != null, fallback: 'internal key type invariant error');
268+
_checkOp(ec.address != 0, fallback: 'internal key type invariant error');
266269
scope.defer(() => ssl.EC_KEY_free(ec));
267270

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

284287
Map<String, dynamic> _exportJwkEcPrivateOrPublicKey(
285288
ffi.Pointer<ssl.EVP_PKEY> key, {
286-
@required bool isPrivateKey,
287-
String jwkUse,
289+
required bool isPrivateKey,
290+
String? jwkUse,
288291
}) {
289-
assert(isPrivateKey != null);
290-
291292
final scope = _Scope();
292293
try {
293294
final ec = ssl.EVP_PKEY_get1_EC_KEY(key);
@@ -318,7 +319,7 @@ Map<String, dynamic> _exportJwkEcPrivateOrPublicKey(
318319
_checkOpIsOne(ssl.BN_bn2bin_padded(p, paramSize, y));
319320
});
320321

321-
Uint8List dAsBytes;
322+
Uint8List? dAsBytes;
322323
if (isPrivateKey) {
323324
final d = ssl.EC_KEY_get0_private_key(ec);
324325
dAsBytes = _withOutPointer(paramSize, (ffi.Pointer<ssl.Bytes> p) {
@@ -332,7 +333,7 @@ Map<String, dynamic> _exportJwkEcPrivateOrPublicKey(
332333
crv: _ecCurveToJwkCrv(curve),
333334
x: _jwkEncodeBase64UrlNoPadding(xAsBytes),
334335
y: _jwkEncodeBase64UrlNoPadding(yAsBytes),
335-
d: isPrivateKey ? _jwkEncodeBase64UrlNoPadding(dAsBytes) : null,
336+
d: dAsBytes != null ? _jwkEncodeBase64UrlNoPadding(dAsBytes) : null,
336337
).toJson();
337338
} finally {
338339
scope.release();

lib/src/impl_ffi/impl_ffi.ecdh.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ class _EcdhPrivateKey implements EcdhPrivateKey {
8888

8989
final scope = _Scope();
9090
try {
91-
final _publicKey = publicKey as _EcdhPublicKey;
92-
93-
final pubEcKey = ssl.EVP_PKEY_get1_EC_KEY(_publicKey._key);
91+
final pubEcKey = ssl.EVP_PKEY_get1_EC_KEY(publicKey._key);
9492
_checkOp(pubEcKey.address != 0, fallback: 'not an ec key');
9593
scope.defer(() => ssl.EC_KEY_free(pubEcKey));
9694

0 commit comments

Comments
 (0)