-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Inconsistent behavior between Node and browser crypto with PKCS8 EC imports #50174
Comments
The specification1 does not specify the intended behaviour in this case. In Node.js we get the successful import behaviour "for free" from OpenSSL which re-calculates the public key for us. I suspect that browser crypto engines don't do this, which is why they chose to throw a DataError instead. To me they're both correct, albeit not interoperable, behaviours. Footnotes |
@panva I think that's right: they're both "correct" but not interoperable. Does Node's "crypto" try to be interoperable in general? For context: the lack of interop hurts when unit testing code meant to run both in browsers and in Node environments. I was surprised when errors happened during manual testing (in browser) while my unit tests were green (using Node) |
The very reason Node.js has WebCryptoAPI is interoperability and code portability across other Web-interoperable runtimes. When it comes to Web APIs, such as WebCryptoAPI, the benchmark for interop is WPTs (Web Platform Tests), you can see how Node.js fares in that regard (also in comparison to other runtimes participating in the WPT reporting) using the wpt.fyi dashboard 1 UI. I went ahead and tested your vector in different WebCryptoAPI implementations as well:
As I've noted above, there's no language present in the specification, or its underpinnings, demanding one behaviour or another, and there are no WPTs for the behaviour either. If something is left unspecified, there can't be any talks of interoperability, it's up to every implementer's best judgement / capabilities. Node.js is able to work with the key, other runtimes can't. FWIW there are even optional to implement features in the specification itself, such as supporting compressed public keys. In those cases the following language is present in the specification:
If you'll open an issue about this specification blind spot it is most likely going to end up being an optional to implement feature as well with a similar language, meaning all runtimes will be in compliance. Footnotes |
Interesting, thank you for running the vector in all implementations! I've opened an issue about this blind spot, we'll see what the feedback is: w3c/webcrypto#356 |
I'm keeping an eye on the w3c/webcrypto repo as well as WPTs for changes. If this turns out to go some place where changes are needed we'll make them. Until then I believe this can be considered resolved? |
Version
v20.8.0
Platform
MacOS
Subsystem
crypto
What steps will reproduce the bug?
In Node console:
In browsers (tried both Firefox and Chrome):
Additional information
The bytes I'm importing are, in hex format:
308141020100301306072a8648ce3d020106082a8648ce3d0301070427302502010104207632de7338577bc12c1731fa29f08019206af381f74af60f4d5e0395218f205c
You can use asn1js to decode this pkc8 structure: https://lapo.it/asn1js/#MIFBAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBCcwJQIBAQQgdjLeczhXe8EsFzH6KfCAGSBq84H3SvYPTV4DlSGPIFw
This is a P-256 private key with no public key bytes (they are optional in pkcs8)
I'm honestly not sure that browsers are doing the right thing here but at the very least this behavior ("error when no public key bytes are present") seems intentional: Chrome has an explicit test case for it:
This specific private key (from the test case) also succeeds in NodeJS:
The text was updated successfully, but these errors were encountered: