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