Schlüssel: privater Schlüssel verschlüsselt auf dem Server

Bisher mussten Nutzende eine Schlüsseldatei selbst sichern und auf jedes
Gerät laden. Mit der erzwungenen Passphrase aus sechs Wörtern (77 Bit)
und Argon2 ist der verschlüsselte Schlüssel offline nicht zu knacken, er
darf also beim Server liegen. Das nimmt den Nutzenden die Verwaltung ab;
die Passphrase verlässt den Browser weiterhin nie.

- Server: account_key.secret_key; pgpkey.CheckSecret prüft Fingerprint
  und Argon2/AEAD-Schutz jedes geheimen Pakets. Neu /user/key/secret
  (nur mit Kontopasswort) und /user/key/passphrase.
- Seite: Einrichten ohne Pflicht-Download und Checkbox (das
  Widerrufszertifikat bleibt freiwillig), Schlüsseldatei herunterladen,
  Passphrase ändern, Widerruf auch per Passphrase.
- Zurückgebaut: Ablage in IndexedDB samt Browser-Prüfung dafür, Laden
  der Schlüsseldatei, Sonderfälle für Schlüssel ohne Schlüsselnamen
  (name ohne Default). DEV-Tabellen account_key und key_name neu aus
  schema.sql; die Fixtures sind neu erzeugt (testdata/gen.mjs).

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-13 11:48:46 +02:00
co-authored by Claude Opus 5.5
parent 18d196231c
commit 93159aa12b
24 changed files with 694 additions and 337 deletions
+104 -20
View File
@@ -21,8 +21,14 @@ func fixture(t *testing.T, name string) []byte {
return b
}
func keyForm(cert []byte, pass string) url.Values {
return url.Values{"key": {base64.StdEncoding.EncodeToString(cert)}, "pass": {pass}}
// keyForm baut das Formular für /user/key (mit secret) und /user/key/update
// (secret nil).
func keyForm(cert, secret []byte, pass string) url.Values {
f := url.Values{"key": {base64.StdEncoding.EncodeToString(cert)}, "pass": {pass}}
if secret != nil {
f.Set("secret", base64.StdEncoding.EncodeToString(secret))
}
return f
}
// getAny holt urlStr und liefert Status und dekodierten Body, auch bei Fehlern.
@@ -56,6 +62,7 @@ func TestKeys(t *testing.T) {
bob := registerAndLogin(t, srv, "bob")
const pass = "supersecret1"
v6, v6b, revoked := fixture(t, "v6.pgp"), fixture(t, "v6-2.pgp"), fixture(t, "v6-revoked.pgp")
locked, lockedB := fixture(t, "v6-locked.pgp"), fixture(t, "v6-2-locked.pgp")
fpr := strings.TrimSpace(string(fixture(t, "v6.fpr")))
fpr2 := strings.TrimSpace(string(fixture(t, "v6-2.fpr")))
keyURL := srv.URL + "/api/u/alice/key"
@@ -76,10 +83,10 @@ func TestKeys(t *testing.T) {
status int
code string
}{
{"ohne login", newClient(t), keyForm(v6, pass), 401, "auth.required"},
{"privater schlüssel", alice, keyForm(fixture(t, "v6-secret.pgp"), pass), 400, "key.secret"},
{"v4", alice, keyForm(fixture(t, "v4.pgp"), pass), 400, "key.unsupported"},
{"fremde user-id", alice, keyForm(fixture(t, "v6-foreign.pgp"), pass), 400, "key.user_id"},
{"ohne login", newClient(t), keyForm(v6, locked, pass), 401, "auth.required"},
{"privater schlüssel", alice, keyForm(fixture(t, "v6-secret.pgp"), locked, pass), 400, "key.secret"},
{"v4", alice, keyForm(fixture(t, "v4.pgp"), locked, pass), 400, "key.unsupported"},
{"fremde user-id", alice, keyForm(fixture(t, "v6-foreign.pgp"), locked, pass), 400, "key.user_id"},
{"kein base64", alice, url.Values{"key": {"%%%"}, "pass": {pass}}, 400, "key.invalid"},
{"leer", alice, url.Values{"pass": {pass}}, 400, "key.invalid"},
} {
@@ -97,30 +104,30 @@ func TestKeys(t *testing.T) {
}
// Auch der erste Schlüssel nur mit Passwort.
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, "")); st != http.StatusForbidden || code != "auth.bad_password" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, locked, "")); st != http.StatusForbidden || code != "auth.bad_password" {
t.Fatalf("erster ohne passwort: %d %s", st, code)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, pass)); st != http.StatusCreated {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, locked, pass)); st != http.StatusCreated {
t.Fatalf("schlüssel setzen: %d %s", st, code)
}
var k keyJSON
if got := getAny(t, keyURL, &k); got != http.StatusOK || k.Fingerprint != fpr || k.Address != "alice@kver.xyz" || !bytes.Equal(k.Key, v6) || k.Status != "current" {
t.Fatalf("aktueller schlüssel: %d %+v", got, k)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, pass)); st != http.StatusConflict || code != "key.exists" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, locked, pass)); st != http.StatusConflict || code != "key.exists" {
t.Fatalf("doppelt: %d %s", st, code)
}
// Derselbe Schlüssel und sein Name gehören nicht zusätzlich bob.
if st, code := postCode(t, bob, srv.URL+"/api/user/key", keyForm(v6, pass)); st != http.StatusConflict || code != "key.name_taken" {
if st, code := postCode(t, bob, srv.URL+"/api/user/key", keyForm(v6, locked, pass)); st != http.StatusConflict || code != "key.name_taken" {
t.Fatalf("fremder schlüssel: %d %s", st, code)
}
// Ersetzen nur mit Passwort.
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, "falsch")); st != http.StatusForbidden || code != "auth.bad_password" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, lockedB, "falsch")); st != http.StatusForbidden || code != "auth.bad_password" {
t.Fatalf("ersetzen mit falschem passwort: %d %s", st, code)
}
// Ersetzen: der alte bleibt in der Historie.
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, pass)); st != http.StatusCreated {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, lockedB, pass)); st != http.StatusCreated {
t.Fatalf("ersetzen: %d %s", st, code)
}
if getAny(t, keyURL, &k); k.Fingerprint != fpr2 {
@@ -128,10 +135,10 @@ func TestKeys(t *testing.T) {
}
// Widerruf des alten Schlüssels nachreichen; bob darf das nicht.
if st, _ := postCode(t, bob, srv.URL+"/api/user/key/update", keyForm(revoked, pass)); st != http.StatusNotFound {
if st, _ := postCode(t, bob, srv.URL+"/api/user/key/update", keyForm(revoked, nil, pass)); st != http.StatusNotFound {
t.Fatalf("update fremder schlüssel: %d", st)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key/update", keyForm(revoked, pass)); st != http.StatusOK {
if st, code := postCode(t, alice, srv.URL+"/api/user/key/update", keyForm(revoked, nil, pass)); st != http.StatusOK {
t.Fatalf("update: %d %s", st, code)
}
var hist struct{ Keys []keyJSON }
@@ -142,7 +149,7 @@ func TestKeys(t *testing.T) {
t.Fatalf("historie: %+v", hist.Keys)
}
// Zurücknehmen mit der alten Fassung geht nicht.
if st, code := postCode(t, alice, srv.URL+"/api/user/key/update", keyForm(v6, pass)); st != http.StatusConflict || code != "key.revoked" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key/update", keyForm(v6, nil, pass)); st != http.StatusConflict || code != "key.revoked" {
t.Fatalf("widerruf zurücknehmen: %d %s", st, code)
}
@@ -160,7 +167,7 @@ func TestKeys(t *testing.T) {
t.Fatalf("doppelt verloren: %d %s", st, code)
}
// Nach dem Verlust geht ein neuer Schlüssel, nur kein schon bekannter.
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, pass)); st != http.StatusConflict || code != "key.exists" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, lockedB, pass)); st != http.StatusConflict || code != "key.exists" {
t.Fatalf("nach verlust bekannter schlüssel: %d %s", st, code)
}
@@ -182,21 +189,22 @@ func TestKeyRevokeCurrent(t *testing.T) {
srv := newTestServer(t)
const pass = "supersecret1"
v6, v6b, revoked := fixture(t, "v6.pgp"), fixture(t, "v6-2.pgp"), fixture(t, "v6-revoked.pgp")
locked, lockedB := fixture(t, "v6-locked.pgp"), fixture(t, "v6-2-locked.pgp")
fpr2 := strings.TrimSpace(string(fixture(t, "v6-2.fpr")))
var e errorResponse
var k keyJSON
carol := registerAndLogin(t, srv, "carol")
if st, code := postCode(t, carol, srv.URL+"/api/user/key", keyForm(v6, pass)); st != http.StatusCreated {
if st, code := postCode(t, carol, srv.URL+"/api/user/key", keyForm(v6, locked, pass)); st != http.StatusCreated {
t.Fatalf("carol schlüssel: %d %s", st, code)
}
if st, code := postCode(t, carol, srv.URL+"/api/user/key/update", keyForm(revoked, pass)); st != http.StatusOK {
if st, code := postCode(t, carol, srv.URL+"/api/user/key/update", keyForm(revoked, nil, pass)); st != http.StatusOK {
t.Fatalf("carol widerruf: %d %s", st, code)
}
if got := getAny(t, srv.URL+"/api/u/carol/key", &e); got != http.StatusNotFound || e.Error.Code != "key.revoked" {
t.Fatalf("widerrufen: %d %s", got, e.Error.Code)
}
if st, code := postCode(t, carol, srv.URL+"/api/user/key", keyForm(v6b, pass)); st != http.StatusCreated {
if st, code := postCode(t, carol, srv.URL+"/api/user/key", keyForm(v6b, lockedB, pass)); st != http.StatusCreated {
t.Fatalf("neuer nach widerruf: %d %s", st, code)
}
if getAny(t, srv.URL+"/api/u/carol/key", &k); k.Fingerprint != fpr2 || k.Status != "current" {
@@ -232,7 +240,7 @@ func TestKeyNames(t *testing.T) {
}
// Ein benutzter Name bleibt vergeben, auch wenn das Konto gelöscht ist.
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(fixture(t, "v6.pgp"), "supersecret1")); st != http.StatusCreated {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(fixture(t, "v6.pgp"), fixture(t, "v6-locked.pgp"), "supersecret1")); st != http.StatusCreated {
t.Fatalf("schlüssel: %d %s", st, code)
}
if st := postStatus(t, alice, srv.URL+"/api/user/delete", url.Values{"pass1": {"supersecret1"}}); st != http.StatusOK {
@@ -242,3 +250,79 @@ func TestKeyNames(t *testing.T) {
t.Fatalf("name nach konto-löschung: %d %s", st, code)
}
}
// TestKeySecret: Der private Schlüssel kommt nur verschlüsselt und passend
// zum Zertifikat an, geht nur mit Passwort an das eigene Konto zurück und
// lässt sich mit neuer Passphrase ersetzen.
func TestKeySecret(t *testing.T) {
t.Parallel()
srv := newTestServer(t)
alice := registerAndLogin(t, srv, "alice")
bob := registerAndLogin(t, srv, "bob")
const pass = "supersecret1"
v6, locked, lockedB := fixture(t, "v6.pgp"), fixture(t, "v6-locked.pgp"), fixture(t, "v6-2-locked.pgp")
fpr := strings.TrimSpace(string(fixture(t, "v6.fpr")))
secretForm := func(secret []byte, pass string) url.Values {
return url.Values{"secret": {base64.StdEncoding.EncodeToString(secret)}, "pass": {pass}}
}
for _, tc := range []struct {
name string
secret []byte
code string
}{
{"ohne", nil, "key.secret_invalid"},
{"ungeschützt", fixture(t, "v6-secret.pgp"), "key.secret_unprotected"},
{"ohne argon2", fixture(t, "v6-iter.pgp"), "key.secret_unprotected"},
{"anderer schlüssel", lockedB, "key.secret_mismatch"},
} {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, tc.secret, pass)); st != 400 || code != tc.code {
t.Errorf("%s: %d %s, erwartet 400 %s", tc.name, st, code, tc.code)
}
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key/secret", url.Values{"pass": {pass}}); st != http.StatusNotFound || code != "key.not_found" {
t.Fatalf("secret ohne schlüssel: %d %s", st, code)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, locked, pass)); st != http.StatusCreated {
t.Fatalf("schlüssel setzen: %d %s", st, code)
}
// Herausgabe nur mit Passwort und nur ans eigene Konto.
if st, code := postCode(t, alice, srv.URL+"/api/user/key/secret", url.Values{"pass": {"falsch"}}); st != http.StatusForbidden || code != "auth.bad_password" {
t.Fatalf("secret falsches passwort: %d %s", st, code)
}
if st, code := postCode(t, bob, srv.URL+"/api/user/key/secret", url.Values{"pass": {pass}}); st != http.StatusNotFound {
t.Fatalf("secret von bob: %d %s", st, code)
}
getSecret := func() []byte {
t.Helper()
resp := postForm(t, alice, srv.URL+"/api/user/key/secret", url.Values{"pass": {pass}})
defer resp.Body.Close()
var got struct {
Fingerprint string
Secret []byte
}
if err := json.NewDecoder(resp.Body).Decode(&got); err != nil || resp.StatusCode != http.StatusOK || got.Fingerprint != fpr {
t.Fatalf("secret: %d %+v %v", resp.StatusCode, got, err)
}
return got.Secret
}
if !bytes.Equal(getSecret(), locked) {
t.Fatal("secret: anderer inhalt als hochgeladen")
}
// Neue Passphrase: nur derselbe Schlüssel, nur geschützt. (Als Ersatz
// dient hier dieselbe Datei; der Server sieht den Unterschied nicht.)
if st, code := postCode(t, alice, srv.URL+"/api/user/key/passphrase", secretForm(lockedB, pass)); st != 400 || code != "key.secret_mismatch" {
t.Fatalf("passphrase fremder schlüssel: %d %s", st, code)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key/passphrase", secretForm(fixture(t, "v6-secret.pgp"), pass)); st != 400 || code != "key.secret_unprotected" {
t.Fatalf("passphrase ungeschützt: %d %s", st, code)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key/passphrase", secretForm(locked, "falsch")); st != http.StatusForbidden || code != "auth.bad_password" {
t.Fatalf("passphrase falsches passwort: %d %s", st, code)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key/passphrase", secretForm(locked, pass)); st != http.StatusOK {
t.Fatalf("passphrase: %d %s", st, code)
}
}