Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 4 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /WebCryptoAPI/export-key-error-order.https.any.html - WPT Dashboard Interop Dashboard
- /WebCryptoAPI/export-key-error-order.https.any.worker.html - WPT Dashboard Interop Dashboard
// META: title=WebCryptoAPI: exportKey() and wrapKey() check export support first
// HKDF and PBKDF2 are not registered for the export key operation, and their
// keys can only be imported as non-extractable. Both error conditions are
// therefore always true at once, which makes the order observable.
//
// exportKey: step 6 (NotSupportedError) precedes step 7 (InvalidAccessError).
//
// wrapKey: step 11 (NotSupportedError) precedes step 12 (InvalidAccessError).
function importDeriveKey(name) {
return crypto.subtle.importKey('raw', new Uint8Array([]), name, false,
['deriveBits']);
}
function importAesKey(name, usages) {
return crypto.subtle.importKey('raw', new Uint8Array(16), name, false,
usages);
}
for (const name of ['HKDF', 'PBKDF2']) {
promise_test(async (t) => {
const key = await importDeriveKey(name);
return promise_rejects_dom(t, 'NotSupportedError',
crypto.subtle.exportKey('raw', key));
}, `exportKey() with a ${name} key throws NotSupportedError`);
promise_test(async (t) => {
const key = await importDeriveKey(name);
const wrappingKey = await importAesKey('AES-KW', ['wrapKey']);
return promise_rejects_dom(
t, 'NotSupportedError',
crypto.subtle.wrapKey('raw', key, wrappingKey, 'AES-KW'));
}, `wrapKey() with a ${name} key throws NotSupportedError`);
}
// An algorithm that does support the export key operation must still report
// the extractable check.
promise_test(async (t) => {
const key = await importAesKey('AES-CBC', ['encrypt']);
return promise_rejects_dom(t, 'InvalidAccessError',
crypto.subtle.exportKey('raw', key));
}, 'exportKey() with a non-extractable AES-CBC key throws InvalidAccessError');
promise_test(async (t) => {
const key = await importAesKey('AES-CBC', ['encrypt']);
const wrappingKey = await importAesKey('AES-KW', ['wrapKey']);
return promise_rejects_dom(
t, 'InvalidAccessError',
crypto.subtle.wrapKey('raw', key, wrappingKey, 'AES-KW'));
}, 'wrapKey() with a non-extractable AES-CBC key throws InvalidAccessError');