Skip to content

Commit

Permalink
Refactor RsaSsaPkcsPrivateKey and RsaSsaPkcsPublicKey Class (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
HamdaanAliQuatil authored Oct 25, 2024
1 parent b9ad66e commit 25db3a0
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 87 deletions.
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 @@ -104,4 +104,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {

@override
final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl();

@override
final rsaSsaPkcs1v15PrivateKey = const _StaticRsaSsaPkcs1V15PrivateKeyImpl();

@override
final rsaSsaPkcs1v15PublicKey = const _StaticRsaSsaPkcs1V15PublicKeyImpl();
}
80 changes: 65 additions & 15 deletions lib/src/impl_ffi/impl_ffi.rsassapkcs1v15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ String _rsassaPkcs1V15JwkAlgFromHash(_Hash hash) {
throw UnsupportedError('hash is not supported');
}

Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importPkcs8Key(
Future<RsaSsaPkcs1V15PrivateKeyImpl> rsassaPkcs1V15PrivateKey_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 _RsassaPkcs1V15PrivateKey(_importPkcs8RsaPrivateKey(keyData), h);
return _RsaSsaPkcs1V15PrivateKeyImpl(_importPkcs8RsaPrivateKey(keyData), h);
}

Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importJsonWebKey(
Future<RsaSsaPkcs1V15PrivateKeyImpl> rsassaPkcs1V15PrivateKey_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 _RsassaPkcs1V15PrivateKey(
return _RsaSsaPkcs1V15PrivateKeyImpl(
_importJwkRsaPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
isPrivateKey: true,
Expand All @@ -59,7 +59,7 @@ Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importJsonWebKey(
);
}

Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
Future<KeyPair<RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl>>
rsassaPkcs1V15PrivateKey_generateKey(
int modulusLength,
BigInt publicExponent,
Expand All @@ -69,27 +69,27 @@ Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
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> rsassaPkcs1V15PublicKey_importSpkiKey(
Future<RsaSsaPkcs1V15PublicKeyImpl> rsassaPkcs1V15PublicKey_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 _RsassaPkcs1V15PublicKey(_importSpkiRsaPublicKey(keyData), h);
return _RsaSsaPkcs1V15PublicKeyImpl(_importSpkiRsaPublicKey(keyData), h);
}

Future<RsassaPkcs1V15PublicKey> rsassaPkcs1V15PublicKey_importJsonWebKey(
Future<RsaSsaPkcs1V15PublicKeyImpl> rsassaPkcs1V15PublicKey_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 _RsassaPkcs1V15PublicKey(
return _RsaSsaPkcs1V15PublicKeyImpl(
_importJwkRsaPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
isPrivateKey: false,
Expand All @@ -100,11 +100,45 @@ Future<RsassaPkcs1V15PublicKey> rsassaPkcs1V15PublicKey_importJsonWebKey(
);
}

class _RsassaPkcs1V15PrivateKey implements RsassaPkcs1V15PrivateKey {
final class _StaticRsaSsaPkcs1V15PrivateKeyImpl
implements StaticRsaSsaPkcs1v15PrivateKeyImpl {
const _StaticRsaSsaPkcs1V15PrivateKeyImpl();

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

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

@override
Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)>
generateKey(
int modulusLength,
BigInt publicExponent,
Hash hash,
) async {
final KeyPair<RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl>
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() {
Expand Down Expand Up @@ -133,11 +167,27 @@ class _RsassaPkcs1V15PrivateKey implements RsassaPkcs1V15PrivateKey {
Future<Uint8List> exportPkcs8Key() async => _exportPkcs8Key(_key);
}

class _RsassaPkcs1V15PublicKey implements RsassaPkcs1V15PublicKey {
final class _StaticRsaSsaPkcs1V15PublicKeyImpl
implements StaticRsaSsaPkcs1v15PublicKeyImpl {
const _StaticRsaSsaPkcs1V15PublicKeyImpl();

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

@override
Future<RsaSsaPkcs1V15PublicKeyImpl> importJsonWebKey(
Map<String, dynamic> 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<bool> verifyBytes(List<int> signature, List<int> data) =>
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 @@ -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<S, T> {
Expand Down Expand Up @@ -93,4 +94,6 @@ abstract interface class WebCryptoImpl {
StaticHkdfSecretKeyImpl get hkdfSecretKey;
StaticRsaPssPrivateKeyImpl get rsaPssPrivateKey;
StaticRsaPssPublicKeyImpl get rsaPssPublicKey;
StaticRsaSsaPkcs1v15PrivateKeyImpl get rsaSsaPkcs1v15PrivateKey;
StaticRsaSsaPkcs1v15PublicKeyImpl get rsaSsaPkcs1v15PublicKey;
}
45 changes: 45 additions & 0 deletions lib/src/impl_interface/impl_interface.rsassapkcs1v15.dart
Original file line number Diff line number Diff line change
@@ -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<RsaSsaPkcs1V15PrivateKeyImpl> importPkcs8Key(
List<int> keyData, Hash hash);
Future<RsaSsaPkcs1V15PrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, Hash hash);
Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)>
generateKey(int modulusLength, BigInt publicExponent, Hash hash);
}

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

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

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

@override
final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl();

@override
final rsaSsaPkcs1v15PrivateKey = const _StaticRsaSsaPkcs1V15PrivateKeyImpl();

@override
final rsaSsaPkcs1v15PublicKey = const _StaticRsaSsaPkcs1V15PublicKeyImpl();
}
78 changes: 62 additions & 16 deletions lib/src/impl_js/impl_js.rsassapkcs1v15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ part of 'impl_js.dart';

const _rsassaPkcs1V15Algorithm = subtle.Algorithm(name: 'RSASSA-PKCS1-v1_5');

Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importPkcs8Key(
Future<RsaSsaPkcs1V15PrivateKeyImpl> rsassaPkcs1V15PrivateKey_importPkcs8Key(
List<int> keyData,
Hash hash,
) async {
return _RsassaPkcs1V15PrivateKey(await _importKey(
return _RsaSsaPkcs1V15PrivateKeyImpl(await _importKey(
'pkcs8',
keyData,
_rsassaPkcs1V15Algorithm.update(hash: _getHashAlgorithm(hash)),
Expand All @@ -31,19 +31,19 @@ Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importPkcs8Key(
));
}

Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importJsonWebKey(
Future<RsaSsaPkcs1V15PrivateKeyImpl> rsassaPkcs1V15PrivateKey_importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return _RsassaPkcs1V15PrivateKey(await _importJsonWebKey(
return _RsaSsaPkcs1V15PrivateKeyImpl(await _importJsonWebKey(
jwk,
_rsassaPkcs1V15Algorithm.update(hash: _getHashAlgorithm(hash)),
_usagesSign,
'private',
));
}

Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
Future<KeyPair<RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl>>
rsassaPkcs1V15PrivateKey_generateKey(
int modulusLength,
BigInt publicExponent,
Expand All @@ -58,16 +58,16 @@ Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
_usagesSignVerify,
);
return createKeyPair(
_RsassaPkcs1V15PrivateKey(pair.privateKey),
_RsassaPkcs1V15PublicKey(pair.publicKey),
_RsaSsaPkcs1V15PrivateKeyImpl(pair.privateKey),
_RsaSsaPkcs1V15PublicKeyImpl(pair.publicKey),
);
}

Future<RsassaPkcs1V15PublicKey> rsassaPkcs1V15PublicKey_importSpkiKey(
Future<RsaSsaPkcs1V15PublicKeyImpl> rsassaPkcs1V15PublicKey_importSpkiKey(
List<int> keyData,
Hash hash,
) async {
return _RsassaPkcs1V15PublicKey(await _importKey(
return _RsaSsaPkcs1V15PublicKeyImpl(await _importKey(
'spki',
keyData,
_rsassaPkcs1V15Algorithm.update(hash: _getHashAlgorithm(hash)),
Expand All @@ -76,21 +76,49 @@ Future<RsassaPkcs1V15PublicKey> rsassaPkcs1V15PublicKey_importSpkiKey(
));
}

Future<RsassaPkcs1V15PublicKey> rsassaPkcs1V15PublicKey_importJsonWebKey(
Future<RsaSsaPkcs1V15PublicKeyImpl> rsassaPkcs1V15PublicKey_importJsonWebKey(
Map<String, dynamic> jwk,
Hash hash,
) async {
return _RsassaPkcs1V15PublicKey(await _importJsonWebKey(
return _RsaSsaPkcs1V15PublicKeyImpl(await _importJsonWebKey(
jwk,
_rsassaPkcs1V15Algorithm.update(hash: _getHashAlgorithm(hash)),
_usagesVerify,
'public',
));
}

class _RsassaPkcs1V15PrivateKey implements RsassaPkcs1V15PrivateKey {
final class _StaticRsaSsaPkcs1V15PrivateKeyImpl
implements StaticRsaSsaPkcs1v15PrivateKeyImpl {
const _StaticRsaSsaPkcs1V15PrivateKeyImpl();

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

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

@override
Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)>
generateKey(int modulusLength, BigInt publicExponent, Hash hash) async {
final KeyPair<RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl>
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<Uint8List> signBytes(List<int> data) async {
Expand All @@ -113,13 +141,31 @@ class _RsassaPkcs1V15PrivateKey implements RsassaPkcs1V15PrivateKey {
}
}

class _RsassaPkcs1V15PublicKey implements RsassaPkcs1V15PublicKey {
final class _StaticRsaSsaPkcs1V15PublicKeyImpl
implements StaticRsaSsaPkcs1v15PublicKeyImpl {
const _StaticRsaSsaPkcs1V15PublicKeyImpl();

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

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

0 comments on commit 25db3a0

Please sign in to comment.