| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "io/fs" |
| 8 | "os" |
| 9 | "regexp" |
| 10 | |
| 11 | "the-valley/note" |
| 12 | ) |
| 13 | |
| 14 | // An attestation ref is named for what it holds: the digest of the tree |
| 15 | // its notes are about, then the key hash of the signer it is filed under |
| 16 | // (archive/design/contribute.md, step 5). attest writes the digest as 64 lowercase |
| 17 | // hex digits and the key hash as 8. |
| 18 | var attestationName = regexp.MustCompile(`^refs/the-valley/attestations/([0-9a-f]{64})/([0-9a-f]{8})$`) |
| 19 | |
| 20 | // checkAttestation decides whether a push may create the attestation ref |
| 21 | // ref pointing at id. A ref takes added notes later (checkAttestationUpdate), |
| 22 | // but no push changes or removes one, so whoever creates a name first holds |
| 23 | // it for good, and a name that anyone could take would let one pusher keep |
| 24 | // another signer's evidence out. So a ref must be what its name says before |
| 25 | // it may exist: |
| 26 | // |
| 27 | // - its name is exactly <digest>/<key hash>. A ref named for the digest |
| 28 | // alone would stop git creating any ref beneath it. |
| 29 | // - it points at a tree of notes, read within fixed bounds (repo.go). |
| 30 | // - every note opens under the keys this host accepts evidence from, |
| 31 | // the way any verifier opens it (the note module): the envelope is |
| 32 | // well formed, every signature by an accepted key verifies, and at |
| 33 | // least one does. |
| 34 | // - every note is a statement about the digest the name gives, and |
| 35 | // carries a verified signature under the key hash the name gives. |
| 36 | // |
| 37 | // A host that names no keys accepts no attestation: a note nobody here |
| 38 | // could verify is not one this namespace can hold. |
| 39 | // |
| 40 | // The pusher is not compared with the signer. Evidence is routinely |
| 41 | // relayed: the host's own key signs what `attest run` observes there, and |
| 42 | // the operator's key pushes it. |
| 43 | func (p policy) checkAttestation(ref, id string) error { |
| 44 | _, err := p.attestationNotes(ref, id) |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | // checkAttestationUpdate decides whether a push may move the attestation |
| 49 | // ref ref from old to id. Published evidence is never changed or removed, |
| 50 | // so the ref only takes added notes: the new tree must be everything a |
| 51 | // created ref must be, hold every note of the old tree at the same path |
| 52 | // with the same bytes, and hold at least one note more. A note is a plain |
| 53 | // file, so the same bytes at the same path are the same mode and blob. |
| 54 | // Which check a note is about is read from its statement, never from its |
| 55 | // path (integrator/evidence.go, status/coverage.go), so a note added under |
| 56 | // any path is judged as what it says. |
| 57 | func (p policy) checkAttestationUpdate(ref, old, id string) error { |
| 58 | notes, err := p.attestationNotes(ref, id) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | held, err := p.repo.notes(old) |
| 63 | if err != nil { |
| 64 | return fmt.Errorf("the evidence it holds could not be read: %v", err) |
| 65 | } |
| 66 | for _, path := range sortedKeys(held) { |
| 67 | kept, ok := notes[path] |
| 68 | switch { |
| 69 | case !ok: |
| 70 | return fmt.Errorf("it drops the note at %s, and published evidence is never removed", path) |
| 71 | case !bytes.Equal(kept, held[path]): |
| 72 | return fmt.Errorf("it changes the note at %s, and published evidence is never changed", path) |
| 73 | } |
| 74 | } |
| 75 | if len(notes) == len(held) { |
| 76 | return errors.New("it adds no note to the ones the ref holds, and an update to an attestation ref adds at least one") |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // attestationNotes reads the tree id names as the attestation ref ref, and |
| 82 | // returns its notes when every one is what the ref's name says. |
| 83 | func (p policy) attestationNotes(ref, id string) (map[string][]byte, error) { |
| 84 | parts := attestationName.FindStringSubmatch(ref) |
| 85 | if parts == nil { |
| 86 | return nil, 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") |
| 87 | } |
| 88 | digest, keyHash := parts[1], parts[2] |
| 89 | if len(p.verifiers) == 0 { |
| 90 | return nil, errors.New("this host names no keys it accepts evidence from, so no attestation can be checked here") |
| 91 | } |
| 92 | |
| 93 | notes, err := p.repo.notes(id) |
| 94 | if err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | for _, path := range sortedKeys(notes) { |
| 98 | if err := p.checkNote(notes[path], digest, keyHash); err != nil { |
| 99 | return nil, fmt.Errorf("the note at %s %s", path, err) |
| 100 | } |
| 101 | } |
| 102 | return notes, nil |
| 103 | } |
| 104 | |
| 105 | func (p policy) checkNote(n []byte, digest, keyHash string) error { |
| 106 | text, checked, err := note.Open(n, p.verifiers) |
| 107 | if err != nil { |
| 108 | return fmt.Errorf("does not open: %v", err) |
| 109 | } |
| 110 | subject, err := note.StatementSubject(text) |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | if subject.Digest != digest { |
| 115 | return fmt.Errorf("is about the tree %s, not the one the ref is named for", subject.Digest) |
| 116 | } |
| 117 | for _, c := range checked { |
| 118 | if c.Verified && fmt.Sprintf("%08x", c.Hash) == keyHash { |
| 119 | return nil |
| 120 | } |
| 121 | } |
| 122 | return fmt.Errorf("carries no signature under the key hash %s by a key this host accepts", keyHash) |
| 123 | } |
| 124 | |
| 125 | // readVerifiers reads the verifier keys in one file. A file that does not |
| 126 | // exist names none: the compiled file is missing until the first |
| 127 | // compilation, and then no attestation is accepted, which is the |
| 128 | // direction a missing key has to default in. A line that is not a |
| 129 | // verifier key fails the read. |
| 130 | func readVerifiers(path string) ([]note.VerifierKey, error) { |
| 131 | data, err := os.ReadFile(path) |
| 132 | if errors.Is(err, fs.ErrNotExist) { |
| 133 | return nil, nil |
| 134 | } |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | keys, err := note.ParseVerifierKeys(data) |
| 139 | if err != nil { |
| 140 | return nil, fmt.Errorf("%s: %w", path, err) |
| 141 | } |
| 142 | return keys, nil |
| 143 | } |