-
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 all 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,105 @@ | ||
| // 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:convert'; | ||
| import 'dart:typed_data'; | ||
| import 'package:webcrypto/webcrypto.dart'; | ||
| import '../utils/utils.dart'; | ||
|
|
||
| void main() => tests().runTests(); | ||
|
|
||
| const _isWeb = bool.fromEnvironment('dart.library.js_util'); | ||
|
|
||
| final _ecPkcs8 = ''' | ||
| MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYLo96dEstESlXmgZW | ||
| deQoLYOSpUzxzaTFzdc/KtJzoGhRANCAATdpa7vSPqEDzyhDB3JIXi8NL5pByNzLM | ||
| xq9CirGP9PzaxiW58t+I0KzUNUwy0McBdzuYAKBFPhGgjzvYJxIExE | ||
| '''.replaceAll(RegExp(r'\s+'), ''); | ||
| const _ecSpki = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE3aWu70j6hA88oQwdySF4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() test})> tests() { | ||
| final tests = <({String name, Future<void> Function() test})>[]; | ||
| void test(String name, Future<void> Function() fn) => | ||
| tests.add((name: name, test: fn)); | ||
|
|
||
| test('Ecdsa: importPkcs8Key rejects trailing bytes', () async { | ||
| final key = base64Decode(_ecPkcs8); | ||
| final badKey = Uint8List.fromList([...key, 0]); | ||
|
|
||
| // Verify valid key imports normally. | ||
| await EcdsaPrivateKey.importPkcs8Key(key, EllipticCurve.p256); | ||
|
|
||
| // Verify key with trailing bytes is rejected. | ||
| bool threw = false; | ||
| try { | ||
| await EcdsaPrivateKey.importPkcs8Key(badKey, EllipticCurve.p256); | ||
| } on FormatException { | ||
| threw = true; | ||
| } | ||
| if (!_isWeb) { | ||
| check(threw, 'Should throw FormatException for trailing bytes'); | ||
|
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. What happens that is different on web?
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. On web, |
||
| } | ||
| }); | ||
|
|
||
| test('Ecdsa: importSpkiKey rejects trailing bytes', () async { | ||
| final key = base64Decode(_ecSpki); | ||
| final badKey = Uint8List.fromList([...key, 0]); | ||
|
|
||
| await EcdsaPublicKey.importSpkiKey(key, EllipticCurve.p256); | ||
|
|
||
| bool threw = false; | ||
| try { | ||
| await EcdsaPublicKey.importSpkiKey(badKey, EllipticCurve.p256); | ||
| } on FormatException { | ||
| threw = true; | ||
| } | ||
| check(threw, 'Should throw FormatException for trailing bytes'); | ||
| }); | ||
|
|
||
| test('RsaPss: importPkcs8Key rejects trailing bytes', () async { | ||
| final key = base64Decode(_rsaPkcs8); | ||
| final badKey = Uint8List.fromList([...key, 0]); | ||
|
|
||
| await RsaPssPrivateKey.importPkcs8Key(key, Hash.sha256); | ||
|
|
||
| bool threw = false; | ||
| try { | ||
| await RsaPssPrivateKey.importPkcs8Key(badKey, Hash.sha256); | ||
| } on FormatException { | ||
| threw = true; | ||
| } | ||
| if (!_isWeb) { | ||
| check(threw, 'Should throw FormatException for trailing bytes'); | ||
| } | ||
| }); | ||
|
|
||
| test('RsaPss: importSpkiKey rejects trailing bytes', () async { | ||
| final key = base64Decode(_rsaSpki); | ||
| final badKey = Uint8List.fromList([...key, 0]); | ||
|
|
||
| await RsaPssPublicKey.importSpkiKey(key, Hash.sha256); | ||
|
|
||
| bool threw = false; | ||
| try { | ||
| await RsaPssPublicKey.importSpkiKey(badKey, Hash.sha256); | ||
| } on FormatException { | ||
| threw = true; | ||
| } | ||
| check(threw, 'Should throw FormatException for trailing bytes'); | ||
| }); | ||
|
|
||
| return tests; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.