| 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 | package skforge |
| 20 | |
| 21 | import ( |
| 22 | "crypto/ecdsa" |
| 23 | "crypto/ed25519" |
| 24 | "crypto/elliptic" |
| 25 | "crypto/rand" |
| 26 | "crypto/sha256" |
| 27 | "crypto/sha512" |
| 28 | "encoding/base64" |
| 29 | "encoding/binary" |
| 30 | "strings" |
| 31 | ) |
| 32 | |
| 33 | // Counter is the signature counter every forged signature carries. |
| 34 | const Counter = 0x12345678 |
| 35 | |
| 36 | // Key is a seeded key that signs in software. |
| 37 | type Key struct { |
| 38 | Type string |
| 39 | application string |
| 40 | ed ed25519.PrivateKey |
| 41 | ec *ecdsa.PrivateKey |
| 42 | } |
| 43 | |
| 44 | // Ed25519SK is a sk-ssh-ed25519@openssh.com key derived from seed. |
| 45 | func Ed25519SK(seed string) Key { |
| 46 | return Key{Type: "sk-ssh-ed25519@openssh.com", application: "ssh:", ed: edKey(seed)} |
| 47 | } |
| 48 | |
| 49 | // ECDSASK is a sk-ecdsa-sha2-nistp256@openssh.com key derived from seed. |
| 50 | func ECDSASK(seed string) Key { |
| 51 | sum := sha256.Sum256([]byte(seed)) |
| 52 | priv, err := ecdsa.ParseRawPrivateKey(elliptic.P256(), sum[:]) |
| 53 | if err != nil { |
| 54 | panic(err) // only if the seed's hash is not below the curve order |
| 55 | } |
| 56 | return Key{Type: "sk-ecdsa-sha2-nistp256@openssh.com", application: "ssh:", ec: priv} |
| 57 | } |
| 58 | |
| 59 | // Ed25519 is a plain ssh-ed25519 software key derived from seed. |
| 60 | func Ed25519(seed string) Key { |
| 61 | return Key{Type: "ssh-ed25519", ed: edKey(seed)} |
| 62 | } |
| 63 | |
| 64 | func edKey(seed string) ed25519.PrivateKey { |
| 65 | sum := sha256.Sum256([]byte(seed)) |
| 66 | return ed25519.NewKeyFromSeed(sum[:]) |
| 67 | } |
| 68 | |
| 69 | // Blob is the public key's wire encoding. |
| 70 | func (k Key) Blob() []byte { |
| 71 | b := str(nil, k.Type) |
| 72 | if k.ec != nil { |
| 73 | point, _ := k.ec.PublicKey.Bytes() |
| 74 | b = str(b, "nistp256") |
| 75 | b = bytesStr(b, point) |
| 76 | } else { |
| 77 | b = bytesStr(b, k.ed.Public().(ed25519.PublicKey)) |
| 78 | } |
| 79 | if k.application != "" { |
| 80 | b = str(b, k.application) |
| 81 | } |
| 82 | return b |
| 83 | } |
| 84 | |
| 85 | // PublicLine is the key as an authorized_keys or allowed-signers line |
| 86 | // writes it: the type and the base64 blob. |
| 87 | func (k Key) PublicLine() string { |
| 88 | return k.Type + " " + base64.StdEncoding.EncodeToString(k.Blob()) |
| 89 | } |
| 90 | |
| 91 | // Sign returns an armored SSHSIG over message in namespace. For a security |
| 92 | // key, flags is the authenticator flags byte: 0x01 is a touch, 0x05 a |
| 93 | // touch and a PIN, and 0x00 a signature nobody was present for. A software |
| 94 | // key ignores it. |
| 95 | func (k Key) Sign(message []byte, namespace string, flags byte) []byte { |
| 96 | sum := sha512.Sum512(message) |
| 97 | data := []byte("SSHSIG") |
| 98 | data = str(data, namespace) |
| 99 | data = str(data, "") |
| 100 | data = str(data, "sha512") |
| 101 | data = bytesStr(data, sum[:]) |
| 102 | |
| 103 | signed := data |
| 104 | if k.application != "" { |
| 105 | app := sha256.Sum256([]byte(k.application)) |
| 106 | msg := sha256.Sum256(data) |
| 107 | signed = append(app[:], flags) |
| 108 | signed = binary.BigEndian.AppendUint32(signed, Counter) |
| 109 | signed = append(signed, msg[:]...) |
| 110 | } |
| 111 | |
| 112 | sig := str(nil, k.Type) |
| 113 | if k.ec != nil { |
| 114 | h := sha256.Sum256(signed) |
| 115 | r, s := mustSignECDSA(k.ec, h[:]) |
| 116 | sig = bytesStr(sig, bytesStr(bytesStr(nil, r), s)) |
| 117 | } else { |
| 118 | sig = bytesStr(sig, ed25519.Sign(k.ed, signed)) |
| 119 | } |
| 120 | if k.application != "" { |
| 121 | sig = append(sig, flags) |
| 122 | sig = binary.BigEndian.AppendUint32(sig, Counter) |
| 123 | } |
| 124 | |
| 125 | blob := []byte("SSHSIG") |
| 126 | blob = binary.BigEndian.AppendUint32(blob, 1) |
| 127 | blob = bytesStr(blob, k.Blob()) |
| 128 | blob = str(blob, namespace) |
| 129 | blob = str(blob, "") |
| 130 | blob = str(blob, "sha512") |
| 131 | blob = bytesStr(blob, sig) |
| 132 | return Armor(blob) |
| 133 | } |
| 134 | |
| 135 | // Armor wraps a raw SSHSIG blob the way ssh-keygen does. |
| 136 | func Armor(blob []byte) []byte { |
| 137 | enc := base64.StdEncoding.EncodeToString(blob) |
| 138 | var b strings.Builder |
| 139 | b.WriteString("-----BEGIN SSH SIGNATURE-----\n") |
| 140 | for len(enc) > 70 { |
| 141 | b.WriteString(enc[:70] + "\n") |
| 142 | enc = enc[70:] |
| 143 | } |
| 144 | b.WriteString(enc + "\n-----END SSH SIGNATURE-----\n") |
| 145 | return []byte(b.String()) |
| 146 | } |
| 147 | |
| 148 | // mustSignECDSA signs and returns r and s as SSH mpints' contents. |
| 149 | func mustSignECDSA(priv *ecdsa.PrivateKey, digest []byte) (r, s []byte) { |
| 150 | ri, si, err := ecdsa.Sign(rand.Reader, priv, digest) |
| 151 | if err != nil { |
| 152 | panic(err) |
| 153 | } |
| 154 | return mpint(ri.Bytes()), mpint(si.Bytes()) |
| 155 | } |
| 156 | |
| 157 | // mpint prefixes a zero byte where the high bit would read as negative. |
| 158 | func mpint(b []byte) []byte { |
| 159 | if len(b) > 0 && b[0]&0x80 != 0 { |
| 160 | return append([]byte{0}, b...) |
| 161 | } |
| 162 | return b |
| 163 | } |
| 164 | |
| 165 | func str(dst []byte, s string) []byte { return bytesStr(dst, []byte(s)) } |
| 166 | |
| 167 | func bytesStr(dst, b []byte) []byte { |
| 168 | dst = binary.BigEndian.AppendUint32(dst, uint32(len(b))) |
| 169 | return append(dst, b...) |
| 170 | } |