| 1 | package main |
| 2 | |
| 3 | // The canonical form of a statement: the exact bytes the signature covers. |
| 4 | // Two tools that compose the same statement must produce the same bytes, |
| 5 | // or a signature made by one cannot be checked by the other. |
| 6 | // |
| 7 | // The rules are RFC 8785's, narrowed to what a statement can hold: object |
| 8 | // members sorted by key, no whitespace between tokens, and minimal string |
| 9 | // escaping. A statement carries strings, objects and arrays only โ never a |
| 10 | // number โ so the one genuinely hard part of RFC 8785, canonicalizing |
| 11 | // floating-point, cannot arise. A number reaching this code is a statement |
| 12 | // shape nobody has decided the canonical form of, so it is an error rather |
| 13 | // than a guess. |
| 14 | // |
| 15 | // Keys are sorted by their UTF-8 bytes. RFC 8785 sorts by UTF-16 code |
| 16 | // units; the two orderings agree over ASCII, and every key in |
| 17 | // schema/attestation.cue is ASCII. |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "sort" |
| 24 | ) |
| 25 | |
| 26 | func canonicalJSON(v any) ([]byte, error) { |
| 27 | var b bytes.Buffer |
| 28 | if err := writeCanonical(&b, v); err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | return b.Bytes(), nil |
| 32 | } |
| 33 | |
| 34 | // canonicalize round-trips a value through encoding/json so that struct |
| 35 | // tags, omitempty and nesting are all applied before canonical bytes are |
| 36 | // rendered from the resulting generic value. |
| 37 | func canonicalize(v any) ([]byte, error) { |
| 38 | raw, err := json.Marshal(v) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | var generic any |
| 43 | dec := json.NewDecoder(bytes.NewReader(raw)) |
| 44 | dec.UseNumber() |
| 45 | if err := dec.Decode(&generic); err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | return canonicalJSON(generic) |
| 49 | } |
| 50 | |
| 51 | func writeCanonical(b *bytes.Buffer, v any) error { |
| 52 | switch t := v.(type) { |
| 53 | case nil: |
| 54 | b.WriteString("null") |
| 55 | case bool: |
| 56 | if t { |
| 57 | b.WriteString("true") |
| 58 | } else { |
| 59 | b.WriteString("false") |
| 60 | } |
| 61 | case string: |
| 62 | return writeCanonicalString(b, t) |
| 63 | case []any: |
| 64 | b.WriteByte('[') |
| 65 | for i, e := range t { |
| 66 | if i > 0 { |
| 67 | b.WriteByte(',') |
| 68 | } |
| 69 | if err := writeCanonical(b, e); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | } |
| 73 | b.WriteByte(']') |
| 74 | case map[string]any: |
| 75 | keys := make([]string, 0, len(t)) |
| 76 | for k := range t { |
| 77 | keys = append(keys, k) |
| 78 | } |
| 79 | sort.Strings(keys) |
| 80 | b.WriteByte('{') |
| 81 | for i, k := range keys { |
| 82 | if i > 0 { |
| 83 | b.WriteByte(',') |
| 84 | } |
| 85 | if err := writeCanonicalString(b, k); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | b.WriteByte(':') |
| 89 | if err := writeCanonical(b, t[k]); err != nil { |
| 90 | return err |
| 91 | } |
| 92 | } |
| 93 | b.WriteByte('}') |
| 94 | default: |
| 95 | return fmt.Errorf("canonical json: a statement may not carry %T", v) |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | // writeCanonicalString delegates escaping to encoding/json, with HTML |
| 101 | // escaping off โ Go escapes <, > and & by default, which RFC 8785 does |
| 102 | // not. |
| 103 | func writeCanonicalString(b *bytes.Buffer, s string) error { |
| 104 | var enc bytes.Buffer |
| 105 | e := json.NewEncoder(&enc) |
| 106 | e.SetEscapeHTML(false) |
| 107 | if err := e.Encode(s); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | b.Write(bytes.TrimRight(enc.Bytes(), "\n")) |
| 111 | return nil |
| 112 | } |