-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathopenssh.go
61 lines (49 loc) · 1.54 KB
/
openssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package keyidentifier
import (
"bytes"
"errors"
"fmt"
"strings"
"golang.org/x/crypto/ssh"
)
const opensshMagic = "openssh-key-v1\x00"
// ParseOpenSSHPrivateKey returns key information from an openssh
// private key. It is adapted from
// https://github.com/golang/crypto/blob/master/ssh/keys.go
func ParseOpenSSHPrivateKey(keyBytes []byte) (*KeyInfo, error) {
if !bytes.HasPrefix(keyBytes, []byte(opensshMagic)) {
return nil, errors.New("missing openssh magic")
}
remaining := keyBytes[len([]byte(opensshMagic)):]
var w struct {
CipherName string
KdfName string
KdfOpts string
NumKeys uint32
PubKey []byte
PrivKeyBlock []byte
}
if err := ssh.Unmarshal(remaining, &w); err != nil {
return nil, fmt.Errorf("ssh.Unmarshal: %w", err)
}
ki := &KeyInfo{
Format: "openssh-new",
Parser: "ParseOpenSSHPrivateKey",
}
if w.KdfName != "none" || w.CipherName != "none" {
ki.Encrypted = boolPtr(true)
ki.Encryption = fmt.Sprintf("%s-%s", w.CipherName, w.KdfName)
} else {
ki.Encrypted = boolPtr(false)
}
// If we can parse the public key. extract info
if pubKey, err := ssh.ParsePublicKey(w.PubKey); err == nil {
ki.Type = pubKey.Type()
ki.FingerprintSHA256 = strings.TrimPrefix(ssh.FingerprintSHA256(pubKey), "SHA256:")
ki.FingerprintMD5 = strings.TrimPrefix(ssh.FingerprintLegacyMD5(pubKey), "MD5:")
// We ought be able to get the size of the key, but I don't see
// how it's exposed. The ssh.PublicKey type is very bare.
// ki.Bits = len(pubKey.Parameters().Y.Bytes()) * 8
}
return ki, nil
}