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:
irrlicht
2026-10-12 14:04:40 +02:00
co-authored by Claude Opus 5.5
parent fa52811018
commit a58e34c2bb
2 changed files with 67 additions and 20 deletions
+62 -17
View File
@@ -1,20 +1,68 @@
// Schlüsselverwaltung im Browser (Plan: notes/schluessel.md). Bisher nur die // Schlüsselverwaltung im Browser (Plan: notes/schluessel.md). Bisher nur die
// Prüfung, ob der Browser alles kann, was die Schlüssel brauchen. // 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 // probeSupport testet die Fähigkeiten selbst, statt Browser-Versionen zu
// raten: Ed25519 (Signaturen), X25519 (Chats) und das Ablegen eines nicht // raten: einen OpenPGP-v6-Schlüssel erzeugen (Ed25519 + X25519, wie später
// exportierbaren Schlüssels in IndexedDB. Letzteres prüft auch, ob der // der echte, nur ohne Passphrase), damit signieren und verschlüsseln und ihn
// zurückgelesene Schlüssel noch signieren kann -- bei jungen Engines // mit einer Passphrase per Argon2 schützen (WebAssembly, braucht
// (Ladybird) der unsicherste Teil. // '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> { export async function probeSupport(): Promise<Support> {
const sign = await attempt(() => crypto.subtle.generateKey("Ed25519", false, ["sign", "verify"])); const none: Support = { generate: false, sign: false, encrypt: false, protect: false, store: false };
const chat = await attempt(() => crypto.subtle.generateKey("X25519", false, ["deriveBits"])); let openpgp: Awaited<ReturnType<typeof loadOpenPGP>>;
const store = sign ? await attempt(probeStore) : false; let key: import("openpgp").PrivateKey;
return { sign, chat, store }; 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> { async function attempt(fn: () => Promise<unknown>): Promise<boolean> {
@@ -28,16 +76,13 @@ async function attempt(fn: () => Promise<unknown>): Promise<boolean> {
const probeDB = "kver-probe"; const probeDB = "kver-probe";
async function probeStore() { // roundTrip legt value in einer Wegwerf-Datenbank ab und liest es zurück.
const pair = (await crypto.subtle.generateKey("Ed25519", false, ["sign", "verify"])) as CryptoKeyPair; async function roundTrip(value: string): Promise<string> {
try { try {
const db = await openDB(probeDB); const db = await openDB(probeDB);
try { try {
await request(db.transaction("k", "readwrite").objectStore("k").put(pair.privateKey, 1)); await request(db.transaction("k", "readwrite").objectStore("k").put(value, 1));
const key = await request<CryptoKey>(db.transaction("k").objectStore("k").get(1)); return await request<string>(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");
} finally { } finally {
db.close(); db.close();
} }
+5 -3
View File
@@ -34,9 +34,11 @@ export function KeysPage() {
} }
const checks: { key: keyof Support; label: string }[] = [ const checks: { key: keyof Support; label: string }[] = [
{ key: "sign", label: "Signieren (Ed25519)" }, { key: "generate", label: "Schlüssel erzeugen (OpenPGP)" },
{ key: "chat", label: "Verschlüsseln (X25519)" }, { key: "sign", label: "Signieren und prüfen" },
{ key: "store", label: "Schlüssel sicher im Browser ablegen" }, { key: "encrypt", label: "Ver- und entschlüsseln" },
{ key: "protect", label: "Mit Passphrase schützen (Argon2)" },
{ key: "store", label: "Schlüssel im Browser ablegen" },
]; ];
function BrowserCheck() { function BrowserCheck() {