Skip to content

Refactor RsaOaepPrivateKey and RsaOaepPublicKey Class #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/src/impl_ffi/impl_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final ecdhPublicKey = const _StaticEcdhPublicKeyImpl();

@override
final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl();

@override
final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl();
}
77 changes: 62 additions & 15 deletions lib/src/impl_ffi/impl_ffi.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ String _rsaOaepJwkAlgFromHash(_Hash hash) {
throw UnsupportedError('hash is not supported');
}

Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importPkcs8Key(
Future<RsaOaepPrivateKeyImpl> rsaOaepPrivateKey_importPkcs8Key(
List<int> 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> rsaOaepPrivateKey_importJsonWebKey(
Future<RsaOaepPrivateKeyImpl> rsaOaepPrivateKey_importJsonWebKey(
Map<String, dynamic> 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,
Expand All @@ -59,7 +59,7 @@ Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importJsonWebKey(
);
}

Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>>
rsaOaepPrivateKey_generateKey(
int modulusLength,
BigInt publicExponent,
Expand All @@ -69,27 +69,27 @@ Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
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),
);
}

Future<RsaOaepPublicKey> rsaOaepPublicKey_importSpkiKey(
Future<RsaOaepPublicKeyImpl> rsaOaepPublicKey_importSpkiKey(
List<int> 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> rsaOaepPublicKey_importJsonWebKey(
Future<RsaOaepPublicKeyImpl> rsaOaepPublicKey_importJsonWebKey(
Map<String, dynamic> 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,
Expand Down Expand Up @@ -167,11 +167,40 @@ Future<Uint8List> _rsaOaepeEncryptOrDecryptBytes(
});
}

class _RsaOaepPrivateKey implements RsaOaepPrivateKey {
final class _StaticRsaOaepPrivateKeyImpl
implements StaticRsaOaepPrivateKeyImpl {
const _StaticRsaOaepPrivateKeyImpl();

@override
Future<RsaOaepPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash) =>
rsaOaepPrivateKey_importPkcs8Key(keyData, hash);

@override
Future<RsaOaepPrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) =>
rsaOaepPrivateKey_importJsonWebKey(jwk, hash);

@override
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(
int modulusLength,
BigInt publicExponent,
Hash hash,
) async {
final KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl> keyPair =
await rsaOaepPrivateKey_generateKey(
modulusLength, publicExponent, hash);

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

final class _RsaOaepPrivateKeyImpl implements RsaOaepPrivateKeyImpl {
final _EvpPKey _key;
final _Hash _hash;

_RsaOaepPrivateKey(this._key, this._hash);
_RsaOaepPrivateKeyImpl(this._key, this._hash);

@override
String toString() {
Expand Down Expand Up @@ -203,11 +232,29 @@ class _RsaOaepPrivateKey implements RsaOaepPrivateKey {
Future<Uint8List> exportPkcs8Key() async => _exportPkcs8Key(_key);
}

class _RsaOaepPublicKey implements RsaOaepPublicKey {
final class _StaticRsaOaepPublicKeyImpl implements StaticRsaOaepPublicKeyImpl {
const _StaticRsaOaepPublicKeyImpl();

@override
Future<RsaOaepPublicKeyImpl> importSpkiKey(
List<int> keyData,
Hash hash,
) =>
rsaOaepPublicKey_importSpkiKey(keyData, hash);

@override
Future<RsaOaepPublicKeyImpl> importJsonWebKey(
Map<String, dynamic> 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() {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/impl_interface/impl_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<S, T> {
Expand Down Expand Up @@ -82,4 +83,6 @@ abstract interface class WebCryptoImpl {
StaticPbkdf2SecretKeyImpl get pbkdf2SecretKey;
StaticEcdhPrivateKeyImpl get ecdhPrivateKey;
StaticEcdhPublicKeyImpl get ecdhPublicKey;
StaticRsaOaepPrivateKeyImpl get rsaOaepPrivateKey;
StaticRsaOaepPublicKeyImpl get rsaOaepPublicKey;
}
41 changes: 41 additions & 0 deletions lib/src/impl_interface/impl_interface.rsaoaep.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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<RsaOaepPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash);
Future<RsaOaepPrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, Hash hash);
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(
int modulusLength, BigInt publicExponent, Hash hash);
}

abstract interface class RsaOaepPrivateKeyImpl {
Future<Uint8List> decryptBytes(List<int> data, {List<int>? label});
Future<Uint8List> exportPkcs8Key();
Future<Map<String, dynamic>> exportJsonWebKey();
}

abstract interface class StaticRsaOaepPublicKeyImpl {
Future<RsaOaepPublicKeyImpl> importSpkiKey(List<int> keyData, Hash hash);
Future<RsaOaepPublicKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, Hash hash);
}

abstract interface class RsaOaepPublicKeyImpl {
Future<Uint8List> encryptBytes(List<int> data, {List<int>? label});
Future<Uint8List> exportSpkiKey();
Future<Map<String, dynamic>> exportJsonWebKey();
}
6 changes: 6 additions & 0 deletions lib/src/impl_js/impl_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final ecdhPublicKey = const _StaticEcdhPublicKeyImpl();

@override
final rsaOaepPrivateKey = const _StaticRsaOaepPrivateKeyImpl();

@override
final rsaOaepPublicKey = const _StaticRsaOaepPublicKeyImpl();
}
75 changes: 58 additions & 17 deletions lib/src/impl_js/impl_js.rsaoaep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ part of 'impl_js.dart';

const _rsaOaepAlgorithmName = 'RSA-OAEP';

Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importPkcs8Key(
Future<RsaOaepPrivateKeyImpl> rsaOaepPrivateKey_importPkcs8Key(
List<int> keyData,
Hash hash,
) async {
return _RsaOaepPrivateKey(await _importKey(
return _RsaOaepPrivateKeyImpl(await _importKey(
'pkcs8',
keyData,
subtle.Algorithm(
Expand All @@ -34,11 +34,11 @@ Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importPkcs8Key(
));
}

Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importJsonWebKey(
Future<RsaOaepPrivateKeyImpl> rsaOaepPrivateKey_importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return _RsaOaepPrivateKey(await _importJsonWebKey(
return _RsaOaepPrivateKeyImpl(await _importJsonWebKey(
jwk,
subtle.Algorithm(
name: _rsaOaepAlgorithmName,
Expand All @@ -49,7 +49,7 @@ Future<RsaOaepPrivateKey> rsaOaepPrivateKey_importJsonWebKey(
));
}

Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
Future<KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl>>
rsaOaepPrivateKey_generateKey(
int modulusLength,
BigInt publicExponent,
Expand All @@ -65,16 +65,16 @@ Future<KeyPair<RsaOaepPrivateKey, RsaOaepPublicKey>>
_usagesEncryptDecrypt,
);
return createKeyPair(
_RsaOaepPrivateKey(pair.privateKey),
_RsaOaepPublicKey(pair.publicKey),
_RsaOaepPrivateKeyImpl(pair.privateKey),
_RsaOaepPublicKeyImpl(pair.publicKey),
);
}

Future<RsaOaepPublicKey> rsaOaepPublicKey_importSpkiKey(
Future<RsaOaepPublicKeyImpl> rsaOaepPublicKey_importSpkiKey(
List<int> keyData,
Hash hash,
) async {
return _RsaOaepPublicKey(await _importKey(
return _RsaOaepPublicKeyImpl(await _importKey(
'spki',
keyData,
subtle.Algorithm(
Expand All @@ -86,11 +86,11 @@ Future<RsaOaepPublicKey> rsaOaepPublicKey_importSpkiKey(
));
}

Future<RsaOaepPublicKey> rsaOaepPublicKey_importJsonWebKey(
Future<RsaOaepPublicKeyImpl> rsaOaepPublicKey_importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return _RsaOaepPublicKey(await _importJsonWebKey(
return _RsaOaepPublicKeyImpl(await _importJsonWebKey(
jwk,
subtle.Algorithm(
name: _rsaOaepAlgorithmName,
Expand All @@ -101,13 +101,39 @@ Future<RsaOaepPublicKey> rsaOaepPublicKey_importJsonWebKey(
));
}

class _RsaOaepPrivateKey implements RsaOaepPrivateKey {
final class _StaticRsaOaepPrivateKeyImpl
implements StaticRsaOaepPrivateKeyImpl {
const _StaticRsaOaepPrivateKeyImpl();

@override
Future<RsaOaepPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash) {
return rsaOaepPrivateKey_importPkcs8Key(keyData, hash);
}

@override
Future<RsaOaepPrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, Hash hash) {
return rsaOaepPrivateKey_importJsonWebKey(jwk, hash);
}

@override
Future<(RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl)> generateKey(
int modulusLength, BigInt publicExponent, Hash hash) async {
final KeyPair<RsaOaepPrivateKeyImpl, RsaOaepPublicKeyImpl> keyPair =
await rsaOaepPrivateKey_generateKey(
modulusLength, publicExponent, hash);

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

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
Expand Down Expand Up @@ -135,13 +161,28 @@ class _RsaOaepPrivateKey implements RsaOaepPrivateKey {
}
}

class _RsaOaepPublicKey implements RsaOaepPublicKey {
final class _StaticRsaOaepPublicKeyImpl implements StaticRsaOaepPublicKeyImpl {
const _StaticRsaOaepPublicKeyImpl();

@override
Future<RsaOaepPublicKeyImpl> importSpkiKey(List<int> keyData, Hash hash) {
return rsaOaepPublicKey_importSpkiKey(keyData, hash);
}

@override
Future<RsaOaepPublicKeyImpl> importJsonWebKey(
Map<String, dynamic> 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
Expand Down
Loading
Loading