crypto: fix public PKCS8 export error · nodejs/node@191ebfb · GitHub
Skip to content

Commit 191ebfb

Browse files
koreahghgaduh95
authored andcommitted
crypto: fix public PKCS8 export error
exportKeySpki() already rejects exporting a private key as 'spki' with an InvalidAccessError, per the Web Crypto export key algorithm steps for each of RSA, EC, CFRG, ML-DSA, and ML-KEM. exportKeyPkcs8() was missing the symmetric check: exporting a public key as 'pkcs8' fell through to the generic "Unable to export ... key using pkcs8 format" NotSupportedError instead of the spec-mandated InvalidAccessError. Add the same key-type check to exportKeyPkcs8(), mirroring exportKeySpki(), and drop the now-redundant type guard around its call site in exportKeySync(). This also fixes wrapKey(), which delegates to the same export path. Add test coverage for both subtle.exportKey('pkcs8', publicKey) and subtle.wrapKey('pkcs8', publicKey, ...). Signed-off-by: koreahghg <koreahghg@gmail.com> PR-URL: #65609 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 10b1a0f commit 191ebfb

3 files changed

Lines changed: 34 additions & 13 deletions

File tree

‎lib/internal/crypto/webcrypto.js‎

Lines changed: 17 additions & 13 deletions

‎test/parallel/test-webcrypto-export-import-ec.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ async function testImportSpki({ name, publicUsages }, namedCurve, extractable) {
120120
assert.strictEqual(
121121
Buffer.from(spki).toString('hex'),
122122
keyData[namedCurve].spki.toString('hex'));
123+
124+
await assert.rejects(
125+
subtle.exportKey('pkcs8', key), {
126+
message: 'Key must be a private key',
127+
name: 'InvalidAccessError',
128+
});
123129
} else {
124130
await assert.rejects(
125131
subtle.exportKey('spki', key), {

‎test/parallel/test-webcrypto-wrap-unwrap.js‎

Lines changed: 11 additions & 0 deletions

0 commit comments

Comments
 (0)