forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
known_hosts.go
229 lines (197 loc) · 6.02 KB
/
known_hosts.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
package ssh
import (
"bufio"
"bytes"
"crypto/md5"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"path"
"strings"
gossh "github.com/coreos/fleet/third_party/code.google.com/p/gosshnew/ssh"
"github.com/coreos/fleet/pkg"
)
const (
DefaultKnownHostsFile = "~/.fleetctl/known_hosts"
warningRemoteHostChanged = `@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!`
)
var (
ErrUnparsableKey = errors.New("unparsable key bytes")
ErrUnsetTrustFunc = errors.New("unset trustHost function")
ErrUntrustHost = errors.New("unauthorized host")
ErrUnmatchKey = errors.New("host key mismatch")
ErrUnfoundHostAddr = errors.New("cannot find out host address")
)
// HostKeyChecker implements gossh.HostKeyChecker interface
// It is used for validation during the cryptographic handshake
type HostKeyChecker struct {
m HostKeyManager
trustHost func(addr, algo, fingerprint string) bool
// errLog is used to print out error/warning message
errLog *log.Logger
}
// NewHostKeyChecker returns new HostKeyChecker
// m manages existing host keys, trustHost tells whether or not to trust
// new host, errWriter indicates the place to write error msg
func NewHostKeyChecker(m HostKeyManager, trustHost func(addr, algo, fingerprint string) bool, errWriter io.Writer) *HostKeyChecker {
if errWriter == nil {
errWriter = os.Stderr
}
return &HostKeyChecker{m, trustHost, log.New(errWriter, "", 0)}
}
// SetTrustHost sets trustHost field
func (kc *HostKeyChecker) SetTrustHost(trustHost func(addr, algo, fingerprint string) bool) {
kc.trustHost = trustHost
}
// Check is called during the handshake to check server's
// public key for unexpected changes. The hostKey argument is
// in SSH wire format. It can be parsed using
// ssh.ParsePublicKey. The address before DNS resolution is
// passed in the addr argument, so the key can also be checked
// against the hostname.
func (kc *HostKeyChecker) Check(addr string, remote net.Addr, key gossh.PublicKey) error {
remoteAddr := remote.String()
algoStr := algoString(key.Type())
keyFingerprintStr := md5String(md5.Sum(key.Marshal()))
// get existing host keys
hostKeys, err := kc.m.GetHostKeys()
_, ok := err.(*os.PathError)
if err != nil && !ok {
kc.errLog.Println("Warning: read host file with", err)
}
// check existing host keys
hostKey, ok := hostKeys[remoteAddr]
if !ok {
if kc.trustHost == nil {
return ErrUnsetTrustFunc
}
if !kc.trustHost(remoteAddr, algoStr, keyFingerprintStr) {
kc.errLog.Println("Host key verification failed.")
return ErrUntrustHost
}
if err := kc.m.PutHostKey(remoteAddr, key); err != nil {
kc.errLog.Printf("Failed to add the host to the list of known hosts (%v).\n", kc.m)
return nil
}
kc.errLog.Printf("Warning: Permanently added '%v' (%v) to the list of known hosts.\n", remoteAddr, algoStr)
return nil
}
if hostKey.Type() != key.Type() || bytes.Compare(hostKey.Marshal(), key.Marshal()) != 0 {
kc.errLog.Printf(`%s
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the %v key sent by the remote host is
%v.
Please contact your system administrator.
Add correct host key in %v to get rid of this message.
Host key verification failed.%c`,
warningRemoteHostChanged, algoStr, keyFingerprintStr, kc.m.String(), '\n')
return ErrUnmatchKey
}
return nil
}
// HostKeyManager gives the interface to manage host keys
type HostKeyManager interface {
String() string
// get all host keys
GetHostKeys() (map[string]gossh.PublicKey, error)
// put new host key under management
PutHostKey(addr string, hostKey gossh.PublicKey) error
}
// HostKeyFile is an implementation of HostKeyManager interface
// Host keys are saved and loaded from file
type HostKeyFile struct {
path string
}
// NewHostKeyFile returns new HostKeyFile using file path
func NewHostKeyFile(path string) *HostKeyFile {
return &HostKeyFile{pkg.ParseFilepath(path)}
}
func (f *HostKeyFile) String() string {
return f.path
}
func (f *HostKeyFile) GetHostKeys() (map[string]gossh.PublicKey, error) {
in, err := os.Open(f.path)
if err != nil {
return nil, err
}
defer in.Close()
hostKeys := make(map[string]gossh.PublicKey)
s := bufio.NewScanner(in)
lineNo := 0
for s.Scan() {
lineNo++
addr, hostKey, err := parseHostLine(s.Bytes())
if err == nil {
hostKeys[addr] = hostKey
}
}
return hostKeys, nil
}
func (f *HostKeyFile) PutHostKey(addr string, hostKey gossh.PublicKey) error {
// Make necessary directories if needed
err := os.MkdirAll(path.Dir(f.path), 0700)
if err != nil {
return err
}
out, err := os.OpenFile(f.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
defer out.Close()
_, err = out.Write(renderHostLine(addr, hostKey))
if err != nil {
return err
}
return nil
}
func parseHostLine(line []byte) (string, gossh.PublicKey, error) {
end := bytes.IndexByte(line, ' ')
if end <= 0 {
return "", nil, ErrUnfoundHostAddr
}
addr := string(line[:end])
keyByte := line[end+1:]
key, _, _, _, err := gossh.ParseAuthorizedKey(keyByte)
if err != nil {
return "", nil, err
}
return addr, key, nil
}
func renderHostLine(addr string, key gossh.PublicKey) []byte {
keyByte := gossh.MarshalAuthorizedKey(key)
// allocate line space in advance
length := len(addr) + 1 + len(keyByte)
line := make([]byte, 0, length)
w := bytes.NewBuffer(line)
w.Write([]byte(addr))
w.WriteByte(' ')
w.Write(keyByte)
return w.Bytes()
}
func algoString(algo string) string {
switch algo {
case gossh.KeyAlgoRSA:
return "RSA"
case gossh.KeyAlgoDSA:
return "DSA"
case gossh.KeyAlgoECDSA256:
return "ECDSA256"
case gossh.KeyAlgoECDSA384:
return "ECDSA384"
case gossh.KeyAlgoECDSA521:
return "ECDSA521"
}
return algo
}
func md5String(md5Sum [16]byte) string {
md5Str := fmt.Sprintf("% x", md5Sum)
md5Str = strings.Replace(md5Str, " ", ":", -1)
return md5Str
}