- services/transparency-log/pkg/backend: Backend interface with in-memory append-log (RFC 6962 Merkle tree) + Trillian wrapper (prod stub). - Idempotenz-Pflicht: Wiederaufruf mit identischem content_hash liefert 200 + gleichen log_index + aktuellen inclusion_proof. - content_hash-Verifikation serverseitig; Mismatch → 409. - pkg/httpapi: ADR-0005-konformer Handler (201/200/400/404/409/503). - cmd/transparency-log: minimaler HTTP-Server, memory default, --backend trillian als Stub (503). - Interop-Test in services/admin-api/internal/tlog/interop_test.go fährt die echte transparency-log pkg gegen die admin-api HTTPSubmitter — Happy-Path, Idempotenz und content_hash-Mismatch abgedeckt. - go test: 10 Tests transparency-log + 2 Interop-Tests admin-api, go vet clean in beiden Modulen. Folge-Subtask: Trillian-Deployment + Keys (siehe ADR-0005 Open Points). Co-Authored-By: Paperclip <noreply@paperclip.ing>
96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
package backend
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
// EnvelopeContentHash returns the SHA-256 of the canonical JSON encoding
|
|
// of env, prefixed with "sha256:". The algorithm MUST match the one
|
|
// admin-api/internal/attest uses on the client side so that a client-sent
|
|
// content_hash is bit-identical to one we recompute here.
|
|
func EnvelopeContentHash(env Envelope) (string, error) {
|
|
b, err := canonicalJSON(env)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
sum := sha256.Sum256(b)
|
|
return "sha256:" + hex.EncodeToString(sum[:]), nil
|
|
}
|
|
|
|
// canonicalJSON is the stable JSON encoding we use for hashing. It round-trips
|
|
// through generic unmarshal and then re-emits with sorted object keys. This
|
|
// is NOT a full RFC 8785 implementation; it is the subset that is sufficient
|
|
// for the admin-api <-> transparency-log boundary (envelopes contain strings,
|
|
// nested string maps, and arrays of those).
|
|
func canonicalJSON(v any) ([]byte, error) {
|
|
first, err := json.Marshal(v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var any_ any
|
|
if err := json.Unmarshal(first, &any_); err != nil {
|
|
return nil, err
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := writeSorted(&buf, any_); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
func writeSorted(buf *bytes.Buffer, v any) error {
|
|
switch x := v.(type) {
|
|
case nil:
|
|
buf.WriteString("null")
|
|
case bool:
|
|
if x {
|
|
buf.WriteString("true")
|
|
} else {
|
|
buf.WriteString("false")
|
|
}
|
|
case float64:
|
|
b, _ := json.Marshal(x)
|
|
buf.Write(b)
|
|
case string:
|
|
b, _ := json.Marshal(x)
|
|
buf.Write(b)
|
|
case []any:
|
|
buf.WriteByte('[')
|
|
for i, el := range x {
|
|
if i > 0 {
|
|
buf.WriteByte(',')
|
|
}
|
|
if err := writeSorted(buf, el); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
buf.WriteByte(']')
|
|
case map[string]any:
|
|
keys := make([]string, 0, len(x))
|
|
for k := range x {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
buf.WriteByte('{')
|
|
for i, k := range keys {
|
|
if i > 0 {
|
|
buf.WriteByte(',')
|
|
}
|
|
kb, _ := json.Marshal(k)
|
|
buf.Write(kb)
|
|
buf.WriteByte(':')
|
|
if err := writeSorted(buf, x[k]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
buf.WriteByte('}')
|
|
default:
|
|
return fmt.Errorf("writeSorted: unsupported type %T", v)
|
|
}
|
|
return nil
|
|
}
|