-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsshkeys.go
274 lines (244 loc) · 6.21 KB
/
sshkeys.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package sshkeys
import (
"context"
"crypto/md5" //nolint: gosec // allow weak cryptographic primitive
"crypto/sha1" //nolint: gosec // allow weak cryptographic primitive
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/google/uuid"
"golang.org/x/crypto/ssh"
)
// DefaultKeyAlgorithms returns the default ssh key algorithms.
func DefaultKeyAlgorithms() []string {
return []string{
ssh.KeyAlgoRSA,
ssh.KeyAlgoDSA,
ssh.KeyAlgoECDSA256,
ssh.KeyAlgoSKECDSA256,
ssh.KeyAlgoECDSA384,
ssh.KeyAlgoECDSA521,
ssh.KeyAlgoED25519,
ssh.KeyAlgoSKED25519,
ssh.KeyAlgoRSASHA256,
ssh.KeyAlgoRSASHA512,
ssh.CertAlgoRSAv01,
ssh.CertAlgoDSAv01,
ssh.CertAlgoECDSA256v01,
ssh.CertAlgoECDSA384v01,
ssh.CertAlgoECDSA521v01,
ssh.CertAlgoSKECDSA256v01,
ssh.CertAlgoED25519v01,
ssh.CertAlgoSKED25519v01,
ssh.CertAlgoRSASHA256v01,
ssh.CertAlgoRSASHA512v01,
}
}
// GetKeys gets the public keys for a host.
// Specify the amount of concurrentWorkers and the algorithms that should be used to fetch the keys.
// If unsure use DefaultKeyAlgorithms.
func GetKeys(
ctx context.Context,
host string,
concurrentWorkers int,
timeout time.Duration,
algorithms ...string,
) (map[string]ssh.PublicKey, error) {
if len(algorithms) == 0 {
algorithms = DefaultKeyAlgorithms()
}
if concurrentWorkers < 0 {
concurrentWorkers = 1
}
workerCtx, cancel := context.WithCancel(ctx)
defer cancel()
algoChan := make(chan string, len(algorithms))
for _, algo := range algorithms {
algoChan <- algo
}
resultChan := make(chan workerResult, len(algorithms))
for i := 0; i < concurrentWorkers; i++ {
go worker(workerCtx, host, timeout, algoChan, resultChan)
}
keys := make(map[string]ssh.PublicKey)
for range algorithms {
result := <-resultChan
if result.err != nil {
return nil, result.err
}
if result.key != nil {
keys[result.algo] = result.key
}
}
return keys, nil
}
type workerResult struct {
algo string
key ssh.PublicKey
err error
}
func worker(ctx context.Context, host string, timeout time.Duration, algoChan chan string, resultChan chan workerResult) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
for {
select {
case <-ctx.Done():
return
case algo := <-algoChan:
key, err := getPublicKey(ctx, host, algo)
resultChan <- workerResult{algo, key, err}
}
}
}
func getPublicKey(ctx context.Context, host, algo string) (key ssh.PublicKey, err error) {
var d net.Dialer
conn, err := d.DialContext(ctx, "tcp", host)
if err != nil {
return nil, err
}
defer conn.Close()
id := uuid.NewString()
config := ssh.ClientConfig{
Auth: nil,
HostKeyAlgorithms: []string{algo},
HostKeyCallback: hostKeyCallback(id, &key),
}
ch := make(chan error)
go func() {
sshconn, _, _, err := ssh.NewClientConn(conn, host, &config)
if err != nil {
if strings.Contains(err.Error(), "no common algorithm for host key") {
ch <- nil
return
}
if strings.Contains(err.Error(), "got hostkey for "+id) {
ch <- nil
return
}
ch <- err
return
}
_ = sshconn.Close()
ch <- errors.New("an session was established without exchanging keys")
}()
select {
case <-ctx.Done():
return nil, ctx.Err()
case err := <-ch:
return key, err
}
}
type hostKeyCallbackError struct {
id string
}
func (a *hostKeyCallbackError) Error() string {
return "got hostkey for " + a.id
}
func hostKeyCallback(id string, key *ssh.PublicKey) func(string, net.Addr, ssh.PublicKey) error { //nolint: gocritic,lll // allow passing key via ref
return func(_ string, _ net.Addr, k ssh.PublicKey) error {
*key = k
return &hostKeyCallbackError{
id: id,
}
}
}
// GetVersion returns the ssh version of the host.
func GetVersion(ctx context.Context, host string) (string, error) {
var d net.Dialer
conn, err := d.DialContext(ctx, "tcp", host)
if err != nil {
return "", err
}
defer conn.Close()
type result struct {
version string
err error
}
const maxSize = 255
const eoh = 32
ch := make(chan result)
go func() {
var n int
bytes := make([]byte, maxSize)
n, err = conn.Read(bytes)
if err != nil {
ch <- result{
version: "",
err: err,
}
return
}
for i := 0; i < n; i++ {
if bytes[i] < eoh {
ch <- result{
version: string(bytes[:i]),
err: nil,
}
return
}
}
ch <- result{
version: "unknown",
err: nil,
}
}()
select {
case <-ctx.Done():
return "", ctx.Err()
case r := <-ch:
return r.version, r.err
}
}
// SumToHexString formats a sum in a aa:bb:cc:dd:ee:ff:... pattern.
func SumToHexString(sum []byte) string {
var sb strings.Builder
for i := 0; i < len(sum); i++ {
fmt.Fprintf(&sb, "%02x", sum[i])
if i < len(sum)-1 {
sb.WriteRune(':')
}
}
return sb.String()
}
type Encoding uint8
const (
HexEncoding Encoding = iota
Base32Encoding
Base64Encoding
)
func encodeFingerprint(encoding Encoding, sum []byte) (string, error) {
switch encoding {
case HexEncoding:
return SumToHexString(sum), nil
case Base32Encoding:
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(sum), nil
case Base64Encoding:
return base64.RawStdEncoding.EncodeToString(sum), nil
default:
return "", errors.New("unknown encoding")
}
}
// FingerprintMD5 creates the md5 fingerprint of the provided public key.
func FingerprintMD5(encoding Encoding, key ssh.PublicKey) (string, error) {
sum := md5.Sum(key.Marshal()) //nolint: gosec // allow weak cryptographic primitive
return encodeFingerprint(encoding, sum[:])
}
// FingerprintSHA1 creates the sha1 fingerprint of the provided public key.
func FingerprintSHA1(encoding Encoding, key ssh.PublicKey) (string, error) {
sum := sha1.Sum(key.Marshal()) //nolint: gosec // allow weak cryptographic primitive
return encodeFingerprint(encoding, sum[:])
}
// FingerprintSHA256 creates the sha256 fingerprint of the provided public key.
func FingerprintSHA256(encoding Encoding, key ssh.PublicKey) (string, error) {
sum := sha256.Sum256(key.Marshal())
return encodeFingerprint(encoding, sum[:])
}
// AuthorizedKey creates the authorized_key of the provided public key.
func AuthorizedKey(key ssh.PublicKey) (string, error) {
return strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))), nil
}