Skip to content

Refactor RsaPssPrivateKey and RsaPssPublicKey Class #169

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 2 commits into from
Oct 24, 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 @@ -98,4 +98,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final hkdfSecretKey = const _StaticHkdfSecretKeyImpl();

@override
final rsaPssPrivateKey = const _StaticRsaPssPrivateKeyImpl();

@override
final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl();
}
83 changes: 68 additions & 15 deletions lib/src/impl_ffi/impl_ffi.rsapss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ String _rsaPssJwkAlgFromHash(_Hash hash) {
throw UnsupportedError('hash is not supported');
}

Future<RsaPssPrivateKey> rsaPssPrivateKey_importPkcs8Key(
Future<RsaPssPrivateKeyImpl> rsaPssPrivateKey_importPkcs8Key(
List<int> keyData,
Hash hash,
) async {
// Validate and get hash function
final h = _Hash.fromHash(hash);
return _RsaPssPrivateKey(_importPkcs8RsaPrivateKey(keyData), h);
return _RsaPssPrivateKeyImpl(_importPkcs8RsaPrivateKey(keyData), h);
}

Future<RsaPssPrivateKey> rsaPssPrivateKey_importJsonWebKey(
Future<RsaPssPrivateKeyImpl> rsaPssPrivateKey_importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
// Validate and get hash function
final h = _Hash.fromHash(hash);
return _RsaPssPrivateKey(
return _RsaPssPrivateKeyImpl(
_importJwkRsaPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
isPrivateKey: true,
Expand All @@ -59,7 +59,8 @@ Future<RsaPssPrivateKey> rsaPssPrivateKey_importJsonWebKey(
);
}

Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
Future<KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl>>
rsaPssPrivateKey_generateKey(
int modulusLength,
BigInt publicExponent,
Hash hash,
Expand All @@ -68,27 +69,27 @@ Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
final h = _Hash.fromHash(hash);
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
return createKeyPair(
_RsaPssPrivateKey(keys.privateKey, h),
_RsaPssPublicKey(keys.publicKey, h),
_RsaPssPrivateKeyImpl(keys.privateKey, h),
_RsaPssPublicKeyImpl(keys.publicKey, h),
);
}

Future<RsaPssPublicKey> rsaPssPublicKey_importSpkiKey(
Future<RsaPssPublicKeyImpl> rsaPssPublicKey_importSpkiKey(
List<int> keyData,
Hash hash,
) async {
// Validate and get hash function
final h = _Hash.fromHash(hash);
return _RsaPssPublicKey(_importSpkiRsaPublicKey(keyData), h);
return _RsaPssPublicKeyImpl(_importSpkiRsaPublicKey(keyData), h);
}

Future<RsaPssPublicKey> rsaPssPublicKey_importJsonWebKey(
Future<RsaPssPublicKeyImpl> rsaPssPublicKey_importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
// Validate and get hash function
final h = _Hash.fromHash(hash);
return _RsaPssPublicKey(
return _RsaPssPublicKeyImpl(
_importJwkRsaPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
isPrivateKey: false,
Expand All @@ -99,11 +100,43 @@ Future<RsaPssPublicKey> rsaPssPublicKey_importJsonWebKey(
);
}

class _RsaPssPrivateKey implements RsaPssPrivateKey {
final class _StaticRsaPssPrivateKeyImpl implements StaticRsaPssPrivateKeyImpl {
const _StaticRsaPssPrivateKeyImpl();

@override
Future<RsaPssPrivateKeyImpl> importPkcs8Key(
List<int> keyData,
Hash hash,
) async {
return await rsaPssPrivateKey_importPkcs8Key(keyData, hash);
}

@override
Future<RsaPssPrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return await rsaPssPrivateKey_importJsonWebKey(jwk, hash);
}

@override
Future<(RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl)> generateKey(
int modulusLength,
BigInt publicExponent,
Hash hash,
) async {
final KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl> keyPair =
await rsaPssPrivateKey_generateKey(modulusLength, publicExponent, hash);

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

final class _RsaPssPrivateKeyImpl implements RsaPssPrivateKeyImpl {
final _EvpPKey _key;
final _Hash _hash;

_RsaPssPrivateKey(this._key, this._hash);
_RsaPssPrivateKeyImpl(this._key, this._hash);

@override
String toString() {
Expand Down Expand Up @@ -148,11 +181,31 @@ class _RsaPssPrivateKey implements RsaPssPrivateKey {
Future<Uint8List> exportPkcs8Key() async => _exportPkcs8Key(_key);
}

class _RsaPssPublicKey implements RsaPssPublicKey {
final class _StaticRsaPssPublicKeyImpl implements StaticRsaPssPublicKeyImpl {
const _StaticRsaPssPublicKeyImpl();

@override
Future<RsaPssPublicKeyImpl> importSpkiKey(
List<int> keyData,
Hash hash,
) async {
return await rsaPssPublicKey_importSpkiKey(keyData, hash);
}

@override
Future<RsaPssPublicKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return await rsaPssPublicKey_importJsonWebKey(jwk, hash);
}
}

final class _RsaPssPublicKeyImpl implements RsaPssPublicKeyImpl {
final _EvpPKey _key;
final _Hash _hash;

_RsaPssPublicKey(this._key, this._hash);
_RsaPssPublicKeyImpl(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 @@ -28,6 +28,7 @@ part 'impl_interface.ecdh.dart';
part 'impl_interface.ecdsa.dart';
part 'impl_interface.rsaoaep.dart';
part 'impl_interface.hkdf.dart';
part 'impl_interface.rsapss.dart';

/// A key-pair as returned from key generation.
class KeyPair<S, T> {
Expand Down Expand Up @@ -90,4 +91,6 @@ abstract interface class WebCryptoImpl {
StaticRsaOaepPrivateKeyImpl get rsaOaepPrivateKey;
StaticRsaOaepPublicKeyImpl get rsaOaepPublicKey;
StaticHkdfSecretKeyImpl get hkdfSecretKey;
StaticRsaPssPrivateKeyImpl get rsaPssPrivateKey;
StaticRsaPssPublicKeyImpl get rsaPssPublicKey;
}
44 changes: 44 additions & 0 deletions lib/src/impl_interface/impl_interface.rsapss.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 StaticRsaPssPrivateKeyImpl {
Future<RsaPssPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash);
Future<RsaPssPrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, Hash hash);
Future<(RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl)> generateKey(
int modulusLength, BigInt publicExponent, Hash hash);
}

abstract interface class RsaPssPrivateKeyImpl {
Future<Uint8List> signBytes(List<int> data, int saltLength);
Future<Uint8List> signStream(Stream<List<int>> data, int saltLength);
Future<Uint8List> exportPkcs8Key();
Future<Map<String, dynamic>> exportJsonWebKey();
}

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

abstract interface class RsaPssPublicKeyImpl {
Future<bool> verifyBytes(List<int> signature, List<int> data, int saltLength);
Future<bool> verifyStream(
List<int> signature, Stream<List<int>> data, int saltLength);
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 @@ -85,4 +85,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final hkdfSecretKey = const _StaticHkdfSecretKeyImpl();

@override
final rsaPssPrivateKey = const _StaticRsaPssPrivateKeyImpl();

@override
final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl();
}
83 changes: 68 additions & 15 deletions lib/src/impl_js/impl_js.rsapss.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ part of 'impl_js.dart';

const _rsaPssAlgorithmName = 'RSA-PSS';

Future<RsaPssPrivateKey> rsaPssPrivateKey_importPkcs8Key(
Future<RsaPssPrivateKeyImpl> rsaPssPrivateKey_importPkcs8Key(
List<int> keyData,
Hash hash,
) async {
return _RsaPssPrivateKey(await _importKey(
return _RsaPssPrivateKeyImpl(await _importKey(
'pkcs8',
keyData,
subtle.Algorithm(name: _rsaPssAlgorithmName, hash: _getHashAlgorithm(hash)),
Expand All @@ -31,19 +31,20 @@ Future<RsaPssPrivateKey> rsaPssPrivateKey_importPkcs8Key(
));
}

Future<RsaPssPrivateKey> rsaPssPrivateKey_importJsonWebKey(
Future<RsaPssPrivateKeyImpl> rsaPssPrivateKey_importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return _RsaPssPrivateKey(await _importJsonWebKey(
return _RsaPssPrivateKeyImpl(await _importJsonWebKey(
jwk,
subtle.Algorithm(name: _rsaPssAlgorithmName, hash: _getHashAlgorithm(hash)),
_usagesSign,
'private',
));
}

Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
Future<KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl>>
rsaPssPrivateKey_generateKey(
int modulusLength,
BigInt publicExponent,
Hash hash,
Expand All @@ -58,16 +59,16 @@ Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
_usagesSignVerify,
);
return createKeyPair(
_RsaPssPrivateKey(pair.privateKey),
_RsaPssPublicKey(pair.publicKey),
_RsaPssPrivateKeyImpl(pair.privateKey),
_RsaPssPublicKeyImpl(pair.publicKey),
);
}

Future<RsaPssPublicKey> rsaPssPublicKey_importSpkiKey(
Future<RsaPssPublicKeyImpl> rsaPssPublicKey_importSpkiKey(
List<int> keyData,
Hash hash,
) async {
return _RsaPssPublicKey(await _importKey(
return _RsaPssPublicKeyImpl(await _importKey(
'spki',
keyData,
subtle.Algorithm(name: _rsaPssAlgorithmName, hash: _getHashAlgorithm(hash)),
Expand All @@ -76,21 +77,53 @@ Future<RsaPssPublicKey> rsaPssPublicKey_importSpkiKey(
));
}

Future<RsaPssPublicKey> rsaPssPublicKey_importJsonWebKey(
Future<RsaPssPublicKeyImpl> rsaPssPublicKey_importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return _RsaPssPublicKey(await _importJsonWebKey(
return _RsaPssPublicKeyImpl(await _importJsonWebKey(
jwk,
subtle.Algorithm(name: _rsaPssAlgorithmName, hash: _getHashAlgorithm(hash)),
_usagesVerify,
'public',
));
}

class _RsaPssPrivateKey implements RsaPssPrivateKey {
final class _StaticRsaPssPrivateKeyImpl implements StaticRsaPssPrivateKeyImpl {
const _StaticRsaPssPrivateKeyImpl();

@override
Future<RsaPssPrivateKeyImpl> importPkcs8Key(
List<int> keyData,
Hash hash,
) async {
return await rsaPssPrivateKey_importPkcs8Key(keyData, hash);
}

@override
Future<RsaPssPrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return await rsaPssPrivateKey_importJsonWebKey(jwk, hash);
}

@override
Future<(RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl)> generateKey(
int modulusLength,
BigInt publicExponent,
Hash hash,
) async {
final KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl> keyPair =
await rsaPssPrivateKey_generateKey(modulusLength, publicExponent, hash);

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

final class _RsaPssPrivateKeyImpl implements RsaPssPrivateKeyImpl {
final subtle.JSCryptoKey _key;
_RsaPssPrivateKey(this._key);
_RsaPssPrivateKeyImpl(this._key);

@override
String toString() {
Expand Down Expand Up @@ -130,9 +163,29 @@ class _RsaPssPrivateKey implements RsaPssPrivateKey {
}
}

class _RsaPssPublicKey implements RsaPssPublicKey {
final class _StaticRsaPssPublicKeyImpl implements StaticRsaPssPublicKeyImpl {
const _StaticRsaPssPublicKeyImpl();

@override
Future<RsaPssPublicKeyImpl> importSpkiKey(
List<int> keyData,
Hash hash,
) async {
return await rsaPssPublicKey_importSpkiKey(keyData, hash);
}

@override
Future<RsaPssPublicKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return await rsaPssPublicKey_importJsonWebKey(jwk, hash);
}
}

final class _RsaPssPublicKeyImpl implements RsaPssPublicKeyImpl {
final subtle.JSCryptoKey _key;
_RsaPssPublicKey(this._key);
_RsaPssPublicKeyImpl(this._key);

@override
String toString() {
Expand Down
Loading
Loading