interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / tkasig / test / oracle.go

274 lines · 9346 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Test-only oracle calls Tailscale v1.102.0, fetched 2026-10-25.
3package main
4
5import (
6 "bytes"
7 "crypto/ed25519"
8 "encoding/hex"
9 "encoding/json"
10 "fmt"
11 "github.com/fxamacker/cbor/v2"
12 "math/rand/v2"
13 "os"
14 "tailscale.com/tka"
15 "tailscale.com/types/key"
16 "tailscale.com/types/tkatype"
17)
18
19type row struct {
20 Name string
21 Input string
22 Node string
23 Keys []string
24 Decode bool
25 Canon string
26 Hash string
27 Auth bool
28}
29
30var enc = json.NewEncoder(os.Stdout)
31var root = ed25519.NewKeyFromSeed(bytes.Repeat([]byte{7}, 32))
32var wrap = ed25519.NewKeyFromSeed(bytes.Repeat([]byte{9}, 32))
33var pub = root.Public().(ed25519.PublicKey)
34var wp = wrap.Public().(ed25519.PublicKey)
35var node = append([]byte("np"), bytes.Repeat([]byte{11}, 32)...)
36var keys = [][]byte{pub}
37var authority *tka.Authority
38
39func makeAuthority(ks [][]byte) *tka.Authority {
40 state := tka.State{DisablementValues: [][]byte{bytes.Repeat([]byte{1}, 32)}}
41 for _, p := range ks {
42 state.Keys = append(state.Keys, tka.Key{Kind: tka.Key25519, Public: p, Votes: 1})
43 }
44 aum := tka.AUM{MessageKind: tka.AUMCheckpoint, State: &state}
45 signer := root
46 if len(ks) == 1 && bytes.Equal(ks[0], wp) {
47 signer = wrap
48 }
49 h := aum.SigHash()
50 aum.Signatures = []tkatype.Signature{{KeyID: tkatype.KeyID(signer.Public().(ed25519.PublicKey)), Signature: ed25519.Sign(signer, h[:])}}
51 a, e := tka.Bootstrap(tka.ChonkMem(), aum)
52 if e != nil {
53 panic(e)
54 }
55 return a
56}
57func emit(name string, b []byte) {
58 r := row{Name: name, Input: hex.EncodeToString(b), Node: hex.EncodeToString(node[2:])}
59 for _, k := range keys {
60 r.Keys = append(r.Keys, hex.EncodeToString(k))
61 }
62 var s tka.NodeKeySignature
63 r.Decode = s.Unserialize(b) == nil
64 if r.Decode {
65 r.Canon = hex.EncodeToString(s.Serialize())
66 h := s.SigHash()
67 r.Hash = hex.EncodeToString(h[:])
68 }
69 var n key.NodePublic
70 if e := n.UnmarshalBinary(node); e != nil {
71 panic(e)
72 }
73 r.Auth = authority.NodeKeyAuthorized(n, b) == nil
74 if e := enc.Encode(r); e != nil {
75 panic(e)
76 }
77}
78func sign(s tka.NodeKeySignature, k ed25519.PrivateKey) tka.NodeKeySignature {
79 h := s.SigHash()
80 s.Signature = ed25519.Sign(k, h[:])
81 return s
82}
83func raw(name, s string) {
84 b, e := hex.DecodeString(s)
85 if e != nil {
86 panic(e)
87 }
88 emit(name, b)
89}
90func main() {
91 authority = makeAuthority(keys)
92 direct := sign(tka.NodeKeySignature{SigKind: tka.SigDirect, Pubkey: node, KeyID: pub, WrappingPubkey: wp}, root)
93 cred := sign(tka.NodeKeySignature{SigKind: tka.SigCredential, KeyID: pub, WrappingPubkey: wp}, root)
94 samples := []tka.NodeKeySignature{direct, cred}
95 for _, base := range []tka.NodeKeySignature{direct, cred} {
96 s := base
97 for depth := 1; depth <= 17; depth++ {
98 inner := s
99 s = sign(tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: node, Nested: &inner}, wrap)
100 samples = append(samples, s)
101 }
102 }
103 for i, s := range samples {
104 emit(fmt.Sprintf("valid-or-depth-%d", i), s.Serialize())
105 if i < 6 {
106 b := append([]byte(nil), s.Serialize()...)
107 b[2] = 0xe0 | b[2]
108 emit(fmt.Sprintf("signed-simple-kind-%d", i), b)
109 }
110 }
111 // Per-byte mutations include signature, identity, bindings, kind and CBOR boundaries.
112 rng := rand.New(rand.NewPCG(41, 9))
113 for i := range 220 {
114 s := samples[rng.IntN(8)]
115 b := append([]byte(nil), s.Serialize()...)
116 p := rng.IntN(len(b))
117 b[p] ^= byte(1 << rng.IntN(8))
118 emit(fmt.Sprintf("mutation-%d", i), b)
119 }
120 b := samples[2].Serialize()
121 for i := range b {
122 emit(fmt.Sprintf("truncate-%d", i), b[:i])
123 }
124 for _, v := range []struct{ n, b string }{
125 {"empty", ""}, {"null", "f6"}, {"undefined", "f7"}, {"empty-map", "a0"}, {"wrong-root", "80"},
126 {"text-kind", "a1613101"}, {"duplicate-alias", "a20101613101"}, {"unknown", "a201011818a201000100"},
127 {"unknown-duplicate", "a2181800181801"}, {"unknown-int-collision", "a21bffffffffffffffff002001"},
128 {"bad-key-bytes", "a1413101"}, {"bad-key-negative", "a13b800000000000000000"}, {"invalid-key-text", "a161ff01"},
129 {"array-bytes", "a202830018fff60382f700"}, {"bad-byte", "a10281190100"}, {"negative-byte", "a1028120"},
130 {"bad-kind", "a101190100"}, {"negative-kind", "a10120"}, {"null-kind", "a101f6"}, {"float-kind", "a101f90000"},
131 {"max-kind", "a10118ff"},
132 {"null-fields", "a601f602f603f604f605f606f6"}, {"wrong-nested", "a10580"}, {"tag", "c0a0"}, {"trailing", "a000"},
133 {"noncanonical", "b800"}, {"undefined-kind", "a101f7"}, {"bool-kind", "a101f5"},
134 } {
135 raw(v.n, v.b)
136 }
137 for v := range 256 {
138 simple := []byte{0xf8, byte(v)}
139 if v < 24 {
140 simple = []byte{0xe0 | byte(v)}
141 }
142 emit(fmt.Sprintf("simple-kind-%d", v), append([]byte{0xa1, 1}, simple...))
143 emit(fmt.Sprintf("simple-byte-%d", v), append([]byte{0xa1, 2, 0x81}, simple...))
144 }
145 // Canonical typed reconstruction of alternate byte-slice representations.
146 cm, _ := cbor.CTAP2EncOptions().EncMode()
147 for i, s := range samples[:6] {
148 var obj map[uint64]any
149 cbor.Unmarshal(s.Serialize(), &obj)
150 for _, field := range []uint64{2, 3, 4, 6} {
151 if data, ok := obj[field].([]byte); ok {
152 a := make([]uint64, len(data))
153 for j, x := range data {
154 a[j] = uint64(x)
155 }
156 obj[field] = a
157 }
158 }
159 obj[99] = map[uint64]any{1: "ignored"}
160 b, _ := cm.Marshal(obj)
161 emit(fmt.Sprintf("array-fields-%d", i), b)
162 for _, field := range []uint64{2, 3, 4, 6} {
163 if data, ok := obj[field].([]uint64); ok {
164 a := make([]any, len(data))
165 for j, x := range data {
166 if x <= 19 || x >= 32 {
167 a[j] = cbor.SimpleValue(x)
168 } else {
169 a[j] = x
170 }
171 }
172 obj[field] = a
173 }
174 }
175 b, err := cm.Marshal(obj)
176 if err != nil {
177 panic(err)
178 }
179 emit(fmt.Sprintf("signed-simple-byte-fields-%d", i), b)
180 }
181 bad := direct
182 bad.Pubkey = bytes.Repeat([]byte{0}, 34)
183 bad = sign(bad, root)
184 emit("signed-wrong-node", bad.Serialize())
185 bad = direct
186 bad.Pubkey = append([]byte("np"), bytes.Repeat([]byte{12}, 32)...)
187 bad = sign(bad, root)
188 emit("signed-other-node", bad.Serialize())
189 bad = direct
190 bad.KeyID = wp
191 bad = sign(bad, root)
192 emit("signed-untrusted-key", bad.Serialize())
193 bad = direct
194 bad.Nested = &cred
195 bad = sign(bad, root)
196 emit("signed-direct-nested", bad.Serialize())
197 bad = cred
198 bad.Pubkey = []byte{1}
199 bad = sign(bad, root)
200 outer := sign(tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: node, Nested: &bad}, wrap)
201 emit("credential-pubkey-invalid-details", outer.Serialize())
202 bad = cred
203 bad.Pubkey = node
204 bad = sign(bad, root)
205 outer = sign(tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: node, Nested: &bad}, wrap)
206 emit("credential-pubkey-valid-details", outer.Serialize())
207 intermediate := sign(tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: node, Nested: &direct, WrappingPubkey: pub}, wrap)
208 outer = sign(tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: node, Nested: &intermediate}, root)
209 emit("overridden-wrapping-key", outer.Serialize())
210 outer = sign(outer, wrap)
211 emit("wrong-overridden-wrapping-key", outer.Serialize())
212 for _, n := range []int{0, 1, 31, 33, 64} {
213 bad = direct
214 bad.WrappingPubkey = make([]byte, n)
215 bad = sign(bad, root)
216 outer = sign(tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: node, Nested: &bad}, wrap)
217 emit(fmt.Sprintf("bad-wrapping-%d", n), outer.Serialize())
218 }
219 for _, n := range []int{0, 1, 63, 65} {
220 bad = direct
221 bad.Signature = make([]byte, n)
222 emit(fmt.Sprintf("bad-signature-length-%d", n), bad.Serialize())
223 }
224 // rotationDetails checks nonempty credential Pubkey even after signature verification.
225 // ZIP-215 witness: noncanonical identity R accepted by consensus but not strict Go.
226 identity := make([]byte, 32)
227 identity[0] = 1
228 keys = [][]byte{pub, identity}
229 authority = makeAuthority(keys)
230 sg := make([]byte, 64)
231 sg[0] = 0xee
232 for j := 1; j < 31; j++ {
233 sg[j] = 0xff
234 }
235 sg[31] = 0x7f
236 z := tka.NodeKeySignature{SigKind: tka.SigDirect, Pubkey: node, KeyID: identity, Signature: sg, WrappingPubkey: identity}
237 emit("zip215-direct", z.Serialize())
238 cr := tka.NodeKeySignature{SigKind: tka.SigCredential, KeyID: identity, Signature: sg, WrappingPubkey: wp}
239 outer = sign(tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: node, Nested: &cr}, wrap)
240 emit("zip215-credential", outer.Serialize())
241 outer = tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: node, Nested: &z, Signature: sg}
242 emit("strict-rotation", outer.Serialize())
243 // Large fields exercise bounded serialization and decoding at the transport ceiling.
244 for _, n := range []int{0, 23, 24, 255, 256, 4096, 65000, 65520, 65529} {
245 s := tka.NodeKeySignature{Pubkey: make([]byte, n)}
246 emit(fmt.Sprintf("large-%d", n), s.Serialize())
247 }
248 for _, n := range []int{16, 17, 1000} {
249 b := []byte{0xa0}
250 for j := 1; j < n; j++ {
251 b = append([]byte{0xa1, 5}, b...)
252 }
253 emit(fmt.Sprintf("depth-%d", n), b)
254 }
255 for _, n := range []int{4096, 4097} {
256 b := []byte{0xa1, 2, 0x99, byte(n >> 8), byte(n)}
257 b = append(b, make([]byte, n)...)
258 emit(fmt.Sprintf("byte-array-limit-%d", n), b)
259 }
260 keys = [][]byte{wp}
261 authority = makeAuthority(keys)
262 emit("authority-key-removed", direct.Serialize())
263 keys = nil
264 for i := range 511 {
265 seed := make([]byte, 32)
266 seed[0] = byte(i)
267 seed[1] = byte(i >> 8)
268 seed[2] = 77
269 keys = append(keys, ed25519.NewKeyFromSeed(seed).Public().(ed25519.PublicKey))
270 }
271 keys = append(keys, pub)
272 authority = makeAuthority(keys)
273 emit("authority-512-last-key", direct.Serialize())
274}