0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -05:00

refactor(ext/crypto): align error messages (#25440)

Aligns the error messages in the ext/crypto folder to be in-line with
the Deno style guide.

https://github.com/denoland/deno/issues/25269
This commit is contained in:
Ian Bull 2024-09-04 23:20:27 -07:00 committed by GitHub
parent e799c2857c
commit 0450c12df5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -292,7 +292,7 @@ function normalizeAlgorithm(algorithm, op) {
normalizedAlgorithm[member] = normalizeAlgorithm(idlValue, "digest"); normalizedAlgorithm[member] = normalizeAlgorithm(idlValue, "digest");
} else if (idlType === "AlgorithmIdentifier") { } else if (idlType === "AlgorithmIdentifier") {
// TODO(lucacasonato): implement // TODO(lucacasonato): implement
throw new TypeError("unimplemented"); throw new TypeError("Unimplemented");
} }
} }
@ -443,7 +443,7 @@ function getKeyLength(algorithm) {
// 1. // 1.
if (!ArrayPrototypeIncludes([128, 192, 256], algorithm.length)) { if (!ArrayPrototypeIncludes([128, 192, 256], algorithm.length)) {
throw new DOMException( throw new DOMException(
"length must be 128, 192, or 256", `Length must be 128, 192, or 256: received ${algorithm.length}`,
"OperationError", "OperationError",
); );
} }
@ -470,14 +470,14 @@ function getKeyLength(algorithm) {
break; break;
default: default:
throw new DOMException( throw new DOMException(
"Unrecognized hash algorithm", `Unrecognized hash algorithm: ${algorithm.hash.name}`,
"NotSupportedError", "NotSupportedError",
); );
} }
} else if (algorithm.length !== 0) { } else if (algorithm.length !== 0) {
length = algorithm.length; length = algorithm.length;
} else { } else {
throw new TypeError("Invalid length."); throw new TypeError(`Invalid length: ${algorithm.length}`);
} }
// 2. // 2.
@ -492,7 +492,7 @@ function getKeyLength(algorithm) {
return null; return null;
} }
default: default:
throw new TypeError("unreachable"); throw new TypeError("Unreachable");
} }
} }
@ -556,7 +556,7 @@ class SubtleCrypto {
// 8. // 8.
if (normalizedAlgorithm.name !== key[_algorithm].name) { if (normalizedAlgorithm.name !== key[_algorithm].name) {
throw new DOMException( throw new DOMException(
"Encryption algorithm doesn't match key algorithm.", `Encryption algorithm '${normalizedAlgorithm.name}' does not match key algorithm`,
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -564,7 +564,7 @@ class SubtleCrypto {
// 9. // 9.
if (!ArrayPrototypeIncludes(key[_usages], "encrypt")) { if (!ArrayPrototypeIncludes(key[_usages], "encrypt")) {
throw new DOMException( throw new DOMException(
"Key does not support the 'encrypt' operation.", "Key does not support the 'encrypt' operation",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -599,7 +599,7 @@ class SubtleCrypto {
// 8. // 8.
if (normalizedAlgorithm.name !== key[_algorithm].name) { if (normalizedAlgorithm.name !== key[_algorithm].name) {
throw new DOMException( throw new DOMException(
"Decryption algorithm doesn't match key algorithm.", `Decryption algorithm "${normalizedAlgorithm.name}" does not match key algorithm`,
"OperationError", "OperationError",
); );
} }
@ -607,7 +607,7 @@ class SubtleCrypto {
// 9. // 9.
if (!ArrayPrototypeIncludes(key[_usages], "decrypt")) { if (!ArrayPrototypeIncludes(key[_usages], "decrypt")) {
throw new DOMException( throw new DOMException(
"Key does not support the 'decrypt' operation.", "Key does not support the 'decrypt' operation",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -683,7 +683,7 @@ class SubtleCrypto {
normalizedAlgorithm.length === 0 || normalizedAlgorithm.length > 128 normalizedAlgorithm.length === 0 || normalizedAlgorithm.length > 128
) { ) {
throw new DOMException( throw new DOMException(
"Counter length must not be 0 or greater than 128", `Counter length must not be 0 or greater than 128: received ${normalizedAlgorithm.length}`,
"OperationError", "OperationError",
); );
} }
@ -713,7 +713,7 @@ class SubtleCrypto {
) )
) { ) {
throw new DOMException( throw new DOMException(
"Invalid tag length", `Invalid tag length: ${normalizedAlgorithm.tagLength}`,
"OperationError", "OperationError",
); );
} }
@ -805,7 +805,7 @@ class SubtleCrypto {
// 8. // 8.
if (normalizedAlgorithm.name !== key[_algorithm].name) { if (normalizedAlgorithm.name !== key[_algorithm].name) {
throw new DOMException( throw new DOMException(
"Signing algorithm doesn't match key algorithm.", "Signing algorithm does not match key algorithm",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -813,7 +813,7 @@ class SubtleCrypto {
// 9. // 9.
if (!ArrayPrototypeIncludes(key[_usages], "sign")) { if (!ArrayPrototypeIncludes(key[_usages], "sign")) {
throw new DOMException( throw new DOMException(
"Key does not support the 'sign' operation.", "Key does not support the 'sign' operation",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -928,7 +928,7 @@ class SubtleCrypto {
} }
} }
throw new TypeError("unreachable"); throw new TypeError("Unreachable");
} }
/** /**
@ -967,11 +967,11 @@ class SubtleCrypto {
if (ArrayBufferIsView(keyData) || isArrayBuffer(keyData)) { if (ArrayBufferIsView(keyData) || isArrayBuffer(keyData)) {
keyData = copyBuffer(keyData); keyData = copyBuffer(keyData);
} else { } else {
throw new TypeError("keyData is a JsonWebKey"); throw new TypeError("Cannot import key: 'keyData' is a JsonWebKey");
} }
} else { } else {
if (ArrayBufferIsView(keyData) || isArrayBuffer(keyData)) { if (ArrayBufferIsView(keyData) || isArrayBuffer(keyData)) {
throw new TypeError("keyData is not a JsonWebKey"); throw new TypeError("Cannot import key: 'keyData' is not a JsonWebKey");
} }
} }
@ -1156,7 +1156,7 @@ class SubtleCrypto {
// 8. // 8.
if (!ArrayPrototypeIncludes(baseKey[_usages], "deriveBits")) { if (!ArrayPrototypeIncludes(baseKey[_usages], "deriveBits")) {
throw new DOMException( throw new DOMException(
"baseKey usages does not contain `deriveBits`", "'baseKey' usages does not contain 'deriveBits'",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -1222,7 +1222,7 @@ class SubtleCrypto {
// 11. // 11.
if (normalizedAlgorithm.name !== baseKey[_algorithm].name) { if (normalizedAlgorithm.name !== baseKey[_algorithm].name) {
throw new DOMException( throw new DOMException(
"Invalid algorithm name", `Invalid algorithm name: ${normalizedAlgorithm.name}`,
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -1230,7 +1230,7 @@ class SubtleCrypto {
// 12. // 12.
if (!ArrayPrototypeIncludes(baseKey[_usages], "deriveKey")) { if (!ArrayPrototypeIncludes(baseKey[_usages], "deriveKey")) {
throw new DOMException( throw new DOMException(
"baseKey usages does not contain `deriveKey`", "'baseKey' usages does not contain 'deriveKey'",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -1259,7 +1259,7 @@ class SubtleCrypto {
ArrayPrototypeIncludes(["private", "secret"], result[_type]) && ArrayPrototypeIncludes(["private", "secret"], result[_type]) &&
keyUsages.length == 0 keyUsages.length == 0
) { ) {
throw new SyntaxError("Invalid key usages"); throw new SyntaxError("Invalid key usage");
} }
// 17. // 17.
return result; return result;
@ -1298,14 +1298,14 @@ class SubtleCrypto {
if (normalizedAlgorithm.name !== key[_algorithm].name) { if (normalizedAlgorithm.name !== key[_algorithm].name) {
throw new DOMException( throw new DOMException(
"Verifying algorithm doesn't match key algorithm.", "Verifying algorithm does not match key algorithm",
"InvalidAccessError", "InvalidAccessError",
); );
} }
if (!ArrayPrototypeIncludes(key[_usages], "verify")) { if (!ArrayPrototypeIncludes(key[_usages], "verify")) {
throw new DOMException( throw new DOMException(
"Key does not support the 'verify' operation.", "Key does not support the 'verify' operation",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -1396,7 +1396,7 @@ class SubtleCrypto {
} }
} }
throw new TypeError("unreachable"); throw new TypeError("Unreachable");
} }
/** /**
@ -1435,7 +1435,7 @@ class SubtleCrypto {
// 8. // 8.
if (normalizedAlgorithm.name !== wrappingKey[_algorithm].name) { if (normalizedAlgorithm.name !== wrappingKey[_algorithm].name) {
throw new DOMException( throw new DOMException(
"Wrapping algorithm doesn't match key algorithm.", "Wrapping algorithm does not match key algorithm",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -1443,7 +1443,7 @@ class SubtleCrypto {
// 9. // 9.
if (!ArrayPrototypeIncludes(wrappingKey[_usages], "wrapKey")) { if (!ArrayPrototypeIncludes(wrappingKey[_usages], "wrapKey")) {
throw new DOMException( throw new DOMException(
"Key does not support the 'wrapKey' operation.", "Key does not support the 'wrapKey' operation",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -1591,7 +1591,7 @@ class SubtleCrypto {
// 11. // 11.
if (normalizedAlgorithm.name !== unwrappingKey[_algorithm].name) { if (normalizedAlgorithm.name !== unwrappingKey[_algorithm].name) {
throw new DOMException( throw new DOMException(
"Unwrapping algorithm doesn't match key algorithm.", "Unwrapping algorithm does not match key algorithm",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -1599,7 +1599,7 @@ class SubtleCrypto {
// 12. // 12.
if (!ArrayPrototypeIncludes(unwrappingKey[_usages], "unwrapKey")) { if (!ArrayPrototypeIncludes(unwrappingKey[_usages], "unwrapKey")) {
throw new DOMException( throw new DOMException(
"Key does not support the 'unwrapKey' operation.", "Key does not support the 'unwrapKey' operation",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -1678,7 +1678,7 @@ class SubtleCrypto {
(result[_type] == "secret" || result[_type] == "private") && (result[_type] == "secret" || result[_type] == "private") &&
keyUsages.length == 0 keyUsages.length == 0
) { ) {
throw new SyntaxError("Invalid key type."); throw new SyntaxError("Invalid key type");
} }
// 17. // 17.
result[_extractable] = extractable; result[_extractable] = extractable;
@ -1726,13 +1726,13 @@ class SubtleCrypto {
if (ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, result)) { if (ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, result)) {
const type = result[_type]; const type = result[_type];
if ((type === "secret" || type === "private") && usages.length === 0) { if ((type === "secret" || type === "private") && usages.length === 0) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
} else if ( } else if (
ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, result.privateKey) ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, result.privateKey)
) { ) {
if (result.privateKey[_usages].length === 0) { if (result.privateKey[_usages].length === 0) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
} }
@ -1758,7 +1758,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u), (u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2. // 2.
@ -1817,7 +1817,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
], u), ], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2. // 2.
@ -1873,7 +1873,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u), (u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2-3. // 2-3.
@ -1933,7 +1933,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u), (u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2-3. // 2-3.
@ -1999,7 +1999,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
], u), ], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
return generateKeyAES(normalizedAlgorithm, extractable, usages); return generateKeyAES(normalizedAlgorithm, extractable, usages);
@ -2012,7 +2012,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
(u) => !ArrayPrototypeIncludes(["wrapKey", "unwrapKey"], u), (u) => !ArrayPrototypeIncludes(["wrapKey", "unwrapKey"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
return generateKeyAES(normalizedAlgorithm, extractable, usages); return generateKeyAES(normalizedAlgorithm, extractable, usages);
@ -2024,7 +2024,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u), (u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const privateKeyData = new Uint8Array(32); const privateKeyData = new Uint8Array(32);
const publicKeyData = new Uint8Array(32); const publicKeyData = new Uint8Array(32);
@ -2065,7 +2065,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u), (u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const ED25519_SEED_LEN = 32; const ED25519_SEED_LEN = 32;
@ -2114,7 +2114,7 @@ async function generateKey(normalizedAlgorithm, extractable, usages) {
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u), (u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2. // 2.
@ -2178,7 +2178,7 @@ function importKeyEd25519(
(u) => !ArrayPrototypeIncludes(["verify"], u), (u) => !ArrayPrototypeIncludes(["verify"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const handle = {}; const handle = {};
@ -2206,7 +2206,7 @@ function importKeyEd25519(
(u) => !ArrayPrototypeIncludes(["verify"], u), (u) => !ArrayPrototypeIncludes(["verify"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const publicKeyData = new Uint8Array(32); const publicKeyData = new Uint8Array(32);
@ -2237,7 +2237,7 @@ function importKeyEd25519(
(u) => !ArrayPrototypeIncludes(["sign"], u), (u) => !ArrayPrototypeIncludes(["sign"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const privateKeyData = new Uint8Array(32); const privateKeyData = new Uint8Array(32);
@ -2276,7 +2276,7 @@ function importKeyEd25519(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
} else { } else {
if ( if (
@ -2289,7 +2289,7 @@ function importKeyEd25519(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
} }
@ -2349,7 +2349,7 @@ function importKeyEd25519(
try { try {
privateKeyData = op_crypto_base64url_decode(jwk.d); privateKeyData = op_crypto_base64url_decode(jwk.d);
} catch (_) { } catch (_) {
throw new DOMException("invalid private key data", "DataError"); throw new DOMException("Invalid private key data", "DataError");
} }
const handle = {}; const handle = {};
@ -2372,7 +2372,7 @@ function importKeyEd25519(
try { try {
publicKeyData = op_crypto_base64url_decode(jwk.x); publicKeyData = op_crypto_base64url_decode(jwk.x);
} catch (_) { } catch (_) {
throw new DOMException("invalid public key data", "DataError"); throw new DOMException("Invalid public key data", "DataError");
} }
const handle = {}; const handle = {};
@ -2406,7 +2406,7 @@ function importKeyX25519(
case "raw": { case "raw": {
// 1. // 1.
if (keyUsages.length > 0) { if (keyUsages.length > 0) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const handle = {}; const handle = {};
@ -2429,7 +2429,7 @@ function importKeyX25519(
case "spki": { case "spki": {
// 1. // 1.
if (keyUsages.length > 0) { if (keyUsages.length > 0) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const publicKeyData = new Uint8Array(32); const publicKeyData = new Uint8Array(32);
@ -2460,7 +2460,7 @@ function importKeyX25519(
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u), (u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const privateKeyData = new Uint8Array(32); const privateKeyData = new Uint8Array(32);
@ -2499,13 +2499,13 @@ function importKeyX25519(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
} }
// 3. // 3.
if (jwk.d === undefined && keyUsages.length > 0) { if (jwk.d === undefined && keyUsages.length > 0) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 4. // 4.
@ -2641,7 +2641,7 @@ function exportKeyAES(
break; break;
default: default:
throw new DOMException( throw new DOMException(
"Invalid key length", `Invalid key length: ${algorithm.length}`,
"NotSupportedError", "NotSupportedError",
); );
} }
@ -2675,7 +2675,7 @@ function importKeyAES(
(u) => !ArrayPrototypeIncludes(supportedKeyUsages, u), (u) => !ArrayPrototypeIncludes(supportedKeyUsages, u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
const algorithmName = normalizedAlgorithm.name; const algorithmName = normalizedAlgorithm.name;
@ -2731,7 +2731,10 @@ function importKeyAES(
jwk.alg !== undefined && jwk.alg !== undefined &&
jwk.alg !== aesJwkAlg[algorithmName][128] jwk.alg !== aesJwkAlg[algorithmName][128]
) { ) {
throw new DOMException("Invalid algorithm", "DataError"); throw new DOMException(
`Invalid algorithm: ${jwk.alg}`,
"DataError",
);
} }
break; break;
case 192: case 192:
@ -2739,7 +2742,10 @@ function importKeyAES(
jwk.alg !== undefined && jwk.alg !== undefined &&
jwk.alg !== aesJwkAlg[algorithmName][192] jwk.alg !== aesJwkAlg[algorithmName][192]
) { ) {
throw new DOMException("Invalid algorithm", "DataError"); throw new DOMException(
`Invalid algorithm: ${jwk.alg}`,
"DataError",
);
} }
break; break;
case 256: case 256:
@ -2747,7 +2753,10 @@ function importKeyAES(
jwk.alg !== undefined && jwk.alg !== undefined &&
jwk.alg !== aesJwkAlg[algorithmName][256] jwk.alg !== aesJwkAlg[algorithmName][256]
) { ) {
throw new DOMException("Invalid algorithm", "DataError"); throw new DOMException(
`Invalid algorithm: ${jwk.alg}`,
"DataError",
);
} }
break; break;
default: default:
@ -2761,7 +2770,7 @@ function importKeyAES(
if ( if (
keyUsages.length > 0 && jwk.use !== undefined && jwk.use !== "enc" keyUsages.length > 0 && jwk.use !== undefined && jwk.use !== "enc"
) { ) {
throw new DOMException("Invalid key usages", "DataError"); throw new DOMException("Invalid key usage", "DataError");
} }
// 7. // 7.
@ -2844,7 +2853,7 @@ function importKeyHMAC(
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u), (u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 3. // 3.
@ -2926,7 +2935,7 @@ function importKeyHMAC(
break; break;
} }
default: default:
throw new TypeError("unreachable"); throw new TypeError("Unreachable");
} }
// 7. // 7.
@ -3059,7 +3068,7 @@ function importKeyEC(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 3. // 3.
@ -3100,7 +3109,7 @@ function importKeyEC(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2-9. // 2-9.
@ -3140,7 +3149,7 @@ function importKeyEC(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
} else if (keyUsages.length != 0) { } else if (keyUsages.length != 0) {
throw new DOMException("Key usage must be empty", "SyntaxError"); throw new DOMException("Key usage must be empty", "SyntaxError");
@ -3183,7 +3192,7 @@ function importKeyEC(
(u) => !ArrayPrototypeIncludes(supportedUsages[keyType], u), (u) => !ArrayPrototypeIncludes(supportedUsages[keyType], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 3. // 3.
@ -3391,7 +3400,7 @@ function importKeyRSA(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2-9. // 2-9.
@ -3436,7 +3445,7 @@ function importKeyRSA(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2-9. // 2-9.
@ -3485,7 +3494,7 @@ function importKeyRSA(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
} else if ( } else if (
ArrayPrototypeFind( ArrayPrototypeFind(
@ -3497,7 +3506,7 @@ function importKeyRSA(
), ),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 3. // 3.
@ -3579,7 +3588,7 @@ function importKeyRSA(
break; break;
default: default:
throw new DOMException( throw new DOMException(
`'alg' property of JsonWebKey must be one of 'RS1', 'RS256', 'RS384', 'RS512'`, `'alg' property of JsonWebKey must be one of 'RS1', 'RS256', 'RS384', 'RS512': received ${jwk.alg}`,
"DataError", "DataError",
); );
} }
@ -3602,7 +3611,7 @@ function importKeyRSA(
break; break;
default: default:
throw new DOMException( throw new DOMException(
`'alg' property of JsonWebKey must be one of 'PS1', 'PS256', 'PS384', 'PS512'`, `'alg' property of JsonWebKey must be one of 'PS1', 'PS256', 'PS384', 'PS512': received ${jwk.alg}`,
"DataError", "DataError",
); );
} }
@ -3625,7 +3634,7 @@ function importKeyRSA(
break; break;
default: default:
throw new DOMException( throw new DOMException(
`'alg' property of JsonWebKey must be one of 'RSA-OAEP', 'RSA-OAEP-256', 'RSA-OAEP-384', or 'RSA-OAEP-512'`, `'alg' property of JsonWebKey must be one of 'RSA-OAEP', 'RSA-OAEP-256', 'RSA-OAEP-384', or 'RSA-OAEP-512': received ${jwk.alg}`,
"DataError", "DataError",
); );
} }
@ -3639,7 +3648,7 @@ function importKeyRSA(
// 9.2. // 9.2.
if (normalizedHash.name !== normalizedAlgorithm.hash.name) { if (normalizedHash.name !== normalizedAlgorithm.hash.name) {
throw new DOMException( throw new DOMException(
`'alg' property of JsonWebKey must be '${normalizedAlgorithm.name}'`, `'alg' property of JsonWebKey must be '${normalizedAlgorithm.name}': received ${jwk.alg}`,
"DataError", "DataError",
); );
} }
@ -3684,7 +3693,7 @@ function importKeyRSA(
} }
} else { } else {
throw new DOMException( throw new DOMException(
"only optimized private keys are supported", "Only optimized private keys are supported",
"NotSupportedError", "NotSupportedError",
); );
} }
@ -3782,7 +3791,7 @@ function importKeyHKDF(
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u), (u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 2. // 2.
@ -3834,7 +3843,7 @@ function importKeyPBKDF2(
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u), (u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
) !== undefined ) !== undefined
) { ) {
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usage", "SyntaxError");
} }
// 3. // 3.
@ -3878,7 +3887,7 @@ function exportKeyHMAC(format, key, innerKey) {
// 3. // 3.
case "raw": { case "raw": {
const bits = innerKey.data; const bits = innerKey.data;
// TODO(petamoriken): Uint8Array doesn't have push method // TODO(petamoriken): Uint8Array does not have push method
// for (let _i = 7 & (8 - bits.length % 8); _i > 0; _i--) { // for (let _i = 7 & (8 - bits.length % 8); _i > 0; _i--) {
// bits.push(0); // bits.push(0);
// } // }
@ -4331,7 +4340,10 @@ async function generateKeyAES(normalizedAlgorithm, extractable, usages) {
// 2. // 2.
if (!ArrayPrototypeIncludes([128, 192, 256], normalizedAlgorithm.length)) { if (!ArrayPrototypeIncludes([128, 192, 256], normalizedAlgorithm.length)) {
throw new DOMException("Invalid key length", "OperationError"); throw new DOMException(
`Invalid key length: ${normalizedAlgorithm.length}`,
"OperationError",
);
} }
// 3. // 3.
@ -4417,7 +4429,7 @@ async function deriveBits(normalizedAlgorithm, baseKey, length) {
publicKey[_algorithm].namedCurve !== baseKey[_algorithm].namedCurve publicKey[_algorithm].namedCurve !== baseKey[_algorithm].namedCurve
) { ) {
throw new DOMException( throw new DOMException(
"namedCurve mismatch", "'namedCurve' mismatch",
"InvalidAccessError", "InvalidAccessError",
); );
} }
@ -4670,7 +4682,7 @@ async function encrypt(normalizedAlgorithm, key, data) {
) )
) { ) {
throw new DOMException( throw new DOMException(
"Invalid tag length", `Invalid tag length: ${normalizedAlgorithm.tagLength}`,
"OperationError", "OperationError",
); );
} }