gunk-dev/the-valley

verify.go

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