SubtleCrypto.exportKey fails for some imported EC keys
Categories
(Core :: DOM: Web Crypto, defect, P2)
Tracking
()
People
(Reporter: panva.ip, Assigned: anna.weine)
Details
Attachments
(1 file, 1 obsolete file)
Steps to reproduce:
Now that EC pkcs8 without a public key can be imported (Fixed in https://bugzilla.mozilla.org/show_bug.cgi?id=1915792) the resulting CryptoKey fails to be re-exported as jwk.
This works in Chrome, Node, Deno, and other server runtimes but fails in Firefox.
Safari still fails to even import the key.
This is causing hurdles with PQ/T Hybrid HPKE KEM key derivation (seed expansion), requiring users to implement scalar multiplication which honestly should not be left to users to do.
const jwk = {
kty: 'EC',
x: 'KJtyCr3jOZjnIHmJf7Mj2W010EkgBCHbWKP_QoFgK04',
y: 'gZRbjpFSPK4H_IIq1Oxs--LS6PjEODMXz-cGIdjsaGI',
crv: 'P-256',
d: '7sOs7L87ue9x5ZsP3f8ScKUFf76mZdkXzNO-Hv1FWI4'
}
const template = Uint8Array.of(0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20)
const d = Uint8Array.fromBase64(jwk.d, { alphabet: 'base64url' })
const pkcs8 = new Uint8Array(template.byteLength + d.byteLength)
pkcs8.set(template)
pkcs8.set(d, template.byteLength)
crypto.subtle.importKey('pkcs8', pkcs8, { name: 'ECDH', namedCurve: 'P-256' }, true, ['deriveBits']).then((key) => {
console.log(key)
crypto.subtle.exportKey('jwk', key).then(console.log, () => console.log('failed to jwk export'))
crypto.subtle.exportKey('pkcs8', key).then(console.log, () => console.log('failed to pkcs8 export'))
}, () => console.log('failed to import'))
Actual results:
prints an imported CryptoKey
prints a re-exported pkcs8 (same as input, no public key included)
fails to export jwk
Expected results:
prints an imported CryptoKey
prints a re-exported pkcs8
prints a re-exported jwk with recalculated x and y coordinates
Comment 1•4 months ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::DOM: Web Crypto' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.
| Assignee | ||
Comment 2•4 months ago
|
||
Oh.
Thanks for the report, I will take a look, probably the next week.
| Assignee | ||
Updated•4 months ago
|
| Assignee | ||
Updated•4 months ago
|
| Assignee | ||
Comment 3•4 months ago
|
||
| Reporter | ||
Comment 4•4 months ago
|
||
WPT Proposal: https://github.com/web-platform-tests/wpt/pull/56086
| Assignee | ||
Comment 5•4 months ago
|
||
Updated•4 months ago
|
Updated•4 months ago
|
| Assignee | ||
Comment 6•4 months ago
|
||
It looks like my patch solves the problem. Filip, whenever you have time, could you check it?
Thanks!
| Reporter | ||
Comment 7•4 months ago
|
||
Don't know where I'd even start checking :) feel free to proceed at your own best judgement please
| Assignee | ||
Updated•4 months ago
|
Thank you Filip And Anna ! This bug is also found me because I wanted to derive a key from a seed. Here is my issue reproduction code snippet. It can be pasted into the Console in the developer tools.
https://picopublish.sequentialread.com/files/subtlecrypto-not-working.js
In Chrome this snippet will print the same jwk formatted key twice, in firefox it only prints the first time and then second time, it gives "The operation failed for an operation-specific reason"
Description
•