| 1 |
package sshutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
|
| 6 |
"crypto/dsa" |
| 7 |
"crypto/ecdsa" |
| 8 |
"crypto/ed25519" |
| 9 |
"crypto/elliptic" |
| 10 |
"crypto/rsa" |
| 11 |
"encoding/binary" |
| 12 |
"fmt" |
| 13 |
"math/big" |
| 14 |
|
| 15 |
"github.com/pkg/errors" |
| 16 |
"golang.org/x/crypto/ssh" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
func NewCertSigner(cert *ssh.Certificate, priv interface{}) (ssh.Signer, error) { |
| 21 |
signer, err := ssh.NewSignerFromKey(priv) |
| 22 |
if err != nil { |
| 23 |
return nil, errors.Wrap(err, "error creating signer") |
| 24 |
} |
| 25 |
certSigner, err := ssh.NewCertSigner(cert, signer) |
| 26 |
if err != nil { |
| 27 |
return nil, errors.Wrap(err, "error creating signer") |
| 28 |
} |
| 29 |
return certSigner, nil |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
func ParseCertificate(in []byte) (*ssh.Certificate, error) { |
| 34 |
pub, err := ssh.ParsePublicKey(in) |
| 35 |
if err != nil { |
| 36 |
return nil, errors.Wrap(err, "error parsing certificate") |
| 37 |
} |
| 38 |
cert, ok := pub.(*ssh.Certificate) |
| 39 |
if !ok { |
| 40 |
return nil, errors.Errorf("error parsing certificate: %T is not a certificate", pub) |
| 41 |
} |
| 42 |
return cert, nil |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func PublicKey(key ssh.PublicKey) (crypto.PublicKey, error) { |
| 47 |
_, in, ok := parseString(key.Marshal()) |
| 48 |
if !ok { |
| 49 |
return nil, errors.New("public key is invalid") |
| 50 |
} |
| 51 |
|
| 52 |
switch key.Type() { |
| 53 |
case ssh.KeyAlgoRSA: |
| 54 |
return parseRSA(in) |
| 55 |
case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521, ssh.KeyAlgoSKECDSA256: |
| 56 |
return parseECDSA(in) |
| 57 |
case ssh.KeyAlgoED25519, ssh.KeyAlgoSKED25519: |
| 58 |
return parseED25519(in) |
| 59 |
case ssh.KeyAlgoDSA: |
| 60 |
return parseDSA(in) |
| 61 |
default: |
| 62 |
return nil, errors.Errorf("public key %s is not supported", key.Type()) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func Fingerprint(in []byte) (string, error) { |
| 69 |
key, comment, _, _, err := ssh.ParseAuthorizedKey(in) |
| 70 |
if err != nil { |
| 71 |
return "", errors.Wrap(err, "error parsing public key") |
| 72 |
} |
| 73 |
if comment == "" { |
| 74 |
comment = "no comment" |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
typ, size, err := publicKeyTypeAndSize(key) |
| 79 |
if err != nil { |
| 80 |
return "", errors.Wrap(err, "error determining key type and size") |
| 81 |
} |
| 82 |
|
| 83 |
return fmt.Sprintf("%d %s %s (%s)", size, ssh.FingerprintSHA256(key), comment, typ), nil |
| 84 |
} |
| 85 |
|
| 86 |
func publicKeyTypeAndSize(key ssh.PublicKey) (string, int, error) { |
| 87 |
var isCert bool |
| 88 |
if cert, ok := key.(*ssh.Certificate); ok { |
| 89 |
key = cert.Key |
| 90 |
isCert = true |
| 91 |
} |
| 92 |
|
| 93 |
var typ string |
| 94 |
var size int |
| 95 |
switch key.Type() { |
| 96 |
case ssh.KeyAlgoECDSA256: |
| 97 |
typ, size = "ECDSA", 256 |
| 98 |
case ssh.KeyAlgoECDSA384: |
| 99 |
typ, size = "ECDSA", 384 |
| 100 |
case ssh.KeyAlgoECDSA521: |
| 101 |
typ, size = "ECDSA", 521 |
| 102 |
case ssh.KeyAlgoSKECDSA256: |
| 103 |
typ, size = "SK-ECDSA", 256 |
| 104 |
case ssh.KeyAlgoED25519: |
| 105 |
typ, size = "ED25519", 256 |
| 106 |
case ssh.KeyAlgoSKED25519: |
| 107 |
typ, size = "SK-ED25519", 256 |
| 108 |
case ssh.KeyAlgoRSA: |
| 109 |
typ = "RSA" |
| 110 |
_, in, ok := parseString(key.Marshal()) |
| 111 |
if !ok { |
| 112 |
return "", 0, errors.New("public key is invalid") |
| 113 |
} |
| 114 |
k, err := parseRSA(in) |
| 115 |
if err != nil { |
| 116 |
return "", 0, err |
| 117 |
} |
| 118 |
size = 8 * k.Size() |
| 119 |
case ssh.KeyAlgoDSA: |
| 120 |
typ = "DSA" |
| 121 |
_, in, ok := parseString(key.Marshal()) |
| 122 |
if !ok { |
| 123 |
return "", 0, errors.New("public key is invalid") |
| 124 |
} |
| 125 |
k, err := parseDSA(in) |
| 126 |
if err != nil { |
| 127 |
return "", 0, err |
| 128 |
} |
| 129 |
size = k.Parameters.P.BitLen() |
| 130 |
default: |
| 131 |
return "", 0, errors.Errorf("public key %s is not supported", key.Type()) |
| 132 |
} |
| 133 |
|
| 134 |
if isCert { |
| 135 |
typ = typ + "-CERT" |
| 136 |
} |
| 137 |
|
| 138 |
return typ, size, nil |
| 139 |
} |
| 140 |
|
| 141 |
func parseString(in []byte) (out, rest []byte, ok bool) { |
| 142 |
if len(in) < 4 { |
| 143 |
return |
| 144 |
} |
| 145 |
length := binary.BigEndian.Uint32(in) |
| 146 |
in = in[4:] |
| 147 |
if uint32(len(in)) < length { |
| 148 |
return |
| 149 |
} |
| 150 |
out = in[:length] |
| 151 |
rest = in[length:] |
| 152 |
ok = true |
| 153 |
return |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func parseDSA(in []byte) (*dsa.PublicKey, error) { |
| 158 |
var w struct { |
| 159 |
P, Q, G, Y *big.Int |
| 160 |
Rest []byte `ssh:"rest"` |
| 161 |
} |
| 162 |
if err := ssh.Unmarshal(in, &w); err != nil { |
| 163 |
return nil, errors.Wrap(err, "error unmarshalling public key") |
| 164 |
} |
| 165 |
|
| 166 |
param := dsa.Parameters{ |
| 167 |
P: w.P, |
| 168 |
Q: w.Q, |
| 169 |
G: w.G, |
| 170 |
} |
| 171 |
return &dsa.PublicKey{ |
| 172 |
Parameters: param, |
| 173 |
Y: w.Y, |
| 174 |
}, nil |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
func parseRSA(in []byte) (*rsa.PublicKey, error) { |
| 179 |
var w struct { |
| 180 |
E *big.Int |
| 181 |
N *big.Int |
| 182 |
Rest []byte `ssh:"rest"` |
| 183 |
} |
| 184 |
if err := ssh.Unmarshal(in, &w); err != nil { |
| 185 |
return nil, errors.Wrap(err, "error unmarshalling public key") |
| 186 |
} |
| 187 |
if w.E.BitLen() > 24 { |
| 188 |
return nil, errors.New("invalid public key: exponent too large") |
| 189 |
} |
| 190 |
e := w.E.Int64() |
| 191 |
if e < 3 || e&1 == 0 { |
| 192 |
return nil, errors.New("invalid public key: incorrect exponent") |
| 193 |
} |
| 194 |
|
| 195 |
var key rsa.PublicKey |
| 196 |
key.E = int(e) |
| 197 |
key.N = w.N |
| 198 |
return &key, nil |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
func parseECDSA(in []byte) (*ecdsa.PublicKey, error) { |
| 203 |
var w struct { |
| 204 |
Curve string |
| 205 |
KeyBytes []byte |
| 206 |
Rest []byte `ssh:"rest"` |
| 207 |
} |
| 208 |
|
| 209 |
if err := ssh.Unmarshal(in, &w); err != nil { |
| 210 |
return nil, errors.Wrap(err, "error unmarshalling public key") |
| 211 |
} |
| 212 |
|
| 213 |
key := new(ecdsa.PublicKey) |
| 214 |
|
| 215 |
switch w.Curve { |
| 216 |
case "nistp256": |
| 217 |
key.Curve = elliptic.P256() |
| 218 |
case "nistp384": |
| 219 |
key.Curve = elliptic.P384() |
| 220 |
case "nistp521": |
| 221 |
key.Curve = elliptic.P521() |
| 222 |
default: |
| 223 |
return nil, errors.Errorf("unsupported curve %s", w.Curve) |
| 224 |
} |
| 225 |
|
| 226 |
key.X, key.Y = elliptic.Unmarshal(key.Curve, w.KeyBytes) |
| 227 |
if key.X == nil || key.Y == nil { |
| 228 |
return nil, errors.New("invalid curve point") |
| 229 |
} |
| 230 |
|
| 231 |
return key, nil |
| 232 |
} |
| 233 |
|
| 234 |
func parseED25519(in []byte) (ed25519.PublicKey, error) { |
| 235 |
var w struct { |
| 236 |
KeyBytes []byte |
| 237 |
Rest []byte `ssh:"rest"` |
| 238 |
} |
| 239 |
|
| 240 |
if err := ssh.Unmarshal(in, &w); err != nil { |
| 241 |
return nil, errors.Wrap(err, "error unmarshalling public key") |
| 242 |
} |
| 243 |
|
| 244 |
return ed25519.PublicKey(w.KeyBytes), nil |
| 245 |
} |