// 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 } // PrivateHalf is a copy of the private key, for the worker's secret scan // (it must never leave the runner in any form). func (k Keys) PrivateHalf() []byte { return append([]byte(nil), k.private[:]...) } // 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 }