@tenonai/yew 0.0.0-stage → 1.4.0
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/CHANGELOG.md +296 -0
- package/LICENSE +21 -0
- package/README.md +55 -2
- package/THIRD-PARTY-NOTICES.md +122 -0
- package/bin/yew.js +9 -0
- package/dist/cli/app.js +198 -0
- package/dist/cli/export.js +57 -0
- package/dist/cli/io.js +65 -0
- package/dist/cli/key.js +113 -0
- package/dist/cli/main.js +44 -0
- package/dist/cli/mcp.js +76 -0
- package/dist/cli/remote.js +116 -0
- package/dist/cli/run.js +72 -0
- package/dist/cli/schedule.js +123 -0
- package/dist/cli/send.js +126 -0
- package/dist/cli/serve.js +301 -0
- package/dist/cli/service.js +241 -0
- package/dist/cli/system.js +44 -0
- package/dist/remote/access.js +107 -0
- package/dist/remote/connector.js +278 -0
- package/dist/remote/identity.js +71 -0
- package/dist/remote/noise.js +287 -0
- package/dist/remote/protocol.js +51 -0
- package/dist/remote/push.js +37 -0
- package/dist/server/agent/compaction.js +242 -0
- package/dist/server/agent/context.js +119 -0
- package/dist/server/agent/held.js +48 -0
- package/dist/server/agent/helper.js +122 -0
- package/dist/server/agent/mailroom.js +510 -0
- package/dist/server/agent/prompt.js +268 -0
- package/dist/server/agent/runner.js +1159 -0
- package/dist/server/artifacts.js +216 -0
- package/dist/server/budget.js +88 -0
- package/dist/server/data-export.js +105 -0
- package/dist/server/data-usage.js +143 -0
- package/dist/server/db/database.js +49 -0
- package/dist/server/db/migrations.js +347 -0
- package/dist/server/errors.js +16 -0
- package/dist/server/events.js +17 -0
- package/dist/server/export.js +378 -0
- package/dist/server/folder-picker.js +32 -0
- package/dist/server/from-jig.js +98 -0
- package/dist/server/http/agents.js +78 -0
- package/dist/server/http/api.js +468 -0
- package/dist/server/http/app.js +111 -0
- package/dist/server/http/artifacts.js +53 -0
- package/dist/server/http/data.js +54 -0
- package/dist/server/http/devices.js +47 -0
- package/dist/server/http/events.js +134 -0
- package/dist/server/http/idempotency.js +53 -0
- package/dist/server/http/input.js +106 -0
- package/dist/server/http/mail.js +52 -0
- package/dist/server/http/mcp.js +134 -0
- package/dist/server/http/notes.js +91 -0
- package/dist/server/http/problem.js +55 -0
- package/dist/server/http/providers.js +131 -0
- package/dist/server/http/schedules.js +58 -0
- package/dist/server/http/search.js +42 -0
- package/dist/server/http/security.js +168 -0
- package/dist/server/http/skills.js +53 -0
- package/dist/server/http/system.js +26 -0
- package/dist/server/ids.js +30 -0
- package/dist/server/keys.js +51 -0
- package/dist/server/mail/envelope.js +152 -0
- package/dist/server/mail/post.js +603 -0
- package/dist/server/mail/store.js +279 -0
- package/dist/server/manage.js +601 -0
- package/dist/server/mcp-builtin.js +179 -0
- package/dist/server/mcp-config.js +305 -0
- package/dist/server/mcp-oauth.js +448 -0
- package/dist/server/mcp.js +668 -0
- package/dist/server/model/gateway.js +410 -0
- package/dist/server/paths.js +34 -0
- package/dist/server/permissions/engine.js +249 -0
- package/dist/server/permissions/paths.js +93 -0
- package/dist/server/permissions/shell.js +635 -0
- package/dist/server/push.js +345 -0
- package/dist/server/reveal.js +36 -0
- package/dist/server/runtime.js +17 -0
- package/dist/server/schedules.js +356 -0
- package/dist/server/settings.js +80 -0
- package/dist/server/skills.js +262 -0
- package/dist/server/store.js +1077 -0
- package/dist/server/tools/files.js +318 -0
- package/dist/server/tools/html.js +221 -0
- package/dist/server/tools/jobs.js +38 -0
- package/dist/server/tools/mail.js +680 -0
- package/dist/server/tools/match.js +81 -0
- package/dist/server/tools/misc.js +253 -0
- package/dist/server/tools/process.js +128 -0
- package/dist/server/tools/read-state.js +40 -0
- package/dist/server/tools/registry.js +43 -0
- package/dist/server/tools/search.js +175 -0
- package/dist/server/tools/shell.js +124 -0
- package/dist/server/tools/types.js +43 -0
- package/dist/server/tools/web-search.js +198 -0
- package/dist/server/tools/web.js +240 -0
- package/dist/shared/cron.js +105 -0
- package/dist/shared/mcp-presets.js +170 -0
- package/dist/shared/messages.js +45 -0
- package/dist/shared/policy.js +42 -0
- package/dist/shared/types.js +34 -0
- package/dist/shared/version.js +8 -0
- package/mcp-builtin/package-lock.json +3398 -0
- package/mcp-builtin/package.json +14 -0
- package/mcp-builtin/presets/gitee/package-lock.json +105 -0
- package/mcp-builtin/presets/gitee/package.json +10 -0
- package/mcp-builtin/presets/notion/package-lock.json +2243 -0
- package/mcp-builtin/presets/notion/package.json +10 -0
- package/mcp-builtin/presets/sentry/package-lock.json +28 -0
- package/mcp-builtin/presets/sentry/package.json +10 -0
- package/package.json +75 -5
- package/web/dist/THIRD-PARTY-NOTICES.txt +1944 -0
- package/web/dist/assets/index-B9oWPSod.css +1 -0
- package/web/dist/assets/index-C5qBMzFX.js +68 -0
- package/web/dist/icons/app-icon.png +0 -0
- package/web/dist/icons/apple-touch-icon.png +0 -0
- package/web/dist/icons/favicon-16.png +0 -0
- package/web/dist/icons/favicon-32.png +0 -0
- package/web/dist/icons/favicon-64.png +0 -0
- package/web/dist/icons/tenon-mark.svg +1 -0
- package/web/dist/icons/yew-mark.svg +1 -0
- package/web/dist/index.html +18 -0
- package/web/dist/mascot/gift.webp +0 -0
- package/web/dist/mascot/greeting.webp +0 -0
- package/web/dist/mascot/happy.webp +0 -0
- package/web/dist/mascot/lost.webp +0 -0
- package/web/dist/mascot/shush.webp +0 -0
- package/web/dist/mascot/sleepy.webp +0 -0
- package/web/dist/mascot/sorry.webp +0 -0
- package/web/dist/mascot/thinking.webp +0 -0
- package/web/dist/mascot/waking.webp +0 -0
- package/web/dist/mascot/writing.webp +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createHash, createPrivateKey, createPublicKey, generateKeyPairSync, randomBytes, sign, timingSafeEqual, } from "node:crypto";
|
|
2
|
+
import { publicKeyOf } from "./noise.js";
|
|
3
|
+
import { CHALLENGE_PREFIX, desktopIdOf, pairingLink } from "./protocol.js";
|
|
4
|
+
const ED25519_PKCS8 = Buffer.from("302e020100300506032b657004220420", "hex");
|
|
5
|
+
const ED25519_SPKI_LENGTH = 12;
|
|
6
|
+
export async function loadIdentity(keys) {
|
|
7
|
+
let noise = await keys.get("noise");
|
|
8
|
+
if (!noise) {
|
|
9
|
+
const { privateKey } = generateKeyPairSync("x25519");
|
|
10
|
+
noise = privateKey.export({ format: "der", type: "pkcs8" }).subarray(16).toString("base64url");
|
|
11
|
+
await keys.set("noise", noise);
|
|
12
|
+
}
|
|
13
|
+
let signingRaw = await keys.get("signing");
|
|
14
|
+
if (!signingRaw) {
|
|
15
|
+
const { privateKey } = generateKeyPairSync("ed25519");
|
|
16
|
+
signingRaw = privateKey
|
|
17
|
+
.export({ format: "der", type: "pkcs8" })
|
|
18
|
+
.subarray(ED25519_PKCS8.length)
|
|
19
|
+
.toString("base64url");
|
|
20
|
+
await keys.set("signing", signingRaw);
|
|
21
|
+
}
|
|
22
|
+
const noisePrivate = Buffer.from(noise, "base64url");
|
|
23
|
+
const signing = createPrivateKey({
|
|
24
|
+
key: Buffer.concat([ED25519_PKCS8, Buffer.from(signingRaw, "base64url")]),
|
|
25
|
+
format: "der",
|
|
26
|
+
type: "pkcs8",
|
|
27
|
+
});
|
|
28
|
+
const signingPublic = createPublicKey(signing)
|
|
29
|
+
.export({ format: "der", type: "spki" })
|
|
30
|
+
.subarray(ED25519_SPKI_LENGTH);
|
|
31
|
+
return {
|
|
32
|
+
noise: { privateKey: noisePrivate, publicKey: publicKeyOf(noisePrivate) },
|
|
33
|
+
signing,
|
|
34
|
+
signingPublic,
|
|
35
|
+
desktopId: desktopIdOf(signingPublic),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function signChallenge(identity, nonce) {
|
|
39
|
+
return sign(null, Buffer.from(CHALLENGE_PREFIX + nonce), identity.signing).toString("base64url");
|
|
40
|
+
}
|
|
41
|
+
const PAIRING_MS = 5 * 60 * 1000;
|
|
42
|
+
const sha256 = (value) => createHash("sha256").update(value).digest();
|
|
43
|
+
/**
|
|
44
|
+
* A new pairing: the QR code's content, valid five minutes and once. Only the secret's hash is kept,
|
|
45
|
+
* in the database, so `yew remote pair` and the running service share it.
|
|
46
|
+
*/
|
|
47
|
+
export function createPairing(store, identity, options) {
|
|
48
|
+
const secret = randomBytes(18).toString("base64url");
|
|
49
|
+
const expiresAt = Date.now() + PAIRING_MS;
|
|
50
|
+
store.setSetting("pairing", { hash: sha256(secret).toString("hex"), expiresAt });
|
|
51
|
+
return {
|
|
52
|
+
link: pairingLink({
|
|
53
|
+
relay: options.relay,
|
|
54
|
+
desktop: identity.desktopId,
|
|
55
|
+
key: identity.noise.publicKey.toString("base64url"),
|
|
56
|
+
secret,
|
|
57
|
+
name: options.name,
|
|
58
|
+
}),
|
|
59
|
+
expiresAt,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** Checks a phone's pairing secret and uses it up; a wrong one also ends the pairing. */
|
|
63
|
+
export function consumePairing(store, secret) {
|
|
64
|
+
const pending = store.getSetting("pairing", null);
|
|
65
|
+
store.setSetting("pairing", null);
|
|
66
|
+
if (!pending || pending.expiresAt < Date.now())
|
|
67
|
+
return false;
|
|
68
|
+
const expected = Buffer.from(pending.hash, "hex");
|
|
69
|
+
const given = sha256(secret);
|
|
70
|
+
return expected.length === given.length && timingSafeEqual(expected, given);
|
|
71
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, createHmac, createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* Noise_IK_25519_ChaChaPoly_SHA256 (Noise Protocol Framework, revision 34), Yew's end-to-end encryption
|
|
4
|
+
* between the Mac and a phone through the relay (M3 plan, section 4). The phone (initiator) knows the
|
|
5
|
+
* Mac's static key from the pairing QR code; the Mac (responder) learns the phone's static key in the
|
|
6
|
+
* first message and accepts it only if it is paired, or if the first payload carries a valid pairing
|
|
7
|
+
* secret. The relay sees only ciphertext and cannot pose as either end.
|
|
8
|
+
*/
|
|
9
|
+
const PROTOCOL = "Noise_IK_25519_ChaChaPoly_SHA256";
|
|
10
|
+
const KEY_LENGTH = 32;
|
|
11
|
+
const TAG_LENGTH = 16;
|
|
12
|
+
/** A Noise message is at most 65535 bytes (section 3). */
|
|
13
|
+
export const MAX_MESSAGE = 65535;
|
|
14
|
+
const EMPTY = Buffer.alloc(0);
|
|
15
|
+
export function generateKeyPair() {
|
|
16
|
+
const { publicKey, privateKey } = generateKeyPairSync("x25519");
|
|
17
|
+
return { publicKey: rawPublic(publicKey), privateKey: rawPrivate(privateKey) };
|
|
18
|
+
}
|
|
19
|
+
/** The public half of a raw X25519 private key. */
|
|
20
|
+
export function publicKeyOf(privateKey) {
|
|
21
|
+
return rawPublic(createPublicKey(privateKeyObject(privateKey)));
|
|
22
|
+
}
|
|
23
|
+
// Raw X25519 keys wrapped in their fixed DER prefixes (RFC 8410), so Node can use them.
|
|
24
|
+
const PKCS8 = Buffer.from("302e020100300506032b656e04220420", "hex");
|
|
25
|
+
const SPKI = Buffer.from("302a300506032b656e032100", "hex");
|
|
26
|
+
const privateKeyObject = (raw) => createPrivateKey({ key: Buffer.concat([PKCS8, raw]), format: "der", type: "pkcs8" });
|
|
27
|
+
const publicKeyObject = (raw) => createPublicKey({ key: Buffer.concat([SPKI, raw]), format: "der", type: "spki" });
|
|
28
|
+
function rawPublic(key) {
|
|
29
|
+
return key.export({ format: "der", type: "spki" }).subarray(SPKI.length);
|
|
30
|
+
}
|
|
31
|
+
function rawPrivate(key) {
|
|
32
|
+
return key.export({ format: "der", type: "pkcs8" }).subarray(PKCS8.length);
|
|
33
|
+
}
|
|
34
|
+
export function dh(pair, publicKey) {
|
|
35
|
+
if (publicKey.length !== KEY_LENGTH)
|
|
36
|
+
throw new NoiseError("A public key has the wrong length.");
|
|
37
|
+
const shared = diffieHellman({
|
|
38
|
+
privateKey: privateKeyObject(pair.privateKey),
|
|
39
|
+
publicKey: publicKeyObject(publicKey),
|
|
40
|
+
});
|
|
41
|
+
// A low-order point gives all zeros; refuse it rather than key a channel with it.
|
|
42
|
+
if (shared.every((byte) => byte === 0))
|
|
43
|
+
throw new NoiseError("A public key is not usable.");
|
|
44
|
+
return shared;
|
|
45
|
+
}
|
|
46
|
+
const hash = (...parts) => {
|
|
47
|
+
const h = createHash("sha256");
|
|
48
|
+
for (const part of parts)
|
|
49
|
+
h.update(part);
|
|
50
|
+
return h.digest();
|
|
51
|
+
};
|
|
52
|
+
const hmac = (key, ...parts) => {
|
|
53
|
+
const h = createHmac("sha256", key);
|
|
54
|
+
for (const part of parts)
|
|
55
|
+
h.update(part);
|
|
56
|
+
return h.digest();
|
|
57
|
+
};
|
|
58
|
+
/** HKDF as Noise defines it (section 4.3), with two outputs. */
|
|
59
|
+
function hkdf(chainingKey, input) {
|
|
60
|
+
const temp = hmac(chainingKey, input);
|
|
61
|
+
const first = hmac(temp, Buffer.from([1]));
|
|
62
|
+
const second = hmac(temp, first, Buffer.from([2]));
|
|
63
|
+
return [first, second];
|
|
64
|
+
}
|
|
65
|
+
/** The ChaChaPoly nonce: 32 bits of zeros, then the counter as a little-endian 64-bit number. */
|
|
66
|
+
function nonce(n) {
|
|
67
|
+
const buffer = Buffer.alloc(12);
|
|
68
|
+
buffer.writeBigUInt64LE(BigInt(n), 4);
|
|
69
|
+
return buffer;
|
|
70
|
+
}
|
|
71
|
+
export class NoiseError extends Error {
|
|
72
|
+
}
|
|
73
|
+
/** One direction of an encrypted channel (Noise CipherState). */
|
|
74
|
+
export class CipherState {
|
|
75
|
+
#key;
|
|
76
|
+
#n = 0;
|
|
77
|
+
constructor(key = null) {
|
|
78
|
+
this.#key = key;
|
|
79
|
+
}
|
|
80
|
+
get hasKey() {
|
|
81
|
+
return this.#key !== null;
|
|
82
|
+
}
|
|
83
|
+
encrypt(ad, plaintext) {
|
|
84
|
+
if (!this.#key)
|
|
85
|
+
return plaintext;
|
|
86
|
+
const cipher = createCipheriv("chacha20-poly1305", this.#key, nonce(this.#next()), {
|
|
87
|
+
authTagLength: TAG_LENGTH,
|
|
88
|
+
});
|
|
89
|
+
cipher.setAAD(ad, { plaintextLength: plaintext.length });
|
|
90
|
+
const body = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
91
|
+
return Buffer.concat([body, cipher.getAuthTag()]);
|
|
92
|
+
}
|
|
93
|
+
decrypt(ad, ciphertext) {
|
|
94
|
+
if (!this.#key)
|
|
95
|
+
return ciphertext;
|
|
96
|
+
if (ciphertext.length < TAG_LENGTH)
|
|
97
|
+
throw new NoiseError("A message is too short.");
|
|
98
|
+
const decipher = createDecipheriv("chacha20-poly1305", this.#key, nonce(this.#n), {
|
|
99
|
+
authTagLength: TAG_LENGTH,
|
|
100
|
+
});
|
|
101
|
+
decipher.setAAD(ad, { plaintextLength: ciphertext.length - TAG_LENGTH });
|
|
102
|
+
decipher.setAuthTag(ciphertext.subarray(ciphertext.length - TAG_LENGTH));
|
|
103
|
+
let plaintext;
|
|
104
|
+
try {
|
|
105
|
+
plaintext = Buffer.concat([
|
|
106
|
+
decipher.update(ciphertext.subarray(0, ciphertext.length - TAG_LENGTH)),
|
|
107
|
+
decipher.final(),
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
throw new NoiseError("A message failed to decrypt.");
|
|
112
|
+
}
|
|
113
|
+
// The counter moves only after a message decrypts, so a forged one cannot desynchronize it.
|
|
114
|
+
this.#next();
|
|
115
|
+
return plaintext;
|
|
116
|
+
}
|
|
117
|
+
#next() {
|
|
118
|
+
if (this.#n >= Number.MAX_SAFE_INTEGER)
|
|
119
|
+
throw new NoiseError("The channel ran out of nonces.");
|
|
120
|
+
return this.#n++;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Noise SymmetricState (section 5.2). */
|
|
124
|
+
class SymmetricState {
|
|
125
|
+
#cipher = new CipherState();
|
|
126
|
+
#ck;
|
|
127
|
+
h;
|
|
128
|
+
constructor() {
|
|
129
|
+
// The protocol name is exactly 32 bytes, so it is the initial hash as it is.
|
|
130
|
+
this.h = Buffer.from(PROTOCOL, "ascii");
|
|
131
|
+
this.#ck = this.h;
|
|
132
|
+
}
|
|
133
|
+
mixKey(input) {
|
|
134
|
+
const [ck, key] = hkdf(this.#ck, input);
|
|
135
|
+
this.#ck = ck;
|
|
136
|
+
this.#cipher = new CipherState(key);
|
|
137
|
+
}
|
|
138
|
+
mixHash(data) {
|
|
139
|
+
this.h = hash(this.h, data);
|
|
140
|
+
}
|
|
141
|
+
encryptAndHash(plaintext) {
|
|
142
|
+
const ciphertext = this.#cipher.encrypt(this.h, plaintext);
|
|
143
|
+
this.mixHash(ciphertext);
|
|
144
|
+
return ciphertext;
|
|
145
|
+
}
|
|
146
|
+
decryptAndHash(ciphertext) {
|
|
147
|
+
const plaintext = this.#cipher.decrypt(this.h, ciphertext);
|
|
148
|
+
this.mixHash(ciphertext);
|
|
149
|
+
return plaintext;
|
|
150
|
+
}
|
|
151
|
+
split() {
|
|
152
|
+
const [first, second] = hkdf(this.#ck, EMPTY);
|
|
153
|
+
return [new CipherState(first), new CipherState(second)];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The phone's side: sends `e, es, s, ss` with a payload, reads `e, ee, se`. The responder's static key
|
|
158
|
+
* must be known in advance (from the pairing QR code).
|
|
159
|
+
*/
|
|
160
|
+
export class Initiator {
|
|
161
|
+
#state = new SymmetricState();
|
|
162
|
+
#static;
|
|
163
|
+
#ephemeral;
|
|
164
|
+
#remoteStatic;
|
|
165
|
+
constructor(options) {
|
|
166
|
+
this.#static = options.static;
|
|
167
|
+
this.#remoteStatic = options.remoteStatic;
|
|
168
|
+
this.#ephemeral = options.ephemeral ?? generateKeyPair();
|
|
169
|
+
this.#state.mixHash(options.prologue);
|
|
170
|
+
this.#state.mixHash(this.#remoteStatic);
|
|
171
|
+
}
|
|
172
|
+
/** Message 1: `e, es, s, ss` and the payload. */
|
|
173
|
+
writeFirst(payload) {
|
|
174
|
+
const state = this.#state;
|
|
175
|
+
state.mixHash(this.#ephemeral.publicKey);
|
|
176
|
+
state.mixKey(dh(this.#ephemeral, this.#remoteStatic));
|
|
177
|
+
const encryptedStatic = state.encryptAndHash(this.#static.publicKey);
|
|
178
|
+
state.mixKey(dh(this.#static, this.#remoteStatic));
|
|
179
|
+
const encryptedPayload = state.encryptAndHash(payload);
|
|
180
|
+
return checked(Buffer.concat([this.#ephemeral.publicKey, encryptedStatic, encryptedPayload]));
|
|
181
|
+
}
|
|
182
|
+
/** Message 2: `e, ee, se`; returns the responder's payload and the channel. */
|
|
183
|
+
readSecond(message) {
|
|
184
|
+
if (message.length < KEY_LENGTH + TAG_LENGTH)
|
|
185
|
+
throw new NoiseError("A message is too short.");
|
|
186
|
+
const state = this.#state;
|
|
187
|
+
const remoteEphemeral = message.subarray(0, KEY_LENGTH);
|
|
188
|
+
state.mixHash(remoteEphemeral);
|
|
189
|
+
state.mixKey(dh(this.#ephemeral, remoteEphemeral));
|
|
190
|
+
state.mixKey(dh(this.#static, remoteEphemeral));
|
|
191
|
+
const payload = state.decryptAndHash(message.subarray(KEY_LENGTH));
|
|
192
|
+
const [first, second] = state.split();
|
|
193
|
+
return { payload, session: { send: first, receive: second, handshakeHash: state.h } };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/** The Mac's side: reads `e, es, s, ss`, decides whether to accept the phone, writes `e, ee, se`. */
|
|
197
|
+
export class Responder {
|
|
198
|
+
#state = new SymmetricState();
|
|
199
|
+
#static;
|
|
200
|
+
#ephemeral;
|
|
201
|
+
#remoteEphemeral = null;
|
|
202
|
+
remoteStatic = null;
|
|
203
|
+
constructor(options) {
|
|
204
|
+
this.#static = options.static;
|
|
205
|
+
this.#ephemeral = options.ephemeral ?? generateKeyPair();
|
|
206
|
+
this.#state.mixHash(options.prologue);
|
|
207
|
+
this.#state.mixHash(this.#static.publicKey);
|
|
208
|
+
}
|
|
209
|
+
/** Message 1; afterwards `remoteStatic` is the phone's static key, authenticated. */
|
|
210
|
+
readFirst(message) {
|
|
211
|
+
if (message.length < KEY_LENGTH + KEY_LENGTH + 2 * TAG_LENGTH) {
|
|
212
|
+
throw new NoiseError("A message is too short.");
|
|
213
|
+
}
|
|
214
|
+
const state = this.#state;
|
|
215
|
+
const remoteEphemeral = message.subarray(0, KEY_LENGTH);
|
|
216
|
+
state.mixHash(remoteEphemeral);
|
|
217
|
+
state.mixKey(dh(this.#static, remoteEphemeral));
|
|
218
|
+
const remoteStatic = state.decryptAndHash(message.subarray(KEY_LENGTH, KEY_LENGTH + KEY_LENGTH + TAG_LENGTH));
|
|
219
|
+
state.mixKey(dh(this.#static, remoteStatic));
|
|
220
|
+
const payload = state.decryptAndHash(message.subarray(KEY_LENGTH + KEY_LENGTH + TAG_LENGTH));
|
|
221
|
+
this.#remoteEphemeral = Buffer.from(remoteEphemeral);
|
|
222
|
+
this.remoteStatic = Buffer.from(remoteStatic);
|
|
223
|
+
return payload;
|
|
224
|
+
}
|
|
225
|
+
/** Message 2 with the payload; returns it and the channel. */
|
|
226
|
+
writeSecond(payload) {
|
|
227
|
+
if (!this.#remoteEphemeral || !this.remoteStatic) {
|
|
228
|
+
throw new NoiseError("Read the first message first.");
|
|
229
|
+
}
|
|
230
|
+
const state = this.#state;
|
|
231
|
+
state.mixHash(this.#ephemeral.publicKey);
|
|
232
|
+
state.mixKey(dh(this.#ephemeral, this.#remoteEphemeral));
|
|
233
|
+
state.mixKey(dh(this.#ephemeral, this.remoteStatic));
|
|
234
|
+
const encryptedPayload = state.encryptAndHash(payload);
|
|
235
|
+
const [first, second] = state.split();
|
|
236
|
+
return {
|
|
237
|
+
message: checked(Buffer.concat([this.#ephemeral.publicKey, encryptedPayload])),
|
|
238
|
+
session: { send: second, receive: first, handshakeHash: state.h },
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function checked(message) {
|
|
243
|
+
if (message.length > MAX_MESSAGE)
|
|
244
|
+
throw new NoiseError("A handshake message is too long.");
|
|
245
|
+
return message;
|
|
246
|
+
}
|
|
247
|
+
/** The most a transport message carries: one Noise message less the tag and the chunk flag. */
|
|
248
|
+
const CHUNK = MAX_MESSAGE - TAG_LENGTH - 1;
|
|
249
|
+
/** Encrypts an application message of any length as one or more Noise messages. */
|
|
250
|
+
export function seal(cipher, message) {
|
|
251
|
+
const parts = [];
|
|
252
|
+
for (let offset = 0; offset < message.length || parts.length === 0; offset += CHUNK) {
|
|
253
|
+
const piece = message.subarray(offset, offset + CHUNK);
|
|
254
|
+
const last = offset + CHUNK >= message.length;
|
|
255
|
+
parts.push(cipher.encrypt(EMPTY, Buffer.concat([Buffer.from([last ? 1 : 0]), piece])));
|
|
256
|
+
}
|
|
257
|
+
return parts;
|
|
258
|
+
}
|
|
259
|
+
/** Joins decrypted chunks back into application messages. */
|
|
260
|
+
export class Opener {
|
|
261
|
+
#cipher;
|
|
262
|
+
#pending = [];
|
|
263
|
+
#size = 0;
|
|
264
|
+
#limit;
|
|
265
|
+
constructor(cipher, limit = 16 * 1024 * 1024) {
|
|
266
|
+
this.#cipher = cipher;
|
|
267
|
+
this.#limit = limit;
|
|
268
|
+
}
|
|
269
|
+
/** Returns a whole message when the last chunk of one arrives, otherwise null. */
|
|
270
|
+
open(ciphertext) {
|
|
271
|
+
if (ciphertext.length > MAX_MESSAGE)
|
|
272
|
+
throw new NoiseError("A message is too long.");
|
|
273
|
+
const plaintext = this.#cipher.decrypt(EMPTY, ciphertext);
|
|
274
|
+
if (plaintext.length === 0)
|
|
275
|
+
throw new NoiseError("A chunk is empty.");
|
|
276
|
+
this.#size += plaintext.length - 1;
|
|
277
|
+
if (this.#size > this.#limit)
|
|
278
|
+
throw new NoiseError("A message is too large.");
|
|
279
|
+
this.#pending.push(plaintext.subarray(1));
|
|
280
|
+
if (plaintext[0] !== 1)
|
|
281
|
+
return null;
|
|
282
|
+
const message = Buffer.concat(this.#pending);
|
|
283
|
+
this.#pending = [];
|
|
284
|
+
this.#size = 0;
|
|
285
|
+
return message;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* What the Mac, the relay and the phone agree on (M3 plan, sections 4–6). The relay reads only the
|
|
4
|
+
* outer frames; everything in `ChannelMessage` travels inside the Noise channel.
|
|
5
|
+
*/
|
|
6
|
+
/** Signed by the Mac when it connects, after the relay's nonce. */
|
|
7
|
+
export const CHALLENGE_PREFIX = "yew-relay-desktop:";
|
|
8
|
+
/** Mixed into every handshake with the Mac's id, so a handshake cannot be replayed to another Mac. */
|
|
9
|
+
export const PROLOGUE_PREFIX = "yew-remote/1:";
|
|
10
|
+
/** The relay closes a phone's connection with this code when its Mac is not connected. */
|
|
11
|
+
export const OFFLINE = 4404;
|
|
12
|
+
/** The id a Mac is reached by: the first 16 bytes of the SHA-256 of its Ed25519 public key. */
|
|
13
|
+
export function desktopIdOf(publicKey) {
|
|
14
|
+
return createHash("sha256").update(publicKey).digest().subarray(0, 16).toString("base64url");
|
|
15
|
+
}
|
|
16
|
+
export const prologueFor = (desktopId) => Buffer.from(PROLOGUE_PREFIX + desktopId);
|
|
17
|
+
/**
|
|
18
|
+
* A relay address Yew accepts: wss:// anywhere, ws:// only inside this network (this Mac, `.local`
|
|
19
|
+
* names, private addresses), for a test relay. The channel is end-to-end encrypted either way.
|
|
20
|
+
*/
|
|
21
|
+
export const RELAY_URL = /^wss:\/\/[^/\s]+|^ws:\/\/(127\.0\.0\.1|localhost|[\w-]+\.local|10\.\d+\.\d+\.\d+|192\.168\.\d+\.\d+|172\.(1[6-9]|2\d|3[01])\.\d+\.\d+)(:\d+)?(\/|$)/;
|
|
22
|
+
export function pairingLink(link) {
|
|
23
|
+
const query = new URLSearchParams({
|
|
24
|
+
r: link.relay,
|
|
25
|
+
d: link.desktop,
|
|
26
|
+
k: link.key,
|
|
27
|
+
s: link.secret,
|
|
28
|
+
n: link.name,
|
|
29
|
+
});
|
|
30
|
+
return `yew://pair?${query.toString()}`;
|
|
31
|
+
}
|
|
32
|
+
const PAIRING_PREFIX = "yew://pair?";
|
|
33
|
+
export function parsePairingLink(text) {
|
|
34
|
+
if (!text.startsWith(PAIRING_PREFIX))
|
|
35
|
+
return null;
|
|
36
|
+
const query = new URLSearchParams(text.slice(PAIRING_PREFIX.length));
|
|
37
|
+
const [relay, desktop, key, secret, name] = ["r", "d", "k", "s", "n"].map((k) => query.get(k));
|
|
38
|
+
if (!relay || !desktop || !key || !secret)
|
|
39
|
+
return null;
|
|
40
|
+
return { relay, desktop, key, secret, name: name ?? "" };
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Requests and responses inside the channel: the phone uses the Mac's local API as the web interface
|
|
44
|
+
* does. A streamed response (the event stream) comes as `head`, then `chunk`s, then `end`.
|
|
45
|
+
*
|
|
46
|
+
* A phone that puts `z: 1` on a request can read a compressed answer: a body of `COMPRESS_FROM`
|
|
47
|
+
* characters or more then comes back with `z: 1` and `body` as base64 of the raw DEFLATE of its
|
|
48
|
+
* UTF-8 text. A long transcript shrinks to about a fifth, which is what a slow link to the relay
|
|
49
|
+
* needs. Phones that do not ask get the text as before.
|
|
50
|
+
*/
|
|
51
|
+
export const COMPRESS_FROM = 1024;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, hkdfSync, randomBytes } from "node:crypto";
|
|
2
|
+
import { dh } from "./noise.js";
|
|
3
|
+
/**
|
|
4
|
+
* Notifications to a paired phone (M3 plan, section 8). The Mac encrypts what a notification says with
|
|
5
|
+
* a key only it and that phone can derive, from the Noise static keys they exchanged when pairing; the
|
|
6
|
+
* relay and Apple's push service carry the ciphertext and the phone's token, never the text. The
|
|
7
|
+
* phone's notification service extension decrypts it before it is shown.
|
|
8
|
+
*
|
|
9
|
+
* key = HKDF-SHA256(X25519(Mac static, phone static), salt = empty, info = "yew-push/1"), 32 bytes
|
|
10
|
+
* sealed = nonce (12 random bytes) | ChaCha20-Poly1305 ciphertext | tag (16 bytes)
|
|
11
|
+
*/
|
|
12
|
+
const INFO = "yew-push/1";
|
|
13
|
+
const NONCE = 12;
|
|
14
|
+
const TAG = 16;
|
|
15
|
+
export function pushKey(own, phonePublic) {
|
|
16
|
+
return Buffer.from(hkdfSync("sha256", dh(own, phonePublic), Buffer.alloc(0), INFO, 32));
|
|
17
|
+
}
|
|
18
|
+
export function sealPush(key, plaintext, nonce = randomBytes(NONCE)) {
|
|
19
|
+
const cipher = createCipheriv("chacha20-poly1305", key, nonce, { authTagLength: TAG });
|
|
20
|
+
return Buffer.concat([nonce, cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
|
|
21
|
+
}
|
|
22
|
+
export function openPush(key, sealed) {
|
|
23
|
+
if (sealed.length < NONCE + TAG)
|
|
24
|
+
throw new Error("The notification is too short.");
|
|
25
|
+
const decipher = createDecipheriv("chacha20-poly1305", key, sealed.subarray(0, NONCE), {
|
|
26
|
+
authTagLength: TAG,
|
|
27
|
+
});
|
|
28
|
+
decipher.setAuthTag(sealed.subarray(sealed.length - TAG));
|
|
29
|
+
return Buffer.concat([
|
|
30
|
+
decipher.update(sealed.subarray(NONCE, sealed.length - TAG)),
|
|
31
|
+
decipher.final(),
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
34
|
+
/** A phone's token as the Mac keeps it: Apple's environment, then the token in hex. */
|
|
35
|
+
export const PUSH_TOKEN = /^(sandbox|production):[0-9a-f]{64,200}$/;
|
|
36
|
+
/** The largest sealed notification the relay takes, in base64 characters (Apple's limit is 4 KB in all). */
|
|
37
|
+
export const MAX_PUSH_DATA = 3400;
|