From 25db3a061191f54652a098a6bc4f29dd27b64480 Mon Sep 17 00:00:00 2001 From: HamdaanAliQuatil <96776914+HamdaanAliQuatil@users.noreply.github.com> Date: Fri, 25 Oct 2024 18:48:55 +0530 Subject: [PATCH] Refactor RsaSsaPkcsPrivateKey and RsaSsaPkcsPublicKey Class (#170) --- lib/src/impl_ffi/impl_ffi.dart | 6 ++ lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart | 80 +++++++++++++++---- lib/src/impl_interface/impl_interface.dart | 3 + .../impl_interface.rsassapkcs1v15.dart | 45 +++++++++++ lib/src/impl_js/impl_js.dart | 6 ++ lib/src/impl_js/impl_js.rsassapkcs1v15.dart | 78 ++++++++++++++---- lib/src/impl_stub.dart | 32 -------- lib/src/impl_stub/impl_stub.dart | 7 ++ lib/src/impl_stub/impl_stub.rsapss.dart | 5 ++ .../impl_stub/impl_stub.rsassapkcs1v15.dart | 62 ++++++++++++++ .../webcrypto/webcrypto.rsassapkcs1v15.dart | 75 +++++++++++------ 11 files changed, 312 insertions(+), 87 deletions(-) create mode 100644 lib/src/impl_interface/impl_interface.rsassapkcs1v15.dart create mode 100644 lib/src/impl_stub/impl_stub.rsassapkcs1v15.dart diff --git a/lib/src/impl_ffi/impl_ffi.dart b/lib/src/impl_ffi/impl_ffi.dart index b0766b9a..8ce00e37 100644 --- a/lib/src/impl_ffi/impl_ffi.dart +++ b/lib/src/impl_ffi/impl_ffi.dart @@ -104,4 +104,10 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl(); + + @override + final rsaSsaPkcs1v15PrivateKey = const _StaticRsaSsaPkcs1V15PrivateKeyImpl(); + + @override + final rsaSsaPkcs1v15PublicKey = const _StaticRsaSsaPkcs1V15PublicKeyImpl(); } diff --git a/lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart b/lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart index f59a6871..7e6850a9 100644 --- a/lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart +++ b/lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart @@ -33,22 +33,22 @@ String _rsassaPkcs1V15JwkAlgFromHash(_Hash hash) { throw UnsupportedError('hash is not supported'); } -Future rsassaPkcs1V15PrivateKey_importPkcs8Key( +Future rsassaPkcs1V15PrivateKey_importPkcs8Key( List keyData, Hash hash, ) async { // Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws final h = _Hash.fromHash(hash); - return _RsassaPkcs1V15PrivateKey(_importPkcs8RsaPrivateKey(keyData), h); + return _RsaSsaPkcs1V15PrivateKeyImpl(_importPkcs8RsaPrivateKey(keyData), h); } -Future rsassaPkcs1V15PrivateKey_importJsonWebKey( +Future rsassaPkcs1V15PrivateKey_importJsonWebKey( Map jwk, Hash hash, ) async { // Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws final h = _Hash.fromHash(hash); - return _RsassaPkcs1V15PrivateKey( + return _RsaSsaPkcs1V15PrivateKeyImpl( _importJwkRsaPrivateOrPublicKey( JsonWebKey.fromJson(jwk), isPrivateKey: true, @@ -59,7 +59,7 @@ Future rsassaPkcs1V15PrivateKey_importJsonWebKey( ); } -Future> +Future> rsassaPkcs1V15PrivateKey_generateKey( int modulusLength, BigInt publicExponent, @@ -69,27 +69,27 @@ Future> final h = _Hash.fromHash(hash); final keys = _generateRsaKeyPair(modulusLength, publicExponent); return createKeyPair( - _RsassaPkcs1V15PrivateKey(keys.privateKey, h), - _RsassaPkcs1V15PublicKey(keys.publicKey, h), + _RsaSsaPkcs1V15PrivateKeyImpl(keys.privateKey, h), + _RsaSsaPkcs1V15PublicKeyImpl(keys.publicKey, h), ); } -Future rsassaPkcs1V15PublicKey_importSpkiKey( +Future rsassaPkcs1V15PublicKey_importSpkiKey( List keyData, Hash hash, ) async { // Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws final h = _Hash.fromHash(hash); - return _RsassaPkcs1V15PublicKey(_importSpkiRsaPublicKey(keyData), h); + return _RsaSsaPkcs1V15PublicKeyImpl(_importSpkiRsaPublicKey(keyData), h); } -Future rsassaPkcs1V15PublicKey_importJsonWebKey( +Future rsassaPkcs1V15PublicKey_importJsonWebKey( Map jwk, Hash hash, ) async { // Get hash first, to avoid a leak of EVP_PKEY if _Hash.fromHash throws final h = _Hash.fromHash(hash); - return _RsassaPkcs1V15PublicKey( + return _RsaSsaPkcs1V15PublicKeyImpl( _importJwkRsaPrivateOrPublicKey( JsonWebKey.fromJson(jwk), isPrivateKey: false, @@ -100,11 +100,45 @@ Future rsassaPkcs1V15PublicKey_importJsonWebKey( ); } -class _RsassaPkcs1V15PrivateKey implements RsassaPkcs1V15PrivateKey { +final class _StaticRsaSsaPkcs1V15PrivateKeyImpl + implements StaticRsaSsaPkcs1v15PrivateKeyImpl { + const _StaticRsaSsaPkcs1V15PrivateKeyImpl(); + + @override + Future importPkcs8Key( + List keyData, + Hash hash, + ) => + rsassaPkcs1V15PrivateKey_importPkcs8Key(keyData, hash); + + @override + Future importJsonWebKey( + Map jwk, + Hash hash, + ) => + rsassaPkcs1V15PrivateKey_importJsonWebKey(jwk, hash); + + @override + Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)> + generateKey( + int modulusLength, + BigInt publicExponent, + Hash hash, + ) async { + final KeyPair + pair = await rsassaPkcs1V15PrivateKey_generateKey( + modulusLength, publicExponent, hash); + + return (pair.privateKey, pair.publicKey); + } +} + +final class _RsaSsaPkcs1V15PrivateKeyImpl + implements RsaSsaPkcs1V15PrivateKeyImpl { final _EvpPKey _key; final _Hash _hash; - _RsassaPkcs1V15PrivateKey(this._key, this._hash); + _RsaSsaPkcs1V15PrivateKeyImpl(this._key, this._hash); @override String toString() { @@ -133,11 +167,27 @@ class _RsassaPkcs1V15PrivateKey implements RsassaPkcs1V15PrivateKey { Future exportPkcs8Key() async => _exportPkcs8Key(_key); } -class _RsassaPkcs1V15PublicKey implements RsassaPkcs1V15PublicKey { +final class _StaticRsaSsaPkcs1V15PublicKeyImpl + implements StaticRsaSsaPkcs1v15PublicKeyImpl { + const _StaticRsaSsaPkcs1V15PublicKeyImpl(); + + @override + Future importSpkiKey( + List keyData, Hash hash) => + rsassaPkcs1V15PublicKey_importSpkiKey(keyData, hash); + + @override + Future importJsonWebKey( + Map jwk, Hash hash) => + rsassaPkcs1V15PublicKey_importJsonWebKey(jwk, hash); +} + +final class _RsaSsaPkcs1V15PublicKeyImpl + implements RsaSsaPkcs1V15PublicKeyImpl { final _EvpPKey _key; final _Hash _hash; - _RsassaPkcs1V15PublicKey(this._key, this._hash); + _RsaSsaPkcs1V15PublicKeyImpl(this._key, this._hash); @override Future verifyBytes(List signature, List data) => diff --git a/lib/src/impl_interface/impl_interface.dart b/lib/src/impl_interface/impl_interface.dart index 9be5bcec..a4b24d1b 100644 --- a/lib/src/impl_interface/impl_interface.dart +++ b/lib/src/impl_interface/impl_interface.dart @@ -29,6 +29,7 @@ part 'impl_interface.ecdsa.dart'; part 'impl_interface.rsaoaep.dart'; part 'impl_interface.hkdf.dart'; part 'impl_interface.rsapss.dart'; +part 'impl_interface.rsassapkcs1v15.dart'; /// A key-pair as returned from key generation. class KeyPair { @@ -93,4 +94,6 @@ abstract interface class WebCryptoImpl { StaticHkdfSecretKeyImpl get hkdfSecretKey; StaticRsaPssPrivateKeyImpl get rsaPssPrivateKey; StaticRsaPssPublicKeyImpl get rsaPssPublicKey; + StaticRsaSsaPkcs1v15PrivateKeyImpl get rsaSsaPkcs1v15PrivateKey; + StaticRsaSsaPkcs1v15PublicKeyImpl get rsaSsaPkcs1v15PublicKey; } diff --git a/lib/src/impl_interface/impl_interface.rsassapkcs1v15.dart b/lib/src/impl_interface/impl_interface.rsassapkcs1v15.dart new file mode 100644 index 00000000..ca4ffe35 --- /dev/null +++ b/lib/src/impl_interface/impl_interface.rsassapkcs1v15.dart @@ -0,0 +1,45 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +part of 'impl_interface.dart'; + +abstract interface class StaticRsaSsaPkcs1v15PrivateKeyImpl { + Future importPkcs8Key( + List keyData, Hash hash); + Future importJsonWebKey( + Map jwk, Hash hash); + Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)> + generateKey(int modulusLength, BigInt publicExponent, Hash hash); +} + +abstract interface class RsaSsaPkcs1V15PrivateKeyImpl { + Future signBytes(List data); + Future signStream(Stream> data); + Future exportPkcs8Key(); + Future> exportJsonWebKey(); +} + +abstract interface class StaticRsaSsaPkcs1v15PublicKeyImpl { + Future importSpkiKey( + List keyData, Hash hash); + Future importJsonWebKey( + Map jwk, Hash hash); +} + +abstract interface class RsaSsaPkcs1V15PublicKeyImpl { + Future verifyBytes(List signature, List data); + Future verifyStream(List signature, Stream> data); + Future exportSpkiKey(); + Future> exportJsonWebKey(); +} diff --git a/lib/src/impl_js/impl_js.dart b/lib/src/impl_js/impl_js.dart index 2b20c90f..d1107def 100644 --- a/lib/src/impl_js/impl_js.dart +++ b/lib/src/impl_js/impl_js.dart @@ -91,4 +91,10 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl(); + + @override + final rsaSsaPkcs1v15PrivateKey = const _StaticRsaSsaPkcs1V15PrivateKeyImpl(); + + @override + final rsaSsaPkcs1v15PublicKey = const _StaticRsaSsaPkcs1V15PublicKeyImpl(); } diff --git a/lib/src/impl_js/impl_js.rsassapkcs1v15.dart b/lib/src/impl_js/impl_js.rsassapkcs1v15.dart index 5b56b451..71ef4593 100644 --- a/lib/src/impl_js/impl_js.rsassapkcs1v15.dart +++ b/lib/src/impl_js/impl_js.rsassapkcs1v15.dart @@ -18,11 +18,11 @@ part of 'impl_js.dart'; const _rsassaPkcs1V15Algorithm = subtle.Algorithm(name: 'RSASSA-PKCS1-v1_5'); -Future rsassaPkcs1V15PrivateKey_importPkcs8Key( +Future rsassaPkcs1V15PrivateKey_importPkcs8Key( List keyData, Hash hash, ) async { - return _RsassaPkcs1V15PrivateKey(await _importKey( + return _RsaSsaPkcs1V15PrivateKeyImpl(await _importKey( 'pkcs8', keyData, _rsassaPkcs1V15Algorithm.update(hash: _getHashAlgorithm(hash)), @@ -31,11 +31,11 @@ Future rsassaPkcs1V15PrivateKey_importPkcs8Key( )); } -Future rsassaPkcs1V15PrivateKey_importJsonWebKey( +Future rsassaPkcs1V15PrivateKey_importJsonWebKey( Map jwk, Hash hash, ) async { - return _RsassaPkcs1V15PrivateKey(await _importJsonWebKey( + return _RsaSsaPkcs1V15PrivateKeyImpl(await _importJsonWebKey( jwk, _rsassaPkcs1V15Algorithm.update(hash: _getHashAlgorithm(hash)), _usagesSign, @@ -43,7 +43,7 @@ Future rsassaPkcs1V15PrivateKey_importJsonWebKey( )); } -Future> +Future> rsassaPkcs1V15PrivateKey_generateKey( int modulusLength, BigInt publicExponent, @@ -58,16 +58,16 @@ Future> _usagesSignVerify, ); return createKeyPair( - _RsassaPkcs1V15PrivateKey(pair.privateKey), - _RsassaPkcs1V15PublicKey(pair.publicKey), + _RsaSsaPkcs1V15PrivateKeyImpl(pair.privateKey), + _RsaSsaPkcs1V15PublicKeyImpl(pair.publicKey), ); } -Future rsassaPkcs1V15PublicKey_importSpkiKey( +Future rsassaPkcs1V15PublicKey_importSpkiKey( List keyData, Hash hash, ) async { - return _RsassaPkcs1V15PublicKey(await _importKey( + return _RsaSsaPkcs1V15PublicKeyImpl(await _importKey( 'spki', keyData, _rsassaPkcs1V15Algorithm.update(hash: _getHashAlgorithm(hash)), @@ -76,11 +76,11 @@ Future rsassaPkcs1V15PublicKey_importSpkiKey( )); } -Future rsassaPkcs1V15PublicKey_importJsonWebKey( +Future rsassaPkcs1V15PublicKey_importJsonWebKey( Map jwk, Hash hash, ) async { - return _RsassaPkcs1V15PublicKey(await _importJsonWebKey( + return _RsaSsaPkcs1V15PublicKeyImpl(await _importJsonWebKey( jwk, _rsassaPkcs1V15Algorithm.update(hash: _getHashAlgorithm(hash)), _usagesVerify, @@ -88,9 +88,37 @@ Future rsassaPkcs1V15PublicKey_importJsonWebKey( )); } -class _RsassaPkcs1V15PrivateKey implements RsassaPkcs1V15PrivateKey { +final class _StaticRsaSsaPkcs1V15PrivateKeyImpl + implements StaticRsaSsaPkcs1v15PrivateKeyImpl { + const _StaticRsaSsaPkcs1V15PrivateKeyImpl(); + + @override + Future importPkcs8Key( + List keyData, Hash hash) async { + return await rsassaPkcs1V15PrivateKey_importPkcs8Key(keyData, hash); + } + + @override + Future importJsonWebKey( + Map jwk, Hash hash) async { + return await rsassaPkcs1V15PrivateKey_importJsonWebKey(jwk, hash); + } + + @override + Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)> + generateKey(int modulusLength, BigInt publicExponent, Hash hash) async { + final KeyPair + pair = await rsassaPkcs1V15PrivateKey_generateKey( + modulusLength, publicExponent, hash); + + return (pair.privateKey, pair.publicKey); + } +} + +final class _RsaSsaPkcs1V15PrivateKeyImpl + implements RsaSsaPkcs1V15PrivateKeyImpl { final subtle.JSCryptoKey _key; - _RsassaPkcs1V15PrivateKey(this._key); + _RsaSsaPkcs1V15PrivateKeyImpl(this._key); @override Future signBytes(List data) async { @@ -113,13 +141,31 @@ class _RsassaPkcs1V15PrivateKey implements RsassaPkcs1V15PrivateKey { } } -class _RsassaPkcs1V15PublicKey implements RsassaPkcs1V15PublicKey { +final class _StaticRsaSsaPkcs1V15PublicKeyImpl + implements StaticRsaSsaPkcs1v15PublicKeyImpl { + const _StaticRsaSsaPkcs1V15PublicKeyImpl(); + + @override + Future importSpkiKey( + List keyData, Hash hash) async { + return await rsassaPkcs1V15PublicKey_importSpkiKey(keyData, hash); + } + + @override + Future importJsonWebKey( + Map jwk, Hash hash) async { + return await rsassaPkcs1V15PublicKey_importJsonWebKey(jwk, hash); + } +} + +final class _RsaSsaPkcs1V15PublicKeyImpl + implements RsaSsaPkcs1V15PublicKeyImpl { final subtle.JSCryptoKey _key; - _RsassaPkcs1V15PublicKey(this._key); + _RsaSsaPkcs1V15PublicKeyImpl(this._key); @override String toString() { - return 'Instance of \'RsassaPkcs1V15PublicKey\''; + return 'Instance of \'RsaSsaPkcs1V15PublicKeyImpl\''; } @override diff --git a/lib/src/impl_stub.dart b/lib/src/impl_stub.dart index e72b7e1e..db0e9ad9 100644 --- a/lib/src/impl_stub.dart +++ b/lib/src/impl_stub.dart @@ -47,38 +47,6 @@ const Hash sha512 = _UnimplementedHash(); //---------------------- RSASSA_PKCS1_v1_5 -Future rsassaPkcs1V15PrivateKey_importPkcs8Key( - List keyData, - Hash hash, -) => - throw _notImplemented; - -Future rsassaPkcs1V15PrivateKey_importJsonWebKey( - Map jwk, - Hash hash, -) => - throw _notImplemented; - -Future> - rsassaPkcs1V15PrivateKey_generateKey( - int modulusLength, - BigInt publicExponent, - Hash hash, -) => - throw _notImplemented; - -Future rsassaPkcs1V15PublicKey_importSpkiKey( - List keyData, - Hash hash, -) => - throw _notImplemented; - -Future rsassaPkcs1V15PublicKey_importJsonWebKey( - Map jwk, - Hash hash, -) => - throw _notImplemented; - //---------------------- RSA-PSS //---------------------- ECDSA diff --git a/lib/src/impl_stub/impl_stub.dart b/lib/src/impl_stub/impl_stub.dart index 2ede5cc0..4bc8a6d8 100644 --- a/lib/src/impl_stub/impl_stub.dart +++ b/lib/src/impl_stub/impl_stub.dart @@ -28,6 +28,7 @@ part 'impl_stub.ecdsa.dart'; part 'impl_stub.rsaoaep.dart'; part 'impl_stub.hkdf.dart'; part 'impl_stub.rsapss.dart'; +part 'impl_stub.rsassapkcs1v15.dart'; const WebCryptoImpl webCryptImpl = _WebCryptoImpl(); @@ -75,4 +76,10 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl(); + + @override + final rsaSsaPkcs1v15PrivateKey = const _StaticRsaSsaPkcs1V15PrivateKeyImpl(); + + @override + final rsaSsaPkcs1v15PublicKey = const _StaticRsaSsaPkcs1V15PublicKeyImpl(); } diff --git a/lib/src/impl_stub/impl_stub.rsapss.dart b/lib/src/impl_stub/impl_stub.rsapss.dart index c3b1d9bd..ce3b4b7b 100644 --- a/lib/src/impl_stub/impl_stub.rsapss.dart +++ b/lib/src/impl_stub/impl_stub.rsapss.dart @@ -17,18 +17,21 @@ part of 'impl_stub.dart'; final class _StaticRsaPssPrivateKeyImpl implements StaticRsaPssPrivateKeyImpl { const _StaticRsaPssPrivateKeyImpl(); + @override Future importPkcs8Key( List keyData, Hash hash, ) => throw UnimplementedError('Not implemented'); + @override Future importJsonWebKey( Map jwk, Hash hash, ) => throw UnimplementedError('Not implemented'); + @override Future<(RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl)> generateKey( int modulusLength, BigInt publicExponent, @@ -40,12 +43,14 @@ final class _StaticRsaPssPrivateKeyImpl implements StaticRsaPssPrivateKeyImpl { final class _StaticRsaPssPublicKeyImpl implements StaticRsaPssPublicKeyImpl { const _StaticRsaPssPublicKeyImpl(); + @override Future importSpkiKey( List keyData, Hash hash, ) => throw UnimplementedError('Not implemented'); + @override Future importJsonWebKey( Map jwk, Hash hash, diff --git a/lib/src/impl_stub/impl_stub.rsassapkcs1v15.dart b/lib/src/impl_stub/impl_stub.rsassapkcs1v15.dart new file mode 100644 index 00000000..03f374ee --- /dev/null +++ b/lib/src/impl_stub/impl_stub.rsassapkcs1v15.dart @@ -0,0 +1,62 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +part of 'impl_stub.dart'; + +final class _StaticRsaSsaPkcs1V15PrivateKeyImpl + implements StaticRsaSsaPkcs1v15PrivateKeyImpl { + const _StaticRsaSsaPkcs1V15PrivateKeyImpl(); + + @override + Future importPkcs8Key( + List keyData, + Hash hash, + ) => + throw UnimplementedError('Not implemented'); + + @override + Future importJsonWebKey( + Map jwk, + Hash hash, + ) => + throw UnimplementedError('Not implemented'); + + @override + Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)> + generateKey( + int modulusLength, + BigInt publicExponent, + Hash hash, + ) => + throw UnimplementedError('Not implemented'); +} + +final class _StaticRsaSsaPkcs1V15PublicKeyImpl + implements StaticRsaSsaPkcs1v15PublicKeyImpl { + const _StaticRsaSsaPkcs1V15PublicKeyImpl(); + + @override + Future importSpkiKey( + List keyData, + Hash hash, + ) => + throw UnimplementedError('Not implemented'); + + @override + Future importJsonWebKey( + Map jwk, + Hash hash, + ) => + throw UnimplementedError('Not implemented'); +} diff --git a/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart b/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart index a0e2910e..8bafb32a 100644 --- a/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart +++ b/lib/src/webcrypto/webcrypto.rsassapkcs1v15.dart @@ -58,9 +58,14 @@ part of 'webcrypto.dart'; /// [1]: https://tools.ietf.org/html/rfc3447 /// [2]: https://tools.ietf.org/html/rfc5208 /// [3]: https://tools.ietf.org/html/rfc7517 -@sealed -abstract class RsassaPkcs1V15PrivateKey { - RsassaPkcs1V15PrivateKey._(); // keep the constructor private. +final class RsassaPkcs1V15PrivateKey { + final RsaSsaPkcs1V15PrivateKeyImpl _impl; + + RsassaPkcs1V15PrivateKey._(this._impl); // keep the constructor private. + + factory RsassaPkcs1V15PrivateKey(RsaSsaPkcs1V15PrivateKeyImpl impl) { + return RsassaPkcs1V15PrivateKey._(impl); + } /// Import RSASSA-PKCS1-v1_5 private key in PKCS #8 format. /// @@ -100,8 +105,10 @@ abstract class RsassaPkcs1V15PrivateKey { static Future importPkcs8Key( List keyData, Hash hash, - ) { - return impl.rsassaPkcs1V15PrivateKey_importPkcs8Key(keyData, hash); + ) async { + final impl = await webCryptImpl.rsaSsaPkcs1v15PrivateKey + .importPkcs8Key(keyData, hash); + return RsassaPkcs1V15PrivateKey._(impl); } /// Import RSASSA-PKCS1-v1_5 private key in [JSON Web Key][1] format. @@ -159,8 +166,10 @@ abstract class RsassaPkcs1V15PrivateKey { static Future importJsonWebKey( Map jwk, Hash hash, - ) { - return impl.rsassaPkcs1V15PrivateKey_importJsonWebKey(jwk, hash); + ) async { + final impl = + await webCryptImpl.rsaSsaPkcs1v15PrivateKey.importJsonWebKey(jwk, hash); + return RsassaPkcs1V15PrivateKey._(impl); } /// Generate an RSASSA-PKCS1-v1_5 public/private key-pair. @@ -239,12 +248,18 @@ abstract class RsassaPkcs1V15PrivateKey { int modulusLength, BigInt publicExponent, Hash hash, - ) { - return impl.rsassaPkcs1V15PrivateKey_generateKey( + ) async { + final (privateKeyImpl, publicKeyImpl) = + await webCryptImpl.rsaSsaPkcs1v15PrivateKey.generateKey( modulusLength, publicExponent, hash, ); + + final privateKey = RsassaPkcs1V15PrivateKey(privateKeyImpl); + final publicKey = RsassaPkcs1V15PublicKey(publicKeyImpl); + + return createKeyPair(privateKey, publicKey); } /// Sign [data] with this RSASSA-PKCS1-v1_5 private key. @@ -278,7 +293,7 @@ abstract class RsassaPkcs1V15PrivateKey { /// /// print('signature: ${base64.encode(signature)}'); /// ``` - Future signBytes(List data); + Future signBytes(List data) => _impl.signBytes(data); /// Sign [data] with this RSASSA-PKCS1-v1_5 private key. /// @@ -313,7 +328,8 @@ abstract class RsassaPkcs1V15PrivateKey { /// /// print('signature: ${base64.encode(signature)}'); /// ``` - Future signStream(Stream> data); + Future signStream(Stream> data) => + _impl.signStream(data); /// Export this RSASSA-PKCS1-v1_5 private key in PKCS #8 format. /// @@ -342,7 +358,7 @@ abstract class RsassaPkcs1V15PrivateKey { /// ``` /// /// [1]: https://tools.ietf.org/html/rfc5208 - Future exportPkcs8Key(); + Future exportPkcs8Key() => _impl.exportPkcs8Key(); /// Export RSASSA-PKCS1-v1_5 private key in [JSON Web Key][1] format. /// @@ -373,7 +389,7 @@ abstract class RsassaPkcs1V15PrivateKey { /// ``` /// /// [1]: https://tools.ietf.org/html/rfc7517 - Future> exportJsonWebKey(); + Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } /// RSASSA-PKCS1-v1_5 public key for signing messages. @@ -395,9 +411,14 @@ abstract class RsassaPkcs1V15PrivateKey { /// [1]: https://tools.ietf.org/html/rfc3447 /// [2]: https://tools.ietf.org/html/rfc5280 /// [3]: https://tools.ietf.org/html/rfc7517 -@sealed -abstract class RsassaPkcs1V15PublicKey { - RsassaPkcs1V15PublicKey._(); // keep the constructor private. +final class RsassaPkcs1V15PublicKey { + final RsaSsaPkcs1V15PublicKeyImpl _impl; + + RsassaPkcs1V15PublicKey._(this._impl); // keep the constructor private. + + factory RsassaPkcs1V15PublicKey(RsaSsaPkcs1V15PublicKeyImpl impl) { + return RsassaPkcs1V15PublicKey._(impl); + } /// Import RSASSA-PKCS1-v1_5 public key in SPKI format. /// @@ -435,8 +456,10 @@ abstract class RsassaPkcs1V15PublicKey { static Future importSpkiKey( List keyData, Hash hash, - ) { - return impl.rsassaPkcs1V15PublicKey_importSpkiKey(keyData, hash); + ) async { + final impl = + await webCryptImpl.rsaSsaPkcs1v15PublicKey.importSpkiKey(keyData, hash); + return RsassaPkcs1V15PublicKey._(impl); } /// Import RSASSA-PKCS1-v1_5 public key in [JSON Web Key][1] format. @@ -476,8 +499,10 @@ abstract class RsassaPkcs1V15PublicKey { static Future importJsonWebKey( Map jwk, Hash hash, - ) { - return impl.rsassaPkcs1V15PublicKey_importJsonWebKey(jwk, hash); + ) async { + final impl = + await webCryptImpl.rsaSsaPkcs1v15PublicKey.importJsonWebKey(jwk, hash); + return RsassaPkcs1V15PublicKey._(impl); } /// Verify [signature] of [data] using this RSASSA-PKCS1-v1_5 public key. @@ -511,7 +536,8 @@ abstract class RsassaPkcs1V15PublicKey { /// print('Authentic message from Bob: $message'); /// } /// ``` - Future verifyBytes(List signature, List data); + Future verifyBytes(List signature, List data) => + _impl.verifyBytes(signature, data); /// Verify [signature] of [data] using this RSASSA-PKCS1-v1_5 public key. /// @@ -544,7 +570,8 @@ abstract class RsassaPkcs1V15PublicKey { /// print('Authentic message from Bob: $message'); /// } /// ``` - Future verifyStream(List signature, Stream> data); + Future verifyStream(List signature, Stream> data) => + _impl.verifyStream(signature, data); /// Export RSASSA-PKCS1-v1_5 public key in SPKI format. /// @@ -573,7 +600,7 @@ abstract class RsassaPkcs1V15PublicKey { /// ``` /// /// [1]: https://tools.ietf.org/html/rfc5280 - Future exportSpkiKey(); + Future exportSpkiKey() => _impl.exportSpkiKey(); /// Export RSASSA-PKCS1-v1_5 public key in [JSON Web Key][1] format. /// @@ -601,5 +628,5 @@ abstract class RsassaPkcs1V15PublicKey { /// ``` /// /// [1]: https://tools.ietf.org/html/rfc7517 - Future> exportJsonWebKey(); + Future> exportJsonWebKey() => _impl.exportJsonWebKey(); }