mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
refactor: use primordials for extensions/crypto (#11229)
This commit is contained in:
parent
01cf8aab9f
commit
91fe137d7d
1 changed files with 71 additions and 20 deletions
|
@ -1,6 +1,7 @@
|
||||||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
/// <reference path="../../core/internal.d.ts" />
|
||||||
/// <reference path="../../core/lib.deno_core.d.ts" />
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
||||||
/// <reference path="../webidl/internal.d.ts" />
|
/// <reference path="../webidl/internal.d.ts" />
|
||||||
/// <reference path="../web/lib.deno_web.d.ts" />
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
||||||
|
@ -12,6 +13,28 @@
|
||||||
const webidl = window.__bootstrap.webidl;
|
const webidl = window.__bootstrap.webidl;
|
||||||
const { DOMException } = window.__bootstrap.domException;
|
const { DOMException } = window.__bootstrap.domException;
|
||||||
|
|
||||||
|
const {
|
||||||
|
ArrayPrototypeFind,
|
||||||
|
ArrayBufferIsView,
|
||||||
|
ArrayPrototypeIncludes,
|
||||||
|
StringPrototypeToUpperCase,
|
||||||
|
Symbol,
|
||||||
|
SymbolFor,
|
||||||
|
SymbolToStringTag,
|
||||||
|
WeakMap,
|
||||||
|
WeakMapPrototypeGet,
|
||||||
|
WeakMapPrototypeSet,
|
||||||
|
Int8Array,
|
||||||
|
Uint8Array,
|
||||||
|
TypedArrayPrototypeSlice,
|
||||||
|
Int16Array,
|
||||||
|
Uint16Array,
|
||||||
|
Int32Array,
|
||||||
|
Uint32Array,
|
||||||
|
Uint8ClampedArray,
|
||||||
|
TypeError,
|
||||||
|
} = window.__bootstrap.primordials;
|
||||||
|
|
||||||
// P-521 is not yet supported.
|
// P-521 is not yet supported.
|
||||||
const supportedNamedCurves = ["P-256", "P-384"];
|
const supportedNamedCurves = ["P-256", "P-384"];
|
||||||
|
|
||||||
|
@ -63,7 +86,9 @@
|
||||||
// 5.
|
// 5.
|
||||||
let desiredType = undefined;
|
let desiredType = undefined;
|
||||||
for (const key in registeredAlgorithms) {
|
for (const key in registeredAlgorithms) {
|
||||||
if (key.toLowerCase() === algName.toLowerCase()) {
|
if (
|
||||||
|
StringPrototypeToUpperCase(key) === StringPrototypeToUpperCase(algName)
|
||||||
|
) {
|
||||||
algName = key;
|
algName = key;
|
||||||
desiredType = registeredAlgorithms[key];
|
desiredType = registeredAlgorithms[key];
|
||||||
}
|
}
|
||||||
|
@ -93,7 +118,8 @@
|
||||||
|
|
||||||
if (idlType === "BufferSource") {
|
if (idlType === "BufferSource") {
|
||||||
normalizedAlgorithm[member] = new Uint8Array(
|
normalizedAlgorithm[member] = new Uint8Array(
|
||||||
(ArrayBuffer.isView(idlValue) ? idlValue.buffer : idlValue).slice(
|
TypedArrayPrototypeSlice(
|
||||||
|
(ArrayBufferIsView(idlValue) ? idlValue.buffer : idlValue),
|
||||||
idlValue.byteOffset ?? 0,
|
idlValue.byteOffset ?? 0,
|
||||||
idlValue.byteLength,
|
idlValue.byteLength,
|
||||||
),
|
),
|
||||||
|
@ -161,7 +187,7 @@
|
||||||
return "CryptoKey";
|
return "CryptoKey";
|
||||||
}
|
}
|
||||||
|
|
||||||
[Symbol.for("Deno.customInspect")](inspect) {
|
[SymbolFor("Deno.customInspect")](inspect) {
|
||||||
return `${this.constructor.name} ${
|
return `${this.constructor.name} ${
|
||||||
inspect({
|
inspect({
|
||||||
type: this.type,
|
type: this.type,
|
||||||
|
@ -201,7 +227,7 @@
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
function usageIntersection(a, b) {
|
function usageIntersection(a, b) {
|
||||||
return a.includes(b) ? [b] : [];
|
return ArrayPrototypeIncludes(a, b) ? [b] : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(lucacasonato): this should be moved to rust
|
// TODO(lucacasonato): this should be moved to rust
|
||||||
|
@ -231,12 +257,13 @@
|
||||||
context: "Argument 2",
|
context: "Argument 2",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (ArrayBuffer.isView(data)) {
|
if (ArrayBufferIsView(data)) {
|
||||||
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
||||||
} else {
|
} else {
|
||||||
data = new Uint8Array(data);
|
data = new Uint8Array(data);
|
||||||
}
|
}
|
||||||
data = data.slice();
|
|
||||||
|
data = TypedArrayPrototypeSlice(data);
|
||||||
|
|
||||||
algorithm = normalizeAlgorithm(algorithm, "digest");
|
algorithm = normalizeAlgorithm(algorithm, "digest");
|
||||||
|
|
||||||
|
@ -273,18 +300,18 @@
|
||||||
});
|
});
|
||||||
|
|
||||||
// 1.
|
// 1.
|
||||||
if (ArrayBuffer.isView(data)) {
|
if (ArrayBufferIsView(data)) {
|
||||||
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
||||||
} else {
|
} else {
|
||||||
data = new Uint8Array(data);
|
data = new Uint8Array(data);
|
||||||
}
|
}
|
||||||
data = data.slice();
|
data = TypedArrayPrototypeSlice(data);
|
||||||
|
|
||||||
// 2.
|
// 2.
|
||||||
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "sign");
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "sign");
|
||||||
|
|
||||||
const handle = key[_handle];
|
const handle = key[_handle];
|
||||||
const keyData = KEY_STORE.get(handle);
|
const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
|
||||||
|
|
||||||
// 8.
|
// 8.
|
||||||
if (normalizedAlgorithm.name !== key[_algorithm].name) {
|
if (normalizedAlgorithm.name !== key[_algorithm].name) {
|
||||||
|
@ -295,7 +322,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9.
|
// 9.
|
||||||
if (!key[_usages].includes("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",
|
||||||
|
@ -354,7 +381,7 @@
|
||||||
// 2.
|
// 2.
|
||||||
const hashAlgorithm = normalizedAlgorithm.hash.name;
|
const hashAlgorithm = normalizedAlgorithm.hash.name;
|
||||||
const namedCurve = key[_algorithm].namedCurve;
|
const namedCurve = key[_algorithm].namedCurve;
|
||||||
if (!supportedNamedCurves.includes(namedCurve)) {
|
if (!ArrayPrototypeIncludes(supportedNamedCurves, namedCurve)) {
|
||||||
throw new DOMException("Curve not supported", "NotSupportedError");
|
throw new DOMException("Curve not supported", "NotSupportedError");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -444,7 +471,12 @@
|
||||||
case "RSASSA-PKCS1-v1_5":
|
case "RSASSA-PKCS1-v1_5":
|
||||||
case "RSA-PSS": {
|
case "RSA-PSS": {
|
||||||
// 1.
|
// 1.
|
||||||
if (usages.find((u) => !["sign", "verify"].includes(u)) !== undefined) {
|
if (
|
||||||
|
ArrayPrototypeFind(
|
||||||
|
usages,
|
||||||
|
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
|
||||||
|
) !== undefined
|
||||||
|
) {
|
||||||
throw new DOMException("Invalid key usages", "SyntaxError");
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -458,7 +490,10 @@
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const handle = {};
|
const handle = {};
|
||||||
KEY_STORE.set(handle, { type: "pkcs8", data: keyData });
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
||||||
|
type: "pkcs8",
|
||||||
|
data: keyData,
|
||||||
|
});
|
||||||
|
|
||||||
// 4-8.
|
// 4-8.
|
||||||
const algorithm = {
|
const algorithm = {
|
||||||
|
@ -492,18 +527,31 @@
|
||||||
// TODO(lucacasonato): RSA-OAEP
|
// TODO(lucacasonato): RSA-OAEP
|
||||||
case "ECDSA": {
|
case "ECDSA": {
|
||||||
// 1.
|
// 1.
|
||||||
if (usages.find((u) => !["sign", "verify"].includes(u)) !== undefined) {
|
if (
|
||||||
|
ArrayPrototypeFind(
|
||||||
|
usages,
|
||||||
|
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
|
||||||
|
) !== undefined
|
||||||
|
) {
|
||||||
throw new DOMException("Invalid key usages", "SyntaxError");
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2-3.
|
// 2-3.
|
||||||
const handle = {};
|
const handle = {};
|
||||||
if (supportedNamedCurves.includes(normalizedAlgorithm.namedCurve)) {
|
if (
|
||||||
|
ArrayPrototypeIncludes(
|
||||||
|
supportedNamedCurves,
|
||||||
|
normalizedAlgorithm.namedCurve,
|
||||||
|
)
|
||||||
|
) {
|
||||||
const keyData = await core.opAsync("op_crypto_generate_key", {
|
const keyData = await core.opAsync("op_crypto_generate_key", {
|
||||||
name: "ECDSA",
|
name: "ECDSA",
|
||||||
namedCurve: normalizedAlgorithm.namedCurve,
|
namedCurve: normalizedAlgorithm.namedCurve,
|
||||||
});
|
});
|
||||||
KEY_STORE.set(handle, { type: "pkcs8", data: keyData });
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
||||||
|
type: "pkcs8",
|
||||||
|
data: keyData,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
throw new DOMException("Curve not supported", "NotSupportedError");
|
throw new DOMException("Curve not supported", "NotSupportedError");
|
||||||
}
|
}
|
||||||
|
@ -543,7 +591,10 @@
|
||||||
case "HMAC": {
|
case "HMAC": {
|
||||||
// 1.
|
// 1.
|
||||||
if (
|
if (
|
||||||
usages.find((u) => !["sign", "verify"].includes(u)) !== undefined
|
ArrayPrototypeFind(
|
||||||
|
usages,
|
||||||
|
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
|
||||||
|
) !== undefined
|
||||||
) {
|
) {
|
||||||
throw new DOMException("Invalid key usages", "SyntaxError");
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
||||||
}
|
}
|
||||||
|
@ -565,7 +616,7 @@
|
||||||
length,
|
length,
|
||||||
});
|
});
|
||||||
const handle = {};
|
const handle = {};
|
||||||
KEY_STORE.set(handle, { type: "raw", data: keyData });
|
WeakMapPrototypeSet(KEY_STORE, handle, { type: "raw", data: keyData });
|
||||||
|
|
||||||
// 6-10.
|
// 6-10.
|
||||||
const algorithm = {
|
const algorithm = {
|
||||||
|
@ -641,11 +692,11 @@
|
||||||
return subtle;
|
return subtle;
|
||||||
}
|
}
|
||||||
|
|
||||||
get [Symbol.toStringTag]() {
|
get [SymbolToStringTag]() {
|
||||||
return "Crypto";
|
return "Crypto";
|
||||||
}
|
}
|
||||||
|
|
||||||
[Symbol.for("Deno.customInspect")](inspect) {
|
[SymbolFor("Deno.customInspect")](inspect) {
|
||||||
return `${this.constructor.name} ${inspect({})}`;
|
return `${this.constructor.name} ${inspect({})}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue