T5: KEX engine (curve25519/ecdh-p256, strict-kex, guess-skip), TOFU host key verifier, SHA256 fingerprint, live KEX diag

This commit is contained in:
STRIX
2026-09-08 12:38:31 +08:00
parent e7ef63e042
commit e6a842d61d
6 changed files with 657 additions and 7 deletions
+120
View File
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2026 Haven for HarmonyOS contributors.
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
*
* TOFU (trust-on-first-use) host key decision engine: pure logic, no UI and
* no database — persistence and user prompting are injected interfaces, so
* the diagnostics page and the future connection layer can share the policy.
*/
import { bytesEqual } from './SshWire';
import { sha256Fingerprint } from '../common/Fingerprint';
/** Outcome of comparing the presented key against stored records. */
export type HostKeyStatus = 'new' | 'changed' | 'trusted';
/** User/policy decision for a 'new' or 'changed' host key. */
export type HostKeyDecision = 'accept-save' | 'accept-once' | 'reject';
/** One known-hosts entry: host spec + algorithm + key blob. */
export interface KnownHostRecord {
hostPort: string;
keyAlgo: string;
keyBlob: Uint8Array;
}
/** Persistence surface injected into the TOFU engine. */
export interface KnownHostStore {
lookup(hostPort: string): Promise<KnownHostRecord[]>;
/** Insert or replace the entry of the same hostPort + keyAlgo. */
save(record: KnownHostRecord): Promise<void>;
}
/** What a HostKeyDecider sees when making its decision. */
export interface HostKeyPrompt {
/** 'new' or 'changed' ('trusted' never prompts). */
status: HostKeyStatus;
hostPort: string;
keyAlgo: string;
/** SHA256:... fingerprint of the presented key. */
fingerprint: string;
/** For 'changed': fingerprints of every stored key for this host. */
knownFingerprints: string[];
}
/** UI/policy callback that resolves a host key prompt. */
export interface HostKeyDecider {
decide(prompt: HostKeyPrompt): Promise<HostKeyDecision>;
}
/** known_hosts style host spec: "host" for port 22, "[host]:port" otherwise. */
export function hostPortString(host: string, port: number): string {
return port === 22 ? host : `[${host}]:${port}`;
}
/**
* TOFU host key verification:
* 1. the blob equals any stored record's blob -> trusted, return silently;
* 2. records exist but none matches -> status 'changed';
* 3. no records at all -> status 'new';
* then ask the decider and honor its decision. 'reject' throws, so callers
* see the failure reason; 'accept-save' persists the key; 'accept-once'
* continues without saving.
*/
export async function verifyHostKeyTofu(store: KnownHostStore, decider: HostKeyDecider,
host: string, port: number, keyAlgo: string, keyBlob: Uint8Array): Promise<void> {
const hostPort: string = hostPortString(host, port);
const records: KnownHostRecord[] = await store.lookup(hostPort);
for (let i = 0; i < records.length; i++) {
if (bytesEqual(records[i].keyBlob, keyBlob)) {
return; // trusted: exact key already on file
}
}
const knownFps: string[] = [];
for (let i = 0; i < records.length; i++) {
knownFps.push(sha256Fingerprint(records[i].keyBlob));
}
const prompt: HostKeyPrompt = {
status: records.length > 0 ? 'changed' : 'new',
hostPort: hostPort,
keyAlgo: keyAlgo,
fingerprint: sha256Fingerprint(keyBlob),
knownFingerprints: knownFps
};
const decision: HostKeyDecision = await decider.decide(prompt);
if (decision === 'reject') {
throw new Error(`host key rejected: ${hostPortString(host, port)}`);
}
if (decision === 'accept-save') {
const record: KnownHostRecord = {
hostPort: hostPort,
keyAlgo: keyAlgo,
keyBlob: keyBlob
};
await store.save(record);
}
// 'accept-once': continue without persisting.
}
/**
* Map-backed KnownHostStore for tests and diagnostics; save() replaces the
* entry with the same hostPort + keyAlgo. A fresh instance per run makes
* every live test see status 'new'.
*/
export class InMemoryKnownHostStore implements KnownHostStore {
private records: Map<string, KnownHostRecord> = new Map<string, KnownHostRecord>();
async lookup(hostPort: string): Promise<KnownHostRecord[]> {
const out: KnownHostRecord[] = [];
this.records.forEach((rec: KnownHostRecord): void => {
if (rec.hostPort === hostPort) {
out.push(rec);
}
});
return out;
}
async save(record: KnownHostRecord): Promise<void> {
this.records.set(`${record.hostPort}|${record.keyAlgo}`, record);
}
}