| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | ) |
| 10 | |
| 11 | // cmdVerify checks an attestation, in the order the protocol states it: |
| 12 | // parse, validate, verify. The note's text is parsed back into a |
| 13 | // statement, which admits nothing but the one written form of what it |
| 14 | // says; that statement is vetted against the schema; and every signature |
| 15 | // by a key the verifier holds is checked over those exact bytes. Given |
| 16 | // --repo, the tree the statement names is then compared with the tree in |
| 17 | // front of the verifier. |
| 18 | // |
| 19 | // Re-running a pure check and confirming its output digest is deliberately |
| 20 | // not here. That is witness re-verification, and it belongs to the trust |
| 21 | // backstop of a later phase; what this verifies is the claim's |
| 22 | // authenticity and its binding to a tree. |
| 23 | func cmdVerify(args []string) error { |
| 24 | fs := newFlagSet("verify") |
| 25 | noteFile := fs.String("note", "", "the signed note") |
| 26 | known := fs.String("known-keys", "", "verifier keys the reader accepts") |
| 27 | schemaFlag := fs.String("schema", "", "attestation schema") |
| 28 | repo := fs.String("repo", "", "repository to re-check against") |
| 29 | rev := fs.String("rev", "HEAD", "revision in it") |
| 30 | var required repeatable |
| 31 | fs.Var(&required, "signer", "signer whose signature must be present") |
| 32 | if err := fs.Parse(args); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | for name, v := range map[string]string{"--note": *noteFile, "--known-keys": *known} { |
| 36 | if v == "" { |
| 37 | return fmt.Errorf("%s is required", name) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | note, err := os.ReadFile(*noteFile) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | keys, err := readVerifierKeys(*known) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | fmt.Printf("note %s\n", *noteFile) |
| 50 | |
| 51 | // The signature covers the text, so the text is what everything below |
| 52 | // is about. Parsing it admits no rendering but the one written form of |
| 53 | // what it says: a statement stored some other way would verify here and |
| 54 | // fail to verify wherever it was written out again. |
| 55 | text, sigs, err := openNote(note, keys) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | doc, err := parseText(text) |
| 60 | if err != nil { |
| 61 | return fmt.Errorf("%s: %w", *noteFile, err) |
| 62 | } |
| 63 | fmt.Printf("text ok (%d bytes signed)\n", len(text)) |
| 64 | |
| 65 | raw, err := json.Marshal(doc) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | root := "" |
| 70 | if *repo != "" { |
| 71 | if root, err = repoRoot(*repo); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | } |
| 75 | schema, err := schemaPath(*schemaFlag, root) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | if err := vetStatement(schema, raw); err != nil { |
| 80 | return err |
| 81 | } |
| 82 | fmt.Printf("schema ok (%s)\n", schema) |
| 83 | |
| 84 | signed := map[string]bool{} |
| 85 | for _, s := range sigs { |
| 86 | state := "unknown" |
| 87 | if s.verified { |
| 88 | state = "ok" |
| 89 | signed[s.name] = true |
| 90 | } |
| 91 | fmt.Printf("signature %-7s %s+%08x\n", state, s.name, s.hash) |
| 92 | } |
| 93 | for _, want := range required { |
| 94 | if !signed[want] { |
| 95 | return fmt.Errorf("no signature on this note is by %s", want) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | var st statement |
| 100 | if err := json.Unmarshal(raw, &st); err != nil { |
| 101 | return err |
| 102 | } |
| 103 | recorded := st.Subject.Digest[st.Subject.Primary] |
| 104 | fmt.Printf("subject %s:%s\n", st.Subject.Primary, recorded) |
| 105 | fmt.Printf("predicate %s (%s %s: %s)\n", st.PredicateType, st.Predicate.Check.Runner, st.Predicate.Check.Name, st.Predicate.Result) |
| 106 | |
| 107 | if root == "" { |
| 108 | fmt.Printf("tree not re-checked; pass --repo to bind this statement to a tree\n") |
| 109 | return nil |
| 110 | } |
| 111 | entries, err := gitTreeEntries(root, *rev) |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | got := treeDigest(entries) |
| 116 | fmt.Printf("tree %s@%s %s:%s\n", root, *rev, treeScheme, got) |
| 117 | if got != recorded { |
| 118 | return fmt.Errorf("tree digest mismatch: the statement is about a different tree\n recorded %s\n found %s", recorded, got) |
| 119 | } |
| 120 | fmt.Printf(" matches the recorded subject digest\n") |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | // cmdRender writes a document as the statement text a signature covers. |
| 125 | // |
| 126 | // It takes either a JSON document or statement text, told apart by the |
| 127 | // first character a JSON object has to open with. Text in, text out is how |
| 128 | // a reader checks that a stored statement is in the one written form of |
| 129 | // what it says: the form is a fixed point, so anything this command |
| 130 | // changes was not it. |
| 131 | func cmdRender(args []string) error { |
| 132 | fs := newFlagSet("render") |
| 133 | if err := fs.Parse(args); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | raw, err := readInput(fs.Args(), "render takes one file, or none to read stdin") |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | var out []byte |
| 141 | if isJSON(raw) { |
| 142 | if out, err = renderJSON(raw); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | } else { |
| 146 | doc, err := parseText(raw) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | if out, err = renderText(doc); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | } |
| 154 | _, err = os.Stdout.Write(out) |
| 155 | return err |
| 156 | } |
| 157 | |
| 158 | // cmdSign signs statement text, or adds a signature to a note that already |
| 159 | // carries one. Which of the two it is doing is decided by the input: |
| 160 | // statement text holds no blank line, and a note is a text and a signature |
| 161 | // block with a blank line between them. |
| 162 | func cmdSign(args []string) error { |
| 163 | fs := newFlagSet("sign") |
| 164 | key := fs.String("key", "", "ed25519 private key to sign with") |
| 165 | name := fs.String("name", defaultSignerName(), "the name this key signs under") |
| 166 | if err := fs.Parse(args); err != nil { |
| 167 | return err |
| 168 | } |
| 169 | raw, err := readInput(fs.Args(), "sign takes one file, or none to read stdin") |
| 170 | if err != nil { |
| 171 | return err |
| 172 | } |
| 173 | s, err := loadSigner(*key, *name) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | var note []byte |
| 178 | if bytes.Contains(raw, []byte("\n\n")) { |
| 179 | note, err = addSignature(raw, s) |
| 180 | } else { |
| 181 | note, err = signNote(raw, s) |
| 182 | } |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | _, err = os.Stdout.Write(note) |
| 187 | return err |
| 188 | } |
| 189 | |
| 190 | // cmdKey prints the verifier key for a signing key: the one line a |
| 191 | // verifier is given in order to accept this signer, and nothing else. |
| 192 | func cmdKey(args []string) error { |
| 193 | fs := newFlagSet("key") |
| 194 | key := fs.String("key", "", "ed25519 private key") |
| 195 | name := fs.String("name", defaultSignerName(), "the name this key signs under") |
| 196 | if err := fs.Parse(args); err != nil { |
| 197 | return err |
| 198 | } |
| 199 | s, err := loadSigner(*key, *name) |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | fmt.Println(s.verifierKey()) |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | // isJSON says whether input is a JSON document rather than statement text. |
| 208 | // A statement is an object, so a JSON one opens with a brace and statement |
| 209 | // text never does. |
| 210 | func isJSON(raw []byte) bool { |
| 211 | return bytes.HasPrefix(bytes.TrimLeft(raw, " \t\r\n"), []byte("{")) |
| 212 | } |
| 213 | |
| 214 | func readInput(args []string, misuse string) ([]byte, error) { |
| 215 | switch len(args) { |
| 216 | case 0: |
| 217 | return io.ReadAll(os.Stdin) |
| 218 | case 1: |
| 219 | return os.ReadFile(args[0]) |
| 220 | default: |
| 221 | return nil, fmt.Errorf("%s", misuse) |
| 222 | } |
| 223 | } |