omnet
aien-dev/omnet
Brought by drakestapleton
omnet / cmd / tkad / test / fixture.go
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Test-only deterministic fixtures using the real pinned tailscale.com/tka API. |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "crypto/ed25519" |
| 8 | "encoding/json" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "github.com/fxamacker/cbor/v2" |
| 12 | "tailscale.com/tailcfg" |
| 13 | "tailscale.com/tka" |
| 14 | "tailscale.com/types/key" |
| 15 | "tailscale.com/types/tkatype" |
| 16 | ) |
| 17 | |
| 18 | func must(e error) { if e != nil { panic(e) } } |
| 19 | func sign(a tka.AUM, p ed25519.PrivateKey) tka.AUM { |
| 20 | h := a.SigHash() |
| 21 | a.Signatures = []tkatype.Signature{{KeyID:tkatype.KeyID(p.Public().(ed25519.PublicKey)), Signature:ed25519.Sign(p,h[:])}} |
| 22 | return a |
| 23 | } |
| 24 | func child(a, prev tka.AUM, p ed25519.PrivateKey) tka.AUM { |
| 25 | h:=prev.Hash(); a.PrevAUMHash=h[:]; return sign(a,p) |
| 26 | } |
| 27 | func hash(a tka.AUM) string { b,e:=a.Hash().MarshalText(); must(e); return string(b) } |
| 28 | func jsonOracle() { |
| 29 | var send tailcfg.TKASyncSendRequest |
| 30 | must(json.Unmarshal([]byte(`{"MiſſingAUMs":["AQ=="],"NodeKey":"nodekey:0909090909090909090909090909090909090909090909090909090909090909"}`), &send)) |
| 31 | if len(send.MissingAUMs)!=1 || send.MissingAUMs[0][0]!=1 || send.NodeKey.IsZero() { panic("Go Unicode field fold changed") } |
| 32 | for _, name:=range []string{"+1","01","-0"} { |
| 33 | var finish tailcfg.TKAInitFinishRequest |
| 34 | must(json.Unmarshal([]byte(`{"Signatures":{"`+name+`":"AQ=="}}`), &finish)) |
| 35 | if len(finish.Signatures)!=1 { panic("Go signed integer map key changed") } |
| 36 | } |
| 37 | var finish tailcfg.TKAInitFinishRequest |
| 38 | if json.Unmarshal([]byte(`{"Signatures":{"9223372036854775808":"AQ=="}}`), &finish)==nil { panic("Go accepted int64 map key overflow") } |
| 39 | } |
| 40 | func main() { |
| 41 | jsonOracle() |
| 42 | p:=ed25519.NewKeyFromSeed(bytes.Repeat([]byte{1},32)) |
| 43 | q:=ed25519.NewKeyFromSeed(bytes.Repeat([]byte{2},32)) |
| 44 | secret:=bytes.Repeat([]byte{42},32) |
| 45 | state:=tka.State{StateID1:123,StateID2:456,DisablementValues:[][]byte{tka.DisablementKDF(secret)},Keys:[]tka.Key{ |
| 46 | {Kind:tka.Key25519,Votes:1,Public:p.Public().(ed25519.PublicKey)}, |
| 47 | {Kind:tka.Key25519,Votes:1,Public:q.Public().(ed25519.PublicKey),Meta:map[string]string{"role":"old"}}, |
| 48 | }} |
| 49 | genesis:=sign(tka.AUM{MessageKind:tka.AUMCheckpoint,State:&state},p) |
| 50 | update:=child(tka.AUM{MessageKind:tka.AUMNoOp},genesis,p) |
| 51 | empty:=child(tka.AUM{MessageKind:tka.AUMUpdateKey,KeyID:tkatype.KeyID(q.Public().(ed25519.PublicKey)),Meta:map[string]string{}},update,p) |
| 52 | removal:=child(tka.AUM{MessageKind:tka.AUMRemoveKey,KeyID:tkatype.KeyID(q.Public().(ed25519.PublicKey))},empty,p) |
| 53 | // Serialize omits an empty Meta. Preserve its non-nil typed value on input, |
| 54 | // while hashing/signing retains upstream canonical omission semantics. |
| 55 | enc,e:=cbor.CTAP2EncOptions().EncMode(); must(e) |
| 56 | var wire map[uint64]any |
| 57 | must(cbor.Unmarshal(empty.Serialize(),&wire)); wire[7]=map[string]string{} |
| 58 | emptyWire,e:=enc.Marshal(wire); must(e) |
| 59 | ch:=tka.ChonkMem(); authority,e:=tka.Bootstrap(ch,genesis); must(e) |
| 60 | nodes:=[]string{}; rots:=[][]byte{}; sigs:=[][]byte{}; goodSigs:=[][]byte{} |
| 61 | for i:=byte(9);i<12;i++ { |
| 62 | var np key.NodePublic |
| 63 | must(np.UnmarshalBinary(append([]byte{'n','p'},bytes.Repeat([]byte{i},32)...))) |
| 64 | binary,e:=np.MarshalBinary(); must(e) |
| 65 | rot:=ed25519.NewKeyFromSeed(bytes.Repeat([]byte{i+32},32)).Public().(ed25519.PublicKey) |
| 66 | s:=tka.NodeKeySignature{SigKind:tka.SigDirect,Pubkey:binary,KeyID:q.Public().(ed25519.PublicKey),WrappingPubkey:rot} |
| 67 | h:=s.SigHash();s.Signature=ed25519.Sign(q,h[:]);must(authority.NodeKeyAuthorized(np,s.Serialize())) |
| 68 | nodes=append(nodes,np.String());rots=append(rots,rot);sigs=append(sigs,s.Serialize()) |
| 69 | s.KeyID=p.Public().(ed25519.PublicKey);h=s.SigHash();s.Signature=ed25519.Sign(p,h[:]);goodSigs=append(goodSigs,s.Serialize()) |
| 70 | } |
| 71 | var decoded tka.AUM;must(decoded.Unserialize(emptyWire));must(decoded.StaticValidate()) |
| 72 | must(authority.Inform(ch,[]tka.AUM{update,decoded,removal})) |
| 73 | var np key.NodePublic;must(np.UnmarshalText([]byte(nodes[0]))) |
| 74 | if authority.NodeKeyAuthorized(np,sigs[0])==nil { panic("removed signer remains trusted") } |
| 75 | must(authority.NodeKeyAuthorized(np,goodSigs[0])) |
| 76 | if !authority.ValidDisablement(secret) { panic("disablement mismatch") } |
| 77 | result:=map[string]any{"genesis":genesis.Serialize(),"head":hash(genesis),"update":update.Serialize(),"updatedHead":hash(update),"emptyMeta":emptyWire,"emptyHead":hash(empty),"removal":removal.Serialize(),"removedHead":hash(removal),"nodes":nodes,"rotations":rots,"signatures":sigs,"goodSignatures":goodSigs,"keyID":q.Public().(ed25519.PublicKey),"secret":secret,"source":"tailscale.com v1.102.0 real tka authority"} |
| 78 | b,e:=json.MarshalIndent(result,""," ");must(e);fmt.Fprintln(os.Stdout,string(b)) |
| 79 | } |