Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/om/cbor/test/gen/oracle/main.go # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / om / cbor / test / gen / oracle / main.go` 729 lines ยท 24228 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // 3 // Test referee only (never part of the om/cbor implementation). It writes the committed vector 4 // files of om/cbor/test/vectors from the real tailscale.com/tka v1.102.0 types and the 5 // github.com/fxamacker/cbor/v2 v2.9.0 decoder that tka uses. 6 // 7 // oracle gen SEED N tka-shaped AUM / NodeKeySignature / State structures 8 // oracle hostile the hostile corpus 9 // oracle fuzz SEED N random mutations of generated structures 10 // oracle utf8 SEED N single text strings, valid and invalid UTF-8 11 // oracle judge reads hex lines on stdin, prints "WF AUM NKS" for each 12 // 13 // WF is the class of the fxamacker Wellformed error under tka's decoder options (0 = well-formed), 14 // numbered as om/cbor cbrdec's error codes. AUM and NKS are 1 when tka's own Unserialize accepts 15 // the bytes into a tka.AUM or a tka.NodeKeySignature. 16 package main 17 18 import ( 19 "bufio" 20 "encoding/hex" 21 "errors" 22 "fmt" 23 "io" 24 "math/rand/v2" 25 "os" 26 "strconv" 27 "strings" 28 "unicode/utf8" 29 30 "github.com/fxamacker/cbor/v2" 31 "golang.org/x/crypto/blake2s" 32 "tailscale.com/tka" 33 "tailscale.com/types/tkatype" 34 ) 35 36 // BEGIN derived from tailscale/tailscale v1.102.0 tka/tka.go lines 22-32 (cborDecOpts). 37 // Copyright (c) 2020 Tailscale Inc & contributors. BSD-3-Clause; the full license text is in 38 // tailscale.LICENSE in this directory. Changes: renamed to tkaDecOpts, comments replaced. 39 // 40 // tkaDecOpts is a copy of tka's cborDecOpts. It is used only for Wellformed (to read the error 41 // class) and to decode a standalone tka.State, which has no Unserialize method. check() proves the 42 // copy equal to tka's own decoder on every judged input: whenever Wellformed fails, tka's 43 // AUM.Unserialize and NodeKeySignature.Unserialize must fail with the same message, because 44 // Unmarshal runs the same Wellformed check first (fxamacker decode.go 1291-1303). 45 var tkaDecOpts = cbor.DecOptions{ 46 DupMapKey: cbor.DupMapKeyEnforcedAPF, 47 IndefLength: cbor.IndefLengthForbidden, 48 TagsMd: cbor.TagsForbidden, 49 MaxNestedLevels: 16, 50 MaxArrayElements: 4096, 51 MaxMapPairs: 1024, 52 } 53 54 // END derived from tailscale tka/tka.go. 55 56 var dec cbor.DecMode 57 var enc cbor.EncMode 58 59 func init() { 60 var err error 61 if dec, err = tkaDecOpts.DecMode(); err != nil { 62 panic(err) 63 } 64 if enc, err = cbor.CTAP2EncOptions().EncMode(); err != nil { 65 panic(err) 66 } 67 } 68 69 // code maps an fxamacker well-formedness error to the om/cbor error code. 70 func code(err error) int { 71 if err == nil { 72 return 0 73 } 74 if err == io.EOF { 75 return 1 76 } 77 if err == io.ErrUnexpectedEOF { 78 return 2 79 } 80 var se *cbor.SyntaxError 81 if errors.As(err, &se) { 82 m := se.Error() 83 switch { 84 case strings.Contains(m, "invalid additional information"): 85 return 3 86 case strings.Contains(m, `unexpected "break" code`): 87 return 5 88 case strings.Contains(m, "invalid simple value"): 89 return 7 90 } 91 } 92 var ie *cbor.IndefiniteLengthError 93 var te *cbor.TagsMdError 94 var ne *cbor.MaxNestedLevelError 95 var ae *cbor.MaxArrayElementsError 96 var me *cbor.MaxMapPairsError 97 var xe *cbor.ExtraneousDataError 98 switch { 99 case errors.As(err, &ie): 100 return 4 101 case errors.As(err, &te): 102 return 6 103 case errors.As(err, &ne): 104 return 8 105 case errors.As(err, &ae): 106 return 9 107 case errors.As(err, &me): 108 return 10 109 case errors.As(err, &xe): 110 return 11 111 case strings.Contains(err.Error(), "too large"): 112 return 12 113 } 114 panic("oracle: unclassified error: " + err.Error()) 115 } 116 117 func b2i(b bool) int { 118 if b { 119 return 1 120 } 121 return 0 122 } 123 124 // verdict is what Go says about one input. 125 type verdict struct { 126 wf int 127 aum, nks bool 128 aumReser []byte // tka Serialize of the decoded AUM, when aum 129 nksReser []byte 130 } 131 132 func check(b []byte) verdict { 133 werr := dec.Wellformed(b) 134 var a tka.AUM 135 aerr := a.Unserialize(b) 136 var s tka.NodeKeySignature 137 serr := s.Unserialize(b) 138 if werr != nil && (aerr == nil || aerr.Error() != werr.Error() || serr == nil || serr.Error() != werr.Error()) { 139 panic(fmt.Sprintf("oracle: tka decoder disagrees with the copied options on %x: %v / %v / %v", b, werr, aerr, serr)) 140 } 141 v := verdict{wf: code(werr), aum: aerr == nil, nks: serr == nil} 142 if v.aum { 143 v.aumReser = a.Serialize() 144 } 145 if v.nks { 146 v.nksReser = s.Serialize() 147 } 148 return v 149 } 150 151 // ---- random tka-shaped structures ---- 152 153 type gen struct{ r *rand.Rand } 154 155 func (g gen) bytes(n int) []byte { 156 b := make([]byte, n) 157 for i := range b { 158 b[i] = byte(g.r.UintN(256)) 159 } 160 return b 161 } 162 163 // a byte length biased to the head boundaries 0, 23, 24, 255, 256 164 func (g gen) blen() int { 165 switch g.r.IntN(10) { 166 case 0: 167 return 0 168 case 1: 169 return 23 + g.r.IntN(3) 170 case 2: 171 return 254 + g.r.IntN(4) 172 case 3: 173 return g.r.IntN(80) 174 } 175 return 32 176 } 177 178 // an unsigned value biased to the head boundaries 179 func (g gen) uval() uint64 { 180 edges := []uint64{0, 1, 23, 24, 255, 256, 65535, 65536, 1<<32 - 1, 1 << 32, 1<<64 - 1} 181 switch g.r.IntN(4) { 182 case 0: 183 return edges[g.r.IntN(len(edges))] 184 case 1: 185 return g.r.Uint64() 186 case 2: 187 return g.r.Uint64N(1 << 20) 188 } 189 return g.r.Uint64N(30) 190 } 191 192 var runes = []rune{'a', 'b', 'z', 'A', '0', ' ', '~', 0x7f, 0x80, 0xfc, 0x7ff, 0x800, 0x6c34, 0xd7ff, 0xe000, 0xfffd, 0xffff, 0x10000, 0x10151, 0x10ffff} 193 194 func (g gen) str() string { 195 n := g.r.IntN(12) 196 if g.r.IntN(8) == 0 { 197 n = 20 + g.r.IntN(120) 198 } 199 var b strings.Builder 200 for range n { 201 if g.r.IntN(3) == 0 { 202 b.WriteRune(runes[g.r.IntN(len(runes))]) 203 } else { 204 b.WriteByte(byte('a' + g.r.IntN(26))) 205 } 206 } 207 return b.String() 208 } 209 210 func (g gen) meta() map[string]string { 211 if g.r.IntN(2) == 0 { 212 return nil 213 } 214 n := g.r.IntN(6) 215 if g.r.IntN(10) == 0 { 216 n = 20 + g.r.IntN(60) 217 } 218 m := make(map[string]string, n) 219 for range n { 220 m[g.str()] = g.str() 221 } 222 return m 223 } 224 225 func (g gen) key() tka.Key { 226 return tka.Key{ 227 Kind: tka.KeyKind(g.r.IntN(3)), 228 Votes: uint(g.uval()), 229 Public: g.bytes(g.blen()), 230 Meta: g.meta(), 231 } 232 } 233 234 func (g gen) state(maxKeys int) *tka.State { 235 s := &tka.State{StateID1: g.uval(), StateID2: g.uval()} 236 if g.r.IntN(4) != 0 { 237 h := tka.AUMHash(g.bytes(32)) 238 s.LastAUMHash = &h 239 } 240 switch g.r.IntN(3) { 241 case 1: 242 s.DisablementValues = [][]byte{} 243 case 2: 244 for i := g.r.IntN(5); i >= 0; i-- { 245 s.DisablementValues = append(s.DisablementValues, g.bytes(g.blen())) 246 } 247 } 248 switch g.r.IntN(3) { 249 case 1: 250 s.Keys = []tka.Key{} 251 case 2: 252 for i := g.r.IntN(maxKeys); i >= 0; i-- { 253 s.Keys = append(s.Keys, g.key()) 254 } 255 } 256 return s 257 } 258 259 func (g gen) aum() tka.AUM { 260 kinds := []tka.AUMKind{tka.AUMAddKey, tka.AUMRemoveKey, tka.AUMNoOp, tka.AUMUpdateKey, tka.AUMCheckpoint, 0, 6, 255} 261 a := tka.AUM{MessageKind: kinds[g.r.IntN(len(kinds))]} 262 switch g.r.IntN(4) { 263 case 0: // genesis: nil parent 264 case 1: 265 a.PrevAUMHash = tka.PrevAUMHash(g.bytes(g.blen())) 266 default: 267 a.PrevAUMHash = tka.PrevAUMHash(g.bytes(32)) 268 } 269 if g.r.IntN(3) == 0 { 270 k := g.key() 271 a.Key = &k 272 } 273 if g.r.IntN(3) == 0 { 274 a.KeyID = g.bytes(g.blen()) 275 } 276 if g.r.IntN(4) == 0 { 277 a.State = g.state(8) 278 } 279 if g.r.IntN(3) == 0 { 280 v := uint(g.uval()) 281 a.Votes = &v 282 } 283 a.Meta = g.meta() 284 for i := g.r.IntN(4) - 1; i >= 0; i-- { 285 a.Signatures = append(a.Signatures, tkatype.Signature{KeyID: g.bytes(32), Signature: g.bytes(64)}) 286 } 287 return a 288 } 289 290 func (g gen) nks(depth int) *tka.NodeKeySignature { 291 s := &tka.NodeKeySignature{SigKind: tka.SigKind(g.r.IntN(5))} 292 if g.r.IntN(5) != 0 { 293 s.Pubkey = g.bytes(32) 294 } 295 if g.r.IntN(2) == 0 { 296 s.KeyID = g.bytes(32) 297 } 298 if g.r.IntN(5) != 0 { 299 s.Signature = g.bytes(64) 300 } 301 if g.r.IntN(2) == 0 { 302 s.WrappingPubkey = g.bytes(32) 303 } 304 if depth > 1 && g.r.IntN(3) != 0 { 305 s.Nested = g.nks(depth - 1) 306 } 307 return s 308 } 309 310 func h32(b []byte) string { 311 s := blake2s.Sum256(b) 312 return hex.EncodeToString(s[:]) 313 } 314 315 // genLine prints "TYPE HEX HASH K1" after checking that Go itself round-trips the bytes. 316 func genLine(w io.Writer, typ string, b []byte, hash string, k1 string) { 317 v := check(b) 318 switch typ { 319 case "aum": 320 if !v.aum || string(v.aumReser) != string(b) { 321 panic("oracle: AUM does not round-trip") 322 } 323 case "nks": 324 if !v.nks || string(v.nksReser) != string(b) { 325 panic("oracle: NKS does not round-trip") 326 } 327 case "state": 328 var s tka.State 329 if err := dec.Unmarshal(b, &s); err != nil { 330 panic(err) 331 } 332 r, err := enc.Marshal(&s) 333 if err != nil || string(r) != string(b) { 334 panic("oracle: State does not round-trip") 335 } 336 } 337 if v.wf != 0 || len(b) > 65536 { 338 panic("oracle: generated structure not usable") 339 } 340 fmt.Fprintf(w, "%s %x %s %s\n", typ, b, hash, k1) 341 } 342 343 func cmdGen(w io.Writer, seed uint64, n int) { 344 g := gen{rand.New(rand.NewPCG(seed, 8949))} 345 for i := range n { 346 switch { 347 case i%12 < 7: 348 a := g.aum() 349 if i == 7*12 { // one checkpoint at tka's 512-key limit (state.go staticValidateCheckpoint) 350 a.State = g.state(1) 351 a.State.Keys = nil 352 for len(a.State.Keys) < 512 { 353 a.State.Keys = append(a.State.Keys, tka.Key{Kind: tka.Key25519, Votes: 1, Public: g.bytes(32)}) 354 } 355 } 356 b := a.Serialize() 357 h := a.Hash() 358 if hex.EncodeToString(h[:]) != h32(b) { 359 panic("oracle: AUM.Hash is not BLAKE2s-256 of Serialize") 360 } 361 genLine(w, "aum", b, hex.EncodeToString(h[:]), "0:"+strconv.Itoa(int(a.MessageKind))) 362 case i%12 < 10: 363 s := g.nks(15) // root plus 14 nested maps at most: depth <= 15 364 b := s.Serialize() 365 genLine(w, "nks", b, h32(b), "0:"+strconv.Itoa(int(s.SigKind))) 366 default: 367 s := g.state(12) 368 b, err := enc.Marshal(s) 369 if err != nil { 370 panic(err) 371 } 372 k1 := "7:22" // LastAUMHash nil encodes as null 373 if s.LastAUMHash != nil { 374 k1 = "2:32" 375 } 376 genLine(w, "state", b, h32(b), k1) 377 } 378 } 379 } 380 381 // ---- hostile corpus ---- 382 383 func mustHex(s string) []byte { 384 b, err := hex.DecodeString(strings.ReplaceAll(s, " ", "")) 385 if err != nil { 386 panic(err) 387 } 388 return b 389 } 390 391 func cat(parts ...[]byte) []byte { 392 var o []byte 393 for _, p := range parts { 394 o = append(o, p...) 395 } 396 return o 397 } 398 399 func rep(b []byte, n int) []byte { 400 var o []byte 401 for range n { 402 o = append(o, b...) 403 } 404 return o 405 } 406 407 // addPairs returns root map m (fewer than 24 pairs, one-byte head) with extra pairs appended. 408 func addPairs(m []byte, k int, pairs ...[]byte) []byte { 409 if m[0] < 0xa0 || m[0]+byte(k) > 0xb7 { 410 panic("oracle: addPairs needs a small map") 411 } 412 return cat([]byte{m[0] + byte(k)}, m[1:], cat(pairs...)) 413 } 414 415 type hcase struct { 416 class, target string 417 b []byte 418 desc string 419 } 420 421 func baseAUM() []byte { 422 a := tka.AUM{ 423 MessageKind: tka.AUMAddKey, 424 PrevAUMHash: tka.PrevAUMHash(rep([]byte{0x11}, 32)), 425 Key: &tka.Key{Kind: tka.Key25519, Votes: 2, Public: rep([]byte{0x22}, 32), Meta: map[string]string{"name": "a"}}, 426 Signatures: []tkatype.Signature{{KeyID: rep([]byte{0x22}, 32), Signature: rep([]byte{0x33}, 64)}}, 427 } 428 return a.Serialize() 429 } 430 431 func nestedNKS(levels int) []byte { 432 var s *tka.NodeKeySignature 433 for i := range levels { 434 s = &tka.NodeKeySignature{SigKind: tka.SigRotation, Pubkey: rep([]byte{byte(i)}, 32), Nested: s} 435 } 436 s.SigKind = tka.SigRotation 437 return s.Serialize() 438 } 439 440 func hostile() []hcase { 441 base := baseAUM() 442 nks := nestedNKS(3) 443 var cs []hcase 444 add := func(class, target string, b []byte, desc string) { 445 cs = append(cs, hcase{class, target, b, desc}) 446 } 447 u99 := mustHex("1863") // unknown key 99 448 unk := func(v []byte) []byte { return addPairs(base, 1, u99, v) } 449 450 add("W", "aum", nil, "empty input") 451 for i := 1; i < len(base); i++ { 452 add("W", "aum", base[:i], fmt.Sprintf("base AUM truncated to %d of %d bytes", i, len(base))) 453 } 454 for i := 1; i < len(nks); i++ { 455 add("W", "nks", nks[:i], fmt.Sprintf("base NKS truncated to %d of %d bytes", i, len(nks))) 456 } 457 add("W", "aum", base, "base AUM (control)") 458 add("W", "nks", nks, "base NKS (control)") 459 add("W", "aum", cat(base, []byte{0}), "one byte after the AUM") 460 add("W", "aum", cat(base, base), "two AUMs back to back") 461 add("W", "aum", cat(base, []byte{0xff}), "break code after the AUM") 462 463 // huge or false declared lengths in a skipped value 464 for _, h := range []string{"`5bffffffffffffffff`", "`5b8000000000000000`", "`5b7fffffffffffffff`", "`5b0000000100000000`", "`5a00010000`", "59ffff", "58ff", "5801", 465 "`7bffffffffffffffff`", "`7b8000000000000000`", "`7a00010000`", "`9bffffffffffffffff`", "`9b8000000000000000`", "`9a00010000`", "991001", "991000", 466 "bbffffffffffffffff", "`bb8000000000000000`", "b90401", "b90400", "1b", "1a0000", "19ff", "18"} { 467 add("W", "aum", unk(mustHex(h)), "skipped value with head "+h+" and no content") 468 } 469 add("W", "aum", unk(cat(mustHex("991000"), rep([]byte{0}, 4096))), "skipped array of 4096 elements (the limit)") 470 add("W", "aum", unk(cat(mustHex("991001"), rep([]byte{0}, 4097))), "skipped array of 4097 elements") 471 var m1024 []byte 472 for i := range 1024 { 473 m1024 = append(m1024, 0x19, byte(i>>8), byte(i), 0) 474 } 475 add("W", "aum", unk(cat(mustHex("b90400"), m1024)), "skipped map of 1024 pairs (the limit)") 476 add("W", "aum", unk(cat(mustHex("b90401"), m1024, mustHex("`1a0001000000`"))), "skipped map of 1025 pairs") 477 add("W", "aum", unk(cat(mustHex("`5a00000004`"), mustHex("`01020304`"))), "skipped byte string with a 4-byte length head") 478 479 // nesting depth: the root map is level 1 480 for _, n := range []int{14, 15, 16, 40, 2000} { 481 add("W", "aum", unk(cat(rep([]byte{0x81}, n), []byte{0x80})), fmt.Sprintf("skipped value: %d nested arrays (depth %d)", n+1, n+2)) 482 add("W", "aum", unk(cat(rep([]byte{0xa1, 0x00}, n), []byte{0xa0})), fmt.Sprintf("skipped value: %d nested maps (depth %d)", n+1, n+2)) 483 } 484 add("W", "aum", unk(cat(rep([]byte{0x81}, 15), []byte{0x9f, 0xff})), "indefinite array at depth 17") 485 add("W", "aum", unk(cat(rep([]byte{0x81}, 14), []byte{0x9f, 0xff})), "indefinite array at depth 16") 486 add("W", "nks", nestedNKS(16), "NodeKeySignature nested 16 deep (tka types, depth 16)") 487 add("W", "nks", nestedNKS(17), "NodeKeySignature nested 17 deep (tka types, depth 17)") 488 add("W", "aum", cat(rep([]byte{0x81}, 3000), []byte{0x80}), "3001 nested arrays at the root") 489 490 // indefinite lengths 491 for _, h := range []string{"5fff", "7fff", "9fff", "bfff", "`5f410000ff`", "`7f6161ff`", "9f00ff", "`bf0000ff`", "5f", "9f"} { 492 add("W", "aum", unk(mustHex(h)), "skipped indefinite-length item "+h) 493 } 494 add("W", "aum", cat([]byte{0xbf}, base[1:], []byte{0xff}), "the AUM as an indefinite-length map") 495 { 496 pb := mustHex("`025f582011111111111111111111111111111111111111111111111111111111111111ff`") 497 i := strings.Index(hex.EncodeToString(base), "025820") 498 if i < 0 || i%2 != 0 { 499 panic("oracle: PrevAUMHash not found") 500 } 501 j := i/2 + 3 + 32 502 add("W", "aum", cat(base[:i/2], pb, base[j:]), "PrevAUMHash as an indefinite byte string of one chunk") 503 } 504 505 // tags 506 for _, h := range []string{"c000", "`c11a514b67b0`", "`d818456449455446`", "`c249010000000000000000`", "c0c000", "`d9d9f700`", "`dbffffffffffffffff00`", "d9", "c0"} { 507 add("W", "aum", unk(mustHex(h)), "skipped tagged item "+h) 508 } 509 add("W", "aum", cat([]byte{0xd8, 0x18}, base), "the AUM wrapped in tag 24") 510 511 // reserved additional information and break codes 512 for m := range 8 { 513 for _, ai := range []int{28, 29, 30} { 514 add("W", "aum", unk([]byte{byte(m<<5 | ai)}), fmt.Sprintf("skipped head: major %d additional information %d", m, ai)) 515 } 516 } 517 for _, h := range []string{"1f", "3f", "df", "ff"} { 518 add("W", "aum", unk(mustHex(h)), "skipped head "+h+" (additional information 31)") 519 } 520 add("W", "aum", mustHex("ff"), "a lone break code") 521 add("W", "aum", mustHex("1c"), "a lone reserved head") 522 523 // simple values and floats (accepted by Go when skipped) 524 for _, h := range []string{"f800", "f813", "f81f", "f820", "f8ff", "e0", "f3", "f7", "f4", "f5", "f6", 525 "f90000", "f97e00", "f97c00", "`fa7fc00000`", "`fb7ff8000000000000`", "`fbfff0000000000000`", "f9", "fa0000", "fb00"} { 526 add("W", "aum", unk(mustHex(h)), "skipped simple value or float "+h) 527 } 528 // form the decoder does not check in skipped values 529 add("W", "aum", unk(mustHex("`a2020101010202`")), "skipped map with unsorted keys") 530 add("W", "aum", unk(mustHex("`a201000100`")), "skipped map with a duplicate key (Go does not check skipped values)") 531 add("W", "aum", unk(mustHex("62c328")), "skipped text string with invalid UTF-8 (Go does not check skipped values)") 532 add("W", "aum", unk(mustHex("`1b0000000000000001`")), "skipped integer in non-shortest form") 533 add("W", "aum", addPairs(base, 1, mustHex("627a7a"), mustHex("00")), "unknown text key \"zz\"") 534 add("W", "aum", addPairs(base, 1, mustHex("38ff"), mustHex("00")), "unknown negative key -256") 535 536 // duplicate keys at the root (APF: Go rejects every duplicate in a struct map) 537 add("D", "aum", addPairs(base, 1, mustHex("01"), mustHex("03")), "MessageKind key 1 twice") 538 add("D", "aum", addPairs(base, 1, mustHex("1801"), mustHex("03")), "key 1 twice, once as 0x1801") 539 add("D", "aum", addPairs(base, 2, u99, mustHex("00"), u99, mustHex("01")), "unknown key 99 twice") 540 add("D", "aum", addPairs(base, 2, mustHex("627a7a"), mustHex("00"), mustHex("627a7a"), mustHex("00")), "unknown text key \"zz\" twice") 541 add("D", "aum", addPairs(base, 2, mustHex("`1bffffffffffffffff`"), mustHex("00"), mustHex("20"), mustHex("00")), "keys 2^64-1 and -1: both int64 -1 in Go") 542 add("D", "nks", addPairs(nks, 1, mustHex("01"), mustHex("02")), "NKS SigKind twice") 543 544 // invalid UTF-8 where Go decodes a text string (Meta) 545 add("U", "aum", mustHex("`a3010302f607a161616261ff`"), "NoOp AUM with Meta value \"a\\xff\"") 546 add("U", "aum", mustHex("`a3010302f607a162c3286161`"), "NoOp AUM with Meta key \"\\xc3(\"") 547 add("U", "aum", mustHex("`a3010302f607a1616163eda080`"), "NoOp AUM with a surrogate (ED A0 80) in a Meta value") 548 549 // non-canonical but accepted: Go's re-serialization is canonical 550 add("R", "aum", mustHex("`a2011803` 02f6"), "MessageKind 3 as 0x1803") 551 add("R", "aum", mustHex("`a201190003` 02f6"), "MessageKind 3 as 0x190003") 552 add("R", "aum", mustHex("`a2011a00000003` 02f6"), "MessageKind 3 as 0x1a00000003") 553 add("R", "aum", mustHex("`a2011b0000000000000003` 02f6"), "MessageKind 3 as 0x1b0000000000000003") 554 add("R", "aum", mustHex("b80201 03 02f6"), "root map count as 0xb802") 555 add("R", "aum", mustHex("`b9000201` 03 02f6"), "root map count as 0xb90002") 556 add("R", "aum", mustHex("`a202f60103`"), "root keys in the order 2, 1") 557 add("R", "aum", mustHex("a3 `07a1616161` 62 0103 02f6"), "root keys in the order 7, 1, 2") 558 add("R", "aum", cat(mustHex("`a301030259` 0020"), rep([]byte{0x11}, 32), mustHex("06 `1b0000000000000005`")), "PrevAUMHash length as 0x590020, Votes 5 as 0x1b0000000000000005") 559 add("R", "aum", mustHex("`a3010302f607a3` 626262 6131 6161 6132 6162 6133"), "Meta keys \"bb\", \"a\", \"b\" (CTAP2 order is a, b, bb)") 560 add("R", "aum", mustHex("`a3010302f607a2` 780161 6131 780162 6132"), "Meta keys with 0x7801 heads") 561 add("R", "aum", cat(mustHex("`a3010302f6179801a2`"), mustHex("015820"), rep([]byte{0x22}, 32), mustHex("025840"), rep([]byte{0x33}, 64)), "Signatures array head 0x9801") 562 add("R", "aum", cat(mustHex("`a3010302f61781a2`"), mustHex("025840"), rep([]byte{0x33}, 64), mustHex("015820"), rep([]byte{0x22}, 32)), "Signature keys in the order 2, 1") 563 add("R", "nks", mustHex("a2 02 41aa 01 01"), "NKS keys in the order 2, 1") 564 add("R", "nks", mustHex("a2 01 02 05 a2 02 41bb 01 1801"), "NKS with a nested NKS whose keys are unsorted and non-shortest") 565 add("R", "aum", mustHex("a3 01 05 02 f6 05 a5 03 80 01 f6 05 01 02 80 04 `1b0000000000000007`"), "Checkpoint with State keys unsorted and StateID1 non-shortest") 566 567 // typed rejections: well-formed CBOR that tka's struct decoding refuses (model-layer duties) 568 add("T", "aum", mustHex("00"), "root is an integer, not a map") 569 add("T", "aum", mustHex("80"), "root is an array") 570 add("T", "aum", mustHex("`a2016178` 02f6"), "MessageKind as a text string") 571 add("T", "aum", mustHex("`a201190100` 02f6"), "MessageKind 256 overflows uint8") 572 add("T", "aum", mustHex("a20120 02f6"), "MessageKind -1") 573 add("T", "aum", mustHex("`a3010302f60620`"), "Votes -1") 574 add("Q", "aum", mustHex("`a201030281` 00"), "PrevAUMHash as the array [0]: fxamacker fills a []byte from an array of integers") 575 add("T", "aum", mustHex("`a3010302f64101` 00"), "byte-string map key") 576 add("T", "aum", mustHex("`a3010302f6` `3bffffffffffffffff` 00"), "negative key below -2^63") 577 add("T", "aum", mustHex("`a3010302f607a2` 6161 6178 6161 6179"), "duplicate Meta key \"a\" (map[string]string)") 578 add("Q", "aum", mustHex("`a3010302f607a16161f6`"), "Meta value null decodes to the empty string") 579 add("T", "aum", mustHex("`a3010302f603a3010102f9000003f6`"), "Key.Votes as a float") 580 // quirks: Go accepts and the canonical re-encoding differs from Go's typed re-serialization 581 add("Q", "aum", mustHex("`a2613103` 02f6"), "text key \"1\" matches the field named 1 (cache.go 139-145)") 582 add("Q", "aum", mustHex("`a3010302f61863f6`"), "unknown key 99 is dropped by Go's re-serialization") 583 add("Q", "aum", mustHex("`a3010302f603f6`"), "Key null decodes to a nil *Key, omitted on re-serialization") 584 return cs 585 } 586 587 func cmdHostile(w io.Writer) { 588 for _, c := range hostile() { 589 v := check(c.b) 590 acc, reser := v.aum, v.aumReser 591 if c.target == "nks" { 592 acc, reser = v.nks, v.nksReser 593 } 594 ok := true 595 switch c.class { 596 case "W": 597 ok = (v.wf == 0) == acc 598 case "D", "U", "T": 599 ok = v.wf == 0 && !acc 600 case "R": 601 ok = acc && string(reser) != string(c.b) 602 case "Q": 603 ok = acc 604 } 605 if !ok { 606 panic(fmt.Sprintf("oracle: hostile case %q (%s) does not fit its class: wf %d accept %v", c.desc, c.class, v.wf, acc)) 607 } 608 h, rs := hex.EncodeToString(c.b), "-" 609 if h == "" { 610 h = "-" 611 } 612 if acc { 613 rs = hex.EncodeToString(reser) 614 } 615 fmt.Fprintf(w, "%s %s %d %d %s %s %s\n", c.class, c.target, v.wf, b2i(acc), rs, h, c.desc) 616 } 617 } 618 619 // ---- fuzz: mutations of generated structures ---- 620 621 func cmdFuzz(w io.Writer, seed uint64, n int) { 622 r := rand.New(rand.NewPCG(seed, 7049)) 623 g := gen{r} 624 for i := range n { 625 var b []byte 626 if i%3 == 2 { 627 b = g.nks(4).Serialize() 628 } else { 629 a := g.aum() // kept small: at most one State key, short Meta 630 if a.State != nil && len(a.State.Keys) > 1 { 631 a.State.Keys = a.State.Keys[:1] 632 } 633 if len(a.Meta) > 2 { 634 a.Meta = map[string]string{"n": "v", "nn": "vv"} 635 } 636 if a.Key != nil { 637 a.Key.Meta = nil 638 } 639 b = a.Serialize() 640 } 641 for k := 1 + r.IntN(3); k > 0 && len(b) > 0; k-- { 642 p := r.IntN(len(b)) 643 switch r.IntN(6) { 644 case 0: 645 b[p] ^= byte(1 << r.IntN(8)) 646 case 1: 647 b[p] = byte(r.UintN(256)) 648 case 2: 649 b = b[:p] 650 case 3: 651 b = cat(b[:p], []byte{byte(r.UintN(256))}, b[p:]) 652 case 4: 653 b = cat(b[:p], b[p+1:]) 654 case 5: 655 specials := []byte{0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1f, 0x5f, 0x7f, 0x9f, 0xbf, 0xc0, 0xd8, 0xf8, 0xf9, 0xfb, 0xff} 656 b[p] = specials[r.IntN(len(specials))] 657 } 658 } 659 v := check(b) 660 h := hex.EncodeToString(b) 661 if h == "" { 662 h = "-" 663 } 664 fmt.Fprintf(w, "%d %d %d %s\n", v.wf, b2i(v.aum), b2i(v.nks), h) 665 } 666 } 667 668 // ---- UTF-8: single text strings ---- 669 670 func cmdUTF8(w io.Writer, seed uint64, n int) { 671 r := rand.New(rand.NewPCG(seed, 3629)) 672 interesting := []byte{0x00, 0x41, 0x7f, 0x80, 0x8f, 0x90, 0x9f, 0xa0, 0xbf, 0xc0, 0xc1, 0xc2, 0xdf, 0xe0, 0xe1, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf3, 0xf4, 0xf5, 0xff} 673 for i := range n { 674 l := r.IntN(9) 675 b := make([]byte, l) 676 for j := range b { 677 b[j] = interesting[r.IntN(len(interesting))] 678 } 679 if i%4 == 0 { // a random valid rune somewhere 680 b = utf8.AppendRune(b, rune(r.IntN(0x110000))) 681 } 682 item, err := enc.Marshal(string(b)) 683 if err != nil { 684 panic(err) 685 } 686 fmt.Fprintf(w, "%d %x\n", b2i(utf8.Valid(b)), item) 687 } 688 } 689 690 func cmdJudge(in io.Reader, w io.Writer) { 691 s := bufio.NewScanner(in) 692 s.Buffer(make([]byte, 1<<20), 1<<20) 693 for s.Scan() { 694 f := strings.Fields(s.Text()) 695 var b []byte 696 if len(f) > 0 && f[0] != "-" { 697 b = mustHex(f[0]) 698 } 699 v := check(b) 700 fmt.Fprintf(w, "%d %d %d\n", v.wf, b2i(v.aum), b2i(v.nks)) 701 } 702 } 703 704 func main() { 705 out := bufio.NewWriter(os.Stdout) 706 defer out.Flush() 707 arg := func(i int) uint64 { 708 v, err := strconv.ParseUint(os.Args[i], 10, 64) 709 if err != nil { 710 panic(err) 711 } 712 return v 713 } 714 switch os.Args[1] { 715 case "gen": 716 cmdGen(out, arg(2), int(arg(3))) 717 case "hostile": 718 cmdHostile(out) 719 case "fuzz": 720 cmdFuzz(out, arg(2), int(arg(3))) 721 case "utf8": 722 cmdUTF8(out, arg(2), int(arg(3))) 723 case "judge": 724 cmdJudge(os.Stdin, out) 725 default: 726 fmt.Fprintln(os.Stderr, "usage: oracle gen SEED N | hostile | fuzz SEED N | utf8 SEED N | judge") 727 os.Exit(2) 728 } 729 }