| 1 | package hostevidence |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "crypto/rand" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestReceiptSignatureBindsProducerKeyAndExactBytes(t *testing.T) { |
| 10 | pub, private, err := ed25519.GenerateKey(rand.Reader) |
| 11 | if err != nil { |
| 12 | t.Fatal(err) |
| 13 | } |
| 14 | key := "control/hosts/x.example/evidence/op/box/receipt.json" |
| 15 | body := []byte(`{"evidence_at":"2026-10-07T00:00:00Z"}`) |
| 16 | wire, err := message(key, body) |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | signature := ed25519.Sign(private, wire) |
| 21 | if err = Verify(pub, key, body, signature); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | other, _, err := ed25519.GenerateKey(rand.Reader) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | for _, test := range []struct { |
| 29 | pub ed25519.PublicKey |
| 30 | key string |
| 31 | body []byte |
| 32 | }{ |
| 33 | {other, key, body}, {pub, key + "changed", body}, {pub, key, []byte(`{"evidence_at":"2026-10-08T00:00:00Z"}`)}, |
| 34 | } { |
| 35 | if Verify(test.pub, test.key, test.body, signature) == nil { |
| 36 | t.Fatal("accepted forged receipt") |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestReceiptSignatureSeparatesServingAndOperationEvidence(t *testing.T) { |
| 42 | pub, private, err := ed25519.GenerateKey(rand.Reader) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | body := []byte(`{"region":"ie","claim_etag":"etag","observed_boot_id":"boot","request_limit_ns":120000000000}`) |
| 47 | keys := []string{ |
| 48 | "control/hosts/x.example/serving-evidence/box/receipt.json", |
| 49 | "control/hosts/x.example/evidence/op/box/receipt.json", |
| 50 | } |
| 51 | for _, key := range keys { |
| 52 | wire, err := message(key, body) |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | sig := ed25519.Sign(private, wire) |
| 57 | if err := Verify(pub, key, body, sig); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | for _, other := range keys { |
| 61 | if other != key && Verify(pub, other, body, sig) == nil { |
| 62 | t.Fatal("receipt signature crossed evidence namespaces") |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | for _, key := range []string{ |
| 67 | "control/hosts/x.example/serving-evidence/op/box/receipt.json", |
| 68 | "control/hosts/x.example/evidence/box/receipt.json", |
| 69 | "control/hosts/x.example/serving-evidence/../receipt.json", |
| 70 | "control/hosts/x.example/serving-evidence//receipt.json", |
| 71 | "control/hosts/x.example/serving-evidence/box/receipt.json.sig", |
| 72 | } { |
| 73 | if _, err := message(key, body); err == nil { |
| 74 | t.Fatalf("accepted invalid evidence key %q", key) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |