sealbox: crypto_box_seal to the runner's key from its enrolment key
Seal and Open over x/crypto's anonymous box (libsodium's construction); the seal key is the X25519 form of the enrolment ed25519 key. Open checks the clear binding against the payload and the expiry.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+210
−15
go.mod+7 −5go.sum+12 −10internal/sealbox/sealbox.go+108 −0internal/sealbox/sealbox_test.go+83 −0
diff --git a/go.mod b/go.mod| index 11454c46..2ca476ba 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -7,7 +7,7 @@ require ( |
| github.com/chromedp/cdproto v0.158.6 |
| github.com/chromedp/chromedp v0.21.0 |
| github.com/go-chi/chi/v5 v5.3.2 |
| - golang.org/x/sys v0.46.0 |
| + golang.org/x/sys v0.49.0 |
| gorm.io/gorm v1.31.2 |
| gorm.io/plugin/dbresolver v1.6.2 |
| modernc.org/sqlite v1.55.0 |
| @@ -15,6 +15,8 @@ require ( |
| require github.com/coder/websocket v1.8.15 |
| +require golang.org/x/crypto v0.58.0 |
| + |
| require ( |
| amadan.net/rastrillo/rastrillo/money v0.1.0 // indirect |
| github.com/BurntSushi/toml v1.6.0 // indirect |
| @@ -28,10 +30,10 @@ require ( |
| github.com/ncruces/go-strftime v1.0.0 // indirect |
| github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect |
| github.com/yuin/goldmark v1.8.6 |
| - golang.org/x/mod v0.37.0 // indirect |
| - golang.org/x/sync v0.21.0 // indirect |
| - golang.org/x/text v0.39.0 // indirect |
| - golang.org/x/tools v0.47.0 // indirect |
| + golang.org/x/mod v0.41.0 // indirect |
| + golang.org/x/sync v0.24.0 // indirect |
| + golang.org/x/text v0.43.0 // indirect |
| + golang.org/x/tools v0.51.0 // indirect |
| modernc.org/libc v1.74.1 // indirect |
| modernc.org/mathutil v1.7.1 // indirect |
| modernc.org/memory v1.11.0 // indirect |
diff --git a/go.sum b/go.sum| index 9a0d47e3..d87a71b2 100644 |
| --- a/go.sum |
| +++ b/go.sum |
| @@ -44,17 +44,19 @@ github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1 |
| github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= |
| github.com/yuin/goldmark v1.8.6 h1:d0VcaP1sx9GkFVkoW+KtggpGi2KZ965i14b0+bDQST4= |
| github.com/yuin/goldmark v1.8.6/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg= |
| -golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ= |
| -golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0= |
| -golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM= |
| -golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= |
| +golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= |
| +golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= |
| +golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= |
| +golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= |
| +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= |
| +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= |
| golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
| -golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= |
| -golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= |
| -golang.org/x/text v0.39.0 h1:UbZz4pLOvn600D6Oh6GGEI6VAmndrEBLv8/6BEXzyus= |
| -golang.org/x/text v0.39.0/go.mod h1:3UwRclnC2g0TU9x8PZiyfOajCd1zaUNHF9cvqcQZ+ZM= |
| -golang.org/x/tools v0.47.0 h1:7Kn5x/d1svx/PzryTsqeoZN4TZwqeH5pGWjefhLi/1Q= |
| -golang.org/x/tools v0.47.0/go.mod h1:dFHnyTvFWY212G+h7ZY4Vsp/K3U4/7W9TyVaAul8uCA= |
| +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= |
| +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= |
| +golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= |
| +golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= |
| +golang.org/x/tools v0.51.0 h1:k4Xc/1Om9jwkBJBo4NVLMSARBoWtK10mx+W5BnXCeAI= |
| +golang.org/x/tools v0.51.0/go.mod h1:9eEncMayCV6zRMGhR5eZEC2iBx98qWcF1HZ9Z7wJOoA= |
| gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= |
| gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= |
| gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= |
diff --git a/internal/sealbox/sealbox.go b/internal/sealbox/sealbox.go| new file mode 100644 |
| index 00000000..81d93975 |
| --- /dev/null |
| +++ b/internal/sealbox/sealbox.go |
| @@ -0,0 +1,108 @@ |
| +// Package sealbox seals a secret to one runner and opens it there |
| +// (Connections spec §2.4). The box is libsodium's crypto_box_seal |
| +// (X25519, XSalsa20-Poly1305, the nonce from BLAKE2b of the two public |
| +// keys), so the web app, the native apps and the runner agree on it. The |
| +// runner's seal key is the X25519 form of its enrolment ed25519 key: the |
| +// private scalar is the clamped SHA-512 of the ed25519 seed, so the |
| +// public key equals the birational map of the ed25519 public key that |
| +// the owner approved, and an app can check one against the other. |
| +package sealbox |
| + |
| +import ( |
| + "crypto/ed25519" |
| + "crypto/rand" |
| + "crypto/sha512" |
| + "encoding/base64" |
| + "encoding/json" |
| + "errors" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| + "golang.org/x/crypto/curve25519" |
| + "golang.org/x/crypto/nacl/box" |
| +) |
| + |
| +// Errors a box is refused with. None carries the secret. |
| +var ( |
| + ErrMalformed = errors.New("sealed: malformed") |
| + ErrOpen = errors.New("sealed: does not open with this runner's key") |
| + ErrBinding = errors.New("sealed: the binding beside the box differs from the one inside") |
| + ErrExpired = errors.New("sealed: expired") |
| +) |
| + |
| +// Keys is a runner's seal key pair. |
| +type Keys struct { |
| + Public, private [32]byte |
| +} |
| + |
| +// FromEd25519 derives the seal keys from the runner's enrolment key. |
| +func FromEd25519(key ed25519.PrivateKey) (Keys, error) { |
| + if len(key) != ed25519.PrivateKeySize { |
| + return Keys{}, ErrMalformed |
| + } |
| + h := sha512.Sum512(key.Seed()) |
| + var k Keys |
| + copy(k.private[:], h[:32]) |
| + k.private[0] &= 248 |
| + k.private[31] &= 127 |
| + k.private[31] |= 64 |
| + pub, err := curve25519.X25519(k.private[:], curve25519.Basepoint) |
| + if err != nil { |
| + return Keys{}, err |
| + } |
| + copy(k.Public[:], pub) |
| + return k, nil |
| +} |
| + |
| +// PublicString is the public key as Runner.seal_key carries it. |
| +func (k Keys) PublicString() string { return base64.StdEncoding.EncodeToString(k.Public[:]) } |
| + |
| +// Seal seals p to the runner's public key (base64) and sets the clear |
| +// binding beside the box from p. The web app does the same in the |
| +// browser; this is for the native apps' fixtures and for tests. |
| +func Seal(public string, p wire.SealedPayload) (wire.Sealed, error) { |
| + raw, err := base64.StdEncoding.DecodeString(public) |
| + if err != nil || len(raw) != 32 { |
| + return wire.Sealed{}, ErrMalformed |
| + } |
| + var pk [32]byte |
| + copy(pk[:], raw) |
| + msg, err := json.Marshal(p) |
| + if err != nil { |
| + return wire.Sealed{}, err |
| + } |
| + out, err := box.SealAnonymous(nil, msg, &pk, rand.Reader) |
| + if err != nil { |
| + return wire.Sealed{}, err |
| + } |
| + return wire.Sealed{Box: base64.StdEncoding.EncodeToString(out), RunnerID: p.RunnerID, ConnectionID: p.ConnectionID, Provider: p.Provider, Endpoint: p.Endpoint, Attempt: p.Attempt, ExpiresAt: p.ExpiresAt}, nil |
| +} |
| + |
| +// Open opens s with the runner's keys and checks it: every clear field |
| +// equal to the one inside, not expired at now. The caller checks the |
| +// attempt is the one it runs and has not been seen. |
| +func (k Keys) Open(s wire.Sealed, now time.Time) (wire.SealedPayload, error) { |
| + var p wire.SealedPayload |
| + if len(s.Box) > wire.MaxSealedBytes { |
| + return p, ErrMalformed |
| + } |
| + raw, err := base64.StdEncoding.DecodeString(s.Box) |
| + if err != nil { |
| + return p, ErrMalformed |
| + } |
| + msg, ok := box.OpenAnonymous(nil, raw, &k.Public, &k.private) |
| + if !ok { |
| + return p, ErrOpen |
| + } |
| + defer clear(msg) |
| + if err := wire.Strict(msg, &p); err != nil { |
| + return wire.SealedPayload{}, ErrMalformed |
| + } |
| + if p.RunnerID != s.RunnerID || p.ConnectionID != s.ConnectionID || p.Provider != s.Provider || p.Endpoint != s.Endpoint || p.Attempt != s.Attempt || !p.ExpiresAt.Equal(s.ExpiresAt) { |
| + return wire.SealedPayload{}, ErrBinding |
| + } |
| + if !now.Before(p.ExpiresAt) || p.ExpiresAt.Sub(now) > wire.SealedTTL+time.Minute { |
| + return wire.SealedPayload{}, ErrExpired |
| + } |
| + return p, nil |
| +} |
diff --git a/internal/sealbox/sealbox_test.go b/internal/sealbox/sealbox_test.go| new file mode 100644 |
| index 00000000..3f8aff09 |
| --- /dev/null |
| +++ b/internal/sealbox/sealbox_test.go |
| @@ -0,0 +1,83 @@ |
| +package sealbox |
| + |
| +import ( |
| + "crypto/ed25519" |
| + "crypto/rand" |
| + "errors" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| + "golang.org/x/crypto/curve25519" |
| + "golang.org/x/crypto/nacl/box" |
| +) |
| + |
| +func keys(t *testing.T) (ed25519.PublicKey, Keys) { |
| + pub, priv, err := ed25519.GenerateKey(rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + k, err := FromEd25519(priv) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return pub, k |
| +} |
| + |
| +func payload(now time.Time) wire.SealedPayload { |
| + 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)} |
| +} |
| + |
| +func TestSealOpen(t *testing.T) { |
| + _, k := keys(t) |
| + now := time.Now() |
| + s, err := Seal(k.PublicString(), payload(now)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + p, err := k.Open(s, now) |
| + if err != nil || p.Secret != "sk-ant-oat01-canary" { |
| + t.Fatalf("open %+v %v", p, err) |
| + } |
| + // Any clear field changed beside the box is refused. |
| + for name, change := range map[string]func(*wire.Sealed){ |
| + "runner": func(s *wire.Sealed) { s.RunnerID = "rn_2" }, |
| + "connection": func(s *wire.Sealed) { s.ConnectionID = "cn_2" }, |
| + "provider": func(s *wire.Sealed) { s.Provider = wire.ProviderCodex }, |
| + "endpoint": func(s *wire.Sealed) { s.Endpoint = "https://api.anthropic.com:8443" }, |
| + "attempt": func(s *wire.Sealed) { s.Attempt = 2 }, |
| + "expiry": func(s *wire.Sealed) { s.ExpiresAt = s.ExpiresAt.Add(time.Minute) }, |
| + } { |
| + bad := s |
| + change(&bad) |
| + if _, err := k.Open(bad, now); !errors.Is(err, ErrBinding) { |
| + t.Errorf("%s changed: %v", name, err) |
| + } |
| + } |
| + if _, err := k.Open(s, now.Add(wire.SealedTTL+time.Second)); !errors.Is(err, ErrExpired) { |
| + t.Fatalf("expired box opened: %v", err) |
| + } |
| + // Another runner's key does not open it. |
| + _, other := keys(t) |
| + if _, err := other.Open(s, now); !errors.Is(err, ErrOpen) { |
| + t.Fatalf("another runner opened it: %v", err) |
| + } |
| +} |
| + |
| +// The seal key is the X25519 form of the approved ed25519 public key: the |
| +// private scalar's public point matches a box sealed by libsodium's |
| +// construction (here: x/crypto's, which follows it) to that key. |
| +func TestSealKeyFromEnrolmentKey(t *testing.T) { |
| + _, k := keys(t) |
| + pub, err := curve25519.X25519(k.private[:], curve25519.Basepoint) |
| + if err != nil || string(pub) != string(k.Public[:]) { |
| + t.Fatal("public key is not the private scalar's") |
| + } |
| + out, err := box.SealAnonymous(nil, []byte("x"), &k.Public, rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if msg, ok := box.OpenAnonymous(nil, out, &k.Public, &k.private); !ok || string(msg) != "x" { |
| + t.Fatal("does not open") |
| + } |
| +} |