cf-workers-native 0.1.4 → 0.1.5
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/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/isolated/transport.js +3 -2
- package/dist/browser/request/index.js +3 -2
- 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 +19 -0
- package/dist/compat/crypto/LICENSE-APACHE +177 -0
- package/dist/compat/crypto/LICENSE.emscripten +102 -0
- package/dist/compat/crypto/LICENSE.musl +193 -0
- package/dist/compat/crypto/LICENSE.openssl +177 -0
- package/dist/compat/crypto/asymmetric.d.ts +13 -0
- package/dist/compat/crypto/asymmetric.js +239 -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/ecdh.d.ts +15 -0
- package/dist/compat/crypto/ecdh.js +109 -0
- package/dist/compat/crypto/hkdf.d.ts +2 -0
- package/dist/compat/crypto/hkdf.js +52 -0
- package/dist/compat/crypto/index.cjs +2 -0
- package/dist/compat/crypto/index.d.cts +2 -0
- package/dist/compat/crypto/index.d.ts +30 -0
- package/dist/compat/crypto/index.js +83 -0
- package/dist/compat/crypto/key-formats.d.ts +6 -0
- package/dist/compat/crypto/key-formats.js +120 -0
- package/dist/compat/crypto/key-state.d.ts +9 -0
- package/dist/compat/crypto/key-state.js +4 -0
- package/dist/compat/crypto/keys.d.ts +50 -0
- package/dist/compat/crypto/keys.js +209 -0
- package/dist/compat/crypto/metadata.d.ts +4 -0
- package/dist/compat/crypto/metadata.js +22 -0
- package/dist/compat/crypto/native-crypto.d.ts +68 -0
- package/dist/compat/crypto/native-crypto.js +569 -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 +7 -0
- package/dist/compat/crypto/random.js +123 -0
- package/dist/compat/crypto/rsa.d.ts +5 -0
- package/dist/compat/crypto/rsa.js +173 -0
- package/dist/compat/crypto/scrypt.d.ts +25 -0
- package/dist/compat/crypto/scrypt.js +150 -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 +105 -0
- package/dist/compat/crypto/signatures.d.ts +28 -0
- package/dist/compat/crypto/signatures.js +252 -0
- package/dist/compat/crypto/symmetric.d.ts +29 -0
- package/dist/compat/crypto/symmetric.js +104 -0
- package/dist/compat/crypto/validation.d.ts +3 -0
- package/dist/compat/crypto/validation.js +19 -0
- package/dist/compat/crypto/x509.d.ts +30 -0
- package/dist/compat/crypto/x509.js +406 -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/embedded.d.ts +85 -0
- package/dist/compat/fs/embedded.js +255 -0
- package/dist/compat/fs/index.d.ts +7 -84
- package/dist/compat/fs/index.js +218 -252
- 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 +1 -1
- package/dist/compat/fs/promises/index.js +1 -1
- package/dist/compat/fs/writable.d.ts +6 -0
- package/dist/compat/fs/writable.js +142 -0
- package/dist/compat/http/symbols.d.ts +5 -0
- package/dist/compat/http/symbols.js +9 -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 +13 -0
- package/dist/compat/http-outgoing/index.js +32 -0
- package/dist/compat/http-server/index.d.ts +18 -0
- package/dist/compat/http-server/index.js +107 -0
- 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 +74 -13
- package/dist/compat/process/index.cjs +2 -0
- package/dist/compat/process/index.d.cts +4 -0
- package/dist/compat/process/index.d.ts +18 -0
- package/dist/compat/process/index.js +140 -0
- package/dist/compat/process/metadata.d.ts +135 -0
- package/dist/compat/process/metadata.js +251 -0
- package/dist/compat/process/state.d.ts +4 -0
- package/dist/compat/process/state.js +4 -0
- 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 +186 -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 +544 -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/types/index.d.ts +3 -0
- package/dist/compat/util/types/index.js +5 -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.zstd +30 -0
- package/dist/compat/zlib/compression.d.ts +55 -0
- package/dist/compat/zlib/compression.js +259 -0
- package/dist/compat/zlib/constants.d.ts +172 -0
- package/dist/compat/zlib/constants.js +173 -0
- package/dist/compat/zlib/index.d.ts +180 -0
- package/dist/compat/zlib/index.js +177 -0
- package/dist/compat/zlib/native-codecs.d.ts +17 -0
- package/dist/compat/zlib/native-codecs.js +168 -0
- package/dist/shared/globals/inject.d.ts +2 -1
- package/dist/shared/globals/inject.js +2 -1
- package/dist/shared/globals/runtime.d.ts +1 -2
- package/dist/shared/globals/runtime.js +1 -2
- package/dist/shared/request-scope.d.ts +4 -0
- package/dist/shared/request-scope.js +11 -0
- package/dist/tooling/async.js +36 -14
- package/dist/tooling/shared.d.ts +1 -0
- package/dist/tooling/shared.js +20 -1
- package/dist/tooling/vite.js +4 -3
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +56 -8
- 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 +3 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
declare class SignHandle {
|
|
3
|
+
private algorithm;
|
|
4
|
+
private hash;
|
|
5
|
+
constructor(algorithm: string);
|
|
6
|
+
update(data: Uint8Array): void;
|
|
7
|
+
sign(value: any, padding?: number, saltLength?: number, encoding?: number): Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike>;
|
|
8
|
+
}
|
|
9
|
+
declare class VerifyHandle {
|
|
10
|
+
private algorithm;
|
|
11
|
+
private hash;
|
|
12
|
+
constructor(algorithm: string);
|
|
13
|
+
update(data: Uint8Array): void;
|
|
14
|
+
verify(value: any, signature: Uint8Array, padding?: number, saltLength?: number, encoding?: number): boolean;
|
|
15
|
+
}
|
|
16
|
+
declare const _default: {
|
|
17
|
+
SignHandle: typeof SignHandle;
|
|
18
|
+
VerifyHandle: typeof VerifyHandle;
|
|
19
|
+
signOneShot(key: any, algorithm: any, data: any, padding?: number, saltLength?: number, encoding?: number): any;
|
|
20
|
+
verifyOneShot(key: any, algorithm: any, data: any, signature: any, padding?: number, saltLength?: number, encoding?: number): any;
|
|
21
|
+
};
|
|
22
|
+
export default _default;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { NativeDigest } from "./native-crypto.js";
|
|
3
|
+
import { keyStates } from "./key-state.js";
|
|
4
|
+
import { KeyObject } from "./keys.js";
|
|
5
|
+
import { sign, verify } from "./asymmetric.js";
|
|
6
|
+
import { der, integer } from "./key-formats.js";
|
|
7
|
+
function key(value) {
|
|
8
|
+
if (value instanceof globalThis.CryptoKey)
|
|
9
|
+
value = KeyObject.from(value);
|
|
10
|
+
const result = keyStates.get(value)?.asymmetric;
|
|
11
|
+
if (!result)
|
|
12
|
+
throw Object.assign(new Error("Synchronous access to opaque host key material is unavailable"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
function rawSignature(data, width) {
|
|
16
|
+
let offset = 0;
|
|
17
|
+
const item = (tag) => {
|
|
18
|
+
if (data[offset++] !== tag)
|
|
19
|
+
throw new Error("Invalid DER signature");
|
|
20
|
+
let length = data[offset++];
|
|
21
|
+
if (length & 128) {
|
|
22
|
+
const count = length & 127;
|
|
23
|
+
length = 0;
|
|
24
|
+
for (let i = 0; i < count; i++)
|
|
25
|
+
length = length * 256 + data[offset++];
|
|
26
|
+
}
|
|
27
|
+
if (offset + length > data.length)
|
|
28
|
+
throw new Error("Invalid DER signature");
|
|
29
|
+
return length;
|
|
30
|
+
};
|
|
31
|
+
const length = item(48);
|
|
32
|
+
if (offset + length !== data.length)
|
|
33
|
+
throw new Error("Invalid DER signature");
|
|
34
|
+
const output = Buffer.alloc(width * 2);
|
|
35
|
+
for (let part = 0; part < 2; part++) {
|
|
36
|
+
let length = item(2);
|
|
37
|
+
while (length > width && data[offset] === 0) {
|
|
38
|
+
offset++;
|
|
39
|
+
length--;
|
|
40
|
+
}
|
|
41
|
+
if (length > width)
|
|
42
|
+
throw new Error("Invalid DER signature");
|
|
43
|
+
output.set(data.subarray(offset, offset + length), part * width + width - length);
|
|
44
|
+
offset += length;
|
|
45
|
+
}
|
|
46
|
+
return output;
|
|
47
|
+
}
|
|
48
|
+
class SignHandle {
|
|
49
|
+
algorithm;
|
|
50
|
+
hash;
|
|
51
|
+
constructor(algorithm) {
|
|
52
|
+
this.algorithm = algorithm;
|
|
53
|
+
this.hash = new NativeDigest(algorithm);
|
|
54
|
+
}
|
|
55
|
+
update(data) {
|
|
56
|
+
this.hash.update(Buffer.from(data.buffer, data.byteOffset, data.byteLength));
|
|
57
|
+
}
|
|
58
|
+
sign(value, padding, saltLength, encoding) {
|
|
59
|
+
const native = key(value), signed = native.signHash(this.algorithm, this.hash.final(), padding, saltLength);
|
|
60
|
+
return encoding === 1 && native.type === "ec"
|
|
61
|
+
? rawSignature(signed, Math.ceil(native.bits / 8))
|
|
62
|
+
: signed;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
class VerifyHandle {
|
|
66
|
+
algorithm;
|
|
67
|
+
hash;
|
|
68
|
+
constructor(algorithm) {
|
|
69
|
+
this.algorithm = algorithm;
|
|
70
|
+
this.hash = new NativeDigest(algorithm, "Verification");
|
|
71
|
+
}
|
|
72
|
+
update(data) {
|
|
73
|
+
this.hash.update(Buffer.from(data.buffer, data.byteOffset, data.byteLength));
|
|
74
|
+
}
|
|
75
|
+
verify(value, signature, padding, saltLength, encoding) {
|
|
76
|
+
const native = key(value), hash = this.hash.final();
|
|
77
|
+
if (encoding === 1 && native.type === "ec") {
|
|
78
|
+
const width = Math.ceil(native.bits / 8);
|
|
79
|
+
if (signature.length !== width * 2)
|
|
80
|
+
return false;
|
|
81
|
+
signature = der(48, integer(signature.subarray(0, width)), integer(signature.subarray(width)));
|
|
82
|
+
}
|
|
83
|
+
return native.verifyHash(this.algorithm, hash, signature, padding, saltLength);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export default {
|
|
87
|
+
SignHandle,
|
|
88
|
+
VerifyHandle,
|
|
89
|
+
signOneShot(key, algorithm, data, padding, saltLength, encoding) {
|
|
90
|
+
return sign(algorithm, data, {
|
|
91
|
+
key,
|
|
92
|
+
padding,
|
|
93
|
+
saltLength,
|
|
94
|
+
dsaEncoding: encoding === 1 ? "ieee-p1363" : "der",
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
verifyOneShot(key, algorithm, data, signature, padding, saltLength, encoding) {
|
|
98
|
+
return verify(algorithm, data, {
|
|
99
|
+
key,
|
|
100
|
+
padding,
|
|
101
|
+
saltLength,
|
|
102
|
+
dsaEncoding: encoding === 1 ? "ieee-p1363" : "der",
|
|
103
|
+
}, signature);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type SignHandle, type VerifyHandle } from "./sign-handle.js";
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
export interface SignOptions {
|
|
4
|
+
}
|
|
5
|
+
export interface Sign extends Writable {
|
|
6
|
+
[kHandle]: SignHandle;
|
|
7
|
+
update(data: any): void;
|
|
8
|
+
sign(privateKey: any): any;
|
|
9
|
+
}
|
|
10
|
+
export interface Verify extends Writable {
|
|
11
|
+
[kHandle]: VerifyHandle;
|
|
12
|
+
update(data: any): void;
|
|
13
|
+
verify(publicKey: any, signature: any): any;
|
|
14
|
+
}
|
|
15
|
+
declare const kHandle: unique symbol;
|
|
16
|
+
export declare const Sign: {
|
|
17
|
+
new (algorithm: string, options?: SignOptions): Sign;
|
|
18
|
+
};
|
|
19
|
+
export declare const Verify: {
|
|
20
|
+
new (algorithm: string, options?: SignOptions): Verify;
|
|
21
|
+
};
|
|
22
|
+
export declare function createSign(algorithm: string, options: any): Sign;
|
|
23
|
+
export declare function createVerify(algorithm: string, options: any): Verify;
|
|
24
|
+
type SignCallback = (err: any, signature?: Buffer) => void;
|
|
25
|
+
type VerifyCallback = (err: any, valid?: boolean) => void;
|
|
26
|
+
export declare function sign(algorithm: string | null | undefined, data: BufferSource, options: any, callback?: SignCallback): Buffer | void;
|
|
27
|
+
export declare function verify(algorithm: string | null | undefined, data: BufferSource, options: any, signature: BufferSource, callback?: VerifyCallback): boolean | undefined;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { createPrivateKey, createPublicKey } from "./asymmetric.js";
|
|
3
|
+
// Copyright (c) 2017-2023 Cloudflare, Inc.
|
|
4
|
+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
5
|
+
// https://opensource.org/licenses/Apache-2.0
|
|
6
|
+
//
|
|
7
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
8
|
+
//
|
|
9
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
10
|
+
// copy of this software and associated documentation files (the
|
|
11
|
+
// "Software"), to deal in the Software without restriction, including
|
|
12
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
13
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
14
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
15
|
+
// following conditions:
|
|
16
|
+
//
|
|
17
|
+
// The above copyright notice and this permission notice shall be included
|
|
18
|
+
// in all copies or substantial portions of the Software.
|
|
19
|
+
//
|
|
20
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
21
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
22
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
23
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
24
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
25
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
26
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
27
|
+
/* TODO: the following is adopted code, enabling linting one day */
|
|
28
|
+
/* eslint-disable */
|
|
29
|
+
import { default as cryptoImpl, } from "./sign-handle.js";
|
|
30
|
+
import { validateString } from "../net/validators.js";
|
|
31
|
+
import { Buffer } from "node:buffer";
|
|
32
|
+
import { isKeyObject, getKeyObjectHandle } from "./keys.js";
|
|
33
|
+
import { ERR_CRYPTO_SIGN_KEY_REQUIRED, ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, } from "./sign-errors.js";
|
|
34
|
+
import { Writable } from "../stream/writable.js";
|
|
35
|
+
import { isArrayBufferView } from "../util/types/index.js";
|
|
36
|
+
const kHandle = Symbol("kHandle");
|
|
37
|
+
// Uses old-style class syntax for Node.js compatibility
|
|
38
|
+
export const Sign = function (algorithm, options) {
|
|
39
|
+
if (!(this instanceof Sign))
|
|
40
|
+
return new Sign(algorithm, options);
|
|
41
|
+
validateString(algorithm, "algorithm");
|
|
42
|
+
this[kHandle] = new cryptoImpl.SignHandle(algorithm);
|
|
43
|
+
Writable.call(this, options);
|
|
44
|
+
return this;
|
|
45
|
+
};
|
|
46
|
+
Object.setPrototypeOf(Sign.prototype, Writable.prototype);
|
|
47
|
+
Object.setPrototypeOf(Sign, Writable);
|
|
48
|
+
Sign.prototype._write = function _write(chunk, encoding, callback) {
|
|
49
|
+
this.update(chunk, encoding);
|
|
50
|
+
callback();
|
|
51
|
+
};
|
|
52
|
+
Sign.prototype.update = function (data, encoding) {
|
|
53
|
+
if (typeof data === "string") {
|
|
54
|
+
data = Buffer.from(data, encoding);
|
|
55
|
+
}
|
|
56
|
+
if (!isArrayBufferView(data)) {
|
|
57
|
+
throw new ERR_INVALID_ARG_TYPE("data", ["string", "Buffer", "TypedArray", "DataView"], data);
|
|
58
|
+
}
|
|
59
|
+
this[kHandle].update(data);
|
|
60
|
+
return this;
|
|
61
|
+
};
|
|
62
|
+
function getIntOption(name, options) {
|
|
63
|
+
const value = options[name];
|
|
64
|
+
if (value !== undefined) {
|
|
65
|
+
if (value === value >> 0) {
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
throw new ERR_INVALID_ARG_VALUE(`options.${name}`, value);
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
function getDSASignatureEncoding(options) {
|
|
73
|
+
if (typeof options === "object") {
|
|
74
|
+
const { dsaEncoding = "der" } = options;
|
|
75
|
+
if (dsaEncoding === "der")
|
|
76
|
+
return 0; // kSigEncDER;
|
|
77
|
+
else if (dsaEncoding === "ieee-p1363")
|
|
78
|
+
return 1; // kSigEncP1363
|
|
79
|
+
throw new ERR_INVALID_ARG_VALUE("options.dsaEncoding", dsaEncoding);
|
|
80
|
+
}
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
function getPrivateKey(options) {
|
|
84
|
+
if (options instanceof CryptoKey) {
|
|
85
|
+
return options;
|
|
86
|
+
}
|
|
87
|
+
else if (isKeyObject(options)) {
|
|
88
|
+
const keyObject = options;
|
|
89
|
+
if (keyObject.type === "secret") {
|
|
90
|
+
throw new ERR_INVALID_ARG_TYPE("options", ["PublicKeyObject", "PrivateKeyObject", "CryptoKey", "object"], keyObject.type);
|
|
91
|
+
}
|
|
92
|
+
if (keyObject.type === "public") {
|
|
93
|
+
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE("public", "private");
|
|
94
|
+
}
|
|
95
|
+
return getKeyObjectHandle(keyObject);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
return getKeyObjectHandle(createPrivateKey(options));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
Sign.prototype.sign = function (options, encoding) {
|
|
102
|
+
if (!options) {
|
|
103
|
+
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
|
|
104
|
+
}
|
|
105
|
+
const key = getPrivateKey(options);
|
|
106
|
+
// Options specific to RSA
|
|
107
|
+
const rsaPadding = getIntOption("padding", options);
|
|
108
|
+
const pssSaltLength = getIntOption("saltLength", options);
|
|
109
|
+
// Options specific to (EC)DSA
|
|
110
|
+
const dsaSigEnc = getDSASignatureEncoding(options);
|
|
111
|
+
const u8 = this[kHandle].sign(key, rsaPadding, pssSaltLength, dsaSigEnc);
|
|
112
|
+
const res = Buffer.from(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
113
|
+
if (encoding && encoding !== "buffer") {
|
|
114
|
+
return res.toString(encoding);
|
|
115
|
+
}
|
|
116
|
+
return res;
|
|
117
|
+
};
|
|
118
|
+
// Uses old-style class syntax for Node.js compatibility
|
|
119
|
+
export const Verify = function (algorithm, options) {
|
|
120
|
+
if (!(this instanceof Verify))
|
|
121
|
+
return new Verify(algorithm, options);
|
|
122
|
+
validateString(algorithm, "algorithm");
|
|
123
|
+
this[kHandle] = new cryptoImpl.VerifyHandle(algorithm);
|
|
124
|
+
Writable.call(this, options);
|
|
125
|
+
return this;
|
|
126
|
+
};
|
|
127
|
+
Object.setPrototypeOf(Verify.prototype, Writable.prototype);
|
|
128
|
+
Object.setPrototypeOf(Verify, Writable);
|
|
129
|
+
Verify.prototype._write = function _write(chunk, encoding, callback) {
|
|
130
|
+
this.update(chunk, encoding);
|
|
131
|
+
callback();
|
|
132
|
+
};
|
|
133
|
+
Verify.prototype.update = function (data, encoding) {
|
|
134
|
+
if (typeof data === "string") {
|
|
135
|
+
data = Buffer.from(data, encoding);
|
|
136
|
+
}
|
|
137
|
+
if (!isArrayBufferView(data)) {
|
|
138
|
+
throw new ERR_INVALID_ARG_TYPE("data", ["string", "Buffer", "TypedArray", "DataView"], data);
|
|
139
|
+
}
|
|
140
|
+
this[kHandle].update(data);
|
|
141
|
+
return this;
|
|
142
|
+
};
|
|
143
|
+
Verify.prototype.verify = function (options, signature, encoding = "utf8") {
|
|
144
|
+
if (!options) {
|
|
145
|
+
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
|
|
146
|
+
}
|
|
147
|
+
let key;
|
|
148
|
+
if (options instanceof CryptoKey) {
|
|
149
|
+
key = options;
|
|
150
|
+
}
|
|
151
|
+
else if (isKeyObject(options)) {
|
|
152
|
+
key = getKeyObjectHandle(options);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
key = getKeyObjectHandle(createPublicKey(options));
|
|
156
|
+
}
|
|
157
|
+
if (!(key instanceof CryptoKey) && !isKeyObject(key)) {
|
|
158
|
+
throw new ERR_INVALID_ARG_TYPE("options", ["KeyObject", "CryptoKey", "object"], key);
|
|
159
|
+
}
|
|
160
|
+
// Options specific to RSA
|
|
161
|
+
const rsaPadding = getIntOption("padding", options);
|
|
162
|
+
const pssSaltLength = getIntOption("saltLength", options);
|
|
163
|
+
// Options specific to (EC)DSA
|
|
164
|
+
const dsaSigEnc = getDSASignatureEncoding(options);
|
|
165
|
+
if (typeof signature === "string") {
|
|
166
|
+
signature = Buffer.from(signature, encoding);
|
|
167
|
+
}
|
|
168
|
+
return this[kHandle].verify(key, signature, rsaPadding, pssSaltLength, dsaSigEnc);
|
|
169
|
+
};
|
|
170
|
+
export function createSign(algorithm, options) {
|
|
171
|
+
return new Sign(algorithm, options);
|
|
172
|
+
}
|
|
173
|
+
export function createVerify(algorithm, options) {
|
|
174
|
+
return new Verify(algorithm, options);
|
|
175
|
+
}
|
|
176
|
+
export function sign(algorithm, data, options, callback) {
|
|
177
|
+
if (algorithm != null) {
|
|
178
|
+
validateString(algorithm, "algorithm");
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
algorithm = undefined;
|
|
182
|
+
}
|
|
183
|
+
if (!isArrayBufferView(data)) {
|
|
184
|
+
throw new ERR_INVALID_ARG_TYPE("data", ["Buffer", "TypedArray", "DataView"], data);
|
|
185
|
+
}
|
|
186
|
+
if (!options) {
|
|
187
|
+
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
|
|
188
|
+
}
|
|
189
|
+
const key = getPrivateKey(options);
|
|
190
|
+
// Options specific to RSA
|
|
191
|
+
const rsaPadding = getIntOption("padding", options);
|
|
192
|
+
const pssSaltLength = getIntOption("saltLength", options);
|
|
193
|
+
// Options specific to (EC)DSA
|
|
194
|
+
const dsaSigEnc = getDSASignatureEncoding(options);
|
|
195
|
+
if (callback === undefined) {
|
|
196
|
+
return Buffer.from(cryptoImpl.signOneShot(key, algorithm, data, rsaPadding, pssSaltLength, dsaSigEnc));
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
const signature = Buffer.from(cryptoImpl.signOneShot(key, algorithm, data, rsaPadding, pssSaltLength, dsaSigEnc));
|
|
200
|
+
queueMicrotask(() => callback(null, signature));
|
|
201
|
+
}
|
|
202
|
+
catch (err) {
|
|
203
|
+
queueMicrotask(() => callback(err));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
export function verify(algorithm, data, options, signature, callback) {
|
|
207
|
+
// Node.js allows the algorithm to be either undefined or null, in which
|
|
208
|
+
// case we just normalize it to undefined.
|
|
209
|
+
if (algorithm != null) {
|
|
210
|
+
validateString(algorithm, "algorithm");
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
algorithm = undefined;
|
|
214
|
+
}
|
|
215
|
+
if (!isArrayBufferView(data)) {
|
|
216
|
+
throw new ERR_INVALID_ARG_TYPE("data", ["Buffer", "TypedArray", "DataView"], data);
|
|
217
|
+
}
|
|
218
|
+
if (!isArrayBufferView(signature)) {
|
|
219
|
+
throw new ERR_INVALID_ARG_TYPE("signature", ["Buffer", "TypedArray", "DataView"], signature);
|
|
220
|
+
}
|
|
221
|
+
if (!options) {
|
|
222
|
+
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
|
|
223
|
+
}
|
|
224
|
+
let key;
|
|
225
|
+
if (options instanceof CryptoKey) {
|
|
226
|
+
key = options;
|
|
227
|
+
}
|
|
228
|
+
else if (isKeyObject(options)) {
|
|
229
|
+
key = getKeyObjectHandle(options);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
key = getKeyObjectHandle(createPublicKey(options));
|
|
233
|
+
}
|
|
234
|
+
if (!(key instanceof CryptoKey) && !isKeyObject(key)) {
|
|
235
|
+
throw new ERR_INVALID_ARG_TYPE("options", ["KeyObject", "CryptoKey", "object"], key);
|
|
236
|
+
}
|
|
237
|
+
// Options specific to RSA
|
|
238
|
+
const rsaPadding = getIntOption("padding", options);
|
|
239
|
+
const pssSaltLength = getIntOption("saltLength", options);
|
|
240
|
+
// Options specific to (EC)DSA
|
|
241
|
+
const dsaSigEnc = getDSASignatureEncoding(options);
|
|
242
|
+
if (callback === undefined) {
|
|
243
|
+
return cryptoImpl.verifyOneShot(key, algorithm, data, signature, rsaPadding, pssSaltLength, dsaSigEnc);
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
callback(null, cryptoImpl.verifyOneShot(key, algorithm, data, signature, rsaPadding, pssSaltLength, dsaSigEnc));
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
queueMicrotask(() => callback(err));
|
|
250
|
+
}
|
|
251
|
+
return; // explicit return is necessary to squelch typescript warning.
|
|
252
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare const BaseTransform: typeof import("node:stream").Transform;
|
|
2
|
+
export declare class Hmac extends BaseTransform {
|
|
3
|
+
constructor(algorithm: string, key: unknown, options?: any);
|
|
4
|
+
update(data: unknown, encoding?: string): this;
|
|
5
|
+
digest(encoding?: string): any;
|
|
6
|
+
_transform(chunk: any, encoding: any, callback: any): void;
|
|
7
|
+
_flush(callback: any): void;
|
|
8
|
+
}
|
|
9
|
+
declare class CipherTransform extends BaseTransform {
|
|
10
|
+
constructor(decrypt: boolean, algorithm: string, key: unknown, iv: unknown, options?: any);
|
|
11
|
+
update(data: unknown, inputEncoding?: string, outputEncoding?: string): any;
|
|
12
|
+
final(encoding?: string): any;
|
|
13
|
+
setAutoPadding(value?: boolean): this;
|
|
14
|
+
setAAD(data: unknown, options?: any): this;
|
|
15
|
+
getAuthTag(): any;
|
|
16
|
+
setAuthTag(data: unknown): this;
|
|
17
|
+
_transform(chunk: any, encoding: any, callback: any): void;
|
|
18
|
+
_flush(callback: any): void;
|
|
19
|
+
}
|
|
20
|
+
export declare class Cipheriv extends CipherTransform {
|
|
21
|
+
constructor(algorithm: string, key: unknown, iv: unknown, options?: any);
|
|
22
|
+
}
|
|
23
|
+
export declare class Decipheriv extends CipherTransform {
|
|
24
|
+
constructor(algorithm: string, key: unknown, iv: unknown, options?: any);
|
|
25
|
+
}
|
|
26
|
+
export declare function createHmac(algorithm: string, key: unknown, options?: any): Hmac;
|
|
27
|
+
export declare function createCipheriv(algorithm: string, key: unknown, iv: unknown, options?: any): Cipheriv;
|
|
28
|
+
export declare function createDecipheriv(algorithm: string, key: unknown, iv: unknown, options?: any): Decipheriv;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { Transform } from "../stream/transform.js";
|
|
3
|
+
// @ts-expect-error dependency has no declarations
|
|
4
|
+
import original from "crypto-browserify/index.js";
|
|
5
|
+
import { secretMaterial } from "./keys.js";
|
|
6
|
+
const BaseTransform = Transform;
|
|
7
|
+
const handles = new WeakMap();
|
|
8
|
+
export class Hmac extends BaseTransform {
|
|
9
|
+
constructor(algorithm, key, options) {
|
|
10
|
+
super(options);
|
|
11
|
+
handles.set(this, original.createHmac(algorithm, typeof key === "string"
|
|
12
|
+
? Buffer.from(key, options?.encoding)
|
|
13
|
+
: secretMaterial(key)));
|
|
14
|
+
}
|
|
15
|
+
update(data, encoding) {
|
|
16
|
+
handles.get(this).update(data, encoding);
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
digest(encoding) {
|
|
20
|
+
return handles.get(this).digest(encoding);
|
|
21
|
+
}
|
|
22
|
+
_transform(chunk, encoding, callback) {
|
|
23
|
+
try {
|
|
24
|
+
this.update(chunk, encoding);
|
|
25
|
+
callback();
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
callback(error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
_flush(callback) {
|
|
32
|
+
try {
|
|
33
|
+
this.push(this.digest());
|
|
34
|
+
callback();
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
callback(error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
class CipherTransform extends BaseTransform {
|
|
42
|
+
constructor(decrypt, algorithm, key, iv, options) {
|
|
43
|
+
super(options);
|
|
44
|
+
handles.set(this, (decrypt ? original.createDecipheriv : original.createCipheriv)(algorithm, secretMaterial(key), iv, options));
|
|
45
|
+
}
|
|
46
|
+
update(data, inputEncoding, outputEncoding) {
|
|
47
|
+
return handles.get(this).update(data, inputEncoding, outputEncoding);
|
|
48
|
+
}
|
|
49
|
+
final(encoding) {
|
|
50
|
+
return handles.get(this).final(encoding);
|
|
51
|
+
}
|
|
52
|
+
setAutoPadding(value) {
|
|
53
|
+
handles.get(this).setAutoPadding(value);
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
setAAD(data, options) {
|
|
57
|
+
handles.get(this).setAAD(data, options);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
getAuthTag() {
|
|
61
|
+
return handles.get(this).getAuthTag();
|
|
62
|
+
}
|
|
63
|
+
setAuthTag(data) {
|
|
64
|
+
handles.get(this).setAuthTag(data);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
_transform(chunk, encoding, callback) {
|
|
68
|
+
try {
|
|
69
|
+
this.push(this.update(chunk, encoding));
|
|
70
|
+
callback();
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
callback(error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
_flush(callback) {
|
|
77
|
+
try {
|
|
78
|
+
this.push(this.final());
|
|
79
|
+
callback();
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
callback(error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export class Cipheriv extends CipherTransform {
|
|
87
|
+
constructor(algorithm, key, iv, options) {
|
|
88
|
+
super(false, algorithm, key, iv, options);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export class Decipheriv extends CipherTransform {
|
|
92
|
+
constructor(algorithm, key, iv, options) {
|
|
93
|
+
super(true, algorithm, key, iv, options);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export function createHmac(algorithm, key, options) {
|
|
97
|
+
return new Hmac(algorithm, key, options);
|
|
98
|
+
}
|
|
99
|
+
export function createCipheriv(algorithm, key, iv, options) {
|
|
100
|
+
return new Cipheriv(algorithm, key, iv, options);
|
|
101
|
+
}
|
|
102
|
+
export function createDecipheriv(algorithm, key, iv, options) {
|
|
103
|
+
return new Decipheriv(algorithm, key, iv, options);
|
|
104
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE } from "../net/errors.js";
|
|
3
|
+
export function validateInteger(value, name, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER) {
|
|
4
|
+
if (typeof value !== "number")
|
|
5
|
+
throw new ERR_INVALID_ARG_TYPE(name, "number", value);
|
|
6
|
+
if (!Number.isInteger(value))
|
|
7
|
+
throw new ERR_OUT_OF_RANGE(name, "an integer", value);
|
|
8
|
+
if (value < min || value > max)
|
|
9
|
+
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
|
|
10
|
+
}
|
|
11
|
+
export function bytes(value, name) {
|
|
12
|
+
if (typeof value === "string")
|
|
13
|
+
return Buffer.from(value);
|
|
14
|
+
if (value instanceof ArrayBuffer)
|
|
15
|
+
return Buffer.from(value);
|
|
16
|
+
if (ArrayBuffer.isView(value))
|
|
17
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
18
|
+
throw new ERR_INVALID_ARG_TYPE(name, ["string", "ArrayBuffer", "Buffer", "TypedArray", "DataView"], value);
|
|
19
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { NativeX509 } from "./native-crypto.js";
|
|
2
|
+
import { PublicKeyObject, PrivateKeyObject } from "./keys.js";
|
|
3
|
+
export declare class X509Certificate {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(buffer: ArrayBufferView | ArrayBuffer | NativeX509 | string);
|
|
6
|
+
get subject(): any;
|
|
7
|
+
get subjectAltName(): any;
|
|
8
|
+
get issuer(): any;
|
|
9
|
+
get issuerCertificate(): any;
|
|
10
|
+
get infoAccess(): any;
|
|
11
|
+
get validFrom(): any;
|
|
12
|
+
get validTo(): any;
|
|
13
|
+
get fingerprint(): any;
|
|
14
|
+
get fingerprint256(): any;
|
|
15
|
+
get fingerprint512(): any;
|
|
16
|
+
get keyUsage(): any;
|
|
17
|
+
get serialNumber(): any;
|
|
18
|
+
get raw(): any;
|
|
19
|
+
get publicKey(): any;
|
|
20
|
+
toString(): any;
|
|
21
|
+
toJSON(): any;
|
|
22
|
+
get ca(): any;
|
|
23
|
+
checkHost(name: string, options?: any): string | undefined;
|
|
24
|
+
checkEmail(email: string, options?: any): string | undefined;
|
|
25
|
+
checkIP(ip: string, options?: any): string | undefined;
|
|
26
|
+
checkIssued(otherCert: X509Certificate): boolean;
|
|
27
|
+
checkPrivateKey(pkey: PrivateKeyObject): boolean;
|
|
28
|
+
verify(pkey: PublicKeyObject): boolean;
|
|
29
|
+
toLegacyObject(): any;
|
|
30
|
+
}
|