crypto: fix raw key export error for wrong key type

Exporting a key in 'raw', 'raw-public' or 'raw-seed' format when the
key type does not match (e.g. an ECDSA private key as 'raw', or an
ML-KEM public key as 'raw-seed') fell through to the generic
NotSupportedError. The Web Crypto and modern-algos export key steps
require an InvalidAccessError in these cases.

Mirror exportKeySpki() and exportKeyPkcs8(): select the exporter per
algorithm first, then check the key type, and drop the type guards
around the call sites in exportKeySync(). Formats an algorithm does
not support (e.g. 'raw' for ML-DSA) still throw NotSupportedError.
This also fixes wrapKey(), which uses the same export path.

Assisted-by: a closed-source coding agent
Signed-off-by: koreahghg <koreahghg@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66217
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
This commit is contained in:
한국 authored and Antoine du Hamel committed 2026-11-11 17:27:52 +01:00
1 parent d62f112d98
commit 9dc5baa18f
6 files changed
+84 -29

No files matched your search

+38 -29
View File
@@ -594,12 +594,14 @@ function exportKeyPkcs8(key) {
}
function exportKeyRawPublic(key, format) {
let exporter;
let exportFormat = kWebCryptoKeyFormatRaw;
switch (getCryptoKeyAlgorithm(key).name) {
case 'ECDSA':
// Fall through
case 'ECDH':
return require('internal/crypto/ec')
.ecExportKey(key, kWebCryptoKeyFormatRaw);
exporter = require('internal/crypto/ec').ecExportKey;
break;
case 'Ed25519':
// Fall through
case 'Ed448':
@@ -607,74 +609,85 @@ function exportKeyRawPublic(key, format) {
case 'X25519':
// Fall through
case 'X448':
return require('internal/crypto/cfrg')
.cfrgExportKey(key, kWebCryptoKeyFormatRaw);
exporter = require('internal/crypto/cfrg').cfrgExportKey;
break;
case 'ML-DSA-44':
// Fall through
case 'ML-DSA-65':
// Fall through
case 'ML-DSA-87': {
case 'ML-DSA-87':
// ML-DSA keys don't recognize "raw"
if (format !== 'raw-public') {
return undefined;
}
return require('internal/crypto/ml_dsa')
.mlDsaExportKey(key, kWebCryptoKeyFormatRaw);
}
exporter = require('internal/crypto/ml_dsa').mlDsaExportKey;
break;
case 'ML-KEM-512':
// Fall through
case 'ML-KEM-768':
// Fall through
case 'ML-KEM-1024': {
case 'ML-KEM-1024':
// ML-KEM keys don't recognize "raw"
if (format !== 'raw-public') {
return undefined;
}
return require('internal/crypto/ml_kem')
.mlKemExportKey(key, kWebCryptoKeyFormatRaw);
}
exporter = require('internal/crypto/ml_kem').mlKemExportKey;
break;
case 'MLKEM768-P256':
// Fall through
case 'MLKEM768-X25519':
// Fall through
case 'MLKEM1024-P384': {
case 'MLKEM1024-P384':
if (format !== 'raw-public') {
return undefined;
}
return require('internal/crypto/kem_hybrids')
.kemHybridExportKey(key, format);
}
exporter = require('internal/crypto/kem_hybrids').kemHybridExportKey;
exportFormat = format;
break;
default:
return undefined;
}
if (getCryptoKeyType(key) !== 'public')
throw lazyDOMException('Key must be a public key', 'InvalidAccessError');
return exporter(key, exportFormat);
}
function exportKeyRawSeed(key) {
let exporter;
let exportFormat = kWebCryptoKeyFormatRaw;
switch (getCryptoKeyAlgorithm(key).name) {
case 'ML-DSA-44':
// Fall through
case 'ML-DSA-65':
// Fall through
case 'ML-DSA-87':
return require('internal/crypto/ml_dsa')
.mlDsaExportKey(key, kWebCryptoKeyFormatRaw);
exporter = require('internal/crypto/ml_dsa').mlDsaExportKey;
break;
case 'ML-KEM-512':
// Fall through
case 'ML-KEM-768':
// Fall through
case 'ML-KEM-1024':
return require('internal/crypto/ml_kem')
.mlKemExportKey(key, kWebCryptoKeyFormatRaw);
exporter = require('internal/crypto/ml_kem').mlKemExportKey;
break;
case 'MLKEM768-P256':
// Fall through
case 'MLKEM768-X25519':
// Fall through
case 'MLKEM1024-P384':
return require('internal/crypto/kem_hybrids')
.kemHybridExportKey(key, 'raw-seed');
exporter = require('internal/crypto/kem_hybrids').kemHybridExportKey;
exportFormat = 'raw-seed';
break;
default:
return undefined;
}
if (getCryptoKeyType(key) !== 'private')
throw lazyDOMException('Key must be a private key', 'InvalidAccessError');
return exporter(key, exportFormat);
}
function exportKeyRawSecret(key, format) {
@@ -844,21 +857,17 @@ function exportKeySync(format, key) {
break;
}
case 'raw-public': {
if (type === 'public') {
result = exportKeyRawPublic(key, format);
}
result = exportKeyRawPublic(key, format);
break;
}
case 'raw-seed': {
if (type === 'private') {
result = exportKeyRawSeed(key);
}
result = exportKeyRawSeed(key);
break;
}
case 'raw': {
if (type === 'secret') {
result = exportKeyRawSecret(key, format);
} else if (type === 'public') {
} else {
result = exportKeyRawPublic(key, format);
}
break;
@@ -171,6 +171,14 @@ async function testImportPkcs8({ name, privateUsages }, extractable) {
assert.strictEqual(
Buffer.from(pkcs8).toString('hex'),
keyData[name].pkcs8.toString('hex'));
for (const format of ['raw', 'raw-public']) {
await assert.rejects(
subtle.exportKey(format, key), {
message: 'Key must be a public key',
name: 'InvalidAccessError',
});
}
} else {
await assert.rejects(
subtle.exportKey('pkcs8', key), {
@@ -174,6 +174,14 @@ async function testImportPkcs8(
message: 'Key must be a public key',
name: 'InvalidAccessError',
});
for (const format of ['raw', 'raw-public']) {
await assert.rejects(
subtle.exportKey(format, key), {
message: 'Key must be a public key',
name: 'InvalidAccessError',
});
}
} else {
await assert.rejects(
subtle.exportKey('pkcs8', key), {
@@ -411,6 +411,11 @@ async function testImportRawPublic({ name, publicUsages }, extractable) {
name: 'NotSupportedError',
message: `Unable to export ${publicKey.algorithm.name} public key using raw format`,
});
await assert.rejects(subtle.exportKey('raw-seed', publicKey), {
name: 'InvalidAccessError',
message: 'Key must be a private key',
});
}
await assert.rejects(
@@ -450,6 +455,11 @@ async function testImportRawSeed({ name, privateUsages }, extractable) {
if (extractable) {
const value = await subtle.exportKey('raw-seed', privateKey);
assert.deepStrictEqual(Buffer.from(value), seed);
await assert.rejects(subtle.exportKey('raw-public', privateKey), {
name: 'InvalidAccessError',
message: 'Key must be a public key',
});
}
await assert.rejects(
@@ -234,6 +234,11 @@ async function testImportRawPublic({ name, publicUsages }, extractable) {
name: 'NotSupportedError',
message: `Unable to export ${publicKey.algorithm.name} public key using raw format`,
});
await assert.rejects(subtle.exportKey('raw-seed', publicKey), {
name: 'InvalidAccessError',
message: 'Key must be a private key',
});
}
await assert.rejects(
@@ -273,6 +278,11 @@ async function testImportRawSeed({ name, privateUsages }, extractable) {
if (extractable) {
const value = await subtle.exportKey('raw-seed', privateKey);
assert.deepStrictEqual(Buffer.from(value), seed);
await assert.rejects(subtle.exportKey('raw-public', privateKey), {
name: 'InvalidAccessError',
message: 'Key must be a public key',
});
}
await assert.rejects(
@@ -578,6 +578,16 @@ async function testNonByteLengthWrapUnwrap({
name: 'InvalidAccessError',
});
// Exporting a private key as 'raw' must also fail with InvalidAccessError.
await assert.rejects(
subtle.wrapKey('raw', ecKey.privateKey, wrapKey, {
name: 'AES-GCM',
iv: new Uint8Array(12),
}), {
message: 'Key must be a public key',
name: 'InvalidAccessError',
});
// --- unwrapKey validation tests ---
const ciphertext = new Uint8Array(32); // Dummy ciphertext