go.crypto/ssh: add hook for host key checking.
R=dave, agl CC=gobot, golang-dev https://golang.org/cl/9922043
This commit is contained in:
committed by
Adam Langley
parent
b88b016522
commit
afdc305bc8
+21
-1
@@ -26,6 +26,9 @@ type ClientConn struct {
|
||||
chanList // channels associated with this connection
|
||||
forwardList // forwarded tcpip connections from the remote side
|
||||
globalRequest
|
||||
|
||||
// Address as passed to the Dial function.
|
||||
dialAddress string
|
||||
}
|
||||
|
||||
type globalRequest struct {
|
||||
@@ -35,11 +38,17 @@ type globalRequest struct {
|
||||
|
||||
// Client returns a new SSH client connection using c as the underlying transport.
|
||||
func Client(c net.Conn, config *ClientConfig) (*ClientConn, error) {
|
||||
return clientWithAddress(c, "", config)
|
||||
}
|
||||
|
||||
func clientWithAddress(c net.Conn, addr string, config *ClientConfig) (*ClientConn, error) {
|
||||
conn := &ClientConn{
|
||||
transport: newTransport(c, config.rand()),
|
||||
config: config,
|
||||
globalRequest: globalRequest{response: make(chan interface{}, 1)},
|
||||
dialAddress: addr,
|
||||
}
|
||||
|
||||
if err := conn.handshake(); err != nil {
|
||||
conn.Close()
|
||||
return nil, fmt.Errorf("handshake failed: %v", err)
|
||||
@@ -168,6 +177,12 @@ func (c *ClientConn) kexDH(group *dhGroup, hashFunc crypto.Hash, magics *handsha
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if checker := c.config.HostKeyChecker; checker != nil {
|
||||
if err = checker.Check(c.dialAddress, c.RemoteAddr(), hostKeyAlgo, kexDHReply.HostKey); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
kInt, err := group.diffieHellman(kexDHReply.Y, x)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -445,7 +460,7 @@ func Dial(network, addr string, config *ClientConfig) (*ClientConn, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Client(conn, config)
|
||||
return clientWithAddress(conn, addr, config)
|
||||
}
|
||||
|
||||
// A ClientConfig structure is used to configure a ClientConn. After one has
|
||||
@@ -463,6 +478,11 @@ type ClientConfig struct {
|
||||
// of a particular RFC 4252 method will be used during authentication.
|
||||
Auth []ClientAuth
|
||||
|
||||
// HostKeyChecker, if not nil, is called during the cryptographic
|
||||
// handshake to validate the server's host key. A nil HostKeyChecker
|
||||
// implies that all host keys are accepted.
|
||||
HostKeyChecker HostKeyChecker
|
||||
|
||||
// Cryptographic-related configuration.
|
||||
Crypto CryptoConfig
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user