LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package ssh | 5 package ssh |
6 | 6 |
7 import ( | 7 import ( |
8 "crypto/rand" | 8 "crypto/rand" |
9 "encoding/binary" | 9 "encoding/binary" |
10 "errors" | 10 "errors" |
11 "fmt" | 11 "fmt" |
12 "io" | 12 "io" |
13 "net" | 13 "net" |
14 "sync" | 14 "sync" |
15 ) | 15 ) |
16 | 16 |
17 // clientVersion is the default identification string that the client will use. | |
18 var clientVersion = []byte("SSH-2.0-Go") | |
19 | |
20 // ClientConn represents the client side of an SSH connection. | 17 // ClientConn represents the client side of an SSH connection. |
21 type ClientConn struct { | 18 type ClientConn struct { |
22 *transport | 19 *transport |
23 config *ClientConfig | 20 config *ClientConfig |
24 chanList // channels associated with this connection | 21 chanList // channels associated with this connection |
25 forwardList // forwarded tcpip connections from the remote side | 22 forwardList // forwarded tcpip connections from the remote side |
26 globalRequest | 23 globalRequest |
27 | 24 |
28 // Address as passed to the Dial function. | 25 // Address as passed to the Dial function. |
29 dialAddress string | 26 dialAddress string |
(...skipping 22 matching lines...) Expand all Loading... |
52 if err := conn.handshake(); err != nil { | 49 if err := conn.handshake(); err != nil { |
53 conn.Close() | 50 conn.Close() |
54 return nil, fmt.Errorf("handshake failed: %v", err) | 51 return nil, fmt.Errorf("handshake failed: %v", err) |
55 } | 52 } |
56 go conn.mainLoop() | 53 go conn.mainLoop() |
57 return conn, nil | 54 return conn, nil |
58 } | 55 } |
59 | 56 |
60 // handshake performs the client side key exchange. See RFC 4253 Section 7. | 57 // handshake performs the client side key exchange. See RFC 4253 Section 7. |
61 func (c *ClientConn) handshake() error { | 58 func (c *ClientConn) handshake() error { |
62 » clientVersion := packageVersion | 59 » clientVersion := []byte(packageVersion) |
63 if c.config.ClientVersion != "" { | 60 if c.config.ClientVersion != "" { |
64 » » clientVersion = c.config.ClientVersion | 61 » » clientVersion = []byte(c.config.ClientVersion) |
65 » } | 62 » } |
66 | 63 |
67 » myVersion, serverVersion, err := exchangeVersions(c.transport.Conn, clie
ntVersion) | 64 » serverVersion, err := exchangeVersions(c.transport.Conn, clientVersion) |
68 if err != nil { | 65 if err != nil { |
69 return err | 66 return err |
70 } | 67 } |
71 c.serverVersion = string(serverVersion) | 68 c.serverVersion = string(serverVersion) |
72 | 69 |
73 clientKexInit := kexInitMsg{ | 70 clientKexInit := kexInitMsg{ |
74 KexAlgos: c.config.Crypto.kexes(), | 71 KexAlgos: c.config.Crypto.kexes(), |
75 ServerHostKeyAlgos: supportedHostKeyAlgos, | 72 ServerHostKeyAlgos: supportedHostKeyAlgos, |
76 CiphersClientServer: c.config.Crypto.ciphers(), | 73 CiphersClientServer: c.config.Crypto.ciphers(), |
77 CiphersServerClient: c.config.Crypto.ciphers(), | 74 CiphersServerClient: c.config.Crypto.ciphers(), |
(...skipping 28 matching lines...) Expand all Loading... |
106 return err | 103 return err |
107 } | 104 } |
108 } | 105 } |
109 | 106 |
110 kex, ok := kexAlgoMap[algs.kex] | 107 kex, ok := kexAlgoMap[algs.kex] |
111 if !ok { | 108 if !ok { |
112 return fmt.Errorf("ssh: unexpected key exchange algorithm %v", a
lgs.kex) | 109 return fmt.Errorf("ssh: unexpected key exchange algorithm %v", a
lgs.kex) |
113 } | 110 } |
114 | 111 |
115 magics := handshakeMagics{ | 112 magics := handshakeMagics{ |
116 » » clientVersion: myVersion, | 113 » » clientVersion: clientVersion, |
117 serverVersion: serverVersion, | 114 serverVersion: serverVersion, |
118 clientKexInit: kexInitPacket, | 115 clientKexInit: kexInitPacket, |
119 serverKexInit: packet, | 116 serverKexInit: packet, |
120 } | 117 } |
121 result, err := kex.Client(c, c.config.rand(), &magics) | 118 result, err := kex.Client(c, c.config.rand(), &magics) |
122 if err != nil { | 119 if err != nil { |
123 return err | 120 return err |
124 } | 121 } |
125 | 122 |
126 err = verifyHostKeySignature(algs.hostKey, result.HostKey, result.H, res
ult.Signature) | 123 err = verifyHostKeySignature(algs.hostKey, result.HostKey, result.H, res
ult.Signature) |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 defer c.Unlock() | 508 defer c.Unlock() |
512 | 509 |
513 for _, ch := range c.chans { | 510 for _, ch := range c.chans { |
514 if ch == nil { | 511 if ch == nil { |
515 continue | 512 continue |
516 } | 513 } |
517 ch.Close() | 514 ch.Close() |
518 close(ch.msg) | 515 close(ch.msg) |
519 } | 516 } |
520 } | 517 } |
LEFT | RIGHT |