gunk-dev/the-valley

verify.go

source07d67d385df1main's published tip history
3.5 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 canonical, err := canonicalRender(raw)
64 if err != nil {
65 return fmt.Errorf("%s: %w", *statementFile, err)
66 }
67 if !bytes.Equal(canonical, raw) {
68 return fmt.Errorf("%s is not in canonical form; the signature would not travel", *statementFile)
69 }
70 fmt.Printf("canonical ok (%d bytes signed)\n", len(raw))
71
72 who := *principal
73 if who == "" {
74 if who, err = findPrincipal(*allowed, *signatureFile); err != nil {
75 return err
76 }
77 }
78 said, err := verifyDetached(*allowed, who, *namespace, *signatureFile, raw)
79 if err != nil {
80 return fmt.Errorf("signature: %s", err)
81 }
82 fmt.Printf("signature %s\n", said)
83
84 var st statement
85 if err := json.Unmarshal(raw, &st); err != nil {
86 return err
87 }
88 recorded := st.Subject.Digest[st.Subject.Primary]
89 fmt.Printf("subject %s:%s\n", st.Subject.Primary, recorded)
90 fmt.Printf("predicate %s (%s %s: %s)\n", st.PredicateType, st.Predicate.Check.Runner, st.Predicate.Check.Name, st.Predicate.Result)
91
92 if root == "" {
93 fmt.Printf("tree not re-checked; pass --repo to bind this statement to a tree\n")
94 return nil
95 }
96 entries, err := gitTreeEntries(root, *rev)
97 if err != nil {
98 return err
99 }
100 got := treeDigest(entries)
101 fmt.Printf("tree %s@%s %s:%s\n", root, *rev, treeScheme, got)
102 if got != recorded {
103 return fmt.Errorf("tree digest mismatch: the statement is about a different tree\n recorded %s\n found %s", recorded, got)
104 }
105 fmt.Printf(" matches the recorded subject digest\n")
106 return nil
107}