Schlüssel: Browser-Prüfung mit OpenPGP.js
Statt einzelner WebCrypto-Verfahren prüft die Seite jetzt genau das, was die Schlüsselverwaltung braucht: einen OpenPGP-v6-Schlüssel erzeugen, signieren, ver- und entschlüsseln, mit Argon2 schützen und im Browser ablegen. openpgp wird erst auf der Schlüsselseite nachgeladen (eigener Chunk), das Haupt-Bundle bleibt gleich groß. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TiXsPUqw7oeomZ8wZrQW5q
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
fa52811018
commit
a58e34c2bb
+62
-17
@@ -1,20 +1,68 @@
|
||||
// Schlüsselverwaltung im Browser (Plan: notes/schluessel.md). Bisher nur die
|
||||
// Prüfung, ob der Browser alles kann, was die Schlüssel brauchen.
|
||||
|
||||
export type Support = { sign: boolean; chat: boolean; store: boolean };
|
||||
export type Support = { generate: boolean; sign: boolean; encrypt: boolean; protect: boolean; store: boolean };
|
||||
|
||||
export const supported = (s: Support) => s.sign && s.chat && s.store;
|
||||
export const supported = (s: Support) => s.generate && s.sign && s.encrypt && s.protect && s.store;
|
||||
|
||||
// openpgp ist groß und wird nur hier gebraucht -> eigener Chunk, nachgeladen
|
||||
// erst auf der Schlüsselseite.
|
||||
const loadOpenPGP = () => import("openpgp");
|
||||
|
||||
// probeSupport testet die Fähigkeiten selbst, statt Browser-Versionen zu
|
||||
// raten: Ed25519 (Signaturen), X25519 (Chats) und das Ablegen eines nicht
|
||||
// exportierbaren Schlüssels in IndexedDB. Letzteres prüft auch, ob der
|
||||
// zurückgelesene Schlüssel noch signieren kann -- bei jungen Engines
|
||||
// (Ladybird) der unsicherste Teil.
|
||||
// raten: einen OpenPGP-v6-Schlüssel erzeugen (Ed25519 + X25519, wie später
|
||||
// der echte, nur ohne Passphrase), damit signieren und verschlüsseln und ihn
|
||||
// mit einer Passphrase per Argon2 schützen (WebAssembly, braucht
|
||||
// 'wasm-unsafe-eval' in der CSP) und über IndexedDB ablegen und zurücklesen.
|
||||
// OpenPGP.js rechnet fehlende Verfahren notfalls selbst; die Ablage ist bei
|
||||
// jungen Engines (Ladybird) der unsicherste Teil.
|
||||
export async function probeSupport(): Promise<Support> {
|
||||
const sign = await attempt(() => crypto.subtle.generateKey("Ed25519", false, ["sign", "verify"]));
|
||||
const chat = await attempt(() => crypto.subtle.generateKey("X25519", false, ["deriveBits"]));
|
||||
const store = sign ? await attempt(probeStore) : false;
|
||||
return { sign, chat, store };
|
||||
const none: Support = { generate: false, sign: false, encrypt: false, protect: false, store: false };
|
||||
let openpgp: Awaited<ReturnType<typeof loadOpenPGP>>;
|
||||
let key: import("openpgp").PrivateKey;
|
||||
try {
|
||||
openpgp = await loadOpenPGP();
|
||||
({ privateKey: key } = await openpgp.generateKey({
|
||||
type: "curve25519",
|
||||
userIDs: [{ name: "probe" }],
|
||||
format: "object",
|
||||
config: { v6Keys: true },
|
||||
}));
|
||||
} catch {
|
||||
return none;
|
||||
}
|
||||
|
||||
const sign = await attempt(async () => {
|
||||
const message = await openpgp.createMessage({ text: "probe" });
|
||||
const armoredSignature = await openpgp.sign({ message, signingKeys: key, detached: true });
|
||||
const signature = await openpgp.readSignature({ armoredSignature });
|
||||
const { signatures } = await openpgp.verify({ message, signature, verificationKeys: key.toPublic() });
|
||||
await signatures[0].verified;
|
||||
});
|
||||
const encrypt = await attempt(async () => {
|
||||
const encrypted = await openpgp.encrypt({
|
||||
message: await openpgp.createMessage({ text: "probe" }),
|
||||
encryptionKeys: key.toPublic(),
|
||||
});
|
||||
const { data } = await openpgp.decrypt({
|
||||
message: await openpgp.readMessage({ armoredMessage: encrypted }),
|
||||
decryptionKeys: key,
|
||||
});
|
||||
if (data !== "probe") throw new Error("decrypt");
|
||||
});
|
||||
const protect = await attempt(async () => {
|
||||
const locked = await openpgp.encryptKey({
|
||||
privateKey: key,
|
||||
passphrase: "probe",
|
||||
config: { s2kType: openpgp.enums.s2k.argon2, aeadProtect: true },
|
||||
});
|
||||
await openpgp.decryptKey({ privateKey: locked, passphrase: "probe" });
|
||||
});
|
||||
const store = await attempt(async () => {
|
||||
const back = await openpgp.readPrivateKey({ armoredKey: await roundTrip(key.armor()) });
|
||||
if (back.getFingerprint() !== key.getFingerprint()) throw new Error("store");
|
||||
});
|
||||
return { generate: true, sign, encrypt, protect, store };
|
||||
}
|
||||
|
||||
async function attempt(fn: () => Promise<unknown>): Promise<boolean> {
|
||||
@@ -28,16 +76,13 @@ async function attempt(fn: () => Promise<unknown>): Promise<boolean> {
|
||||
|
||||
const probeDB = "kver-probe";
|
||||
|
||||
async function probeStore() {
|
||||
const pair = (await crypto.subtle.generateKey("Ed25519", false, ["sign", "verify"])) as CryptoKeyPair;
|
||||
// roundTrip legt value in einer Wegwerf-Datenbank ab und liest es zurück.
|
||||
async function roundTrip(value: string): Promise<string> {
|
||||
try {
|
||||
const db = await openDB(probeDB);
|
||||
try {
|
||||
await request(db.transaction("k", "readwrite").objectStore("k").put(pair.privateKey, 1));
|
||||
const key = await request<CryptoKey>(db.transaction("k").objectStore("k").get(1));
|
||||
const data = new TextEncoder().encode("probe");
|
||||
const sig = await crypto.subtle.sign("Ed25519", key, data);
|
||||
if (!(await crypto.subtle.verify("Ed25519", pair.publicKey, sig, data))) throw new Error("verify");
|
||||
await request(db.transaction("k", "readwrite").objectStore("k").put(value, 1));
|
||||
return await request<string>(db.transaction("k").objectStore("k").get(1));
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user