Skip to content

Commit b9ad66e

Browse files
Refactor RsaPssPrivateKey and RsaPssPublicKey Class (#169)
* refactor: update rsapss-private-key rsapss-public-key * style: format dart code
1 parent b0d09f6 commit b9ad66e

File tree

10 files changed

+307
-89
lines changed

10 files changed

+307
-89
lines changed

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {
9898

9999
@override
100100
final hkdfSecretKey = const _StaticHkdfSecretKeyImpl();
101+
102+
@override
103+
final rsaPssPrivateKey = const _StaticRsaPssPrivateKeyImpl();
104+
105+
@override
106+
final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl();
101107
}

lib/src/impl_ffi/impl_ffi.rsapss.dart

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ String _rsaPssJwkAlgFromHash(_Hash hash) {
3333
throw UnsupportedError('hash is not supported');
3434
}
3535

36-
Future<RsaPssPrivateKey> rsaPssPrivateKey_importPkcs8Key(
36+
Future<RsaPssPrivateKeyImpl> rsaPssPrivateKey_importPkcs8Key(
3737
List<int> keyData,
3838
Hash hash,
3939
) async {
4040
// Validate and get hash function
4141
final h = _Hash.fromHash(hash);
42-
return _RsaPssPrivateKey(_importPkcs8RsaPrivateKey(keyData), h);
42+
return _RsaPssPrivateKeyImpl(_importPkcs8RsaPrivateKey(keyData), h);
4343
}
4444

45-
Future<RsaPssPrivateKey> rsaPssPrivateKey_importJsonWebKey(
45+
Future<RsaPssPrivateKeyImpl> rsaPssPrivateKey_importJsonWebKey(
4646
Map<String, dynamic> jwk,
4747
Hash hash,
4848
) async {
4949
// Validate and get hash function
5050
final h = _Hash.fromHash(hash);
51-
return _RsaPssPrivateKey(
51+
return _RsaPssPrivateKeyImpl(
5252
_importJwkRsaPrivateOrPublicKey(
5353
JsonWebKey.fromJson(jwk),
5454
isPrivateKey: true,
@@ -59,7 +59,8 @@ Future<RsaPssPrivateKey> rsaPssPrivateKey_importJsonWebKey(
5959
);
6060
}
6161

62-
Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
62+
Future<KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl>>
63+
rsaPssPrivateKey_generateKey(
6364
int modulusLength,
6465
BigInt publicExponent,
6566
Hash hash,
@@ -68,27 +69,27 @@ Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
6869
final h = _Hash.fromHash(hash);
6970
final keys = _generateRsaKeyPair(modulusLength, publicExponent);
7071
return createKeyPair(
71-
_RsaPssPrivateKey(keys.privateKey, h),
72-
_RsaPssPublicKey(keys.publicKey, h),
72+
_RsaPssPrivateKeyImpl(keys.privateKey, h),
73+
_RsaPssPublicKeyImpl(keys.publicKey, h),
7374
);
7475
}
7576

76-
Future<RsaPssPublicKey> rsaPssPublicKey_importSpkiKey(
77+
Future<RsaPssPublicKeyImpl> rsaPssPublicKey_importSpkiKey(
7778
List<int> keyData,
7879
Hash hash,
7980
) async {
8081
// Validate and get hash function
8182
final h = _Hash.fromHash(hash);
82-
return _RsaPssPublicKey(_importSpkiRsaPublicKey(keyData), h);
83+
return _RsaPssPublicKeyImpl(_importSpkiRsaPublicKey(keyData), h);
8384
}
8485

85-
Future<RsaPssPublicKey> rsaPssPublicKey_importJsonWebKey(
86+
Future<RsaPssPublicKeyImpl> rsaPssPublicKey_importJsonWebKey(
8687
Map<String, dynamic> jwk,
8788
Hash hash,
8889
) async {
8990
// Validate and get hash function
9091
final h = _Hash.fromHash(hash);
91-
return _RsaPssPublicKey(
92+
return _RsaPssPublicKeyImpl(
9293
_importJwkRsaPrivateOrPublicKey(
9394
JsonWebKey.fromJson(jwk),
9495
isPrivateKey: false,
@@ -99,11 +100,43 @@ Future<RsaPssPublicKey> rsaPssPublicKey_importJsonWebKey(
99100
);
100101
}
101102

102-
class _RsaPssPrivateKey implements RsaPssPrivateKey {
103+
final class _StaticRsaPssPrivateKeyImpl implements StaticRsaPssPrivateKeyImpl {
104+
const _StaticRsaPssPrivateKeyImpl();
105+
106+
@override
107+
Future<RsaPssPrivateKeyImpl> importPkcs8Key(
108+
List<int> keyData,
109+
Hash hash,
110+
) async {
111+
return await rsaPssPrivateKey_importPkcs8Key(keyData, hash);
112+
}
113+
114+
@override
115+
Future<RsaPssPrivateKeyImpl> importJsonWebKey(
116+
Map<String, dynamic> jwk,
117+
Hash hash,
118+
) async {
119+
return await rsaPssPrivateKey_importJsonWebKey(jwk, hash);
120+
}
121+
122+
@override
123+
Future<(RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl)> generateKey(
124+
int modulusLength,
125+
BigInt publicExponent,
126+
Hash hash,
127+
) async {
128+
final KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl> keyPair =
129+
await rsaPssPrivateKey_generateKey(modulusLength, publicExponent, hash);
130+
131+
return (keyPair.privateKey, keyPair.publicKey);
132+
}
133+
}
134+
135+
final class _RsaPssPrivateKeyImpl implements RsaPssPrivateKeyImpl {
103136
final _EvpPKey _key;
104137
final _Hash _hash;
105138

106-
_RsaPssPrivateKey(this._key, this._hash);
139+
_RsaPssPrivateKeyImpl(this._key, this._hash);
107140

108141
@override
109142
String toString() {
@@ -148,11 +181,31 @@ class _RsaPssPrivateKey implements RsaPssPrivateKey {
148181
Future<Uint8List> exportPkcs8Key() async => _exportPkcs8Key(_key);
149182
}
150183

151-
class _RsaPssPublicKey implements RsaPssPublicKey {
184+
final class _StaticRsaPssPublicKeyImpl implements StaticRsaPssPublicKeyImpl {
185+
const _StaticRsaPssPublicKeyImpl();
186+
187+
@override
188+
Future<RsaPssPublicKeyImpl> importSpkiKey(
189+
List<int> keyData,
190+
Hash hash,
191+
) async {
192+
return await rsaPssPublicKey_importSpkiKey(keyData, hash);
193+
}
194+
195+
@override
196+
Future<RsaPssPublicKeyImpl> importJsonWebKey(
197+
Map<String, dynamic> jwk,
198+
Hash hash,
199+
) async {
200+
return await rsaPssPublicKey_importJsonWebKey(jwk, hash);
201+
}
202+
}
203+
204+
final class _RsaPssPublicKeyImpl implements RsaPssPublicKeyImpl {
152205
final _EvpPKey _key;
153206
final _Hash _hash;
154207

155-
_RsaPssPublicKey(this._key, this._hash);
208+
_RsaPssPublicKeyImpl(this._key, this._hash);
156209

157210
@override
158211
String toString() {

lib/src/impl_interface/impl_interface.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ part 'impl_interface.ecdh.dart';
2828
part 'impl_interface.ecdsa.dart';
2929
part 'impl_interface.rsaoaep.dart';
3030
part 'impl_interface.hkdf.dart';
31+
part 'impl_interface.rsapss.dart';
3132

3233
/// A key-pair as returned from key generation.
3334
class KeyPair<S, T> {
@@ -90,4 +91,6 @@ abstract interface class WebCryptoImpl {
9091
StaticRsaOaepPrivateKeyImpl get rsaOaepPrivateKey;
9192
StaticRsaOaepPublicKeyImpl get rsaOaepPublicKey;
9293
StaticHkdfSecretKeyImpl get hkdfSecretKey;
94+
StaticRsaPssPrivateKeyImpl get rsaPssPrivateKey;
95+
StaticRsaPssPublicKeyImpl get rsaPssPublicKey;
9396
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
part of 'impl_interface.dart';
16+
17+
abstract interface class StaticRsaPssPrivateKeyImpl {
18+
Future<RsaPssPrivateKeyImpl> importPkcs8Key(List<int> keyData, Hash hash);
19+
Future<RsaPssPrivateKeyImpl> importJsonWebKey(
20+
Map<String, dynamic> jwk, Hash hash);
21+
Future<(RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl)> generateKey(
22+
int modulusLength, BigInt publicExponent, Hash hash);
23+
}
24+
25+
abstract interface class RsaPssPrivateKeyImpl {
26+
Future<Uint8List> signBytes(List<int> data, int saltLength);
27+
Future<Uint8List> signStream(Stream<List<int>> data, int saltLength);
28+
Future<Uint8List> exportPkcs8Key();
29+
Future<Map<String, dynamic>> exportJsonWebKey();
30+
}
31+
32+
abstract interface class StaticRsaPssPublicKeyImpl {
33+
Future<RsaPssPublicKeyImpl> importSpkiKey(List<int> keyData, Hash hash);
34+
Future<RsaPssPublicKeyImpl> importJsonWebKey(
35+
Map<String, dynamic> jwk, Hash hash);
36+
}
37+
38+
abstract interface class RsaPssPublicKeyImpl {
39+
Future<bool> verifyBytes(List<int> signature, List<int> data, int saltLength);
40+
Future<bool> verifyStream(
41+
List<int> signature, Stream<List<int>> data, int saltLength);
42+
Future<Uint8List> exportSpkiKey();
43+
Future<Map<String, dynamic>> exportJsonWebKey();
44+
}

lib/src/impl_js/impl_js.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ final class _WebCryptoImpl implements WebCryptoImpl {
8585

8686
@override
8787
final hkdfSecretKey = const _StaticHkdfSecretKeyImpl();
88+
89+
@override
90+
final rsaPssPrivateKey = const _StaticRsaPssPrivateKeyImpl();
91+
92+
@override
93+
final rsaPssPublicKey = const _StaticRsaPssPublicKeyImpl();
8894
}

lib/src/impl_js/impl_js.rsapss.dart

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ part of 'impl_js.dart';
1818

1919
const _rsaPssAlgorithmName = 'RSA-PSS';
2020

21-
Future<RsaPssPrivateKey> rsaPssPrivateKey_importPkcs8Key(
21+
Future<RsaPssPrivateKeyImpl> rsaPssPrivateKey_importPkcs8Key(
2222
List<int> keyData,
2323
Hash hash,
2424
) async {
25-
return _RsaPssPrivateKey(await _importKey(
25+
return _RsaPssPrivateKeyImpl(await _importKey(
2626
'pkcs8',
2727
keyData,
2828
subtle.Algorithm(name: _rsaPssAlgorithmName, hash: _getHashAlgorithm(hash)),
@@ -31,19 +31,20 @@ Future<RsaPssPrivateKey> rsaPssPrivateKey_importPkcs8Key(
3131
));
3232
}
3333

34-
Future<RsaPssPrivateKey> rsaPssPrivateKey_importJsonWebKey(
34+
Future<RsaPssPrivateKeyImpl> rsaPssPrivateKey_importJsonWebKey(
3535
Map<String, dynamic> jwk,
3636
Hash hash,
3737
) async {
38-
return _RsaPssPrivateKey(await _importJsonWebKey(
38+
return _RsaPssPrivateKeyImpl(await _importJsonWebKey(
3939
jwk,
4040
subtle.Algorithm(name: _rsaPssAlgorithmName, hash: _getHashAlgorithm(hash)),
4141
_usagesSign,
4242
'private',
4343
));
4444
}
4545

46-
Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
46+
Future<KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl>>
47+
rsaPssPrivateKey_generateKey(
4748
int modulusLength,
4849
BigInt publicExponent,
4950
Hash hash,
@@ -58,16 +59,16 @@ Future<KeyPair<RsaPssPrivateKey, RsaPssPublicKey>> rsaPssPrivateKey_generateKey(
5859
_usagesSignVerify,
5960
);
6061
return createKeyPair(
61-
_RsaPssPrivateKey(pair.privateKey),
62-
_RsaPssPublicKey(pair.publicKey),
62+
_RsaPssPrivateKeyImpl(pair.privateKey),
63+
_RsaPssPublicKeyImpl(pair.publicKey),
6364
);
6465
}
6566

66-
Future<RsaPssPublicKey> rsaPssPublicKey_importSpkiKey(
67+
Future<RsaPssPublicKeyImpl> rsaPssPublicKey_importSpkiKey(
6768
List<int> keyData,
6869
Hash hash,
6970
) async {
70-
return _RsaPssPublicKey(await _importKey(
71+
return _RsaPssPublicKeyImpl(await _importKey(
7172
'spki',
7273
keyData,
7374
subtle.Algorithm(name: _rsaPssAlgorithmName, hash: _getHashAlgorithm(hash)),
@@ -76,21 +77,53 @@ Future<RsaPssPublicKey> rsaPssPublicKey_importSpkiKey(
7677
));
7778
}
7879

79-
Future<RsaPssPublicKey> rsaPssPublicKey_importJsonWebKey(
80+
Future<RsaPssPublicKeyImpl> rsaPssPublicKey_importJsonWebKey(
8081
Map<String, dynamic> jwk,
8182
Hash hash,
8283
) async {
83-
return _RsaPssPublicKey(await _importJsonWebKey(
84+
return _RsaPssPublicKeyImpl(await _importJsonWebKey(
8485
jwk,
8586
subtle.Algorithm(name: _rsaPssAlgorithmName, hash: _getHashAlgorithm(hash)),
8687
_usagesVerify,
8788
'public',
8889
));
8990
}
9091

91-
class _RsaPssPrivateKey implements RsaPssPrivateKey {
92+
final class _StaticRsaPssPrivateKeyImpl implements StaticRsaPssPrivateKeyImpl {
93+
const _StaticRsaPssPrivateKeyImpl();
94+
95+
@override
96+
Future<RsaPssPrivateKeyImpl> importPkcs8Key(
97+
List<int> keyData,
98+
Hash hash,
99+
) async {
100+
return await rsaPssPrivateKey_importPkcs8Key(keyData, hash);
101+
}
102+
103+
@override
104+
Future<RsaPssPrivateKeyImpl> importJsonWebKey(
105+
Map<String, dynamic> jwk,
106+
Hash hash,
107+
) async {
108+
return await rsaPssPrivateKey_importJsonWebKey(jwk, hash);
109+
}
110+
111+
@override
112+
Future<(RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl)> generateKey(
113+
int modulusLength,
114+
BigInt publicExponent,
115+
Hash hash,
116+
) async {
117+
final KeyPair<RsaPssPrivateKeyImpl, RsaPssPublicKeyImpl> keyPair =
118+
await rsaPssPrivateKey_generateKey(modulusLength, publicExponent, hash);
119+
120+
return (keyPair.privateKey, keyPair.publicKey);
121+
}
122+
}
123+
124+
final class _RsaPssPrivateKeyImpl implements RsaPssPrivateKeyImpl {
92125
final subtle.JSCryptoKey _key;
93-
_RsaPssPrivateKey(this._key);
126+
_RsaPssPrivateKeyImpl(this._key);
94127

95128
@override
96129
String toString() {
@@ -130,9 +163,29 @@ class _RsaPssPrivateKey implements RsaPssPrivateKey {
130163
}
131164
}
132165

133-
class _RsaPssPublicKey implements RsaPssPublicKey {
166+
final class _StaticRsaPssPublicKeyImpl implements StaticRsaPssPublicKeyImpl {
167+
const _StaticRsaPssPublicKeyImpl();
168+
169+
@override
170+
Future<RsaPssPublicKeyImpl> importSpkiKey(
171+
List<int> keyData,
172+
Hash hash,
173+
) async {
174+
return await rsaPssPublicKey_importSpkiKey(keyData, hash);
175+
}
176+
177+
@override
178+
Future<RsaPssPublicKeyImpl> importJsonWebKey(
179+
Map<String, dynamic> jwk,
180+
Hash hash,
181+
) async {
182+
return await rsaPssPublicKey_importJsonWebKey(jwk, hash);
183+
}
184+
}
185+
186+
final class _RsaPssPublicKeyImpl implements RsaPssPublicKeyImpl {
134187
final subtle.JSCryptoKey _key;
135-
_RsaPssPublicKey(this._key);
188+
_RsaPssPublicKeyImpl(this._key);
136189

137190
@override
138191
String toString() {

0 commit comments

Comments
 (0)