diff --git a/internal/server/handlers.go b/internal/server/handlers.go index c7a8752..fc196e4 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -315,7 +315,7 @@ func (s *Server) analyzeUploadedFile(filename, mimeType string, payload []byte) } if looksLikeJSONSnapshot(filename, payload) { - if rawPkg, ok, err := parseRawExportPackage(payload); err != nil { + if rawPkg, ok, err := parseRawExportPackage(payload, nil); err != nil { return nil, "", nil, err } else if ok { result, vendor, err := s.reanalyzeRawExportPackage(rawPkg) @@ -1315,6 +1315,22 @@ func (s *Server) handleGetPrivacyScan(w http.ResponseWriter, r *http.Request) { }{result.PrivacyScan, s.canSanitizeCurrent()}) } +// handleGetSignatureStatus reports whether the currently loaded bundle was +// an AUDIT-SP "audit.raw-export.v1" export and, if so, whether its +// raw_export.sig verified against the embedded trusted key. The frontend +// uses this to show a signed/verified badge next to the file actions. +func (s *Server) handleGetSignatureStatus(w http.ResponseWriter, r *http.Request) { + pkg := s.GetRawExport() + if pkg == nil || !pkg.SignatureChecked { + jsonResponse(w, map[string]any{"checked": false}) + return + } + jsonResponse(w, map[string]any{ + "checked": true, + "verified": pkg.SignatureVerified, + }) +} + func (s *Server) canSanitizeCurrent() bool { pkg := s.GetRawExport() return pkg != nil && pkg.Source.Kind == "file_bytes" && sanitize.CanSanitize(pkg.Source.Filename) diff --git a/internal/server/raw_export.go b/internal/server/raw_export.go index 09a0ae3..6387d2c 100644 --- a/internal/server/raw_export.go +++ b/internal/server/raw_export.go @@ -3,26 +3,68 @@ package server import ( "archive/zip" "bytes" + "crypto/ed25519" + "crypto/x509" "encoding/base64" "encoding/json" + "encoding/pem" "fmt" "io" "strings" "time" "git.mchus.pro/mchus/logpile/internal/models" + "git.mchus.pro/mchus/logpile/keys" ) const rawExportFormatV1 = "logpile.raw-export.v1" +// rawExportFormatAuditV1 is emitted by the AUDIT-SP collection tool +// (a rebrand of the same collector LOGPile used to call "logpile"). Unlike +// rawExportFormatV1, bundles in this format carry a raw_export.sig +// signature over the exact bytes of raw_export.json that LOGPile verifies +// against the embedded trusted key (see keys.AuditSigningPublicKeyPEM). +const rawExportFormatAuditV1 = "audit.raw-export.v1" + const ( - rawExportBundlePackageFile = "raw_export.json" - rawExportBundleLogFile = "collect.log" - rawExportBundleFieldsFile = "parser_fields.json" - rawExportBundlePrivacyFile = "privacy_report.json" - rawExportBundleTopologyFile = "topology.json" + rawExportBundlePackageFile = "raw_export.json" + rawExportBundleSignatureFile = "raw_export.sig" + rawExportBundleLogFile = "collect.log" + rawExportBundleFieldsFile = "parser_fields.json" + rawExportBundlePrivacyFile = "privacy_report.json" + rawExportBundleTopologyFile = "topology.json" ) +const ed25519SignatureSize = ed25519.SignatureSize // 64 bytes + +// auditSigningPublicKey lazily parses the embedded trusted public key. +var auditSigningPublicKey = func() ed25519.PublicKey { + block, _ := pem.Decode(keys.AuditSigningPublicKeyPEM) + if block == nil { + panic("raw_export: embedded audit-signing-public.pem has no PEM block") + } + pub, err := x509.ParsePKIXPublicKey(block.Bytes) + if err != nil { + panic("raw_export: embedded audit-signing-public.pem is not a valid public key: " + err.Error()) + } + edPub, ok := pub.(ed25519.PublicKey) + if !ok { + panic("raw_export: embedded audit-signing-public.pem is not an Ed25519 key") + } + return edPub +}() + +// verifyAuditSignature checks sig (raw_export.sig) against jsonBody (the +// exact, unmodified bytes of raw_export.json) using the embedded AUDIT-SP +// trusted public key. It never consults raw_export.sha256 — only the +// signature is treated as proof of authenticity. +func verifyAuditSignature(jsonBody, sig []byte) bool { + if len(sig) != ed25519SignatureSize { + return false + } + return ed25519.Verify(auditSigningPublicKey, jsonBody, sig) +} + type RawExportPackage struct { Format string `json:"format"` ExportedAt time.Time `json:"exported_at"` @@ -31,6 +73,15 @@ type RawExportPackage struct { // CollectedAtHint is extracted from parser_fields.json when importing // a raw-export bundle and represents original collection time. CollectedAtHint time.Time `json:"-"` + // SignatureChecked and SignatureVerified describe the outcome of + // verifying an AUDIT-SP raw_export.sig against the embedded trusted + // key. SignatureChecked is false for bundles in a format that doesn't + // carry a signature (e.g. plain rawExportFormatV1); it is true for + // rawExportFormatAuditV1 bundles whether or not the signature + // actually verified. Both are recomputed on every import, never + // persisted across re-export. + SignatureChecked bool `json:"-"` + SignatureVerified bool `json:"-"` } type RawExportSource struct { @@ -94,12 +145,21 @@ func newRawExportFromLiveCollect(result *models.AnalysisResult, req CollectReque } } -func parseRawExportPackage(payload []byte) (*RawExportPackage, bool, error) { +// parseRawExportPackage parses a bare raw_export.json payload. sig is the +// accompanying raw_export.sig bytes when one was found alongside payload in +// the same bundle (nil if there wasn't one, e.g. a bare .json upload). +func parseRawExportPackage(payload []byte, sig []byte) (*RawExportPackage, bool, error) { var pkg RawExportPackage if err := json.Unmarshal(payload, &pkg); err != nil { return nil, false, err } - if pkg.Format != rawExportFormatV1 { + switch pkg.Format { + case rawExportFormatV1: + // No signature in this format; nothing to check. + case rawExportFormatAuditV1: + pkg.SignatureChecked = true + pkg.SignatureVerified = sig != nil && verifyAuditSignature(payload, sig) + default: return nil, false, nil } if pkg.ExportedAt.IsZero() { @@ -195,12 +255,20 @@ func parseRawExportBundle(payload []byte) (*RawExportPackage, bool, error) { return nil, false, nil } var pkgBody []byte + var sigBody []byte var parserFieldsBody []byte + var pkgSeen, sigSeen bool for _, f := range zr.File { - if f.Name != rawExportBundlePackageFile && f.Name != rawExportBundleFieldsFile { + if f.Name != rawExportBundlePackageFile && f.Name != rawExportBundleSignatureFile && f.Name != rawExportBundleFieldsFile { continue } + if f.Name == rawExportBundlePackageFile && pkgSeen { + return nil, true, fmt.Errorf("raw export bundle contains duplicate %s entries", rawExportBundlePackageFile) + } + if f.Name == rawExportBundleSignatureFile && sigSeen { + return nil, true, fmt.Errorf("raw export bundle contains duplicate %s entries", rawExportBundleSignatureFile) + } rc, err := f.Open() if err != nil { return nil, true, err @@ -213,6 +281,10 @@ func parseRawExportBundle(payload []byte) (*RawExportPackage, bool, error) { switch f.Name { case rawExportBundlePackageFile: pkgBody = body + pkgSeen = true + case rawExportBundleSignatureFile: + sigBody = body + sigSeen = true case rawExportBundleFieldsFile: parserFieldsBody = body } @@ -221,7 +293,7 @@ func parseRawExportBundle(payload []byte) (*RawExportPackage, bool, error) { if len(pkgBody) == 0 { return nil, false, nil } - pkg, ok, err := parseRawExportPackage(pkgBody) + pkg, ok, err := parseRawExportPackage(pkgBody, sigBody) if err != nil || !ok { return pkg, ok, err } diff --git a/internal/server/raw_export_audit_signature_test.go b/internal/server/raw_export_audit_signature_test.go new file mode 100644 index 0000000..ba4af2a --- /dev/null +++ b/internal/server/raw_export_audit_signature_test.go @@ -0,0 +1,200 @@ +package server + +import ( + "archive/zip" + "bytes" + "crypto/ed25519" + "crypto/rand" + "testing" +) + +// withTestAuditKey swaps the embedded trusted key for a freshly generated +// keypair for the duration of the test, so tests can sign payloads without +// needing the real AUDIT-SP private key. +func withTestAuditKey(t *testing.T) ed25519.PrivateKey { + t.Helper() + pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatalf("generate test key: %v", err) + } + orig := auditSigningPublicKey + auditSigningPublicKey = pub + t.Cleanup(func() { auditSigningPublicKey = orig }) + return priv +} + +func auditPackageJSON() []byte { + return []byte(`{"format":"audit.raw-export.v1","exported_at":"2026-10-08T08:11:40.529667Z","source":{"kind":"live_redfish","protocol":"redfish","target_host":"172.16.41.43"}}`) +} + +func TestParseRawExportPackage_AuditFormatValidSignature(t *testing.T) { + priv := withTestAuditKey(t) + body := auditPackageJSON() + sig := ed25519.Sign(priv, body) + + pkg, ok, err := parseRawExportPackage(body, sig) + if err != nil || !ok { + t.Fatalf("expected ok parse, got ok=%v err=%v", ok, err) + } + if !pkg.SignatureChecked { + t.Fatal("expected SignatureChecked=true for audit format") + } + if !pkg.SignatureVerified { + t.Fatal("expected SignatureVerified=true for a correctly signed payload") + } +} + +func TestParseRawExportPackage_AuditFormatTamperedPayload(t *testing.T) { + priv := withTestAuditKey(t) + body := auditPackageJSON() + sig := ed25519.Sign(priv, body) + + tampered := append([]byte(nil), body...) + tampered = bytes.Replace(tampered, []byte("172.16.41.43"), []byte("172.16.41.99"), 1) + + pkg, ok, err := parseRawExportPackage(tampered, sig) + if err != nil || !ok { + t.Fatalf("expected the bundle to still import, got ok=%v err=%v", ok, err) + } + if !pkg.SignatureChecked { + t.Fatal("expected SignatureChecked=true") + } + if pkg.SignatureVerified { + t.Fatal("expected SignatureVerified=false for a tampered payload") + } +} + +func TestParseRawExportPackage_AuditFormatMissingSignature(t *testing.T) { + withTestAuditKey(t) + body := auditPackageJSON() + + pkg, ok, err := parseRawExportPackage(body, nil) + if err != nil || !ok { + t.Fatalf("expected the bundle to still import without a signature, got ok=%v err=%v", ok, err) + } + if !pkg.SignatureChecked { + t.Fatal("expected SignatureChecked=true") + } + if pkg.SignatureVerified { + t.Fatal("expected SignatureVerified=false when no raw_export.sig is present") + } +} + +func TestParseRawExportPackage_LegacyFormatUnaffected(t *testing.T) { + body := []byte(`{"format":"logpile.raw-export.v1","exported_at":"2026-10-08T08:11:40.529667Z","source":{"kind":"live_redfish","protocol":"redfish","target_host":"172.16.41.43"}}`) + + pkg, ok, err := parseRawExportPackage(body, nil) + if err != nil || !ok { + t.Fatalf("expected ok parse, got ok=%v err=%v", ok, err) + } + if pkg.SignatureChecked { + t.Fatal("expected SignatureChecked=false for the unsigned legacy format") + } + if pkg.SignatureVerified { + t.Fatal("expected SignatureVerified=false for the unsigned legacy format") + } +} + +func TestVerifyAuditSignature_RejectsWrongSizeSignature(t *testing.T) { + withTestAuditKey(t) + if verifyAuditSignature([]byte("payload"), []byte("too-short")) { + t.Fatal("expected a non-64-byte signature to be rejected") + } +} + +func buildAuditBundleZip(t *testing.T, entries map[string][]byte) []byte { + t.Helper() + var buf bytes.Buffer + zw := zip.NewWriter(&buf) + for name, content := range entries { + w, err := zw.Create(name) + if err != nil { + t.Fatalf("create %s: %v", name, err) + } + if _, err := w.Write(content); err != nil { + t.Fatalf("write %s: %v", name, err) + } + } + if err := zw.Close(); err != nil { + t.Fatalf("close zip: %v", err) + } + return buf.Bytes() +} + +func TestParseRawExportBundle_AuditFormatEndToEnd(t *testing.T) { + priv := withTestAuditKey(t) + body := auditPackageJSON() + sig := ed25519.Sign(priv, body) + + payload := buildAuditBundleZip(t, map[string][]byte{ + rawExportBundlePackageFile: body, + rawExportBundleSignatureFile: sig, + rawExportBundleLogFile: []byte("Audit 1.3.0\n"), + }) + + pkg, ok, err := parseRawExportBundle(payload) + if err != nil || !ok { + t.Fatalf("expected ok parse, got ok=%v err=%v", ok, err) + } + if !pkg.SignatureChecked || !pkg.SignatureVerified { + t.Fatalf("expected signature checked+verified, got checked=%v verified=%v", pkg.SignatureChecked, pkg.SignatureVerified) + } +} + +func TestParseRawExportBundle_RejectsDuplicatePackageFile(t *testing.T) { + withTestAuditKey(t) + body := auditPackageJSON() + + var buf bytes.Buffer + zw := zip.NewWriter(&buf) + for i := 0; i < 2; i++ { + w, err := zw.Create(rawExportBundlePackageFile) + if err != nil { + t.Fatal(err) + } + if _, err := w.Write(body); err != nil { + t.Fatal(err) + } + } + if err := zw.Close(); err != nil { + t.Fatal(err) + } + + _, ok, err := parseRawExportBundle(buf.Bytes()) + if err == nil { + t.Fatalf("expected an error for duplicate %s entries, got ok=%v", rawExportBundlePackageFile, ok) + } +} + +func TestParseRawExportBundle_RejectsDuplicateSignatureFile(t *testing.T) { + priv := withTestAuditKey(t) + body := auditPackageJSON() + sig := ed25519.Sign(priv, body) + + var buf bytes.Buffer + zw := zip.NewWriter(&buf) + w, err := zw.Create(rawExportBundlePackageFile) + if err != nil { + t.Fatal(err) + } + if _, err := w.Write(body); err != nil { + t.Fatal(err) + } + for i := 0; i < 2; i++ { + sw, err := zw.Create(rawExportBundleSignatureFile) + if err != nil { + t.Fatal(err) + } + if _, err := sw.Write(sig); err != nil { + t.Fatal(err) + } + } + if err := zw.Close(); err != nil { + t.Fatal(err) + } + + _, ok, err := parseRawExportBundle(buf.Bytes()) + if err == nil { + t.Fatalf("expected an error for duplicate %s entries, got ok=%v", rawExportBundleSignatureFile, ok) + } +} diff --git a/internal/server/server.go b/internal/server/server.go index 87189e9..09b853b 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -92,6 +92,7 @@ func (s *Server) setupRoutes() { s.mux.HandleFunc("GET /api/firmware", s.handleGetFirmware) s.mux.HandleFunc("GET /api/parse-errors", s.handleGetParseErrors) s.mux.HandleFunc("GET /api/privacy-scan", s.handleGetPrivacyScan) + s.mux.HandleFunc("GET /api/signature-status", s.handleGetSignatureStatus) s.mux.HandleFunc("POST /api/sanitize", s.handleSanitize) s.mux.HandleFunc("GET /api/sanitize/download", s.handleSanitizeDownload) s.mux.HandleFunc("GET /api/export/csv", s.handleExportCSV) diff --git a/keys/audit-signing-public.pem b/keys/audit-signing-public.pem new file mode 100644 index 0000000..6bafa97 --- /dev/null +++ b/keys/audit-signing-public.pem @@ -0,0 +1,3 @@ +-----BEGIN PUBLIC KEY----- +MCowBQYDK2VwAyEA2If86N3AOVSQijsrmC5GjYNJ0TBkG4v2HTVzMfYjc84= +-----END PUBLIC KEY----- diff --git a/keys/keys.go b/keys/keys.go new file mode 100644 index 0000000..0fbb8ac --- /dev/null +++ b/keys/keys.go @@ -0,0 +1,13 @@ +// Package keys embeds the trusted public key material LOGPile ships with, +// so it is baked into the binary rather than read from the filesystem at +// runtime. +package keys + +import _ "embed" + +// AuditSigningPublicKeyPEM is the Ed25519 public key (SubjectPublicKeyInfo, +// PEM-encoded) used to verify the signature AUDIT-SP attaches to +// raw_export.json in its "audit.raw-export.v1" format (raw_export.sig). +// +//go:embed audit-signing-public.pem +var AuditSigningPublicKeyPEM []byte diff --git a/web/static/css/style.css b/web/static/css/style.css index b8fc852..0604515 100644 --- a/web/static/css/style.css +++ b/web/static/css/style.css @@ -141,6 +141,19 @@ a:hover { cursor: not-allowed; } +.signature-badge { + display: inline-flex; + align-items: center; + gap: 5px; + min-height: 34px; + border-radius: 4px; + padding: 7px 12px; + background: rgba(255, 255, 255, 0.12); + color: rgba(255, 255, 255, 0.86); + font-size: 13px; + font-weight: 700; +} + .page-main { width: min(1500px, calc(100vw - 48px)); margin: 28px auto 56px; diff --git a/web/static/js/app.js b/web/static/js/app.js index 341ed5e..cb34bd4 100644 --- a/web/static/js/app.js +++ b/web/static/js/app.js @@ -1416,6 +1416,33 @@ async function loadData(vendor, filename) { loadAuditViewer(); loadParseErrors(); loadPrivacyScan(); + loadSignatureStatus(); +} + +async function loadSignatureStatus() { + const badge = document.getElementById('signature-badge'); + if (!badge) return; + + let data; + try { + const resp = await fetch('/api/signature-status'); + if (!resp.ok) { + badge.classList.add('hidden'); + return; + } + data = await resp.json(); + } catch (e) { + badge.classList.add('hidden'); + return; + } + + if (!data || !data.checked || !data.verified) { + badge.classList.add('hidden'); + return; + } + + badge.title = 'This export carries a valid AUDIT-SP signature over raw_export.json, verified against the trusted key built into LOGPile.'; + badge.classList.remove('hidden'); } async function loadPrivacyScan() { diff --git a/web/templates/index.html b/web/templates/index.html index 01e73c3..3b47bb2 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -15,6 +15,7 @@