lib: improve Web Cryptography key validation ordering by panva · Pull Request #62749 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions lib/internal/crypto/webcrypto.js
26 changes: 23 additions & 3 deletions test/parallel/test-webcrypto-encrypt-decrypt-aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function testEncryptNoEncrypt({ keyBuffer, algorithm, plaintext }) {
['decrypt']);

return assert.rejects(subtle.encrypt(algorithm, key, plaintext), {
message: /The requested operation is not valid for the provided key/
message: /Unable to use this key to encrypt/
});
}

Expand All @@ -65,7 +65,7 @@ async function testEncryptNoDecrypt({ keyBuffer, algorithm, plaintext }) {
const output = await subtle.encrypt(algorithm, key, plaintext);

return assert.rejects(subtle.decrypt(algorithm, key, output), {
message: /The requested operation is not valid for the provided key/
message: /Unable to use this key to decrypt/
});
}

Expand All @@ -80,7 +80,23 @@ async function testEncryptWrongAlg({ keyBuffer, algorithm, plaintext }, alg) {
['encrypt']);

return assert.rejects(subtle.encrypt(algorithm, key, plaintext), {
message: /The requested operation is not valid for the provided key/
message: /Key algorithm mismatch/
});
}

async function testDecryptWrongAlg({ keyBuffer, algorithm, result }, alg) {
if (result === undefined) return;
assert.notStrictEqual(algorithm.name, alg);
const keyFormat = alg === 'AES-OCB' ? 'raw-secret' : 'raw';
const key = await subtle.importKey(
keyFormat,
keyBuffer,
{ name: alg },
false,
['decrypt']);

return assert.rejects(subtle.decrypt(algorithm, key, result), {
message: /Key algorithm mismatch/
});
}

Expand Down Expand Up @@ -112,6 +128,7 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-CTR'));
variations.push(testDecryptWrongAlg(vector, 'AES-CTR'));
});

failing.forEach((vector) => {
Expand Down Expand Up @@ -149,6 +166,7 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-CBC'));
variations.push(testDecryptWrongAlg(vector, 'AES-CBC'));
});

// TODO(@jasnell): These fail for different reasons. Need to
Expand Down Expand Up @@ -188,6 +206,7 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-CBC'));
variations.push(testDecryptWrongAlg(vector, 'AES-CBC'));
});

failing.forEach((vector) => {
Expand Down Expand Up @@ -225,6 +244,7 @@ if (hasOpenSSL(3)) {
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-GCM'));
variations.push(testDecryptWrongAlg(vector, 'AES-GCM'));
});

failing.forEach((vector) => {
Expand Down
22 changes: 19 additions & 3 deletions test/parallel/test-webcrypto-encrypt-decrypt-chacha20-poly1305.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function testEncryptNoEncrypt({ keyBuffer, algorithm, plaintext }) {
['decrypt']);

return assert.rejects(subtle.encrypt(algorithm, key, plaintext), {
message: /The requested operation is not valid for the provided key/
message: /Unable to use this key to encrypt/
});
}

Expand All @@ -63,7 +63,7 @@ async function testEncryptNoDecrypt({ keyBuffer, algorithm, plaintext }) {
const output = await subtle.encrypt(algorithm, key, plaintext);

return assert.rejects(subtle.decrypt(algorithm, key, output), {
message: /The requested operation is not valid for the provided key/
message: /Unable to use this key to decrypt/
});
}

Expand All @@ -77,7 +77,22 @@ async function testEncryptWrongAlg({ keyBuffer, algorithm, plaintext }, alg) {
['encrypt']);

return assert.rejects(subtle.encrypt(algorithm, key, plaintext), {
message: /The requested operation is not valid for the provided key/
message: /Key algorithm mismatch/
});
}

async function testDecryptWrongAlg({ keyBuffer, algorithm, result }, alg) {
if (result === undefined) return;
assert.notStrictEqual(algorithm.name, alg);
const key = await subtle.importKey(
'raw-secret',
keyBuffer,
{ name: alg },
false,
['decrypt']);

return assert.rejects(subtle.decrypt(algorithm, key, result), {
message: /Key algorithm mismatch/
});
}

Expand Down Expand Up @@ -107,6 +122,7 @@ async function testDecrypt({ keyBuffer, algorithm, result }) {
variations.push(testEncryptNoEncrypt(vector));
variations.push(testEncryptNoDecrypt(vector));
variations.push(testEncryptWrongAlg(vector, 'AES-GCM'));
variations.push(testDecryptWrongAlg(vector, 'AES-GCM'));
});

failing.forEach((vector) => {
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-webcrypto-encrypt-decrypt-rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async function testEncryptionWrongKey({ algorithm,
['decrypt']);
return assert.rejects(
subtle.encrypt(algorithm, privateKey, plaintext), {
message: /The requested operation is not valid/
message: /Unable to use this key to encrypt/
});
}

Expand All @@ -167,7 +167,7 @@ async function testEncryptionBadUsage({ algorithm,
['decrypt']);
return assert.rejects(
subtle.encrypt(algorithm, publicKey, plaintext), {
message: /The requested operation is not valid/
message: /Unable to use this key to encrypt/
});
}

Expand All @@ -191,7 +191,7 @@ async function testDecryptionWrongKey({ ciphertext,

return assert.rejects(
subtle.decrypt(algorithm, publicKey, ciphertext), {
message: /The requested operation is not valid/
message: /Unable to use this key to decrypt/
});
}

Expand All @@ -215,7 +215,7 @@ async function testDecryptionBadUsage({ ciphertext,

return assert.rejects(
subtle.decrypt(algorithm, publicKey, ciphertext), {
message: /The requested operation is not valid/
message: /Unable to use this key to decrypt/
});
}

Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-webcrypto-encrypt-decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ const { subtle } = globalThis.crypto;
name: 'RSA-OAEP',
}, privateKey, buf), {
name: 'InvalidAccessError',
message: 'The requested operation is not valid for the provided key'
message: 'Unable to use this key to encrypt'
});

await assert.rejects(() => subtle.decrypt({
name: 'RSA-OAEP',
}, publicKey, ciphertext), {
name: 'InvalidAccessError',
message: 'The requested operation is not valid for the provided key'
message: 'Unable to use this key to decrypt'
});
}

Expand Down Expand Up @@ -88,14 +88,14 @@ if (!process.features.openssl_is_boringssl) {
name: 'RSA-OAEP',
}, privateKey, buf), {
name: 'InvalidAccessError',
message: 'The requested operation is not valid for the provided key'
message: 'Unable to use this key to encrypt'
});

await assert.rejects(() => subtle.decrypt({
name: 'RSA-OAEP',
}, publicKey, ciphertext), {
name: 'InvalidAccessError',
message: 'The requested operation is not valid for the provided key'
message: 'Unable to use this key to decrypt'
});
}

Expand Down
12 changes: 6 additions & 6 deletions test/parallel/test-webcrypto-sign-verify-ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ async function testVerify({ name,
// Test failure when using the wrong algorithms
await assert.rejects(
subtle.verify({ name, hash }, hmacKey, signature, plaintext), {
message: /Unable to use this key to verify/
message: /Key algorithm mismatch/
});

await assert.rejects(
subtle.verify({ name, hash }, rsaKeys.publicKey, signature, plaintext), {
message: /Unable to use this key to verify/
message: /Key algorithm mismatch/
});

await assert.rejects(
subtle.verify({ name, hash }, okpKeys.publicKey, signature, plaintext), {
message: /Unable to use this key to verify/
message: /Key algorithm mismatch/
});

// Test failure when signature is altered
Expand Down Expand Up @@ -210,17 +210,17 @@ async function testSign({ name,
// Test failure when using the wrong algorithms
await assert.rejects(
subtle.sign({ name, hash }, hmacKey, plaintext), {
message: /Unable to use this key to sign/
message: /Key algorithm mismatch/
});

await assert.rejects(
subtle.sign({ name, hash }, rsaKeys.privateKey, plaintext), {
message: /Unable to use this key to sign/
message: /Key algorithm mismatch/
});

await assert.rejects(
subtle.sign({ name, hash }, okpKeys.privateKey, plaintext), {
message: /Unable to use this key to sign/
message: /Key algorithm mismatch/
});
}

Expand Down
12 changes: 6 additions & 6 deletions test/parallel/test-webcrypto-sign-verify-eddsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ async function testVerify({ name,
// Test failure when using the wrong algorithms
await assert.rejects(
subtle.verify({ name, context }, hmacKey, signature, data), {
message: /Unable to use this key to verify/
message: /Key algorithm mismatch/
});

await assert.rejects(
subtle.verify({ name, context }, rsaKeys.publicKey, signature, data), {
message: /Unable to use this key to verify/
message: /Key algorithm mismatch/
});

await assert.rejects(
subtle.verify({ name, context }, ecKeys.publicKey, signature, data), {
message: /Unable to use this key to verify/
message: /Key algorithm mismatch/
});

if (name === 'Ed448' && supportsContext) {
Expand Down Expand Up @@ -227,17 +227,17 @@ async function testSign({ name,
// Test failure when using the wrong algorithms
await assert.rejects(
subtle.sign({ name, context }, hmacKey, data), {
message: /Unable to use this key to sign/
message: /Key algorithm mismatch/
});

await assert.rejects(
subtle.sign({ name, context }, rsaKeys.privateKey, data), {
message: /Unable to use this key to sign/
message: /Key algorithm mismatch/
});

await assert.rejects(
subtle.sign({ name, context }, ecKeys.privateKey, data), {
message: /Unable to use this key to sign/
message: /Key algorithm mismatch/
});

if (name === 'Ed448' && supportsContext) {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-webcrypto-sign-verify-hmac.js
Loading
Loading