Schlüssel: ohne Kontopasswort, alle eigenen privaten Schlüssel abrufbar

Die Schlüssel schützen die Chats vor dem Server, nicht den Client vor
einer übernommenen Session; die zusätzliche Passwortabfrage brachte dafür
nur Reibung. Ohne Passphrase nützt der verschlüsselte private Schlüssel
ohnehin niemandem.

- Keine Passwortprüfung mehr an den Schlüssel-Endpunkten,
  /user/password/check und die Abfrage beim Öffnen der Seite entfallen.
- GET /user/keys/secret statt POST /user/key/secret: alle eigenen
  privaten Schlüssel, neueste zuerst, damit die Chats nach einem Wechsel
  auch ältere Nachrichten entschlüsseln können.

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 12:44:53 +02:00
co-authored by Claude Opus 5.5
parent e5ec60d72c
commit ed227f0d81
9 changed files with 152 additions and 219 deletions
+68 -77
View File
@@ -22,18 +22,25 @@ func fixture(t *testing.T, name string) []byte {
}
// keyForm baut das Formular für /user/key.
func keyForm(cert, secret []byte, pass string) url.Values {
f := url.Values{"key": {base64.StdEncoding.EncodeToString(cert)}, "pass": {pass}}
func keyForm(cert, secret []byte) url.Values {
f := url.Values{"key": {base64.StdEncoding.EncodeToString(cert)}}
if secret != nil {
f.Set("secret", base64.StdEncoding.EncodeToString(secret))
}
return f
}
// getAny holt urlStr und liefert Status und dekodierten Body, auch bei Fehlern.
// getAny holt urlStr ohne Login und liefert Status und dekodierten Body,
// auch bei Fehlern.
func getAny(t *testing.T, urlStr string, v any) int {
t.Helper()
resp, err := http.Get(urlStr)
return getAs(t, http.DefaultClient, urlStr, v)
}
// getAs holt urlStr mit dem Client c (und dessen Session).
func getAs(t *testing.T, c *http.Client, urlStr string, v any) int {
t.Helper()
resp, err := c.Get(urlStr)
if err != nil {
t.Fatal(err)
}
@@ -59,7 +66,6 @@ func TestKeys(t *testing.T) {
srv := newTestServer(t)
alice := registerAndLogin(t, srv, "alice")
bob := registerAndLogin(t, srv, "bob")
const pass = "supersecret1"
v6, v6b := fixture(t, "v6.pgp"), fixture(t, "v6-2.pgp")
locked, lockedB := fixture(t, "v6-locked.pgp"), fixture(t, "v6-2-locked.pgp")
fpr := strings.TrimSpace(string(fixture(t, "v6.fpr")))
@@ -82,49 +88,34 @@ func TestKeys(t *testing.T) {
status int
code string
}{
{"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"},
{"kein base64", alice, url.Values{"key": {"%%%"}, "pass": {pass}}, 400, "key.invalid"},
{"leer", alice, url.Values{"pass": {pass}}, 400, "key.invalid"},
{"ohne login", newClient(t), keyForm(v6, locked), 401, "auth.required"},
{"privater schlüssel", alice, keyForm(fixture(t, "v6-secret.pgp"), locked), 400, "key.secret"},
{"v4", alice, keyForm(fixture(t, "v4.pgp"), locked), 400, "key.unsupported"},
{"kein base64", alice, url.Values{"key": {"%%%"}}, 400, "key.invalid"},
{"leer", alice, url.Values{}, 400, "key.invalid"},
} {
if st, code := postCode(t, tc.c, srv.URL+"/api/user/key", tc.form); st != tc.status || code != tc.code {
t.Errorf("%s: %d %s, erwartet %d %s", tc.name, st, code, tc.status, tc.code)
}
}
// Passwortprüfung der Schlüsselseite.
if st, code := postCode(t, alice, srv.URL+"/api/user/password/check", url.Values{"pass": {"falsch"}}); st != http.StatusForbidden || code != "auth.bad_password" {
t.Fatalf("prüfung falsch: %d %s", st, code)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/password/check", url.Values{"pass": {pass}}); st != http.StatusOK {
t.Fatalf("prüfung: %d %s", st, code)
}
// Auch der erste Schlüssel nur mit Passwort.
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, locked, pass)); st != http.StatusCreated {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, locked)); 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 || !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, locked, pass)); st != http.StatusConflict || code != "key.exists" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, locked)); st != http.StatusConflict || code != "key.exists" {
t.Fatalf("doppelt: %d %s", st, code)
}
// Derselbe Schlüssel gehört nicht zusätzlich bob.
if st, code := postCode(t, bob, srv.URL+"/api/user/key", keyForm(v6, locked, pass)); st != http.StatusConflict || code != "key.exists" {
if st, code := postCode(t, bob, srv.URL+"/api/user/key", keyForm(v6, locked)); st != http.StatusConflict || code != "key.exists" {
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, lockedB, "falsch")); st != http.StatusForbidden || code != "auth.bad_password" {
t.Fatalf("ersetzen mit falschem passwort: %d %s", st, code)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, lockedB, pass)); st != http.StatusCreated {
// Ersetzen: der alte bleibt in der Historie.
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, lockedB)); st != http.StatusCreated {
t.Fatalf("ersetzen: %d %s", st, code)
}
if getAny(t, keyURL, &k); k.Fingerprint != fpr2 {
@@ -132,25 +123,22 @@ func TestKeys(t *testing.T) {
}
// Als verloren markieren.
if st, _ := postCode(t, alice, srv.URL+"/api/user/key/lost", url.Values{"pass": {"falsch"}}); st != http.StatusForbidden {
t.Fatalf("verloren mit falschem passwort: %d", st)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key/lost", url.Values{"pass": {pass}}); st != http.StatusOK {
if st, code := postCode(t, alice, srv.URL+"/api/user/key/lost", nil); st != http.StatusOK {
t.Fatalf("verloren: %d %s", st, code)
}
if got := getAny(t, keyURL, &e); got != http.StatusNotFound || e.Error.Code != "key.lost" {
t.Fatalf("verloren abrufen: %d %s", got, e.Error.Code)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key/lost", url.Values{"pass": {pass}}); st != http.StatusNotFound || code != "key.not_found" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key/lost", nil); st != http.StatusNotFound || code != "key.not_found" {
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, lockedB, pass)); st != http.StatusConflict || code != "key.exists" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, lockedB)); st != http.StatusConflict || code != "key.exists" {
t.Fatalf("nach verlust bekannter schlüssel: %d %s", st, code)
}
// Konto löschen nimmt die Schlüssel mit.
if st := postStatus(t, alice, srv.URL+"/api/user/delete", url.Values{"pass1": {pass}}); st != http.StatusOK {
if st := postStatus(t, alice, srv.URL+"/api/user/delete", url.Values{"pass1": {"supersecret1"}}); st != http.StatusOK {
t.Fatalf("konto löschen: %d", st)
}
var n int
@@ -159,20 +147,31 @@ func TestKeys(t *testing.T) {
}
}
// secretsJSON ist die Antwort von /user/keys/secret.
type secretsJSON struct {
Keys []struct {
Fingerprint string
Secret []byte
Current bool
}
}
// 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.
// zum Zertifikat an, geht nur 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")
v6, locked := fixture(t, "v6.pgp"), fixture(t, "v6-locked.pgp")
v6b, lockedB := fixture(t, "v6-2.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}}
fpr2 := strings.TrimSpace(string(fixture(t, "v6-2.fpr")))
secretForm := func(secret []byte) url.Values {
return url.Values{"secret": {base64.StdEncoding.EncodeToString(secret)}}
}
secretsURL := srv.URL + "/api/user/keys/secret"
for _, tc := range []struct {
name string
@@ -184,53 +183,45 @@ func TestKeySecret(t *testing.T) {
{"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 {
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, tc.secret)); 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)
var got secretsJSON
if st := getAs(t, alice, secretsURL, &got); st != http.StatusOK || len(got.Keys) != 0 {
t.Fatalf("secrets ohne schlüssel: %d %+v", st, got)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, locked, pass)); st != http.StatusCreated {
if st := getAs(t, newClient(t), secretsURL, &got); st != http.StatusUnauthorized {
t.Fatalf("secrets ohne login: %d", st)
}
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6, locked)); 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 getAs(t, alice, secretsURL, &got); len(got.Keys) != 1 || got.Keys[0].Fingerprint != fpr || !got.Keys[0].Current || !bytes.Equal(got.Keys[0].Secret, locked) {
t.Fatalf("secrets: %+v", got)
}
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")
// Nur ans eigene Konto.
if getAs(t, bob, secretsURL, &got); len(got.Keys) != 0 {
t.Fatalf("secrets von bob: %+v", got)
}
// 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" {
// Neue Passphrase: nur derselbe Schlüssel, nur geschützt.
if st, code := postCode(t, alice, srv.URL+"/api/user/key/passphrase", secretForm(lockedB)); 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" {
if st, code := postCode(t, alice, srv.URL+"/api/user/key/passphrase", secretForm(fixture(t, "v6-secret.pgp"))); 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 {
if st, code := postCode(t, alice, srv.URL+"/api/user/key/passphrase", secretForm(locked)); st != http.StatusOK {
t.Fatalf("passphrase: %d %s", st, code)
}
// Nach dem Ersetzen: beide, der neue zuerst.
if st, code := postCode(t, alice, srv.URL+"/api/user/key", keyForm(v6b, lockedB)); st != http.StatusCreated {
t.Fatalf("ersetzen: %d %s", st, code)
}
if getAs(t, alice, secretsURL, &got); len(got.Keys) != 2 ||
got.Keys[0].Fingerprint != fpr2 || !got.Keys[0].Current || got.Keys[1].Fingerprint != fpr || got.Keys[1].Current {
t.Fatalf("secrets nach ersetzen: %+v", got)
}
}