oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package runnerproto
2
3import (
4 "bytes"
5 "crypto/ed25519"
6 "crypto/sha256"
7 "encoding/base64"
8 "encoding/hex"
9 "encoding/json"
10 "errors"
11 "io"
12 "strings"
13 "unicode"
14 "unicode/utf8"
15)
16
17var ErrInvalid = errors.New("runner.invalid")
18
19func Decode(r io.Reader, value any) error {
20 return DecodeLimit(r, value, 8192)
21}
22
23func DecodeLimit(r io.Reader, value any, limit int64) error {
24 data, err := io.ReadAll(io.LimitReader(r, limit+1))
25 if err != nil || int64(len(data)) > limit {
26 return ErrInvalid
27 }
28 dec := json.NewDecoder(bytes.NewReader(data))
29 if err = uniqueJSON(dec); err != nil {
30 return ErrInvalid
31 }
32 if _, err = dec.Token(); err != io.EOF {
33 return ErrInvalid
34 }
35 if decodeWire(data, value) != nil {
36 return ErrInvalid
37 }
38 return nil
39}
40
41func uniqueJSON(d *json.Decoder) error {
42 token, err := d.Token()
43 if err != nil {
44 return err
45 }
46 switch token {
47 case json.Delim('{'):
48 keys := map[string]bool{}
49 for d.More() {
50 key, err := d.Token()
51 if err != nil {
52 return err
53 }
54 s, ok := key.(string)
55 // Typed envelope names are checked by incoming. Opaque provider
56 // payloads retain their native field names, with no duplicate keys.
57 if !ok || s == "" || keys[s] {
58 return ErrInvalid
59 }
60 keys[s] = true
61 if err := uniqueJSON(d); err != nil {
62 return err
63 }
64 }
65 end, err := d.Token()
66 if err != nil || end != json.Delim('}') {
67 return ErrInvalid
68 }
69 case json.Delim('['):
70 for d.More() {
71 if err := uniqueJSON(d); err != nil {
72 return err
73 }
74 }
75 end, err := d.Token()
76 if err != nil || end != json.Delim(']') {
77 return ErrInvalid
78 }
79 }
80 return nil
81}
82
83func DecodeBytes(s string, size int) ([]byte, error) {
84 b, err := base64.RawURLEncoding.Strict().DecodeString(s)
85 if err != nil || len(b) != size || base64.RawURLEncoding.EncodeToString(b) != s {
86 return nil, ErrInvalid
87 }
88 return b, nil
89}
90
91func (r BeginEnrolmentRequest) Validate() error {
92 if r.Version != Version {
93 return ErrInvalid
94 }
95 if _, err := DecodeBytes(r.MachinePublicKey, ed25519.PublicKeySize); err != nil {
96 return err
97 }
98 if !utf8.ValidString(r.Label) || strings.TrimSpace(r.Label) != r.Label || len(r.Label) == 0 || len(r.Label) > 120 || strings.ContainsFunc(r.Label, unicode.IsControl) {
99 return ErrInvalid
100 }
101 return nil
102}
103
104func (r ExchangeEnrolmentRequest) Validate() error {
105 if r.Version != Version {
106 return ErrInvalid
107 }
108 if _, err := DecodeBytes(r.DeviceSecret, 32); err != nil {
109 return err
110 }
111 if r.Proof.EnrolmentID == "" || len(r.Proof.EnrolmentID) > 128 {
112 return ErrInvalid
113 }
114 for _, s := range []string{r.Proof.Challenge, r.Proof.ExchangeNonce} {
115 if _, err := DecodeBytes(s, 32); err != nil {
116 return err
117 }
118 }
119 _, err := DecodeBytes(r.Proof.Signature, ed25519.SignatureSize)
120 return err
121}
122
123// The fixed array gives every signed field an unambiguous, versioned position.
124func EnrolmentProofMessage(instanceID string, p ExchangeProof) []byte {
125 data, _ := json.Marshal([5]string{DeviceAudience, instanceID, p.EnrolmentID, p.Challenge, p.ExchangeNonce})
126 return data
127}
128
129func MachineKeyHash(publicKey string) string {
130 key, err := DecodeBytes(publicKey, ed25519.PublicKeySize)
131 if err != nil {
132 return ""
133 }
134 digest := sha256.Sum256(key)
135 return hex.EncodeToString(digest[:])
136}
137