-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.go
112 lines (94 loc) · 2.09 KB
/
key.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/md5"
"errors"
"fmt"
"math/big"
"strings"
"golang.org/x/crypto/ssh"
)
type publicKey struct {
key ssh.PublicKey
blacklisted bool
}
func (p *publicKey) BitLen() (int, error) {
var (
length int
err error
)
switch p.key.Type() {
case ssh.KeyAlgoRSA:
length, err = rsaKeyLength(p.key)
case ssh.KeyAlgoDSA:
length, err = dsaKeyLength(p.key)
case ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521:
length, err = ecdsaKeyLength(p.key)
default:
err = errors.New("Key type not supported: " + p.key.Type())
}
return length, err
}
func (p *publicKey) Fingerprint() string {
return md5HexString(md5.Sum(p.key.Marshal()))
}
func rsaKeyLength(key ssh.PublicKey) (int, error) {
var w struct {
Name string
E *big.Int
N *big.Int
Rest []byte `ssh:"rest"`
}
err := ssh.Unmarshal(key.Marshal(), &w)
if err != nil {
return 0, err
}
return w.N.BitLen(), nil
}
func dsaKeyLength(key ssh.PublicKey) (int, error) {
var w struct {
Name string
P, Q, G, Y *big.Int
Rest []byte `ssh:"rest"`
}
err := ssh.Unmarshal(key.Marshal(), &w)
if err != nil {
return 0, err
}
return w.P.BitLen(), nil
}
func ecdsaKeyLength(key ssh.PublicKey) (int, error) {
var w struct {
Name string
Curve string
KeyBytes []byte
Rest []byte `ssh:"rest"`
}
err := ssh.Unmarshal(key.Marshal(), &w)
if err != nil {
return 0, err
}
k := new(ecdsa.PublicKey)
switch w.Curve {
case "nistp256":
k.Curve = elliptic.P256()
case "nistp384":
k.Curve = elliptic.P384()
case "nistp521":
k.Curve = elliptic.P521()
default:
return 0, fmt.Errorf("ECSDA curve not supported: %q", w.Curve)
}
k.X, k.Y = elliptic.Unmarshal(k.Curve, w.KeyBytes)
if k.X == nil || k.Y == nil {
return 0, fmt.Errorf("ECDSA X or Y points were nil: %q, %q", k.X, k.Y)
}
return k.Params().BitSize, nil
}
// md5HexString returns a formatted string representing the given md5 sum in hex
func md5HexString(md5 [16]byte) (s string) {
s = fmt.Sprintf("% x", md5)
s = strings.Replace(s, " ", ":", -1)
return s
}