OLD | NEW |
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" | 8 "crypto" |
9 "crypto/rand" | 9 "crypto/rand" |
10 "errors" | 10 "errors" |
(...skipping 17 matching lines...) Expand all Loading... |
28 // Client returns a new SSH client connection using c as the underlying transpor
t. | 28 // Client returns a new SSH client connection using c as the underlying transpor
t. |
29 func Client(c net.Conn, config *ClientConfig) (*ClientConn, error) { | 29 func Client(c net.Conn, config *ClientConfig) (*ClientConn, error) { |
30 conn := &ClientConn{ | 30 conn := &ClientConn{ |
31 transport: newTransport(c, config.rand()), | 31 transport: newTransport(c, config.rand()), |
32 config: config, | 32 config: config, |
33 } | 33 } |
34 if err := conn.handshake(); err != nil { | 34 if err := conn.handshake(); err != nil { |
35 conn.Close() | 35 conn.Close() |
36 return nil, err | 36 return nil, err |
37 } | 37 } |
38 if err := conn.authenticate(); err != nil { | |
39 conn.Close() | |
40 return nil, err | |
41 } | |
42 go conn.mainLoop() | 38 go conn.mainLoop() |
43 return conn, nil | 39 return conn, nil |
44 } | 40 } |
45 | 41 |
46 // handshake performs the client side key exchange. See RFC 4253 Section 7. | 42 // handshake performs the client side key exchange. See RFC 4253 Section 7. |
47 func (c *ClientConn) handshake() error { | 43 func (c *ClientConn) handshake() error { |
48 var magics handshakeMagics | 44 var magics handshakeMagics |
49 | 45 |
50 if _, err := c.Write(clientVersion); err != nil { | 46 if _, err := c.Write(clientVersion); err != nil { |
51 return err | 47 return err |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 } | 117 } |
122 if err = c.transport.writer.setupKeys(clientKeys, K, H, H, hashFunc); er
r != nil { | 118 if err = c.transport.writer.setupKeys(clientKeys, K, H, H, hashFunc); er
r != nil { |
123 return err | 119 return err |
124 } | 120 } |
125 if packet, err = c.readPacket(); err != nil { | 121 if packet, err = c.readPacket(); err != nil { |
126 return err | 122 return err |
127 } | 123 } |
128 if packet[0] != msgNewKeys { | 124 if packet[0] != msgNewKeys { |
129 return UnexpectedMessageError{msgNewKeys, packet[0]} | 125 return UnexpectedMessageError{msgNewKeys, packet[0]} |
130 } | 126 } |
131 » return c.transport.reader.setupKeys(serverKeys, K, H, H, hashFunc) | 127 » if err := c.transport.reader.setupKeys(serverKeys, K, H, H, hashFunc); e
rr != nil { |
| 128 » » return err |
| 129 » } |
| 130 » return c.authenticate(H) |
132 } | 131 } |
133 | 132 |
134 // kexDH performs Diffie-Hellman key agreement on a ClientConn. The | 133 // kexDH performs Diffie-Hellman key agreement on a ClientConn. The |
135 // returned values are given the same names as in RFC 4253, section 8. | 134 // returned values are given the same names as in RFC 4253, section 8. |
136 func (c *ClientConn) kexDH(group *dhGroup, hashFunc crypto.Hash, magics *handsha
keMagics, hostKeyAlgo string) ([]byte, []byte, error) { | 135 func (c *ClientConn) kexDH(group *dhGroup, hashFunc crypto.Hash, magics *handsha
keMagics, hostKeyAlgo string) ([]byte, []byte, error) { |
137 x, err := rand.Int(c.config.rand(), group.p) | 136 x, err := rand.Int(c.config.rand(), group.p) |
138 if err != nil { | 137 if err != nil { |
139 return nil, nil, err | 138 return nil, nil, err |
140 } | 139 } |
141 X := new(big.Int).Exp(group.g, x, group.p) | 140 X := new(big.Int).Exp(group.g, x, group.p) |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 if !ok { | 452 if !ok { |
454 return 0, io.EOF | 453 return 0, io.EOF |
455 } | 454 } |
456 } | 455 } |
457 panic("unreachable") | 456 panic("unreachable") |
458 } | 457 } |
459 | 458 |
460 func (r *chanReader) Close() error { | 459 func (r *chanReader) Close() error { |
461 return r.writePacket(marshal(msgChannelEOF, channelEOFMsg{r.id})) | 460 return r.writePacket(marshal(msgChannelEOF, channelEOFMsg{r.id})) |
462 } | 461 } |
OLD | NEW |