generated from ali/HarmonyTemplate
T8: data layer (RDB schema, SecretStore, OpenSSH key containers, repositories)
- data/Models.ts: AuthMethod/SshKeyType enums, HostProfile/KeyRecord/KnownHostRow - data/Database.ts: haven.db (S3) with version-ladder migration, getInstance/openFresh - data/SecretStore.ts: Asset-backed master key (Preferences fallback), ENC: password envelopes, AES-256-GCM private key files under files/keys/ - data/OpenSshKey.ts: kit-free openssh-key-v1 serialize/parse (raw magic, mpint-style EC scalars, checkint/padding validation, cross-checked pub blobs) + PKCS#1/SEC1/ PKCS#8 PEM import, encrypted containers rejected - data/KeyGenerator.ts: ed25519/ecdsa-p256/rsa-4096 generation with fingerprints - HostRepository/KeyRepository/KnownHostRepository(DbKnownHostStore)/SettingsRepository - common/QuickConnect.ts: user@host[:port] parser incl. bracketed IPv6 - DiagPage button 12: generate Ed25519, persist, reopen fresh handles, read back - Node self-test 19/19 incl. real ssh-keygen interop both directions (-y roundtrip)
This commit is contained in:
@@ -21,10 +21,18 @@ import { verifyHostKeyTofu, InMemoryKnownHostStore, HostKeyDecider, HostKeyPromp
|
||||
HostKeyDecision, HostKeyStatus } from '../ssh/HostKeyVerifier';
|
||||
import { SshSession, SshSessionOptions, ExecResult } from '../ssh/SshSession';
|
||||
import { AuthResult } from '../ssh/Auth';
|
||||
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter } from '../ssh/SshWire';
|
||||
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter, bytesToBase64 } from '../ssh/SshWire';
|
||||
import { runVtSelfTest, VtTestResult } from '../vterm/VtSelfTest';
|
||||
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
import { common } from '@kit.AbilityKit';
|
||||
import { Database } from '../data/Database';
|
||||
import { SecretStore } from '../data/SecretStore';
|
||||
import { KeyRepository } from '../data/KeyRepository';
|
||||
import { SshKeyType, KeyRecord } from '../data/Models';
|
||||
import { generateKey, GeneratedKey } from '../data/KeyGenerator';
|
||||
import { parsePrivateKeyPem, ParsedPrivateKey } from '../data/OpenSshKey';
|
||||
import { sha256Fingerprint } from '../common/Fingerprint';
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
@@ -143,6 +151,11 @@ struct DiagPage {
|
||||
.onClick(() => {
|
||||
this.runTest((): Promise<void> => this.testVtEmulator());
|
||||
})
|
||||
Button('12. 数据层: 密钥生成→落库→重启读回')
|
||||
.width('100%')
|
||||
.onClick(() => {
|
||||
this.runTest((): Promise<void> => this.testDbKeyStore());
|
||||
})
|
||||
}
|
||||
.width('100%')
|
||||
|
||||
@@ -849,4 +862,61 @@ struct DiagPage {
|
||||
this.log('FAIL', `VT 终端模拟器自测 ${pass}/${results.length},${fail} 项失败`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 12) T8 acceptance: generate an Ed25519 key, persist it (keys row +
|
||||
* encrypted file), reopen the database with fresh handles simulating a
|
||||
* restart, read everything back and verify it parses to the same key.
|
||||
*/
|
||||
private async testDbKeyStore(): Promise<void> {
|
||||
const context: common.Context = this.getUIContext().getHostContext() as common.Context;
|
||||
|
||||
this.log('INFO', '生成 Ed25519 密钥…');
|
||||
const gen: GeneratedKey = await generateKey(SshKeyType.ED25519, 'diag-key');
|
||||
this.log('INFO', `指纹: ${gen.fingerprint}`);
|
||||
|
||||
const db: Database = await Database.getInstance(context);
|
||||
const secrets: SecretStore = await SecretStore.getInstance(context);
|
||||
if (secrets.isUsingFallback()) {
|
||||
this.log('INFO', '注意: Asset Store 不可用,主密钥已降级到 Preferences 存储');
|
||||
}
|
||||
const keyRepo: KeyRepository = new KeyRepository(db, secrets, context);
|
||||
const record: KeyRecord = {
|
||||
id: 0,
|
||||
name: 'diag-key',
|
||||
algorithm: gen.keyType,
|
||||
publicBlob: bytesToBase64(gen.pubBlob),
|
||||
fingerprint: gen.fingerprint,
|
||||
createdAt: Date.now()
|
||||
};
|
||||
const id: number = await keyRepo.insert(record, gen.opensshPrivate);
|
||||
this.log('INFO', `已写入 keys 表 (id=${id}),私钥文件已加密落盘`);
|
||||
|
||||
// Simulated restart: brand-new Database + SecretStore handles.
|
||||
const db2: Database = await Database.openFresh(context);
|
||||
const secrets2: SecretStore = new SecretStore();
|
||||
await secrets2.ensureMasterKey(context);
|
||||
const keyRepo2: KeyRepository = new KeyRepository(db2, secrets2, context);
|
||||
|
||||
const loaded: KeyRecord | null = await keyRepo2.getById(id);
|
||||
if (loaded === null) {
|
||||
this.log('FAIL', `重启后读不到 keys 行 id=${id}`);
|
||||
return;
|
||||
}
|
||||
const pem: string = await keyRepo2.loadPrivateKeyText(id);
|
||||
const parsed: ParsedPrivateKey = parsePrivateKeyPem(pem);
|
||||
|
||||
const sameBlob: boolean = bytesEqual(parsed.pubBlob, gen.pubBlob);
|
||||
const sameFp: boolean = sha256Fingerprint(parsed.pubBlob) === loaded.fingerprint;
|
||||
if (parsed.keyType === SshKeyType.ED25519 && sameBlob && sameFp &&
|
||||
parsed.comment === 'diag-key') {
|
||||
this.log('PASS', `重启读回一致: ${parsed.keyType} ${loaded.fingerprint} ✓`);
|
||||
} else {
|
||||
this.log('FAIL', `读回不一致: type=${parsed.keyType} blob一致=${sameBlob} ` +
|
||||
`指纹一致=${sameFp} comment=${parsed.comment}`);
|
||||
}
|
||||
|
||||
await keyRepo2.remove(id);
|
||||
this.log('INFO', `已清理测试数据 (id=${id})`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user