| 1 | package sigverify |
| 2 | |
| 3 | import ( |
| 4 | "crypto" |
| 5 | "crypto/ecdsa" |
| 6 | "crypto/ed25519" |
| 7 | "crypto/elliptic" |
| 8 | "crypto/rsa" |
| 9 | "crypto/sha256" |
| 10 | "crypto/sha512" |
| 11 | "encoding/base64" |
| 12 | "encoding/binary" |
| 13 | "fmt" |
| 14 | "hash" |
| 15 | ) |
| 16 | |
| 17 | // The OpenSSH key types this package reads. The two sk- types are FIDO |
| 18 | // security keys. The others are software keys, and they carry no |
| 19 | // authenticator flags at all. |
| 20 | const ( |
| 21 | TypeSKEd25519 = "sk-ssh-ed25519@openssh.com" |
| 22 | TypeSKECDSA = "sk-ecdsa-sha2-nistp256@openssh.com" |
| 23 | TypeEd25519 = "ssh-ed25519" |
| 24 | TypeECDSA256 = "ecdsa-sha2-nistp256" |
| 25 | TypeECDSA384 = "ecdsa-sha2-nistp384" |
| 26 | TypeECDSA521 = "ecdsa-sha2-nistp521" |
| 27 | TypeRSA = "ssh-rsa" |
| 28 | ) |
| 29 | |
| 30 | // minRSABits is the smallest RSA modulus accepted. OpenSSH accepts 1024 |
| 31 | // bits; this verifier holds RSA keys to 2048. |
| 32 | const minRSABits = 2048 |
| 33 | |
| 34 | // unsupportedKeyError is a key whose type this package does not verify, |
| 35 | // as opposed to a key whose encoding is broken. |
| 36 | type unsupportedKeyError struct{ reason string } |
| 37 | |
| 38 | func (e unsupportedKeyError) Error() string { return e.reason } |
| 39 | |
| 40 | // publicKey is one parsed public key. |
| 41 | type publicKey struct { |
| 42 | typ string |
| 43 | blob []byte // the wire encoding, which allowed-signers lines are compared by |
| 44 | |
| 45 | // application is the FIDO application a security key was enrolled |
| 46 | // under, "ssh:" by default. The authenticator hashes it into every |
| 47 | // signature, so verification needs it. |
| 48 | application string |
| 49 | |
| 50 | ed25519 ed25519.PublicKey |
| 51 | ecdsa *ecdsa.PublicKey |
| 52 | rsa *rsa.PublicKey |
| 53 | } |
| 54 | |
| 55 | func (k *publicKey) securityKey() bool { |
| 56 | return k.typ == TypeSKEd25519 || k.typ == TypeSKECDSA |
| 57 | } |
| 58 | |
| 59 | // fingerprint is the SHA-256 fingerprint `ssh-keygen -l` prints. |
| 60 | func (k *publicKey) fingerprint() string { |
| 61 | sum := sha256.Sum256(k.blob) |
| 62 | return "SHA256:" + base64.RawStdEncoding.EncodeToString(sum[:]) |
| 63 | } |
| 64 | |
| 65 | func parsePublicKey(blob []byte) (*publicKey, error) { |
| 66 | r := reader{rest: blob} |
| 67 | k := &publicKey{typ: string(r.str()), blob: blob} |
| 68 | if r.err != nil { |
| 69 | return nil, fmt.Errorf("the public key %w", r.err) |
| 70 | } |
| 71 | switch k.typ { |
| 72 | case TypeEd25519, TypeSKEd25519: |
| 73 | pub := r.str() |
| 74 | if k.typ == TypeSKEd25519 { |
| 75 | k.application = string(r.str()) |
| 76 | } |
| 77 | if err := r.end(); err != nil { |
| 78 | return nil, fmt.Errorf("the %s public key %w", k.typ, err) |
| 79 | } |
| 80 | if len(pub) != ed25519.PublicKeySize { |
| 81 | return nil, fmt.Errorf("the %s public key holds %d bytes, not %d", k.typ, len(pub), ed25519.PublicKeySize) |
| 82 | } |
| 83 | k.ed25519 = ed25519.PublicKey(pub) |
| 84 | case TypeECDSA256, TypeECDSA384, TypeECDSA521, TypeSKECDSA: |
| 85 | curveName, point := string(r.str()), r.str() |
| 86 | if k.typ == TypeSKECDSA { |
| 87 | k.application = string(r.str()) |
| 88 | } |
| 89 | if err := r.end(); err != nil { |
| 90 | return nil, fmt.Errorf("the %s public key %w", k.typ, err) |
| 91 | } |
| 92 | curve, want := curveFor(k.typ) |
| 93 | if curveName != want { |
| 94 | return nil, fmt.Errorf("the %s public key names curve %q", k.typ, curveName) |
| 95 | } |
| 96 | pub, err := ecdsa.ParseUncompressedPublicKey(curve, point) |
| 97 | if err != nil { |
| 98 | return nil, fmt.Errorf("the %s public key: %w", k.typ, err) |
| 99 | } |
| 100 | k.ecdsa = pub |
| 101 | case TypeRSA: |
| 102 | e, n := r.mpint(), r.mpint() |
| 103 | if err := r.end(); err != nil { |
| 104 | return nil, fmt.Errorf("the %s public key %w", k.typ, err) |
| 105 | } |
| 106 | if n.BitLen() < minRSABits { |
| 107 | return nil, unsupportedKeyError{fmt.Sprintf("the RSA key has %d bits, and at least %d are required", n.BitLen(), minRSABits)} |
| 108 | } |
| 109 | if !e.IsInt64() || e.Int64() < 3 || e.Int64() > 1<<31-1 || e.Bit(0) == 0 { |
| 110 | return nil, fmt.Errorf("the RSA public exponent %v is not usable", e) |
| 111 | } |
| 112 | k.rsa = &rsa.PublicKey{N: n, E: int(e.Int64())} |
| 113 | default: |
| 114 | return nil, unsupportedKeyError{fmt.Sprintf("keys of type %q are not supported", k.typ)} |
| 115 | } |
| 116 | return k, nil |
| 117 | } |
| 118 | |
| 119 | func curveFor(typ string) (elliptic.Curve, string) { |
| 120 | switch typ { |
| 121 | case TypeECDSA384: |
| 122 | return elliptic.P384(), "nistp384" |
| 123 | case TypeECDSA521: |
| 124 | return elliptic.P521(), "nistp521" |
| 125 | default: |
| 126 | return elliptic.P256(), "nistp256" |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // authenticatorData is what a security key's signature carries besides the |
| 131 | // signature itself. |
| 132 | type authenticatorData struct { |
| 133 | flags byte |
| 134 | counter uint32 |
| 135 | } |
| 136 | |
| 137 | // verify checks sig, a signature blob in the SSH wire encoding, over data. |
| 138 | // It returns an error if the blob is malformed, and ok false if it is well |
| 139 | // formed but does not verify. |
| 140 | // |
| 141 | // A security key does not sign data directly. The authenticator signs its |
| 142 | // own authenticator data, with the hash of data inside it: |
| 143 | // |
| 144 | // sha256(application) || flags || counter || sha256(data) |
| 145 | // |
| 146 | // The flags byte holds the user-presence and user-verification bits. They |
| 147 | // are inside the signed bytes, so nobody can set or clear them after the |
| 148 | // fact, and verify returns them for the caller to check. |
| 149 | func (k *publicKey) verify(data, sig []byte) (auth authenticatorData, ok bool, err error) { |
| 150 | r := reader{rest: sig} |
| 151 | sigType := string(r.str()) |
| 152 | if r.err != nil { |
| 153 | return auth, false, fmt.Errorf("the signature blob %w", r.err) |
| 154 | } |
| 155 | if k.typ == TypeRSA { |
| 156 | ok, err := verifyRSA(k.rsa, sigType, &r, data) |
| 157 | return auth, ok, err |
| 158 | } |
| 159 | if sigType != k.typ { |
| 160 | return auth, false, fmt.Errorf("a %s key cannot make a %q signature", k.typ, sigType) |
| 161 | } |
| 162 | |
| 163 | raw := r.str() |
| 164 | if k.securityKey() { |
| 165 | auth.flags, auth.counter = r.u8(), r.u32() |
| 166 | } |
| 167 | if err := r.end(); err != nil { |
| 168 | return auth, false, fmt.Errorf("the %s signature %w", k.typ, err) |
| 169 | } |
| 170 | |
| 171 | signed := data |
| 172 | if k.securityKey() { |
| 173 | app := sha256.Sum256([]byte(k.application)) |
| 174 | msg := sha256.Sum256(data) |
| 175 | signed = make([]byte, 0, len(app)+1+4+len(msg)) |
| 176 | signed = append(signed, app[:]...) |
| 177 | signed = append(signed, auth.flags) |
| 178 | signed = binary.BigEndian.AppendUint32(signed, auth.counter) |
| 179 | signed = append(signed, msg[:]...) |
| 180 | } |
| 181 | |
| 182 | if k.ed25519 != nil { |
| 183 | if len(raw) != ed25519.SignatureSize { |
| 184 | return auth, false, fmt.Errorf("the %s signature holds %d bytes, not %d", k.typ, len(raw), ed25519.SignatureSize) |
| 185 | } |
| 186 | return auth, ed25519.Verify(k.ed25519, signed, raw), nil |
| 187 | } |
| 188 | |
| 189 | // An ECDSA signature is the two integers r and s, inside the string. |
| 190 | rs := reader{rest: raw} |
| 191 | sr, ss := rs.mpint(), rs.mpint() |
| 192 | if err := rs.end(); err != nil { |
| 193 | return auth, false, fmt.Errorf("the %s signature %w", k.typ, err) |
| 194 | } |
| 195 | h := ecdsaHash(k.typ) |
| 196 | h.Write(signed) |
| 197 | return auth, ecdsa.Verify(k.ecdsa, h.Sum(nil), sr, ss), nil |
| 198 | } |
| 199 | |
| 200 | // ecdsaHash is the digest each ECDSA type signs with. A security key's |
| 201 | // authenticator data is always hashed with SHA-256. |
| 202 | func ecdsaHash(typ string) hash.Hash { |
| 203 | switch typ { |
| 204 | case TypeECDSA384: |
| 205 | return sha512.New384() |
| 206 | case TypeECDSA521: |
| 207 | return sha512.New() |
| 208 | default: |
| 209 | return sha256.New() |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // verifyRSA checks an RSA signature. ssh-keygen accepts only the SHA-2 |
| 214 | // signature types for SSHSIG, never SHA-1 "ssh-rsa", and so does this. |
| 215 | func verifyRSA(pub *rsa.PublicKey, sigType string, r *reader, data []byte) (bool, error) { |
| 216 | var h crypto.Hash |
| 217 | switch sigType { |
| 218 | case "rsa-sha2-256": |
| 219 | h = crypto.SHA256 |
| 220 | case "rsa-sha2-512": |
| 221 | h = crypto.SHA512 |
| 222 | default: |
| 223 | return false, fmt.Errorf("an RSA signature of type %q is not accepted", sigType) |
| 224 | } |
| 225 | raw := r.str() |
| 226 | if err := r.end(); err != nil { |
| 227 | return false, fmt.Errorf("the RSA signature %w", err) |
| 228 | } |
| 229 | // OpenSSH accepts a short signature and left-pads it. It never writes |
| 230 | // one, so this requires the full modulus length: one signature, one |
| 231 | // encoding. |
| 232 | if len(raw) != pub.Size() { |
| 233 | return false, fmt.Errorf("the RSA signature holds %d bytes, not the key's %d", len(raw), pub.Size()) |
| 234 | } |
| 235 | d := h.New() |
| 236 | d.Write(data) |
| 237 | return rsa.VerifyPKCS1v15(pub, h, d.Sum(nil), raw) == nil, nil |
| 238 | } |