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
+87 -2
View File
@@ -14,9 +14,13 @@ import { GcmPacketCipher, CtrMacPacketCipher } from '../ssh/Ciphers';
import { CryptoAdapter, RawKeyPair } from '../ssh/crypto/CryptoAdapter';
import { DerUtils, RsaPublicKeyParts, EcdsaParts } from '../ssh/crypto/DerUtils';
import { buildPublicKeyBlob, verifyHostKeySignature, PublicKeyMaterial } from '../ssh/crypto/KeyImport';
import { SshVersion } from '../ssh/SshTypes';
import { SshVersion, SshMsg } from '../ssh/SshTypes';
import { ClientProposals } from '../ssh/Algorithms';
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter } from '../ssh/SshWire';
import { KexEngine, KexResult } from '../ssh/Kex';
import { verifyHostKeyTofu, InMemoryKnownHostStore, HostKeyDecider, HostKeyPrompt,
HostKeyDecision } from '../ssh/HostKeyVerifier';
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter,
SshReader } from '../ssh/SshWire';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
@@ -103,6 +107,11 @@ struct DiagPage {
.onClick(() => {
this.runTest((): Promise<void> => this.testCryptoSuite());
})
Button('9. 完整 KEX 密钥交换 (真实服务器)')
.width('100%')
.onClick(() => {
this.runTest((): Promise<void> => this.testLiveKex());
})
}
.width('100%')
@@ -671,4 +680,80 @@ struct DiagPage {
this.log('FAIL', `加密套件自测 ${pass}/${total},${fail} 项失败`);
}
}
/** 9) Full KEX against a live server: negotiate → agree → verify → NEWKEYS. */
private async testLiveKex(): Promise<void> {
const host: string = this.host.trim();
if (host.length === 0) {
this.log('FAIL', '请先输入 SSH 服务器地址');
return;
}
const port: number = this.parsePort();
this.log('INFO', `连接 ${host}:${port} 进行完整 KEX 密钥交换 ...`);
const socket: TcpRawSocket = new TcpRawSocket();
const transport: SshTransport = new SshTransport(socket);
try {
await socket.connect(host, port, 10000);
const serverBanner: string = await transport.start(SshVersion.CLIENT_BANNER, 15000);
this.log('INFO', `服务器版本: ${serverBanner}`);
// Fresh in-memory store per run → every test sees status 'new'.
const store: InMemoryKnownHostStore = new InMemoryKnownHostStore();
const decider: HostKeyDecider = {
decide: async (prompt: HostKeyPrompt): Promise<HostKeyDecision> => {
this.log('INFO', `主机密钥 [${prompt.status}] ${prompt.hostPort} (${prompt.keyAlgo})`);
this.log('INFO', `本次指纹: ${prompt.fingerprint}`);
if (prompt.status === 'changed') {
for (let i = 0; i < prompt.knownFingerprints.length; i++) {
this.log('INFO', `已知指纹 ${i + 1}: ${prompt.knownFingerprints[i]}`);
}
}
return 'accept-once';
}
};
const engine: KexEngine = new KexEngine(transport,
(algo: string, blob: Uint8Array, fp: string): Promise<boolean> => {
this.log('INFO', `校验主机密钥: ${algo} / ${fp}`);
// Rejection rejects this promise → run() aborts with the reason.
return verifyHostKeyTofu(store, decider, host, port, algo, blob)
.then((): boolean => true);
});
const result: KexResult = await engine.run(SshVersion.CLIENT_BANNER, serverBanner, 20000);
this.log('INFO', `kex: ${result.negotiated.kex}`);
this.log('INFO', `hostKeyAlgo: ${result.negotiated.hostKeyAlgo}`);
this.log('INFO', `cipherC2s: ${result.negotiated.cipherC2s}`);
this.log('INFO', `cipherS2c: ${result.negotiated.cipherS2c}`);
this.log('INFO', `macC2s: ${result.negotiated.macC2s.length > 0 ? result.negotiated.macC2s : 'implicit (AEAD)'}`);
this.log('INFO', `macS2c: ${result.negotiated.macS2c.length > 0 ? result.negotiated.macS2c : 'implicit (AEAD)'}`);
this.log('INFO', `strictKex: ${result.strictKex}`);
this.log('INFO', `extInfoExpected: ${result.extInfoExpected}`);
this.log('INFO', `hostKeyFingerprint: ${result.hostKeyFingerprint}`);
this.log('INFO', `sessionId: ${bytesToHex(result.sessionId)}`);
if (result.extInfoExpected) {
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();
this.log('INFO', ` ${name} = ${this.trunc(value, 80)}`);
}
} else {
this.log('INFO', `KEX 后消息 ${p[0]} 忽略`);
}
} catch (e) {
this.log('INFO', '2 秒内未收到 EXT_INFO (ok)');
}
}
this.log('PASS', 'KEX 完成 ✓');
} finally {
transport.close();
}
}
}