-
Notifications
You must be signed in to change notification settings - Fork 91
Reject imported EC and RSA keys with trailing bytes #251
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
9566c6f
0a2fe1d
b402d66
4b903e1
b25d912
2a3136d
f9c752d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright 2026 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. | ||
|
|
||
| import 'dart:typed_data'; | ||
| import 'package:webcrypto/webcrypto.dart'; | ||
| import '../utils/utils.dart'; | ||
|
|
||
| void main() => tests().runTests(); | ||
|
|
||
| /// Tests for issue #60, exported for use in `../testing.dart`. | ||
| List<({String name, Future<void> Function() test})> tests() { | ||
| final tests = <({String name, Future<void> Function() test})>[]; | ||
| void test(String name, Future<void> Function() testFn) => | ||
| tests.add((name: name, test: testFn)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add something like this to List<({String name, Future<void> Function() test})> defineTests(
void Function(void Function(String name, Future<void> Function() testFn) test) tests
) {
final tests_ = <({String name, Future<void> Function() test})>[];
void test(String name, Future<void> Function() testFn) =>
tests_.add((name: name, test: testFn));
tests(test);
return tests_;
}Then this could just be: void main() => tests.runTests();
final tests = defineTests((test) {
test('...', () async {
...
});
...
});But we don't have to do that in this PR. I'm happy to land this as is.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| test('Ecdsa: importPkcs8Key with trailing bytes throws FormatException', () async { | ||
| final kp = await EcdsaPrivateKey.generateKey(EllipticCurve.p256); | ||
| final validKey = await kp.privateKey.exportPkcs8Key(); | ||
| final invalidKey = Uint8List.fromList([...validKey, 0]); | ||
|
|
||
| bool threw = false; | ||
| try { | ||
| await EcdsaPrivateKey.importPkcs8Key(invalidKey, EllipticCurve.p256); | ||
| } on FormatException { | ||
| threw = true; | ||
| } | ||
| check(threw, 'Expected FormatException when importing EC private key with trailing bytes'); | ||
| }); | ||
|
|
||
| test('Ecdsa: importSpkiKey with trailing bytes throws FormatException', () async { | ||
| final kp = await EcdsaPrivateKey.generateKey(EllipticCurve.p256); | ||
| final validKey = await kp.publicKey.exportSpkiKey(); | ||
| final invalidKey = Uint8List.fromList([...validKey, 0]); | ||
|
|
||
| bool threw = false; | ||
| try { | ||
| await EcdsaPublicKey.importSpkiKey(invalidKey, EllipticCurve.p256); | ||
| } on FormatException { | ||
| threw = true; | ||
| } | ||
| check(threw, 'Expected FormatException when importing EC public key with trailing bytes'); | ||
| }); | ||
|
|
||
| test('RsaPss: importPkcs8Key with trailing bytes throws FormatException', () async { | ||
| final kp = await RsaPssPrivateKey.generateKey(2048, BigInt.from(65537), Hash.sha256); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also hardcode these keys, to make tests faster and more consistent. Maybe it might also be worth testing that before we append zero, we can't import, and when we've appended zero we can't import. This way we're sure it's not because we have hardcoded an invalid :D
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| final validKey = await kp.privateKey.exportPkcs8Key(); | ||
| final invalidKey = Uint8List.fromList([...validKey, 0]); | ||
|
|
||
| bool threw = false; | ||
| try { | ||
| await RsaPssPrivateKey.importPkcs8Key(invalidKey, Hash.sha256); | ||
| } on FormatException { | ||
| threw = true; | ||
| } | ||
| check(threw, 'Expected FormatException when importing RSA private key with trailing bytes'); | ||
| }); | ||
|
|
||
| test('RsaPss: importSpkiKey with trailing bytes throws FormatException', () async { | ||
| final kp = await RsaPssPrivateKey.generateKey(2048, BigInt.from(65537), Hash.sha256); | ||
| final validKey = await kp.publicKey.exportSpkiKey(); | ||
| final invalidKey = Uint8List.fromList([...validKey, 0]); | ||
|
|
||
| bool threw = false; | ||
| try { | ||
| await RsaPssPublicKey.importSpkiKey(invalidKey, Hash.sha256); | ||
| } on FormatException { | ||
| threw = true; | ||
| } | ||
| check(threw, 'Expected FormatException when importing RSA public key with trailing bytes'); | ||
| }); | ||
|
|
||
| return tests; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.