cf-workers-native 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/browser/documents/page-source.js +1 -1
- package/dist/browser/fetch/host-call.js +6 -1
- package/dist/browser/fetch/native-network.d.ts +1 -1
- package/dist/browser/fetch/relay.js +2 -0
- package/dist/browser/headers/index.d.ts +2 -0
- package/dist/browser/headers/index.js +5 -0
- package/dist/browser/isolated/transport.js +3 -2
- package/dist/browser/request/index.js +3 -2
- package/dist/browser/response/index.d.ts +2 -0
- package/dist/browser/response/index.js +11 -3
- package/dist/browser/structured-clone/index.d.ts +3 -0
- package/dist/browser/structured-clone/index.js +75 -7
- package/dist/browser/websocket/attachment-size.d.ts +3 -0
- package/dist/browser/websocket/attachment-size.js +336 -0
- package/dist/browser/websocket/brand.d.ts +4 -0
- package/dist/browser/websocket/brand.js +16 -0
- package/dist/browser/websocket/close-event.d.ts +7 -0
- package/dist/browser/websocket/close-event.js +30 -0
- package/dist/browser/websocket/compression.d.ts +5 -0
- package/dist/browser/websocket/compression.js +124 -0
- package/dist/browser/websocket/conversion.d.ts +2 -0
- package/dist/browser/websocket/conversion.js +13 -0
- package/dist/browser/websocket/endpoint.d.ts +39 -0
- package/dist/browser/websocket/endpoint.js +379 -0
- package/dist/browser/websocket/frames.d.ts +21 -0
- package/dist/browser/websocket/frames.js +121 -0
- package/dist/browser/websocket/native.d.ts +14 -0
- package/dist/browser/websocket/native.js +226 -0
- package/dist/cloudflare/apis/cloudflare-node/index.js +11 -23
- package/dist/cloudflare/workers/worker-runtime.js +2 -1
- package/dist/compat/console/index.d.ts +35 -0
- package/dist/compat/console/index.js +92 -0
- package/dist/compat/crypto/LICENSE-3RD-PARTY.md +39 -0
- package/dist/compat/crypto/LICENSE-APACHE +177 -0
- package/dist/compat/crypto/LICENSE.boringssl +238 -0
- package/dist/compat/crypto/LICENSE.emscripten +102 -0
- package/dist/compat/crypto/LICENSE.musl +193 -0
- package/dist/compat/crypto/LICENSE.ncrypto +21 -0
- package/dist/compat/crypto/LICENSE.openssl +177 -0
- package/dist/compat/crypto/asymmetric.d.ts +13 -0
- package/dist/compat/crypto/asymmetric.js +312 -0
- package/dist/compat/crypto/brorand.cjs +74 -0
- package/dist/compat/crypto/brorand.d.cts +6 -0
- package/dist/compat/crypto/certificate.d.ts +14 -0
- package/dist/compat/crypto/certificate.js +66 -0
- package/dist/compat/crypto/cipher-handle.d.ts +27 -0
- package/dist/compat/crypto/cipher-handle.js +239 -0
- package/dist/compat/crypto/cipher-info.d.ts +12 -0
- package/dist/compat/crypto/cipher-info.js +324 -0
- package/dist/compat/crypto/constants.d.ts +62 -0
- package/dist/compat/crypto/constants.js +68 -0
- package/dist/compat/crypto/dh-handle.d.ts +17 -0
- package/dist/compat/crypto/dh-handle.js +180 -0
- package/dist/compat/crypto/dh.d.ts +53 -0
- package/dist/compat/crypto/dh.js +138 -0
- package/dist/compat/crypto/digest-stream.d.ts +15 -0
- package/dist/compat/crypto/digest-stream.js +213 -0
- package/dist/compat/crypto/digests.d.ts +3 -0
- package/dist/compat/crypto/digests.js +32 -0
- package/dist/compat/crypto/ecdh.d.ts +25 -0
- package/dist/compat/crypto/ecdh.js +109 -0
- package/dist/compat/crypto/hash-handle.d.ts +23 -0
- package/dist/compat/crypto/hash-handle.js +70 -0
- package/dist/compat/crypto/hash.d.ts +37 -0
- package/dist/compat/crypto/hash.js +218 -0
- package/dist/compat/crypto/hkdf.d.ts +2 -0
- package/dist/compat/crypto/hkdf.js +78 -0
- package/dist/compat/crypto/index.cjs +2 -0
- package/dist/compat/crypto/index.d.cts +169 -0
- package/dist/compat/crypto/index.d.ts +213 -0
- package/dist/compat/crypto/index.js +96 -0
- package/dist/compat/crypto/key-errors.d.ts +10 -0
- package/dist/compat/crypto/key-errors.js +17 -0
- package/dist/compat/crypto/key-export.d.ts +8 -0
- package/dist/compat/crypto/key-export.js +49 -0
- package/dist/compat/crypto/key-formats.d.ts +7 -0
- package/dist/compat/crypto/key-formats.js +251 -0
- package/dist/compat/crypto/key-options.d.ts +1 -0
- package/dist/compat/crypto/key-options.js +74 -0
- package/dist/compat/crypto/key-state.d.ts +10 -0
- package/dist/compat/crypto/key-state.js +4 -0
- package/dist/compat/crypto/keys.d.ts +58 -0
- package/dist/compat/crypto/keys.js +278 -0
- package/dist/compat/crypto/metadata.d.ts +13 -0
- package/dist/compat/crypto/metadata.js +109 -0
- package/dist/compat/crypto/native-cipher.d.ts +17 -0
- package/dist/compat/crypto/native-cipher.js +192 -0
- package/dist/compat/crypto/native-crypto.d.ts +81 -0
- package/dist/compat/crypto/native-crypto.js +681 -0
- package/dist/compat/crypto/pbkdf2-handle.d.ts +4 -0
- package/dist/compat/crypto/pbkdf2-handle.js +26 -0
- package/dist/compat/crypto/pbkdf2.d.ts +5 -0
- package/dist/compat/crypto/pbkdf2.js +60 -0
- package/dist/compat/crypto/prime.d.ts +19 -0
- package/dist/compat/crypto/prime.js +146 -0
- package/dist/compat/crypto/random.d.ts +18 -0
- package/dist/compat/crypto/random.js +203 -0
- package/dist/compat/crypto/rsa.d.ts +21 -0
- package/dist/compat/crypto/rsa.js +177 -0
- package/dist/compat/crypto/scrypt.d.ts +25 -0
- package/dist/compat/crypto/scrypt.js +152 -0
- package/dist/compat/crypto/sign-errors.d.ts +8 -0
- package/dist/compat/crypto/sign-errors.js +13 -0
- package/dist/compat/crypto/sign-handle.d.ts +22 -0
- package/dist/compat/crypto/sign-handle.js +149 -0
- package/dist/compat/crypto/signatures.d.ts +34 -0
- package/dist/compat/crypto/signatures.js +252 -0
- package/dist/compat/crypto/subtle-digest.d.ts +1 -0
- package/dist/compat/crypto/subtle-digest.js +56 -0
- package/dist/compat/crypto/symmetric.d.ts +45 -0
- package/dist/compat/crypto/symmetric.js +239 -0
- package/dist/compat/crypto/validation.d.ts +7 -0
- package/dist/compat/crypto/validation.js +46 -0
- package/dist/compat/crypto/webcrypto.d.ts +13 -0
- package/dist/compat/crypto/webcrypto.js +51 -0
- package/dist/compat/crypto/x509.d.ts +30 -0
- package/dist/compat/crypto/x509.js +407 -0
- package/dist/compat/events/errors.d.ts +8 -0
- package/dist/compat/events/errors.js +39 -0
- package/dist/compat/events/index.cjs +2 -0
- package/dist/compat/events/index.d.cts +2 -0
- package/dist/compat/events/index.d.ts +64 -0
- package/dist/compat/events/index.js +792 -0
- package/dist/compat/events/intrinsics.d.ts +1 -0
- package/dist/compat/events/intrinsics.js +2 -0
- package/dist/compat/fs/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/fs/LICENSE-APACHE +177 -0
- package/dist/compat/fs/directory-backend.d.ts +6 -0
- package/dist/compat/fs/directory-backend.js +51 -0
- package/dist/compat/fs/directory.d.ts +37 -0
- package/dist/compat/fs/directory.js +137 -0
- package/dist/compat/fs/embedded.d.ts +85 -0
- package/dist/compat/fs/embedded.js +260 -0
- package/dist/compat/fs/file-handle.d.ts +75 -0
- package/dist/compat/fs/file-handle.js +226 -0
- package/dist/compat/fs/glob-internal.d.ts +15 -0
- package/dist/compat/fs/glob-internal.js +417 -0
- package/dist/compat/fs/glob.d.ts +10 -0
- package/dist/compat/fs/glob.js +71 -0
- package/dist/compat/fs/index.d.ts +8 -82
- package/dist/compat/fs/index.js +736 -238
- package/dist/compat/fs/io-validation.d.ts +14 -0
- package/dist/compat/fs/io-validation.js +130 -0
- package/dist/compat/fs/io.d.ts +1 -0
- package/dist/compat/fs/io.js +282 -0
- package/dist/compat/fs/mode.d.ts +1 -0
- package/dist/compat/fs/mode.js +13 -0
- package/dist/compat/fs/promises/index.d.ts +3 -1
- package/dist/compat/fs/promises/index.js +1 -1
- package/dist/compat/fs/public-types.d.ts +120 -0
- package/dist/compat/fs/public-types.js +1 -0
- package/dist/compat/fs/stream-errors.d.ts +6 -0
- package/dist/compat/fs/stream-errors.js +8 -0
- package/dist/compat/fs/stream-utils.d.ts +16 -0
- package/dist/compat/fs/stream-utils.js +29 -0
- package/dist/compat/fs/streams.d.ts +104 -0
- package/dist/compat/fs/streams.js +794 -0
- package/dist/compat/fs/writable.d.ts +14 -0
- package/dist/compat/fs/writable.js +532 -0
- package/dist/compat/http/index.d.ts +11 -28
- package/dist/compat/http/index.js +81 -125
- package/dist/compat/http/server-options.d.ts +1 -0
- package/dist/compat/http/server-options.js +87 -0
- package/dist/compat/http/symbols.d.ts +5 -0
- package/dist/compat/http/symbols.js +9 -0
- package/dist/compat/http/unique-headers.d.ts +1 -0
- package/dist/compat/http/unique-headers.js +21 -0
- package/dist/compat/http-common/index.d.ts +37 -0
- package/dist/compat/http-common/index.js +43 -0
- package/dist/compat/http-outgoing/index.d.ts +14 -0
- package/dist/compat/http-outgoing/index.js +17 -0
- package/dist/compat/http-server/index.d.ts +18 -0
- package/dist/compat/http-server/index.js +26 -0
- package/dist/compat/https/index.d.ts +4 -2
- package/dist/compat/module/builtins.d.ts +1 -0
- package/dist/compat/module/builtins.js +101 -0
- package/dist/compat/module/index.d.ts +4 -2
- package/dist/compat/module/index.js +22 -17
- package/dist/compat/module/metadata.d.ts +44 -0
- package/dist/compat/module/metadata.js +127 -0
- package/dist/compat/net/internal-net.js +4 -0
- package/dist/compat/net/sockets.d.ts +5 -0
- package/dist/compat/net/sockets.js +153 -14
- package/dist/compat/net/validators.d.ts +1 -0
- package/dist/compat/net/validators.js +8 -0
- package/dist/compat/process/configuration.d.ts +113 -0
- package/dist/compat/process/configuration.js +115 -0
- package/dist/compat/process/directory.d.ts +4 -0
- package/dist/compat/process/directory.js +28 -0
- package/dist/compat/process/files.d.ts +3 -0
- package/dist/compat/process/files.js +10 -0
- package/dist/compat/process/index.cjs +2 -0
- package/dist/compat/process/index.d.cts +4 -0
- package/dist/compat/process/index.d.ts +20 -0
- package/dist/compat/process/index.js +113 -0
- package/dist/compat/process/metadata.d.ts +135 -0
- package/dist/compat/process/metadata.js +251 -0
- package/dist/compat/process/scheduling.d.ts +2 -0
- package/dist/compat/process/scheduling.js +60 -0
- package/dist/compat/process/state.d.ts +4 -0
- package/dist/compat/process/state.js +5 -0
- package/dist/compat/process/stdio.d.ts +18 -0
- package/dist/compat/process/stdio.js +66 -0
- package/dist/compat/stream/destroy.d.ts +1 -0
- package/dist/compat/stream/destroy.js +21 -1
- package/dist/compat/stream/readable.js +10 -2
- package/dist/compat/stream-web/LICENSE +22 -0
- package/dist/compat/stream-web/ponyfill.d.ts +1 -1
- package/dist/compat/stream-web/ponyfill.js +2369 -1775
- package/dist/compat/stream-web/runtime.d.ts +1 -1
- package/dist/compat/stream-web/runtime.js +3 -2
- package/dist/compat/stream-wrap/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/stream-wrap/LICENSE-APACHE +177 -0
- package/dist/compat/stream-wrap/index.d.ts +1 -0
- package/dist/compat/stream-wrap/index.js +1 -0
- package/dist/compat/tls/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls/LICENSE-APACHE +177 -0
- package/dist/compat/tls/constants.d.ts +7 -0
- package/dist/compat/tls/constants.js +14 -0
- package/dist/compat/tls/errors.d.ts +35 -0
- package/dist/compat/tls/errors.js +68 -0
- package/dist/compat/tls/helpers.d.ts +9 -0
- package/dist/compat/tls/helpers.js +207 -0
- package/dist/compat/tls/index.d.ts +26 -0
- package/dist/compat/tls/index.js +50 -0
- package/dist/compat/tls/jsstream.d.ts +19 -0
- package/dist/compat/tls/jsstream.js +196 -0
- package/dist/compat/tls/native-session.d.ts +3 -0
- package/dist/compat/tls/native-session.js +187 -0
- package/dist/compat/tls/wrap.d.ts +65 -0
- package/dist/compat/tls/wrap.js +541 -0
- package/dist/compat/tls-wrap/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls-wrap/LICENSE-APACHE +177 -0
- package/dist/compat/tls-wrap/index.d.ts +7 -0
- package/dist/compat/tls-wrap/index.js +3 -0
- package/dist/compat/util/index.js +43 -1
- package/dist/compat/util/inspect.js +5 -1
- package/dist/compat/util/types/index.d.ts +3 -0
- package/dist/compat/util/types/index.js +5 -0
- package/dist/compat/zlib/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/zlib/LICENSE-APACHE +177 -0
- package/dist/compat/zlib/LICENSE.brotli +19 -0
- package/dist/compat/zlib/LICENSE.emscripten +102 -0
- package/dist/compat/zlib/LICENSE.musl +193 -0
- package/dist/compat/zlib/LICENSE.zlib +22 -0
- package/dist/compat/zlib/LICENSE.zstd +30 -0
- package/dist/compat/zlib/classic-base.cjs +600 -0
- package/dist/compat/zlib/classic-base.d.cts +39 -0
- package/dist/compat/zlib/classic-binding.d.ts +28 -0
- package/dist/compat/zlib/classic-binding.js +105 -0
- package/dist/compat/zlib/classic.d.ts +44 -0
- package/dist/compat/zlib/classic.js +199 -0
- package/dist/compat/zlib/codec-binding.d.ts +33 -0
- package/dist/compat/zlib/codec-binding.js +141 -0
- package/dist/compat/zlib/compression.d.ts +26 -0
- package/dist/compat/zlib/compression.js +170 -0
- package/dist/compat/zlib/constants.d.ts +172 -0
- package/dist/compat/zlib/constants.js +173 -0
- package/dist/compat/zlib/errors.d.ts +21 -0
- package/dist/compat/zlib/errors.js +32 -0
- package/dist/compat/zlib/index.d.ts +544 -0
- package/dist/compat/zlib/index.js +194 -0
- package/dist/compat/zlib/input.d.ts +16 -0
- package/dist/compat/zlib/input.js +132 -0
- package/dist/compat/zlib/native-codecs.d.ts +24 -0
- package/dist/compat/zlib/native-codecs.js +180 -0
- package/dist/compat/zlib/native-zlib.d.ts +5 -0
- package/dist/compat/zlib/native-zlib.js +52 -0
- package/dist/compat/zlib/public-types.d.ts +48 -0
- package/dist/compat/zlib/public-types.js +1 -0
- package/dist/compat/zlib/validators.d.ts +3 -0
- package/dist/compat/zlib/validators.js +18 -0
- package/dist/shared/buffer-source.d.ts +5 -0
- package/dist/shared/buffer-source.js +70 -0
- package/dist/shared/globals/inject.d.ts +2 -1
- package/dist/shared/globals/inject.js +2 -1
- package/dist/shared/globals/platform.d.ts +8 -0
- package/dist/shared/globals/platform.js +1 -0
- package/dist/shared/globals/runtime.d.ts +1 -2
- package/dist/shared/globals/runtime.js +1 -2
- package/dist/shared/globals/web.js +5 -0
- package/dist/shared/request-context.d.ts +2 -0
- package/dist/shared/request-context.js +11 -0
- package/dist/shared/request-scope.d.ts +4 -0
- package/dist/shared/request-scope.js +14 -0
- package/dist/tooling/async.js +36 -14
- package/dist/tooling/shared.d.ts +1 -0
- package/dist/tooling/shared.js +26 -1
- package/dist/tooling/vite.js +8 -3
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +92 -13
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersCapacitorPlugin.java +4 -2
- package/native/ios/NetworkBridge.swift +11 -1
- package/native/ios/SocketBridge.swift +37 -2
- package/native/ios/WorkersCapacitorPlugin.swift +4 -0
- package/package.json +4 -2
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { NativeKey } from "./native-crypto.js";
|
|
3
|
+
import { curveName, validateEcPrivate } from "./native-cipher.js";
|
|
4
|
+
// DER encodings follow RFC 5208/5915/8410; JWK members follow RFC 7518/8037.
|
|
5
|
+
const curves = {
|
|
6
|
+
"P-256": { oid: "2a8648ce3d030107", size: 32, native: "prime256v1" },
|
|
7
|
+
"P-384": { oid: "2b81040022", size: 48, native: "secp384r1" },
|
|
8
|
+
"P-521": { oid: "2b81040023", size: 66, native: "secp521r1" },
|
|
9
|
+
"": { oid: "2b81040021", size: 28, native: "secp224r1" },
|
|
10
|
+
};
|
|
11
|
+
export function der(tag, ...parts) {
|
|
12
|
+
const data = Buffer.concat(parts);
|
|
13
|
+
let length = [];
|
|
14
|
+
if (data.length < 128)
|
|
15
|
+
length = [data.length];
|
|
16
|
+
else {
|
|
17
|
+
let size = data.length;
|
|
18
|
+
while (size) {
|
|
19
|
+
length.unshift(size & 255);
|
|
20
|
+
size = Math.floor(size / 256);
|
|
21
|
+
}
|
|
22
|
+
length.unshift(128 + length.length);
|
|
23
|
+
}
|
|
24
|
+
return Buffer.concat([Buffer.from([tag, ...length]), data]);
|
|
25
|
+
}
|
|
26
|
+
export function integer(bytes) {
|
|
27
|
+
let offset = 0;
|
|
28
|
+
while (offset < bytes.length - 1 && bytes[offset] === 0)
|
|
29
|
+
offset++;
|
|
30
|
+
const data = bytes.length ? bytes.subarray(offset) : Buffer.from([0]);
|
|
31
|
+
return der(2, ...(data[0] & 128 ? [Buffer.from([0]), data] : [data]));
|
|
32
|
+
}
|
|
33
|
+
function required(jwk, name) {
|
|
34
|
+
if (jwk[name] === undefined)
|
|
35
|
+
throw new Error(`${jwk.kty} JWK missing ${name} parameter`);
|
|
36
|
+
return `${jwk[name]}`;
|
|
37
|
+
}
|
|
38
|
+
function field(jwk, name) {
|
|
39
|
+
const value = required(jwk, name).replace(/[\t\n\f\r ]/g, "");
|
|
40
|
+
if (!/^[A-Za-z0-9_-]*={0,2}$/.test(value) ||
|
|
41
|
+
value.replace(/=+$/, "").length % 4 === 1 ||
|
|
42
|
+
(value.includes("=") && value.length % 4 !== 0))
|
|
43
|
+
throw new Error(`${jwk.kty} JWK invalid ${name} parameter`);
|
|
44
|
+
return Buffer.from(value, "base64url");
|
|
45
|
+
}
|
|
46
|
+
function fixed(bytes, length) {
|
|
47
|
+
let start = 0;
|
|
48
|
+
while (start < bytes.length && bytes[start] === 0)
|
|
49
|
+
start++;
|
|
50
|
+
bytes = bytes.subarray(start);
|
|
51
|
+
if (bytes.length > length)
|
|
52
|
+
throw new Error("EC JWK invalid public key");
|
|
53
|
+
return Buffer.concat([Buffer.alloc(length - bytes.length), bytes]);
|
|
54
|
+
}
|
|
55
|
+
export function dictionaryString(value) {
|
|
56
|
+
const object = (item) => item !== null && (typeof item === "object" || typeof item === "function");
|
|
57
|
+
if (object(value)) {
|
|
58
|
+
const primitive = value[Symbol.toPrimitive];
|
|
59
|
+
if (primitive != null) {
|
|
60
|
+
value = Reflect.apply(primitive, value, ["string"]);
|
|
61
|
+
if (object(value))
|
|
62
|
+
throw new TypeError("Cannot convert object to primitive value");
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const original = value;
|
|
66
|
+
for (const name of ["toString", "valueOf"]) {
|
|
67
|
+
const method = original[name];
|
|
68
|
+
if (typeof method !== "function")
|
|
69
|
+
continue;
|
|
70
|
+
value = Reflect.apply(method, original, []);
|
|
71
|
+
if (!object(value))
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
if (object(value))
|
|
75
|
+
throw new TypeError("Cannot convert object to primitive value");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (typeof value === "symbol")
|
|
79
|
+
throw new TypeError("Cannot convert a Symbol value to a string");
|
|
80
|
+
return String(value).replace(/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g, "\ufffd");
|
|
81
|
+
}
|
|
82
|
+
function dictionary(value) {
|
|
83
|
+
const result = {};
|
|
84
|
+
for (const name of [
|
|
85
|
+
"kty",
|
|
86
|
+
"use",
|
|
87
|
+
"key_ops",
|
|
88
|
+
"alg",
|
|
89
|
+
"ext",
|
|
90
|
+
"crv",
|
|
91
|
+
"x",
|
|
92
|
+
"y",
|
|
93
|
+
"d",
|
|
94
|
+
"n",
|
|
95
|
+
"e",
|
|
96
|
+
"p",
|
|
97
|
+
"q",
|
|
98
|
+
"dp",
|
|
99
|
+
"dq",
|
|
100
|
+
"qi",
|
|
101
|
+
"oth",
|
|
102
|
+
"k",
|
|
103
|
+
"pub",
|
|
104
|
+
"priv",
|
|
105
|
+
]) {
|
|
106
|
+
const field = value[name];
|
|
107
|
+
if (field === undefined && name !== "kty")
|
|
108
|
+
continue;
|
|
109
|
+
if (name === "key_ops" || name === "oth") {
|
|
110
|
+
if (!Array.isArray(field))
|
|
111
|
+
throw new TypeError(`Incorrect type for the '${name}' field on 'JsonWebKey': the provided value is not of type 'Array'.`);
|
|
112
|
+
result[name] = [];
|
|
113
|
+
const length = field.length;
|
|
114
|
+
for (let i = 0; i < length; i++) {
|
|
115
|
+
const item = field[i];
|
|
116
|
+
if (name === "key_ops")
|
|
117
|
+
result[name].push(dictionaryString(item));
|
|
118
|
+
else {
|
|
119
|
+
if (item != null &&
|
|
120
|
+
typeof item !== "object" &&
|
|
121
|
+
typeof item !== "function")
|
|
122
|
+
throw new TypeError(`Incorrect type for array element ${i}: the provided value is not of type 'RsaOtherPrimesInfo'.`);
|
|
123
|
+
const prime = {};
|
|
124
|
+
for (const member of ["r", "d", "t"]) {
|
|
125
|
+
const part = item?.[member];
|
|
126
|
+
if (part !== undefined)
|
|
127
|
+
prime[member] = dictionaryString(part);
|
|
128
|
+
}
|
|
129
|
+
result[name].push(prime);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else
|
|
134
|
+
result[name] = name === "ext" ? Boolean(field) : dictionaryString(field);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
export function importJwk(value, privateKey) {
|
|
139
|
+
const jwk = dictionary(value);
|
|
140
|
+
const unsupported = () => new Error(`JWK ${privateKey ? "private" : "public"} key import is not implemented for this key type`);
|
|
141
|
+
if (jwk?.kty === "RSA") {
|
|
142
|
+
required(jwk, "n");
|
|
143
|
+
required(jwk, "e");
|
|
144
|
+
const values = [field(jwk, "n"), field(jwk, "e")];
|
|
145
|
+
if (privateKey) {
|
|
146
|
+
const names = ["d", "p", "q", "dp", "dq", "qi"];
|
|
147
|
+
for (const name of names)
|
|
148
|
+
required(jwk, name);
|
|
149
|
+
values.push(...names.map((name) => field(jwk, name)));
|
|
150
|
+
}
|
|
151
|
+
return NativeKey.parse(der(48, ...(privateKey ? [integer(Buffer.from([0]))] : []), ...values.map(integer)), false, privateKey);
|
|
152
|
+
}
|
|
153
|
+
if (jwk?.kty === "EC") {
|
|
154
|
+
const crv = required(jwk, "crv");
|
|
155
|
+
let native;
|
|
156
|
+
try {
|
|
157
|
+
native = curveName(crv);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
throw new Error(error.message === "Failed to initialize key"
|
|
161
|
+
? "EC JWK unsupported curve"
|
|
162
|
+
: "EC JWK unsupported crv parameter");
|
|
163
|
+
}
|
|
164
|
+
const curve = Object.values(curves).find((value) => value.native === native);
|
|
165
|
+
required(jwk, "x");
|
|
166
|
+
required(jwk, "y");
|
|
167
|
+
const x = field(jwk, "x"), y = field(jwk, "y");
|
|
168
|
+
const oid = der(6, Buffer.from(curve.oid, "hex"));
|
|
169
|
+
const point = Buffer.concat([
|
|
170
|
+
Buffer.from([4]),
|
|
171
|
+
fixed(x, curve.size),
|
|
172
|
+
fixed(y, curve.size),
|
|
173
|
+
]);
|
|
174
|
+
const bits = der(3, Buffer.from([0]), point);
|
|
175
|
+
const publicData = der(48, der(48, der(6, Buffer.from("2a8648ce3d0201", "hex")), oid), bits);
|
|
176
|
+
let publicKey;
|
|
177
|
+
try {
|
|
178
|
+
publicKey = NativeKey.parse(publicData, false, false);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
throw new Error("EC JWK invalid public key");
|
|
182
|
+
}
|
|
183
|
+
if (!privateKey)
|
|
184
|
+
return publicKey;
|
|
185
|
+
publicKey.dispose();
|
|
186
|
+
const d = field(jwk, "d");
|
|
187
|
+
validateEcPrivate(native, d);
|
|
188
|
+
return NativeKey.parse(der(48, integer(Buffer.from([1])), der(4, d), der(160, oid), der(161, bits)), false, true);
|
|
189
|
+
}
|
|
190
|
+
if (jwk?.kty === "OKP") {
|
|
191
|
+
const crv = required(jwk, "crv");
|
|
192
|
+
if (!["Ed25519", "X25519"].includes(crv))
|
|
193
|
+
throw new Error("OKP JWK unsupported crv parameter");
|
|
194
|
+
const value = field(jwk, privateKey ? "d" : "x");
|
|
195
|
+
if (value.length !== 32)
|
|
196
|
+
throw unsupported();
|
|
197
|
+
const algorithm = der(48, der(6, Buffer.from([43, 101, crv === "Ed25519" ? 112 : 110])));
|
|
198
|
+
const data = privateKey
|
|
199
|
+
? der(48, integer(Buffer.from([0])), algorithm, der(4, der(4, value)))
|
|
200
|
+
: der(48, algorithm, der(3, Buffer.from([0]), value));
|
|
201
|
+
return NativeKey.parse(data, false, privateKey);
|
|
202
|
+
}
|
|
203
|
+
throw unsupported();
|
|
204
|
+
}
|
|
205
|
+
function encode(bytes, size) {
|
|
206
|
+
if (size !== undefined && bytes.length < size)
|
|
207
|
+
bytes = Buffer.concat([Buffer.alloc(size - bytes.length), bytes]);
|
|
208
|
+
return Buffer.from(bytes).toString("base64url");
|
|
209
|
+
}
|
|
210
|
+
export function exportJwk(key, privateKey) {
|
|
211
|
+
if (key.type === "rsa") {
|
|
212
|
+
const result = {
|
|
213
|
+
kty: "RSA",
|
|
214
|
+
n: encode(key.parameter("n")),
|
|
215
|
+
e: encode(key.parameter("e")),
|
|
216
|
+
};
|
|
217
|
+
if (privateKey)
|
|
218
|
+
for (const [name, param] of [
|
|
219
|
+
["d", "d"],
|
|
220
|
+
["p", "rsa-factor1"],
|
|
221
|
+
["q", "rsa-factor2"],
|
|
222
|
+
["dp", "rsa-exponent1"],
|
|
223
|
+
["dq", "rsa-exponent2"],
|
|
224
|
+
["qi", "rsa-coefficient1"],
|
|
225
|
+
])
|
|
226
|
+
result[name] = encode(key.parameter(param));
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
if (key.type === "ec") {
|
|
230
|
+
const found = Object.entries(curves).find(([, value]) => value.native === key.group);
|
|
231
|
+
if (!found)
|
|
232
|
+
throw new TypeError("Unsupported JWK curve");
|
|
233
|
+
const [crv, curve] = found;
|
|
234
|
+
return {
|
|
235
|
+
kty: "EC",
|
|
236
|
+
crv,
|
|
237
|
+
x: encode(key.parameter("qx"), curve.size),
|
|
238
|
+
y: encode(key.parameter("qy"), curve.size),
|
|
239
|
+
...(privateKey ? { d: encode(key.parameter("priv"), curve.size) } : {}),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (["ed25519", "x25519"].includes(key.type))
|
|
243
|
+
return {
|
|
244
|
+
kty: "OKP",
|
|
245
|
+
...(key.type === "ed25519" ? { alg: "EdDSA" } : {}),
|
|
246
|
+
crv: key.type === "ed25519" ? "Ed25519" : "X25519",
|
|
247
|
+
x: encode(key.raw(false)),
|
|
248
|
+
...(privateKey ? { d: encode(key.raw(true)) } : {}),
|
|
249
|
+
};
|
|
250
|
+
throw new Error("Key type is invalid for JWK export");
|
|
251
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function validateExportOptions(options: any, type: string, name?: string, asymmetricKeyType?: string): void;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Copyright (c) 2017-2022 Cloudflare, Inc.
|
|
2
|
+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
3
|
+
// https://opensource.org/licenses/Apache-2.0
|
|
4
|
+
//
|
|
5
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
6
|
+
//
|
|
7
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
8
|
+
// copy of this software and associated documentation files (the
|
|
9
|
+
// "Software"), to deal in the Software without restriction, including
|
|
10
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
12
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
13
|
+
// following conditions:
|
|
14
|
+
//
|
|
15
|
+
// The above copyright notice and this permission notice shall be included
|
|
16
|
+
// in all copies or substantial portions of the Software.
|
|
17
|
+
//
|
|
18
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
19
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
21
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
22
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT ORs
|
|
23
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
24
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
import { Buffer } from "node:buffer";
|
|
26
|
+
import { validateObject, validateString } from "../net/validators.js";
|
|
27
|
+
import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
|
|
28
|
+
import { ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS } from "./key-errors.js";
|
|
29
|
+
import { isUint8Array } from "../util/types/index.js";
|
|
30
|
+
export function validateExportOptions(options, type, name = "options", asymmetricKeyType) {
|
|
31
|
+
validateObject(options, name);
|
|
32
|
+
// Yes, converting to any is a bit of a cheat, but it allows us to check
|
|
33
|
+
// each option individually without having to do a bunch of type guards.
|
|
34
|
+
const opts = options;
|
|
35
|
+
if (opts.format !== undefined) {
|
|
36
|
+
validateString(opts.format, `${name}.format`);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
options.format = "buffer";
|
|
40
|
+
}
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
42
|
+
if ("type" in opts && opts.type !== undefined) {
|
|
43
|
+
validateString(opts.type, `${name}.type`);
|
|
44
|
+
}
|
|
45
|
+
if (type === "private") {
|
|
46
|
+
if ("type" in opts &&
|
|
47
|
+
opts.type === "pkcs1" &&
|
|
48
|
+
asymmetricKeyType !== undefined &&
|
|
49
|
+
asymmetricKeyType !== "rsa") {
|
|
50
|
+
throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(opts.type, "can only be used for RSA keys");
|
|
51
|
+
}
|
|
52
|
+
if ("type" in opts &&
|
|
53
|
+
opts.type === "sec1" &&
|
|
54
|
+
asymmetricKeyType !== undefined &&
|
|
55
|
+
asymmetricKeyType !== "ec" &&
|
|
56
|
+
asymmetricKeyType !== "ecdh") {
|
|
57
|
+
throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(opts.type, "can only be used for EC keys");
|
|
58
|
+
}
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
60
|
+
if ("cipher" in opts && opts.cipher !== undefined) {
|
|
61
|
+
validateString(opts.cipher, `${name}.cipher`);
|
|
62
|
+
if (opts.format === "der" &&
|
|
63
|
+
(opts.type === "pkcs1" || opts.type === "sec1")) {
|
|
64
|
+
throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(opts.type, "does not support encryption");
|
|
65
|
+
}
|
|
66
|
+
if (typeof opts.passphrase === "string") {
|
|
67
|
+
opts.passphrase = Buffer.from(opts.passphrase, opts.encoding);
|
|
68
|
+
}
|
|
69
|
+
if (!isUint8Array(opts.passphrase)) {
|
|
70
|
+
throw new ERR_INVALID_ARG_TYPE(`${name}.passphrase`, ["string", "Uint8Array"], opts.passphrase);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { NativeKey } from "./native-crypto.js";
|
|
2
|
+
export interface KeyState {
|
|
3
|
+
type: "secret" | "private" | "public";
|
|
4
|
+
bytes?: Uint8Array;
|
|
5
|
+
asymmetric?: NativeKey;
|
|
6
|
+
native?: CryptoKey;
|
|
7
|
+
nativeAlgorithm?: KeyAlgorithm;
|
|
8
|
+
}
|
|
9
|
+
export declare const keyStates: WeakMap<object, KeyState>;
|
|
10
|
+
export declare function isKeyObject(value: unknown): boolean;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type KeyState } from "./key-state.js";
|
|
2
|
+
export declare function create(type: KeyState["type"], value: Omit<KeyState, "type">): KeyObject;
|
|
3
|
+
export declare abstract class KeyObject {
|
|
4
|
+
constructor();
|
|
5
|
+
static from(key: CryptoKey): KeyObject;
|
|
6
|
+
abstract get type(): KeyState["type"];
|
|
7
|
+
export(options?: any): any;
|
|
8
|
+
equals(other: KeyObject): boolean;
|
|
9
|
+
get [Symbol.toStringTag](): string;
|
|
10
|
+
}
|
|
11
|
+
export declare class SecretKeyObject extends KeyObject {
|
|
12
|
+
get symmetricKeySize(): number;
|
|
13
|
+
export(options?: any): any;
|
|
14
|
+
get type(): "secret";
|
|
15
|
+
}
|
|
16
|
+
declare abstract class AsymmetricKeyObject extends KeyObject {
|
|
17
|
+
get asymmetricKeyDetails(): {
|
|
18
|
+
modulusLength: number;
|
|
19
|
+
publicExponent: bigint;
|
|
20
|
+
divisorLength: undefined;
|
|
21
|
+
namedCurve: undefined;
|
|
22
|
+
} | {
|
|
23
|
+
namedCurve: string;
|
|
24
|
+
modulusLength: undefined;
|
|
25
|
+
publicExponent: undefined;
|
|
26
|
+
divisorLength: undefined;
|
|
27
|
+
} | {
|
|
28
|
+
modulusLength: undefined;
|
|
29
|
+
publicExponent: undefined;
|
|
30
|
+
divisorLength: undefined;
|
|
31
|
+
namedCurve: undefined;
|
|
32
|
+
} | {
|
|
33
|
+
modulusLength: number;
|
|
34
|
+
divisorLength: number;
|
|
35
|
+
publicExponent: undefined;
|
|
36
|
+
namedCurve: undefined;
|
|
37
|
+
};
|
|
38
|
+
get asymmetricKeyType(): string;
|
|
39
|
+
toCryptoKey(): void;
|
|
40
|
+
}
|
|
41
|
+
export declare class PublicKeyObject extends AsymmetricKeyObject {
|
|
42
|
+
export(options?: any): any;
|
|
43
|
+
get type(): "public";
|
|
44
|
+
}
|
|
45
|
+
export declare class PrivateKeyObject extends AsymmetricKeyObject {
|
|
46
|
+
export(options?: any): any;
|
|
47
|
+
get type(): "private";
|
|
48
|
+
}
|
|
49
|
+
export declare function createSecretKey(value: unknown, encoding?: BufferEncoding): SecretKeyObject;
|
|
50
|
+
export declare function secretMaterial(value: unknown): any;
|
|
51
|
+
export declare function generateKeySync(type: string, options: {
|
|
52
|
+
length: number;
|
|
53
|
+
}): SecretKeyObject;
|
|
54
|
+
export declare function generateKey(type: string, options: {
|
|
55
|
+
length: number;
|
|
56
|
+
}, callback: (error: unknown, key?: SecretKeyObject) => void): void;
|
|
57
|
+
export { isKeyObject } from "./key-state.js";
|
|
58
|
+
export declare function getKeyObjectHandle(key: KeyObject): KeyObject;
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { inspect } from "../util/inspect.js";
|
|
2
|
+
import { exportOptions, exportAsymmetric } from "./key-export.js";
|
|
3
|
+
import { validateExportOptions } from "./key-options.js";
|
|
4
|
+
import { exportJwk } from "./key-formats.js";
|
|
5
|
+
// API behavior follows workerd@5358e3d337336eaf696a48bc1563d808b10feba5
|
|
6
|
+
// crypto_keys.ts (Apache-2.0 and Node MIT). Host CryptoKey export is an explicit
|
|
7
|
+
// synchronous-engine boundary; adapter-created secret keys own copied bytes.
|
|
8
|
+
import { Buffer } from "node:buffer";
|
|
9
|
+
import { keyStates } from "./key-state.js";
|
|
10
|
+
import { isAnyBuffer, validateInteger } from "./validation.js";
|
|
11
|
+
import { validateObject, validateOneOf } from "../net/validators.js";
|
|
12
|
+
import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
|
|
13
|
+
function state(value) {
|
|
14
|
+
const result = keyStates.get(value);
|
|
15
|
+
if (!result)
|
|
16
|
+
throw new ERR_INVALID_ARG_TYPE("key", "KeyObject", value);
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
function boundary() {
|
|
20
|
+
throw Object.assign(new Error("Synchronous export of a host CryptoKey is unavailable in this WebView runtime"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
|
|
21
|
+
}
|
|
22
|
+
const nativeAlgorithm = Object.getOwnPropertyDescriptor(globalThis.CryptoKey.prototype, "algorithm").get;
|
|
23
|
+
const nativeType = Object.getOwnPropertyDescriptor(globalThis.CryptoKey.prototype, "type").get;
|
|
24
|
+
function cloneAlgorithm(value) {
|
|
25
|
+
if (ArrayBuffer.isView(value))
|
|
26
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength).slice();
|
|
27
|
+
if (value && typeof value === "object")
|
|
28
|
+
return Object.fromEntries(Object.entries(value).map(([name, entry]) => [
|
|
29
|
+
name,
|
|
30
|
+
cloneAlgorithm(entry),
|
|
31
|
+
]));
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
export function create(type, value) {
|
|
35
|
+
const Class = type === "secret"
|
|
36
|
+
? SecretKeyObject
|
|
37
|
+
: type === "public"
|
|
38
|
+
? PublicKeyObject
|
|
39
|
+
: PrivateKeyObject;
|
|
40
|
+
const result = Object.create(Class.prototype);
|
|
41
|
+
keyStates.set(result, { type, ...value });
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
export class KeyObject {
|
|
45
|
+
constructor() {
|
|
46
|
+
throw new Error("Illegal constructor");
|
|
47
|
+
}
|
|
48
|
+
static from(key) {
|
|
49
|
+
if (!(key instanceof globalThis.CryptoKey))
|
|
50
|
+
throw new ERR_INVALID_ARG_TYPE("key", "CryptoKey", key);
|
|
51
|
+
return create(nativeType.call(key), {
|
|
52
|
+
native: key,
|
|
53
|
+
nativeAlgorithm: cloneAlgorithm(nativeAlgorithm.call(key)),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export(options = {}) {
|
|
57
|
+
validateObject(options, "options");
|
|
58
|
+
const current = state(this);
|
|
59
|
+
validateExportOptions(options, current.type, "options", current.asymmetric?.type);
|
|
60
|
+
options = exportOptions(options);
|
|
61
|
+
if (options.format === undefined)
|
|
62
|
+
throw new TypeError("Missing format option");
|
|
63
|
+
if (current.asymmetric) {
|
|
64
|
+
if (options.format === "jwk")
|
|
65
|
+
return exportJwk(current.asymmetric, current.type === "private");
|
|
66
|
+
const result = exportAsymmetric(current.asymmetric, current.type === "private", options);
|
|
67
|
+
return options.format === "pem"
|
|
68
|
+
? new TextDecoder().decode(result)
|
|
69
|
+
: Buffer.from(result);
|
|
70
|
+
}
|
|
71
|
+
const data = current.bytes;
|
|
72
|
+
if (!data)
|
|
73
|
+
return boundary();
|
|
74
|
+
if (options.format === "buffer")
|
|
75
|
+
return Buffer.from(data);
|
|
76
|
+
if (options.format === "jwk")
|
|
77
|
+
return {
|
|
78
|
+
kty: "oct",
|
|
79
|
+
ext: true,
|
|
80
|
+
k: Buffer.from(data)
|
|
81
|
+
.toString("base64")
|
|
82
|
+
.replace(/=/g, "")
|
|
83
|
+
.replace(/\+/g, "-")
|
|
84
|
+
.replace(/\//g, "_"),
|
|
85
|
+
};
|
|
86
|
+
throw new TypeError(`Invalid format for secret key export: ${options.format}`);
|
|
87
|
+
}
|
|
88
|
+
equals(other) {
|
|
89
|
+
if (other == null)
|
|
90
|
+
throw new TypeError("Cannot read properties of null or undefined");
|
|
91
|
+
const a = state(this), b = keyStates.get(other);
|
|
92
|
+
if (this === other || (a.native && a.native === b?.native))
|
|
93
|
+
return true;
|
|
94
|
+
if (a.type !== other?.type)
|
|
95
|
+
return false;
|
|
96
|
+
if (!b)
|
|
97
|
+
throw new ERR_INVALID_ARG_TYPE("otherKeyObject", "KeyObject", other);
|
|
98
|
+
if (a.asymmetric && b.asymmetric)
|
|
99
|
+
return a.asymmetric.equals(b.asymmetric);
|
|
100
|
+
if (!a.bytes || !b.bytes)
|
|
101
|
+
return boundary();
|
|
102
|
+
if (a.bytes.length !== b.bytes.length)
|
|
103
|
+
return false;
|
|
104
|
+
let difference = 0;
|
|
105
|
+
for (let i = 0; i < a.bytes.length; i++)
|
|
106
|
+
difference |= a.bytes[i] ^ b.bytes[i];
|
|
107
|
+
return difference === 0;
|
|
108
|
+
}
|
|
109
|
+
get [Symbol.toStringTag]() {
|
|
110
|
+
return "KeyObject";
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export class SecretKeyObject extends KeyObject {
|
|
114
|
+
get symmetricKeySize() {
|
|
115
|
+
const key = state(this);
|
|
116
|
+
return ((key.bytes?.length ?? key.native?.algorithm?.length ?? 0) | 0);
|
|
117
|
+
}
|
|
118
|
+
export(options) {
|
|
119
|
+
return super.export(options);
|
|
120
|
+
}
|
|
121
|
+
get type() {
|
|
122
|
+
return "secret";
|
|
123
|
+
}
|
|
124
|
+
[Symbol.for("nodejs.util.inspect.custom")](depth, options) {
|
|
125
|
+
if (depth < 0)
|
|
126
|
+
return this;
|
|
127
|
+
const opts = {
|
|
128
|
+
...options,
|
|
129
|
+
depth: options.depth == null ? null : options.depth - 1,
|
|
130
|
+
};
|
|
131
|
+
return `${this.constructor.name} ${inspect({ size: this.symmetricKeySize }, opts)}`;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
class AsymmetricKeyObject extends KeyObject {
|
|
135
|
+
get asymmetricKeyDetails() {
|
|
136
|
+
const details = (() => {
|
|
137
|
+
const current = state(this), key = current.asymmetric;
|
|
138
|
+
if (!key) {
|
|
139
|
+
const algorithm = current.nativeAlgorithm;
|
|
140
|
+
if (!algorithm)
|
|
141
|
+
return boundary();
|
|
142
|
+
if ("modulusLength" in algorithm) {
|
|
143
|
+
let publicExponent = 0n;
|
|
144
|
+
for (const value of algorithm.publicExponent)
|
|
145
|
+
publicExponent = (publicExponent << 8n) | BigInt(value);
|
|
146
|
+
return { modulusLength: algorithm.modulusLength, publicExponent };
|
|
147
|
+
}
|
|
148
|
+
if ("namedCurve" in algorithm)
|
|
149
|
+
return {
|
|
150
|
+
namedCurve: {
|
|
151
|
+
"P-256": "prime256v1",
|
|
152
|
+
"P-384": "secp384r1",
|
|
153
|
+
"P-521": "secp521r1",
|
|
154
|
+
}[algorithm.namedCurve] ?? algorithm.namedCurve,
|
|
155
|
+
};
|
|
156
|
+
return {};
|
|
157
|
+
}
|
|
158
|
+
if (key.type === "rsa")
|
|
159
|
+
return {
|
|
160
|
+
modulusLength: key.bits,
|
|
161
|
+
publicExponent: BigInt("0x" + Buffer.from(key.parameter("e")).toString("hex")),
|
|
162
|
+
};
|
|
163
|
+
if (key.type === "ec")
|
|
164
|
+
return { namedCurve: key.group };
|
|
165
|
+
if (key.type === "dsa") {
|
|
166
|
+
const q = key.parameter("q");
|
|
167
|
+
return {
|
|
168
|
+
modulusLength: key.bits,
|
|
169
|
+
divisorLength: q.length
|
|
170
|
+
? (q.length - 1) * 8 + 32 - Math.clz32(q[0])
|
|
171
|
+
: 0,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
return {};
|
|
175
|
+
})();
|
|
176
|
+
return {
|
|
177
|
+
modulusLength: undefined,
|
|
178
|
+
publicExponent: undefined,
|
|
179
|
+
divisorLength: undefined,
|
|
180
|
+
namedCurve: undefined,
|
|
181
|
+
...details,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
get asymmetricKeyType() {
|
|
185
|
+
const current = state(this), key = current.asymmetric;
|
|
186
|
+
if (!key) {
|
|
187
|
+
const name = current.nativeAlgorithm?.name;
|
|
188
|
+
if (name === undefined)
|
|
189
|
+
return boundary();
|
|
190
|
+
return ({
|
|
191
|
+
"RSASSA-PKCS1-v1_5": "rsa",
|
|
192
|
+
"RSA-PSS": "rsa",
|
|
193
|
+
"RSA-OAEP": "rsa",
|
|
194
|
+
ECDSA: "ec",
|
|
195
|
+
ECDH: "ecdh",
|
|
196
|
+
Ed25519: "ed25519",
|
|
197
|
+
"NODE-ED25519": "ed25519",
|
|
198
|
+
X25519: "x25519",
|
|
199
|
+
}[name] ?? name);
|
|
200
|
+
}
|
|
201
|
+
return key.type;
|
|
202
|
+
}
|
|
203
|
+
toCryptoKey() {
|
|
204
|
+
throw Object.assign(new Error("The toCryptoKey method is not implemented"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
|
|
205
|
+
}
|
|
206
|
+
[Symbol.for("nodejs.util.inspect.custom")](depth, options) {
|
|
207
|
+
if (depth < 0)
|
|
208
|
+
return this;
|
|
209
|
+
const opts = {
|
|
210
|
+
...options,
|
|
211
|
+
depth: options.depth == null ? null : options.depth - 1,
|
|
212
|
+
};
|
|
213
|
+
return `${this.constructor.name} ${inspect({ type: this.asymmetricKeyType, details: this.asymmetricKeyDetails }, opts)}`;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
export class PublicKeyObject extends AsymmetricKeyObject {
|
|
217
|
+
export(options) {
|
|
218
|
+
return super.export(options);
|
|
219
|
+
}
|
|
220
|
+
get type() {
|
|
221
|
+
return "public";
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
export class PrivateKeyObject extends AsymmetricKeyObject {
|
|
225
|
+
export(options) {
|
|
226
|
+
return super.export(options);
|
|
227
|
+
}
|
|
228
|
+
get type() {
|
|
229
|
+
return "private";
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
export function createSecretKey(value, encoding) {
|
|
233
|
+
if (typeof value !== "string" &&
|
|
234
|
+
!isAnyBuffer(value) &&
|
|
235
|
+
!ArrayBuffer.isView(value))
|
|
236
|
+
throw new ERR_INVALID_ARG_TYPE("key", ["string", "ArrayBuffer", "TypedArray", "DataView"], value);
|
|
237
|
+
const data = typeof value === "string"
|
|
238
|
+
? Buffer.from(value, encoding)
|
|
239
|
+
: isAnyBuffer(value)
|
|
240
|
+
? new Uint8Array(value)
|
|
241
|
+
: new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
242
|
+
return create("secret", { bytes: new Uint8Array(data) });
|
|
243
|
+
}
|
|
244
|
+
export function secretMaterial(value) {
|
|
245
|
+
if (typeof value !== "object" || value === null || !keyStates.has(value))
|
|
246
|
+
return value;
|
|
247
|
+
const key = state(value);
|
|
248
|
+
if (key.type !== "secret")
|
|
249
|
+
throw new TypeError("Expected a secret key");
|
|
250
|
+
if (!key.bytes)
|
|
251
|
+
return boundary();
|
|
252
|
+
return Buffer.from(key.bytes);
|
|
253
|
+
}
|
|
254
|
+
export function generateKeySync(type, options) {
|
|
255
|
+
validateOneOf(type, "type", ["hmac", "aes"]);
|
|
256
|
+
validateObject(options, "options");
|
|
257
|
+
if (type === "hmac")
|
|
258
|
+
validateInteger(options.length, "options.length", 8, 65536);
|
|
259
|
+
else
|
|
260
|
+
validateOneOf(options.length, "options.length", [128, 192, 256]);
|
|
261
|
+
const material = new Uint8Array(Math.floor(options.length / 8));
|
|
262
|
+
globalThis.crypto.getRandomValues(material);
|
|
263
|
+
return createSecretKey(material);
|
|
264
|
+
}
|
|
265
|
+
export function generateKey(type, options, callback) {
|
|
266
|
+
let key, failure;
|
|
267
|
+
try {
|
|
268
|
+
key = generateKeySync(type, options);
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
failure = error;
|
|
272
|
+
}
|
|
273
|
+
queueMicrotask(() => callback(failure ?? null, key));
|
|
274
|
+
}
|
|
275
|
+
export { isKeyObject } from "./key-state.js";
|
|
276
|
+
export function getKeyObjectHandle(key) {
|
|
277
|
+
return key;
|
|
278
|
+
}
|