From 1b8f5caeff25ffd4bf7adcbca3d44d13cb57790b Mon Sep 17 00:00:00 2001 From: STRIX Date: Tue, 8 Sep 2026 12:56:39 +0800 Subject: [PATCH] T5 review: HostKeyStatus/HostKeyDecision as string enums (ArkTS subset), Kex.parseExtInfo export + diag EXT_INFO reuse --- entry/src/main/ets/pages/DiagPage.ets | 23 ++++++++------------ entry/src/main/ets/ssh/HostKeyVerifier.ts | 26 +++++++++++++++++------ entry/src/main/ets/ssh/Kex.ts | 22 +++++++++++++++++++ 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/entry/src/main/ets/pages/DiagPage.ets b/entry/src/main/ets/pages/DiagPage.ets index baa963d..3103b5c 100644 --- a/entry/src/main/ets/pages/DiagPage.ets +++ b/entry/src/main/ets/pages/DiagPage.ets @@ -16,11 +16,10 @@ import { DerUtils, RsaPublicKeyParts, EcdsaParts } from '../ssh/crypto/DerUtils' import { buildPublicKeyBlob, verifyHostKeySignature, PublicKeyMaterial } from '../ssh/crypto/KeyImport'; import { SshVersion, SshMsg } from '../ssh/SshTypes'; import { ClientProposals } from '../ssh/Algorithms'; -import { KexEngine, KexResult } from '../ssh/Kex'; +import { KexEngine, KexResult, parseExtInfo } from '../ssh/Kex'; import { verifyHostKeyTofu, InMemoryKnownHostStore, HostKeyDecider, HostKeyPrompt, - HostKeyDecision } from '../ssh/HostKeyVerifier'; -import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter, - SshReader } from '../ssh/SshWire'; + HostKeyDecision, HostKeyStatus } from '../ssh/HostKeyVerifier'; +import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter } from '../ssh/SshWire'; import { cryptoFramework } from '@kit.CryptoArchitectureKit'; import { BusinessError } from '@kit.BasicServicesKit'; @@ -703,12 +702,12 @@ struct DiagPage { decide: async (prompt: HostKeyPrompt): Promise => { this.log('INFO', `主机密钥 [${prompt.status}] ${prompt.hostPort} (${prompt.keyAlgo})`); this.log('INFO', `本次指纹: ${prompt.fingerprint}`); - if (prompt.status === 'changed') { + if (prompt.status === HostKeyStatus.CHANGED) { for (let i = 0; i < prompt.knownFingerprints.length; i++) { this.log('INFO', `已知指纹 ${i + 1}: ${prompt.knownFingerprints[i]}`); } } - return 'accept-once'; + return HostKeyDecision.ACCEPT_ONCE; } }; const engine: KexEngine = new KexEngine(transport, @@ -735,15 +734,11 @@ struct DiagPage { try { const p: Uint8Array = await transport.waitPacket(2000); if (p[0] === SshMsg.EXT_INFO) { - const er: SshReader = new SshReader(p); - er.readByte(); - const count: number = er.readUint32(); - this.log('INFO', `EXT_INFO (${count} 项扩展):`); - for (let i = 0; i < count; i++) { - const name: string = er.readString(); - const value: string = er.readString(); + const ext: Map = parseExtInfo(p); + this.log('INFO', `EXT_INFO (${ext.size} 项扩展):`); + ext.forEach((value: string, name: string): void => { this.log('INFO', ` ${name} = ${this.trunc(value, 80)}`); - } + }); } else { this.log('INFO', `KEX 后消息 ${p[0]} 忽略`); } diff --git a/entry/src/main/ets/ssh/HostKeyVerifier.ts b/entry/src/main/ets/ssh/HostKeyVerifier.ts index 1abaec1..e4248e3 100644 --- a/entry/src/main/ets/ssh/HostKeyVerifier.ts +++ b/entry/src/main/ets/ssh/HostKeyVerifier.ts @@ -11,10 +11,24 @@ import { bytesEqual } from './SshWire'; import { sha256Fingerprint } from '../common/Fingerprint'; /** Outcome of comparing the presented key against stored records. */ -export type HostKeyStatus = 'new' | 'changed' | 'trusted'; +export enum HostKeyStatus { + /** No stored key for this host — first contact (TOFU "new"). */ + NEW = 'new', + /** Keys stored but none matches — possible MITM, must warn. */ + CHANGED = 'changed', + /** Exact match with a stored key (internal short-circuit, never prompted). */ + TRUSTED = 'trusted' +} /** User/policy decision for a 'new' or 'changed' host key. */ -export type HostKeyDecision = 'accept-save' | 'accept-once' | 'reject'; +export enum HostKeyDecision { + /** Accept and persist to the known-hosts store. */ + ACCEPT_SAVE = 'accept-save', + /** Accept for this connection only. */ + ACCEPT_ONCE = 'accept-once', + /** Abort the connection. */ + REJECT = 'reject' +} /** One known-hosts entry: host spec + algorithm + key blob. */ export interface KnownHostRecord { @@ -75,17 +89,17 @@ export async function verifyHostKeyTofu(store: KnownHostStore, decider: HostKeyD knownFps.push(sha256Fingerprint(records[i].keyBlob)); } const prompt: HostKeyPrompt = { - status: records.length > 0 ? 'changed' : 'new', + status: records.length > 0 ? HostKeyStatus.CHANGED : HostKeyStatus.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 === HostKeyDecision.REJECT) { + throw new Error(`host key for ${hostPort} rejected (${prompt.status})`); } - if (decision === 'accept-save') { + if (decision === HostKeyDecision.ACCEPT_SAVE) { const record: KnownHostRecord = { hostPort: hostPort, keyAlgo: keyAlgo, diff --git a/entry/src/main/ets/ssh/Kex.ts b/entry/src/main/ets/ssh/Kex.ts index 144aec8..508c535 100644 --- a/entry/src/main/ets/ssh/Kex.ts +++ b/entry/src/main/ets/ssh/Kex.ts @@ -74,6 +74,28 @@ function stripKexMarkers(list: string[]): string[] { return out; } +/** + * Parse SSH_MSG_EXT_INFO (RFC 8308 §2.2): + * byte(SSH_MSG_EXT_INFO) + uint32 nr-extensions + + * nr-extensions × (string extension-name, string extension-value). + * Later duplicate names overwrite earlier ones (last-wins). + */ +export function parseExtInfo(payload: Uint8Array): Map { + const r: SshReader = new SshReader(payload); + const msg: number = r.readByte(); + if (msg !== SshMsg.EXT_INFO) { + throw new Error(`parseExtInfo: expected msg ${SshMsg.EXT_INFO}, got ${msg}`); + } + const count: number = r.readUint32(); + const out: Map = new Map(); + for (let i = 0; i < count; i++) { + const name: string = r.readString(); + const value: string = r.readString(); + out.set(name, value); + } + return out; +} + export class KexEngine { private static readonly TAG: string = 'KexEngine';