Repository navigation
feat(net): origins prove a route key in lite-07 SETUP #7937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b5235d9
chore(quest): claim quest/m1/cluster-routing/routes/proof.md
kixelated 0007074
feat(net): origins prove a route key in lite-07 SETUP
kixelated 30ae988
test(net): l1's own broadcast fails over in the reference model
kixelated f8edf41
style: rustfmt
kixelated ca7c2b5
test(net): unwrap the fallible peer SETUP record in feature-gated tests
kixelated 1994aaf
fix(net): read an origin's config under one lock
kixelated 83af022
fix(net): close a refused accepted session with the refusal's code
kixelated 64a0517
fix(net): merge main and settle session route proof decisions
kixelated b6ac5aa
test(net): isolate unavailable browser key capability
kixelated 507d425
docs(net): attach setup test comments to their cases
kixelated 9795891
quest: date the SETUP route-key decision correctly in session.md
kixelated de4167f
Merge origin/main into route-key proof
kixelated 3523119
Merge base into pr-7937, preserving both migration notes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { proveRouteKey, sign } from "./route_key.ts"; | ||
|
|
||
| // PKCS#8 v1 (RFC 8410) prefix of an Ed25519 key, up to its 32-byte seed. | ||
| const PKCS8_SEED_PREFIX = [ | ||
| 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20, | ||
| ]; | ||
|
|
||
| // Shared with rs/moq-net/src/route/proof.rs, so both sides sign the same transcript. | ||
| const SEED = new Uint8Array(32).fill(1); | ||
| const SECRET = new Uint8Array(32).fill(2); | ||
| const CLIENT_PROOF = | ||
| "8a88e3dd7409f195fd52db2d3cba5d72ca6709bf1d94121bf3748801b40f6f5c" + | ||
| "e7ea4e34a65b8501185b805d09b58a61b5b8495654fcaac0949d4f98aa35002e" + | ||
| "6fca096329973e91e1105030dd88fa0eb543c68c00e18394f8300cb0574cf901"; | ||
|
|
||
| async function keyFromSeed(seed: Uint8Array): Promise<CryptoKeyPair> { | ||
| const der = new Uint8Array([...PKCS8_SEED_PREFIX, ...seed]); | ||
| const privateKey = await crypto.subtle.importKey("pkcs8", der, { name: "Ed25519" }, true, ["sign"]); | ||
| // WebCrypto derives the public key only through a JWK export. | ||
| const jwk = await crypto.subtle.exportKey("jwk", privateKey); | ||
| const publicKey = await crypto.subtle.importKey( | ||
| "jwk", | ||
| { kty: jwk.kty, crv: jwk.crv, x: jwk.x }, | ||
| { name: "Ed25519" }, | ||
| true, | ||
| ["verify"], | ||
| ); | ||
| return { privateKey, publicKey }; | ||
| } | ||
|
|
||
| function hex(bytes: Uint8Array): string { | ||
| return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join(""); | ||
| } | ||
|
|
||
| test("a proof matches the Rust transcript", async () => { | ||
| const proof = await sign(await keyFromSeed(SEED), SECRET, true); | ||
| expect(hex(proof)).toBe(CLIENT_PROOF); | ||
| }); | ||
|
|
||
| test("the prover's side changes the signature", async () => { | ||
| const key = await keyFromSeed(SEED); | ||
| const client = await sign(key, SECRET, true); | ||
| const server = await sign(key, SECRET, false); | ||
| expect(client.subarray(0, 32)).toEqual(server.subarray(0, 32)); | ||
| expect(client.subarray(32)).not.toEqual(server.subarray(32)); | ||
| }); | ||
|
|
||
| test("a transport without an exporter proves nothing", async () => { | ||
| expect(await proveRouteKey({} as WebTransport, true)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("a transport with an exporter proves one key per page", async () => { | ||
| const labels: string[] = []; | ||
| const transport = { | ||
| exportKeyingMaterial: async (label: BufferSource, context: BufferSource, length: number) => { | ||
| labels.push(new TextDecoder().decode(label)); | ||
| expect((context as Uint8Array).byteLength).toBe(0); | ||
| return new Uint8Array(length).fill(3); | ||
| }, | ||
| } as unknown as WebTransport; | ||
| const first = await proveRouteKey(transport, true); | ||
| const second = await proveRouteKey(transport, false); | ||
| expect(first?.byteLength).toBe(96); | ||
| expect(first?.subarray(0, 32)).toEqual(second?.subarray(0, 32)); | ||
| expect(labels).toEqual(["EXPORTER-moq-lite-route-key", "EXPORTER-moq-lite-route-key"]); | ||
| }); | ||
|
|
||
| test("unavailable Ed25519 leaves this page unproven across sessions", () => { | ||
| // A child has its own page capability cache, independent of the successful proof tests. | ||
| const moduleUrl = new URL("./route_key.ts", import.meta.url).href; | ||
| const child = Bun.spawnSync([ | ||
| process.execPath, | ||
| "-e", | ||
| ` | ||
| import { strict as assert } from "node:assert"; | ||
| import { proveRouteKey } from ${JSON.stringify(moduleUrl)}; | ||
| let calls = 0; | ||
| crypto.subtle.generateKey = async () => { calls++; throw new Error("unavailable"); }; | ||
| const transport = { exportKeyingMaterial: async () => new Uint8Array(32) }; | ||
| assert.equal(await proveRouteKey(transport, true), undefined); | ||
| assert.equal(await proveRouteKey(transport, true), undefined); | ||
| assert.equal(calls, 1); | ||
| `, | ||
| ]); | ||
| expect(child.stderr.toString()).toBe(""); | ||
| expect(child.exitCode).toBe(0); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Route key proofs: an endpoint shows its peer that it holds an Ed25519 key, bound to the | ||
| * session by keying material both ends export from its handshake. | ||
| * | ||
| * A tab proves one key per page load, so a peer recognizes it across its sessions. Only a | ||
| * transport that exposes `exportKeyingMaterial` can bind a proof; elsewhere the tab sends | ||
| * none and stays unproven. | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| const EXPORTER_LABEL = new TextEncoder().encode("EXPORTER-moq-lite-route-key"); | ||
| const DOMAIN = new TextEncoder().encode("moq-lite route key"); | ||
| const SECRET_SIZE = 32; | ||
|
|
||
| /** The keying material export a WebTransport may offer; only some browsers ship it. */ | ||
| type Exporter = (label: BufferSource, context: BufferSource, outputLength: number) => Promise<ArrayBuffer | Uint8Array>; | ||
|
|
||
| let pageKey: Promise<CryptoKeyPair | undefined> | undefined; | ||
|
|
||
| /** | ||
| * The proof to send in SETUP for `transport`, or `undefined` when it cannot export keying | ||
| * material. `client` is whether this endpoint dialed the session. | ||
| */ | ||
| export async function proveRouteKey(transport: WebTransport, client: boolean): Promise<Uint8Array | undefined> { | ||
| const exporter = (transport as { exportKeyingMaterial?: Exporter }).exportKeyingMaterial; | ||
| if (typeof exporter !== "function") return undefined; | ||
| const secret = new Uint8Array(await exporter.call(transport, EXPORTER_LABEL, new Uint8Array(), SECRET_SIZE)); | ||
| // Select page capability once: unavailable Ed25519 leaves sessions unproven. | ||
| pageKey ??= ( | ||
| crypto.subtle.generateKey({ name: "Ed25519" }, false, ["sign", "verify"]) as Promise<CryptoKeyPair> | ||
| ).catch(() => undefined); | ||
| const key = await pageKey; | ||
| return key ? sign(key, secret, client) : undefined; | ||
| } | ||
|
|
||
| /** Sign the proof for `secret`, the session's exported keying material. Exported for tests. */ | ||
| export async function sign(key: CryptoKeyPair, secret: Uint8Array, client: boolean): Promise<Uint8Array> { | ||
| if (secret.byteLength !== SECRET_SIZE) throw new Error("route key secret must be 32 bytes"); | ||
| // The domain, then the prover's side of the session (0 dialed, 1 accepted), then the secret. | ||
| const message = new Uint8Array(DOMAIN.byteLength + 1 + SECRET_SIZE); | ||
| message.set(DOMAIN); | ||
| message[DOMAIN.byteLength] = client ? 0 : 1; | ||
| message.set(secret, DOMAIN.byteLength + 1); | ||
|
|
||
| const publicKey = new Uint8Array(await crypto.subtle.exportKey("raw", key.publicKey)); | ||
| const signature = new Uint8Array(await crypto.subtle.sign({ name: "Ed25519" }, key.privateKey, message)); | ||
| const proof = new Uint8Array(publicKey.byteLength + signature.byteLength); | ||
| proof.set(publicKey); | ||
| proof.set(signature, publicKey.byteLength); | ||
| return proof; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: moq-dev/moq
Length of output: 2701
Script executed:
Repository: moq-dev/moq
Length of output: 7114
Script executed:
Repository: moq-dev/moq
Length of output: 8011
Script executed:
Repository: moq-dev/moq
Length of output: 4808
Catch exporter failures in
proveRouteKeyand send SETUP without a proof.proveRouteKeyawaitsexportKeyingMaterialwith no guard. A rejection there ends#sendSetup, which ends the whole session. Treat an exporter failure like a missing exporter: returnundefined. The peer then records the session as unproven and keeps it open.Suggested fix