Skip to content

Commit

Permalink
fix!: remove cyclic imports and migrate keypair class
Browse files Browse the repository at this point in the history
  • Loading branch information
HamdaanAliQuatil committed Oct 17, 2024
1 parent ec05a3d commit 4d4bd36
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 102 deletions.
12 changes: 0 additions & 12 deletions lib/src/impl_ffi/impl_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ class _OperationError extends Error implements OperationError {
String toString() => _message;
}

/// Implementation of [KeyPair].
class _KeyPair<S, T> implements KeyPair<S, T> {
@override
final S privateKey;

@override
final T publicKey;

_KeyPair({required this.privateKey, required this.publicKey});
}


const WebCryptoImpl webCryptImpl = _WebCryptoImpl();

final class _WebCryptoImpl implements WebCryptoImpl {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.ec_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ KeyPair<_EvpPKey, _EvpPKey> _generateEcKeyPair(
final pubKey = _EvpPKey();
_checkOpIsOne(ssl.EVP_PKEY_set1_EC_KEY.invoke(pubKey, ecPub));

return _KeyPair(
privateKey: privKey,
publicKey: pubKey,
return createKeyPair(
privKey,
pubKey,
);
});
}
15 changes: 9 additions & 6 deletions lib/src/impl_ffi/impl_ffi.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> ecdhPrivateKey_generateKe
EllipticCurve curve,
) async {
final p = _generateEcKeyPair(curve);
return _KeyPair(
privateKey: _EcdhPrivateKeyImpl(p.privateKey),
publicKey: _EcdhPublicKeyImpl(p.publicKey),
return createKeyPair(
_EcdhPrivateKeyImpl(p.privateKey),
_EcdhPublicKeyImpl(p.publicKey),
);
}

Expand Down Expand Up @@ -80,8 +80,11 @@ final class _StaticEcdhPrivateKeyImpl implements StaticEcdhPrivateKeyImpl {
ecdhPrivateKey_importJsonWebKey(jwk, curve);

@override
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve) =>
ecdhPrivateKey_generateKey(curve);
Future<(EcdhPrivateKeyImpl, EcdhPublicKeyImpl)> generateKey(EllipticCurve curve) async {
final KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl> keyPair = await ecdhPrivateKey_generateKey(curve);

return (keyPair.privateKey, keyPair.publicKey);
}
}

final class _EcdhPrivateKeyImpl implements EcdhPrivateKeyImpl {
Expand All @@ -100,7 +103,7 @@ final class _EcdhPrivateKeyImpl implements EcdhPrivateKeyImpl {
throw ArgumentError.value(
publicKey,
'publicKey',
'custom implementations of EcdhPublicKeyImpl is not supported',
'custom implementations of EcdhPublicKey is not supported',
);
}
if (length <= 0) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ Future<KeyPair<EcdsaPrivateKey, EcdsaPublicKey>> ecdsaPrivateKey_generateKey(
EllipticCurve curve,
) async {
final p = _generateEcKeyPair(curve);
return _KeyPair(
privateKey: _EcdsaPrivateKey(p.privateKey),
publicKey: _EcdsaPublicKey(p.publicKey),
return createKeyPair(
_EcdsaPrivateKey(p.privateKey),
_EcdsaPublicKey(p.publicKey),
);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/impl_ffi/impl_ffi.rsa_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ Map<String, dynamic> _exportJwkRsaPrivateOrPublicKey(
});
}

_KeyPair<_EvpPKey, _EvpPKey> _generateRsaKeyPair(
KeyPair<_EvpPKey, _EvpPKey> _generateRsaKeyPair(
int modulusLength,
BigInt publicExponent,
) {
Expand Down Expand Up @@ -278,9 +278,9 @@ _KeyPair<_EvpPKey, _EvpPKey> _generateRsaKeyPair(
final pubKey = _EvpPKey();
_checkOp(ssl.EVP_PKEY_set1_RSA.invoke(pubKey, pubRSA) == 1);

return _KeyPair(
privateKey: privKey,
publicKey: pubKey,
return createKeyPair(
privKey,
pubKey,
);
});
}
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
// Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws
final h = _Hash.fromHash(hash);
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
return _KeyPair(
privateKey: _RsaOaepPrivateKey(keys.privateKey, h),
publicKey: _RsaOaepPublicKey(keys.publicKey, h),
return createKeyPair(
_RsaOaepPrivateKey(keys.privateKey, h),
_RsaOaepPublicKey(keys.publicKey, h),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.rsapss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
// Validate and get hash function
final h = _Hash.fromHash(hash);
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
return _KeyPair(
privateKey: _RsaPssPrivateKey(keys.privateKey, h),
publicKey: _RsaPssPublicKey(keys.publicKey, h),
return createKeyPair(
_RsaPssPrivateKey(keys.privateKey, h),
_RsaPssPublicKey(keys.publicKey, h),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
// Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws
final h = _Hash.fromHash(hash);
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
return _KeyPair(
privateKey: _RsassaPkcs1V15PrivateKey(keys.privateKey, h),
publicKey: _RsassaPkcs1V15PublicKey(keys.publicKey, h),
return createKeyPair(
_RsassaPkcs1V15PrivateKey(keys.privateKey, h),
_RsassaPkcs1V15PublicKey(keys.publicKey, h),
);
}

Expand Down
34 changes: 32 additions & 2 deletions lib/src/impl_interface/impl_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ library impl_stub;
import 'dart:typed_data';
import 'dart:async';

import 'package:webcrypto/webcrypto.dart';

import 'package:webcrypto/webcrypto.dart' show Hash;

part 'impl_interface.aescbc.dart';
part 'impl_interface.aesctr.dart';
Expand All @@ -27,6 +26,37 @@ part 'impl_interface.pbkdf2.dart';
part 'impl_interface.aesgcm.dart';
part 'impl_interface.ecdh.dart';

/// A key-pair as returned from key generation.
class KeyPair<S, T> {
KeyPair._(this.privateKey, this.publicKey); // keep the constructor private.

/// Private key for [publicKey].
final S privateKey;

/// Public key matching [privateKey].
final T publicKey;
}

/// Factory method to create KeyPair instance
KeyPair<S, T> createKeyPair<S, T>(S privateKey, T publicKey) {
return KeyPair._(privateKey, publicKey);
}

/// Elliptic curves supported by ECDSA and ECDH.
///
/// > [!NOTE]
/// > Additional values may be added to this enum in the future.
enum EllipticCurve {
p256,
p384,

///
///
/// P-521 is **not supported on Safari**, see [bug 216755 (bugs.webkit.org)][1].
///
/// [1]: https://bugs.webkit.org/show_bug.cgi?id=216755
p521,
}

/// Interface to be provided by platform implementations.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/src/impl_interface/impl_interface.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ part of 'impl_interface.dart';
abstract interface class StaticEcdhPrivateKeyImpl {
Future<EcdhPrivateKeyImpl> importPkcs8Key(List<int> keyData, EllipticCurve curve);
Future<EcdhPrivateKeyImpl> importJsonWebKey(Map<String, dynamic> jwk, EllipticCurve curve);
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve);
Future<(EcdhPrivateKeyImpl, EcdhPublicKeyImpl)> generateKey(EllipticCurve curve);
}

abstract interface class EcdhPrivateKeyImpl {
Expand Down
11 changes: 0 additions & 11 deletions lib/src/impl_js/impl_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ class _OperationError extends Error implements OperationError {
String toString() => _message;
}

/// Implementation of [KeyPair].
class _KeyPair<S, T> implements KeyPair<S, T> {
@override
final S privateKey;

@override
final T publicKey;

_KeyPair({required this.privateKey, required this.publicKey});
}

const WebCryptoImpl webCryptImpl = _WebCryptoImpl();

final class _WebCryptoImpl implements WebCryptoImpl {
Expand Down
13 changes: 8 additions & 5 deletions lib/src/impl_js/impl_js.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> ecdhPrivateKey_generateKe
),
_usagesDeriveBits,
);
return _KeyPair(
privateKey: _EcdhPrivateKeyImpl(pair.privateKey),
publicKey: _EcdhPublicKeyImpl(pair.publicKey),
return createKeyPair(
_EcdhPrivateKeyImpl(pair.privateKey),
_EcdhPublicKeyImpl(pair.publicKey),
);
}

Expand Down Expand Up @@ -136,8 +136,11 @@ final class _StaticEcdhPrivateKeyImpl implements StaticEcdhPrivateKeyImpl {
ecdhPrivateKey_importJsonWebKey(jwk, curve);

@override
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve) =>
ecdhPrivateKey_generateKey(curve);
Future<(EcdhPrivateKeyImpl, EcdhPublicKeyImpl)> generateKey(EllipticCurve curve) async {
final KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl> keyPair = await ecdhPrivateKey_generateKey(curve);

return (keyPair.privateKey, keyPair.publicKey);
}
}

final class _EcdhPrivateKeyImpl implements EcdhPrivateKeyImpl {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.ecdsa.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ Future<KeyPair<EcdsaPrivateKey, EcdsaPublicKey>> ecdsaPrivateKey_generateKey(
),
_usagesSignVerify,
);
return _KeyPair(
privateKey: _EcdsaPrivateKey(pair.privateKey),
publicKey: _EcdsaPublicKey(pair.publicKey),
return createKeyPair(
_EcdsaPrivateKey(pair.privateKey),
_EcdsaPublicKey(pair.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
),
_usagesEncryptDecrypt,
);
return _KeyPair(
privateKey: _RsaOaepPrivateKey(pair.privateKey),
publicKey: _RsaOaepPublicKey(pair.publicKey),
return createKeyPair(
_RsaOaepPrivateKey(pair.privateKey),
_RsaOaepPublicKey(pair.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.rsapss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
),
_usagesSignVerify,
);
return _KeyPair(
privateKey: _RsaPssPrivateKey(pair.privateKey),
publicKey: _RsaPssPublicKey(pair.publicKey),
return createKeyPair(
_RsaPssPrivateKey(pair.privateKey),
_RsaPssPublicKey(pair.publicKey),
);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl_js/impl_js.rsassapkcs1v15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
),
_usagesSignVerify,
);
return _KeyPair(
privateKey: _RsassaPkcs1V15PrivateKey(pair.privateKey),
publicKey: _RsassaPkcs1V15PublicKey(pair.publicKey),
return createKeyPair(
_RsassaPkcs1V15PrivateKey(pair.privateKey),
_RsassaPkcs1V15PublicKey(pair.publicKey),
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/impl_stub/impl_stub.ecdh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class _StaticEcdhPrivateKeyImpl implements StaticEcdhPrivateKeyImpl {
throw UnimplementedError('Not implemented');

@override
Future<KeyPair<EcdhPrivateKeyImpl, EcdhPublicKeyImpl>> generateKey(EllipticCurve curve) =>
Future<(EcdhPrivateKeyImpl, EcdhPublicKeyImpl)> generateKey(EllipticCurve curve) =>
throw UnimplementedError('Not implemented');
}

Expand Down
30 changes: 2 additions & 28 deletions lib/src/webcrypto/webcrypto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import '../impl_stub/impl_stub.dart'
if (dart.library.ffi) '../impl_ffi/impl_ffi.dart'
if (dart.library.js) '../impl_js/impl_js.dart' show webCryptImpl;

export '../impl_interface/impl_interface.dart' show KeyPair, EllipticCurve;

part 'webcrypto.aescbc.dart';
part 'webcrypto.aesctr.dart';
part 'webcrypto.aesgcm.dart';
Expand All @@ -51,31 +53,3 @@ part 'webcrypto.rsassapkcs1v15.dart';
abstract class OperationError extends Error {
OperationError._(); // keep the constructor private.
}

/// A key-pair as returned from key generation.
@sealed
abstract class KeyPair<S, T> {
KeyPair._(); // keep the constructor private.

/// Private key for [publicKey].
S get privateKey;

/// Public key matching [privateKey].
T get publicKey;
}

/// Elliptic curves supported by ECDSA and ECDH.
///
/// > [!NOTE]
/// > Additional values may be added to this enum in the future.
enum EllipticCurve {
p256,
p384,

///
///
/// P-521 is **not supported on Safari**, see [bug 216755 (bugs.webkit.org)][1].
///
/// [1]: https://bugs.webkit.org/show_bug.cgi?id=216755
p521,
}
Loading

0 comments on commit 4d4bd36

Please sign in to comment.