| 1 | package approval |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/ed25519" |
| 6 | "encoding/base64" |
| 7 | "encoding/binary" |
| 8 | "encoding/pem" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | |
| 12 | "the-valley/note" |
| 13 | ) |
| 14 | |
| 15 | // A Signer makes the raw Ed25519 signature over a statement's text with |
| 16 | // one key: a software key (Key) or a YubiKey's PIV slot (PIVSession). |
| 17 | type Signer interface { |
| 18 | Public() ed25519.PublicKey |
| 19 | Sign(text []byte) ([]byte, error) |
| 20 | } |
| 21 | |
| 22 | // Sign signs a statement as its principal and seals it in a note: the |
| 23 | // complete statement text, a blank line, and one signature line named by |
| 24 | // the principal. The note is opened under the signer's own key before it |
| 25 | // is returned, so a signer that signed something else, or with another |
| 26 | // key, returns an error and no note. |
| 27 | func Sign(s Statement, signer Signer) ([]byte, error) { |
| 28 | text, err := s.Encode() |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | pub := signer.Public() |
| 33 | if len(pub) != ed25519.PublicKeySize { |
| 34 | return nil, errors.New("the signer holds no ed25519 public key") |
| 35 | } |
| 36 | sig, err := signer.Sign(text) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | if len(sig) != ed25519.SignatureSize { |
| 41 | return nil, fmt.Errorf("the signer returned %d bytes, not an ed25519 signature", len(sig)) |
| 42 | } |
| 43 | var hash [4]byte |
| 44 | binary.BigEndian.PutUint32(hash[:], note.KeyHash(s.Principal, pub)) |
| 45 | var b bytes.Buffer |
| 46 | b.Write(text) |
| 47 | b.WriteString("\n" + note.Mark + s.Principal + " ") |
| 48 | b.WriteString(base64.StdEncoding.EncodeToString(append(hash[:], sig...))) |
| 49 | b.WriteString("\n") |
| 50 | if _, _, err := note.Open(b.Bytes(), []note.VerifierKey{note.NewVerifierKey(s.Principal, pub)}); err != nil { |
| 51 | return nil, fmt.Errorf("the signature does not verify under the signer's own key: %w", err) |
| 52 | } |
| 53 | return b.Bytes(), nil |
| 54 | } |
| 55 | |
| 56 | // Verify opens an approval note under the approval keys a reader holds, |
| 57 | // and returns the statement it carries. The note carries one signature, |
| 58 | // by a key among those keys, over a statement in its written form that |
| 59 | // names the key's principal as its approver. |
| 60 | // |
| 61 | // Which keys a reader holds decides which approvals count: the keys of |
| 62 | // approval-signers, or of approval-signers-hardware when only hardware |
| 63 | // keys may approve. |
| 64 | func Verify(n []byte, authority []note.VerifierKey) (Statement, error) { |
| 65 | if len(n) > MaxNote { |
| 66 | return Statement{}, fmt.Errorf("the approval note is %d bytes; one holds at most %d", len(n), MaxNote) |
| 67 | } |
| 68 | _, sigs, err := note.Split(n) |
| 69 | if err != nil { |
| 70 | return Statement{}, err |
| 71 | } |
| 72 | if len(sigs) != 1 { |
| 73 | return Statement{}, fmt.Errorf("an approval note carries one signature, and this carries %d", len(sigs)) |
| 74 | } |
| 75 | text, checked, err := note.Open(n, authority) |
| 76 | if err != nil { |
| 77 | return Statement{}, err |
| 78 | } |
| 79 | s, err := Parse(text) |
| 80 | if err != nil { |
| 81 | return Statement{}, fmt.Errorf("the signed text %w", err) |
| 82 | } |
| 83 | if checked[0].Name != s.Principal { |
| 84 | return Statement{}, fmt.Errorf("the approval is signed with a key of %s, and names %s as its approver", checked[0].Name, s.Principal) |
| 85 | } |
| 86 | return s, nil |
| 87 | } |
| 88 | |
| 89 | // Key is a software approval key. |
| 90 | type Key struct { |
| 91 | priv ed25519.PrivateKey |
| 92 | } |
| 93 | |
| 94 | // NewKey is the software key with this private half. |
| 95 | func NewKey(priv ed25519.PrivateKey) Key { return Key{priv: priv} } |
| 96 | |
| 97 | func (k Key) Public() ed25519.PublicKey { return k.priv.Public().(ed25519.PublicKey) } |
| 98 | |
| 99 | func (k Key) Sign(text []byte) ([]byte, error) { return ed25519.Sign(k.priv, text), nil } |
| 100 | |
| 101 | // ParseKey reads the unencrypted OpenSSH Ed25519 private key that |
| 102 | // `ssh-keygen -t ed25519 -N ”` writes, as attest/sign.go reads a host's. |
| 103 | // A key behind a passphrase is refused. |
| 104 | func ParseKey(raw []byte) (Key, error) { |
| 105 | block, _ := pem.Decode(raw) |
| 106 | if block == nil || block.Type != "OPENSSH PRIVATE KEY" { |
| 107 | return Key{}, errors.New("not an openssh private key") |
| 108 | } |
| 109 | body := sshBytes{rest: block.Bytes} |
| 110 | magic := []byte("openssh-key-v1\x00") |
| 111 | if !bytes.HasPrefix(body.rest, magic) { |
| 112 | return Key{}, errors.New("not an openssh private key: the container is unrecognised") |
| 113 | } |
| 114 | body.rest = body.rest[len(magic):] |
| 115 | cipher, kdf := body.str(), body.str() |
| 116 | body.str() // the kdf options |
| 117 | count := body.u32() |
| 118 | body.str() // the public key, which the private section repeats |
| 119 | section := sshBytes{rest: body.str()} |
| 120 | if body.err != nil { |
| 121 | return Key{}, body.err |
| 122 | } |
| 123 | if string(cipher) != "none" || string(kdf) != "none" { |
| 124 | return Key{}, fmt.Errorf("the key is encrypted with %s; only an unencrypted key is read", cipher) |
| 125 | } |
| 126 | if count != 1 { |
| 127 | return Key{}, fmt.Errorf("the file holds %d keys, not one", count) |
| 128 | } |
| 129 | if a, b := section.u32(), section.u32(); a != b { |
| 130 | return Key{}, errors.New("the key is encrypted or damaged: its check words differ") |
| 131 | } |
| 132 | if kind := section.str(); string(kind) != "ssh-ed25519" { |
| 133 | return Key{}, fmt.Errorf("the key is %s, not ssh-ed25519", kind) |
| 134 | } |
| 135 | pub, priv := section.str(), section.str() |
| 136 | if section.err != nil { |
| 137 | return Key{}, section.err |
| 138 | } |
| 139 | if len(pub) != ed25519.PublicKeySize || len(priv) != ed25519.PrivateKeySize { |
| 140 | return Key{}, errors.New("the key is not the size an ed25519 key is") |
| 141 | } |
| 142 | key := ed25519.PrivateKey(priv) |
| 143 | if !bytes.Equal(key.Public().(ed25519.PublicKey), pub) { |
| 144 | return Key{}, errors.New("the key's private half does not produce its public half") |
| 145 | } |
| 146 | return Key{priv: key}, nil |
| 147 | } |
| 148 | |
| 149 | // sshBytes reads the ssh wire encoding, where a string is a four-byte |
| 150 | // big-endian length and that many bytes. The first error is kept and every |
| 151 | // later read is a no-op. |
| 152 | type sshBytes struct { |
| 153 | rest []byte |
| 154 | err error |
| 155 | } |
| 156 | |
| 157 | func (s *sshBytes) u32() uint32 { |
| 158 | if s.err != nil { |
| 159 | return 0 |
| 160 | } |
| 161 | if len(s.rest) < 4 { |
| 162 | s.err = errors.New("the key ends in the middle of a field") |
| 163 | return 0 |
| 164 | } |
| 165 | v := binary.BigEndian.Uint32(s.rest) |
| 166 | s.rest = s.rest[4:] |
| 167 | return v |
| 168 | } |
| 169 | |
| 170 | func (s *sshBytes) str() []byte { |
| 171 | n := s.u32() |
| 172 | if s.err != nil { |
| 173 | return nil |
| 174 | } |
| 175 | if uint64(n) > uint64(len(s.rest)) { |
| 176 | s.err = fmt.Errorf("the key claims a %d-byte field with %d bytes left", n, len(s.rest)) |
| 177 | return nil |
| 178 | } |
| 179 | v := s.rest[:n] |
| 180 | s.rest = s.rest[n:] |
| 181 | return v |
| 182 | } |