1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

refactor(ext/crypto): various cleanups in js code (#13027)

Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
Sean Michael Wykes 2021-12-09 19:32:10 -03:00 committed by GitHub
parent 345f0fbe5c
commit 0129c74fd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -799,7 +799,9 @@
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "importKey"); const normalizedAlgorithm = normalizeAlgorithm(algorithm, "importKey");
switch (normalizedAlgorithm.name) { const algorithmName = normalizedAlgorithm.name;
switch (algorithmName) {
case "HMAC": { case "HMAC": {
return importKeyHMAC( return importKeyHMAC(
format, format,
@ -819,15 +821,7 @@
); );
} }
case "RSASSA-PKCS1-v1_5": case "RSASSA-PKCS1-v1_5":
case "RSA-PSS": { case "RSA-PSS":
return await importKeyRSA(
format,
normalizedAlgorithm,
keyData,
extractable,
keyUsages,
);
}
case "RSA-OAEP": { case "RSA-OAEP": {
return await importKeyRSA( return await importKeyRSA(
format, format,
@ -846,7 +840,7 @@
case "AES-CTR": case "AES-CTR":
case "AES-CBC": case "AES-CBC":
case "AES-GCM": { case "AES-GCM": {
return await importKeyAES( return importKeyAES(
format, format,
normalizedAlgorithm, normalizedAlgorithm,
keyData, keyData,
@ -892,7 +886,9 @@
// 2. // 2.
const innerKey = WeakMapPrototypeGet(KEY_STORE, handle); const innerKey = WeakMapPrototypeGet(KEY_STORE, handle);
switch (key[_algorithm].name) { const algorithmName = key[_algorithm].name;
switch (algorithmName) {
case "HMAC": { case "HMAC": {
return exportKeyHMAC(format, key, innerKey); return exportKeyHMAC(format, key, innerKey);
} }
@ -1469,7 +1465,9 @@
} }
async function generateKey(normalizedAlgorithm, extractable, usages) { async function generateKey(normalizedAlgorithm, extractable, usages) {
switch (normalizedAlgorithm.name) { const algorithmName = normalizedAlgorithm.name;
switch (algorithmName) {
case "RSASSA-PKCS1-v1_5": case "RSASSA-PKCS1-v1_5":
case "RSA-PSS": { case "RSA-PSS": {
// 1. // 1.
@ -1486,7 +1484,7 @@
const keyData = await core.opAsync( const keyData = await core.opAsync(
"op_crypto_generate_key", "op_crypto_generate_key",
{ {
name: normalizedAlgorithm.name, name: algorithmName,
modulusLength: normalizedAlgorithm.modulusLength, modulusLength: normalizedAlgorithm.modulusLength,
publicExponent: normalizedAlgorithm.publicExponent, publicExponent: normalizedAlgorithm.publicExponent,
}, },
@ -1499,7 +1497,7 @@
// 4-8. // 4-8.
const algorithm = { const algorithm = {
name: normalizedAlgorithm.name, name: algorithmName,
modulusLength: normalizedAlgorithm.modulusLength, modulusLength: normalizedAlgorithm.modulusLength,
publicExponent: normalizedAlgorithm.publicExponent, publicExponent: normalizedAlgorithm.publicExponent,
hash: normalizedAlgorithm.hash, hash: normalizedAlgorithm.hash,
@ -1546,7 +1544,7 @@
const keyData = await core.opAsync( const keyData = await core.opAsync(
"op_crypto_generate_key", "op_crypto_generate_key",
{ {
name: normalizedAlgorithm.name, name: algorithmName,
modulusLength: normalizedAlgorithm.modulusLength, modulusLength: normalizedAlgorithm.modulusLength,
publicExponent: normalizedAlgorithm.publicExponent, publicExponent: normalizedAlgorithm.publicExponent,
}, },
@ -1559,7 +1557,7 @@
// 4-8. // 4-8.
const algorithm = { const algorithm = {
name: normalizedAlgorithm.name, name: algorithmName,
modulusLength: normalizedAlgorithm.modulusLength, modulusLength: normalizedAlgorithm.modulusLength,
publicExponent: normalizedAlgorithm.publicExponent, publicExponent: normalizedAlgorithm.publicExponent,
hash: normalizedAlgorithm.hash, hash: normalizedAlgorithm.hash,
@ -1587,6 +1585,8 @@
return { publicKey, privateKey }; return { publicKey, privateKey };
} }
case "ECDSA": { case "ECDSA": {
const namedCurve = normalizedAlgorithm.namedCurve;
// 1. // 1.
if ( if (
ArrayPrototypeFind( ArrayPrototypeFind(
@ -1602,12 +1602,12 @@
if ( if (
ArrayPrototypeIncludes( ArrayPrototypeIncludes(
supportedNamedCurves, supportedNamedCurves,
normalizedAlgorithm.namedCurve, namedCurve,
) )
) { ) {
const keyData = await core.opAsync("op_crypto_generate_key", { const keyData = await core.opAsync("op_crypto_generate_key", {
name: "ECDSA", name: algorithmName,
namedCurve: normalizedAlgorithm.namedCurve, namedCurve,
}); });
WeakMapPrototypeSet(KEY_STORE, handle, { WeakMapPrototypeSet(KEY_STORE, handle, {
type: "private", type: "private",
@ -1619,8 +1619,8 @@
// 4-6. // 4-6.
const algorithm = { const algorithm = {
name: "ECDSA", name: algorithmName,
namedCurve: normalizedAlgorithm.namedCurve, namedCurve,
}; };
// 7-11. // 7-11.
@ -1645,6 +1645,8 @@
return { publicKey, privateKey }; return { publicKey, privateKey };
} }
case "ECDH": { case "ECDH": {
const namedCurve = normalizedAlgorithm.namedCurve;
// 1. // 1.
if ( if (
ArrayPrototypeFind( ArrayPrototypeFind(
@ -1660,12 +1662,12 @@
if ( if (
ArrayPrototypeIncludes( ArrayPrototypeIncludes(
supportedNamedCurves, supportedNamedCurves,
normalizedAlgorithm.namedCurve, namedCurve,
) )
) { ) {
const keyData = await core.opAsync("op_crypto_generate_key", { const keyData = await core.opAsync("op_crypto_generate_key", {
name: "ECDH", name: algorithmName,
namedCurve: normalizedAlgorithm.namedCurve, namedCurve,
}); });
WeakMapPrototypeSet(KEY_STORE, handle, { WeakMapPrototypeSet(KEY_STORE, handle, {
type: "private", type: "private",
@ -1677,8 +1679,8 @@
// 4-6. // 4-6.
const algorithm = { const algorithm = {
name: "ECDH", name: algorithmName,
namedCurve: normalizedAlgorithm.namedCurve, namedCurve,
}; };
// 7-11. // 7-11.
@ -1759,7 +1761,7 @@
// 3-4. // 3-4.
const keyData = await core.opAsync("op_crypto_generate_key", { const keyData = await core.opAsync("op_crypto_generate_key", {
name: "HMAC", name: algorithmName,
hash: normalizedAlgorithm.hash.name, hash: normalizedAlgorithm.hash.name,
length, length,
}); });
@ -1771,7 +1773,7 @@
// 6-10. // 6-10.
const algorithm = { const algorithm = {
name: "HMAC", name: algorithmName,
hash: { hash: {
name: normalizedAlgorithm.hash.name, name: normalizedAlgorithm.hash.name,
}, },
@ -1810,6 +1812,10 @@
// 1-3. // 1-3.
const jwk = { const jwk = {
kty: "oct", kty: "oct",
// 5.
ext: key[_extractable],
// 6.
"key_ops": key.usages,
k: unpaddedBase64(innerKey.data), k: unpaddedBase64(innerKey.data),
}; };
@ -1832,10 +1838,6 @@
); );
} }
// 5.
jwk.key_ops = key[_usages];
// 6.
jwk.ext = key[_extractable];
// 7. // 7.
return jwk; return jwk;
} }
@ -1862,8 +1864,11 @@
throw new DOMException("Invalid key usages", "SyntaxError"); throw new DOMException("Invalid key usages", "SyntaxError");
} }
const algorithmName = normalizedAlgorithm.name;
// 2. // 2.
let data = keyData; let data = keyData;
switch (format) { switch (format) {
case "raw": { case "raw": {
// 2. // 2.
@ -1902,7 +1907,7 @@
case 128: case 128:
if ( if (
jwk.alg !== undefined && jwk.alg !== undefined &&
jwk.alg !== aesJwkAlg[normalizedAlgorithm.name][128] jwk.alg !== aesJwkAlg[algorithmName][128]
) { ) {
throw new DOMException("Invalid algorithm", "DataError"); throw new DOMException("Invalid algorithm", "DataError");
} }
@ -1910,7 +1915,7 @@
case 192: case 192:
if ( if (
jwk.alg !== undefined && jwk.alg !== undefined &&
jwk.alg !== aesJwkAlg[normalizedAlgorithm.name][192] jwk.alg !== aesJwkAlg[algorithmName][192]
) { ) {
throw new DOMException("Invalid algorithm", "DataError"); throw new DOMException("Invalid algorithm", "DataError");
} }
@ -1918,7 +1923,7 @@
case 256: case 256:
if ( if (
jwk.alg !== undefined && jwk.alg !== undefined &&
jwk.alg !== aesJwkAlg[normalizedAlgorithm.name][256] jwk.alg !== aesJwkAlg[algorithmName][256]
) { ) {
throw new DOMException("Invalid algorithm", "DataError"); throw new DOMException("Invalid algorithm", "DataError");
} }
@ -1985,7 +1990,7 @@
// 4-7. // 4-7.
const algorithm = { const algorithm = {
name: normalizedAlgorithm.name, name: algorithmName,
length: data.byteLength * 8, length: data.byteLength * 8,
}; };
@ -2050,8 +2055,10 @@
// 4. // 4.
data = decodeSymmetricKey(jwk.k); data = decodeSymmetricKey(jwk.k);
// 5. // 5.
hash = normalizedAlgorithm.hash; hash = normalizedAlgorithm.hash;
// 6. // 6.
switch (hash.name) { switch (hash.name) {
case "SHA-1": { case "SHA-1": {
@ -2592,6 +2599,8 @@
} }
async function generateKeyAES(normalizedAlgorithm, extractable, usages) { async function generateKeyAES(normalizedAlgorithm, extractable, usages) {
const algorithmName = normalizedAlgorithm.name;
// 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", "OperationError");
@ -2599,7 +2608,7 @@
// 3. // 3.
const keyData = await core.opAsync("op_crypto_generate_key", { const keyData = await core.opAsync("op_crypto_generate_key", {
name: normalizedAlgorithm.name, name: algorithmName,
length: normalizedAlgorithm.length, length: normalizedAlgorithm.length,
}); });
const handle = {}; const handle = {};
@ -2610,7 +2619,7 @@
// 6-8. // 6-8.
const algorithm = { const algorithm = {
name: normalizedAlgorithm.name, name: algorithmName,
length: normalizedAlgorithm.length, length: normalizedAlgorithm.length,
}; };