From 115547fe88cc1f4c36e212fe34b891c51ed2d2ef Mon Sep 17 00:00:00 2001 From: HamdaanAliQuatil Date: Fri, 20 Sep 2024 22:55:16 +0530 Subject: [PATCH 1/3] refactor: update rsaoaep-private-key rsaoaep-public-key --- lib/src/impl_ffi/impl_ffi.dart | 6 ++ lib/src/impl_ffi/impl_ffi.rsaoaep.dart | 71 +++++++++++++++---- lib/src/impl_interface/impl_interface.dart | 3 + .../impl_interface.rsaoaep.dart | 38 ++++++++++ lib/src/impl_js/impl_js.dart | 6 ++ lib/src/impl_js/impl_js.rsaoaep.dart | 67 ++++++++++++----- lib/src/impl_stub.dart | 32 --------- lib/src/impl_stub/impl_stub.dart | 7 ++ lib/src/impl_stub/impl_stub.rsaoaep.dart | 54 ++++++++++++++ lib/src/webcrypto/webcrypto.rsaoaep.dart | 57 ++++++++------- 10 files changed, 251 insertions(+), 90 deletions(-) create mode 100644 lib/src/impl_interface/impl_interface.rsaoaep.dart create mode 100644 lib/src/impl_stub/impl_stub.rsaoaep.dart diff --git a/lib/src/impl_ffi/impl_ffi.dart b/lib/src/impl_ffi/impl_ffi.dart index 95de3f58..a903db08 100644 --- a/lib/src/impl_ffi/impl_ffi.dart +++ b/lib/src/impl_ffi/impl_ffi.dart @@ -83,4 +83,10 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final hmacSecretKey = const _StaticHmacSecretKeyImpl(); + + @override + final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl(); + + @override + final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl(); } diff --git a/lib/src/impl_ffi/impl_ffi.rsaoaep.dart b/lib/src/impl_ffi/impl_ffi.rsaoaep.dart index bdd9c028..c85084f6 100644 --- a/lib/src/impl_ffi/impl_ffi.rsaoaep.dart +++ b/lib/src/impl_ffi/impl_ffi.rsaoaep.dart @@ -33,22 +33,22 @@ String _rsaOaepJwkAlgFromHash(_Hash hash) { throw UnsupportedError('hash is not supported'); } -Future rsaOaepPrivateKey_importPkcs8Key( +Future rsaOaepPrivateKey_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 _RsaOaepPrivateKey(_importPkcs8RsaPrivateKey(keyData), h); + return _RsaOaepPrivateKeyImpl(_importPkcs8RsaPrivateKey(keyData), h); } -Future rsaOaepPrivateKey_importJsonWebKey( +Future rsaOaepPrivateKey_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 _RsaOaepPrivateKey( + return _RsaOaepPrivateKeyImpl( _importJwkRsaPrivateOrPublicKey( JsonWebKey.fromJson(jwk), isPrivateKey: true, @@ -59,7 +59,7 @@ Future rsaOaepPrivateKey_importJsonWebKey( ); } -Future> +Future> rsaOaepPrivateKey_generateKey( int modulusLength, BigInt publicExponent, @@ -69,27 +69,27 @@ Future> final h = _Hash.fromHash(hash); final keys = _generateRsaKeyPair(modulusLength, publicExponent); return _KeyPair( - privateKey: _RsaOaepPrivateKey(keys.privateKey, h), - publicKey: _RsaOaepPublicKey(keys.publicKey, h), + privateKey: _RsaOaepPrivateKeyImpl(keys.privateKey, h), + publicKey: _RsaOaepPublicKeyImpl(keys.publicKey, h), ); } -Future rsaOaepPublicKey_importSpkiKey( +Future rsaOaepPublicKey_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 _RsaOaepPublicKey(_importSpkiRsaPublicKey(keyData), h); + return _RsaOaepPublicKeyImpl(_importSpkiRsaPublicKey(keyData), h); } -Future rsaOaepPublicKey_importJsonWebKey( +Future rsaOaepPublicKey_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 _RsaOaepPublicKey( + return _RsaOaepPublicKeyImpl( _importJwkRsaPrivateOrPublicKey( JsonWebKey.fromJson(jwk), isPrivateKey: false, @@ -167,11 +167,34 @@ Future _rsaOaepeEncryptOrDecryptBytes( }); } -class _RsaOaepPrivateKey implements RsaOaepPrivateKey { +final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl { + const _StaticRsaOaepPrivateKeyImpl(); + + @override + Future importPkcs8Key(List keyData, Hash hash) => + rsaOaepPrivateKey_importPkcs8Key(keyData, hash); + + @override + Future importJsonWebKey( + Map jwk, + Hash hash, + ) => + rsaOaepPrivateKey_importJsonWebKey(jwk, hash); + + @override + Future> generateKey( + int modulusLength, + BigInt publicExponent, + Hash hash, + ) => + rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash); +} + +final class _RsaOaepPrivateKeyImpl implements RsaOaepPrivateKeyImpl { final _EvpPKey _key; final _Hash _hash; - _RsaOaepPrivateKey(this._key, this._hash); + _RsaOaepPrivateKeyImpl(this._key, this._hash); @override String toString() { @@ -203,11 +226,29 @@ class _RsaOaepPrivateKey implements RsaOaepPrivateKey { Future exportPkcs8Key() async => _exportPkcs8Key(_key); } -class _RsaOaepPublicKey implements RsaOaepPublicKey { +final class _StaticRsaOaepPublicKeyImpl implements StaticRsaOaepPublicKeyImpl { + const _StaticRsaOaepPublicKeyImpl(); + + @override + Future importSpkiKey( + List keyData, + Hash hash, + ) => + rsaOaepPublicKey_importSpkiKey(keyData, hash); + + @override + Future importJsonWebKey( + Map jwk, + Hash hash, + ) => + rsaOaepPublicKey_importJsonWebKey(jwk, hash); +} + +final class _RsaOaepPublicKeyImpl implements RsaOaepPublicKeyImpl { final _EvpPKey _key; final _Hash _hash; - _RsaOaepPublicKey(this._key, this._hash); + _RsaOaepPublicKeyImpl(this._key, this._hash); @override String toString() { diff --git a/lib/src/impl_interface/impl_interface.dart b/lib/src/impl_interface/impl_interface.dart index bfc53b9c..7c01424d 100644 --- a/lib/src/impl_interface/impl_interface.dart +++ b/lib/src/impl_interface/impl_interface.dart @@ -23,6 +23,7 @@ import 'package:webcrypto/webcrypto.dart'; part 'impl_interface.aescbc.dart'; part 'impl_interface.aesctr.dart'; part 'impl_interface.hmac.dart'; +part 'impl_interface.rsaoaep.dart'; /// Interface to be provided by platform implementations. /// @@ -44,4 +45,6 @@ abstract interface class WebCryptoImpl { StaticAesCbcSecretKeyImpl get aesCbcSecretKey; StaticAesCtrSecretKeyImpl get aesCtrSecretKey; StaticHmacSecretKeyImpl get hmacSecretKey; + StaticRsaOaepPrivateKeyImpl get rsaOaepPrivateKey; + StaticRsaOaepPublicKeyImpl get rsaOaepPublicKey; } diff --git a/lib/src/impl_interface/impl_interface.rsaoaep.dart b/lib/src/impl_interface/impl_interface.rsaoaep.dart new file mode 100644 index 00000000..2c07a5e1 --- /dev/null +++ b/lib/src/impl_interface/impl_interface.rsaoaep.dart @@ -0,0 +1,38 @@ +// 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 StaticRsaOaepPrivateKeyImpl { + Future importPkcs8Key(List keyData, Hash hash); + Future importJsonWebKey(Map jwk, Hash hash); + Future> generateKey(int modulusLength, BigInt publicExponent, Hash hash); +} + +abstract interface class RsaOaepPrivateKeyImpl { + Future decryptBytes(List data, {List? label}); + Future exportPkcs8Key(); + Future> exportJsonWebKey(); +} + +abstract interface class StaticRsaOaepPublicKeyImpl { + Future importSpkiKey(List keyData, Hash hash); + Future importJsonWebKey(Map jwk, Hash hash); +} + +abstract interface class RsaOaepPublicKeyImpl { + Future encryptBytes(List data, {List? label}); + Future exportSpkiKey(); + Future> exportJsonWebKey(); +} diff --git a/lib/src/impl_js/impl_js.dart b/lib/src/impl_js/impl_js.dart index 6984219a..7ffb0fcb 100644 --- a/lib/src/impl_js/impl_js.dart +++ b/lib/src/impl_js/impl_js.dart @@ -69,4 +69,10 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final hmacSecretKey = const _StaticHmacSecretKeyImpl(); + + @override + final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl(); + + @override + final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl(); } diff --git a/lib/src/impl_js/impl_js.rsaoaep.dart b/lib/src/impl_js/impl_js.rsaoaep.dart index 0cc42210..36a8307f 100644 --- a/lib/src/impl_js/impl_js.rsaoaep.dart +++ b/lib/src/impl_js/impl_js.rsaoaep.dart @@ -18,11 +18,11 @@ part of 'impl_js.dart'; const _rsaOaepAlgorithmName = 'RSA-OAEP'; -Future rsaOaepPrivateKey_importPkcs8Key( +Future rsaOaepPrivateKey_importPkcs8Key( List keyData, Hash hash, ) async { - return _RsaOaepPrivateKey(await _importKey( + return _RsaOaepPrivateKeyImpl(await _importKey( 'pkcs8', keyData, subtle.Algorithm( @@ -34,11 +34,11 @@ Future rsaOaepPrivateKey_importPkcs8Key( )); } -Future rsaOaepPrivateKey_importJsonWebKey( +Future rsaOaepPrivateKey_importJsonWebKey( Map jwk, Hash hash, ) async { - return _RsaOaepPrivateKey(await _importJsonWebKey( + return _RsaOaepPrivateKeyImpl(await _importJsonWebKey( jwk, subtle.Algorithm( name: _rsaOaepAlgorithmName, @@ -49,7 +49,7 @@ Future rsaOaepPrivateKey_importJsonWebKey( )); } -Future> +Future> rsaOaepPrivateKey_generateKey( int modulusLength, BigInt publicExponent, @@ -65,16 +65,16 @@ Future> _usagesEncryptDecrypt, ); return _KeyPair( - privateKey: _RsaOaepPrivateKey(pair.privateKey), - publicKey: _RsaOaepPublicKey(pair.publicKey), + privateKey: _RsaOaepPrivateKeyImpl(pair.privateKey), + publicKey: _RsaOaepPublicKeyImpl(pair.publicKey), ); } -Future rsaOaepPublicKey_importSpkiKey( +Future rsaOaepPublicKey_importSpkiKey( List keyData, Hash hash, ) async { - return _RsaOaepPublicKey(await _importKey( + return _RsaOaepPublicKeyImpl(await _importKey( 'spki', keyData, subtle.Algorithm( @@ -86,11 +86,11 @@ Future rsaOaepPublicKey_importSpkiKey( )); } -Future rsaOaepPublicKey_importJsonWebKey( +Future rsaOaepPublicKey_importJsonWebKey( Map jwk, Hash hash, ) async { - return _RsaOaepPublicKey(await _importJsonWebKey( + return _RsaOaepPublicKeyImpl(await _importJsonWebKey( jwk, subtle.Algorithm( name: _rsaOaepAlgorithmName, @@ -101,13 +101,32 @@ Future rsaOaepPublicKey_importJsonWebKey( )); } -class _RsaOaepPrivateKey implements RsaOaepPrivateKey { +final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl { + const _StaticRsaOaepPrivateKeyImpl(); + + @override + Future importPkcs8Key(List keyData, Hash hash) { + return rsaOaepPrivateKey_importPkcs8Key(keyData, hash); + } + + @override + Future importJsonWebKey(Map jwk, Hash hash) { + return rsaOaepPrivateKey_importJsonWebKey(jwk, hash); + } + + @override + Future> generateKey(int modulusLength, BigInt publicExponent, Hash hash) { + return rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash); + } +} + +final class _RsaOaepPrivateKeyImpl implements RsaOaepPrivateKeyImpl { final subtle.JSCryptoKey _key; - _RsaOaepPrivateKey(this._key); + _RsaOaepPrivateKeyImpl(this._key); @override String toString() { - return 'Instance of \'RsaOaepPrivateKey\''; + return 'Instance of \'RsaOaepPrivateKeyImpl\''; } @override @@ -135,13 +154,27 @@ class _RsaOaepPrivateKey implements RsaOaepPrivateKey { } } -class _RsaOaepPublicKey implements RsaOaepPublicKey { +final class _StaticRsaOaepPublicKeyImpl implements StaticRsaOaepPublicKeyImpl { + const _StaticRsaOaepPublicKeyImpl(); + + @override + Future importSpkiKey(List keyData, Hash hash) { + return rsaOaepPublicKey_importSpkiKey(keyData, hash); + } + + @override + Future importJsonWebKey(Map jwk, Hash hash) { + return rsaOaepPublicKey_importJsonWebKey(jwk, hash); + } +} + +final class _RsaOaepPublicKeyImpl implements RsaOaepPublicKeyImpl { final subtle.JSCryptoKey _key; - _RsaOaepPublicKey(this._key); + _RsaOaepPublicKeyImpl(this._key); @override String toString() { - return 'Instance of \'RsaOaepPublicKey\''; + return 'Instance of \'RsaOaepPublicKeyImpl\''; } @override diff --git a/lib/src/impl_stub.dart b/lib/src/impl_stub.dart index 18326ed8..3011276a 100644 --- a/lib/src/impl_stub.dart +++ b/lib/src/impl_stub.dart @@ -152,38 +152,6 @@ Future ecdsaPublicKey_importJsonWebKey( //---------------------- RSA-OAEP -Future rsaOaepPrivateKey_importPkcs8Key( - List keyData, - Hash hash, -) => - throw _notImplemented; - -Future rsaOaepPrivateKey_importJsonWebKey( - Map jwk, - Hash hash, -) => - throw _notImplemented; - -Future> - rsaOaepPrivateKey_generateKey( - int modulusLength, - BigInt publicExponent, - Hash hash, -) => - throw _notImplemented; - -Future rsaOaepPublicKey_importSpkiKey( - List keyData, - Hash hash, -) => - throw _notImplemented; - -Future rsaOaepPublicKey_importJsonWebKey( - Map jwk, - Hash hash, -) => - throw _notImplemented; - //---------------------- AES-CTR //---------------------- AES-CBC diff --git a/lib/src/impl_stub/impl_stub.dart b/lib/src/impl_stub/impl_stub.dart index 84ea0832..5d72a38b 100644 --- a/lib/src/impl_stub/impl_stub.dart +++ b/lib/src/impl_stub/impl_stub.dart @@ -20,6 +20,7 @@ import 'package:webcrypto/webcrypto.dart'; part 'impl_stub.aescbc.dart'; part 'impl_stub.aesctr.dart'; part 'impl_stub.hmac.dart'; +part 'impl_stub.rsaoaep.dart'; const WebCryptoImpl webCryptImpl = _WebCryptoImpl(); @@ -34,4 +35,10 @@ final class _WebCryptoImpl implements WebCryptoImpl { @override final hmacSecretKey = const _StaticHmacSecretKeyImpl(); + + @override + final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl(); + + @override + final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl(); } diff --git a/lib/src/impl_stub/impl_stub.rsaoaep.dart b/lib/src/impl_stub/impl_stub.rsaoaep.dart new file mode 100644 index 00000000..52d12871 --- /dev/null +++ b/lib/src/impl_stub/impl_stub.rsaoaep.dart @@ -0,0 +1,54 @@ +// 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 _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl { + const _StaticRsaOaepPrivateKeyImpl(); + + @override + Future importPkcs8Key(List keyData, Hash hash) { + throw UnimplementedError('Not implemented'); + } + + @override + Future importJsonWebKey(Map jwk, Hash hash) { + throw UnimplementedError('Not implemented'); + } + + @override + Future> generateKey(int modulusLength, BigInt publicExponent, Hash hash) { + throw UnimplementedError('Not implemented'); + } +} + +final class _StaticRsaOaepPublicKeyImpl implements StaticRsaOaepPublicKeyImpl { + const _StaticRsaOaepPublicKeyImpl(); + + @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.rsaoaep.dart b/lib/src/webcrypto/webcrypto.rsaoaep.dart index edce7e53..efcf1160 100644 --- a/lib/src/webcrypto/webcrypto.rsaoaep.dart +++ b/lib/src/webcrypto/webcrypto.rsaoaep.dart @@ -72,9 +72,10 @@ 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 RsaOaepPrivateKey { - RsaOaepPrivateKey._(); // keep the constructor private. +final class RsaOaepPrivateKey { + final RsaOaepPrivateKeyImpl _impl; + + RsaOaepPrivateKey._(this._impl); // keep the constructor private. /// Import RSAES-OAEP private key in PKCS #8 format. /// @@ -112,8 +113,9 @@ abstract class RsaOaepPrivateKey { static Future importPkcs8Key( List keyData, Hash hash, - ) { - return impl.rsaOaepPrivateKey_importPkcs8Key(keyData, hash); + ) async { + final impl = await webCryptImpl.rsaOaepPrivateKey.importPkcs8Key(keyData, hash); + return RsaOaepPrivateKey._(impl); } /// Import RSAES-OAEP private key in [JSON Web Key][1] format. @@ -159,8 +161,9 @@ abstract class RsaOaepPrivateKey { static Future importJsonWebKey( Map jwk, Hash hash, - ) { - return impl.rsaOaepPrivateKey_importJsonWebKey(jwk, hash); + ) async { + final impl = await webCryptImpl.rsaOaepPrivateKey.importJsonWebKey(jwk, hash); + return RsaOaepPrivateKey._(impl); } /// Generate an RSAES-OAEP public/private key-pair. @@ -215,12 +218,9 @@ abstract class RsaOaepPrivateKey { int modulusLength, BigInt publicExponent, Hash hash, - ) { - return impl.rsaOaepPrivateKey_generateKey( - modulusLength, - publicExponent, - hash, - ); + ) async { + final pair = await webCryptImpl.rsaOaepPrivateKey.generateKey(modulusLength, publicExponent, hash); + return pair as KeyPair; } /// Decrypt [data] encrypted with [RsaOaepPublicKey.encryptBytes] from the @@ -263,7 +263,8 @@ abstract class RsaOaepPrivateKey { /// ); /// // Now both Alice and Bob share a secret key. /// ``` - Future decryptBytes(List data, {List? label}); + Future decryptBytes(List data, {List? label}) => + _impl.decryptBytes(data, label: label); /// Export this RSAES-OAEP private key in PKCS #8 format. /// @@ -292,7 +293,7 @@ abstract class RsaOaepPrivateKey { /// ``` /// /// [1]: https://tools.ietf.org/html/rfc5208 - Future exportPkcs8Key(); + Future exportPkcs8Key() => _impl.exportPkcs8Key(); /// Export RSAES-OAEP private key in [JSON Web Key][1] format. /// @@ -320,7 +321,7 @@ abstract class RsaOaepPrivateKey { /// ``` /// /// [1]: https://tools.ietf.org/html/rfc7517 - Future> exportJsonWebKey(); + Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } /// RSAES-OAEP public key for decryption of messages. @@ -342,9 +343,10 @@ abstract class RsaOaepPrivateKey { /// [1]: https://tools.ietf.org/html/rfc3447 /// [2]: https://tools.ietf.org/html/rfc5280 /// [3]: https://tools.ietf.org/html/rfc7517 -@sealed -abstract class RsaOaepPublicKey { - RsaOaepPublicKey._(); // keep the constructor private. +final class RsaOaepPublicKey { + final RsaOaepPublicKeyImpl _impl; + + RsaOaepPublicKey._(this._impl); // keep the constructor private. /// Import RSAES-OAEP public key in SPKI format. /// @@ -382,8 +384,9 @@ abstract class RsaOaepPublicKey { static Future importSpkiKey( List keyData, Hash hash, - ) { - return impl.rsaOaepPublicKey_importSpkiKey(keyData, hash); + ) async { + final impl = await webCryptImpl.rsaOaepPublicKey.importSpkiKey(keyData, hash); + return RsaOaepPublicKey._(impl); } /// Import RSAES-OAEP public key in [JSON Web Key][1] format. @@ -423,8 +426,9 @@ abstract class RsaOaepPublicKey { static Future importJsonWebKey( Map jwk, Hash hash, - ) { - return impl.rsaOaepPublicKey_importJsonWebKey(jwk, hash); + ) async { + final impl = await webCryptImpl.rsaOaepPublicKey.importJsonWebKey(jwk, hash); + return RsaOaepPublicKey._(impl); } /// Encrypt [data] such that it can only be decrypted with @@ -493,7 +497,8 @@ abstract class RsaOaepPublicKey { // // See also documentation for crypto/rsa in golang: // https://pkg.go.dev/crypto/rsa#EncryptOAEP - Future encryptBytes(List data, {List? label}); + Future encryptBytes(List data, {List? label}) => + _impl.encryptBytes(data, label: label); /// Export this RSAES-OAEP public key in SPKI format. /// @@ -522,7 +527,7 @@ abstract class RsaOaepPublicKey { /// ``` /// /// [1]: https://tools.ietf.org/html/rfc5280 - Future exportSpkiKey(); + Future exportSpkiKey() => _impl.exportSpkiKey(); /// Export RSAES-OAEP public key in [JSON Web Key][1] format. /// @@ -550,5 +555,5 @@ abstract class RsaOaepPublicKey { /// ``` /// /// [1]: https://tools.ietf.org/html/rfc7517 - Future> exportJsonWebKey(); + Future> exportJsonWebKey() => _impl.exportJsonWebKey(); } From 2991d1eee086667b1f8eea980ad034e29cdf41f3 Mon Sep 17 00:00:00 2001 From: HamdaanAliQuatil Date: Sat, 19 Oct 2024 05:11:37 +0530 Subject: [PATCH 2/3] fix: migrate rsaoaep keypair to tuples --- lib/src/impl_ffi/impl_ffi.rsaoaep.dart | 14 +++++++++----- lib/src/impl_interface/impl_interface.dart | 2 +- .../impl_interface/impl_interface.rsaoaep.dart | 2 +- lib/src/impl_js/impl_js.rsaoaep.dart | 10 ++++++---- lib/src/impl_stub/impl_stub.rsaoaep.dart | 2 +- lib/src/webcrypto/webcrypto.rsaoaep.dart | 16 ++++++++++++++-- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/src/impl_ffi/impl_ffi.rsaoaep.dart b/lib/src/impl_ffi/impl_ffi.rsaoaep.dart index 31d82001..178df0e8 100644 --- a/lib/src/impl_ffi/impl_ffi.rsaoaep.dart +++ b/lib/src/impl_ffi/impl_ffi.rsaoaep.dart @@ -69,8 +69,8 @@ Future> final h = _Hash.fromHash(hash); final keys = _generateRsaKeyPair(modulusLength, publicExponent); return createKeyPair( - _RsaOaepPrivateKey(keys.privateKey, h), - _RsaOaepPublicKey(keys.publicKey, h), + _RsaOaepPrivateKeyImpl(keys.privateKey, h), + _RsaOaepPublicKeyImpl(keys.publicKey, h), ); } @@ -182,12 +182,16 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl rsaOaepPrivateKey_importJsonWebKey(jwk, hash); @override - Future> generateKey( + Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey( int modulusLength, BigInt publicExponent, Hash hash, - ) => - rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash); + ) async { + final KeyPair keyPair = + await rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash); + + return (keyPair.privateKey, keyPair.publicKey); + } } final class _RsaOaepPrivateKeyImpl implements RsaOaepPrivateKeyImpl { diff --git a/lib/src/impl_interface/impl_interface.dart b/lib/src/impl_interface/impl_interface.dart index 75224296..e9258c61 100644 --- a/lib/src/impl_interface/impl_interface.dart +++ b/lib/src/impl_interface/impl_interface.dart @@ -25,6 +25,7 @@ part 'impl_interface.hmac.dart'; part 'impl_interface.pbkdf2.dart'; part 'impl_interface.aesgcm.dart'; part 'impl_interface.ecdh.dart'; +part 'impl_interface.rsaoaep.dart'; /// A key-pair as returned from key generation. class KeyPair { @@ -57,7 +58,6 @@ enum EllipticCurve { /// [1]: https://bugs.webkit.org/show_bug.cgi?id=216755 p521, } -part 'impl_interface.rsaoaep.dart'; /// Interface to be provided by platform implementations. /// diff --git a/lib/src/impl_interface/impl_interface.rsaoaep.dart b/lib/src/impl_interface/impl_interface.rsaoaep.dart index 2c07a5e1..c7ec953c 100644 --- a/lib/src/impl_interface/impl_interface.rsaoaep.dart +++ b/lib/src/impl_interface/impl_interface.rsaoaep.dart @@ -17,7 +17,7 @@ part of 'impl_interface.dart'; abstract interface class StaticRsaOaepPrivateKeyImpl { Future importPkcs8Key(List keyData, Hash hash); Future importJsonWebKey(Map jwk, Hash hash); - Future> generateKey(int modulusLength, BigInt publicExponent, Hash hash); + Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash); } abstract interface class RsaOaepPrivateKeyImpl { diff --git a/lib/src/impl_js/impl_js.rsaoaep.dart b/lib/src/impl_js/impl_js.rsaoaep.dart index e50593eb..3bddc64a 100644 --- a/lib/src/impl_js/impl_js.rsaoaep.dart +++ b/lib/src/impl_js/impl_js.rsaoaep.dart @@ -65,8 +65,8 @@ Future> _usagesEncryptDecrypt, ); return createKeyPair( - _RsaOaepPrivateKey(pair.privateKey), - _RsaOaepPublicKey(pair.publicKey), + _RsaOaepPrivateKeyImpl(pair.privateKey), + _RsaOaepPublicKeyImpl(pair.publicKey), ); } @@ -115,8 +115,10 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl } @override - Future> generateKey(int modulusLength, BigInt publicExponent, Hash hash) { - return rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash); + Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash) async { + final KeyPair keyPair = await rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash); + + return (keyPair.privateKey, keyPair.publicKey); } } diff --git a/lib/src/impl_stub/impl_stub.rsaoaep.dart b/lib/src/impl_stub/impl_stub.rsaoaep.dart index 52d12871..516a9907 100644 --- a/lib/src/impl_stub/impl_stub.rsaoaep.dart +++ b/lib/src/impl_stub/impl_stub.rsaoaep.dart @@ -28,7 +28,7 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl } @override - Future> generateKey(int modulusLength, BigInt publicExponent, Hash hash) { + Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash) { throw UnimplementedError('Not implemented'); } } diff --git a/lib/src/webcrypto/webcrypto.rsaoaep.dart b/lib/src/webcrypto/webcrypto.rsaoaep.dart index efcf1160..e5acf0af 100644 --- a/lib/src/webcrypto/webcrypto.rsaoaep.dart +++ b/lib/src/webcrypto/webcrypto.rsaoaep.dart @@ -77,6 +77,10 @@ final class RsaOaepPrivateKey { RsaOaepPrivateKey._(this._impl); // keep the constructor private. + factory RsaOaepPrivateKey(RsaOaepPrivateKeyImpl impl) { + return RsaOaepPrivateKey._(impl); + } + /// Import RSAES-OAEP private key in PKCS #8 format. /// /// Creates an [RsaOaepPrivateKey] from [keyData] given as the DER @@ -219,8 +223,12 @@ final class RsaOaepPrivateKey { BigInt publicExponent, Hash hash, ) async { - final pair = await webCryptImpl.rsaOaepPrivateKey.generateKey(modulusLength, publicExponent, hash); - return pair as KeyPair; + final (privateKeyImpl, publicKeyImpl) = await webCryptImpl.rsaOaepPrivateKey.generateKey(modulusLength, publicExponent, hash); + + final privateKey = RsaOaepPrivateKey(privateKeyImpl); + final publicKey = RsaOaepPublicKey(publicKeyImpl); + + return createKeyPair(privateKey, publicKey); } /// Decrypt [data] encrypted with [RsaOaepPublicKey.encryptBytes] from the @@ -348,6 +356,10 @@ final class RsaOaepPublicKey { RsaOaepPublicKey._(this._impl); // keep the constructor private. + factory RsaOaepPublicKey(RsaOaepPublicKeyImpl impl) { + return RsaOaepPublicKey._(impl); + } + /// Import RSAES-OAEP public key in SPKI format. /// /// Creates an [RsaOaepPublicKey] from [keyData] given as the DER From a9c33f331f1b94431cd827b3a8ce0dc0c67e31c1 Mon Sep 17 00:00:00 2001 From: HamdaanAliQuatil Date: Sat, 19 Oct 2024 05:13:55 +0530 Subject: [PATCH 3/3] chore: format dart code --- lib/src/impl_ffi/impl_ffi.rsaoaep.dart | 6 ++++-- .../impl_interface/impl_interface.rsaoaep.dart | 9 ++++++--- lib/src/impl_js/impl_js.rsaoaep.dart | 16 +++++++++++----- lib/src/impl_stub/impl_stub.rsaoaep.dart | 9 ++++++--- lib/src/webcrypto/webcrypto.rsaoaep.dart | 17 +++++++++++------ 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/lib/src/impl_ffi/impl_ffi.rsaoaep.dart b/lib/src/impl_ffi/impl_ffi.rsaoaep.dart index 178df0e8..eeadbc89 100644 --- a/lib/src/impl_ffi/impl_ffi.rsaoaep.dart +++ b/lib/src/impl_ffi/impl_ffi.rsaoaep.dart @@ -167,7 +167,8 @@ Future _rsaOaepeEncryptOrDecryptBytes( }); } -final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl { +final class _StaticRsaOaepPrivateKeyImpl + implements StaticRsaOaepPrivateKeyImpl { const _StaticRsaOaepPrivateKeyImpl(); @override @@ -188,7 +189,8 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl Hash hash, ) async { final KeyPair keyPair = - await rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash); + await rsaOaepPrivateKey_generateKey( + modulusLength, publicExponent, hash); return (keyPair.privateKey, keyPair.publicKey); } diff --git a/lib/src/impl_interface/impl_interface.rsaoaep.dart b/lib/src/impl_interface/impl_interface.rsaoaep.dart index c7ec953c..dda69049 100644 --- a/lib/src/impl_interface/impl_interface.rsaoaep.dart +++ b/lib/src/impl_interface/impl_interface.rsaoaep.dart @@ -16,8 +16,10 @@ part of 'impl_interface.dart'; abstract interface class StaticRsaOaepPrivateKeyImpl { Future importPkcs8Key(List keyData, Hash hash); - Future importJsonWebKey(Map jwk, Hash hash); - Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash); + Future importJsonWebKey( + Map jwk, Hash hash); + Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey( + int modulusLength, BigInt publicExponent, Hash hash); } abstract interface class RsaOaepPrivateKeyImpl { @@ -28,7 +30,8 @@ abstract interface class RsaOaepPrivateKeyImpl { abstract interface class StaticRsaOaepPublicKeyImpl { Future importSpkiKey(List keyData, Hash hash); - Future importJsonWebKey(Map jwk, Hash hash); + Future importJsonWebKey( + Map jwk, Hash hash); } abstract interface class RsaOaepPublicKeyImpl { diff --git a/lib/src/impl_js/impl_js.rsaoaep.dart b/lib/src/impl_js/impl_js.rsaoaep.dart index 3bddc64a..1c719741 100644 --- a/lib/src/impl_js/impl_js.rsaoaep.dart +++ b/lib/src/impl_js/impl_js.rsaoaep.dart @@ -101,7 +101,8 @@ Future rsaOaepPublicKey_importJsonWebKey( )); } -final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl { +final class _StaticRsaOaepPrivateKeyImpl + implements StaticRsaOaepPrivateKeyImpl { const _StaticRsaOaepPrivateKeyImpl(); @override @@ -110,13 +111,17 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl } @override - Future importJsonWebKey(Map jwk, Hash hash) { + Future importJsonWebKey( + Map jwk, Hash hash) { return rsaOaepPrivateKey_importJsonWebKey(jwk, hash); } @override - Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash) async { - final KeyPair keyPair = await rsaOaepPrivateKey_generateKey(modulusLength, publicExponent, hash); + Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey( + int modulusLength, BigInt publicExponent, Hash hash) async { + final KeyPair keyPair = + await rsaOaepPrivateKey_generateKey( + modulusLength, publicExponent, hash); return (keyPair.privateKey, keyPair.publicKey); } @@ -165,7 +170,8 @@ final class _StaticRsaOaepPublicKeyImpl implements StaticRsaOaepPublicKeyImpl { } @override - Future importJsonWebKey(Map jwk, Hash hash) { + Future importJsonWebKey( + Map jwk, Hash hash) { return rsaOaepPublicKey_importJsonWebKey(jwk, hash); } } diff --git a/lib/src/impl_stub/impl_stub.rsaoaep.dart b/lib/src/impl_stub/impl_stub.rsaoaep.dart index 516a9907..b18be026 100644 --- a/lib/src/impl_stub/impl_stub.rsaoaep.dart +++ b/lib/src/impl_stub/impl_stub.rsaoaep.dart @@ -14,7 +14,8 @@ part of 'impl_stub.dart'; -final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl { +final class _StaticRsaOaepPrivateKeyImpl + implements StaticRsaOaepPrivateKeyImpl { const _StaticRsaOaepPrivateKeyImpl(); @override @@ -23,12 +24,14 @@ final class _StaticRsaOaepPrivateKeyImpl implements StaticRsaOaepPrivateKeyImpl } @override - Future importJsonWebKey(Map jwk, Hash hash) { + Future importJsonWebKey( + Map jwk, Hash hash) { throw UnimplementedError('Not implemented'); } @override - Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(int modulusLength, BigInt publicExponent, Hash hash) { + Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey( + int modulusLength, BigInt publicExponent, Hash hash) { throw UnimplementedError('Not implemented'); } } diff --git a/lib/src/webcrypto/webcrypto.rsaoaep.dart b/lib/src/webcrypto/webcrypto.rsaoaep.dart index e5acf0af..61d15bef 100644 --- a/lib/src/webcrypto/webcrypto.rsaoaep.dart +++ b/lib/src/webcrypto/webcrypto.rsaoaep.dart @@ -118,7 +118,8 @@ final class RsaOaepPrivateKey { List keyData, Hash hash, ) async { - final impl = await webCryptImpl.rsaOaepPrivateKey.importPkcs8Key(keyData, hash); + final impl = + await webCryptImpl.rsaOaepPrivateKey.importPkcs8Key(keyData, hash); return RsaOaepPrivateKey._(impl); } @@ -166,7 +167,8 @@ final class RsaOaepPrivateKey { Map jwk, Hash hash, ) async { - final impl = await webCryptImpl.rsaOaepPrivateKey.importJsonWebKey(jwk, hash); + final impl = + await webCryptImpl.rsaOaepPrivateKey.importJsonWebKey(jwk, hash); return RsaOaepPrivateKey._(impl); } @@ -223,8 +225,9 @@ final class RsaOaepPrivateKey { BigInt publicExponent, Hash hash, ) async { - final (privateKeyImpl, publicKeyImpl) = await webCryptImpl.rsaOaepPrivateKey.generateKey(modulusLength, publicExponent, hash); - + final (privateKeyImpl, publicKeyImpl) = await webCryptImpl.rsaOaepPrivateKey + .generateKey(modulusLength, publicExponent, hash); + final privateKey = RsaOaepPrivateKey(privateKeyImpl); final publicKey = RsaOaepPublicKey(publicKeyImpl); @@ -397,7 +400,8 @@ final class RsaOaepPublicKey { List keyData, Hash hash, ) async { - final impl = await webCryptImpl.rsaOaepPublicKey.importSpkiKey(keyData, hash); + final impl = + await webCryptImpl.rsaOaepPublicKey.importSpkiKey(keyData, hash); return RsaOaepPublicKey._(impl); } @@ -439,7 +443,8 @@ final class RsaOaepPublicKey { Map jwk, Hash hash, ) async { - final impl = await webCryptImpl.rsaOaepPublicKey.importJsonWebKey(jwk, hash); + final impl = + await webCryptImpl.rsaOaepPublicKey.importJsonWebKey(jwk, hash); return RsaOaepPublicKey._(impl); }