blob: dc39e4d2318294d8ac3db54f9a67f49207209224 [file] [log] [blame]
Russ Cox470549d2012-01-25 15:31:12 -05001// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package ssh
6
7import (
Han-Wen Nienhuysd7d50b02013-08-28 10:50:25 -04008 "crypto"
Adam Langleyfa50e742014-04-09 13:57:52 -07009 "crypto/rand"
Dave Cheney1582bf02012-10-30 18:13:59 +110010 "fmt"
Adam Langleyfa50e742014-04-09 13:57:52 -070011 "io"
Han-Wen Nienhuysc78caca2017-04-04 17:27:40 +020012 "math"
Russ Cox470549d2012-01-25 15:31:12 -050013 "sync"
Jonathan Pittman6a743c52013-09-09 13:06:04 -040014
15 _ "crypto/sha1"
16 _ "crypto/sha256"
17 _ "crypto/sha512"
Russ Cox470549d2012-01-25 15:31:12 -050018)
19
20// These are string constants in the SSH protocol.
21const (
Russ Cox470549d2012-01-25 15:31:12 -050022 compressionNone = "none"
23 serviceUserAuth = "ssh-userauth"
24 serviceSSH = "ssh-connection"
25)
26
Adam Langleyfa50e742014-04-09 13:57:52 -070027// supportedCiphers specifies the supported ciphers in preference order.
28var supportedCiphers = []string{
29 "aes128-ctr", "aes192-ctr", "aes256-ctr",
30 "aes128-gcm@openssh.com",
31 "arcfour256", "arcfour128",
32}
33
34// supportedKexAlgos specifies the supported key-exchange algorithms in
35// preference order.
Han-Wen Nienhuysd7d50b02013-08-28 10:50:25 -040036var supportedKexAlgos = []string{
hanwenaca188f2015-08-12 11:17:08 +020037 kexAlgoCurve25519SHA256,
Adam Langleyfa50e742014-04-09 13:57:52 -070038 // P384 and P521 are not constant-time yet, but since we don't
39 // reuse ephemeral keys, using them for ECDH should be OK.
Han-Wen Nienhuysd7d50b02013-08-28 10:50:25 -040040 kexAlgoECDH256, kexAlgoECDH384, kexAlgoECDH521,
41 kexAlgoDH14SHA1, kexAlgoDH1SHA1,
42}
43
Brad Fitzpatrick3cb07272017-03-30 16:02:45 +000044// supportedHostKeyAlgos specifies the supported host-key algorithms (i.e. methods
Adam Langleyfa50e742014-04-09 13:57:52 -070045// of authenticating servers) in preference order.
Han-Wen Nienhuys41400fe2013-10-07 18:30:34 -040046var supportedHostKeyAlgos = []string{
Adam Langleyfa50e742014-04-09 13:57:52 -070047 CertAlgoRSAv01, CertAlgoDSAv01, CertAlgoECDSA256v01,
Peter Moody10c26742016-07-08 11:32:35 -070048 CertAlgoECDSA384v01, CertAlgoECDSA521v01, CertAlgoED25519v01,
Adam Langleyfa50e742014-04-09 13:57:52 -070049
Han-Wen Nienhuys41400fe2013-10-07 18:30:34 -040050 KeyAlgoECDSA256, KeyAlgoECDSA384, KeyAlgoECDSA521,
51 KeyAlgoRSA, KeyAlgoDSA,
Martin Garton1e61df82016-04-30 22:10:58 +010052
53 KeyAlgoED25519,
Han-Wen Nienhuys41400fe2013-10-07 18:30:34 -040054}
55
Adam Langleyfa50e742014-04-09 13:57:52 -070056// supportedMACs specifies a default set of MAC algorithms in preference order.
57// This is based on RFC 4253, section 6.4, but with hmac-md5 variants removed
58// because they have reached the end of their useful life.
59var supportedMACs = []string{
MiLk84bacda2017-01-20 08:44:06 +090060 "hmac-sha2-256-etm@openssh.com", "hmac-sha2-256", "hmac-sha1", "hmac-sha1-96",
Adam Langleyfa50e742014-04-09 13:57:52 -070061}
62
Russ Cox470549d2012-01-25 15:31:12 -050063var supportedCompressions = []string{compressionNone}
64
Jonathan Pittman6a743c52013-09-09 13:06:04 -040065// hashFuncs keeps the mapping of supported algorithms to their respective
66// hashes needed for signature verification.
67var hashFuncs = map[string]crypto.Hash{
68 KeyAlgoRSA: crypto.SHA1,
69 KeyAlgoDSA: crypto.SHA1,
70 KeyAlgoECDSA256: crypto.SHA256,
71 KeyAlgoECDSA384: crypto.SHA384,
72 KeyAlgoECDSA521: crypto.SHA512,
73 CertAlgoRSAv01: crypto.SHA1,
74 CertAlgoDSAv01: crypto.SHA1,
75 CertAlgoECDSA256v01: crypto.SHA256,
76 CertAlgoECDSA384v01: crypto.SHA384,
77 CertAlgoECDSA521v01: crypto.SHA512,
78}
79
Adam Langleyfa50e742014-04-09 13:57:52 -070080// unexpectedMessageError results when the SSH message that we received didn't
Russ Cox470549d2012-01-25 15:31:12 -050081// match what we wanted.
Adam Langleyfa50e742014-04-09 13:57:52 -070082func unexpectedMessageError(expected, got uint8) error {
83 return fmt.Errorf("ssh: unexpected message type %d (expected %d)", got, expected)
Russ Cox470549d2012-01-25 15:31:12 -050084}
85
Adam Langleyfa50e742014-04-09 13:57:52 -070086// parseError results from a malformed SSH message.
87func parseError(tag uint8) error {
88 return fmt.Errorf("ssh: parse error in message type %d", tag)
Russ Cox470549d2012-01-25 15:31:12 -050089}
90
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -040091func findCommon(what string, client []string, server []string) (common string, err error) {
92 for _, c := range client {
93 for _, s := range server {
94 if c == s {
95 return c, nil
Russ Cox470549d2012-01-25 15:31:12 -050096 }
97 }
98 }
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -040099 return "", fmt.Errorf("ssh: no common algorithm for %s; client offered: %v, server offered: %v", what, client, server)
Russ Cox470549d2012-01-25 15:31:12 -0500100}
101
Adam Langleyfa50e742014-04-09 13:57:52 -0700102type directionAlgorithms struct {
103 Cipher string
104 MAC string
105 Compression string
106}
107
Han-Wen Nienhuysa59c1272017-01-17 18:15:27 +0100108// rekeyBytes returns a rekeying intervals in bytes.
109func (a *directionAlgorithms) rekeyBytes() int64 {
110 // According to RFC4344 block ciphers should rekey after
111 // 2^(BLOCKSIZE/4) blocks. For all AES flavors BLOCKSIZE is
112 // 128.
113 switch a.Cipher {
114 case "aes128-ctr", "aes192-ctr", "aes256-ctr", gcmCipherID, aes128cbcID:
115 return 16 * (1 << 32)
116
117 }
118
119 // For others, stick with RFC4253 recommendation to rekey after 1 Gb of data.
120 return 1 << 30
121}
122
Han-Wen Nienhuys41472562013-10-08 15:47:04 -0400123type algorithms struct {
Adam Langleyfa50e742014-04-09 13:57:52 -0700124 kex string
125 hostKey string
126 w directionAlgorithms
127 r directionAlgorithms
Han-Wen Nienhuys41472562013-10-08 15:47:04 -0400128}
129
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400130func findAgreedAlgorithms(clientKexInit, serverKexInit *kexInitMsg) (algs *algorithms, err error) {
Han-Wen Nienhuys41472562013-10-08 15:47:04 -0400131 result := &algorithms{}
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400132
133 result.kex, err = findCommon("key exchange", clientKexInit.KexAlgos, serverKexInit.KexAlgos)
134 if err != nil {
Russ Cox470549d2012-01-25 15:31:12 -0500135 return
136 }
137
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400138 result.hostKey, err = findCommon("host key", clientKexInit.ServerHostKeyAlgos, serverKexInit.ServerHostKeyAlgos)
139 if err != nil {
Russ Cox470549d2012-01-25 15:31:12 -0500140 return
141 }
142
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400143 result.w.Cipher, err = findCommon("client to server cipher", clientKexInit.CiphersClientServer, serverKexInit.CiphersClientServer)
144 if err != nil {
Russ Cox470549d2012-01-25 15:31:12 -0500145 return
146 }
147
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400148 result.r.Cipher, err = findCommon("server to client cipher", clientKexInit.CiphersServerClient, serverKexInit.CiphersServerClient)
149 if err != nil {
Russ Cox470549d2012-01-25 15:31:12 -0500150 return
151 }
152
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400153 result.w.MAC, err = findCommon("client to server MAC", clientKexInit.MACsClientServer, serverKexInit.MACsClientServer)
154 if err != nil {
Russ Cox470549d2012-01-25 15:31:12 -0500155 return
156 }
157
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400158 result.r.MAC, err = findCommon("server to client MAC", clientKexInit.MACsServerClient, serverKexInit.MACsServerClient)
159 if err != nil {
Russ Cox470549d2012-01-25 15:31:12 -0500160 return
161 }
162
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400163 result.w.Compression, err = findCommon("client to server compression", clientKexInit.CompressionClientServer, serverKexInit.CompressionClientServer)
164 if err != nil {
Russ Cox470549d2012-01-25 15:31:12 -0500165 return
166 }
167
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400168 result.r.Compression, err = findCommon("server to client compression", clientKexInit.CompressionServerClient, serverKexInit.CompressionServerClient)
169 if err != nil {
Russ Cox470549d2012-01-25 15:31:12 -0500170 return
171 }
172
Thomas Desrosiers6c2080b2015-08-21 21:25:57 -0400173 return result, nil
Russ Cox470549d2012-01-25 15:31:12 -0500174}
175
Adam Langleyfa50e742014-04-09 13:57:52 -0700176// If rekeythreshold is too small, we can't make any progress sending
177// stuff.
178const minRekeyThreshold uint64 = 256
179
180// Config contains configuration data common to both ServerConfig and
181// ClientConfig.
182type Config struct {
183 // Rand provides the source of entropy for cryptographic
184 // primitives. If Rand is nil, the cryptographic random reader
185 // in package crypto/rand will be used.
186 Rand io.Reader
187
188 // The maximum number of bytes sent or received after which a
189 // new key is negotiated. It must be at least 256. If
Han-Wen Nienhuysc78caca2017-04-04 17:27:40 +0200190 // unspecified, a size suitable for the chosen cipher is used.
Adam Langleyfa50e742014-04-09 13:57:52 -0700191 RekeyThreshold uint64
192
Han-Wen Nienhuysd7d50b02013-08-28 10:50:25 -0400193 // The allowed key exchanges algorithms. If unspecified then a
194 // default set of algorithms is used.
195 KeyExchanges []string
196
Adam Langleyfa50e742014-04-09 13:57:52 -0700197 // The allowed cipher algorithms. If unspecified then a sensible
198 // default is used.
Russ Cox470549d2012-01-25 15:31:12 -0500199 Ciphers []string
Dave Cheney6de97b52012-02-27 19:40:52 -0500200
Adam Langleyfa50e742014-04-09 13:57:52 -0700201 // The allowed MAC algorithms. If unspecified then a sensible default
202 // is used.
Dave Cheney6de97b52012-02-27 19:40:52 -0500203 MACs []string
Russ Cox470549d2012-01-25 15:31:12 -0500204}
205
Adam Langleyfa50e742014-04-09 13:57:52 -0700206// SetDefaults sets sensible values for unset fields in config. This is
207// exported for testing: Configs passed to SSH functions are copied and have
208// default values set automatically.
209func (c *Config) SetDefaults() {
210 if c.Rand == nil {
211 c.Rand = rand.Reader
212 }
Russ Cox470549d2012-01-25 15:31:12 -0500213 if c.Ciphers == nil {
Adam Langleyfa50e742014-04-09 13:57:52 -0700214 c.Ciphers = supportedCiphers
Russ Cox470549d2012-01-25 15:31:12 -0500215 }
Nathan(yinian) Hu5c68cfd2015-04-08 12:39:14 +1000216 var ciphers []string
217 for _, c := range c.Ciphers {
218 if cipherModes[c] != nil {
219 // reject the cipher if we have no cipherModes definition
220 ciphers = append(ciphers, c)
221 }
222 }
223 c.Ciphers = ciphers
Russ Cox470549d2012-01-25 15:31:12 -0500224
Han-Wen Nienhuysd7d50b02013-08-28 10:50:25 -0400225 if c.KeyExchanges == nil {
Adam Langleyfa50e742014-04-09 13:57:52 -0700226 c.KeyExchanges = supportedKexAlgos
Han-Wen Nienhuysd7d50b02013-08-28 10:50:25 -0400227 }
Han-Wen Nienhuysd7d50b02013-08-28 10:50:25 -0400228
Dave Cheney6de97b52012-02-27 19:40:52 -0500229 if c.MACs == nil {
Adam Langleyfa50e742014-04-09 13:57:52 -0700230 c.MACs = supportedMACs
Dave Cheney6de97b52012-02-27 19:40:52 -0500231 }
Dave Cheney6de97b52012-02-27 19:40:52 -0500232
Adam Langleyfa50e742014-04-09 13:57:52 -0700233 if c.RekeyThreshold == 0 {
Han-Wen Nienhuysc78caca2017-04-04 17:27:40 +0200234 // cipher specific default
235 } else if c.RekeyThreshold < minRekeyThreshold {
Adam Langleyfa50e742014-04-09 13:57:52 -0700236 c.RekeyThreshold = minRekeyThreshold
Han-Wen Nienhuysc78caca2017-04-04 17:27:40 +0200237 } else if c.RekeyThreshold >= math.MaxInt64 {
238 // Avoid weirdness if somebody uses -1 as a threshold.
239 c.RekeyThreshold = math.MaxInt64
Adam Langleyfa50e742014-04-09 13:57:52 -0700240 }
Jonathan Pittman6a743c52013-09-09 13:06:04 -0400241}
242
Russ Cox470549d2012-01-25 15:31:12 -0500243// buildDataSignedForAuth returns the data that is signed in order to prove
Jonathan Pittman44256fa2013-10-10 11:35:15 -0400244// possession of a private key. See RFC 4252, section 7.
Russ Cox470549d2012-01-25 15:31:12 -0500245func buildDataSignedForAuth(sessionId []byte, req userAuthRequestMsg, algo, pubKey []byte) []byte {
Adam Langleyfa50e742014-04-09 13:57:52 -0700246 data := struct {
247 Session []byte
248 Type byte
249 User string
250 Service string
251 Method string
252 Sign bool
253 Algo []byte
254 PubKey []byte
255 }{
256 sessionId,
257 msgUserAuthRequest,
258 req.User,
259 req.Service,
260 req.Method,
261 true,
262 algo,
263 pubKey,
Russ Cox470549d2012-01-25 15:31:12 -0500264 }
Adam Langleyfa50e742014-04-09 13:57:52 -0700265 return Marshal(data)
Russ Cox470549d2012-01-25 15:31:12 -0500266}
Dave Cheney79d53bd2012-03-04 14:34:24 -0800267
268func appendU16(buf []byte, n uint16) []byte {
269 return append(buf, byte(n>>8), byte(n))
270}
271
272func appendU32(buf []byte, n uint32) []byte {
273 return append(buf, byte(n>>24), byte(n>>16), byte(n>>8), byte(n))
274}
275
Adam Langleyfa50e742014-04-09 13:57:52 -0700276func appendU64(buf []byte, n uint64) []byte {
277 return append(buf,
278 byte(n>>56), byte(n>>48), byte(n>>40), byte(n>>32),
279 byte(n>>24), byte(n>>16), byte(n>>8), byte(n))
280}
281
Dave Cheney79d53bd2012-03-04 14:34:24 -0800282func appendInt(buf []byte, n int) []byte {
283 return appendU32(buf, uint32(n))
284}
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000285
Han-Wen Nienhuysc7df5652013-06-06 10:44:12 -0400286func appendString(buf []byte, s string) []byte {
287 buf = appendU32(buf, uint32(len(s)))
288 buf = append(buf, s...)
289 return buf
290}
291
292func appendBool(buf []byte, b bool) []byte {
293 if b {
Adam Langleyfa50e742014-04-09 13:57:52 -0700294 return append(buf, 1)
Han-Wen Nienhuysc7df5652013-06-06 10:44:12 -0400295 }
Adam Langleyfa50e742014-04-09 13:57:52 -0700296 return append(buf, 0)
Han-Wen Nienhuysc7df5652013-06-06 10:44:12 -0400297}
298
Adam Langley4002be22012-12-10 18:12:36 -0500299// newCond is a helper to hide the fact that there is no usable zero
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000300// value for sync.Cond.
301func newCond() *sync.Cond { return sync.NewCond(new(sync.Mutex)) }
302
Adam Langley4002be22012-12-10 18:12:36 -0500303// window represents the buffer available to clients
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000304// wishing to write to a channel.
305type window struct {
306 *sync.Cond
Adam Langleyfa50e742014-04-09 13:57:52 -0700307 win uint32 // RFC 4254 5.2 says the window size can grow to 2^32-1
308 writeWaiters int
309 closed bool
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000310}
311
312// add adds win to the amount of window available
313// for consumers.
314func (w *window) add(win uint32) bool {
Dave Cheney55aa0812012-05-22 12:04:51 +1000315 // a zero sized window adjust is a noop.
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000316 if win == 0 {
Dave Cheney55aa0812012-05-22 12:04:51 +1000317 return true
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000318 }
319 w.L.Lock()
320 if w.win+win < win {
321 w.L.Unlock()
322 return false
323 }
324 w.win += win
325 // It is unusual that multiple goroutines would be attempting to reserve
Adam Langley4002be22012-12-10 18:12:36 -0500326 // window space, but not guaranteed. Use broadcast to notify all waiters
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000327 // that additional window is available.
328 w.Broadcast()
329 w.L.Unlock()
330 return true
331}
332
Adam Langleyfa50e742014-04-09 13:57:52 -0700333// close sets the window to closed, so all reservations fail
334// immediately.
335func (w *window) close() {
336 w.L.Lock()
337 w.closed = true
338 w.Broadcast()
339 w.L.Unlock()
340}
341
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000342// reserve reserves win from the available window capacity.
343// If no capacity remains, reserve will block. reserve may
344// return less than requested.
Adam Langleyfa50e742014-04-09 13:57:52 -0700345func (w *window) reserve(win uint32) (uint32, error) {
346 var err error
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000347 w.L.Lock()
Adam Langleyfa50e742014-04-09 13:57:52 -0700348 w.writeWaiters++
349 w.Broadcast()
350 for w.win == 0 && !w.closed {
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000351 w.Wait()
352 }
Adam Langleyfa50e742014-04-09 13:57:52 -0700353 w.writeWaiters--
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000354 if w.win < win {
355 win = w.win
356 }
357 w.win -= win
Adam Langleyfa50e742014-04-09 13:57:52 -0700358 if w.closed {
359 err = io.EOF
360 }
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000361 w.L.Unlock()
Adam Langleyfa50e742014-04-09 13:57:52 -0700362 return win, err
363}
364
365// waitWriterBlocked waits until some goroutine is blocked for further
366// writes. It is used in tests only.
367func (w *window) waitWriterBlocked() {
368 w.Cond.L.Lock()
369 for w.writeWaiters == 0 {
370 w.Cond.Wait()
371 }
372 w.Cond.L.Unlock()
Dave Cheney8a2e7c92012-05-11 05:56:44 +1000373}