Skip to content

Commit

Permalink
fix: add getter for hashimpl
Browse files Browse the repository at this point in the history
  • Loading branch information
HamdaanAliQuatil committed Oct 25, 2024
2 parents 5ce6512 + 25db3a0 commit d7fa231
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 110 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 @@ -105,6 +105,12 @@ final class _WebCryptoImpl implements WebCryptoImpl {
@override
final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl();

@override
final rsaSsaPkcs1v15PrivateKey = const _StaticRsaSsaPkcs1V15PrivateKeyImpl();

@override
final rsaSsaPkcs1v15PublicKey = const _StaticRsaSsaPkcs1V15PublicKeyImpl();

@override
final sha1 = const _Sha1();

Expand Down
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 @@ -16,22 +16,22 @@

part of 'impl_ffi.dart';

Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importPkcs8Key(
Future<RsaSsaPkcs1V15PrivateKeyImpl> rsassaPkcs1V15PrivateKey_importPkcs8Key(
List<int> keyData,
HashImpl hash,
) async {
// Get hash first, to avoid a leak of EVP_PKEY if _HashImpl.fromHash throws
final h = _HashImpl.fromHash(hash);
return _RsassaPkcs1V15PrivateKey(_importPkcs8RsaPrivateKey(keyData), h);
return _RsaSsaPkcs1V15PrivateKeyImpl(_importPkcs8RsaPrivateKey(keyData), h);
}

Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importJsonWebKey(
Future<RsaSsaPkcs1V15PrivateKeyImpl> rsassaPkcs1V15PrivateKey_importJsonWebKey(
Map<String, dynamic> jwk,
HashImpl hash,
) async {
// Get hash first, to avoid a leak of EVP_PKEY if _HashImpl.fromHash throws
final h = _HashImpl.fromHash(hash);
return _RsassaPkcs1V15PrivateKey(
return _RsaSsaPkcs1V15PrivateKeyImpl(
_importJwkRsaPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
isPrivateKey: true,
Expand All @@ -42,7 +42,7 @@ Future<RsassaPkcs1V15PrivateKey> rsassaPkcs1V15PrivateKey_importJsonWebKey(
);
}

Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
Future<KeyPair<RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl>>
rsassaPkcs1V15PrivateKey_generateKey(
int modulusLength,
BigInt publicExponent,
Expand All @@ -52,27 +52,27 @@ Future<KeyPair<RsassaPkcs1V15PrivateKey, RsassaPkcs1V15PublicKey>>
final h = _HashImpl.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,
HashImpl hash,
) async {
// Get hash first, to avoid a leak of EVP_PKEY if _HashImpl.fromHash throws
final h = _HashImpl.fromHash(hash);
return _RsassaPkcs1V15PublicKey(_importSpkiRsaPublicKey(keyData), h);
return _RsaSsaPkcs1V15PublicKeyImpl(_importSpkiRsaPublicKey(keyData), h);
}

Future<RsassaPkcs1V15PublicKey> rsassaPkcs1V15PublicKey_importJsonWebKey(
Future<RsaSsaPkcs1V15PublicKeyImpl> rsassaPkcs1V15PublicKey_importJsonWebKey(
Map<String, dynamic> jwk,
HashImpl hash,
) async {
// Get hash first, to avoid a leak of EVP_PKEY if _HashImpl.fromHash throws
final h = _HashImpl.fromHash(hash);
return _RsassaPkcs1V15PublicKey(
return _RsaSsaPkcs1V15PublicKeyImpl(
_importJwkRsaPrivateOrPublicKey(
JsonWebKey.fromJson(jwk),
isPrivateKey: false,
Expand All @@ -83,11 +83,45 @@ Future<RsassaPkcs1V15PublicKey> rsassaPkcs1V15PublicKey_importJsonWebKey(
);
}

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

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

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

@override
Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)>
generateKey(
int modulusLength,
BigInt publicExponent,
HashImpl 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 _HashImpl _hash;

_RsassaPkcs1V15PrivateKey(this._key, this._hash);
_RsaSsaPkcs1V15PrivateKeyImpl(this._key, this._hash);

@override
String toString() {
Expand Down Expand Up @@ -116,11 +150,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, HashImpl hash) =>
rsassaPkcs1V15PublicKey_importSpkiKey(keyData, hash);

@override
Future<RsaSsaPkcs1V15PublicKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, HashImpl hash) =>
rsassaPkcs1V15PublicKey_importJsonWebKey(jwk, hash);
}

final class _RsaSsaPkcs1V15PublicKeyImpl
implements RsaSsaPkcs1V15PublicKeyImpl {
final _EvpPKey _key;
final _HashImpl _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 @@ -27,6 +27,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';
part 'impl_interface.digest.dart';

/// A key-pair as returned from key generation.
Expand Down Expand Up @@ -92,6 +93,8 @@ abstract interface class WebCryptoImpl {
StaticHkdfSecretKeyImpl get hkdfSecretKey;
StaticRsaPssPrivateKeyImpl get rsaPssPrivateKey;
StaticRsaPssPublicKeyImpl get rsaPssPublicKey;
StaticRsaSsaPkcs1v15PrivateKeyImpl get rsaSsaPkcs1v15PrivateKey;
StaticRsaSsaPkcs1v15PublicKeyImpl get rsaSsaPkcs1v15PublicKey;
HashImpl get sha1;
HashImpl get sha256;
HashImpl get sha384;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/impl_interface/impl_interface.digest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
part of 'impl_interface.dart';

abstract interface class HashImpl {
HashImpl(HashImpl impl);

Future<Uint8List> digestBytes(List<int> data);
Future<Uint8List> digestStream(Stream<List<int>> data);
String hmacJwkAlg(HashImpl hash);
Expand Down
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, HashImpl hash);
Future<RsaSsaPkcs1V15PrivateKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, HashImpl hash);
Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)>
generateKey(int modulusLength, BigInt publicExponent, HashImpl 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, HashImpl hash);
Future<RsaSsaPkcs1V15PublicKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, HashImpl 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 @@ -92,6 +92,12 @@ final class _WebCryptoImpl implements WebCryptoImpl {
@override
final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl();

@override
final rsaSsaPkcs1v15PrivateKey = const _StaticRsaSsaPkcs1V15PrivateKeyImpl();

@override
final rsaSsaPkcs1v15PublicKey = const _StaticRsaSsaPkcs1V15PublicKeyImpl();

@override
final sha1 = const _HashImpl('SHA-1');

Expand Down
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,
HashImpl 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,
HashImpl 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,
HashImpl 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,
HashImpl 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, HashImpl hash) async {
return await rsassaPkcs1V15PrivateKey_importPkcs8Key(keyData, hash);
}

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

@override
Future<(RsaSsaPkcs1V15PrivateKeyImpl, RsaSsaPkcs1V15PublicKeyImpl)>
generateKey(int modulusLength, BigInt publicExponent, HashImpl 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, HashImpl hash) async {
return await rsassaPkcs1V15PublicKey_importSpkiKey(keyData, hash);
}

@override
Future<RsaSsaPkcs1V15PublicKeyImpl> importJsonWebKey(
Map<String, dynamic> jwk, HashImpl 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 d7fa231

Please sign in to comment.