oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package sealbox seals a secret to one runner and opens it there
2// (Connections spec §2.4). The box is libsodium's crypto_box_seal
3// (X25519, XSalsa20-Poly1305, the nonce from BLAKE2b of the two public
4// keys), so the web app, the native apps and the runner agree on it. The
5// runner's seal key is the X25519 form of its enrolment ed25519 key: the
6// private scalar is the clamped SHA-512 of the ed25519 seed, so the
7// public key equals the birational map of the ed25519 public key that
8// the owner approved, and an app can check one against the other.
9package sealbox
10
11import (
12 "crypto/ed25519"
13 "crypto/rand"
14 "crypto/sha512"
15 "encoding/base64"
16 "encoding/json"
17 "errors"
18 "time"
19
20 "amadan.net/oficina/equipo/internal/wire"
21 "golang.org/x/crypto/curve25519"
22 "golang.org/x/crypto/nacl/box"
23)
24
25// Errors a box is refused with. None carries the secret.
26var (
27 ErrMalformed = errors.New("sealed: malformed")
28 ErrOpen = errors.New("sealed: does not open with this runner's key")
29 ErrBinding = errors.New("sealed: the binding beside the box differs from the one inside")
30 ErrExpired = errors.New("sealed: expired")
31)
32
33// Keys is a runner's seal key pair.
34type Keys struct {
35 Public, private [32]byte
36}
37
38// FromEd25519 derives the seal keys from the runner's enrolment key.
39func FromEd25519(key ed25519.PrivateKey) (Keys, error) {
40 if len(key) != ed25519.PrivateKeySize {
41 return Keys{}, ErrMalformed
42 }
43 h := sha512.Sum512(key.Seed())
44 var k Keys
45 copy(k.private[:], h[:32])
46 k.private[0] &= 248
47 k.private[31] &= 127
48 k.private[31] |= 64
49 pub, err := curve25519.X25519(k.private[:], curve25519.Basepoint)
50 if err != nil {
51 return Keys{}, err
52 }
53 copy(k.Public[:], pub)
54 return k, nil
55}
56
57// PrivateHalf is a copy of the private key, for the worker's secret scan
58// (it must never leave the runner in any form).
59func (k Keys) PrivateHalf() []byte { return append([]byte(nil), k.private[:]...) }
60
61// PublicString is the public key as Runner.seal_key carries it.
62func (k Keys) PublicString() string { return base64.StdEncoding.EncodeToString(k.Public[:]) }
63
64// Seal seals p to the runner's public key (base64) and sets the clear
65// binding beside the box from p. The web app does the same in the
66// browser; this is for the native apps' fixtures and for tests.
67func Seal(public string, p wire.SealedPayload) (wire.Sealed, error) {
68 raw, err := base64.StdEncoding.DecodeString(public)
69 if err != nil || len(raw) != 32 {
70 return wire.Sealed{}, ErrMalformed
71 }
72 var pk [32]byte
73 copy(pk[:], raw)
74 msg, err := json.Marshal(p)
75 if err != nil {
76 return wire.Sealed{}, err
77 }
78 out, err := box.SealAnonymous(nil, msg, &pk, rand.Reader)
79 if err != nil {
80 return wire.Sealed{}, err
81 }
82 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
83}
84
85// Open opens s with the runner's keys and checks it: every clear field
86// equal to the one inside, not expired at now. The caller checks the
87// attempt is the one it runs and has not been seen.
88func (k Keys) Open(s wire.Sealed, now time.Time) (wire.SealedPayload, error) {
89 var p wire.SealedPayload
90 if len(s.Box) > wire.MaxSealedBytes {
91 return p, ErrMalformed
92 }
93 raw, err := base64.StdEncoding.DecodeString(s.Box)
94 if err != nil {
95 return p, ErrMalformed
96 }
97 msg, ok := box.OpenAnonymous(nil, raw, &k.Public, &k.private)
98 if !ok {
99 return p, ErrOpen
100 }
101 defer clear(msg)
102 if err := wire.Strict(msg, &p); err != nil {
103 return wire.SealedPayload{}, ErrMalformed
104 }
105 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) {
106 return wire.SealedPayload{}, ErrBinding
107 }
108 if !now.Before(p.ExpiresAt) || p.ExpiresAt.Sub(now) > wire.SealedTTL+time.Minute {
109 return wire.SealedPayload{}, ErrExpired
110 }
111 return p, nil
112}
113