2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-07-06 08:16:04 -04:00
|
|
|
|
|
|
|
// @ts-check
|
2021-07-08 11:58:38 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
2021-07-06 08:16:04 -04:00
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const ops = core.ops;
|
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import DOMException from "ext:deno_web/01_dom_exception.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayBufferIsView,
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayBufferPrototype,
|
2023-04-02 13:41:41 -04:00
|
|
|
ArrayBufferPrototypeGetByteLength,
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayBufferPrototypeSlice,
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypeEvery,
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeFilter,
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypeFind,
|
|
|
|
ArrayPrototypeIncludes,
|
2023-04-02 13:41:41 -04:00
|
|
|
DataViewPrototypeGetBuffer,
|
|
|
|
DataViewPrototypeGetByteLength,
|
|
|
|
DataViewPrototypeGetByteOffset,
|
2023-02-07 14:22:46 -05:00
|
|
|
JSONParse,
|
|
|
|
JSONStringify,
|
|
|
|
MathCeil,
|
|
|
|
ObjectAssign,
|
2023-05-02 06:15:45 -04:00
|
|
|
ObjectHasOwn,
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
SafeArrayIterator,
|
2023-04-14 16:23:28 -04:00
|
|
|
SafeWeakMap,
|
2023-05-01 09:30:02 -04:00
|
|
|
StringFromCharCode,
|
|
|
|
StringPrototypeCharCodeAt,
|
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
StringPrototypeToUpperCase,
|
2023-02-07 14:22:46 -05:00
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
SyntaxError,
|
2023-05-01 09:30:02 -04:00
|
|
|
TypeError,
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetBuffer,
|
|
|
|
TypedArrayPrototypeGetByteLength,
|
|
|
|
TypedArrayPrototypeGetByteOffset,
|
|
|
|
TypedArrayPrototypeGetSymbolToStringTag,
|
2023-05-01 09:30:02 -04:00
|
|
|
TypedArrayPrototypeSlice,
|
2023-02-07 14:22:46 -05:00
|
|
|
Uint8Array,
|
|
|
|
WeakMapPrototypeGet,
|
|
|
|
WeakMapPrototypeSet,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
// P-521 is not yet supported.
|
|
|
|
const supportedNamedCurves = ["P-256", "P-384"];
|
|
|
|
const recognisedUsages = [
|
|
|
|
"encrypt",
|
|
|
|
"decrypt",
|
|
|
|
"sign",
|
|
|
|
"verify",
|
|
|
|
"deriveKey",
|
|
|
|
"deriveBits",
|
|
|
|
"wrapKey",
|
|
|
|
"unwrapKey",
|
|
|
|
];
|
|
|
|
|
|
|
|
const simpleAlgorithmDictionaries = {
|
|
|
|
AesGcmParams: { iv: "BufferSource", additionalData: "BufferSource" },
|
|
|
|
RsaHashedKeyGenParams: { hash: "HashAlgorithmIdentifier" },
|
|
|
|
EcKeyGenParams: {},
|
|
|
|
HmacKeyGenParams: { hash: "HashAlgorithmIdentifier" },
|
|
|
|
RsaPssParams: {},
|
|
|
|
EcdsaParams: { hash: "HashAlgorithmIdentifier" },
|
|
|
|
HmacImportParams: { hash: "HashAlgorithmIdentifier" },
|
|
|
|
HkdfParams: {
|
|
|
|
hash: "HashAlgorithmIdentifier",
|
|
|
|
salt: "BufferSource",
|
|
|
|
info: "BufferSource",
|
|
|
|
},
|
|
|
|
Pbkdf2Params: { hash: "HashAlgorithmIdentifier", salt: "BufferSource" },
|
|
|
|
RsaOaepParams: { label: "BufferSource" },
|
|
|
|
RsaHashedImportParams: { hash: "HashAlgorithmIdentifier" },
|
|
|
|
EcKeyImportParams: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const supportedAlgorithms = {
|
|
|
|
"digest": {
|
|
|
|
"SHA-1": null,
|
|
|
|
"SHA-256": null,
|
|
|
|
"SHA-384": null,
|
|
|
|
"SHA-512": null,
|
|
|
|
},
|
|
|
|
"generateKey": {
|
|
|
|
"RSASSA-PKCS1-v1_5": "RsaHashedKeyGenParams",
|
|
|
|
"RSA-PSS": "RsaHashedKeyGenParams",
|
|
|
|
"RSA-OAEP": "RsaHashedKeyGenParams",
|
|
|
|
"ECDSA": "EcKeyGenParams",
|
|
|
|
"ECDH": "EcKeyGenParams",
|
|
|
|
"AES-CTR": "AesKeyGenParams",
|
|
|
|
"AES-CBC": "AesKeyGenParams",
|
|
|
|
"AES-GCM": "AesKeyGenParams",
|
|
|
|
"AES-KW": "AesKeyGenParams",
|
|
|
|
"HMAC": "HmacKeyGenParams",
|
|
|
|
"X25519": null,
|
|
|
|
"Ed25519": null,
|
|
|
|
},
|
|
|
|
"sign": {
|
|
|
|
"RSASSA-PKCS1-v1_5": null,
|
|
|
|
"RSA-PSS": "RsaPssParams",
|
|
|
|
"ECDSA": "EcdsaParams",
|
|
|
|
"HMAC": null,
|
|
|
|
"Ed25519": null,
|
|
|
|
},
|
|
|
|
"verify": {
|
|
|
|
"RSASSA-PKCS1-v1_5": null,
|
|
|
|
"RSA-PSS": "RsaPssParams",
|
|
|
|
"ECDSA": "EcdsaParams",
|
|
|
|
"HMAC": null,
|
|
|
|
"Ed25519": null,
|
|
|
|
},
|
|
|
|
"importKey": {
|
|
|
|
"RSASSA-PKCS1-v1_5": "RsaHashedImportParams",
|
|
|
|
"RSA-PSS": "RsaHashedImportParams",
|
|
|
|
"RSA-OAEP": "RsaHashedImportParams",
|
|
|
|
"ECDSA": "EcKeyImportParams",
|
|
|
|
"ECDH": "EcKeyImportParams",
|
|
|
|
"HMAC": "HmacImportParams",
|
|
|
|
"HKDF": null,
|
|
|
|
"PBKDF2": null,
|
|
|
|
"AES-CTR": null,
|
|
|
|
"AES-CBC": null,
|
|
|
|
"AES-GCM": null,
|
|
|
|
"AES-KW": null,
|
|
|
|
"Ed25519": null,
|
|
|
|
"X25519": null,
|
|
|
|
},
|
|
|
|
"deriveBits": {
|
|
|
|
"HKDF": "HkdfParams",
|
|
|
|
"PBKDF2": "Pbkdf2Params",
|
|
|
|
"ECDH": "EcdhKeyDeriveParams",
|
|
|
|
"X25519": "EcdhKeyDeriveParams",
|
|
|
|
},
|
|
|
|
"encrypt": {
|
|
|
|
"RSA-OAEP": "RsaOaepParams",
|
|
|
|
"AES-CBC": "AesCbcParams",
|
|
|
|
"AES-GCM": "AesGcmParams",
|
|
|
|
"AES-CTR": "AesCtrParams",
|
|
|
|
},
|
|
|
|
"decrypt": {
|
|
|
|
"RSA-OAEP": "RsaOaepParams",
|
|
|
|
"AES-CBC": "AesCbcParams",
|
|
|
|
"AES-GCM": "AesGcmParams",
|
|
|
|
"AES-CTR": "AesCtrParams",
|
|
|
|
},
|
|
|
|
"get key length": {
|
|
|
|
"AES-CBC": "AesDerivedKeyParams",
|
|
|
|
"AES-CTR": "AesDerivedKeyParams",
|
|
|
|
"AES-GCM": "AesDerivedKeyParams",
|
|
|
|
"AES-KW": "AesDerivedKeyParams",
|
|
|
|
"HMAC": "HmacImportParams",
|
|
|
|
"HKDF": null,
|
|
|
|
"PBKDF2": null,
|
|
|
|
},
|
|
|
|
"wrapKey": {
|
|
|
|
"AES-KW": null,
|
|
|
|
},
|
|
|
|
"unwrapKey": {
|
|
|
|
"AES-KW": null,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const aesJwkAlg = {
|
|
|
|
"AES-CTR": {
|
|
|
|
128: "A128CTR",
|
|
|
|
192: "A192CTR",
|
|
|
|
256: "A256CTR",
|
|
|
|
},
|
|
|
|
"AES-CBC": {
|
|
|
|
128: "A128CBC",
|
|
|
|
192: "A192CBC",
|
|
|
|
256: "A256CBC",
|
|
|
|
},
|
|
|
|
"AES-GCM": {
|
|
|
|
128: "A128GCM",
|
|
|
|
192: "A192GCM",
|
|
|
|
256: "A256GCM",
|
|
|
|
},
|
|
|
|
"AES-KW": {
|
|
|
|
128: "A128KW",
|
|
|
|
192: "A192KW",
|
|
|
|
256: "A256KW",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// See https://www.w3.org/TR/WebCryptoAPI/#dfn-normalize-an-algorithm
|
|
|
|
// 18.4.4
|
|
|
|
function normalizeAlgorithm(algorithm, op) {
|
|
|
|
if (typeof algorithm == "string") {
|
|
|
|
return normalizeAlgorithm({ name: algorithm }, op);
|
|
|
|
}
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
|
|
|
const registeredAlgorithms = supportedAlgorithms[op];
|
|
|
|
// 2. 3.
|
2023-05-01 06:47:13 -04:00
|
|
|
const initialAlg = webidl.converters.Algorithm(
|
|
|
|
algorithm,
|
|
|
|
"Failed to normalize algorithm",
|
|
|
|
"passed algorithm",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
let algName = initialAlg.name;
|
|
|
|
|
|
|
|
// 5.
|
|
|
|
let desiredType = undefined;
|
|
|
|
for (const key in registeredAlgorithms) {
|
2023-05-02 06:15:45 -04:00
|
|
|
if (!ObjectHasOwn(registeredAlgorithms, key)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
StringPrototypeToUpperCase(key) === StringPrototypeToUpperCase(algName)
|
|
|
|
) {
|
|
|
|
algName = key;
|
|
|
|
desiredType = registeredAlgorithms[key];
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (desiredType === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Unrecognized algorithm name",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Fast path everything below if the registered dictionary is "None".
|
|
|
|
if (desiredType === null) {
|
|
|
|
return { name: algName };
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
2023-05-01 06:47:13 -04:00
|
|
|
const normalizedAlgorithm = webidl.converters[desiredType](
|
|
|
|
algorithm,
|
|
|
|
"Failed to normalize algorithm",
|
|
|
|
"passed algorithm",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
// 7.
|
|
|
|
normalizedAlgorithm.name = algName;
|
|
|
|
|
|
|
|
// 9.
|
|
|
|
const dict = simpleAlgorithmDictionaries[desiredType];
|
|
|
|
// 10.
|
|
|
|
for (const member in dict) {
|
2023-05-02 06:15:45 -04:00
|
|
|
if (!ObjectHasOwn(dict, member)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
continue;
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const idlType = dict[member];
|
|
|
|
const idlValue = normalizedAlgorithm[member];
|
|
|
|
// 3.
|
|
|
|
if (idlType === "BufferSource" && idlValue) {
|
2023-04-02 13:41:41 -04:00
|
|
|
normalizedAlgorithm[member] = copyBuffer(idlValue);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (idlType === "HashAlgorithmIdentifier") {
|
|
|
|
normalizedAlgorithm[member] = normalizeAlgorithm(idlValue, "digest");
|
|
|
|
} else if (idlType === "AlgorithmIdentifier") {
|
|
|
|
// TODO(lucacasonato): implement
|
|
|
|
throw new TypeError("unimplemented");
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return normalizedAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ArrayBufferView | ArrayBuffer} input
|
|
|
|
* @returns {Uint8Array}
|
|
|
|
*/
|
|
|
|
function copyBuffer(input) {
|
2023-04-02 13:41:41 -04:00
|
|
|
if (ArrayBufferIsView(input)) {
|
|
|
|
if (TypedArrayPrototypeGetSymbolToStringTag(input) !== undefined) {
|
|
|
|
// TypedArray
|
|
|
|
return TypedArrayPrototypeSlice(
|
|
|
|
new Uint8Array(
|
|
|
|
TypedArrayPrototypeGetBuffer(/** @type {Uint8Array} */ (input)),
|
|
|
|
TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (input)),
|
|
|
|
TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (input)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// DataView
|
|
|
|
return TypedArrayPrototypeSlice(
|
|
|
|
new Uint8Array(
|
|
|
|
DataViewPrototypeGetBuffer(/** @type {DataView} */ (input)),
|
|
|
|
DataViewPrototypeGetByteOffset(/** @type {DataView} */ (input)),
|
|
|
|
DataViewPrototypeGetByteLength(/** @type {DataView} */ (input)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ArrayBuffer
|
2023-02-07 14:22:46 -05:00
|
|
|
return TypedArrayPrototypeSlice(
|
2023-04-02 13:41:41 -04:00
|
|
|
new Uint8Array(
|
|
|
|
input,
|
|
|
|
0,
|
|
|
|
ArrayBufferPrototypeGetByteLength(input),
|
|
|
|
),
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const _handle = Symbol("[[handle]]");
|
|
|
|
const _algorithm = Symbol("[[algorithm]]");
|
|
|
|
const _extractable = Symbol("[[extractable]]");
|
|
|
|
const _usages = Symbol("[[usages]]");
|
|
|
|
const _type = Symbol("[[type]]");
|
|
|
|
|
|
|
|
class CryptoKey {
|
|
|
|
/** @type {string} */
|
|
|
|
[_type];
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_extractable];
|
|
|
|
/** @type {object} */
|
|
|
|
[_algorithm];
|
|
|
|
/** @type {string[]} */
|
|
|
|
[_usages];
|
|
|
|
/** @type {object} */
|
|
|
|
[_handle];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {string} */
|
|
|
|
get type() {
|
|
|
|
webidl.assertBranded(this, CryptoKeyPrototype);
|
|
|
|
return this[_type];
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {boolean} */
|
|
|
|
get extractable() {
|
|
|
|
webidl.assertBranded(this, CryptoKeyPrototype);
|
|
|
|
return this[_extractable];
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {string[]} */
|
|
|
|
get usages() {
|
|
|
|
webidl.assertBranded(this, CryptoKeyPrototype);
|
|
|
|
// TODO(lucacasonato): return a SameObject copy
|
|
|
|
return this[_usages];
|
2021-12-09 10:50:04 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {object} */
|
|
|
|
get algorithm() {
|
|
|
|
webidl.assertBranded(this, CryptoKeyPrototype);
|
|
|
|
// TODO(lucacasonato): return a SameObject copy
|
|
|
|
return this[_algorithm];
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.customInspect")](inspect) {
|
|
|
|
return `${this.constructor.name} ${
|
|
|
|
inspect({
|
|
|
|
type: this.type,
|
|
|
|
extractable: this.extractable,
|
|
|
|
algorithm: this.algorithm,
|
|
|
|
usages: this.usages,
|
|
|
|
})
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(CryptoKey);
|
2023-02-07 14:22:46 -05:00
|
|
|
const CryptoKeyPrototype = CryptoKey.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} type
|
|
|
|
* @param {boolean} extractable
|
|
|
|
* @param {string[]} usages
|
|
|
|
* @param {object} algorithm
|
|
|
|
* @param {object} handle
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
function constructKey(type, extractable, usages, algorithm, handle) {
|
|
|
|
const key = webidl.createBranded(CryptoKey);
|
|
|
|
key[_type] = type;
|
|
|
|
key[_extractable] = extractable;
|
|
|
|
key[_usages] = usages;
|
|
|
|
key[_algorithm] = algorithm;
|
|
|
|
key[_handle] = handle;
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://w3c.github.io/webcrypto/#concept-usage-intersection
|
|
|
|
/**
|
|
|
|
* @param {string[]} a
|
|
|
|
* @param {string[]} b
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
function usageIntersection(a, b) {
|
2023-05-01 09:30:02 -04:00
|
|
|
return ArrayPrototypeFilter(
|
|
|
|
a,
|
|
|
|
(i) => ArrayPrototypeIncludes(b, i),
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(lucacasonato): this should be moved to rust
|
|
|
|
/** @type {WeakMap<object, object>} */
|
2023-04-14 16:23:28 -04:00
|
|
|
const KEY_STORE = new SafeWeakMap();
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
function getKeyLength(algorithm) {
|
|
|
|
switch (algorithm.name) {
|
|
|
|
case "AES-CBC":
|
|
|
|
case "AES-CTR":
|
|
|
|
case "AES-GCM":
|
|
|
|
case "AES-KW": {
|
|
|
|
// 1.
|
|
|
|
if (!ArrayPrototypeIncludes([128, 192, 256], algorithm.length)) {
|
|
|
|
throw new DOMException(
|
|
|
|
"length must be 128, 192, or 256",
|
|
|
|
"OperationError",
|
|
|
|
);
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
return algorithm.length;
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "HMAC": {
|
|
|
|
// 1.
|
|
|
|
let length;
|
|
|
|
if (algorithm.length === undefined) {
|
|
|
|
switch (algorithm.hash.name) {
|
|
|
|
case "SHA-1":
|
|
|
|
length = 512;
|
|
|
|
break;
|
|
|
|
case "SHA-256":
|
|
|
|
length = 512;
|
|
|
|
break;
|
|
|
|
case "SHA-384":
|
|
|
|
length = 1024;
|
|
|
|
break;
|
|
|
|
case "SHA-512":
|
|
|
|
length = 1024;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Unrecognized hash algorithm",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (algorithm.length !== 0) {
|
|
|
|
length = algorithm.length;
|
|
|
|
} else {
|
|
|
|
throw new TypeError("Invalid length.");
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
return length;
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "HKDF": {
|
|
|
|
// 1.
|
|
|
|
return null;
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "PBKDF2": {
|
|
|
|
// 1.
|
|
|
|
return null;
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new TypeError("unreachable");
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class SubtleCrypto {
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-07 14:22:46 -05:00
|
|
|
* @param {string} algorithm
|
|
|
|
* @param {BufferSource} data
|
2023-04-02 13:41:41 -04:00
|
|
|
* @returns {Promise<ArrayBuffer>}
|
2021-07-06 08:16:04 -04:00
|
|
|
*/
|
2023-02-07 14:22:46 -05:00
|
|
|
async digest(algorithm, data) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'digest' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 2, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
|
|
|
data = webidl.converters.BufferSource(data, prefix, "Argument 2");
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
data = copyBuffer(data);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
algorithm = normalizeAlgorithm(algorithm, "digest");
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const result = await core.opAsync(
|
|
|
|
"op_crypto_subtle_digest",
|
|
|
|
algorithm.name,
|
|
|
|
data,
|
|
|
|
);
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(result);
|
2021-10-12 06:39:46 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} algorithm
|
|
|
|
* @param {CryptoKey} key
|
|
|
|
* @param {BufferSource} data
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
async encrypt(algorithm, key, data) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'encrypt' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 3, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
|
|
|
key = webidl.converters.CryptoKey(key, prefix, "Argument 2");
|
|
|
|
data = webidl.converters.BufferSource(data, prefix, "Argument 3");
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
data = copyBuffer(data);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "encrypt");
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
|
|
|
if (normalizedAlgorithm.name !== key[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Encryption algorithm doesn't match key algorithm.",
|
|
|
|
"InvalidAccessError",
|
2021-07-06 08:16:04 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
|
|
|
if (!ArrayPrototypeIncludes(key[_usages], "encrypt")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key does not support the 'encrypt' operation.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return await encrypt(normalizedAlgorithm, key, data);
|
|
|
|
}
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} algorithm
|
|
|
|
* @param {CryptoKey} key
|
|
|
|
* @param {BufferSource} data
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
async decrypt(algorithm, key, data) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'decrypt' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 3, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
|
|
|
key = webidl.converters.CryptoKey(key, prefix, "Argument 2");
|
|
|
|
data = webidl.converters.BufferSource(data, prefix, "Argument 3");
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
data = copyBuffer(data);
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "decrypt");
|
2021-10-11 10:37:51 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
|
|
|
if (normalizedAlgorithm.name !== key[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Decryption algorithm doesn't match key algorithm.",
|
|
|
|
"OperationError",
|
|
|
|
);
|
|
|
|
}
|
2021-11-25 09:57:01 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
|
|
|
if (!ArrayPrototypeIncludes(key[_usages], "decrypt")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key does not support the 'decrypt' operation.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2021-08-24 15:59:02 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = key[_handle];
|
|
|
|
const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (normalizedAlgorithm.name) {
|
|
|
|
case "RSA-OAEP": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (normalizedAlgorithm.label) {
|
|
|
|
normalizedAlgorithm.label = copyBuffer(normalizedAlgorithm.label);
|
|
|
|
} else {
|
|
|
|
normalizedAlgorithm.label = new Uint8Array();
|
|
|
|
}
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3-5.
|
|
|
|
const hashAlgorithm = key[_algorithm].hash.name;
|
|
|
|
const plainText = await core.opAsync("op_crypto_decrypt", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "RSA-OAEP",
|
|
|
|
hash: hashAlgorithm,
|
|
|
|
label: normalizedAlgorithm.label,
|
|
|
|
}, data);
|
2021-11-25 09:57:01 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(plainText);
|
2021-11-25 09:57:01 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "AES-CBC": {
|
|
|
|
normalizedAlgorithm.iv = copyBuffer(normalizedAlgorithm.iv);
|
2021-11-25 09:57:01 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
2023-04-02 13:41:41 -04:00
|
|
|
if (TypedArrayPrototypeGetByteLength(normalizedAlgorithm.iv) !== 16) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"Counter must be 16 bytes",
|
|
|
|
"OperationError",
|
|
|
|
);
|
|
|
|
}
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const plainText = await core.opAsync("op_crypto_decrypt", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "AES-CBC",
|
|
|
|
iv: normalizedAlgorithm.iv,
|
|
|
|
length: key[_algorithm].length,
|
|
|
|
}, data);
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(plainText);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "AES-CTR": {
|
|
|
|
normalizedAlgorithm.counter = copyBuffer(normalizedAlgorithm.counter);
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
2023-04-02 13:41:41 -04:00
|
|
|
if (
|
|
|
|
TypedArrayPrototypeGetByteLength(normalizedAlgorithm.counter) !== 16
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"Counter vector must be 16 bytes",
|
|
|
|
"OperationError",
|
|
|
|
);
|
|
|
|
}
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (
|
|
|
|
normalizedAlgorithm.length === 0 || normalizedAlgorithm.length > 128
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Counter length must not be 0 or greater than 128",
|
|
|
|
"OperationError",
|
|
|
|
);
|
2021-08-24 15:59:02 -04:00
|
|
|
}
|
2021-10-11 10:37:51 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const cipherText = await core.opAsync("op_crypto_decrypt", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "AES-CTR",
|
|
|
|
keyLength: key[_algorithm].length,
|
|
|
|
counter: normalizedAlgorithm.counter,
|
|
|
|
ctrLength: normalizedAlgorithm.length,
|
|
|
|
}, data);
|
2021-10-11 10:37:51 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(cipherText);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "AES-GCM": {
|
|
|
|
normalizedAlgorithm.iv = copyBuffer(normalizedAlgorithm.iv);
|
2021-10-11 10:37:51 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
|
|
|
if (normalizedAlgorithm.tagLength === undefined) {
|
|
|
|
normalizedAlgorithm.tagLength = 128;
|
|
|
|
} else if (
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
[32, 64, 96, 104, 112, 120, 128],
|
|
|
|
normalizedAlgorithm.tagLength,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Invalid tag length",
|
|
|
|
"OperationError",
|
|
|
|
);
|
2021-10-11 10:37:51 -04:00
|
|
|
}
|
2022-01-03 06:27:28 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
2023-04-02 13:41:41 -04:00
|
|
|
if (
|
|
|
|
TypedArrayPrototypeGetByteLength(data) <
|
|
|
|
normalizedAlgorithm.tagLength / 8
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"Tag length overflows ciphertext",
|
|
|
|
"OperationError",
|
|
|
|
);
|
2022-01-03 06:27:28 -05:00
|
|
|
}
|
2022-01-14 03:48:53 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3. We only support 96-bit and 128-bit nonce.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(
|
|
|
|
[12, 16],
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetByteLength(normalizedAlgorithm.iv),
|
2023-02-07 14:22:46 -05:00
|
|
|
) === undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Initialization vector length not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
2022-01-14 03:48:53 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
if (normalizedAlgorithm.additionalData !== undefined) {
|
2023-04-02 13:41:41 -04:00
|
|
|
// NOTE: over the size of Number.MAX_SAFE_INTEGER is not available in V8
|
|
|
|
// if (normalizedAlgorithm.additionalData.byteLength > (2 ** 64) - 1) {
|
|
|
|
// throw new DOMException(
|
|
|
|
// "Additional data too large",
|
|
|
|
// "OperationError",
|
|
|
|
// );
|
|
|
|
// }
|
2023-02-07 14:22:46 -05:00
|
|
|
normalizedAlgorithm.additionalData = copyBuffer(
|
|
|
|
normalizedAlgorithm.additionalData,
|
|
|
|
);
|
|
|
|
}
|
2022-01-14 03:48:53 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5-8.
|
|
|
|
const plaintext = await core.opAsync("op_crypto_decrypt", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "AES-GCM",
|
|
|
|
length: key[_algorithm].length,
|
|
|
|
iv: normalizedAlgorithm.iv,
|
|
|
|
additionalData: normalizedAlgorithm.additionalData ||
|
|
|
|
null,
|
|
|
|
tagLength: normalizedAlgorithm.tagLength,
|
|
|
|
}, data);
|
2022-01-14 03:48:53 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(plaintext);
|
2021-08-24 15:59:02 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
2021-08-24 15:59:02 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} algorithm
|
|
|
|
* @param {CryptoKey} key
|
|
|
|
* @param {BufferSource} data
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
async sign(algorithm, key, data) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'sign' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 3, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
|
|
|
key = webidl.converters.CryptoKey(key, prefix, "Argument 2");
|
|
|
|
data = webidl.converters.BufferSource(data, prefix, "Argument 3");
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
|
|
|
data = copyBuffer(data);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "sign");
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = key[_handle];
|
|
|
|
const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
|
|
|
if (normalizedAlgorithm.name !== key[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Signing algorithm doesn't match key algorithm.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
|
|
|
if (!ArrayPrototypeIncludes(key[_usages], "sign")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key does not support the 'sign' operation.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (normalizedAlgorithm.name) {
|
|
|
|
case "RSASSA-PKCS1-v1_5": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const hashAlgorithm = key[_algorithm].hash.name;
|
|
|
|
const signature = await core.opAsync("op_crypto_sign_key", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "RSASSA-PKCS1-v1_5",
|
|
|
|
hash: hashAlgorithm,
|
|
|
|
}, data);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(signature);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "RSA-PSS": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const hashAlgorithm = key[_algorithm].hash.name;
|
|
|
|
const signature = await core.opAsync("op_crypto_sign_key", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "RSA-PSS",
|
|
|
|
hash: hashAlgorithm,
|
|
|
|
saltLength: normalizedAlgorithm.saltLength,
|
|
|
|
}, data);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(signature);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "ECDSA": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const hashAlgorithm = normalizedAlgorithm.hash.name;
|
|
|
|
const namedCurve = key[_algorithm].namedCurve;
|
|
|
|
if (!ArrayPrototypeIncludes(supportedNamedCurves, namedCurve)) {
|
|
|
|
throw new DOMException("Curve not supported", "NotSupportedError");
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-03-05 07:34:07 -05:00
|
|
|
if (
|
|
|
|
(key[_algorithm].namedCurve === "P-256" &&
|
|
|
|
hashAlgorithm !== "SHA-256") ||
|
|
|
|
(key[_algorithm].namedCurve === "P-384" &&
|
|
|
|
hashAlgorithm !== "SHA-384")
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Not implemented",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const signature = await core.opAsync("op_crypto_sign_key", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "ECDSA",
|
|
|
|
hash: hashAlgorithm,
|
|
|
|
namedCurve,
|
|
|
|
}, data);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(signature);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "HMAC": {
|
|
|
|
const hashAlgorithm = key[_algorithm].hash.name;
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const signature = await core.opAsync("op_crypto_sign_key", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "HMAC",
|
|
|
|
hash: hashAlgorithm,
|
|
|
|
}, data);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(signature);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "Ed25519": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// https://briansmith.org/rustdoc/src/ring/ec/curve25519/ed25519/signing.rs.html#260
|
|
|
|
const SIGNATURE_LEN = 32 * 2; // ELEM_LEN + SCALAR_LEN
|
|
|
|
const signature = new Uint8Array(SIGNATURE_LEN);
|
2023-05-08 17:07:45 -04:00
|
|
|
if (!ops.op_crypto_sign_ed25519(keyData, data, signature)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"Failed to sign",
|
|
|
|
"OperationError",
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(signature);
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new TypeError("unreachable");
|
|
|
|
}
|
2021-08-03 15:24:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} format
|
|
|
|
* @param {BufferSource} keyData
|
|
|
|
* @param {string} algorithm
|
|
|
|
* @param {boolean} extractable
|
|
|
|
* @param {KeyUsages[]} keyUsages
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
// deno-lint-ignore require-await
|
|
|
|
async importKey(format, keyData, algorithm, extractable, keyUsages) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'importKey' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 4, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
format = webidl.converters.KeyFormat(format, prefix, "Argument 1");
|
|
|
|
keyData = webidl.converters["BufferSource or JsonWebKey"](
|
|
|
|
keyData,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
);
|
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 3",
|
|
|
|
);
|
|
|
|
extractable = webidl.converters.boolean(extractable, prefix, "Argument 4");
|
|
|
|
keyUsages = webidl.converters["sequence<KeyUsage>"](
|
|
|
|
keyUsages,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 5",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
// 2.
|
|
|
|
if (format !== "jwk") {
|
|
|
|
if (
|
|
|
|
ArrayBufferIsView(keyData) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, keyData)
|
|
|
|
) {
|
|
|
|
keyData = copyBuffer(keyData);
|
2021-08-13 05:27:56 -04:00
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new TypeError("keyData is a JsonWebKey");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
ArrayBufferIsView(keyData) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, keyData)
|
|
|
|
) {
|
|
|
|
throw new TypeError("keyData is not a JsonWebKey");
|
2021-08-13 05:27:56 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-13 05:27:56 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "importKey");
|
2021-08-03 15:24:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithmName = normalizedAlgorithm.name;
|
2021-12-09 17:32:10 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (algorithmName) {
|
|
|
|
case "HMAC": {
|
|
|
|
return importKeyHMAC(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "ECDH":
|
|
|
|
case "ECDSA": {
|
|
|
|
return importKeyEC(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "RSASSA-PKCS1-v1_5":
|
|
|
|
case "RSA-PSS":
|
|
|
|
case "RSA-OAEP": {
|
|
|
|
return importKeyRSA(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "HKDF": {
|
|
|
|
return importKeyHKDF(format, keyData, extractable, keyUsages);
|
|
|
|
}
|
|
|
|
case "PBKDF2": {
|
|
|
|
return importKeyPBKDF2(format, keyData, extractable, keyUsages);
|
|
|
|
}
|
|
|
|
case "AES-CTR":
|
|
|
|
case "AES-CBC":
|
|
|
|
case "AES-GCM": {
|
|
|
|
return importKeyAES(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
["encrypt", "decrypt", "wrapKey", "unwrapKey"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "AES-KW": {
|
|
|
|
return importKeyAES(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
["wrapKey", "unwrapKey"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "X25519": {
|
|
|
|
return importKeyX25519(
|
|
|
|
format,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "Ed25519": {
|
|
|
|
return importKeyEd25519(
|
|
|
|
format,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
);
|
2021-08-03 15:24:02 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
2021-08-03 15:24:02 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-03 15:24:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} format
|
|
|
|
* @param {CryptoKey} key
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
// deno-lint-ignore require-await
|
|
|
|
async exportKey(format, key) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'exportKey' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 2, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
format = webidl.converters.KeyFormat(format, prefix, "Argument 1");
|
|
|
|
key = webidl.converters.CryptoKey(key, prefix, "Argument 2");
|
2021-08-03 15:24:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = key[_handle];
|
|
|
|
// 2.
|
|
|
|
const innerKey = WeakMapPrototypeGet(KEY_STORE, handle);
|
2021-08-03 15:24:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithmName = key[_algorithm].name;
|
2021-12-09 17:32:10 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let result;
|
2022-04-07 08:58:56 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (algorithmName) {
|
|
|
|
case "HMAC": {
|
|
|
|
result = exportKeyHMAC(format, key, innerKey);
|
|
|
|
break;
|
2021-08-03 15:24:02 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "RSASSA-PKCS1-v1_5":
|
|
|
|
case "RSA-PSS":
|
|
|
|
case "RSA-OAEP": {
|
|
|
|
result = exportKeyRSA(format, key, innerKey);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ECDH":
|
|
|
|
case "ECDSA": {
|
|
|
|
result = exportKeyEC(format, key, innerKey);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Ed25519": {
|
|
|
|
result = exportKeyEd25519(format, key, innerKey);
|
|
|
|
break;
|
2022-04-07 08:58:56 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "X25519": {
|
|
|
|
result = exportKeyX25519(format, key, innerKey);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "AES-CTR":
|
|
|
|
case "AES-CBC":
|
|
|
|
case "AES-GCM":
|
|
|
|
case "AES-KW": {
|
|
|
|
result = exportKeyAES(format, key, innerKey);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
2022-04-07 08:58:56 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (key.extractable === false) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not extractable",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2021-08-03 15:24:02 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {AlgorithmIdentifier} algorithm
|
|
|
|
* @param {CryptoKey} baseKey
|
|
|
|
* @param {number | null} length
|
|
|
|
* @returns {Promise<ArrayBuffer>}
|
|
|
|
*/
|
|
|
|
async deriveBits(algorithm, baseKey, length) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'deriveBits' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 3, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
|
|
|
baseKey = webidl.converters.CryptoKey(baseKey, prefix, "Argument 2");
|
2023-02-07 14:22:46 -05:00
|
|
|
if (length !== null) {
|
2023-05-01 06:47:13 -04:00
|
|
|
length = webidl.converters["unsigned long"](length, prefix, "Argument 3");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-26 06:48:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "deriveBits");
|
|
|
|
// 4-6.
|
|
|
|
const result = await deriveBits(normalizedAlgorithm, baseKey, length);
|
|
|
|
// 7.
|
|
|
|
if (normalizedAlgorithm.name !== baseKey[_algorithm].name) {
|
|
|
|
throw new DOMException("Invalid algorithm name", "InvalidAccessError");
|
|
|
|
}
|
|
|
|
// 8.
|
|
|
|
if (!ArrayPrototypeIncludes(baseKey[_usages], "deriveBits")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"baseKey usages does not contain `deriveBits`",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2021-08-26 06:48:07 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9-10.
|
|
|
|
return result;
|
|
|
|
}
|
2021-08-26 06:48:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {AlgorithmIdentifier} algorithm
|
|
|
|
* @param {CryptoKey} baseKey
|
|
|
|
* @param {number} length
|
|
|
|
* @returns {Promise<ArrayBuffer>}
|
|
|
|
*/
|
|
|
|
async deriveKey(
|
|
|
|
algorithm,
|
|
|
|
baseKey,
|
|
|
|
derivedKeyType,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'deriveKey' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 5, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
|
|
|
baseKey = webidl.converters.CryptoKey(baseKey, prefix, "Argument 2");
|
|
|
|
derivedKeyType = webidl.converters.AlgorithmIdentifier(
|
|
|
|
derivedKeyType,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 3",
|
|
|
|
);
|
|
|
|
extractable = webidl.converters["boolean"](
|
|
|
|
extractable,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 4",
|
|
|
|
);
|
|
|
|
keyUsages = webidl.converters["sequence<KeyUsage>"](
|
|
|
|
keyUsages,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 5",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
// 2-3.
|
|
|
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "deriveBits");
|
|
|
|
|
|
|
|
// 4-5.
|
|
|
|
const normalizedDerivedKeyAlgorithmImport = normalizeAlgorithm(
|
|
|
|
derivedKeyType,
|
|
|
|
"importKey",
|
|
|
|
);
|
|
|
|
|
|
|
|
// 6-7.
|
|
|
|
const normalizedDerivedKeyAlgorithmLength = normalizeAlgorithm(
|
2021-10-12 06:39:46 -04:00
|
|
|
derivedKeyType,
|
2023-02-07 14:22:46 -05:00
|
|
|
"get key length",
|
|
|
|
);
|
|
|
|
|
|
|
|
// 8-10.
|
|
|
|
|
|
|
|
// 11.
|
|
|
|
if (normalizedAlgorithm.name !== baseKey[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Invalid algorithm name",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 12.
|
|
|
|
if (!ArrayPrototypeIncludes(baseKey[_usages], "deriveKey")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"baseKey usages does not contain `deriveKey`",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 13.
|
|
|
|
const length = getKeyLength(normalizedDerivedKeyAlgorithmLength);
|
|
|
|
|
|
|
|
// 14.
|
2023-06-19 03:56:58 -04:00
|
|
|
const secret = await deriveBits(
|
2023-02-07 14:22:46 -05:00
|
|
|
normalizedAlgorithm,
|
|
|
|
baseKey,
|
|
|
|
length,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 15.
|
|
|
|
const result = await this.importKey(
|
|
|
|
"raw",
|
|
|
|
secret,
|
|
|
|
normalizedDerivedKeyAlgorithmImport,
|
2021-10-12 06:39:46 -04:00
|
|
|
extractable,
|
|
|
|
keyUsages,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// 16.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(["private", "secret"], result[_type]) &&
|
|
|
|
keyUsages.length == 0
|
2021-10-12 06:39:46 -04:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new SyntaxError("Invalid key usages");
|
|
|
|
}
|
|
|
|
// 17.
|
|
|
|
return result;
|
|
|
|
}
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} algorithm
|
|
|
|
* @param {CryptoKey} key
|
|
|
|
* @param {BufferSource} signature
|
|
|
|
* @param {BufferSource} data
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async verify(algorithm, key, signature, data) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'verify' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 4, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
|
|
|
key = webidl.converters.CryptoKey(key, prefix, "Argument 2");
|
|
|
|
signature = webidl.converters.BufferSource(signature, prefix, "Argument 3");
|
|
|
|
data = webidl.converters.BufferSource(data, prefix, "Argument 4");
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
signature = copyBuffer(signature);
|
|
|
|
|
|
|
|
// 3.
|
|
|
|
data = copyBuffer(data);
|
|
|
|
|
|
|
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "verify");
|
|
|
|
|
|
|
|
const handle = key[_handle];
|
|
|
|
const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
|
|
|
|
|
|
|
|
if (normalizedAlgorithm.name !== key[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Verifying algorithm doesn't match key algorithm.",
|
|
|
|
"InvalidAccessError",
|
2021-10-12 06:39:46 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!ArrayPrototypeIncludes(key[_usages], "verify")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key does not support the 'verify' operation.",
|
|
|
|
"InvalidAccessError",
|
2021-10-12 06:39:46 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (normalizedAlgorithm.name) {
|
|
|
|
case "RSASSA-PKCS1-v1_5": {
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const hashAlgorithm = key[_algorithm].hash.name;
|
|
|
|
return await core.opAsync("op_crypto_verify_key", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "RSASSA-PKCS1-v1_5",
|
|
|
|
hash: hashAlgorithm,
|
|
|
|
signature,
|
|
|
|
}, data);
|
2021-10-12 06:39:46 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "RSA-PSS": {
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const hashAlgorithm = key[_algorithm].hash.name;
|
|
|
|
return await core.opAsync("op_crypto_verify_key", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "RSA-PSS",
|
|
|
|
hash: hashAlgorithm,
|
|
|
|
signature,
|
2023-10-30 11:25:12 -04:00
|
|
|
saltLength: normalizedAlgorithm.saltLength,
|
2023-02-07 14:22:46 -05:00
|
|
|
}, data);
|
2021-10-12 06:39:46 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "HMAC": {
|
|
|
|
const hash = key[_algorithm].hash.name;
|
|
|
|
return await core.opAsync("op_crypto_verify_key", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "HMAC",
|
|
|
|
hash,
|
|
|
|
signature,
|
|
|
|
}, data);
|
|
|
|
}
|
|
|
|
case "ECDSA": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// 2.
|
|
|
|
const hash = normalizedAlgorithm.hash.name;
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-03-05 07:34:07 -05:00
|
|
|
if (
|
|
|
|
(key[_algorithm].namedCurve === "P-256" && hash !== "SHA-256") ||
|
|
|
|
(key[_algorithm].namedCurve === "P-384" && hash !== "SHA-384")
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Not implemented",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3-8.
|
|
|
|
return await core.opAsync("op_crypto_verify_key", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "ECDSA",
|
|
|
|
hash,
|
|
|
|
signature,
|
|
|
|
namedCurve: key[_algorithm].namedCurve,
|
|
|
|
}, data);
|
|
|
|
}
|
|
|
|
case "Ed25519": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-10-12 06:39:46 -04:00
|
|
|
|
2023-05-08 17:07:45 -04:00
|
|
|
return ops.op_crypto_verify_ed25519(keyData, data, signature);
|
2021-10-12 06:39:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new TypeError("unreachable");
|
|
|
|
}
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} algorithm
|
|
|
|
* @param {boolean} extractable
|
|
|
|
* @param {KeyUsage[]} keyUsages
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
async wrapKey(format, key, wrappingKey, wrapAlgorithm) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'wrapKey' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 4, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
format = webidl.converters.KeyFormat(format, prefix, "Argument 1");
|
|
|
|
key = webidl.converters.CryptoKey(key, prefix, "Argument 2");
|
|
|
|
wrappingKey = webidl.converters.CryptoKey(
|
|
|
|
wrappingKey,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 3",
|
|
|
|
);
|
|
|
|
wrapAlgorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
wrapAlgorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 4",
|
|
|
|
);
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let normalizedAlgorithm;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// 2.
|
|
|
|
normalizedAlgorithm = normalizeAlgorithm(wrapAlgorithm, "wrapKey");
|
|
|
|
} catch (_) {
|
2021-07-12 08:45:36 -04:00
|
|
|
// 3.
|
2023-02-07 14:22:46 -05:00
|
|
|
normalizedAlgorithm = normalizeAlgorithm(wrapAlgorithm, "encrypt");
|
|
|
|
}
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
|
|
|
if (normalizedAlgorithm.name !== wrappingKey[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Wrapping algorithm doesn't match key algorithm.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
|
|
|
if (!ArrayPrototypeIncludes(wrappingKey[_usages], "wrapKey")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key does not support the 'wrapKey' operation.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 10. NotSupportedError will be thrown in step 12.
|
|
|
|
// 11.
|
|
|
|
if (key[_extractable] === false) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not extractable",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 12.
|
|
|
|
const exportedKey = await this.exportKey(format, key);
|
|
|
|
|
|
|
|
let bytes;
|
|
|
|
// 13.
|
|
|
|
if (format !== "jwk") {
|
|
|
|
bytes = new Uint8Array(exportedKey);
|
|
|
|
} else {
|
|
|
|
const jwk = JSONStringify(exportedKey);
|
|
|
|
const ret = new Uint8Array(jwk.length);
|
|
|
|
for (let i = 0; i < jwk.length; i++) {
|
|
|
|
ret[i] = StringPrototypeCharCodeAt(jwk, i);
|
2021-07-12 08:45:36 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
bytes = ret;
|
|
|
|
}
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 14-15.
|
|
|
|
if (
|
|
|
|
supportedAlgorithms["wrapKey"][normalizedAlgorithm.name] !== undefined
|
|
|
|
) {
|
|
|
|
const handle = wrappingKey[_handle];
|
|
|
|
const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (normalizedAlgorithm.name) {
|
|
|
|
case "AES-KW": {
|
|
|
|
const cipherText = await ops.op_crypto_wrap_key({
|
2021-08-04 15:49:27 -04:00
|
|
|
key: keyData,
|
2023-02-07 14:22:46 -05:00
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
}, bytes);
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(cipherText);
|
2021-09-11 16:49:53 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default: {
|
|
|
|
throw new DOMException(
|
|
|
|
"Not implemented",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2021-07-12 08:45:36 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (
|
|
|
|
supportedAlgorithms["encrypt"][normalizedAlgorithm.name] !== undefined
|
|
|
|
) {
|
|
|
|
// must construct a new key, since keyUsages is ["wrapKey"] and not ["encrypt"]
|
|
|
|
return await encrypt(
|
|
|
|
normalizedAlgorithm,
|
|
|
|
constructKey(
|
|
|
|
wrappingKey[_type],
|
|
|
|
wrappingKey[_extractable],
|
|
|
|
["encrypt"],
|
|
|
|
wrappingKey[_algorithm],
|
|
|
|
wrappingKey[_handle],
|
|
|
|
),
|
|
|
|
bytes,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw new DOMException(
|
|
|
|
"Algorithm not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param {string} format
|
|
|
|
* @param {BufferSource} wrappedKey
|
|
|
|
* @param {CryptoKey} unwrappingKey
|
|
|
|
* @param {AlgorithmIdentifier} unwrapAlgorithm
|
|
|
|
* @param {AlgorithmIdentifier} unwrappedKeyAlgorithm
|
|
|
|
* @param {boolean} extractable
|
|
|
|
* @param {KeyUsage[]} keyUsages
|
|
|
|
* @returns {Promise<CryptoKey>}
|
|
|
|
*/
|
|
|
|
async unwrapKey(
|
|
|
|
format,
|
|
|
|
wrappedKey,
|
|
|
|
unwrappingKey,
|
|
|
|
unwrapAlgorithm,
|
|
|
|
unwrappedKeyAlgorithm,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'unwrapKey' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 7, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
format = webidl.converters.KeyFormat(format, prefix, "Argument 1");
|
|
|
|
wrappedKey = webidl.converters.BufferSource(
|
|
|
|
wrappedKey,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
);
|
|
|
|
unwrappingKey = webidl.converters.CryptoKey(
|
|
|
|
unwrappingKey,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 3",
|
|
|
|
);
|
|
|
|
unwrapAlgorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
unwrapAlgorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 4",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
unwrappedKeyAlgorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
unwrappedKeyAlgorithm,
|
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 5",
|
|
|
|
);
|
|
|
|
extractable = webidl.converters.boolean(extractable, prefix, "Argument 6");
|
|
|
|
keyUsages = webidl.converters["sequence<KeyUsage>"](
|
|
|
|
keyUsages,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 7",
|
|
|
|
);
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
wrappedKey = copyBuffer(wrappedKey);
|
2021-07-12 08:45:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let normalizedAlgorithm;
|
2021-10-01 05:39:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
// 3.
|
|
|
|
normalizedAlgorithm = normalizeAlgorithm(unwrapAlgorithm, "unwrapKey");
|
|
|
|
} catch (_) {
|
|
|
|
// 4.
|
|
|
|
normalizedAlgorithm = normalizeAlgorithm(unwrapAlgorithm, "decrypt");
|
|
|
|
}
|
2021-10-01 05:39:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
|
|
|
const normalizedKeyAlgorithm = normalizeAlgorithm(
|
|
|
|
unwrappedKeyAlgorithm,
|
|
|
|
"importKey",
|
|
|
|
);
|
2021-10-01 05:39:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 11.
|
|
|
|
if (normalizedAlgorithm.name !== unwrappingKey[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Unwrapping algorithm doesn't match key algorithm.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-10-01 05:39:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 12.
|
|
|
|
if (!ArrayPrototypeIncludes(unwrappingKey[_usages], "unwrapKey")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key does not support the 'unwrapKey' operation.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-10-01 05:39:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 13.
|
|
|
|
let key;
|
|
|
|
if (
|
|
|
|
supportedAlgorithms["unwrapKey"][normalizedAlgorithm.name] !== undefined
|
|
|
|
) {
|
|
|
|
const handle = unwrappingKey[_handle];
|
|
|
|
const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
|
2021-10-01 05:39:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (normalizedAlgorithm.name) {
|
|
|
|
case "AES-KW": {
|
|
|
|
const plainText = await ops.op_crypto_unwrap_key({
|
|
|
|
key: keyData,
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
}, wrappedKey);
|
2021-10-01 05:39:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
2023-04-02 13:41:41 -04:00
|
|
|
key = TypedArrayPrototypeGetBuffer(plainText);
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new DOMException(
|
|
|
|
"Not implemented",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
2022-02-01 07:02:10 -05:00
|
|
|
}
|
2021-10-01 05:39:49 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (
|
|
|
|
supportedAlgorithms["decrypt"][normalizedAlgorithm.name] !== undefined
|
|
|
|
) {
|
|
|
|
// must construct a new key, since keyUsages is ["unwrapKey"] and not ["decrypt"]
|
|
|
|
key = await this.decrypt(
|
|
|
|
normalizedAlgorithm,
|
|
|
|
constructKey(
|
|
|
|
unwrappingKey[_type],
|
|
|
|
unwrappingKey[_extractable],
|
|
|
|
["decrypt"],
|
|
|
|
unwrappingKey[_algorithm],
|
|
|
|
unwrappingKey[_handle],
|
|
|
|
),
|
|
|
|
wrappedKey,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw new DOMException(
|
|
|
|
"Algorithm not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
2021-10-01 05:39:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let bytes;
|
|
|
|
// 14.
|
|
|
|
if (format !== "jwk") {
|
|
|
|
bytes = key;
|
|
|
|
} else {
|
|
|
|
const k = new Uint8Array(key);
|
|
|
|
let str = "";
|
|
|
|
for (let i = 0; i < k.length; i++) {
|
|
|
|
str += StringFromCharCode(k[i]);
|
2021-10-01 05:39:49 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
bytes = JSONParse(str);
|
2021-10-01 05:39:49 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
// 15.
|
|
|
|
const result = await this.importKey(
|
2021-12-04 22:55:11 -05:00
|
|
|
format,
|
2023-02-07 14:22:46 -05:00
|
|
|
bytes,
|
|
|
|
normalizedKeyAlgorithm,
|
2021-12-04 22:55:11 -05:00
|
|
|
extractable,
|
|
|
|
keyUsages,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
// 16.
|
|
|
|
if (
|
|
|
|
(result[_type] == "secret" || result[_type] == "private") &&
|
|
|
|
keyUsages.length == 0
|
2021-12-04 22:55:11 -05:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new SyntaxError("Invalid key type.");
|
|
|
|
}
|
|
|
|
// 17.
|
|
|
|
result[_extractable] = extractable;
|
|
|
|
// 18.
|
|
|
|
result[_usages] = usageIntersection(keyUsages, recognisedUsages);
|
|
|
|
// 19.
|
|
|
|
return result;
|
|
|
|
}
|
2021-12-04 22:55:11 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} algorithm
|
|
|
|
* @param {boolean} extractable
|
|
|
|
* @param {KeyUsage[]} keyUsages
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
async generateKey(algorithm, extractable, keyUsages) {
|
|
|
|
webidl.assertBranded(this, SubtleCryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'generateKey' on 'SubtleCrypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 3, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
algorithm = webidl.converters.AlgorithmIdentifier(
|
|
|
|
algorithm,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
|
|
|
extractable = webidl.converters["boolean"](
|
|
|
|
extractable,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
);
|
|
|
|
keyUsages = webidl.converters["sequence<KeyUsage>"](
|
|
|
|
keyUsages,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 3",
|
|
|
|
);
|
2021-12-04 22:55:11 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const usages = keyUsages;
|
2021-12-04 22:55:11 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const normalizedAlgorithm = normalizeAlgorithm(algorithm, "generateKey");
|
|
|
|
const result = await generateKey(
|
|
|
|
normalizedAlgorithm,
|
|
|
|
extractable,
|
|
|
|
usages,
|
|
|
|
);
|
2021-12-04 22:55:11 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, result)) {
|
|
|
|
const type = result[_type];
|
|
|
|
if ((type === "secret" || type === "private") && usages.length === 0) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-12-04 22:55:11 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, result.privateKey)
|
|
|
|
) {
|
|
|
|
if (result.privateKey[_usages].length === 0) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-12-04 22:55:11 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-04 22:55:11 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const SubtleCryptoPrototype = SubtleCrypto.prototype;
|
2022-01-10 23:44:47 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function generateKey(normalizedAlgorithm, extractable, usages) {
|
|
|
|
const algorithmName = normalizedAlgorithm.name;
|
2022-01-10 23:44:47 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (algorithmName) {
|
|
|
|
case "RSASSA-PKCS1-v1_5":
|
|
|
|
case "RSA-PSS": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
|
|
|
|
) !== undefined
|
2021-12-04 22:55:11 -05:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-12-04 22:55:11 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const keyData = await core.opAsync(
|
|
|
|
"op_crypto_generate_key",
|
|
|
|
{
|
|
|
|
algorithm: "RSA",
|
|
|
|
modulusLength: normalizedAlgorithm.modulusLength,
|
|
|
|
publicExponent: normalizedAlgorithm.publicExponent,
|
|
|
|
},
|
2021-12-04 22:55:11 -05:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "private",
|
|
|
|
data: keyData,
|
2021-07-06 08:16:04 -04:00
|
|
|
});
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-8.
|
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
|
|
|
modulusLength: normalizedAlgorithm.modulusLength,
|
|
|
|
publicExponent: normalizedAlgorithm.publicExponent,
|
|
|
|
hash: normalizedAlgorithm.hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 9-13.
|
|
|
|
const publicKey = constructKey(
|
|
|
|
"public",
|
|
|
|
true,
|
|
|
|
usageIntersection(usages, ["verify"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 14-18.
|
|
|
|
const privateKey = constructKey(
|
|
|
|
"private",
|
2021-07-06 08:16:04 -04:00
|
|
|
extractable,
|
2023-02-07 14:22:46 -05:00
|
|
|
usageIntersection(usages, ["sign"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
2021-07-06 08:16:04 -04:00
|
|
|
);
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 19-22.
|
|
|
|
return { publicKey, privateKey };
|
|
|
|
}
|
|
|
|
case "RSA-OAEP": {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes([
|
|
|
|
"encrypt",
|
|
|
|
"decrypt",
|
|
|
|
"wrapKey",
|
|
|
|
"unwrapKey",
|
|
|
|
], u),
|
|
|
|
) !== undefined
|
2022-02-01 12:06:11 -05:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const keyData = await core.opAsync(
|
|
|
|
"op_crypto_generate_key",
|
|
|
|
{
|
|
|
|
algorithm: "RSA",
|
2021-07-06 08:16:04 -04:00
|
|
|
modulusLength: normalizedAlgorithm.modulusLength,
|
|
|
|
publicExponent: normalizedAlgorithm.publicExponent,
|
2023-02-07 14:22:46 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "private",
|
|
|
|
data: keyData,
|
|
|
|
});
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-8.
|
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
|
|
|
modulusLength: normalizedAlgorithm.modulusLength,
|
|
|
|
publicExponent: normalizedAlgorithm.publicExponent,
|
|
|
|
hash: normalizedAlgorithm.hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 9-13.
|
|
|
|
const publicKey = constructKey(
|
|
|
|
"public",
|
|
|
|
true,
|
|
|
|
usageIntersection(usages, ["encrypt", "wrapKey"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 14-18.
|
|
|
|
const privateKey = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(usages, ["decrypt", "unwrapKey"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 19-22.
|
|
|
|
return { publicKey, privateKey };
|
|
|
|
}
|
|
|
|
case "ECDSA": {
|
|
|
|
const namedCurve = normalizedAlgorithm.namedCurve;
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2-3.
|
|
|
|
const handle = {};
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(
|
|
|
|
supportedNamedCurves,
|
|
|
|
namedCurve,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const keyData = await core.opAsync("op_crypto_generate_key", {
|
|
|
|
algorithm: "EC",
|
|
|
|
namedCurve,
|
|
|
|
});
|
2021-08-24 15:59:02 -04:00
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
2021-12-09 14:32:55 -05:00
|
|
|
type: "private",
|
2021-08-24 15:59:02 -04:00
|
|
|
data: keyData,
|
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
throw new DOMException("Curve not supported", "NotSupportedError");
|
|
|
|
}
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-6.
|
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
|
|
|
namedCurve,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 7-11.
|
|
|
|
const publicKey = constructKey(
|
|
|
|
"public",
|
|
|
|
true,
|
|
|
|
usageIntersection(usages, ["verify"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 12-16.
|
|
|
|
const privateKey = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(usages, ["sign"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 17-20.
|
|
|
|
return { publicKey, privateKey };
|
|
|
|
}
|
|
|
|
case "ECDH": {
|
|
|
|
const namedCurve = normalizedAlgorithm.namedCurve;
|
2021-08-24 15:59:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-08-24 15:59:02 -04:00
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2-3.
|
|
|
|
const handle = {};
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(
|
|
|
|
supportedNamedCurves,
|
2021-12-09 17:32:10 -05:00
|
|
|
namedCurve,
|
2023-02-07 14:22:46 -05:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
const keyData = await core.opAsync("op_crypto_generate_key", {
|
|
|
|
algorithm: "EC",
|
|
|
|
namedCurve,
|
|
|
|
});
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "private",
|
|
|
|
data: keyData,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new DOMException("Curve not supported", "NotSupportedError");
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-6.
|
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
|
|
|
namedCurve,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 7-11.
|
|
|
|
const publicKey = constructKey(
|
|
|
|
"public",
|
|
|
|
true,
|
|
|
|
usageIntersection(usages, []),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 12-16.
|
|
|
|
const privateKey = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(usages, ["deriveKey", "deriveBits"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 17-20.
|
|
|
|
return { publicKey, privateKey };
|
|
|
|
}
|
|
|
|
case "AES-CTR":
|
|
|
|
case "AES-CBC":
|
|
|
|
case "AES-GCM": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes([
|
|
|
|
"encrypt",
|
|
|
|
"decrypt",
|
|
|
|
"wrapKey",
|
|
|
|
"unwrapKey",
|
|
|
|
], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2021-09-13 05:35:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return generateKeyAES(normalizedAlgorithm, extractable, usages);
|
|
|
|
}
|
|
|
|
case "AES-KW": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["wrapKey", "unwrapKey"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-09-13 05:35:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return generateKeyAES(normalizedAlgorithm, extractable, usages);
|
|
|
|
}
|
|
|
|
case "X25519": {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
|
|
|
const privateKeyData = new Uint8Array(32);
|
|
|
|
const publicKeyData = new Uint8Array(32);
|
2023-05-08 17:07:45 -04:00
|
|
|
ops.op_crypto_generate_x25519_keypair(privateKeyData, publicKeyData);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, privateKeyData);
|
|
|
|
|
|
|
|
const publicHandle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, publicHandle, publicKeyData);
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
|
|
|
};
|
|
|
|
|
|
|
|
const publicKey = constructKey(
|
|
|
|
"public",
|
|
|
|
true,
|
|
|
|
usageIntersection(usages, []),
|
|
|
|
algorithm,
|
|
|
|
publicHandle,
|
|
|
|
);
|
2021-09-13 05:35:49 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const privateKey = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(usages, ["deriveKey", "deriveBits"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return { publicKey, privateKey };
|
|
|
|
}
|
|
|
|
case "Ed25519": {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const ED25519_SEED_LEN = 32;
|
|
|
|
const ED25519_PUBLIC_KEY_LEN = 32;
|
|
|
|
const privateKeyData = new Uint8Array(ED25519_SEED_LEN);
|
|
|
|
const publicKeyData = new Uint8Array(ED25519_PUBLIC_KEY_LEN);
|
|
|
|
if (
|
2023-05-08 17:07:45 -04:00
|
|
|
!ops.op_crypto_generate_ed25519_keypair(privateKeyData, publicKeyData)
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
throw new DOMException("Failed to generate key", "OperationError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, privateKeyData);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const publicHandle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, publicHandle, publicKeyData);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
|
|
|
};
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const publicKey = constructKey(
|
|
|
|
"public",
|
|
|
|
true,
|
|
|
|
usageIntersection(usages, ["verify"]),
|
|
|
|
algorithm,
|
|
|
|
publicHandle,
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const privateKey = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(usages, ["sign"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return { publicKey, privateKey };
|
|
|
|
}
|
|
|
|
case "HMAC": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
usages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
let length;
|
|
|
|
if (normalizedAlgorithm.length === undefined) {
|
|
|
|
length = null;
|
|
|
|
} else if (normalizedAlgorithm.length !== 0) {
|
|
|
|
length = normalizedAlgorithm.length;
|
|
|
|
} else {
|
|
|
|
throw new DOMException("Invalid length", "OperationError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3-4.
|
|
|
|
const keyData = await core.opAsync("op_crypto_generate_key", {
|
|
|
|
algorithm: "HMAC",
|
|
|
|
hash: normalizedAlgorithm.hash.name,
|
|
|
|
length,
|
|
|
|
});
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "secret",
|
|
|
|
data: keyData,
|
|
|
|
});
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6-10.
|
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
|
|
|
hash: {
|
|
|
|
name: normalizedAlgorithm.hash.name,
|
|
|
|
},
|
2023-04-02 13:41:41 -04:00
|
|
|
length: TypedArrayPrototypeGetByteLength(keyData) * 8,
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5, 11-13.
|
|
|
|
const key = constructKey(
|
|
|
|
"secret",
|
|
|
|
extractable,
|
|
|
|
usages,
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 14.
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function importKeyEd25519(
|
|
|
|
format,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
switch (format) {
|
|
|
|
case "raw": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["verify"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, keyData);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2-3.
|
|
|
|
const algorithm = {
|
|
|
|
name: "Ed25519",
|
|
|
|
};
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-6.
|
|
|
|
return constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "spki": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["verify"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const publicKeyData = new Uint8Array(32);
|
2023-05-08 17:07:45 -04:00
|
|
|
if (!ops.op_crypto_import_spki_ed25519(keyData, publicKeyData)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid key data", "DataError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, publicKeyData);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithm = {
|
|
|
|
name: "Ed25519",
|
|
|
|
};
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "pkcs8": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["sign"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const privateKeyData = new Uint8Array(32);
|
2023-05-08 17:07:45 -04:00
|
|
|
if (!ops.op_crypto_import_pkcs8_ed25519(keyData, privateKeyData)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid key data", "DataError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, privateKeyData);
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: "Ed25519",
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "jwk": {
|
|
|
|
// 1.
|
|
|
|
const jwk = keyData;
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (jwk.d !== undefined) {
|
2022-09-27 08:13:42 -04:00
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
2023-02-07 14:22:46 -05:00
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
["sign"],
|
|
|
|
u,
|
|
|
|
),
|
2022-09-27 08:13:42 -04:00
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2022-09-27 08:13:42 -04:00
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
2023-02-07 14:22:46 -05:00
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
["verify"],
|
|
|
|
u,
|
|
|
|
),
|
2022-09-27 08:13:42 -04:00
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
if (jwk.kty !== "OKP") {
|
|
|
|
throw new DOMException("Invalid key type", "DataError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
if (jwk.crv !== "Ed25519") {
|
|
|
|
throw new DOMException("Invalid curve", "DataError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
if (
|
|
|
|
keyUsages.length > 0 && jwk.use !== undefined && jwk.use !== "sig"
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usage", "DataError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-08-31 08:56:26 -04:00
|
|
|
// 6.
|
2023-02-07 14:22:46 -05:00
|
|
|
if (jwk.key_ops !== undefined) {
|
2022-09-27 08:13:42 -04:00
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.key_ops,
|
|
|
|
(u) => !ArrayPrototypeIncludes(recognisedUsages, u),
|
2022-09-27 08:13:42 -04:00
|
|
|
) !== undefined
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
|
|
|
"DataError",
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (
|
|
|
|
!ArrayPrototypeEvery(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => ArrayPrototypeIncludes(keyUsages, u),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
|
|
|
"DataError",
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-08-31 08:56:26 -04:00
|
|
|
// 7.
|
2023-02-07 14:22:46 -05:00
|
|
|
if (jwk.ext !== undefined && jwk.ext === false && extractable) {
|
|
|
|
throw new DOMException("Invalid key extractability", "DataError");
|
|
|
|
}
|
|
|
|
|
2023-08-31 08:56:26 -04:00
|
|
|
// 8.
|
2023-02-07 14:22:46 -05:00
|
|
|
if (jwk.d !== undefined) {
|
|
|
|
// https://www.rfc-editor.org/rfc/rfc8037#section-2
|
2023-06-05 08:52:02 -04:00
|
|
|
let privateKeyData;
|
|
|
|
try {
|
|
|
|
privateKeyData = ops.op_crypto_base64url_decode(jwk.d);
|
|
|
|
} catch (_) {
|
|
|
|
throw new DOMException("invalid private key data", "DataError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, privateKeyData);
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: "Ed25519",
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
2022-10-04 02:06:25 -04:00
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
2022-09-27 08:13:42 -04:00
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
// https://www.rfc-editor.org/rfc/rfc8037#section-2
|
2023-06-05 08:52:02 -04:00
|
|
|
let publicKeyData;
|
|
|
|
try {
|
|
|
|
publicKeyData = ops.op_crypto_base64url_decode(jwk.x);
|
|
|
|
} catch (_) {
|
|
|
|
throw new DOMException("invalid public key data", "DataError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
|
|
|
const handle = {};
|
2023-02-07 14:22:46 -05:00
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, publicKeyData);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
|
|
|
const algorithm = {
|
2023-02-07 14:22:46 -05:00
|
|
|
name: "Ed25519",
|
2022-09-27 08:13:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return constructKey(
|
|
|
|
"public",
|
2021-09-13 05:35:49 -04:00
|
|
|
extractable,
|
2023-02-07 14:22:46 -05:00
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
2021-09-13 05:35:49 -04:00
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function importKeyX25519(
|
|
|
|
format,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
switch (format) {
|
|
|
|
case "raw": {
|
|
|
|
// 1.
|
|
|
|
if (keyUsages.length > 0) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-08-31 05:25:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, keyData);
|
2021-08-31 05:25:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2-3.
|
|
|
|
const algorithm = {
|
|
|
|
name: "X25519",
|
|
|
|
};
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-6.
|
|
|
|
return constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
[],
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "spki": {
|
|
|
|
// 1.
|
|
|
|
if (keyUsages.length > 0) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const publicKeyData = new Uint8Array(32);
|
2023-05-08 17:07:45 -04:00
|
|
|
if (!ops.op_crypto_import_spki_x25519(keyData, publicKeyData)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid key data", "DataError");
|
2021-08-31 05:25:44 -04:00
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, publicKeyData);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithm = {
|
|
|
|
name: "X25519",
|
|
|
|
};
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
[],
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "pkcs8": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const privateKeyData = new Uint8Array(32);
|
2023-05-08 17:07:45 -04:00
|
|
|
if (!ops.op_crypto_import_pkcs8_x25519(keyData, privateKeyData)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid key data", "DataError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, privateKeyData);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithm = {
|
|
|
|
name: "X25519",
|
|
|
|
};
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
// 1.
|
|
|
|
const jwk = keyData;
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (jwk.d !== undefined) {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
["deriveKey", "deriveBits"],
|
|
|
|
u,
|
|
|
|
),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
if (jwk.d === undefined && keyUsages.length > 0) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
if (jwk.kty !== "OKP") {
|
|
|
|
throw new DOMException("Invalid key type", "DataError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
if (jwk.crv !== "X25519") {
|
|
|
|
throw new DOMException("Invalid curve", "DataError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
|
|
|
if (keyUsages.length > 0 && jwk.use !== undefined) {
|
|
|
|
if (jwk.use !== "enc") {
|
|
|
|
throw new DOMException("Invalid key use", "DataError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 7.
|
|
|
|
if (jwk.key_ops !== undefined) {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => !ArrayPrototypeIncludes(recognisedUsages, u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
|
|
|
"DataError",
|
2022-09-27 08:13:42 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!ArrayPrototypeEvery(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => ArrayPrototypeIncludes(keyUsages, u),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
|
|
|
"DataError",
|
2022-09-27 08:13:42 -04:00
|
|
|
);
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
|
|
|
if (jwk.ext !== undefined && jwk.ext === false && extractable) {
|
|
|
|
throw new DOMException("Invalid key extractability", "DataError");
|
2021-11-28 10:48:49 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
|
|
|
if (jwk.d !== undefined) {
|
|
|
|
// https://www.rfc-editor.org/rfc/rfc8037#section-2
|
|
|
|
const privateKeyData = ops.op_crypto_base64url_decode(jwk.d);
|
2022-01-04 19:00:37 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, privateKeyData);
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithm = {
|
|
|
|
name: "X25519",
|
|
|
|
};
|
2022-01-04 19:00:37 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, ["deriveKey", "deriveBits"]),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// https://www.rfc-editor.org/rfc/rfc8037#section-2
|
|
|
|
const publicKeyData = ops.op_crypto_base64url_decode(jwk.x);
|
2022-01-04 19:00:37 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, publicKeyData);
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: "X25519",
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
[],
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-11-28 10:48:49 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
2021-11-28 10:48:49 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function exportKeyAES(
|
|
|
|
format,
|
|
|
|
key,
|
|
|
|
innerKey,
|
|
|
|
) {
|
|
|
|
switch (format) {
|
|
|
|
// 2.
|
|
|
|
case "raw": {
|
|
|
|
// 1.
|
|
|
|
const data = innerKey.data;
|
|
|
|
// 2.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(data);
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "jwk": {
|
|
|
|
// 1-2.
|
|
|
|
const jwk = {
|
|
|
|
kty: "oct",
|
|
|
|
};
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
format: "jwksecret",
|
|
|
|
algorithm: "AES",
|
|
|
|
}, innerKey);
|
|
|
|
ObjectAssign(jwk, data);
|
|
|
|
|
|
|
|
// 4.
|
|
|
|
const algorithm = key[_algorithm];
|
|
|
|
switch (algorithm.length) {
|
|
|
|
case 128:
|
|
|
|
jwk.alg = aesJwkAlg[algorithm.name][128];
|
|
|
|
break;
|
|
|
|
case 192:
|
|
|
|
jwk.alg = aesJwkAlg[algorithm.name][192];
|
|
|
|
break;
|
|
|
|
case 256:
|
|
|
|
jwk.alg = aesJwkAlg[algorithm.name][256];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Invalid key length",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-09 17:32:10 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
jwk.key_ops = key.usages;
|
2021-12-09 17:32:10 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
|
|
|
jwk.ext = key[_extractable];
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 7.
|
|
|
|
return jwk;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function importKeyAES(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
supportedKeyUsages,
|
|
|
|
) {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(supportedKeyUsages, u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-12-15 09:46:29 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithmName = normalizedAlgorithm.name;
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
let data = keyData;
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (format) {
|
|
|
|
case "raw": {
|
|
|
|
// 2.
|
|
|
|
if (
|
2023-04-02 13:41:41 -04:00
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
[128, 192, 256],
|
|
|
|
TypedArrayPrototypeGetByteLength(keyData) * 8,
|
|
|
|
)
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
2023-06-26 09:10:27 -04:00
|
|
|
throw new DOMException("Invalid key length", "DataError");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
// 1.
|
|
|
|
const jwk = keyData;
|
|
|
|
|
|
|
|
// 2.
|
|
|
|
if (jwk.kty !== "oct") {
|
|
|
|
throw new DOMException(
|
|
|
|
"'kty' property of JsonWebKey must be 'oct'",
|
|
|
|
"DataError",
|
2021-12-10 16:23:19 -05:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Section 6.4.1 of RFC7518
|
|
|
|
if (jwk.k === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'k' property of JsonWebKey must be present",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
const { rawData } = ops.op_crypto_import_key(
|
|
|
|
{ algorithm: "AES" },
|
|
|
|
{ jwkSecret: jwk },
|
|
|
|
);
|
|
|
|
data = rawData.data;
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
2023-04-02 13:41:41 -04:00
|
|
|
switch (TypedArrayPrototypeGetByteLength(data) * 8) {
|
2023-02-07 14:22:46 -05:00
|
|
|
case 128:
|
2021-11-28 10:48:49 -05:00
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.alg !== undefined &&
|
|
|
|
jwk.alg !== aesJwkAlg[algorithmName][128]
|
2021-11-28 10:48:49 -05:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid algorithm", "DataError");
|
2021-11-28 10:48:49 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
case 192:
|
2021-11-28 10:48:49 -05:00
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.alg !== undefined &&
|
|
|
|
jwk.alg !== aesJwkAlg[algorithmName][192]
|
2021-11-28 10:48:49 -05:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid algorithm", "DataError");
|
2021-11-28 10:48:49 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
case 256:
|
|
|
|
if (
|
|
|
|
jwk.alg !== undefined &&
|
|
|
|
jwk.alg !== aesJwkAlg[algorithmName][256]
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid algorithm", "DataError");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Invalid key length",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
|
|
|
if (
|
|
|
|
keyUsages.length > 0 && jwk.use !== undefined && jwk.use !== "enc"
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "DataError");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 7.
|
|
|
|
// Section 4.3 of RFC7517
|
|
|
|
if (jwk.key_ops !== undefined) {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => !ArrayPrototypeIncludes(recognisedUsages, u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
2021-11-28 10:48:49 -05:00
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
2021-11-28 10:48:49 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (
|
|
|
|
!ArrayPrototypeEvery(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => ArrayPrototypeIncludes(keyUsages, u),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-11-28 10:48:49 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
|
|
|
if (jwk.ext === false && extractable === true) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'ext' property of JsonWebKey must not be false if extractable is true",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "secret",
|
|
|
|
data,
|
|
|
|
});
|
2021-11-28 10:48:49 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-7.
|
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
2023-04-02 13:41:41 -04:00
|
|
|
length: TypedArrayPrototypeGetByteLength(data) * 8,
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const key = constructKey(
|
|
|
|
"secret",
|
2021-12-09 10:20:14 -05:00
|
|
|
extractable,
|
2023-02-07 14:22:46 -05:00
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 8.
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
function importKeyHMAC(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
// 2.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["sign", "verify"], u),
|
|
|
|
) !== undefined
|
2021-12-09 10:20:14 -05:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
let hash;
|
|
|
|
let data;
|
2021-12-10 16:23:19 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4. https://w3c.github.io/webcrypto/#hmac-operations
|
|
|
|
switch (format) {
|
|
|
|
case "raw": {
|
|
|
|
data = keyData;
|
|
|
|
hash = normalizedAlgorithm.hash;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
const jwk = keyData;
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (jwk.kty !== "oct") {
|
|
|
|
throw new DOMException(
|
|
|
|
"'kty' property of JsonWebKey must be 'oct'",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Section 6.4.1 of RFC7518
|
|
|
|
if (jwk.k === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'k' property of JsonWebKey must be present",
|
|
|
|
"DataError",
|
2021-12-10 16:23:19 -05:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-09 17:32:10 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
const { rawData } = ops.op_crypto_import_key(
|
|
|
|
{ algorithm: "HMAC" },
|
|
|
|
{ jwkSecret: jwk },
|
|
|
|
);
|
|
|
|
data = rawData.data;
|
2021-12-09 17:32:10 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
hash = normalizedAlgorithm.hash;
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
|
|
|
switch (hash.name) {
|
|
|
|
case "SHA-1": {
|
|
|
|
if (jwk.alg !== undefined && jwk.alg !== "HS1") {
|
|
|
|
throw new DOMException(
|
|
|
|
"'alg' property of JsonWebKey must be 'HS1'",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "SHA-256": {
|
|
|
|
if (jwk.alg !== undefined && jwk.alg !== "HS256") {
|
2021-12-09 10:20:14 -05:00
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'alg' property of JsonWebKey must be 'HS256'",
|
2021-12-09 10:20:14 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "SHA-384": {
|
|
|
|
if (jwk.alg !== undefined && jwk.alg !== "HS384") {
|
2021-12-09 10:20:14 -05:00
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'alg' property of JsonWebKey must be 'HS384'",
|
2021-12-09 10:20:14 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "SHA-512": {
|
|
|
|
if (jwk.alg !== undefined && jwk.alg !== "HS512") {
|
|
|
|
throw new DOMException(
|
|
|
|
"'alg' property of JsonWebKey must be 'HS512'",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new TypeError("unreachable");
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 7.
|
2021-12-09 10:20:14 -05:00
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
keyUsages.length > 0 && jwk.use !== undefined && jwk.use !== "sig"
|
2021-12-09 10:20:14 -05:00
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'use' property of JsonWebKey must be 'sig'",
|
2021-12-09 10:20:14 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
|
|
|
// Section 4.3 of RFC7517
|
|
|
|
if (jwk.key_ops !== undefined) {
|
2021-12-09 10:20:14 -05:00
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypeFind(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => !ArrayPrototypeIncludes(recognisedUsages, u),
|
|
|
|
) !== undefined
|
2021-12-09 10:20:14 -05:00
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
2021-12-09 10:20:14 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
!ArrayPrototypeEvery(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => ArrayPrototypeIncludes(keyUsages, u),
|
|
|
|
)
|
2021-12-09 10:20:14 -05:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
|
|
|
"DataError",
|
|
|
|
);
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
|
|
|
if (jwk.ext === false && extractable === true) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'ext' property of JsonWebKey must not be false if extractable is true",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
2023-04-02 13:41:41 -04:00
|
|
|
let length = TypedArrayPrototypeGetByteLength(data) * 8;
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
|
|
|
if (length === 0) {
|
|
|
|
throw new DOMException("Key length is zero", "DataError");
|
|
|
|
}
|
|
|
|
// 7.
|
|
|
|
if (normalizedAlgorithm.length !== undefined) {
|
|
|
|
if (
|
|
|
|
normalizedAlgorithm.length > length ||
|
|
|
|
normalizedAlgorithm.length <= (length - 8)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key length is invalid",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
length = normalizedAlgorithm.length;
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "secret",
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: "HMAC",
|
|
|
|
length,
|
|
|
|
hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
const key = constructKey(
|
|
|
|
"secret",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
function importKeyEC(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
const supportedUsages = SUPPORTED_KEY_USAGES[normalizedAlgorithm.name];
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case "raw": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
supportedNamedCurves,
|
|
|
|
normalizedAlgorithm.namedCurve,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Invalid namedCurve",
|
|
|
|
"DataError",
|
2021-12-16 11:28:43 -05:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].public,
|
|
|
|
u,
|
|
|
|
),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-12-16 11:28:43 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
// 3.
|
|
|
|
const { rawData } = ops.op_crypto_import_key({
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
|
|
|
}, { raw: keyData });
|
|
|
|
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
|
|
|
|
|
|
|
// 4-5.
|
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 6-8.
|
|
|
|
const key = constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
case "pkcs8": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].private,
|
|
|
|
u,
|
|
|
|
),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2-9.
|
|
|
|
const { rawData } = ops.op_crypto_import_key({
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
|
|
|
}, { pkcs8: keyData });
|
|
|
|
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
|
|
|
};
|
|
|
|
|
|
|
|
const key = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
case "spki": {
|
|
|
|
// 1.
|
|
|
|
if (normalizedAlgorithm.name == "ECDSA") {
|
2021-12-16 11:28:43 -05:00
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
2022-01-19 10:20:28 -05:00
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
2023-02-07 14:22:46 -05:00
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].public,
|
2022-01-19 10:20:28 -05:00
|
|
|
u,
|
|
|
|
),
|
2021-12-16 11:28:43 -05:00
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (keyUsages.length != 0) {
|
|
|
|
throw new DOMException("Key usage must be empty", "SyntaxError");
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2-12
|
|
|
|
const { rawData } = ops.op_crypto_import_key({
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
|
|
|
}, { spki: keyData });
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
|
|
|
};
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6-8.
|
|
|
|
const key = constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
const jwk = keyData;
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const keyType = (jwk.d !== undefined) ? "private" : "public";
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(supportedUsages[keyType], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
if (jwk.kty !== "EC") {
|
|
|
|
throw new DOMException(
|
|
|
|
"'kty' property of JsonWebKey must be 'EC'",
|
|
|
|
"DataError",
|
2021-12-09 10:20:14 -05:00
|
|
|
);
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
if (
|
|
|
|
keyUsages.length > 0 && jwk.use !== undefined &&
|
|
|
|
jwk.use !== supportedUsages.jwkUse
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
`'use' property of JsonWebKey must be '${supportedUsages.jwkUse}'`,
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
// Section 4.3 of RFC7517
|
|
|
|
if (jwk.key_ops !== undefined) {
|
2021-12-16 11:28:43 -05:00
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.key_ops,
|
|
|
|
(u) => !ArrayPrototypeIncludes(recognisedUsages, u),
|
2021-12-16 11:28:43 -05:00
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'key_ops' member of JsonWebKey is invalid",
|
2021-12-16 11:28:43 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
!ArrayPrototypeEvery(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => ArrayPrototypeIncludes(keyUsages, u),
|
|
|
|
)
|
2021-12-16 11:28:43 -05:00
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'key_ops' member of JsonWebKey is invalid",
|
2021-12-16 11:28:43 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
|
|
|
if (jwk.ext === false && extractable === true) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'ext' property of JsonWebKey must not be false if extractable is true",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
|
|
|
if (jwk.alg !== undefined && normalizedAlgorithm.name == "ECDSA") {
|
|
|
|
let algNamedCurve;
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (jwk.alg) {
|
|
|
|
case "ES256": {
|
|
|
|
algNamedCurve = "P-256";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ES384": {
|
|
|
|
algNamedCurve = "P-384";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ES512": {
|
|
|
|
algNamedCurve = "P-521";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Curve algorithm not supported",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (algNamedCurve) {
|
|
|
|
if (algNamedCurve !== normalizedAlgorithm.namedCurve) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Mismatched curve algorithm",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-09 14:32:55 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Validate that this is a valid public key.
|
|
|
|
if (jwk.x === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'x' property of JsonWebKey is required for EC keys",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (jwk.y === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'y' property of JsonWebKey is required for EC keys",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (jwk.d !== undefined) {
|
|
|
|
// it's also a Private key
|
|
|
|
const { rawData } = ops.op_crypto_import_key({
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
|
|
|
}, { jwkPrivateEc: jwk });
|
2021-12-09 10:20:14 -05:00
|
|
|
|
|
|
|
const handle = {};
|
2021-12-10 09:06:03 -05:00
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
2021-12-09 10:20:14 -05:00
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
2023-02-07 14:22:46 -05:00
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
2021-12-09 10:20:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const key = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
return key;
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
const { rawData } = ops.op_crypto_import_key({
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
|
|
|
}, { jwkPublicEc: jwk });
|
2021-12-09 14:32:55 -05:00
|
|
|
|
|
|
|
const handle = {};
|
2021-12-10 09:06:03 -05:00
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
2021-12-09 14:32:55 -05:00
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
2023-02-07 14:22:46 -05:00
|
|
|
namedCurve: normalizedAlgorithm.namedCurve,
|
2021-12-09 14:32:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const key = constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SUPPORTED_KEY_USAGES = {
|
|
|
|
"RSASSA-PKCS1-v1_5": {
|
|
|
|
public: ["verify"],
|
|
|
|
private: ["sign"],
|
|
|
|
jwkUse: "sig",
|
|
|
|
},
|
|
|
|
"RSA-PSS": {
|
|
|
|
public: ["verify"],
|
|
|
|
private: ["sign"],
|
|
|
|
jwkUse: "sig",
|
|
|
|
},
|
|
|
|
"RSA-OAEP": {
|
|
|
|
public: ["encrypt", "wrapKey"],
|
|
|
|
private: ["decrypt", "unwrapKey"],
|
|
|
|
jwkUse: "enc",
|
|
|
|
},
|
|
|
|
"ECDSA": {
|
|
|
|
public: ["verify"],
|
|
|
|
private: ["sign"],
|
|
|
|
jwkUse: "sig",
|
|
|
|
},
|
|
|
|
"ECDH": {
|
|
|
|
public: [],
|
|
|
|
private: ["deriveKey", "deriveBits"],
|
|
|
|
jwkUse: "enc",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function importKeyRSA(
|
|
|
|
format,
|
|
|
|
normalizedAlgorithm,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
switch (format) {
|
|
|
|
case "pkcs8": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].private,
|
|
|
|
u,
|
|
|
|
),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-12-14 06:25:07 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2-9.
|
|
|
|
const { modulusLength, publicExponent, rawData } = ops
|
|
|
|
.op_crypto_import_key(
|
|
|
|
{
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
// Needed to perform step 7 without normalization.
|
|
|
|
hash: normalizedAlgorithm.hash.name,
|
|
|
|
},
|
|
|
|
{ pkcs8: keyData },
|
|
|
|
);
|
|
|
|
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
|
|
|
modulusLength,
|
|
|
|
publicExponent,
|
|
|
|
hash: normalizedAlgorithm.hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
const key = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
case "spki": {
|
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].public,
|
|
|
|
u,
|
|
|
|
),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2-9.
|
|
|
|
const { modulusLength, publicExponent, rawData } = ops
|
|
|
|
.op_crypto_import_key(
|
|
|
|
{
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
// Needed to perform step 7 without normalization.
|
|
|
|
hash: normalizedAlgorithm.hash.name,
|
|
|
|
},
|
|
|
|
{ spki: keyData },
|
|
|
|
);
|
|
|
|
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
|
|
|
modulusLength,
|
|
|
|
publicExponent,
|
|
|
|
hash: normalizedAlgorithm.hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
const key = constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
// 1.
|
|
|
|
const jwk = keyData;
|
|
|
|
|
|
|
|
// 2.
|
|
|
|
if (jwk.d !== undefined) {
|
|
|
|
if (
|
2021-12-15 09:46:29 -05:00
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
2023-02-07 14:22:46 -05:00
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].private,
|
2021-12-15 09:46:29 -05:00
|
|
|
u,
|
|
|
|
),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
2021-12-14 06:25:07 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) =>
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].public,
|
|
|
|
u,
|
|
|
|
),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-12-14 06:25:07 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
if (StringPrototypeToUpperCase(jwk.kty) !== "RSA") {
|
|
|
|
throw new DOMException(
|
|
|
|
"'kty' property of JsonWebKey must be 'RSA'",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4.
|
|
|
|
if (
|
|
|
|
keyUsages.length > 0 && jwk.use !== undefined &&
|
|
|
|
StringPrototypeToLowerCase(jwk.use) !==
|
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].jwkUse
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
`'use' property of JsonWebKey must be '${
|
|
|
|
SUPPORTED_KEY_USAGES[normalizedAlgorithm.name].jwkUse
|
|
|
|
}'`,
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5.
|
|
|
|
if (jwk.key_ops !== undefined) {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => !ArrayPrototypeIncludes(recognisedUsages, u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
2021-12-14 06:25:07 -05:00
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
2021-12-14 06:25:07 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
!ArrayPrototypeEvery(
|
|
|
|
jwk.key_ops,
|
|
|
|
(u) => ArrayPrototypeIncludes(keyUsages, u),
|
|
|
|
)
|
2021-12-14 06:25:07 -05:00
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'key_ops' property of JsonWebKey is invalid",
|
2021-12-14 06:25:07 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-14 06:25:07 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (jwk.ext === false && extractable === true) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'ext' property of JsonWebKey must not be false if extractable is true",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 7.
|
|
|
|
let hash;
|
|
|
|
|
|
|
|
// 8.
|
|
|
|
if (normalizedAlgorithm.name === "RSASSA-PKCS1-v1_5") {
|
|
|
|
switch (jwk.alg) {
|
|
|
|
case undefined:
|
|
|
|
hash = undefined;
|
|
|
|
break;
|
|
|
|
case "RS1":
|
|
|
|
hash = "SHA-1";
|
|
|
|
break;
|
|
|
|
case "RS256":
|
|
|
|
hash = "SHA-256";
|
|
|
|
break;
|
|
|
|
case "RS384":
|
|
|
|
hash = "SHA-384";
|
|
|
|
break;
|
|
|
|
case "RS512":
|
|
|
|
hash = "SHA-512";
|
|
|
|
break;
|
|
|
|
default:
|
2021-12-14 06:25:07 -05:00
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
`'alg' property of JsonWebKey must be one of 'RS1', 'RS256', 'RS384', 'RS512'`,
|
2021-12-14 06:25:07 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
} else if (normalizedAlgorithm.name === "RSA-PSS") {
|
|
|
|
switch (jwk.alg) {
|
|
|
|
case undefined:
|
|
|
|
hash = undefined;
|
|
|
|
break;
|
|
|
|
case "PS1":
|
|
|
|
hash = "SHA-1";
|
|
|
|
break;
|
|
|
|
case "PS256":
|
|
|
|
hash = "SHA-256";
|
|
|
|
break;
|
|
|
|
case "PS384":
|
|
|
|
hash = "SHA-384";
|
|
|
|
break;
|
|
|
|
case "PS512":
|
|
|
|
hash = "SHA-512";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
`'alg' property of JsonWebKey must be one of 'PS1', 'PS256', 'PS384', 'PS512'`,
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (jwk.alg) {
|
|
|
|
case undefined:
|
|
|
|
hash = undefined;
|
|
|
|
break;
|
|
|
|
case "RSA-OAEP":
|
|
|
|
hash = "SHA-1";
|
|
|
|
break;
|
|
|
|
case "RSA-OAEP-256":
|
|
|
|
hash = "SHA-256";
|
|
|
|
break;
|
|
|
|
case "RSA-OAEP-384":
|
|
|
|
hash = "SHA-384";
|
|
|
|
break;
|
|
|
|
case "RSA-OAEP-512":
|
|
|
|
hash = "SHA-512";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
`'alg' property of JsonWebKey must be one of 'RSA-OAEP', 'RSA-OAEP-256', 'RSA-OAEP-384', or 'RSA-OAEP-512'`,
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 06:25:07 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9.
|
|
|
|
if (hash !== undefined) {
|
|
|
|
// 9.1.
|
|
|
|
const normalizedHash = normalizeAlgorithm(hash, "digest");
|
|
|
|
|
|
|
|
// 9.2.
|
|
|
|
if (normalizedHash.name !== normalizedAlgorithm.hash.name) {
|
|
|
|
throw new DOMException(
|
|
|
|
`'alg' property of JsonWebKey must be '${normalizedAlgorithm.name}'`,
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10.
|
|
|
|
if (jwk.d !== undefined) {
|
|
|
|
// Private key
|
|
|
|
const optimizationsPresent = jwk.p !== undefined ||
|
|
|
|
jwk.q !== undefined || jwk.dp !== undefined ||
|
|
|
|
jwk.dq !== undefined || jwk.qi !== undefined;
|
|
|
|
if (optimizationsPresent) {
|
|
|
|
if (jwk.q === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'q' property of JsonWebKey is required for private keys",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (jwk.dp === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'dp' property of JsonWebKey is required for private keys",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (jwk.dq === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'dq' property of JsonWebKey is required for private keys",
|
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (jwk.qi === undefined) {
|
2021-12-14 06:25:07 -05:00
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'qi' property of JsonWebKey is required for private keys",
|
2021-12-14 06:25:07 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (jwk.oth !== undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'oth' property of JsonWebKey is not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new DOMException(
|
|
|
|
"only optimized private keys are supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
2021-12-14 06:25:07 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const { modulusLength, publicExponent, rawData } = ops
|
|
|
|
.op_crypto_import_key(
|
|
|
|
{
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
hash: normalizedAlgorithm.hash.name,
|
|
|
|
},
|
|
|
|
{ jwkPrivateRsa: jwk },
|
|
|
|
);
|
|
|
|
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
|
|
|
|
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
|
|
|
modulusLength,
|
|
|
|
publicExponent,
|
|
|
|
hash: normalizedAlgorithm.hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
const key = constructKey(
|
|
|
|
"private",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
} else {
|
|
|
|
// Validate that this is a valid public key.
|
|
|
|
if (jwk.n === undefined) {
|
2021-12-14 06:25:07 -05:00
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"'n' property of JsonWebKey is required for public keys",
|
2021-12-14 06:25:07 -05:00
|
|
|
"DataError",
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (jwk.e === undefined) {
|
|
|
|
throw new DOMException(
|
|
|
|
"'e' property of JsonWebKey is required for public keys",
|
|
|
|
"DataError",
|
|
|
|
);
|
2021-12-14 06:25:07 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const { modulusLength, publicExponent, rawData } = ops
|
|
|
|
.op_crypto_import_key(
|
|
|
|
{
|
|
|
|
algorithm: normalizedAlgorithm.name,
|
|
|
|
hash: normalizedAlgorithm.hash.name,
|
|
|
|
},
|
|
|
|
{ jwkPublicRsa: jwk },
|
2021-12-14 06:25:07 -05:00
|
|
|
);
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, rawData);
|
2021-12-14 06:25:07 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const algorithm = {
|
|
|
|
name: normalizedAlgorithm.name,
|
|
|
|
modulusLength,
|
|
|
|
publicExponent,
|
|
|
|
hash: normalizedAlgorithm.hash,
|
|
|
|
};
|
2021-12-14 06:25:07 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const key = constructKey(
|
|
|
|
"public",
|
|
|
|
extractable,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
2021-12-14 06:25:07 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return key;
|
2021-12-14 06:25:07 -05:00
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function importKeyHKDF(
|
|
|
|
format,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
if (format !== "raw") {
|
|
|
|
throw new DOMException("Format not supported", "NotSupportedError");
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
|
|
|
|
) !== undefined
|
2021-12-09 10:20:14 -05:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (extractable !== false) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key must not be extractable",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "secret",
|
|
|
|
data: keyData,
|
|
|
|
});
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-8.
|
|
|
|
const algorithm = {
|
|
|
|
name: "HKDF",
|
|
|
|
};
|
|
|
|
const key = constructKey(
|
|
|
|
"secret",
|
|
|
|
false,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 9.
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
function importKeyPBKDF2(
|
|
|
|
format,
|
|
|
|
keyData,
|
|
|
|
extractable,
|
|
|
|
keyUsages,
|
|
|
|
) {
|
|
|
|
// 1.
|
|
|
|
if (format !== "raw") {
|
|
|
|
throw new DOMException("Format not supported", "NotSupportedError");
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeFind(
|
|
|
|
keyUsages,
|
|
|
|
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
|
|
|
|
) !== undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid key usages", "SyntaxError");
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
if (extractable !== false) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key must not be extractable",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "secret",
|
|
|
|
data: keyData,
|
|
|
|
});
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5-9.
|
|
|
|
const algorithm = {
|
|
|
|
name: "PBKDF2",
|
|
|
|
};
|
|
|
|
const key = constructKey(
|
|
|
|
"secret",
|
|
|
|
false,
|
|
|
|
usageIntersection(keyUsages, recognisedUsages),
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 10.
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
function exportKeyHMAC(format, key, innerKey) {
|
|
|
|
// 1.
|
|
|
|
if (innerKey == null) {
|
|
|
|
throw new DOMException("Key is not available", "OperationError");
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (format) {
|
2021-12-09 10:20:14 -05:00
|
|
|
// 3.
|
2023-02-07 14:22:46 -05:00
|
|
|
case "raw": {
|
|
|
|
const bits = innerKey.data;
|
2023-04-02 13:41:41 -04:00
|
|
|
// TODO(petamoriken): Uint8Array doesn't have push method
|
|
|
|
// for (let _i = 7 & (8 - bits.length % 8); _i > 0; _i--) {
|
|
|
|
// bits.push(0);
|
|
|
|
// }
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4-5.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(bits);
|
2021-12-09 10:20:14 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "jwk": {
|
|
|
|
// 1-2.
|
|
|
|
const jwk = {
|
|
|
|
kty: "oct",
|
|
|
|
};
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
format: "jwksecret",
|
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
}, innerKey);
|
|
|
|
jwk.k = data.k;
|
|
|
|
|
|
|
|
// 4.
|
|
|
|
const algorithm = key[_algorithm];
|
|
|
|
// 5.
|
|
|
|
const hash = algorithm.hash;
|
|
|
|
// 6.
|
|
|
|
switch (hash.name) {
|
|
|
|
case "SHA-1":
|
|
|
|
jwk.alg = "HS1";
|
|
|
|
break;
|
|
|
|
case "SHA-256":
|
|
|
|
jwk.alg = "HS256";
|
|
|
|
break;
|
|
|
|
case "SHA-384":
|
|
|
|
jwk.alg = "HS384";
|
|
|
|
break;
|
|
|
|
case "SHA-512":
|
|
|
|
jwk.alg = "HS512";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Hash algorithm not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// 7.
|
|
|
|
jwk.key_ops = key.usages;
|
|
|
|
// 8.
|
|
|
|
jwk.ext = key[_extractable];
|
|
|
|
// 9.
|
|
|
|
return jwk;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function exportKeyRSA(format, key, innerKey) {
|
|
|
|
switch (format) {
|
|
|
|
case "pkcs8": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a private key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
format: "pkcs8",
|
|
|
|
}, innerKey);
|
2021-12-09 10:20:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(data);
|
2021-12-09 10:47:17 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "spki": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2.
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
format: "spki",
|
|
|
|
}, innerKey);
|
2021-12-09 10:47:17 -05:00
|
|
|
|
|
|
|
// 3.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(data);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
// 1-2.
|
|
|
|
const jwk = {
|
|
|
|
kty: "RSA",
|
|
|
|
};
|
2022-01-04 19:00:37 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const hash = key[_algorithm].hash.name;
|
2022-01-04 19:00:37 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
if (key[_algorithm].name === "RSASSA-PKCS1-v1_5") {
|
|
|
|
switch (hash) {
|
2021-12-09 10:47:17 -05:00
|
|
|
case "SHA-1":
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.alg = "RS1";
|
2021-12-09 10:47:17 -05:00
|
|
|
break;
|
|
|
|
case "SHA-256":
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.alg = "RS256";
|
2021-12-09 10:47:17 -05:00
|
|
|
break;
|
|
|
|
case "SHA-384":
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.alg = "RS384";
|
2021-12-09 10:47:17 -05:00
|
|
|
break;
|
|
|
|
case "SHA-512":
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.alg = "RS512";
|
2021-12-09 10:47:17 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Hash algorithm not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (key[_algorithm].name === "RSA-PSS") {
|
|
|
|
switch (hash) {
|
|
|
|
case "SHA-1":
|
|
|
|
jwk.alg = "PS1";
|
|
|
|
break;
|
|
|
|
case "SHA-256":
|
|
|
|
jwk.alg = "PS256";
|
|
|
|
break;
|
|
|
|
case "SHA-384":
|
|
|
|
jwk.alg = "PS384";
|
|
|
|
break;
|
|
|
|
case "SHA-512":
|
|
|
|
jwk.alg = "PS512";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Hash algorithm not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
2021-12-09 10:47:17 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
switch (hash) {
|
|
|
|
case "SHA-1":
|
|
|
|
jwk.alg = "RSA-OAEP";
|
|
|
|
break;
|
|
|
|
case "SHA-256":
|
|
|
|
jwk.alg = "RSA-OAEP-256";
|
|
|
|
break;
|
|
|
|
case "SHA-384":
|
|
|
|
jwk.alg = "RSA-OAEP-384";
|
|
|
|
break;
|
|
|
|
case "SHA-512":
|
|
|
|
jwk.alg = "RSA-OAEP-512";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Hash algorithm not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
2021-12-09 10:47:17 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-09 10:47:17 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5-6.
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
format: key[_type] === "private" ? "jwkprivate" : "jwkpublic",
|
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
}, innerKey);
|
|
|
|
ObjectAssign(jwk, data);
|
2021-12-09 10:47:17 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 7.
|
|
|
|
jwk.key_ops = key.usages;
|
2021-12-14 11:02:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
|
|
|
jwk.ext = key[_extractable];
|
2021-12-14 11:02:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return jwk;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 11:02:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function exportKeyEd25519(format, key, innerKey) {
|
|
|
|
switch (format) {
|
|
|
|
case "raw": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-14 11:02:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2-3.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(innerKey);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "spki": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-14 11:02:14 -05:00
|
|
|
|
2023-05-08 17:07:45 -04:00
|
|
|
const spkiDer = ops.op_crypto_export_spki_ed25519(innerKey);
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(spkiDer);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "pkcs8": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-12-14 11:02:14 -05:00
|
|
|
|
2023-05-08 17:07:45 -04:00
|
|
|
const pkcs8Der = ops.op_crypto_export_pkcs8_ed25519(
|
2023-02-07 14:22:46 -05:00
|
|
|
new Uint8Array([0x04, 0x22, ...new SafeArrayIterator(innerKey)]),
|
|
|
|
);
|
|
|
|
pkcs8Der[15] = 0x20;
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(pkcs8Der);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
const x = key[_type] === "private"
|
2023-05-08 17:07:45 -04:00
|
|
|
? ops.op_crypto_jwk_x_ed25519(innerKey)
|
2023-02-07 14:22:46 -05:00
|
|
|
: ops.op_crypto_base64url_encode(innerKey);
|
|
|
|
const jwk = {
|
|
|
|
kty: "OKP",
|
|
|
|
crv: "Ed25519",
|
|
|
|
x,
|
|
|
|
"key_ops": key.usages,
|
|
|
|
ext: key[_extractable],
|
|
|
|
};
|
|
|
|
if (key[_type] === "private") {
|
|
|
|
jwk.d = ops.op_crypto_base64url_encode(innerKey);
|
2021-12-14 11:02:14 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return jwk;
|
2021-12-09 10:47:17 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
2021-12-09 10:47:17 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-12-09 10:47:17 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function exportKeyX25519(format, key, innerKey) {
|
|
|
|
switch (format) {
|
|
|
|
case "raw": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2-3.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(innerKey);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "spki": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
2023-05-08 17:07:45 -04:00
|
|
|
const spkiDer = ops.op_crypto_export_spki_x25519(innerKey);
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(spkiDer);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "pkcs8": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
2022-10-04 02:06:25 -04:00
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-05-08 17:07:45 -04:00
|
|
|
const pkcs8Der = ops.op_crypto_export_pkcs8_x25519(
|
2023-02-07 14:22:46 -05:00
|
|
|
new Uint8Array([0x04, 0x22, ...new SafeArrayIterator(innerKey)]),
|
|
|
|
);
|
|
|
|
pkcs8Der[15] = 0x20;
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(pkcs8Der);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
if (key[_type] === "private") {
|
2022-09-27 08:13:42 -04:00
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
const x = ops.op_crypto_base64url_encode(innerKey);
|
|
|
|
const jwk = {
|
|
|
|
kty: "OKP",
|
|
|
|
crv: "X25519",
|
|
|
|
x,
|
|
|
|
"key_ops": key.usages,
|
|
|
|
ext: key[_extractable],
|
|
|
|
};
|
|
|
|
return jwk;
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function exportKeyEC(format, key, innerKey) {
|
|
|
|
switch (format) {
|
|
|
|
case "raw": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2022-10-04 02:06:25 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
namedCurve: key[_algorithm].namedCurve,
|
|
|
|
format: "raw",
|
|
|
|
}, innerKey);
|
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(data);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "pkcs8": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "private") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a private key",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2022-10-04 02:06:25 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
namedCurve: key[_algorithm].namedCurve,
|
|
|
|
format: "pkcs8",
|
|
|
|
}, innerKey);
|
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(data);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "spki": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key is not a public key",
|
|
|
|
"InvalidAccessError",
|
2022-10-04 02:06:25 -04:00
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
// 2.
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
namedCurve: key[_algorithm].namedCurve,
|
|
|
|
format: "spki",
|
|
|
|
}, innerKey);
|
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(data);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "jwk": {
|
|
|
|
if (key[_algorithm].name == "ECDSA") {
|
|
|
|
// 1-2.
|
2022-10-04 02:06:25 -04:00
|
|
|
const jwk = {
|
2023-02-07 14:22:46 -05:00
|
|
|
kty: "EC",
|
2022-10-04 02:06:25 -04:00
|
|
|
};
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.1
|
|
|
|
jwk.crv = key[_algorithm].namedCurve;
|
2022-06-07 22:59:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Missing from spec
|
|
|
|
let algNamedCurve;
|
2022-06-07 22:59:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (key[_algorithm].namedCurve) {
|
|
|
|
case "P-256": {
|
|
|
|
algNamedCurve = "ES256";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "P-384": {
|
|
|
|
algNamedCurve = "ES384";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "P-521": {
|
|
|
|
algNamedCurve = "ES512";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"Curve algorithm not supported",
|
|
|
|
"DataError",
|
|
|
|
);
|
2022-01-18 22:38:35 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
jwk.alg = algNamedCurve;
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.2 - 3.4.
|
2022-08-11 09:56:56 -04:00
|
|
|
const data = ops.op_crypto_export_key({
|
2023-02-07 14:22:46 -05:00
|
|
|
format: key[_type] === "private" ? "jwkprivate" : "jwkpublic",
|
2022-01-18 22:38:35 -05:00
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
namedCurve: key[_algorithm].namedCurve,
|
|
|
|
}, innerKey);
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectAssign(jwk, data);
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
jwk.key_ops = key.usages;
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
jwk.ext = key[_extractable];
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return jwk;
|
|
|
|
} else { // ECDH
|
|
|
|
// 1-2.
|
|
|
|
const jwk = {
|
|
|
|
kty: "EC",
|
|
|
|
};
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// missing step from spec
|
|
|
|
jwk.alg = "ECDH";
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.1
|
|
|
|
jwk.crv = key[_algorithm].namedCurve;
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.2 - 3.4
|
|
|
|
const data = ops.op_crypto_export_key({
|
|
|
|
format: key[_type] === "private" ? "jwkprivate" : "jwkpublic",
|
|
|
|
algorithm: key[_algorithm].name,
|
|
|
|
namedCurve: key[_algorithm].namedCurve,
|
|
|
|
}, innerKey);
|
|
|
|
ObjectAssign(jwk, data);
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
jwk.key_ops = key.usages;
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
jwk.ext = key[_extractable];
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return jwk;
|
2022-01-18 22:38:35 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
2022-01-18 22:38:35 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-01-18 22:38:35 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function generateKeyAES(normalizedAlgorithm, extractable, usages) {
|
|
|
|
const algorithmName = normalizedAlgorithm.name;
|
2021-08-31 05:25:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (!ArrayPrototypeIncludes([128, 192, 256], normalizedAlgorithm.length)) {
|
|
|
|
throw new DOMException("Invalid key length", "OperationError");
|
|
|
|
}
|
2021-08-31 05:25:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const keyData = await core.opAsync("op_crypto_generate_key", {
|
|
|
|
algorithm: "AES",
|
|
|
|
length: normalizedAlgorithm.length,
|
|
|
|
});
|
|
|
|
const handle = {};
|
|
|
|
WeakMapPrototypeSet(KEY_STORE, handle, {
|
|
|
|
type: "secret",
|
|
|
|
data: keyData,
|
|
|
|
});
|
|
|
|
|
|
|
|
// 6-8.
|
|
|
|
const algorithm = {
|
|
|
|
name: algorithmName,
|
|
|
|
length: normalizedAlgorithm.length,
|
|
|
|
};
|
2021-08-31 05:25:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 9-11.
|
|
|
|
const key = constructKey(
|
|
|
|
"secret",
|
|
|
|
extractable,
|
|
|
|
usages,
|
|
|
|
algorithm,
|
|
|
|
handle,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 12.
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deriveBits(normalizedAlgorithm, baseKey, length) {
|
|
|
|
switch (normalizedAlgorithm.name) {
|
|
|
|
case "PBKDF2": {
|
|
|
|
// 1.
|
|
|
|
if (length == null || length == 0 || length % 8 !== 0) {
|
|
|
|
throw new DOMException("Invalid length", "OperationError");
|
|
|
|
}
|
2021-08-31 05:25:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (normalizedAlgorithm.iterations == 0) {
|
|
|
|
throw new DOMException(
|
|
|
|
"iterations must not be zero",
|
|
|
|
"OperationError",
|
|
|
|
);
|
|
|
|
}
|
2021-08-31 05:25:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = baseKey[_handle];
|
|
|
|
const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
|
2021-08-26 06:48:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
normalizedAlgorithm.salt = copyBuffer(normalizedAlgorithm.salt);
|
2021-08-26 06:48:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const buf = await core.opAsync("op_crypto_derive_bits", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "PBKDF2",
|
|
|
|
hash: normalizedAlgorithm.hash.name,
|
|
|
|
iterations: normalizedAlgorithm.iterations,
|
|
|
|
length,
|
|
|
|
}, normalizedAlgorithm.salt);
|
2021-08-26 06:48:07 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(buf);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "ECDH": {
|
|
|
|
// 1.
|
|
|
|
if (baseKey[_type] !== "private") {
|
|
|
|
throw new DOMException("Invalid key type", "InvalidAccessError");
|
|
|
|
}
|
|
|
|
// 2.
|
|
|
|
const publicKey = normalizedAlgorithm.public;
|
|
|
|
// 3.
|
|
|
|
if (publicKey[_type] !== "public") {
|
|
|
|
throw new DOMException("Invalid key type", "InvalidAccessError");
|
|
|
|
}
|
|
|
|
// 4.
|
|
|
|
if (publicKey[_algorithm].name !== baseKey[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Algorithm mismatch",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// 5.
|
|
|
|
if (
|
|
|
|
publicKey[_algorithm].namedCurve !== baseKey[_algorithm].namedCurve
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"namedCurve mismatch",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// 6.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(
|
|
|
|
supportedNamedCurves,
|
|
|
|
publicKey[_algorithm].namedCurve,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const baseKeyhandle = baseKey[_handle];
|
|
|
|
const baseKeyData = WeakMapPrototypeGet(KEY_STORE, baseKeyhandle);
|
|
|
|
const publicKeyhandle = publicKey[_handle];
|
|
|
|
const publicKeyData = WeakMapPrototypeGet(KEY_STORE, publicKeyhandle);
|
2021-08-26 06:48:07 -04:00
|
|
|
|
|
|
|
const buf = await core.opAsync("op_crypto_derive_bits", {
|
2023-02-07 14:22:46 -05:00
|
|
|
key: baseKeyData,
|
|
|
|
publicKey: publicKeyData,
|
|
|
|
algorithm: "ECDH",
|
|
|
|
namedCurve: publicKey[_algorithm].namedCurve,
|
2023-06-23 06:52:48 -04:00
|
|
|
length: length ?? 0,
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// 8.
|
|
|
|
if (length === null) {
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(buf);
|
|
|
|
} else if (TypedArrayPrototypeGetByteLength(buf) * 8 < length) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException("Invalid length", "OperationError");
|
2021-10-08 11:29:36 -04:00
|
|
|
} else {
|
2023-04-02 13:41:41 -04:00
|
|
|
return ArrayBufferPrototypeSlice(
|
|
|
|
TypedArrayPrototypeGetBuffer(buf),
|
|
|
|
0,
|
|
|
|
MathCeil(length / 8),
|
|
|
|
);
|
2021-10-08 11:29:36 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "HKDF": {
|
|
|
|
// 1.
|
|
|
|
if (length === null || length === 0 || length % 8 !== 0) {
|
|
|
|
throw new DOMException("Invalid length", "OperationError");
|
2021-10-08 11:29:36 -04:00
|
|
|
}
|
2021-09-11 16:54:03 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const handle = baseKey[_handle];
|
|
|
|
const keyDerivationKey = WeakMapPrototypeGet(KEY_STORE, handle);
|
2021-09-11 16:54:03 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
normalizedAlgorithm.salt = copyBuffer(normalizedAlgorithm.salt);
|
2021-09-11 16:54:03 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
normalizedAlgorithm.info = copyBuffer(normalizedAlgorithm.info);
|
2021-09-11 16:54:03 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const buf = await core.opAsync("op_crypto_derive_bits", {
|
|
|
|
key: keyDerivationKey,
|
|
|
|
algorithm: "HKDF",
|
|
|
|
hash: normalizedAlgorithm.hash.name,
|
|
|
|
info: normalizedAlgorithm.info,
|
|
|
|
length,
|
|
|
|
}, normalizedAlgorithm.salt);
|
2021-09-11 16:54:03 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(buf);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "X25519": {
|
|
|
|
// 1.
|
|
|
|
if (baseKey[_type] !== "private") {
|
|
|
|
throw new DOMException("Invalid key type", "InvalidAccessError");
|
|
|
|
}
|
|
|
|
// 2.
|
|
|
|
const publicKey = normalizedAlgorithm.public;
|
|
|
|
// 3.
|
|
|
|
if (publicKey[_type] !== "public") {
|
|
|
|
throw new DOMException("Invalid key type", "InvalidAccessError");
|
|
|
|
}
|
|
|
|
// 4.
|
|
|
|
if (publicKey[_algorithm].name !== baseKey[_algorithm].name) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Algorithm mismatch",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
2021-09-11 16:54:03 -04:00
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
const kHandle = baseKey[_handle];
|
|
|
|
const k = WeakMapPrototypeGet(KEY_STORE, kHandle);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const uHandle = publicKey[_handle];
|
|
|
|
const u = WeakMapPrototypeGet(KEY_STORE, uHandle);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const secret = new Uint8Array(32);
|
2023-05-08 17:07:45 -04:00
|
|
|
const isIdentity = ops.op_crypto_derive_bits_x25519(k, u, secret);
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
|
|
|
if (isIdentity) {
|
|
|
|
throw new DOMException("Invalid key", "OperationError");
|
|
|
|
}
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 7.
|
|
|
|
if (length === null) {
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(secret);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetByteLength(secret) * 8 < length
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
throw new DOMException("Invalid length", "OperationError");
|
|
|
|
} else {
|
2023-04-02 13:41:41 -04:00
|
|
|
return ArrayBufferPrototypeSlice(
|
|
|
|
TypedArrayPrototypeGetBuffer(secret),
|
|
|
|
0,
|
|
|
|
MathCeil(length / 8),
|
|
|
|
);
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
2021-08-26 06:48:07 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
2021-08-26 06:48:07 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-26 06:48:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function encrypt(normalizedAlgorithm, key, data) {
|
|
|
|
const handle = key[_handle];
|
|
|
|
const keyData = WeakMapPrototypeGet(KEY_STORE, handle);
|
2021-11-25 09:57:01 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (normalizedAlgorithm.name) {
|
|
|
|
case "RSA-OAEP": {
|
|
|
|
// 1.
|
|
|
|
if (key[_type] !== "public") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Key type not supported",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
2021-11-25 09:57:01 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (normalizedAlgorithm.label) {
|
|
|
|
normalizedAlgorithm.label = copyBuffer(normalizedAlgorithm.label);
|
|
|
|
} else {
|
|
|
|
normalizedAlgorithm.label = new Uint8Array();
|
2021-11-25 09:57:01 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3-5.
|
|
|
|
const hashAlgorithm = key[_algorithm].hash.name;
|
|
|
|
const cipherText = await core.opAsync("op_crypto_encrypt", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "RSA-OAEP",
|
|
|
|
hash: hashAlgorithm,
|
|
|
|
label: normalizedAlgorithm.label,
|
|
|
|
}, data);
|
2021-11-25 09:57:01 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 6.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(cipherText);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "AES-CBC": {
|
|
|
|
normalizedAlgorithm.iv = copyBuffer(normalizedAlgorithm.iv);
|
2021-11-25 09:57:01 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
2023-04-02 13:41:41 -04:00
|
|
|
if (TypedArrayPrototypeGetByteLength(normalizedAlgorithm.iv) !== 16) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"Initialization vector must be 16 bytes",
|
|
|
|
"OperationError",
|
|
|
|
);
|
2022-01-03 06:27:28 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const cipherText = await core.opAsync("op_crypto_encrypt", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "AES-CBC",
|
|
|
|
length: key[_algorithm].length,
|
|
|
|
iv: normalizedAlgorithm.iv,
|
|
|
|
}, data);
|
|
|
|
|
|
|
|
// 4.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(cipherText);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "AES-CTR": {
|
|
|
|
normalizedAlgorithm.counter = copyBuffer(normalizedAlgorithm.counter);
|
2022-01-03 06:27:28 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
2023-04-02 13:41:41 -04:00
|
|
|
if (
|
|
|
|
TypedArrayPrototypeGetByteLength(normalizedAlgorithm.counter) !== 16
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"Counter vector must be 16 bytes",
|
|
|
|
"OperationError",
|
|
|
|
);
|
|
|
|
}
|
2022-01-03 06:27:28 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
if (
|
|
|
|
normalizedAlgorithm.length == 0 || normalizedAlgorithm.length > 128
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Counter length must not be 0 or greater than 128",
|
|
|
|
"OperationError",
|
|
|
|
);
|
2022-01-05 10:12:30 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
|
|
|
const cipherText = await core.opAsync("op_crypto_encrypt", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "AES-CTR",
|
|
|
|
keyLength: key[_algorithm].length,
|
|
|
|
counter: normalizedAlgorithm.counter,
|
|
|
|
ctrLength: normalizedAlgorithm.length,
|
|
|
|
}, data);
|
|
|
|
|
|
|
|
// 4.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(cipherText);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
case "AES-GCM": {
|
|
|
|
normalizedAlgorithm.iv = copyBuffer(normalizedAlgorithm.iv);
|
2022-01-05 10:12:30 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 1.
|
2023-04-02 13:41:41 -04:00
|
|
|
if (TypedArrayPrototypeGetByteLength(data) > (2 ** 39) - 256) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"Plaintext too large",
|
|
|
|
"OperationError",
|
|
|
|
);
|
|
|
|
}
|
2022-01-05 10:12:30 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
// We only support 96-bit and 128-bit nonce.
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(
|
|
|
|
[12, 16],
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetByteLength(normalizedAlgorithm.iv),
|
2023-02-07 14:22:46 -05:00
|
|
|
) === undefined
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Initialization vector length not supported",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
2022-01-05 10:12:30 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 3.
|
2023-04-02 13:41:41 -04:00
|
|
|
// NOTE: over the size of Number.MAX_SAFE_INTEGER is not available in V8
|
|
|
|
// if (normalizedAlgorithm.additionalData !== undefined) {
|
|
|
|
// if (normalizedAlgorithm.additionalData.byteLength > (2 ** 64) - 1) {
|
|
|
|
// throw new DOMException(
|
|
|
|
// "Additional data too large",
|
|
|
|
// "OperationError",
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// }
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 4.
|
|
|
|
if (normalizedAlgorithm.tagLength == undefined) {
|
|
|
|
normalizedAlgorithm.tagLength = 128;
|
|
|
|
} else if (
|
|
|
|
!ArrayPrototypeIncludes(
|
|
|
|
[32, 64, 96, 104, 112, 120, 128],
|
|
|
|
normalizedAlgorithm.tagLength,
|
2021-07-06 08:16:04 -04:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2023-02-07 14:22:46 -05:00
|
|
|
"Invalid tag length",
|
|
|
|
"OperationError",
|
2021-07-06 08:16:04 -04:00
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
// 5.
|
|
|
|
if (normalizedAlgorithm.additionalData) {
|
|
|
|
normalizedAlgorithm.additionalData = copyBuffer(
|
|
|
|
normalizedAlgorithm.additionalData,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// 6-7.
|
|
|
|
const cipherText = await core.opAsync("op_crypto_encrypt", {
|
|
|
|
key: keyData,
|
|
|
|
algorithm: "AES-GCM",
|
|
|
|
length: key[_algorithm].length,
|
|
|
|
iv: normalizedAlgorithm.iv,
|
|
|
|
additionalData: normalizedAlgorithm.additionalData || null,
|
|
|
|
tagLength: normalizedAlgorithm.tagLength,
|
|
|
|
}, data);
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 8.
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(cipherText);
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new DOMException("Not implemented", "NotSupportedError");
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(SubtleCrypto);
|
2023-02-07 14:22:46 -05:00
|
|
|
const subtle = webidl.createBranded(SubtleCrypto);
|
|
|
|
|
|
|
|
class Crypto {
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
getRandomValues(typedArray) {
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.assertBranded(this, CryptoPrototype);
|
|
|
|
const prefix = "Failed to execute 'getRandomValues' on 'Crypto'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-02-07 14:22:46 -05:00
|
|
|
// Fast path for Uint8Array
|
2023-04-02 13:41:41 -04:00
|
|
|
const tag = TypedArrayPrototypeGetSymbolToStringTag(typedArray);
|
|
|
|
if (tag === "Uint8Array") {
|
|
|
|
ops.op_crypto_get_random_values(typedArray);
|
|
|
|
return typedArray;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-05-01 06:47:13 -04:00
|
|
|
typedArray = webidl.converters.ArrayBufferView(
|
|
|
|
typedArray,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
2023-04-02 13:41:41 -04:00
|
|
|
switch (tag) {
|
|
|
|
case "Int8Array":
|
|
|
|
case "Uint8ClampedArray":
|
|
|
|
case "Int16Array":
|
|
|
|
case "Uint16Array":
|
|
|
|
case "Int32Array":
|
|
|
|
case "Uint32Array":
|
|
|
|
case "BigInt64Array":
|
|
|
|
case "BigUint64Array":
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new DOMException(
|
|
|
|
"The provided ArrayBufferView is not an integer array type",
|
|
|
|
"TypeMismatchError",
|
|
|
|
);
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const ui8 = new Uint8Array(
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetBuffer(typedArray),
|
|
|
|
TypedArrayPrototypeGetByteOffset(typedArray),
|
|
|
|
TypedArrayPrototypeGetByteLength(typedArray),
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
ops.op_crypto_get_random_values(ui8);
|
2023-04-02 13:41:41 -04:00
|
|
|
return typedArray;
|
2021-07-06 08:16:04 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
randomUUID() {
|
|
|
|
webidl.assertBranded(this, CryptoPrototype);
|
|
|
|
return ops.op_crypto_random_uuid();
|
|
|
|
}
|
2021-07-06 08:16:04 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get subtle() {
|
|
|
|
webidl.assertBranded(this, CryptoPrototype);
|
|
|
|
return subtle;
|
|
|
|
}
|
|
|
|
|
|
|
|
[SymbolFor("Deno.customInspect")](inspect) {
|
|
|
|
return `${this.constructor.name} ${inspect({})}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(Crypto);
|
2023-02-07 14:22:46 -05:00
|
|
|
const CryptoPrototype = Crypto.prototype;
|
|
|
|
|
|
|
|
const crypto = webidl.createBranded(Crypto);
|
2023-05-09 06:37:13 -04:00
|
|
|
|
|
|
|
webidl.converters.AlgorithmIdentifier = (V, prefix, context, opts) => {
|
|
|
|
// Union for (object or DOMString)
|
|
|
|
if (webidl.type(V) == "Object") {
|
|
|
|
return webidl.converters.object(V, prefix, context, opts);
|
|
|
|
}
|
|
|
|
return webidl.converters.DOMString(V, prefix, context, opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
webidl.converters["BufferSource or JsonWebKey"] = (
|
|
|
|
V,
|
|
|
|
prefix,
|
|
|
|
context,
|
|
|
|
opts,
|
|
|
|
) => {
|
|
|
|
// Union for (BufferSource or JsonWebKey)
|
|
|
|
if (
|
|
|
|
ArrayBufferIsView(V) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V)
|
|
|
|
) {
|
|
|
|
return webidl.converters.BufferSource(V, prefix, context, opts);
|
|
|
|
}
|
|
|
|
return webidl.converters.JsonWebKey(V, prefix, context, opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
webidl.converters.KeyType = webidl.createEnumConverter("KeyType", [
|
|
|
|
"public",
|
|
|
|
"private",
|
|
|
|
"secret",
|
|
|
|
]);
|
|
|
|
|
|
|
|
webidl.converters.KeyFormat = webidl.createEnumConverter("KeyFormat", [
|
|
|
|
"raw",
|
|
|
|
"pkcs8",
|
|
|
|
"spki",
|
|
|
|
"jwk",
|
|
|
|
]);
|
|
|
|
|
|
|
|
webidl.converters.KeyUsage = webidl.createEnumConverter("KeyUsage", [
|
|
|
|
"encrypt",
|
|
|
|
"decrypt",
|
|
|
|
"sign",
|
|
|
|
"verify",
|
|
|
|
"deriveKey",
|
|
|
|
"deriveBits",
|
|
|
|
"wrapKey",
|
|
|
|
"unwrapKey",
|
|
|
|
]);
|
|
|
|
|
|
|
|
webidl.converters["sequence<KeyUsage>"] = webidl.createSequenceConverter(
|
|
|
|
webidl.converters.KeyUsage,
|
|
|
|
);
|
|
|
|
|
|
|
|
webidl.converters.HashAlgorithmIdentifier =
|
|
|
|
webidl.converters.AlgorithmIdentifier;
|
|
|
|
|
|
|
|
/** @type {webidl.Dictionary} */
|
|
|
|
const dictAlgorithm = [{
|
|
|
|
key: "name",
|
|
|
|
converter: webidl.converters.DOMString,
|
|
|
|
required: true,
|
|
|
|
}];
|
|
|
|
|
|
|
|
webidl.converters.Algorithm = webidl
|
|
|
|
.createDictionaryConverter("Algorithm", dictAlgorithm);
|
|
|
|
|
|
|
|
webidl.converters.BigInteger = webidl.converters.Uint8Array;
|
|
|
|
|
|
|
|
/** @type {webidl.Dictionary} */
|
|
|
|
const dictRsaKeyGenParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "modulusLength",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned long"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "publicExponent",
|
|
|
|
converter: webidl.converters.BigInteger,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.RsaKeyGenParams = webidl
|
|
|
|
.createDictionaryConverter("RsaKeyGenParams", dictRsaKeyGenParams);
|
|
|
|
|
|
|
|
const dictRsaHashedKeyGenParams = [
|
|
|
|
...new SafeArrayIterator(dictRsaKeyGenParams),
|
|
|
|
{
|
|
|
|
key: "hash",
|
|
|
|
converter: webidl.converters.HashAlgorithmIdentifier,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.RsaHashedKeyGenParams = webidl.createDictionaryConverter(
|
|
|
|
"RsaHashedKeyGenParams",
|
|
|
|
dictRsaHashedKeyGenParams,
|
|
|
|
);
|
|
|
|
|
|
|
|
const dictRsaHashedImportParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "hash",
|
|
|
|
converter: webidl.converters.HashAlgorithmIdentifier,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.RsaHashedImportParams = webidl.createDictionaryConverter(
|
|
|
|
"RsaHashedImportParams",
|
|
|
|
dictRsaHashedImportParams,
|
|
|
|
);
|
|
|
|
|
|
|
|
webidl.converters.NamedCurve = webidl.converters.DOMString;
|
|
|
|
|
|
|
|
const dictEcKeyImportParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "namedCurve",
|
|
|
|
converter: webidl.converters.NamedCurve,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.EcKeyImportParams = webidl.createDictionaryConverter(
|
|
|
|
"EcKeyImportParams",
|
|
|
|
dictEcKeyImportParams,
|
|
|
|
);
|
|
|
|
|
|
|
|
const dictEcKeyGenParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "namedCurve",
|
|
|
|
converter: webidl.converters.NamedCurve,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.EcKeyGenParams = webidl
|
|
|
|
.createDictionaryConverter("EcKeyGenParams", dictEcKeyGenParams);
|
|
|
|
|
|
|
|
const dictAesKeyGenParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "length",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned short"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.AesKeyGenParams = webidl
|
|
|
|
.createDictionaryConverter("AesKeyGenParams", dictAesKeyGenParams);
|
|
|
|
|
|
|
|
const dictHmacKeyGenParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "hash",
|
|
|
|
converter: webidl.converters.HashAlgorithmIdentifier,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "length",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned long"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.HmacKeyGenParams = webidl
|
|
|
|
.createDictionaryConverter("HmacKeyGenParams", dictHmacKeyGenParams);
|
|
|
|
|
|
|
|
const dictRsaPssParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "saltLength",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned long"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.RsaPssParams = webidl
|
|
|
|
.createDictionaryConverter("RsaPssParams", dictRsaPssParams);
|
|
|
|
|
|
|
|
const dictRsaOaepParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "label",
|
|
|
|
converter: webidl.converters["BufferSource"],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.RsaOaepParams = webidl
|
|
|
|
.createDictionaryConverter("RsaOaepParams", dictRsaOaepParams);
|
|
|
|
|
|
|
|
const dictEcdsaParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "hash",
|
|
|
|
converter: webidl.converters.HashAlgorithmIdentifier,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters["EcdsaParams"] = webidl
|
|
|
|
.createDictionaryConverter("EcdsaParams", dictEcdsaParams);
|
|
|
|
|
|
|
|
const dictHmacImportParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "hash",
|
|
|
|
converter: webidl.converters.HashAlgorithmIdentifier,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "length",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned long"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.HmacImportParams = webidl
|
|
|
|
.createDictionaryConverter("HmacImportParams", dictHmacImportParams);
|
|
|
|
|
|
|
|
const dictRsaOtherPrimesInfo = [
|
|
|
|
{
|
|
|
|
key: "r",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "d",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "t",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.RsaOtherPrimesInfo = webidl.createDictionaryConverter(
|
|
|
|
"RsaOtherPrimesInfo",
|
|
|
|
dictRsaOtherPrimesInfo,
|
|
|
|
);
|
|
|
|
webidl.converters["sequence<RsaOtherPrimesInfo>"] = webidl
|
|
|
|
.createSequenceConverter(
|
|
|
|
webidl.converters.RsaOtherPrimesInfo,
|
|
|
|
);
|
|
|
|
|
|
|
|
const dictJsonWebKey = [
|
|
|
|
// Sections 4.2 and 4.3 of RFC7517.
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc7517#section-4
|
|
|
|
{
|
|
|
|
key: "kty",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "use",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "key_ops",
|
|
|
|
converter: webidl.converters["sequence<DOMString>"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "alg",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
// JSON Web Key Parameters Registration
|
|
|
|
{
|
|
|
|
key: "ext",
|
|
|
|
converter: webidl.converters["boolean"],
|
|
|
|
},
|
|
|
|
// Section 6 of RFC7518 JSON Web Algorithms
|
|
|
|
// https://datatracker.ietf.org/doc/html/rfc7518#section-6
|
|
|
|
{
|
|
|
|
key: "crv",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "x",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "y",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "d",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "n",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "e",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "p",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "q",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "dp",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "dq",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "qi",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "oth",
|
|
|
|
converter: webidl.converters["sequence<RsaOtherPrimesInfo>"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "k",
|
|
|
|
converter: webidl.converters["DOMString"],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.JsonWebKey = webidl.createDictionaryConverter(
|
|
|
|
"JsonWebKey",
|
|
|
|
dictJsonWebKey,
|
|
|
|
);
|
|
|
|
|
|
|
|
const dictHkdfParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "hash",
|
|
|
|
converter: webidl.converters.HashAlgorithmIdentifier,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "salt",
|
|
|
|
converter: webidl.converters["BufferSource"],
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "info",
|
|
|
|
converter: webidl.converters["BufferSource"],
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.HkdfParams = webidl
|
|
|
|
.createDictionaryConverter("HkdfParams", dictHkdfParams);
|
|
|
|
|
|
|
|
const dictPbkdf2Params = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "hash",
|
|
|
|
converter: webidl.converters.HashAlgorithmIdentifier,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "iterations",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned long"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "salt",
|
|
|
|
converter: webidl.converters["BufferSource"],
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.Pbkdf2Params = webidl
|
|
|
|
.createDictionaryConverter("Pbkdf2Params", dictPbkdf2Params);
|
|
|
|
|
|
|
|
const dictAesDerivedKeyParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "length",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned long"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const dictAesCbcParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "iv",
|
|
|
|
converter: webidl.converters["BufferSource"],
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const dictAesGcmParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "iv",
|
|
|
|
converter: webidl.converters["BufferSource"],
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "tagLength",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned long"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "additionalData",
|
|
|
|
converter: webidl.converters["BufferSource"],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const dictAesCtrParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "counter",
|
|
|
|
converter: webidl.converters["BufferSource"],
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "length",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned short"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.AesDerivedKeyParams = webidl
|
|
|
|
.createDictionaryConverter("AesDerivedKeyParams", dictAesDerivedKeyParams);
|
|
|
|
|
|
|
|
webidl.converters.AesCbcParams = webidl
|
|
|
|
.createDictionaryConverter("AesCbcParams", dictAesCbcParams);
|
|
|
|
|
|
|
|
webidl.converters.AesGcmParams = webidl
|
|
|
|
.createDictionaryConverter("AesGcmParams", dictAesGcmParams);
|
|
|
|
|
|
|
|
webidl.converters.AesCtrParams = webidl
|
|
|
|
.createDictionaryConverter("AesCtrParams", dictAesCtrParams);
|
|
|
|
|
|
|
|
webidl.converters.CryptoKey = webidl.createInterfaceConverter(
|
|
|
|
"CryptoKey",
|
|
|
|
CryptoKey.prototype,
|
|
|
|
);
|
|
|
|
|
|
|
|
const dictCryptoKeyPair = [
|
|
|
|
{
|
|
|
|
key: "publicKey",
|
|
|
|
converter: webidl.converters.CryptoKey,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "privateKey",
|
|
|
|
converter: webidl.converters.CryptoKey,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.CryptoKeyPair = webidl
|
|
|
|
.createDictionaryConverter("CryptoKeyPair", dictCryptoKeyPair);
|
|
|
|
|
|
|
|
const dictEcdhKeyDeriveParams = [
|
|
|
|
...new SafeArrayIterator(dictAlgorithm),
|
|
|
|
{
|
|
|
|
key: "public",
|
|
|
|
converter: webidl.converters.CryptoKey,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
webidl.converters.EcdhKeyDeriveParams = webidl
|
|
|
|
.createDictionaryConverter("EcdhKeyDeriveParams", dictEcdhKeyDeriveParams);
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { Crypto, crypto, CryptoKey, SubtleCrypto };
|