| 1 | package sealbox |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "crypto/rand" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | "amadan.net/rastrillo/rastrillo/budget" |
| 13 | "golang.org/x/crypto/curve25519" |
| 14 | "golang.org/x/crypto/nacl/box" |
| 15 | ) |
| 16 | |
| 17 | func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } |
| 18 | |
| 19 | func keys(t *testing.T) (ed25519.PublicKey, Keys) { |
| 20 | pub, priv, err := ed25519.GenerateKey(rand.Reader) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | k, err := FromEd25519(priv) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | return pub, k |
| 29 | } |
| 30 | |
| 31 | func payload(now time.Time) wire.SealedPayload { |
| 32 | return wire.SealedPayload{Secret: "sk-ant-oat01-canary", RunnerID: "rn_1", ConnectionID: "cn_1", Provider: wire.ProviderClaude, Endpoint: wire.EndpointAnthropic, Attempt: 1, ExpiresAt: now.Add(wire.SealedTTL).UTC().Truncate(time.Second)} |
| 33 | } |
| 34 | |
| 35 | func TestSealOpen(t *testing.T) { |
| 36 | _, k := keys(t) |
| 37 | now := time.Now() |
| 38 | s, err := Seal(k.PublicString(), payload(now)) |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | p, err := k.Open(s, now) |
| 43 | if err != nil || p.Secret != "sk-ant-oat01-canary" { |
| 44 | t.Fatalf("open %+v %v", p, err) |
| 45 | } |
| 46 | // Any clear field changed beside the box is refused. |
| 47 | for name, change := range map[string]func(*wire.Sealed){ |
| 48 | "runner": func(s *wire.Sealed) { s.RunnerID = "rn_2" }, |
| 49 | "connection": func(s *wire.Sealed) { s.ConnectionID = "cn_2" }, |
| 50 | "provider": func(s *wire.Sealed) { s.Provider = wire.ProviderCodex }, |
| 51 | "endpoint": func(s *wire.Sealed) { s.Endpoint = "https://api.anthropic.com:8443" }, |
| 52 | "attempt": func(s *wire.Sealed) { s.Attempt = 2 }, |
| 53 | "expiry": func(s *wire.Sealed) { s.ExpiresAt = s.ExpiresAt.Add(time.Minute) }, |
| 54 | } { |
| 55 | bad := s |
| 56 | change(&bad) |
| 57 | if _, err := k.Open(bad, now); !errors.Is(err, ErrBinding) { |
| 58 | t.Errorf("%s changed: %v", name, err) |
| 59 | } |
| 60 | } |
| 61 | if _, err := k.Open(s, now.Add(wire.SealedTTL+time.Second)); !errors.Is(err, ErrExpired) { |
| 62 | t.Fatalf("expired box opened: %v", err) |
| 63 | } |
| 64 | // Another runner's key does not open it. |
| 65 | _, other := keys(t) |
| 66 | if _, err := other.Open(s, now); !errors.Is(err, ErrOpen) { |
| 67 | t.Fatalf("another runner opened it: %v", err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // The seal key is the X25519 form of the approved ed25519 public key: the |
| 72 | // private scalar's public point matches a box sealed by libsodium's |
| 73 | // construction (here: x/crypto's, which follows it) to that key. |
| 74 | func TestSealKeyFromEnrolmentKey(t *testing.T) { |
| 75 | _, k := keys(t) |
| 76 | pub, err := curve25519.X25519(k.private[:], curve25519.Basepoint) |
| 77 | if err != nil || string(pub) != string(k.Public[:]) { |
| 78 | t.Fatal("public key is not the private scalar's") |
| 79 | } |
| 80 | out, err := box.SealAnonymous(nil, []byte("x"), &k.Public, rand.Reader) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if msg, ok := box.OpenAnonymous(nil, out, &k.Public, &k.private); !ok || string(msg) != "x" { |
| 85 | t.Fatal("does not open") |
| 86 | } |
| 87 | } |
| 88 | |