Skip to content

Commit 12ca012

Browse files
authored
Make OperationError final (#177)
1 parent 0ce9a00 commit 12ca012

File tree

11 files changed

+29
-38
lines changed

11 files changed

+29
-38
lines changed

lib/src/impl_ffi/impl_ffi.aesgcm.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
4141
final additionalData_ = additionalData ??= <int>[];
4242
if (isEncrypt && data.length > (1 << 39) - 256) {
4343
// More than this is not allowed by Web crypto spec, we shall honor that.
44-
throw _OperationError('data may not be more than 2^39 - 256 bytes');
44+
throw operationError('data may not be more than 2^39 - 256 bytes');
4545
}
4646
if (tagLength != 32 &&
4747
tagLength != 64 &&
@@ -50,7 +50,7 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
5050
tagLength != 112 &&
5151
tagLength != 120 &&
5252
tagLength != 128) {
53-
throw _OperationError('tagLength must be 32, 64, 96, 104, 112, 120 or 128');
53+
throw operationError('tagLength must be 32, 64, 96, 104, 112, 120 or 128');
5454
}
5555

5656
// TODO: Check iv length is less than EVP_AEAD_nonce_length, if this is a requirement!

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ part 'impl_ffi.rsa_common.dart';
4848
part 'impl_ffi.ec_common.dart';
4949
part 'impl_ffi.aes_common.dart';
5050

51-
/// Implementation of [OperationError].
52-
class _OperationError extends Error implements OperationError {
53-
final String _message;
54-
55-
_OperationError(this._message);
56-
57-
@override
58-
String toString() => _message;
59-
}
60-
6151
const WebCryptoImpl webCryptImpl = _WebCryptoImpl();
6252

6353
final class _WebCryptoImpl implements WebCryptoImpl {

lib/src/impl_ffi/impl_ffi.ec_common.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ EllipticCurve _ecCurveFromNID(int nid) {
4141
return EllipticCurve.p521;
4242
}
4343
// This should never happen!
44-
throw _OperationError('internal error detecting curve');
44+
throw operationError('internal error detecting curve');
4545
}
4646

4747
String _ecCurveToJwkCrv(EllipticCurve curve) {

lib/src/impl_ffi/impl_ffi.ecdh.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class _EcdhPrivateKeyImpl implements EcdhPrivateKeyImpl {
142142
ssl.EC_GROUP_get_degree(ssl.EC_KEY_get0_group(privEcKey));
143143
final maxLength = 8 * (fieldSize / 8).ceil();
144144
if (length > maxLength) {
145-
throw _OperationError(
145+
throw operationError(
146146
'Length in ECDH key derivation is too large. '
147147
'Maximum allowed is $maxLength bits.',
148148
);

lib/src/impl_ffi/impl_ffi.hkdf.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final class _HkdfSecretKeyImpl implements HkdfSecretKeyImpl {
5353
// Mirroring limitations in chromium:
5454
// https://chromium.googlesource.com/chromium/src/+/43d62c50b705f88c67b14539e91fd8fd017f70c4/components/webcrypto/algorithms/hkdf.cc#74
5555
if (length % 8 != 0) {
56-
throw _OperationError('The length for HKDF must be a multiple of 8 bits');
56+
throw operationError('The length for HKDF must be a multiple of 8 bits');
5757
}
5858

5959
final lengthInBytes = length ~/ 8;
@@ -76,7 +76,7 @@ final class _HkdfSecretKeyImpl implements HkdfSecretKeyImpl {
7676
if (ERR_GET_LIB(packed_error) == ERR_LIB_HKDF &&
7777
ERR_GET_REASON(packed_error) == HKDF_R_OUTPUT_TOO_LARGE) {
7878
ssl.ERR_clear_error();
79-
throw _OperationError(
79+
throw operationError(
8080
'Length specified for HkdfSecretKey.deriveBits is too long',
8181
);
8282
}

lib/src/impl_ffi/impl_ffi.pbkdf2.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ final class _Pbkdf2SecretKeyImpl implements Pbkdf2SecretKeyImpl {
5555
// Mirroring limitations in chromium:
5656
// https://chromium.googlesource.com/chromium/src/+/43d62c50b705f88c67b14539e91fd8fd017f70c4/components/webcrypto/algorithms/pbkdf2.cc#75
5757
if (length % 8 != 0) {
58-
throw _OperationError(
58+
throw operationError(
5959
'The length for PBKDF2 must be a multiple of 8 bits');
6060
}
6161
if (length == 0) {
62-
throw _OperationError(
62+
throw operationError(
6363
'A length of zero is not allowed Pbkdf2SecretKey.deriveBits');
6464
}
6565
if (iterations <= 0) {
66-
throw _OperationError(
66+
throw operationError(
6767
'Iterations <= 0 is not allowed for Pbkdf2SecretKey.deriveBits');
6868
}
6969

lib/src/impl_ffi/impl_ffi.utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void _checkOp(bool condition, {String? message, String? fallback}) {
102102
// Always extract the error to ensure we clear the error queue.
103103
final err = _extractError();
104104
message ??= err ?? fallback ?? 'unknown error';
105-
throw _OperationError(message);
105+
throw operationError(message);
106106
}
107107
}
108108

lib/src/impl_interface/impl_interface.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ library impl_stub;
1717
import 'dart:typed_data';
1818
import 'dart:async';
1919

20+
import 'package:meta/meta.dart';
21+
2022
part 'impl_interface.aescbc.dart';
2123
part 'impl_interface.aesctr.dart';
2224
part 'impl_interface.hmac.dart';
@@ -62,6 +64,20 @@ enum EllipticCurve {
6264
p521,
6365
}
6466

67+
/// Thrown when an operation failed for an operation-specific reason.
68+
final class OperationError extends Error {
69+
final String _message;
70+
71+
OperationError._(this._message); // keep the constructor private.
72+
73+
@override
74+
String toString() => _message;
75+
}
76+
77+
/// Creating an [OperationError].
78+
@internal
79+
OperationError operationError(String message) => OperationError._(message);
80+
6581
/// Interface to be provided by platform implementations.
6682
///
6783
/// A platform implementation of `package:webcrypto` must define a

lib/src/impl_js/impl_js.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import 'dart:typed_data';
1919

2020
import 'package:webcrypto/src/impl_interface/impl_interface.dart';
2121

22-
import '../webcrypto/webcrypto.dart';
2322
import '../crypto_subtle.dart' as subtle;
2423

2524
part 'impl_js.aescbc.dart';
@@ -37,14 +36,6 @@ part 'impl_js.rsapss.dart';
3736
part 'impl_js.rsassapkcs1v15.dart';
3837
part 'impl_js.utils.dart';
3938

40-
/// Implementation of [OperationError].
41-
class _OperationError extends Error implements OperationError {
42-
final String _message;
43-
_OperationError(this._message);
44-
@override
45-
String toString() => _message;
46-
}
47-
4839
const WebCryptoImpl webCryptImpl = _WebCryptoImpl();
4940

5041
final class _WebCryptoImpl implements WebCryptoImpl {

lib/src/impl_js/impl_js.utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Object _translateDomException(
6969
case 'DataError':
7070
return FormatException(message);
7171
case 'OperationError':
72-
return _OperationError(message);
72+
return operationError(message);
7373
case 'InvalidAccessError':
7474
// InvalidAccessError occurs when the request operation is not valid for
7575
// the provided key. This is typically because:

0 commit comments

Comments
 (0)