| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "io/fs" |
| 7 | "os" |
| 8 | "regexp" |
| 9 | |
| 10 | "the-valley/note" |
| 11 | ) |
| 12 | |
| 13 | // An attestation ref is named for what it holds: the digest of the tree |
| 14 | // its notes are about, then the key hash of the signer it is filed under |
| 15 | // (archive/design/contribute.md, step 5). attest writes the digest as 64 lowercase |
| 16 | // hex digits and the key hash as 8. |
| 17 | var attestationName = regexp.MustCompile(`^refs/the-valley/attestations/([0-9a-f]{64})/([0-9a-f]{8})$`) |
| 18 | |
| 19 | // checkAttestation decides whether a push may create the attestation ref |
| 20 | // ref pointing at id. The namespace is create-only, so whoever creates a |
| 21 | // name first holds it for good, and a name that anyone could take would |
| 22 | // let one pusher keep another signer's evidence out. So a ref must be what |
| 23 | // its name says before it may exist: |
| 24 | // |
| 25 | // - its name is exactly <digest>/<key hash>. A ref named for the digest |
| 26 | // alone would stop git creating any ref beneath it. |
| 27 | // - it points at a tree of notes, read within fixed bounds (repo.go). |
| 28 | // - every note opens under the keys this host accepts evidence from, |
| 29 | // the way any verifier opens it (the note module): the envelope is |
| 30 | // well formed, every signature by an accepted key verifies, and at |
| 31 | // least one does. |
| 32 | // - every note is a statement about the digest the name gives, and |
| 33 | // carries a verified signature under the key hash the name gives. |
| 34 | // |
| 35 | // A host that names no keys accepts no attestation: a note nobody here |
| 36 | // could verify is not one this namespace can hold. |
| 37 | // |
| 38 | // The pusher is not compared with the signer. Evidence is routinely |
| 39 | // relayed: the host's own key signs what `attest run` observes there, and |
| 40 | // the operator's key pushes it. |
| 41 | func (p policy) checkAttestation(ref, id string) error { |
| 42 | parts := attestationName.FindStringSubmatch(ref) |
| 43 | if parts == nil { |
| 44 | return errors.New("an attestation ref is named refs/the-valley/attestations/<digest>/<key hash>, with the tree digest as 64 lowercase hex digits and the signer's key hash as 8") |
| 45 | } |
| 46 | digest, keyHash := parts[1], parts[2] |
| 47 | if len(p.verifiers) == 0 { |
| 48 | return errors.New("this host names no keys it accepts evidence from, so no attestation can be checked here") |
| 49 | } |
| 50 | |
| 51 | notes, err := p.repo.notes(id) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | for _, path := range sortedKeys(notes) { |
| 56 | if err := p.checkNote(notes[path], digest, keyHash); err != nil { |
| 57 | return fmt.Errorf("the note at %s %s", path, err) |
| 58 | } |
| 59 | } |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | func (p policy) checkNote(n []byte, digest, keyHash string) error { |
| 64 | text, checked, err := note.Open(n, p.verifiers) |
| 65 | if err != nil { |
| 66 | return fmt.Errorf("does not open: %v", err) |
| 67 | } |
| 68 | subject, err := note.StatementSubject(text) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if subject.Digest != digest { |
| 73 | return fmt.Errorf("is about the tree %s, not the one the ref is named for", subject.Digest) |
| 74 | } |
| 75 | for _, c := range checked { |
| 76 | if c.Verified && fmt.Sprintf("%08x", c.Hash) == keyHash { |
| 77 | return nil |
| 78 | } |
| 79 | } |
| 80 | return fmt.Errorf("carries no signature under the key hash %s by a key this host accepts", keyHash) |
| 81 | } |
| 82 | |
| 83 | // readVerifiers reads the verifier keys in one file. A file that does not |
| 84 | // exist names none: the compiled file is missing until the first |
| 85 | // compilation, and then no attestation is accepted, which is the |
| 86 | // direction a missing key has to default in. A line that is not a |
| 87 | // verifier key fails the read. |
| 88 | func readVerifiers(path string) ([]note.VerifierKey, error) { |
| 89 | data, err := os.ReadFile(path) |
| 90 | if errors.Is(err, fs.ErrNotExist) { |
| 91 | return nil, nil |
| 92 | } |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | keys, err := note.ParseVerifierKeys(data) |
| 97 | if err != nil { |
| 98 | return nil, fmt.Errorf("%s: %w", path, err) |
| 99 | } |
| 100 | return keys, nil |
| 101 | } |