generated from ali/HarmonyTemplate
T5 review: HostKeyStatus/HostKeyDecision as string enums (ArkTS subset), Kex.parseExtInfo export + diag EXT_INFO reuse
This commit is contained in:
@@ -16,11 +16,10 @@ import { DerUtils, RsaPublicKeyParts, EcdsaParts } from '../ssh/crypto/DerUtils'
|
|||||||
import { buildPublicKeyBlob, verifyHostKeySignature, PublicKeyMaterial } from '../ssh/crypto/KeyImport';
|
import { buildPublicKeyBlob, verifyHostKeySignature, PublicKeyMaterial } from '../ssh/crypto/KeyImport';
|
||||||
import { SshVersion, SshMsg } from '../ssh/SshTypes';
|
import { SshVersion, SshMsg } from '../ssh/SshTypes';
|
||||||
import { ClientProposals } from '../ssh/Algorithms';
|
import { ClientProposals } from '../ssh/Algorithms';
|
||||||
import { KexEngine, KexResult } from '../ssh/Kex';
|
import { KexEngine, KexResult, parseExtInfo } from '../ssh/Kex';
|
||||||
import { verifyHostKeyTofu, InMemoryKnownHostStore, HostKeyDecider, HostKeyPrompt,
|
import { verifyHostKeyTofu, InMemoryKnownHostStore, HostKeyDecider, HostKeyPrompt,
|
||||||
HostKeyDecision } from '../ssh/HostKeyVerifier';
|
HostKeyDecision, HostKeyStatus } from '../ssh/HostKeyVerifier';
|
||||||
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter,
|
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter } from '../ssh/SshWire';
|
||||||
SshReader } from '../ssh/SshWire';
|
|
||||||
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
|
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
|
||||||
import { BusinessError } from '@kit.BasicServicesKit';
|
import { BusinessError } from '@kit.BasicServicesKit';
|
||||||
|
|
||||||
@@ -703,12 +702,12 @@ struct DiagPage {
|
|||||||
decide: async (prompt: HostKeyPrompt): Promise<HostKeyDecision> => {
|
decide: async (prompt: HostKeyPrompt): Promise<HostKeyDecision> => {
|
||||||
this.log('INFO', `主机密钥 [${prompt.status}] ${prompt.hostPort} (${prompt.keyAlgo})`);
|
this.log('INFO', `主机密钥 [${prompt.status}] ${prompt.hostPort} (${prompt.keyAlgo})`);
|
||||||
this.log('INFO', `本次指纹: ${prompt.fingerprint}`);
|
this.log('INFO', `本次指纹: ${prompt.fingerprint}`);
|
||||||
if (prompt.status === 'changed') {
|
if (prompt.status === HostKeyStatus.CHANGED) {
|
||||||
for (let i = 0; i < prompt.knownFingerprints.length; i++) {
|
for (let i = 0; i < prompt.knownFingerprints.length; i++) {
|
||||||
this.log('INFO', `已知指纹 ${i + 1}: ${prompt.knownFingerprints[i]}`);
|
this.log('INFO', `已知指纹 ${i + 1}: ${prompt.knownFingerprints[i]}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 'accept-once';
|
return HostKeyDecision.ACCEPT_ONCE;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const engine: KexEngine = new KexEngine(transport,
|
const engine: KexEngine = new KexEngine(transport,
|
||||||
@@ -735,15 +734,11 @@ struct DiagPage {
|
|||||||
try {
|
try {
|
||||||
const p: Uint8Array = await transport.waitPacket(2000);
|
const p: Uint8Array = await transport.waitPacket(2000);
|
||||||
if (p[0] === SshMsg.EXT_INFO) {
|
if (p[0] === SshMsg.EXT_INFO) {
|
||||||
const er: SshReader = new SshReader(p);
|
const ext: Map<string, string> = parseExtInfo(p);
|
||||||
er.readByte();
|
this.log('INFO', `EXT_INFO (${ext.size} 项扩展):`);
|
||||||
const count: number = er.readUint32();
|
ext.forEach((value: string, name: string): void => {
|
||||||
this.log('INFO', `EXT_INFO (${count} 项扩展):`);
|
|
||||||
for (let i = 0; i < count; i++) {
|
|
||||||
const name: string = er.readString();
|
|
||||||
const value: string = er.readString();
|
|
||||||
this.log('INFO', ` ${name} = ${this.trunc(value, 80)}`);
|
this.log('INFO', ` ${name} = ${this.trunc(value, 80)}`);
|
||||||
}
|
});
|
||||||
} else {
|
} else {
|
||||||
this.log('INFO', `KEX 后消息 ${p[0]} 忽略`);
|
this.log('INFO', `KEX 后消息 ${p[0]} 忽略`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,24 @@ import { bytesEqual } from './SshWire';
|
|||||||
import { sha256Fingerprint } from '../common/Fingerprint';
|
import { sha256Fingerprint } from '../common/Fingerprint';
|
||||||
|
|
||||||
/** Outcome of comparing the presented key against stored records. */
|
/** 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. */
|
/** 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. */
|
/** One known-hosts entry: host spec + algorithm + key blob. */
|
||||||
export interface KnownHostRecord {
|
export interface KnownHostRecord {
|
||||||
@@ -75,17 +89,17 @@ export async function verifyHostKeyTofu(store: KnownHostStore, decider: HostKeyD
|
|||||||
knownFps.push(sha256Fingerprint(records[i].keyBlob));
|
knownFps.push(sha256Fingerprint(records[i].keyBlob));
|
||||||
}
|
}
|
||||||
const prompt: HostKeyPrompt = {
|
const prompt: HostKeyPrompt = {
|
||||||
status: records.length > 0 ? 'changed' : 'new',
|
status: records.length > 0 ? HostKeyStatus.CHANGED : HostKeyStatus.NEW,
|
||||||
hostPort: hostPort,
|
hostPort: hostPort,
|
||||||
keyAlgo: keyAlgo,
|
keyAlgo: keyAlgo,
|
||||||
fingerprint: sha256Fingerprint(keyBlob),
|
fingerprint: sha256Fingerprint(keyBlob),
|
||||||
knownFingerprints: knownFps
|
knownFingerprints: knownFps
|
||||||
};
|
};
|
||||||
const decision: HostKeyDecision = await decider.decide(prompt);
|
const decision: HostKeyDecision = await decider.decide(prompt);
|
||||||
if (decision === 'reject') {
|
if (decision === HostKeyDecision.REJECT) {
|
||||||
throw new Error(`host key rejected: ${hostPortString(host, port)}`);
|
throw new Error(`host key for ${hostPort} rejected (${prompt.status})`);
|
||||||
}
|
}
|
||||||
if (decision === 'accept-save') {
|
if (decision === HostKeyDecision.ACCEPT_SAVE) {
|
||||||
const record: KnownHostRecord = {
|
const record: KnownHostRecord = {
|
||||||
hostPort: hostPort,
|
hostPort: hostPort,
|
||||||
keyAlgo: keyAlgo,
|
keyAlgo: keyAlgo,
|
||||||
|
|||||||
@@ -74,6 +74,28 @@ function stripKexMarkers(list: string[]): string[] {
|
|||||||
return out;
|
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<string, string> {
|
||||||
|
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<string, string> = new Map<string, string>();
|
||||||
|
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 {
|
export class KexEngine {
|
||||||
private static readonly TAG: string = 'KexEngine';
|
private static readonly TAG: string = 'KexEngine';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user