gunk-dev/the-valley

verify.go

sourceb9d42e2a97e7main's published tip history
3.6 KiB
1package main
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "os"
8)
9
10// cmdVerify checks an attestation: that the statement satisfies the
11// schema, that its bytes are the canonical form of what it says, that an
12// allowed signer signed exactly those bytes, and that the tree the
13// statement names is the tree in front of the verifier.
14//
15// Re-running a pure check and confirming its output digest is deliberately
16// not here. That is witness re-verification, and it belongs to the trust
17// backstop of a later phase; what this verifies is the claim's
18// authenticity and its binding to a tree.
19func cmdVerify(args []string) error {
20 fs := newFlagSet("verify")
21 statementFile := fs.String("statement", "", "the statement")
22 signatureFile := fs.String("signature", "", "its detached signature")
23 allowed := fs.String("allowed-signers", "", "ssh allowed-signers file")
24 principal := fs.String("principal", "", "expected signer")
25 namespace := fs.String("namespace", defaultNamespace, "SSHSIG namespace")
26 schemaFlag := fs.String("schema", "", "attestation schema")
27 repo := fs.String("repo", "", "repository to re-check against")
28 rev := fs.String("rev", "HEAD", "revision in it")
29 if err := fs.Parse(args); err != nil {
30 return err
31 }
32 for name, v := range map[string]string{"--statement": *statementFile, "--signature": *signatureFile, "--allowed-signers": *allowed} {
33 if v == "" {
34 return fmt.Errorf("%s is required", name)
35 }
36 }
37
38 raw, err := os.ReadFile(*statementFile)
39 if err != nil {
40 return err
41 }
42 fmt.Printf("statement %s\n", *statementFile)
43
44 root := ""
45 if *repo != "" {
46 if root, err = repoRoot(*repo); err != nil {
47 return err
48 }
49 }
50 schema, err := schemaPath(*schemaFlag, root)
51 if err != nil {
52 return err
53 }
54 if err := vetStatement(schema, raw); err != nil {
55 return err
56 }
57 fmt.Printf("schema ok (%s)\n", schema)
58
59 // The signature covers bytes, and canonical form is what makes those
60 // bytes a function of the statement. A statement stored in some other
61 // rendering would verify here and fail to verify wherever it was
62 // recomposed, so it is rejected.
63 var generic any
64 dec := json.NewDecoder(bytes.NewReader(raw))
65 dec.UseNumber()
66 if err := dec.Decode(&generic); err != nil {
67 return fmt.Errorf("%s: %w", *statementFile, err)
68 }
69 canonical, err := canonicalJSON(generic)
70 if err != nil {
71 return err
72 }
73 if !bytes.Equal(canonical, raw) {
74 return fmt.Errorf("%s is not in canonical form; the signature would not travel", *statementFile)
75 }
76 fmt.Printf("canonical ok (%d bytes signed)\n", len(raw))
77
78 who := *principal
79 if who == "" {
80 if who, err = findPrincipal(*allowed, *signatureFile); err != nil {
81 return err
82 }
83 }
84 said, err := verifyDetached(*allowed, who, *namespace, *signatureFile, raw)
85 if err != nil {
86 return fmt.Errorf("signature: %s", err)
87 }
88 fmt.Printf("signature %s\n", said)
89
90 var st statement
91 if err := json.Unmarshal(raw, &st); err != nil {
92 return err
93 }
94 recorded := st.Subject.Digest[st.Subject.Primary]
95 fmt.Printf("subject %s:%s\n", st.Subject.Primary, recorded)
96 fmt.Printf("predicate %s (%s %s: %s)\n", st.PredicateType, st.Predicate.Check.Runner, st.Predicate.Check.Name, st.Predicate.Result)
97
98 if root == "" {
99 fmt.Printf("tree not re-checked; pass --repo to bind this statement to a tree\n")
100 return nil
101 }
102 entries, err := gitTreeEntries(root, *rev)
103 if err != nil {
104 return err
105 }
106 got := treeDigest(entries)
107 fmt.Printf("tree %s@%s %s:%s\n", root, *rev, treeScheme, got)
108 if got != recorded {
109 return fmt.Errorf("tree digest mismatch: the statement is about a different tree\n recorded %s\n found %s", recorded, got)
110 }
111 fmt.Printf(" matches the recorded subject digest\n")
112 return nil
113}