Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 1 | // 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 | |
| 5 | package ssh |
| 6 | |
| 7 | import ( |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 8 | "crypto" |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 9 | "crypto/rand" |
Dave Cheney | 1582bf0 | 2012-10-30 18:13:59 +1100 | [diff] [blame] | 10 | "fmt" |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 11 | "io" |
Han-Wen Nienhuys | c78caca | 2017-04-04 17:27:40 +0200 | [diff] [blame] | 12 | "math" |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 13 | "sync" |
Jonathan Pittman | 6a743c5 | 2013-09-09 13:06:04 -0400 | [diff] [blame] | 14 | |
| 15 | _ "crypto/sha1" |
| 16 | _ "crypto/sha256" |
| 17 | _ "crypto/sha512" |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | // These are string constants in the SSH protocol. |
| 21 | const ( |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 22 | compressionNone = "none" |
| 23 | serviceUserAuth = "ssh-userauth" |
| 24 | serviceSSH = "ssh-connection" |
| 25 | ) |
| 26 | |
Han-Wen Nienhuys | 1835319 | 2018-01-10 11:56:48 +0100 | [diff] [blame] | 27 | // supportedCiphers lists ciphers we support but might not recommend. |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 28 | var supportedCiphers = []string{ |
| 29 | "aes128-ctr", "aes192-ctr", "aes256-ctr", |
| 30 | "aes128-gcm@openssh.com", |
Han-Wen Nienhuys | ee41a25 | 2018-01-10 12:42:30 +0100 | [diff] [blame] | 31 | chacha20Poly1305ID, |
Han-Wen Nienhuys | 1835319 | 2018-01-10 11:56:48 +0100 | [diff] [blame] | 32 | "arcfour256", "arcfour128", "arcfour", |
| 33 | aes128cbcID, |
| 34 | tripledescbcID, |
| 35 | } |
| 36 | |
| 37 | // preferredCiphers specifies the default preference for ciphers. |
| 38 | var preferredCiphers = []string{ |
Han-Wen Nienhuys | 1835319 | 2018-01-10 11:56:48 +0100 | [diff] [blame] | 39 | "aes128-gcm@openssh.com", |
Han-Wen Nienhuys | d94f6bc | 2018-01-22 20:45:19 +0100 | [diff] [blame^] | 40 | chacha20Poly1305ID, |
| 41 | "aes128-ctr", "aes192-ctr", "aes256-ctr", |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // supportedKexAlgos specifies the supported key-exchange algorithms in |
| 45 | // preference order. |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 46 | var supportedKexAlgos = []string{ |
hanwen | aca188f | 2015-08-12 11:17:08 +0200 | [diff] [blame] | 47 | kexAlgoCurve25519SHA256, |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 48 | // P384 and P521 are not constant-time yet, but since we don't |
| 49 | // reuse ephemeral keys, using them for ECDH should be OK. |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 50 | kexAlgoECDH256, kexAlgoECDH384, kexAlgoECDH521, |
| 51 | kexAlgoDH14SHA1, kexAlgoDH1SHA1, |
| 52 | } |
| 53 | |
Brad Fitzpatrick | 3cb0727 | 2017-03-30 16:02:45 +0000 | [diff] [blame] | 54 | // supportedHostKeyAlgos specifies the supported host-key algorithms (i.e. methods |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 55 | // of authenticating servers) in preference order. |
Han-Wen Nienhuys | 41400fe | 2013-10-07 18:30:34 -0400 | [diff] [blame] | 56 | var supportedHostKeyAlgos = []string{ |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 57 | CertAlgoRSAv01, CertAlgoDSAv01, CertAlgoECDSA256v01, |
Peter Moody | 10c2674 | 2016-07-08 11:32:35 -0700 | [diff] [blame] | 58 | CertAlgoECDSA384v01, CertAlgoECDSA521v01, CertAlgoED25519v01, |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 59 | |
Han-Wen Nienhuys | 41400fe | 2013-10-07 18:30:34 -0400 | [diff] [blame] | 60 | KeyAlgoECDSA256, KeyAlgoECDSA384, KeyAlgoECDSA521, |
| 61 | KeyAlgoRSA, KeyAlgoDSA, |
Martin Garton | 1e61df8 | 2016-04-30 22:10:58 +0100 | [diff] [blame] | 62 | |
| 63 | KeyAlgoED25519, |
Han-Wen Nienhuys | 41400fe | 2013-10-07 18:30:34 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 66 | // supportedMACs specifies a default set of MAC algorithms in preference order. |
| 67 | // This is based on RFC 4253, section 6.4, but with hmac-md5 variants removed |
| 68 | // because they have reached the end of their useful life. |
| 69 | var supportedMACs = []string{ |
MiLk | 84bacda | 2017-01-20 08:44:06 +0900 | [diff] [blame] | 70 | "hmac-sha2-256-etm@openssh.com", "hmac-sha2-256", "hmac-sha1", "hmac-sha1-96", |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 73 | var supportedCompressions = []string{compressionNone} |
| 74 | |
Jonathan Pittman | 6a743c5 | 2013-09-09 13:06:04 -0400 | [diff] [blame] | 75 | // hashFuncs keeps the mapping of supported algorithms to their respective |
| 76 | // hashes needed for signature verification. |
| 77 | var hashFuncs = map[string]crypto.Hash{ |
| 78 | KeyAlgoRSA: crypto.SHA1, |
| 79 | KeyAlgoDSA: crypto.SHA1, |
| 80 | KeyAlgoECDSA256: crypto.SHA256, |
| 81 | KeyAlgoECDSA384: crypto.SHA384, |
| 82 | KeyAlgoECDSA521: crypto.SHA512, |
| 83 | CertAlgoRSAv01: crypto.SHA1, |
| 84 | CertAlgoDSAv01: crypto.SHA1, |
| 85 | CertAlgoECDSA256v01: crypto.SHA256, |
| 86 | CertAlgoECDSA384v01: crypto.SHA384, |
| 87 | CertAlgoECDSA521v01: crypto.SHA512, |
| 88 | } |
| 89 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 90 | // unexpectedMessageError results when the SSH message that we received didn't |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 91 | // match what we wanted. |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 92 | func unexpectedMessageError(expected, got uint8) error { |
| 93 | return fmt.Errorf("ssh: unexpected message type %d (expected %d)", got, expected) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 96 | // parseError results from a malformed SSH message. |
| 97 | func parseError(tag uint8) error { |
| 98 | return fmt.Errorf("ssh: parse error in message type %d", tag) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 101 | func findCommon(what string, client []string, server []string) (common string, err error) { |
| 102 | for _, c := range client { |
| 103 | for _, s := range server { |
| 104 | if c == s { |
| 105 | return c, nil |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 109 | return "", fmt.Errorf("ssh: no common algorithm for %s; client offered: %v, server offered: %v", what, client, server) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 112 | type directionAlgorithms struct { |
| 113 | Cipher string |
| 114 | MAC string |
| 115 | Compression string |
| 116 | } |
| 117 | |
Han-Wen Nienhuys | a59c127 | 2017-01-17 18:15:27 +0100 | [diff] [blame] | 118 | // rekeyBytes returns a rekeying intervals in bytes. |
| 119 | func (a *directionAlgorithms) rekeyBytes() int64 { |
| 120 | // According to RFC4344 block ciphers should rekey after |
| 121 | // 2^(BLOCKSIZE/4) blocks. For all AES flavors BLOCKSIZE is |
| 122 | // 128. |
| 123 | switch a.Cipher { |
| 124 | case "aes128-ctr", "aes192-ctr", "aes256-ctr", gcmCipherID, aes128cbcID: |
| 125 | return 16 * (1 << 32) |
| 126 | |
| 127 | } |
| 128 | |
| 129 | // For others, stick with RFC4253 recommendation to rekey after 1 Gb of data. |
| 130 | return 1 << 30 |
| 131 | } |
| 132 | |
Han-Wen Nienhuys | 4147256 | 2013-10-08 15:47:04 -0400 | [diff] [blame] | 133 | type algorithms struct { |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 134 | kex string |
| 135 | hostKey string |
| 136 | w directionAlgorithms |
| 137 | r directionAlgorithms |
Han-Wen Nienhuys | 4147256 | 2013-10-08 15:47:04 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 140 | func findAgreedAlgorithms(clientKexInit, serverKexInit *kexInitMsg) (algs *algorithms, err error) { |
Han-Wen Nienhuys | 4147256 | 2013-10-08 15:47:04 -0400 | [diff] [blame] | 141 | result := &algorithms{} |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 142 | |
| 143 | result.kex, err = findCommon("key exchange", clientKexInit.KexAlgos, serverKexInit.KexAlgos) |
| 144 | if err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 145 | return |
| 146 | } |
| 147 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 148 | result.hostKey, err = findCommon("host key", clientKexInit.ServerHostKeyAlgos, serverKexInit.ServerHostKeyAlgos) |
| 149 | if err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 150 | return |
| 151 | } |
| 152 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 153 | result.w.Cipher, err = findCommon("client to server cipher", clientKexInit.CiphersClientServer, serverKexInit.CiphersClientServer) |
| 154 | if err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 155 | return |
| 156 | } |
| 157 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 158 | result.r.Cipher, err = findCommon("server to client cipher", clientKexInit.CiphersServerClient, serverKexInit.CiphersServerClient) |
| 159 | if err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 160 | return |
| 161 | } |
| 162 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 163 | result.w.MAC, err = findCommon("client to server MAC", clientKexInit.MACsClientServer, serverKexInit.MACsClientServer) |
| 164 | if err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 165 | return |
| 166 | } |
| 167 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 168 | result.r.MAC, err = findCommon("server to client MAC", clientKexInit.MACsServerClient, serverKexInit.MACsServerClient) |
| 169 | if err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 170 | return |
| 171 | } |
| 172 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 173 | result.w.Compression, err = findCommon("client to server compression", clientKexInit.CompressionClientServer, serverKexInit.CompressionClientServer) |
| 174 | if err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 175 | return |
| 176 | } |
| 177 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 178 | result.r.Compression, err = findCommon("server to client compression", clientKexInit.CompressionServerClient, serverKexInit.CompressionServerClient) |
| 179 | if err != nil { |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 180 | return |
| 181 | } |
| 182 | |
Thomas Desrosiers | 6c2080b | 2015-08-21 21:25:57 -0400 | [diff] [blame] | 183 | return result, nil |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 186 | // If rekeythreshold is too small, we can't make any progress sending |
| 187 | // stuff. |
| 188 | const minRekeyThreshold uint64 = 256 |
| 189 | |
| 190 | // Config contains configuration data common to both ServerConfig and |
| 191 | // ClientConfig. |
| 192 | type Config struct { |
| 193 | // Rand provides the source of entropy for cryptographic |
| 194 | // primitives. If Rand is nil, the cryptographic random reader |
| 195 | // in package crypto/rand will be used. |
| 196 | Rand io.Reader |
| 197 | |
| 198 | // The maximum number of bytes sent or received after which a |
| 199 | // new key is negotiated. It must be at least 256. If |
Han-Wen Nienhuys | c78caca | 2017-04-04 17:27:40 +0200 | [diff] [blame] | 200 | // unspecified, a size suitable for the chosen cipher is used. |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 201 | RekeyThreshold uint64 |
| 202 | |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 203 | // The allowed key exchanges algorithms. If unspecified then a |
| 204 | // default set of algorithms is used. |
| 205 | KeyExchanges []string |
| 206 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 207 | // The allowed cipher algorithms. If unspecified then a sensible |
| 208 | // default is used. |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 209 | Ciphers []string |
Dave Cheney | 6de97b5 | 2012-02-27 19:40:52 -0500 | [diff] [blame] | 210 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 211 | // The allowed MAC algorithms. If unspecified then a sensible default |
| 212 | // is used. |
Dave Cheney | 6de97b5 | 2012-02-27 19:40:52 -0500 | [diff] [blame] | 213 | MACs []string |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 216 | // SetDefaults sets sensible values for unset fields in config. This is |
| 217 | // exported for testing: Configs passed to SSH functions are copied and have |
| 218 | // default values set automatically. |
| 219 | func (c *Config) SetDefaults() { |
| 220 | if c.Rand == nil { |
| 221 | c.Rand = rand.Reader |
| 222 | } |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 223 | if c.Ciphers == nil { |
Han-Wen Nienhuys | 1835319 | 2018-01-10 11:56:48 +0100 | [diff] [blame] | 224 | c.Ciphers = preferredCiphers |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 225 | } |
Nathan(yinian) Hu | 5c68cfd | 2015-04-08 12:39:14 +1000 | [diff] [blame] | 226 | var ciphers []string |
| 227 | for _, c := range c.Ciphers { |
| 228 | if cipherModes[c] != nil { |
| 229 | // reject the cipher if we have no cipherModes definition |
| 230 | ciphers = append(ciphers, c) |
| 231 | } |
| 232 | } |
| 233 | c.Ciphers = ciphers |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 234 | |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 235 | if c.KeyExchanges == nil { |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 236 | c.KeyExchanges = supportedKexAlgos |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 237 | } |
Han-Wen Nienhuys | d7d50b0 | 2013-08-28 10:50:25 -0400 | [diff] [blame] | 238 | |
Dave Cheney | 6de97b5 | 2012-02-27 19:40:52 -0500 | [diff] [blame] | 239 | if c.MACs == nil { |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 240 | c.MACs = supportedMACs |
Dave Cheney | 6de97b5 | 2012-02-27 19:40:52 -0500 | [diff] [blame] | 241 | } |
Dave Cheney | 6de97b5 | 2012-02-27 19:40:52 -0500 | [diff] [blame] | 242 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 243 | if c.RekeyThreshold == 0 { |
Han-Wen Nienhuys | c78caca | 2017-04-04 17:27:40 +0200 | [diff] [blame] | 244 | // cipher specific default |
| 245 | } else if c.RekeyThreshold < minRekeyThreshold { |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 246 | c.RekeyThreshold = minRekeyThreshold |
Han-Wen Nienhuys | c78caca | 2017-04-04 17:27:40 +0200 | [diff] [blame] | 247 | } else if c.RekeyThreshold >= math.MaxInt64 { |
| 248 | // Avoid weirdness if somebody uses -1 as a threshold. |
| 249 | c.RekeyThreshold = math.MaxInt64 |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 250 | } |
Jonathan Pittman | 6a743c5 | 2013-09-09 13:06:04 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 253 | // buildDataSignedForAuth returns the data that is signed in order to prove |
Jonathan Pittman | 44256fa | 2013-10-10 11:35:15 -0400 | [diff] [blame] | 254 | // possession of a private key. See RFC 4252, section 7. |
Kevin Burke | e8f2298 | 2017-11-27 20:39:32 -0800 | [diff] [blame] | 255 | func buildDataSignedForAuth(sessionID []byte, req userAuthRequestMsg, algo, pubKey []byte) []byte { |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 256 | data := struct { |
| 257 | Session []byte |
| 258 | Type byte |
| 259 | User string |
| 260 | Service string |
| 261 | Method string |
| 262 | Sign bool |
| 263 | Algo []byte |
| 264 | PubKey []byte |
| 265 | }{ |
Kevin Burke | e8f2298 | 2017-11-27 20:39:32 -0800 | [diff] [blame] | 266 | sessionID, |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 267 | msgUserAuthRequest, |
| 268 | req.User, |
| 269 | req.Service, |
| 270 | req.Method, |
| 271 | true, |
| 272 | algo, |
| 273 | pubKey, |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 274 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 275 | return Marshal(data) |
Russ Cox | 470549d | 2012-01-25 15:31:12 -0500 | [diff] [blame] | 276 | } |
Dave Cheney | 79d53bd | 2012-03-04 14:34:24 -0800 | [diff] [blame] | 277 | |
| 278 | func appendU16(buf []byte, n uint16) []byte { |
| 279 | return append(buf, byte(n>>8), byte(n)) |
| 280 | } |
| 281 | |
| 282 | func appendU32(buf []byte, n uint32) []byte { |
| 283 | return append(buf, byte(n>>24), byte(n>>16), byte(n>>8), byte(n)) |
| 284 | } |
| 285 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 286 | func appendU64(buf []byte, n uint64) []byte { |
| 287 | return append(buf, |
| 288 | byte(n>>56), byte(n>>48), byte(n>>40), byte(n>>32), |
| 289 | byte(n>>24), byte(n>>16), byte(n>>8), byte(n)) |
| 290 | } |
| 291 | |
Dave Cheney | 79d53bd | 2012-03-04 14:34:24 -0800 | [diff] [blame] | 292 | func appendInt(buf []byte, n int) []byte { |
| 293 | return appendU32(buf, uint32(n)) |
| 294 | } |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 295 | |
Han-Wen Nienhuys | c7df565 | 2013-06-06 10:44:12 -0400 | [diff] [blame] | 296 | func appendString(buf []byte, s string) []byte { |
| 297 | buf = appendU32(buf, uint32(len(s))) |
| 298 | buf = append(buf, s...) |
| 299 | return buf |
| 300 | } |
| 301 | |
| 302 | func appendBool(buf []byte, b bool) []byte { |
| 303 | if b { |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 304 | return append(buf, 1) |
Han-Wen Nienhuys | c7df565 | 2013-06-06 10:44:12 -0400 | [diff] [blame] | 305 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 306 | return append(buf, 0) |
Han-Wen Nienhuys | c7df565 | 2013-06-06 10:44:12 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Adam Langley | 4002be2 | 2012-12-10 18:12:36 -0500 | [diff] [blame] | 309 | // newCond is a helper to hide the fact that there is no usable zero |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 310 | // value for sync.Cond. |
| 311 | func newCond() *sync.Cond { return sync.NewCond(new(sync.Mutex)) } |
| 312 | |
Adam Langley | 4002be2 | 2012-12-10 18:12:36 -0500 | [diff] [blame] | 313 | // window represents the buffer available to clients |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 314 | // wishing to write to a channel. |
| 315 | type window struct { |
| 316 | *sync.Cond |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 317 | win uint32 // RFC 4254 5.2 says the window size can grow to 2^32-1 |
| 318 | writeWaiters int |
| 319 | closed bool |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // add adds win to the amount of window available |
| 323 | // for consumers. |
| 324 | func (w *window) add(win uint32) bool { |
Dave Cheney | 55aa081 | 2012-05-22 12:04:51 +1000 | [diff] [blame] | 325 | // a zero sized window adjust is a noop. |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 326 | if win == 0 { |
Dave Cheney | 55aa081 | 2012-05-22 12:04:51 +1000 | [diff] [blame] | 327 | return true |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 328 | } |
| 329 | w.L.Lock() |
| 330 | if w.win+win < win { |
| 331 | w.L.Unlock() |
| 332 | return false |
| 333 | } |
| 334 | w.win += win |
| 335 | // It is unusual that multiple goroutines would be attempting to reserve |
Adam Langley | 4002be2 | 2012-12-10 18:12:36 -0500 | [diff] [blame] | 336 | // window space, but not guaranteed. Use broadcast to notify all waiters |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 337 | // that additional window is available. |
| 338 | w.Broadcast() |
| 339 | w.L.Unlock() |
| 340 | return true |
| 341 | } |
| 342 | |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 343 | // close sets the window to closed, so all reservations fail |
| 344 | // immediately. |
| 345 | func (w *window) close() { |
| 346 | w.L.Lock() |
| 347 | w.closed = true |
| 348 | w.Broadcast() |
| 349 | w.L.Unlock() |
| 350 | } |
| 351 | |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 352 | // reserve reserves win from the available window capacity. |
| 353 | // If no capacity remains, reserve will block. reserve may |
| 354 | // return less than requested. |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 355 | func (w *window) reserve(win uint32) (uint32, error) { |
| 356 | var err error |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 357 | w.L.Lock() |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 358 | w.writeWaiters++ |
| 359 | w.Broadcast() |
| 360 | for w.win == 0 && !w.closed { |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 361 | w.Wait() |
| 362 | } |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 363 | w.writeWaiters-- |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 364 | if w.win < win { |
| 365 | win = w.win |
| 366 | } |
| 367 | w.win -= win |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 368 | if w.closed { |
| 369 | err = io.EOF |
| 370 | } |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 371 | w.L.Unlock() |
Adam Langley | fa50e74 | 2014-04-09 13:57:52 -0700 | [diff] [blame] | 372 | return win, err |
| 373 | } |
| 374 | |
| 375 | // waitWriterBlocked waits until some goroutine is blocked for further |
| 376 | // writes. It is used in tests only. |
| 377 | func (w *window) waitWriterBlocked() { |
| 378 | w.Cond.L.Lock() |
| 379 | for w.writeWaiters == 0 { |
| 380 | w.Cond.Wait() |
| 381 | } |
| 382 | w.Cond.L.Unlock() |
Dave Cheney | 8a2e7c9 | 2012-05-11 05:56:44 +1000 | [diff] [blame] | 383 | } |