| 1 | // Package skforge makes security-key signatures in software, for tests. |
| 2 | // |
| 3 | // A security-key signature is supposed to mean that a person touched |
| 4 | // hardware, and no test machine has any. So the tests that pin what the |
| 5 | // verifier accepts and refuses need signatures nobody touched anything to |
| 6 | // make, with the authenticator flags chosen by the test. This package |
| 7 | // produces them as standard armored SSHSIG. It derives an ed25519 or P-256 |
| 8 | // key from a seed string and wraps it in the sk- key and signature |
| 9 | // envelopes. The cryptography is real: each signature is a valid signature |
| 10 | // over the bytes a real authenticator would sign. |
| 11 | // |
| 12 | // It is a forgery tool, so it lives under internal/ and only tests import |
| 13 | // it. Nothing built from this module for shipping links it. The keys exist |
| 14 | // in their seeds and nowhere else. |
| 15 | // |
| 16 | // It rebuilds the signed bytes independently of the verifier, so that a |
| 17 | // mistake in one does not hide the same mistake in the other. The tests |
| 18 | // also check real ssh-keygen signatures, which keeps both honest. |
| 19 | // |
| 20 | // Envelope takes a finished signature apart so a test can change one field |
| 21 | // after signing, as an attacker holding the signature could. |
| 22 | package skforge |
| 23 | |
| 24 | import ( |
| 25 | "crypto/ecdsa" |
| 26 | "crypto/ed25519" |
| 27 | "crypto/elliptic" |
| 28 | "crypto/rand" |
| 29 | "crypto/sha256" |
| 30 | "crypto/sha512" |
| 31 | "encoding/base64" |
| 32 | "encoding/binary" |
| 33 | "strings" |
| 34 | ) |
| 35 | |
| 36 | // Counter is the signature counter every forged signature carries. |
| 37 | const Counter = 0x12345678 |
| 38 | |
| 39 | // Key is a seeded key that signs in software. |
| 40 | type Key struct { |
| 41 | Type string |
| 42 | application string |
| 43 | ed ed25519.PrivateKey |
| 44 | ec *ecdsa.PrivateKey |
| 45 | } |
| 46 | |
| 47 | // Ed25519SK is a sk-ssh-ed25519@openssh.com key derived from seed. |
| 48 | func Ed25519SK(seed string) Key { |
| 49 | return Key{Type: "sk-ssh-ed25519@openssh.com", application: "ssh:", ed: edKey(seed)} |
| 50 | } |
| 51 | |
| 52 | // ECDSASK is a sk-ecdsa-sha2-nistp256@openssh.com key derived from seed. |
| 53 | func ECDSASK(seed string) Key { |
| 54 | sum := sha256.Sum256([]byte(seed)) |
| 55 | priv, err := ecdsa.ParseRawPrivateKey(elliptic.P256(), sum[:]) |
| 56 | if err != nil { |
| 57 | panic(err) // only if the seed's hash is not below the curve order |
| 58 | } |
| 59 | return Key{Type: "sk-ecdsa-sha2-nistp256@openssh.com", application: "ssh:", ec: priv} |
| 60 | } |
| 61 | |
| 62 | // WithApplication returns the same security key enrolled under another |
| 63 | // FIDO application. Its public key blob changes, and so do the bytes it |
| 64 | // signs. |
| 65 | func (k Key) WithApplication(application string) Key { |
| 66 | k.application = application |
| 67 | return k |
| 68 | } |
| 69 | |
| 70 | // Ed25519 is a plain ssh-ed25519 software key derived from seed. A TKey's |
| 71 | // signatures are exactly what this key makes: the TKey's guarantees live in |
| 72 | // the device and in which public key it has, not in the signature bytes. |
| 73 | func Ed25519(seed string) Key { |
| 74 | return Key{Type: "ssh-ed25519", ed: edKey(seed)} |
| 75 | } |
| 76 | |
| 77 | func edKey(seed string) ed25519.PrivateKey { |
| 78 | sum := sha256.Sum256([]byte(seed)) |
| 79 | return ed25519.NewKeyFromSeed(sum[:]) |
| 80 | } |
| 81 | |
| 82 | // Blob is the public key's wire encoding. |
| 83 | func (k Key) Blob() []byte { |
| 84 | b := str(nil, k.Type) |
| 85 | if k.ec != nil { |
| 86 | point, _ := k.ec.PublicKey.Bytes() |
| 87 | b = str(b, "nistp256") |
| 88 | b = bytesStr(b, point) |
| 89 | } else { |
| 90 | b = bytesStr(b, k.ed.Public().(ed25519.PublicKey)) |
| 91 | } |
| 92 | if k.application != "" { |
| 93 | b = str(b, k.application) |
| 94 | } |
| 95 | return b |
| 96 | } |
| 97 | |
| 98 | // PublicLine is the key as an authorized_keys or allowed-signers line |
| 99 | // writes it: the type and the base64 blob. |
| 100 | func (k Key) PublicLine() string { |
| 101 | return k.Type + " " + base64.StdEncoding.EncodeToString(k.Blob()) |
| 102 | } |
| 103 | |
| 104 | // Sign returns an armored SSHSIG over message in namespace. For a security |
| 105 | // key, flags is the authenticator flags byte: 0x01 is a touch, 0x05 a |
| 106 | // touch and a PIN, and 0x00 a signature nobody was present for. A software |
| 107 | // key ignores it. |
| 108 | func (k Key) Sign(message []byte, namespace string, flags byte) []byte { |
| 109 | sum := sha512.Sum512(message) |
| 110 | data := []byte("SSHSIG") |
| 111 | data = str(data, namespace) |
| 112 | data = str(data, "") |
| 113 | data = str(data, "sha512") |
| 114 | data = bytesStr(data, sum[:]) |
| 115 | |
| 116 | signed := data |
| 117 | if k.application != "" { |
| 118 | app := sha256.Sum256([]byte(k.application)) |
| 119 | msg := sha256.Sum256(data) |
| 120 | signed = append(app[:], flags) |
| 121 | signed = binary.BigEndian.AppendUint32(signed, Counter) |
| 122 | signed = append(signed, msg[:]...) |
| 123 | } |
| 124 | |
| 125 | sig := str(nil, k.Type) |
| 126 | if k.ec != nil { |
| 127 | h := sha256.Sum256(signed) |
| 128 | r, s := mustSignECDSA(k.ec, h[:]) |
| 129 | sig = bytesStr(sig, bytesStr(bytesStr(nil, r), s)) |
| 130 | } else { |
| 131 | sig = bytesStr(sig, ed25519.Sign(k.ed, signed)) |
| 132 | } |
| 133 | if k.application != "" { |
| 134 | sig = append(sig, flags) |
| 135 | sig = binary.BigEndian.AppendUint32(sig, Counter) |
| 136 | } |
| 137 | |
| 138 | blob := []byte("SSHSIG") |
| 139 | blob = binary.BigEndian.AppendUint32(blob, 1) |
| 140 | blob = bytesStr(blob, k.Blob()) |
| 141 | blob = str(blob, namespace) |
| 142 | blob = str(blob, "") |
| 143 | blob = str(blob, "sha512") |
| 144 | blob = bytesStr(blob, sig) |
| 145 | return Armor(blob) |
| 146 | } |
| 147 | |
| 148 | // Envelope is an SSHSIG blob split into its fields, so a test can change |
| 149 | // one and reassemble the rest exactly. |
| 150 | type Envelope struct { |
| 151 | Version uint32 |
| 152 | PublicKey []byte |
| 153 | Namespace string |
| 154 | Reserved []byte |
| 155 | HashAlg string |
| 156 | Signature []byte |
| 157 | Trailing []byte // bytes after the last field |
| 158 | } |
| 159 | |
| 160 | // ParseEnvelope splits an armored SSHSIG. It panics on input this package |
| 161 | // did not make. |
| 162 | func ParseEnvelope(armored []byte) Envelope { |
| 163 | text := strings.TrimSpace(string(armored)) |
| 164 | text = strings.TrimPrefix(text, "-----BEGIN SSH SIGNATURE-----") |
| 165 | text = strings.TrimSuffix(text, "-----END SSH SIGNATURE-----") |
| 166 | blob, err := base64.StdEncoding.DecodeString(strings.Join(strings.Fields(text), "")) |
| 167 | if err != nil || !strings.HasPrefix(string(blob), "SSHSIG") { |
| 168 | panic("skforge: not an armored SSHSIG") |
| 169 | } |
| 170 | r := Reader{Rest: blob[len("SSHSIG"):]} |
| 171 | e := Envelope{Version: r.U32()} |
| 172 | e.PublicKey = r.String() |
| 173 | e.Namespace = string(r.String()) |
| 174 | e.Reserved = r.String() |
| 175 | e.HashAlg = string(r.String()) |
| 176 | e.Signature = r.String() |
| 177 | e.Trailing = r.Rest |
| 178 | return e |
| 179 | } |
| 180 | |
| 181 | // Blob reassembles the raw SSHSIG. |
| 182 | func (e Envelope) Blob() []byte { |
| 183 | b := binary.BigEndian.AppendUint32([]byte("SSHSIG"), e.Version) |
| 184 | b = bytesStr(b, e.PublicKey) |
| 185 | b = str(b, e.Namespace) |
| 186 | b = bytesStr(b, e.Reserved) |
| 187 | b = str(b, e.HashAlg) |
| 188 | b = bytesStr(b, e.Signature) |
| 189 | return append(b, e.Trailing...) |
| 190 | } |
| 191 | |
| 192 | // Armored reassembles the armored SSHSIG. |
| 193 | func (e Envelope) Armored() []byte { return Armor(e.Blob()) } |
| 194 | |
| 195 | // Reader reads SSH wire strings, for taking a signature blob apart. It |
| 196 | // panics on a short read: tests only use it on blobs they made. |
| 197 | type Reader struct{ Rest []byte } |
| 198 | |
| 199 | func (r *Reader) U32() uint32 { |
| 200 | v := binary.BigEndian.Uint32(r.Rest) |
| 201 | r.Rest = r.Rest[4:] |
| 202 | return v |
| 203 | } |
| 204 | |
| 205 | func (r *Reader) String() []byte { |
| 206 | n := r.U32() |
| 207 | s := r.Rest[:n] |
| 208 | r.Rest = r.Rest[n:] |
| 209 | return s |
| 210 | } |
| 211 | |
| 212 | // String encodes b as an SSH wire string. |
| 213 | func String(b []byte) []byte { return bytesStr(nil, b) } |
| 214 | |
| 215 | // Armor wraps a raw SSHSIG blob the way ssh-keygen does. |
| 216 | func Armor(blob []byte) []byte { |
| 217 | enc := base64.StdEncoding.EncodeToString(blob) |
| 218 | var b strings.Builder |
| 219 | b.WriteString("-----BEGIN SSH SIGNATURE-----\n") |
| 220 | for len(enc) > 70 { |
| 221 | b.WriteString(enc[:70] + "\n") |
| 222 | enc = enc[70:] |
| 223 | } |
| 224 | b.WriteString(enc + "\n-----END SSH SIGNATURE-----\n") |
| 225 | return []byte(b.String()) |
| 226 | } |
| 227 | |
| 228 | // mustSignECDSA signs and returns r and s as SSH mpints' contents. |
| 229 | func mustSignECDSA(priv *ecdsa.PrivateKey, digest []byte) (r, s []byte) { |
| 230 | ri, si, err := ecdsa.Sign(rand.Reader, priv, digest) |
| 231 | if err != nil { |
| 232 | panic(err) |
| 233 | } |
| 234 | return mpint(ri.Bytes()), mpint(si.Bytes()) |
| 235 | } |
| 236 | |
| 237 | // mpint prefixes a zero byte where the high bit would read as negative. |
| 238 | func mpint(b []byte) []byte { |
| 239 | if len(b) > 0 && b[0]&0x80 != 0 { |
| 240 | return append([]byte{0}, b...) |
| 241 | } |
| 242 | return b |
| 243 | } |
| 244 | |
| 245 | func str(dst []byte, s string) []byte { return bytesStr(dst, []byte(s)) } |
| 246 | |
| 247 | func bytesStr(dst, b []byte) []byte { |
| 248 | dst = binary.BigEndian.AppendUint32(dst, uint32(len(b))) |
| 249 | return append(dst, b...) |
| 250 | } |