| 1 | package main |
| 2 | |
| 3 | // The envelope: a signed note. |
| 4 | // |
| 5 | // A note is the statement text, then a blank line, then one or more |
| 6 | // signature lines. Each signature line is an em-dash, a space, the |
| 7 | // signer's name, a space, and base64 of a four-byte key hash followed by |
| 8 | // the raw Ed25519 signature over the text: |
| 9 | // |
| 10 | // the-valley/attestation/v1 |
| 11 | // subject.primary valley-tree-v1 |
| 12 | // … |
| 13 | // |
| 14 | // — valley-host.example.org/attestations WhFo+DGb1c/Fk6… |
| 15 | // — witness.example.org/attestations Ro+Yc3q9nSs8W… |
| 16 | // |
| 17 | // This is the format golang.org/x/mod/sumdb/note defines and transparency |
| 18 | // log checkpoints are published in. |
| 19 | // |
| 20 | // It is chosen for what it does to composition. Several parties attesting |
| 21 | // to one subject become sibling signature lines under one text, so a |
| 22 | // signature cannot be lifted onto a different statement: it sits under the |
| 23 | // text it covers, and moving it means moving the text with it. That |
| 24 | // property is structural here rather than a rule a verifier has to |
| 25 | // remember to apply. It is also the envelope a Phase 6 checkpoint arrives |
| 26 | // in, so the same envelope carries an attestation now and a log checkpoint |
| 27 | // later rather than changing at the boundary. |
| 28 | // |
| 29 | // A signature covers the text and nothing else — not the signer's name, |
| 30 | // not a namespace, and no git object (ida-51605e8). What separates an |
| 31 | // attestation from any other note a key signs is therefore the text's own |
| 32 | // first line, which is the statement type. |
| 33 | // |
| 34 | // # One reading of the envelope |
| 35 | // |
| 36 | // The envelope is read and written by the note module (../note), not by |
| 37 | // this program alone. attest signs with it and attest verify opens with |
| 38 | // it, and so does the pre-receive hook a valley host checks pushed |
| 39 | // attestations with (valleyhook/). One reading means a note the hook |
| 40 | // accepts into the create-only namespace is a note every verifier opens, |
| 41 | // and a note this program writes is one the hook accepts. |
| 42 | // |
| 43 | // golang.org/x/mod/sumdb/note is the reference implementation of the |
| 44 | // format, and the note module is that format written out again. The |
| 45 | // format is frozen — it is what sum.golang.org publishes and every |
| 46 | // checkpoint verifier reads — and the project holds that the size of a |
| 47 | // dependency graph is itself the exposure, so the valley's programs depend |
| 48 | // on nothing outside the standard library and their own repository. |
| 49 | // Interoperability is bought by pinning bytes rather than by sharing code: |
| 50 | // attest/conformance/ holds notes that must verify, and the unit tests |
| 51 | // re-derive the published key hash of sum.golang.org from its name and its |
| 52 | // public key. |
| 53 | |
| 54 | import ( |
| 55 | "bytes" |
| 56 | "crypto/ed25519" |
| 57 | "encoding/binary" |
| 58 | "encoding/pem" |
| 59 | "fmt" |
| 60 | "os" |
| 61 | |
| 62 | "the-valley/note" |
| 63 | ) |
| 64 | |
| 65 | // algEd25519 is the algorithm identifier the note format prefixes a public |
| 66 | // key with. Ed25519 is the only algorithm this program signs or verifies |
| 67 | // with. |
| 68 | const algEd25519 = note.AlgEd25519 |
| 69 | |
| 70 | // signer is a key this host signs with: a name, the private key, and the |
| 71 | // key hash the two together produce. |
| 72 | type signer struct { |
| 73 | name string |
| 74 | priv ed25519.PrivateKey |
| 75 | hash uint32 |
| 76 | } |
| 77 | |
| 78 | // verifierKey is a key a verifier is willing to accept a signature from. |
| 79 | // It is written as one line — the name, the key hash in hex, and base64 of |
| 80 | // the algorithm byte and the public key, joined by plus signs. |
| 81 | // |
| 82 | // This is the whole of what a verifier is given. There is no allowed |
| 83 | // signers file, no certificate and no directory lookup: `attest verify` |
| 84 | // takes a file of these lines, and a signature by a key not among them is |
| 85 | // a signature by nobody it accepts. |
| 86 | type verifierKey = note.VerifierKey |
| 87 | |
| 88 | func keyHash(name string, pub ed25519.PublicKey) uint32 { return note.KeyHash(name, pub) } |
| 89 | |
| 90 | func checkSignerName(name string) error { return note.CheckName(name) } |
| 91 | |
| 92 | func checkNoteText(text []byte) error { return note.CheckText(text) } |
| 93 | |
| 94 | func (s signer) verifierKey() verifierKey { |
| 95 | return note.NewVerifierKey(s.name, s.priv.Public().(ed25519.PublicKey)) |
| 96 | } |
| 97 | |
| 98 | func parseVerifierKey(line string) (verifierKey, error) { return note.ParseVerifierKey(line) } |
| 99 | |
| 100 | // readVerifierKeys reads a known-keys file: one verifier key per line, |
| 101 | // with blank lines and # comments ignored. |
| 102 | func readVerifierKeys(path string) ([]verifierKey, error) { |
| 103 | raw, err := os.ReadFile(path) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | keys, err := note.ParseVerifierKeys(raw) |
| 108 | if err != nil { |
| 109 | return nil, fmt.Errorf("%s: %w", path, err) |
| 110 | } |
| 111 | if len(keys) == 0 { |
| 112 | return nil, fmt.Errorf("%s lists no verifier keys", path) |
| 113 | } |
| 114 | return keys, nil |
| 115 | } |
| 116 | |
| 117 | // signNote writes a note: the text, a blank line, and one signature line. |
| 118 | func signNote(text []byte, s signer) ([]byte, error) { |
| 119 | return note.Sign(text, s.name, s.priv) |
| 120 | } |
| 121 | |
| 122 | // addSignature adds a signature line to a note that already carries one. |
| 123 | // This is how several parties attest to one subject: the second signer |
| 124 | // signs the same text, and its signature lands beside the first, under the |
| 125 | // text both of them cover. |
| 126 | func addSignature(n []byte, s signer) ([]byte, error) { |
| 127 | return note.AddSignature(n, s.name, s.priv) |
| 128 | } |
| 129 | |
| 130 | // noteSignature is one signature line as read back: who it says signed, |
| 131 | // under which key hash, and what a verifier made of it. |
| 132 | type noteSignature struct { |
| 133 | name string |
| 134 | hash uint32 |
| 135 | known bool |
| 136 | verified bool |
| 137 | } |
| 138 | |
| 139 | // openNote checks a note against the keys a verifier holds, and returns |
| 140 | // the text and what became of every signature line over it. The reading |
| 141 | // and the rules are the note module's: a signature by a key the verifier |
| 142 | // holds must check out, at least one must, and a line that is not a |
| 143 | // well-formed Ed25519 signature line refuses the note. |
| 144 | func openNote(n []byte, known []verifierKey) ([]byte, []noteSignature, error) { |
| 145 | text, checked, err := note.Open(n, known) |
| 146 | if err != nil { |
| 147 | return nil, nil, err |
| 148 | } |
| 149 | var found []noteSignature |
| 150 | for _, c := range checked { |
| 151 | found = append(found, noteSignature{name: c.Name, hash: c.Hash, known: c.Known, verified: c.Verified}) |
| 152 | } |
| 153 | return text, found, nil |
| 154 | } |
| 155 | |
| 156 | // loadSigner reads an unencrypted OpenSSH Ed25519 private key and pairs it |
| 157 | // with the name it signs under. /etc/ssh/ssh_host_ed25519_key is the |
| 158 | // natural production identity — the host already has it, and it already |
| 159 | // says which host this is — but the key is a parameter and provisioning |
| 160 | // one is deployment. |
| 161 | func loadSigner(path, name string) (signer, error) { |
| 162 | if path == "" { |
| 163 | return signer{}, fmt.Errorf("no signing key: pass --key. The host signs; an unsigned statement is not an attestation") |
| 164 | } |
| 165 | if err := checkSignerName(name); err != nil { |
| 166 | return signer{}, err |
| 167 | } |
| 168 | raw, err := os.ReadFile(path) |
| 169 | if err != nil { |
| 170 | return signer{}, fmt.Errorf("signing key: %w", err) |
| 171 | } |
| 172 | priv, err := parseOpenSSHEd25519(raw) |
| 173 | if err != nil { |
| 174 | return signer{}, fmt.Errorf("%s: %w", path, err) |
| 175 | } |
| 176 | pub := priv.Public().(ed25519.PublicKey) |
| 177 | return signer{name: name, priv: priv, hash: keyHash(name, pub)}, nil |
| 178 | } |
| 179 | |
| 180 | // parseOpenSSHEd25519 reads the private key format ssh-keygen writes. The |
| 181 | // container is |
| 182 | // |
| 183 | // "openssh-key-v1\0" |
| 184 | // string cipher, string kdf, string kdf options |
| 185 | // uint32 number of keys, string public key |
| 186 | // string the private section, which for one unencrypted key is |
| 187 | // uint32 check, uint32 check, string "ssh-ed25519", |
| 188 | // string public key, string seed and public key, string comment |
| 189 | // |
| 190 | // Only the unencrypted single-key case is read. A key behind a passphrase |
| 191 | // is refused rather than prompted for: attest runs unattended, and a tool |
| 192 | // that asked a host for a passphrase it does not have would fail later and |
| 193 | // less clearly. |
| 194 | func parseOpenSSHEd25519(raw []byte) (ed25519.PrivateKey, error) { |
| 195 | block, _ := pem.Decode(raw) |
| 196 | if block == nil || block.Type != "OPENSSH PRIVATE KEY" { |
| 197 | return nil, fmt.Errorf("not an openssh private key; attest signs with the ed25519 key `ssh-keygen -t ed25519` writes") |
| 198 | } |
| 199 | body := sshBytes{rest: block.Bytes} |
| 200 | magic := []byte("openssh-key-v1\x00") |
| 201 | if !bytes.HasPrefix(body.rest, magic) { |
| 202 | return nil, fmt.Errorf("not an openssh private key: the container is unrecognised") |
| 203 | } |
| 204 | body.rest = body.rest[len(magic):] |
| 205 | |
| 206 | cipher := body.str() |
| 207 | kdf := body.str() |
| 208 | body.str() // the kdf options |
| 209 | count := body.u32() |
| 210 | body.str() // the public key, which the private section repeats |
| 211 | section := sshBytes{rest: body.str()} |
| 212 | if body.err != nil { |
| 213 | return nil, body.err |
| 214 | } |
| 215 | if string(cipher) != "none" || string(kdf) != "none" { |
| 216 | return nil, fmt.Errorf("the key is encrypted with %s; attest runs unattended and cannot ask for a passphrase", cipher) |
| 217 | } |
| 218 | if count != 1 { |
| 219 | return nil, fmt.Errorf("the file holds %d keys; attest signs with one", count) |
| 220 | } |
| 221 | |
| 222 | if a, b := section.u32(), section.u32(); a != b { |
| 223 | return nil, fmt.Errorf("the key is encrypted or damaged: its check words differ") |
| 224 | } |
| 225 | if kind := section.str(); string(kind) != "ssh-ed25519" { |
| 226 | return nil, fmt.Errorf("the key is %s; attest signs with ed25519, which is what a note carries", kind) |
| 227 | } |
| 228 | pub := section.str() |
| 229 | priv := section.str() |
| 230 | if section.err != nil { |
| 231 | return nil, section.err |
| 232 | } |
| 233 | if len(pub) != ed25519.PublicKeySize || len(priv) != ed25519.PrivateKeySize { |
| 234 | return nil, fmt.Errorf("the key is not the size an ed25519 key is") |
| 235 | } |
| 236 | key := ed25519.PrivateKey(priv) |
| 237 | if !bytes.Equal(key.Public().(ed25519.PublicKey), pub) { |
| 238 | return nil, fmt.Errorf("the key's two halves do not agree: its private half does not produce its public one") |
| 239 | } |
| 240 | return key, nil |
| 241 | } |
| 242 | |
| 243 | // sshBytes reads the ssh wire encoding, where a string is a four-byte |
| 244 | // big-endian length and that many bytes. The first error is kept and every |
| 245 | // later read is a no-op, so a caller checks once at the end. |
| 246 | type sshBytes struct { |
| 247 | rest []byte |
| 248 | err error |
| 249 | } |
| 250 | |
| 251 | func (s *sshBytes) u32() uint32 { |
| 252 | if s.err != nil { |
| 253 | return 0 |
| 254 | } |
| 255 | if len(s.rest) < 4 { |
| 256 | s.err = fmt.Errorf("the key ends in the middle of a field") |
| 257 | return 0 |
| 258 | } |
| 259 | v := binary.BigEndian.Uint32(s.rest) |
| 260 | s.rest = s.rest[4:] |
| 261 | return v |
| 262 | } |
| 263 | |
| 264 | func (s *sshBytes) str() []byte { |
| 265 | n := s.u32() |
| 266 | if s.err != nil { |
| 267 | return nil |
| 268 | } |
| 269 | if uint64(n) > uint64(len(s.rest)) { |
| 270 | s.err = fmt.Errorf("the key claims a %d-byte field with %d bytes left", n, len(s.rest)) |
| 271 | return nil |
| 272 | } |
| 273 | v := s.rest[:n] |
| 274 | s.rest = s.rest[n:] |
| 275 | return v |
| 276 | } |