Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | // Copyright 2009 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 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 5 | package runner |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "bytes" |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 9 | "crypto" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 10 | "crypto/ecdsa" |
| 11 | "crypto/elliptic" |
| 12 | "crypto/rsa" |
| 13 | "crypto/subtle" |
| 14 | "crypto/x509" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 15 | "errors" |
| 16 | "fmt" |
| 17 | "io" |
| 18 | "math/big" |
| 19 | "net" |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 20 | "time" |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 21 | |
Robert Sloan | f573be7 | 2018-09-17 15:29:11 -0700 | [diff] [blame] | 22 | "boringssl.googlesource.com/boringssl/ssl/test/runner/ed25519" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | type clientHandshakeState struct { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 26 | c *Conn |
| 27 | serverHello *serverHelloMsg |
| 28 | hello *clientHelloMsg |
| 29 | suite *cipherSuite |
| 30 | finishedHash finishedHash |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 31 | keyShares map[CurveID]ecdhCurve |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 32 | masterSecret []byte |
| 33 | session *ClientSessionState |
| 34 | finishedBytes []byte |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 35 | peerPublicKey crypto.PublicKey |
| 36 | skxAlgo signatureAlgorithm |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 39 | func mapClientHelloVersion(vers uint16, isDTLS bool) uint16 { |
| 40 | if !isDTLS { |
| 41 | return vers |
| 42 | } |
| 43 | |
| 44 | switch vers { |
| 45 | case VersionTLS12: |
| 46 | return VersionDTLS12 |
| 47 | case VersionTLS10: |
| 48 | return VersionDTLS10 |
| 49 | } |
| 50 | |
| 51 | panic("Unknown ClientHello version.") |
| 52 | } |
| 53 | |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 54 | func fixClientHellos(hello *clientHelloMsg, in []byte) ([]byte, error) { |
| 55 | ret := append([]byte{}, in...) |
| 56 | newHello := new(clientHelloMsg) |
| 57 | if !newHello.unmarshal(ret) { |
| 58 | return nil, errors.New("tls: invalid ClientHello") |
| 59 | } |
| 60 | |
| 61 | hello.random = newHello.random |
| 62 | hello.sessionId = newHello.sessionId |
| 63 | |
| 64 | // Replace |ret|'s key shares with those of |hello|. For simplicity, we |
| 65 | // require their lengths match, which is satisfied by matching the |
| 66 | // DefaultCurves setting to the selection in the replacement |
| 67 | // ClientHello. |
| 68 | bb := newByteBuilder() |
| 69 | hello.marshalKeyShares(bb) |
| 70 | keyShares := bb.finish() |
| 71 | if len(keyShares) != len(newHello.keySharesRaw) { |
| 72 | return nil, errors.New("tls: ClientHello key share length is inconsistent with DefaultCurves setting") |
| 73 | } |
| 74 | // |newHello.keySharesRaw| aliases |ret|. |
| 75 | copy(newHello.keySharesRaw, keyShares) |
| 76 | |
| 77 | return ret, nil |
| 78 | } |
| 79 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 80 | func (c *Conn) clientHandshake() error { |
| 81 | if c.config == nil { |
| 82 | c.config = defaultConfig() |
| 83 | } |
| 84 | |
| 85 | if len(c.config.ServerName) == 0 && !c.config.InsecureSkipVerify { |
| 86 | return errors.New("tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config") |
| 87 | } |
| 88 | |
| 89 | c.sendHandshakeSeq = 0 |
| 90 | c.recvHandshakeSeq = 0 |
| 91 | |
| 92 | nextProtosLength := 0 |
| 93 | for _, proto := range c.config.NextProtos { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 94 | if l := len(proto); l > 255 { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 95 | return errors.New("tls: invalid NextProtos value") |
| 96 | } else { |
| 97 | nextProtosLength += 1 + l |
| 98 | } |
| 99 | } |
| 100 | if nextProtosLength > 0xffff { |
| 101 | return errors.New("tls: NextProtos values too large") |
| 102 | } |
| 103 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 104 | minVersion := c.config.minVersion(c.isDTLS) |
| 105 | maxVersion := c.config.maxVersion(c.isDTLS) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 106 | hello := &clientHelloMsg{ |
| 107 | isDTLS: c.isDTLS, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 108 | compressionMethods: []uint8{compressionNone}, |
| 109 | random: make([]byte, 32), |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 110 | ocspStapling: !c.config.Bugs.NoOCSPStapling, |
| 111 | sctListSupported: !c.config.Bugs.NoSignedCertificateTimestamps, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 112 | serverName: c.config.ServerName, |
| 113 | supportedCurves: c.config.curvePreferences(), |
| 114 | supportedPoints: []uint8{pointFormatUncompressed}, |
| 115 | nextProtoNeg: len(c.config.NextProtos) > 0, |
| 116 | secureRenegotiation: []byte{}, |
| 117 | alpnProtocols: c.config.NextProtos, |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 118 | quicTransportParams: c.config.QUICTransportParams, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 119 | duplicateExtension: c.config.Bugs.DuplicateExtension, |
| 120 | channelIDSupported: c.config.ChannelID != nil, |
Robert Sloan | 978112c | 2018-01-22 12:53:01 -0800 | [diff] [blame] | 121 | tokenBindingParams: c.config.TokenBindingParams, |
| 122 | tokenBindingVersion: c.config.TokenBindingVersion, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 123 | npnAfterAlpn: c.config.Bugs.SwapNPNAndALPN, |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 124 | extendedMasterSecret: maxVersion >= VersionTLS10, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 125 | srtpProtectionProfiles: c.config.SRTPProtectionProfiles, |
| 126 | srtpMasterKeyIdentifier: c.config.Bugs.SRTPMasterKeyIdentifer, |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 127 | customExtension: c.config.Bugs.CustomExtension, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 128 | pskBinderFirst: c.config.Bugs.PSKBinderFirst, |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 129 | omitExtensions: c.config.Bugs.OmitExtensions, |
| 130 | emptyExtensions: c.config.Bugs.EmptyExtensions, |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 131 | delegatedCredentials: !c.config.Bugs.DisableDelegatedCredentials, |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 132 | pqExperimentSignal: c.config.PQExperimentSignal, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 135 | if maxVersion >= VersionTLS13 { |
| 136 | hello.vers = mapClientHelloVersion(VersionTLS12, c.isDTLS) |
| 137 | if !c.config.Bugs.OmitSupportedVersions { |
| 138 | hello.supportedVersions = c.config.supportedVersions(c.isDTLS) |
| 139 | } |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 140 | hello.pskKEModes = []byte{pskDHEKEMode} |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 141 | } else { |
| 142 | hello.vers = mapClientHelloVersion(maxVersion, c.isDTLS) |
| 143 | } |
| 144 | |
| 145 | if c.config.Bugs.SendClientVersion != 0 { |
| 146 | hello.vers = c.config.Bugs.SendClientVersion |
| 147 | } |
| 148 | |
| 149 | if len(c.config.Bugs.SendSupportedVersions) > 0 { |
| 150 | hello.supportedVersions = c.config.Bugs.SendSupportedVersions |
| 151 | } |
| 152 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 153 | disableEMS := c.config.Bugs.NoExtendedMasterSecret |
| 154 | if c.cipherSuite != nil { |
| 155 | disableEMS = c.config.Bugs.NoExtendedMasterSecretOnRenegotiation |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 156 | } |
| 157 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 158 | if disableEMS { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 159 | hello.extendedMasterSecret = false |
| 160 | } |
| 161 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 162 | if c.config.Bugs.NoSupportedCurves { |
| 163 | hello.supportedCurves = nil |
| 164 | } |
| 165 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 166 | if len(c.config.Bugs.SendPSKKeyExchangeModes) != 0 { |
| 167 | hello.pskKEModes = c.config.Bugs.SendPSKKeyExchangeModes |
| 168 | } |
| 169 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 170 | if c.config.Bugs.SendCompressionMethods != nil { |
| 171 | hello.compressionMethods = c.config.Bugs.SendCompressionMethods |
| 172 | } |
| 173 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 174 | if c.config.Bugs.SendSupportedPointFormats != nil { |
| 175 | hello.supportedPoints = c.config.Bugs.SendSupportedPointFormats |
| 176 | } |
| 177 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 178 | if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo { |
| 179 | if c.config.Bugs.BadRenegotiationInfo { |
| 180 | hello.secureRenegotiation = append(hello.secureRenegotiation, c.clientVerify...) |
| 181 | hello.secureRenegotiation[0] ^= 0x80 |
| 182 | } else { |
| 183 | hello.secureRenegotiation = c.clientVerify |
| 184 | } |
| 185 | } |
| 186 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 187 | if c.config.Bugs.DuplicateCompressedCertAlgs { |
| 188 | hello.compressedCertAlgs = []uint16{1, 1} |
| 189 | } else if len(c.config.CertCompressionAlgs) > 0 { |
| 190 | hello.compressedCertAlgs = make([]uint16, 0, len(c.config.CertCompressionAlgs)) |
| 191 | for id, _ := range c.config.CertCompressionAlgs { |
| 192 | hello.compressedCertAlgs = append(hello.compressedCertAlgs, uint16(id)) |
| 193 | } |
| 194 | } |
| 195 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 196 | if c.noRenegotiationInfo() { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 197 | hello.secureRenegotiation = nil |
| 198 | } |
| 199 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 200 | var keyShares map[CurveID]ecdhCurve |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 201 | if maxVersion >= VersionTLS13 { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 202 | keyShares = make(map[CurveID]ecdhCurve) |
| 203 | hello.hasKeyShares = true |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 204 | hello.trailingKeyShareData = c.config.Bugs.TrailingKeyShareData |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 205 | curvesToSend := c.config.defaultCurves() |
| 206 | for _, curveID := range hello.supportedCurves { |
| 207 | if !curvesToSend[curveID] { |
| 208 | continue |
| 209 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame] | 210 | curve, ok := curveForCurveID(curveID, c.config) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 211 | if !ok { |
| 212 | continue |
| 213 | } |
| 214 | publicKey, err := curve.offer(c.config.rand()) |
| 215 | if err != nil { |
| 216 | return err |
| 217 | } |
| 218 | |
| 219 | if c.config.Bugs.SendCurve != 0 { |
| 220 | curveID = c.config.Bugs.SendCurve |
| 221 | } |
| 222 | if c.config.Bugs.InvalidECDHPoint { |
| 223 | publicKey[0] ^= 0xff |
| 224 | } |
| 225 | |
| 226 | hello.keyShares = append(hello.keyShares, keyShareEntry{ |
| 227 | group: curveID, |
| 228 | keyExchange: publicKey, |
| 229 | }) |
| 230 | keyShares[curveID] = curve |
| 231 | |
| 232 | if c.config.Bugs.DuplicateKeyShares { |
| 233 | hello.keyShares = append(hello.keyShares, hello.keyShares[len(hello.keyShares)-1]) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if c.config.Bugs.MissingKeyShare { |
| 238 | hello.hasKeyShares = false |
| 239 | } |
| 240 | } |
| 241 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 242 | possibleCipherSuites := c.config.cipherSuites() |
| 243 | hello.cipherSuites = make([]uint16, 0, len(possibleCipherSuites)) |
| 244 | |
| 245 | NextCipherSuite: |
| 246 | for _, suiteId := range possibleCipherSuites { |
| 247 | for _, suite := range cipherSuites { |
| 248 | if suite.id != suiteId { |
| 249 | continue |
| 250 | } |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 251 | // Don't advertise TLS 1.2-only cipher suites unless |
| 252 | // we're attempting TLS 1.2. |
| 253 | if maxVersion < VersionTLS12 && suite.flags&suiteTLS12 != 0 { |
| 254 | continue |
| 255 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 256 | hello.cipherSuites = append(hello.cipherSuites, suiteId) |
| 257 | continue NextCipherSuite |
| 258 | } |
| 259 | } |
| 260 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 261 | if c.config.Bugs.AdvertiseAllConfiguredCiphers { |
| 262 | hello.cipherSuites = possibleCipherSuites |
| 263 | } |
| 264 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 265 | if c.config.Bugs.SendRenegotiationSCSV { |
| 266 | hello.cipherSuites = append(hello.cipherSuites, renegotiationSCSV) |
| 267 | } |
| 268 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 269 | if c.config.Bugs.SendFallbackSCSV { |
| 270 | hello.cipherSuites = append(hello.cipherSuites, fallbackSCSV) |
| 271 | } |
| 272 | |
| 273 | _, err := io.ReadFull(c.config.rand(), hello.random) |
| 274 | if err != nil { |
| 275 | c.sendAlert(alertInternalError) |
| 276 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 277 | } |
| 278 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 279 | if maxVersion >= VersionTLS12 && !c.config.Bugs.NoSignatureAlgorithms { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 280 | hello.signatureAlgorithms = c.config.verifySignatureAlgorithms() |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | var session *ClientSessionState |
| 284 | var cacheKey string |
| 285 | sessionCache := c.config.ClientSessionCache |
| 286 | |
| 287 | if sessionCache != nil { |
| 288 | hello.ticketSupported = !c.config.SessionTicketsDisabled |
| 289 | |
| 290 | // Try to resume a previously negotiated TLS session, if |
| 291 | // available. |
| 292 | cacheKey = clientSessionCacheKey(c.conn.RemoteAddr(), c.config) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 293 | // TODO(nharper): Support storing more than one session |
| 294 | // ticket for TLS 1.3. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 295 | candidateSession, ok := sessionCache.Get(cacheKey) |
| 296 | if ok { |
| 297 | ticketOk := !c.config.SessionTicketsDisabled || candidateSession.sessionTicket == nil |
| 298 | |
| 299 | // Check that the ciphersuite/version used for the |
| 300 | // previous session are still valid. |
| 301 | cipherSuiteOk := false |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 302 | if candidateSession.vers <= VersionTLS12 { |
| 303 | for _, id := range hello.cipherSuites { |
| 304 | if id == candidateSession.cipherSuite { |
| 305 | cipherSuiteOk = true |
| 306 | break |
| 307 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 308 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 309 | } else { |
| 310 | // TLS 1.3 allows the cipher to change on |
| 311 | // resumption. |
| 312 | cipherSuiteOk = true |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 315 | versOk := candidateSession.vers >= minVersion && |
| 316 | candidateSession.vers <= maxVersion |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 317 | if ticketOk && versOk && cipherSuiteOk { |
| 318 | session = candidateSession |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 323 | var pskCipherSuite *cipherSuite |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 324 | if session != nil && c.config.time().Before(session.ticketExpiration) { |
| 325 | ticket := session.sessionTicket |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 326 | if c.config.Bugs.FilterTicket != nil && len(ticket) > 0 { |
| 327 | // Copy the ticket so FilterTicket may act in-place. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 328 | ticket = make([]byte, len(session.sessionTicket)) |
| 329 | copy(ticket, session.sessionTicket) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 330 | |
| 331 | ticket, err = c.config.Bugs.FilterTicket(ticket) |
| 332 | if err != nil { |
| 333 | return err |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 334 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 335 | } |
| 336 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 337 | if session.vers >= VersionTLS13 || c.config.Bugs.SendBothTickets { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 338 | pskCipherSuite = cipherSuiteFromID(session.cipherSuite) |
| 339 | if pskCipherSuite == nil { |
| 340 | return errors.New("tls: client session cache has invalid cipher suite") |
| 341 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 342 | // TODO(nharper): Support sending more |
| 343 | // than one PSK identity. |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 344 | ticketAge := uint32(c.config.time().Sub(session.ticketCreationTime) / time.Millisecond) |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 345 | if c.config.Bugs.SendTicketAge != 0 { |
| 346 | ticketAge = uint32(c.config.Bugs.SendTicketAge / time.Millisecond) |
| 347 | } |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 348 | psk := pskIdentity{ |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 349 | ticket: ticket, |
| 350 | obfuscatedTicketAge: session.ticketAgeAdd + ticketAge, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 351 | } |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 352 | hello.pskIdentities = []pskIdentity{psk} |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 353 | |
| 354 | if c.config.Bugs.ExtraPSKIdentity { |
| 355 | hello.pskIdentities = append(hello.pskIdentities, psk) |
| 356 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 357 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 358 | |
| 359 | if session.vers < VersionTLS13 || c.config.Bugs.SendBothTickets { |
| 360 | if ticket != nil { |
| 361 | hello.sessionTicket = ticket |
| 362 | // A random session ID is used to detect when the |
| 363 | // server accepted the ticket and is resuming a session |
| 364 | // (see RFC 5077). |
| 365 | sessionIdLen := 16 |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 366 | if c.config.Bugs.TicketSessionIDLength != 0 { |
| 367 | sessionIdLen = c.config.Bugs.TicketSessionIDLength |
| 368 | } |
| 369 | if c.config.Bugs.EmptyTicketSessionID { |
| 370 | sessionIdLen = 0 |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 371 | } |
| 372 | hello.sessionId = make([]byte, sessionIdLen) |
| 373 | if _, err := io.ReadFull(c.config.rand(), hello.sessionId); err != nil { |
| 374 | c.sendAlert(alertInternalError) |
| 375 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 376 | } |
| 377 | } else { |
| 378 | hello.sessionId = session.sessionId |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame] | 383 | // Request compatibility mode from the client by sending a fake session |
| 384 | // ID. Although BoringSSL always enables compatibility mode, other |
| 385 | // implementations make it conditional on the ClientHello. We test |
| 386 | // BoringSSL's expected behavior with SendClientHelloSessionID. |
| 387 | if len(hello.sessionId) == 0 && maxVersion >= VersionTLS13 { |
| 388 | hello.sessionId = make([]byte, 32) |
| 389 | if _, err := io.ReadFull(c.config.rand(), hello.sessionId); err != nil { |
| 390 | c.sendAlert(alertInternalError) |
| 391 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 392 | } |
| 393 | } |
| 394 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 395 | if c.config.Bugs.SendCipherSuites != nil { |
| 396 | hello.cipherSuites = c.config.Bugs.SendCipherSuites |
| 397 | } |
| 398 | |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 399 | var sendEarlyData bool |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 400 | if len(hello.pskIdentities) > 0 && c.config.Bugs.SendEarlyData != nil { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 401 | hello.hasEarlyData = true |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 402 | sendEarlyData = true |
| 403 | } |
| 404 | if c.config.Bugs.SendFakeEarlyDataLength > 0 { |
| 405 | hello.hasEarlyData = true |
| 406 | } |
| 407 | if c.config.Bugs.OmitEarlyDataExtension { |
| 408 | hello.hasEarlyData = false |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 409 | } |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 410 | if c.config.Bugs.SendClientHelloSessionID != nil { |
| 411 | hello.sessionId = c.config.Bugs.SendClientHelloSessionID |
| 412 | } |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 413 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 414 | var helloBytes []byte |
| 415 | if c.config.Bugs.SendV2ClientHello { |
| 416 | // Test that the peer left-pads random. |
| 417 | hello.random[0] = 0 |
| 418 | v2Hello := &v2ClientHelloMsg{ |
| 419 | vers: hello.vers, |
| 420 | cipherSuites: hello.cipherSuites, |
| 421 | // No session resumption for V2ClientHello. |
| 422 | sessionId: nil, |
| 423 | challenge: hello.random[1:], |
| 424 | } |
| 425 | helloBytes = v2Hello.marshal() |
| 426 | c.writeV2Record(helloBytes) |
| 427 | } else { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 428 | if len(hello.pskIdentities) > 0 { |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 429 | version := session.wireVersion |
| 430 | // We may have a pre-1.3 session if SendBothTickets is |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 431 | // set. |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 432 | if session.vers < VersionTLS13 { |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 433 | version = VersionTLS13 |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 434 | } |
| 435 | generatePSKBinders(version, hello, pskCipherSuite, session.masterSecret, []byte{}, []byte{}, c.config) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 436 | } |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 437 | if c.config.Bugs.SendClientHelloWithFixes != nil { |
| 438 | helloBytes, err = fixClientHellos(hello, c.config.Bugs.SendClientHelloWithFixes) |
| 439 | if err != nil { |
| 440 | return err |
| 441 | } |
| 442 | } else { |
| 443 | helloBytes = hello.marshal() |
| 444 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 445 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 446 | if c.config.Bugs.PartialClientFinishedWithClientHello { |
| 447 | // Include one byte of Finished. We can compute it |
| 448 | // without completing the handshake. This assumes we |
| 449 | // negotiate TLS 1.3 with no HelloRetryRequest or |
| 450 | // CertificateRequest. |
| 451 | toWrite := make([]byte, 0, len(helloBytes)+1) |
| 452 | toWrite = append(toWrite, helloBytes...) |
| 453 | toWrite = append(toWrite, typeFinished) |
| 454 | c.writeRecord(recordTypeHandshake, toWrite) |
| 455 | } else { |
| 456 | c.writeRecord(recordTypeHandshake, helloBytes) |
| 457 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 458 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 459 | c.flushHandshake() |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 460 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 461 | if err := c.simulatePacketLoss(nil); err != nil { |
| 462 | return err |
| 463 | } |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 464 | if c.config.Bugs.SendEarlyAlert { |
| 465 | c.sendAlert(alertHandshakeFailure) |
| 466 | } |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 467 | if c.config.Bugs.SendFakeEarlyDataLength > 0 { |
| 468 | c.sendFakeEarlyData(c.config.Bugs.SendFakeEarlyDataLength) |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 469 | } |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 470 | |
| 471 | // Derive early write keys and set Conn state to allow early writes. |
| 472 | if sendEarlyData { |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 473 | finishedHash := newFinishedHash(session.wireVersion, c.isDTLS, pskCipherSuite) |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 474 | finishedHash.addEntropy(session.masterSecret) |
| 475 | finishedHash.Write(helloBytes) |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 476 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 477 | if !c.config.Bugs.SkipChangeCipherSpec { |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 478 | c.wireVersion = session.wireVersion |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 479 | c.vers = VersionTLS13 |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 480 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 481 | c.wireVersion = 0 |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 482 | c.vers = 0 |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 485 | earlyTrafficSecret := finishedHash.deriveSecret(earlyTrafficLabel) |
| 486 | c.earlyExporterSecret = finishedHash.deriveSecret(earlyExporterLabel) |
Robert Sloan | 5581810 | 2017-12-18 11:26:17 -0800 | [diff] [blame] | 487 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 488 | c.useOutTrafficSecret(session.wireVersion, pskCipherSuite, earlyTrafficSecret) |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 489 | for _, earlyData := range c.config.Bugs.SendEarlyData { |
| 490 | if _, err := c.writeRecord(recordTypeApplicationData, earlyData); err != nil { |
| 491 | return err |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 496 | msg, err := c.readHandshake() |
| 497 | if err != nil { |
| 498 | return err |
| 499 | } |
| 500 | |
| 501 | if c.isDTLS { |
| 502 | helloVerifyRequest, ok := msg.(*helloVerifyRequestMsg) |
| 503 | if ok { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 504 | if helloVerifyRequest.vers != VersionDTLS10 { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 505 | // Per RFC 6347, the version field in |
| 506 | // HelloVerifyRequest SHOULD be always DTLS |
| 507 | // 1.0. Enforce this for testing purposes. |
| 508 | return errors.New("dtls: bad HelloVerifyRequest version") |
| 509 | } |
| 510 | |
| 511 | hello.raw = nil |
| 512 | hello.cookie = helloVerifyRequest.cookie |
| 513 | helloBytes = hello.marshal() |
| 514 | c.writeRecord(recordTypeHandshake, helloBytes) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 515 | c.flushHandshake() |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 516 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 517 | if err := c.simulatePacketLoss(nil); err != nil { |
| 518 | return err |
| 519 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 520 | msg, err = c.readHandshake() |
| 521 | if err != nil { |
| 522 | return err |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 527 | var serverWireVersion uint16 |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 528 | switch m := msg.(type) { |
| 529 | case *helloRetryRequestMsg: |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 530 | serverWireVersion = m.vers |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 531 | case *serverHelloMsg: |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 532 | serverWireVersion = m.vers |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 533 | default: |
| 534 | c.sendAlert(alertUnexpectedMessage) |
| 535 | return fmt.Errorf("tls: received unexpected message of type %T when waiting for HelloRetryRequest or ServerHello", msg) |
| 536 | } |
| 537 | |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 538 | serverVersion, ok := c.config.isSupportedVersion(serverWireVersion, c.isDTLS) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 539 | if !ok { |
| 540 | c.sendAlert(alertProtocolVersion) |
| 541 | return fmt.Errorf("tls: server selected unsupported protocol version %x", c.vers) |
| 542 | } |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 543 | c.wireVersion = serverWireVersion |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 544 | c.vers = serverVersion |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 545 | c.haveVers = true |
| 546 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 547 | if c.vers >= VersionTLS13 { |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 548 | // The first server message must be followed by a ChangeCipherSpec. |
| 549 | c.expectTLS13ChangeCipherSpec = true |
| 550 | } |
| 551 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 552 | helloRetryRequest, haveHelloRetryRequest := msg.(*helloRetryRequestMsg) |
| 553 | var secondHelloBytes []byte |
| 554 | if haveHelloRetryRequest { |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 555 | if c.config.Bugs.FailIfHelloRetryRequested { |
| 556 | return errors.New("tls: unexpected HelloRetryRequest") |
| 557 | } |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 558 | // Explicitly read the ChangeCipherSpec now; it should |
| 559 | // be attached to the first flight, not the second flight. |
| 560 | if err := c.readTLS13ChangeCipherSpec(); err != nil { |
| 561 | return err |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 564 | c.out.resetCipher() |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 565 | if len(helloRetryRequest.cookie) > 0 { |
| 566 | hello.tls13Cookie = helloRetryRequest.cookie |
| 567 | } |
| 568 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 569 | if c.config.Bugs.MisinterpretHelloRetryRequestCurve != 0 { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 570 | helloRetryRequest.hasSelectedGroup = true |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 571 | helloRetryRequest.selectedGroup = c.config.Bugs.MisinterpretHelloRetryRequestCurve |
| 572 | } |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 573 | if helloRetryRequest.hasSelectedGroup { |
| 574 | var hrrCurveFound bool |
| 575 | group := helloRetryRequest.selectedGroup |
| 576 | for _, curveID := range hello.supportedCurves { |
| 577 | if group == curveID { |
| 578 | hrrCurveFound = true |
| 579 | break |
| 580 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 581 | } |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 582 | if !hrrCurveFound || keyShares[group] != nil { |
| 583 | c.sendAlert(alertHandshakeFailure) |
| 584 | return errors.New("tls: received invalid HelloRetryRequest") |
| 585 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame] | 586 | curve, ok := curveForCurveID(group, c.config) |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 587 | if !ok { |
| 588 | return errors.New("tls: Unable to get curve requested in HelloRetryRequest") |
| 589 | } |
| 590 | publicKey, err := curve.offer(c.config.rand()) |
| 591 | if err != nil { |
| 592 | return err |
| 593 | } |
| 594 | keyShares[group] = curve |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 595 | hello.keyShares = []keyShareEntry{{ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 596 | group: group, |
| 597 | keyExchange: publicKey, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 598 | }} |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 599 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 600 | |
| 601 | if c.config.Bugs.SecondClientHelloMissingKeyShare { |
| 602 | hello.hasKeyShares = false |
| 603 | } |
| 604 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 605 | hello.hasEarlyData = c.config.Bugs.SendEarlyDataOnSecondClientHello |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 606 | hello.raw = nil |
| 607 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 608 | if len(hello.pskIdentities) > 0 { |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 609 | generatePSKBinders(c.wireVersion, hello, pskCipherSuite, session.masterSecret, helloBytes, helloRetryRequest.marshal(), c.config) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 610 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 611 | secondHelloBytes = hello.marshal() |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 612 | |
| 613 | if c.config.Bugs.InterleaveEarlyData { |
| 614 | c.sendFakeEarlyData(4) |
| 615 | c.writeRecord(recordTypeHandshake, secondHelloBytes[:16]) |
| 616 | c.sendFakeEarlyData(4) |
| 617 | c.writeRecord(recordTypeHandshake, secondHelloBytes[16:]) |
| 618 | } else { |
| 619 | c.writeRecord(recordTypeHandshake, secondHelloBytes) |
| 620 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 621 | c.flushHandshake() |
| 622 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 623 | if c.config.Bugs.SendEarlyDataOnSecondClientHello { |
| 624 | c.sendFakeEarlyData(4) |
| 625 | } |
| 626 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 627 | msg, err = c.readHandshake() |
| 628 | if err != nil { |
| 629 | return err |
| 630 | } |
| 631 | } |
| 632 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 633 | serverHello, ok := msg.(*serverHelloMsg) |
| 634 | if !ok { |
| 635 | c.sendAlert(alertUnexpectedMessage) |
| 636 | return unexpectedMessageError(serverHello, msg) |
| 637 | } |
| 638 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 639 | if serverWireVersion != serverHello.vers { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 640 | c.sendAlert(alertIllegalParameter) |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 641 | return fmt.Errorf("tls: server sent non-matching version %x vs %x", serverWireVersion, serverHello.vers) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 642 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 643 | |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 644 | _, supportsTLS13 := c.config.isSupportedVersion(VersionTLS13, false) |
| 645 | // Check for downgrade signals in the server random, per RFC 8446, section 4.1.3. |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 646 | gotDowngrade := serverHello.random[len(serverHello.random)-8:] |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 647 | if (supportsTLS13 || c.config.Bugs.CheckTLS13DowngradeRandom) && !c.config.Bugs.IgnoreTLS13DowngradeRandom { |
| 648 | if c.vers <= VersionTLS12 && c.config.maxVersion(c.isDTLS) >= VersionTLS13 { |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 649 | if bytes.Equal(gotDowngrade, downgradeTLS13) { |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 650 | c.sendAlert(alertProtocolVersion) |
| 651 | return errors.New("tls: downgrade from TLS 1.3 detected") |
| 652 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 653 | } |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 654 | if c.vers <= VersionTLS11 && c.config.maxVersion(c.isDTLS) >= VersionTLS12 { |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 655 | if bytes.Equal(gotDowngrade, downgradeTLS12) { |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 656 | c.sendAlert(alertProtocolVersion) |
| 657 | return errors.New("tls: downgrade from TLS 1.2 detected") |
| 658 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 662 | if bytes.Equal(gotDowngrade, downgradeJDK11) != c.config.Bugs.ExpectJDK11DowngradeRandom { |
| 663 | c.sendAlert(alertProtocolVersion) |
| 664 | if c.config.Bugs.ExpectJDK11DowngradeRandom { |
| 665 | return errors.New("tls: server did not send a JDK 11 downgrade signal") |
| 666 | } |
| 667 | return errors.New("tls: server sent an unexpected JDK 11 downgrade signal") |
| 668 | } |
| 669 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 670 | suite := mutualCipherSuite(hello.cipherSuites, serverHello.cipherSuite) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 671 | if suite == nil { |
| 672 | c.sendAlert(alertHandshakeFailure) |
| 673 | return fmt.Errorf("tls: server selected an unsupported cipher suite") |
| 674 | } |
| 675 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 676 | if haveHelloRetryRequest && helloRetryRequest.hasSelectedGroup && helloRetryRequest.selectedGroup != serverHello.keyShare.group { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 677 | c.sendAlert(alertHandshakeFailure) |
| 678 | return errors.New("tls: ServerHello parameters did not match HelloRetryRequest") |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 681 | if c.config.Bugs.ExpectOmitExtensions && !serverHello.omitExtensions { |
| 682 | return errors.New("tls: ServerHello did not omit extensions") |
| 683 | } |
| 684 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 685 | hs := &clientHandshakeState{ |
| 686 | c: c, |
| 687 | serverHello: serverHello, |
| 688 | hello: hello, |
| 689 | suite: suite, |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 690 | finishedHash: newFinishedHash(c.wireVersion, c.isDTLS, suite), |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 691 | keyShares: keyShares, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 692 | session: session, |
| 693 | } |
| 694 | |
| 695 | hs.writeHash(helloBytes, hs.c.sendHandshakeSeq-1) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 696 | if haveHelloRetryRequest { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 697 | err = hs.finishedHash.UpdateForHelloRetryRequest() |
| 698 | if err != nil { |
| 699 | return err |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 700 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 701 | hs.writeServerHash(helloRetryRequest.marshal()) |
| 702 | hs.writeClientHash(secondHelloBytes) |
| 703 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 704 | hs.writeServerHash(hs.serverHello.marshal()) |
| 705 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 706 | if c.vers >= VersionTLS13 { |
| 707 | if err := hs.doTLS13Handshake(); err != nil { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 708 | return err |
| 709 | } |
| 710 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 711 | if c.config.Bugs.EarlyChangeCipherSpec > 0 { |
| 712 | hs.establishKeys() |
| 713 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 714 | } |
| 715 | |
| 716 | if hs.serverHello.compressionMethod != compressionNone { |
| 717 | c.sendAlert(alertUnexpectedMessage) |
| 718 | return errors.New("tls: server selected unsupported compression format") |
| 719 | } |
| 720 | |
| 721 | err = hs.processServerExtensions(&serverHello.extensions) |
| 722 | if err != nil { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 723 | return err |
| 724 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 725 | |
| 726 | isResume, err := hs.processServerHello() |
| 727 | if err != nil { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 728 | return err |
| 729 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 730 | |
| 731 | if isResume { |
| 732 | if c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 733 | if err := hs.establishKeys(); err != nil { |
| 734 | return err |
| 735 | } |
| 736 | } |
| 737 | if err := hs.readSessionTicket(); err != nil { |
| 738 | return err |
| 739 | } |
| 740 | if err := hs.readFinished(c.firstFinished[:]); err != nil { |
| 741 | return err |
| 742 | } |
| 743 | if err := hs.sendFinished(nil, isResume); err != nil { |
| 744 | return err |
| 745 | } |
| 746 | } else { |
| 747 | if err := hs.doFullHandshake(); err != nil { |
| 748 | return err |
| 749 | } |
| 750 | if err := hs.establishKeys(); err != nil { |
| 751 | return err |
| 752 | } |
| 753 | if err := hs.sendFinished(c.firstFinished[:], isResume); err != nil { |
| 754 | return err |
| 755 | } |
| 756 | // Most retransmits are triggered by a timeout, but the final |
| 757 | // leg of the handshake is retransmited upon re-receiving a |
| 758 | // Finished. |
| 759 | if err := c.simulatePacketLoss(func() { |
| 760 | c.sendHandshakeSeq-- |
| 761 | c.writeRecord(recordTypeHandshake, hs.finishedBytes) |
| 762 | c.flushHandshake() |
| 763 | }); err != nil { |
| 764 | return err |
| 765 | } |
| 766 | if err := hs.readSessionTicket(); err != nil { |
| 767 | return err |
| 768 | } |
| 769 | if err := hs.readFinished(nil); err != nil { |
| 770 | return err |
| 771 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 772 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 773 | |
| 774 | if sessionCache != nil && hs.session != nil && session != hs.session { |
| 775 | if c.config.Bugs.RequireSessionTickets && len(hs.session.sessionTicket) == 0 { |
| 776 | return errors.New("tls: new session used session IDs instead of tickets") |
| 777 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 778 | if c.config.Bugs.RequireSessionIDs && len(hs.session.sessionId) == 0 { |
| 779 | return errors.New("tls: new session used session tickets instead of IDs") |
| 780 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 781 | sessionCache.Put(cacheKey, hs.session) |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 782 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 783 | |
| 784 | c.didResume = isResume |
| 785 | c.exporterSecret = hs.masterSecret |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 788 | c.handshakeComplete = true |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 789 | c.cipherSuite = suite |
| 790 | copy(c.clientRandom[:], hs.hello.random) |
| 791 | copy(c.serverRandom[:], hs.serverHello.random) |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 792 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 793 | return nil |
| 794 | } |
| 795 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 796 | func (hs *clientHandshakeState) doTLS13Handshake() error { |
| 797 | c := hs.c |
| 798 | |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 799 | if !bytes.Equal(hs.hello.sessionId, hs.serverHello.sessionId) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 800 | return errors.New("tls: session IDs did not match.") |
| 801 | } |
| 802 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 803 | // Once the PRF hash is known, TLS 1.3 does not require a handshake |
| 804 | // buffer. |
| 805 | hs.finishedHash.discardHandshakeBuffer() |
| 806 | |
| 807 | zeroSecret := hs.finishedHash.zeroSecret() |
| 808 | |
| 809 | // Resolve PSK and compute the early secret. |
| 810 | // |
| 811 | // TODO(davidben): This will need to be handled slightly earlier once |
| 812 | // 0-RTT is implemented. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 813 | if hs.serverHello.hasPSKIdentity { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 814 | // We send at most one PSK identity. |
| 815 | if hs.session == nil || hs.serverHello.pskIdentity != 0 { |
| 816 | c.sendAlert(alertUnknownPSKIdentity) |
| 817 | return errors.New("tls: server sent unknown PSK identity") |
| 818 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 819 | sessionCipher := cipherSuiteFromID(hs.session.cipherSuite) |
| 820 | if sessionCipher == nil || sessionCipher.hash() != hs.suite.hash() { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 821 | c.sendAlert(alertHandshakeFailure) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 822 | return errors.New("tls: server resumed an invalid session for the cipher suite") |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 823 | } |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 824 | hs.finishedHash.addEntropy(hs.session.masterSecret) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 825 | c.didResume = true |
| 826 | } else { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 827 | hs.finishedHash.addEntropy(zeroSecret) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 828 | } |
| 829 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 830 | if !hs.serverHello.hasKeyShare { |
| 831 | c.sendAlert(alertUnsupportedExtension) |
| 832 | return errors.New("tls: server omitted KeyShare on resumption.") |
| 833 | } |
| 834 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 835 | // Resolve ECDHE and compute the handshake secret. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 836 | if !c.config.Bugs.MissingKeyShare && !c.config.Bugs.SecondClientHelloMissingKeyShare { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 837 | curve, ok := hs.keyShares[hs.serverHello.keyShare.group] |
| 838 | if !ok { |
| 839 | c.sendAlert(alertHandshakeFailure) |
| 840 | return errors.New("tls: server selected an unsupported group") |
| 841 | } |
| 842 | c.curveID = hs.serverHello.keyShare.group |
| 843 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 844 | ecdheSecret, err := curve.finish(hs.serverHello.keyShare.keyExchange) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 845 | if err != nil { |
| 846 | return err |
| 847 | } |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 848 | hs.finishedHash.nextSecret() |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 849 | hs.finishedHash.addEntropy(ecdheSecret) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 850 | } else { |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 851 | hs.finishedHash.nextSecret() |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 852 | hs.finishedHash.addEntropy(zeroSecret) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 853 | } |
| 854 | |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 855 | // Derive handshake traffic keys and switch read key to handshake |
| 856 | // traffic key. |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 857 | clientHandshakeTrafficSecret := hs.finishedHash.deriveSecret(clientHandshakeTrafficLabel) |
| 858 | serverHandshakeTrafficSecret := hs.finishedHash.deriveSecret(serverHandshakeTrafficLabel) |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 859 | if err := c.useInTrafficSecret(c.wireVersion, hs.suite, serverHandshakeTrafficSecret); err != nil { |
| 860 | return err |
| 861 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 862 | |
| 863 | msg, err := c.readHandshake() |
| 864 | if err != nil { |
| 865 | return err |
| 866 | } |
| 867 | |
| 868 | encryptedExtensions, ok := msg.(*encryptedExtensionsMsg) |
| 869 | if !ok { |
| 870 | c.sendAlert(alertUnexpectedMessage) |
| 871 | return unexpectedMessageError(encryptedExtensions, msg) |
| 872 | } |
| 873 | hs.writeServerHash(encryptedExtensions.marshal()) |
| 874 | |
| 875 | err = hs.processServerExtensions(&encryptedExtensions.extensions) |
| 876 | if err != nil { |
| 877 | return err |
| 878 | } |
| 879 | |
| 880 | var chainToSend *Certificate |
| 881 | var certReq *certificateRequestMsg |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 882 | if c.didResume { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 883 | // Copy over authentication from the session. |
| 884 | c.peerCertificates = hs.session.serverCertificates |
| 885 | c.sctList = hs.session.sctList |
| 886 | c.ocspResponse = hs.session.ocspResponse |
| 887 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 888 | msg, err := c.readHandshake() |
| 889 | if err != nil { |
| 890 | return err |
| 891 | } |
| 892 | |
| 893 | var ok bool |
| 894 | certReq, ok = msg.(*certificateRequestMsg) |
| 895 | if ok { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 896 | if len(certReq.requestContext) != 0 { |
| 897 | return errors.New("tls: non-empty certificate request context sent in handshake") |
| 898 | } |
| 899 | |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 900 | if c.config.Bugs.ExpectNoCertificateAuthoritiesExtension && certReq.hasCAExtension { |
| 901 | return errors.New("tls: expected no certificate_authorities extension") |
| 902 | } |
| 903 | |
Robert Sloan | 5cbb5c8 | 2018-04-24 11:35:46 -0700 | [diff] [blame] | 904 | if err := checkRSAPSSSupport(c.config.Bugs.ExpectRSAPSSSupport, certReq.signatureAlgorithms, certReq.signatureAlgorithmsCert); err != nil { |
| 905 | return err |
| 906 | } |
| 907 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 908 | if c.config.Bugs.IgnorePeerSignatureAlgorithmPreferences { |
| 909 | certReq.signatureAlgorithms = c.config.signSignatureAlgorithms() |
| 910 | } |
| 911 | |
| 912 | hs.writeServerHash(certReq.marshal()) |
| 913 | |
| 914 | chainToSend, err = selectClientCertificate(c, certReq) |
| 915 | if err != nil { |
| 916 | return err |
| 917 | } |
| 918 | |
| 919 | msg, err = c.readHandshake() |
| 920 | if err != nil { |
| 921 | return err |
| 922 | } |
| 923 | } |
| 924 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 925 | var certMsg *certificateMsg |
| 926 | |
| 927 | if compressedCertMsg, ok := msg.(*compressedCertificateMsg); ok { |
| 928 | hs.writeServerHash(compressedCertMsg.marshal()) |
| 929 | |
| 930 | alg, ok := c.config.CertCompressionAlgs[compressedCertMsg.algID] |
| 931 | if !ok { |
| 932 | c.sendAlert(alertBadCertificate) |
| 933 | return fmt.Errorf("tls: received certificate compressed with unknown algorithm %x", compressedCertMsg.algID) |
| 934 | } |
| 935 | |
| 936 | decompressed := make([]byte, 4+int(compressedCertMsg.uncompressedLength)) |
| 937 | if !alg.Decompress(decompressed[4:], compressedCertMsg.compressed) { |
| 938 | c.sendAlert(alertBadCertificate) |
| 939 | return fmt.Errorf("tls: failed to decompress certificate with algorithm %x", compressedCertMsg.algID) |
| 940 | } |
| 941 | |
| 942 | certMsg = &certificateMsg{ |
| 943 | hasRequestContext: true, |
| 944 | } |
| 945 | |
| 946 | if !certMsg.unmarshal(decompressed) { |
| 947 | c.sendAlert(alertBadCertificate) |
| 948 | return errors.New("tls: failed to parse decompressed certificate") |
| 949 | } |
| 950 | |
| 951 | if expected := c.config.Bugs.ExpectedCompressedCert; expected != 0 && expected != compressedCertMsg.algID { |
| 952 | return fmt.Errorf("tls: expected certificate compressed with algorithm %x, but message used %x", expected, compressedCertMsg.algID) |
| 953 | } |
| 954 | } else { |
| 955 | if certMsg, ok = msg.(*certificateMsg); !ok { |
| 956 | c.sendAlert(alertUnexpectedMessage) |
| 957 | return unexpectedMessageError(certMsg, msg) |
| 958 | } |
| 959 | hs.writeServerHash(certMsg.marshal()) |
| 960 | |
| 961 | if c.config.Bugs.ExpectedCompressedCert != 0 { |
| 962 | return errors.New("tls: uncompressed certificate received") |
| 963 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 964 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 965 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 966 | // Check for unsolicited extensions. |
| 967 | for i, cert := range certMsg.certificates { |
| 968 | if c.config.Bugs.NoOCSPStapling && cert.ocspResponse != nil { |
| 969 | c.sendAlert(alertUnsupportedExtension) |
| 970 | return errors.New("tls: unexpected OCSP response in the server certificate") |
| 971 | } |
| 972 | if c.config.Bugs.NoSignedCertificateTimestamps && cert.sctList != nil { |
| 973 | c.sendAlert(alertUnsupportedExtension) |
| 974 | return errors.New("tls: unexpected SCT list in the server certificate") |
| 975 | } |
| 976 | if i > 0 && c.config.Bugs.ExpectNoExtensionsOnIntermediate && (cert.ocspResponse != nil || cert.sctList != nil) { |
| 977 | c.sendAlert(alertUnsupportedExtension) |
| 978 | return errors.New("tls: unexpected extensions in the server certificate") |
| 979 | } |
| 980 | } |
| 981 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 982 | if err := hs.verifyCertificates(certMsg); err != nil { |
| 983 | return err |
| 984 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 985 | c.ocspResponse = certMsg.certificates[0].ocspResponse |
| 986 | c.sctList = certMsg.certificates[0].sctList |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 987 | |
| 988 | msg, err = c.readHandshake() |
| 989 | if err != nil { |
| 990 | return err |
| 991 | } |
| 992 | certVerifyMsg, ok := msg.(*certificateVerifyMsg) |
| 993 | if !ok { |
| 994 | c.sendAlert(alertUnexpectedMessage) |
| 995 | return unexpectedMessageError(certVerifyMsg, msg) |
| 996 | } |
| 997 | |
| 998 | c.peerSignatureAlgorithm = certVerifyMsg.signatureAlgorithm |
| 999 | input := hs.finishedHash.certificateVerifyInput(serverCertificateVerifyContextTLS13) |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1000 | err = verifyMessage(c.vers, hs.peerPublicKey, c.config, certVerifyMsg.signatureAlgorithm, input, certVerifyMsg.signature) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1001 | if err != nil { |
| 1002 | return err |
| 1003 | } |
| 1004 | |
| 1005 | hs.writeServerHash(certVerifyMsg.marshal()) |
| 1006 | } |
| 1007 | |
| 1008 | msg, err = c.readHandshake() |
| 1009 | if err != nil { |
| 1010 | return err |
| 1011 | } |
| 1012 | serverFinished, ok := msg.(*finishedMsg) |
| 1013 | if !ok { |
| 1014 | c.sendAlert(alertUnexpectedMessage) |
| 1015 | return unexpectedMessageError(serverFinished, msg) |
| 1016 | } |
| 1017 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1018 | verify := hs.finishedHash.serverSum(serverHandshakeTrafficSecret) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1019 | if len(verify) != len(serverFinished.verifyData) || |
| 1020 | subtle.ConstantTimeCompare(verify, serverFinished.verifyData) != 1 { |
| 1021 | c.sendAlert(alertHandshakeFailure) |
| 1022 | return errors.New("tls: server's Finished message was incorrect") |
| 1023 | } |
| 1024 | |
| 1025 | hs.writeServerHash(serverFinished.marshal()) |
| 1026 | |
| 1027 | // The various secrets do not incorporate the client's final leg, so |
| 1028 | // derive them now before updating the handshake context. |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 1029 | hs.finishedHash.nextSecret() |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1030 | hs.finishedHash.addEntropy(zeroSecret) |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 1031 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 1032 | clientTrafficSecret := hs.finishedHash.deriveSecret(clientApplicationTrafficLabel) |
| 1033 | serverTrafficSecret := hs.finishedHash.deriveSecret(serverApplicationTrafficLabel) |
| 1034 | c.exporterSecret = hs.finishedHash.deriveSecret(exporterLabel) |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 1035 | |
| 1036 | // Switch to application data keys on read. In particular, any alerts |
| 1037 | // from the client certificate are read over these keys. |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 1038 | if err := c.useInTrafficSecret(c.wireVersion, hs.suite, serverTrafficSecret); err != nil { |
| 1039 | return err |
| 1040 | } |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1041 | |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 1042 | // If we're expecting 0.5-RTT messages from the server, read them now. |
| 1043 | var deferredTickets []*newSessionTicketMsg |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 1044 | if encryptedExtensions.extensions.hasEarlyData { |
| 1045 | // BoringSSL will always send two tickets half-RTT when |
| 1046 | // negotiating 0-RTT. |
| 1047 | for i := 0; i < shimConfig.HalfRTTTickets; i++ { |
| 1048 | msg, err := c.readHandshake() |
| 1049 | if err != nil { |
| 1050 | return fmt.Errorf("tls: error reading half-RTT ticket: %s", err) |
| 1051 | } |
| 1052 | newSessionTicket, ok := msg.(*newSessionTicketMsg) |
| 1053 | if !ok { |
| 1054 | return errors.New("tls: expected half-RTT ticket") |
| 1055 | } |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 1056 | // Defer processing until the resumption secret is computed. |
| 1057 | deferredTickets = append(deferredTickets, newSessionTicket) |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1058 | } |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 1059 | for _, expectedMsg := range c.config.Bugs.ExpectHalfRTTData { |
| 1060 | if err := c.readRecord(recordTypeApplicationData); err != nil { |
| 1061 | return err |
| 1062 | } |
| 1063 | if !bytes.Equal(c.input.data[c.input.off:], expectedMsg) { |
| 1064 | return errors.New("ExpectHalfRTTData: did not get expected message") |
| 1065 | } |
| 1066 | c.in.freeBlock(c.input) |
| 1067 | c.input = nil |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1068 | } |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1069 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1070 | |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 1071 | // Send EndOfEarlyData and then switch write key to handshake |
| 1072 | // traffic key. |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 1073 | if encryptedExtensions.extensions.hasEarlyData && c.out.cipher != nil && !c.config.Bugs.SkipEndOfEarlyData { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 1074 | if c.config.Bugs.SendStrayEarlyHandshake { |
| 1075 | helloRequest := new(helloRequestMsg) |
| 1076 | c.writeRecord(recordTypeHandshake, helloRequest.marshal()) |
| 1077 | } |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 1078 | endOfEarlyData := new(endOfEarlyDataMsg) |
| 1079 | endOfEarlyData.nonEmpty = c.config.Bugs.NonEmptyEndOfEarlyData |
| 1080 | c.writeRecord(recordTypeHandshake, endOfEarlyData.marshal()) |
| 1081 | hs.writeClientHash(endOfEarlyData.marshal()) |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 1082 | } |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 1083 | |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 1084 | if !c.config.Bugs.SkipChangeCipherSpec && !hs.hello.hasEarlyData { |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 1085 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 1086 | } |
| 1087 | |
| 1088 | for i := 0; i < c.config.Bugs.SendExtraChangeCipherSpec; i++ { |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 1089 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 1090 | } |
| 1091 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 1092 | c.useOutTrafficSecret(c.wireVersion, hs.suite, clientHandshakeTrafficSecret) |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 1093 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1094 | if certReq != nil && !c.config.Bugs.SkipClientCertificate { |
| 1095 | certMsg := &certificateMsg{ |
| 1096 | hasRequestContext: true, |
| 1097 | requestContext: certReq.requestContext, |
| 1098 | } |
| 1099 | if chainToSend != nil { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1100 | for _, certData := range chainToSend.Certificate { |
| 1101 | certMsg.certificates = append(certMsg.certificates, certificateEntry{ |
| 1102 | data: certData, |
| 1103 | extraExtension: c.config.Bugs.SendExtensionOnCertificate, |
| 1104 | }) |
| 1105 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1106 | } |
| 1107 | hs.writeClientHash(certMsg.marshal()) |
| 1108 | c.writeRecord(recordTypeHandshake, certMsg.marshal()) |
| 1109 | |
| 1110 | if chainToSend != nil { |
| 1111 | certVerify := &certificateVerifyMsg{ |
| 1112 | hasSignatureAlgorithm: true, |
| 1113 | } |
| 1114 | |
| 1115 | // Determine the hash to sign. |
| 1116 | privKey := chainToSend.PrivateKey |
| 1117 | |
| 1118 | var err error |
| 1119 | certVerify.signatureAlgorithm, err = selectSignatureAlgorithm(c.vers, privKey, c.config, certReq.signatureAlgorithms) |
| 1120 | if err != nil { |
| 1121 | c.sendAlert(alertInternalError) |
| 1122 | return err |
| 1123 | } |
| 1124 | |
| 1125 | input := hs.finishedHash.certificateVerifyInput(clientCertificateVerifyContextTLS13) |
| 1126 | certVerify.signature, err = signMessage(c.vers, privKey, c.config, certVerify.signatureAlgorithm, input) |
| 1127 | if err != nil { |
| 1128 | c.sendAlert(alertInternalError) |
| 1129 | return err |
| 1130 | } |
| 1131 | if c.config.Bugs.SendSignatureAlgorithm != 0 { |
| 1132 | certVerify.signatureAlgorithm = c.config.Bugs.SendSignatureAlgorithm |
| 1133 | } |
| 1134 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1135 | if !c.config.Bugs.SkipCertificateVerify { |
| 1136 | hs.writeClientHash(certVerify.marshal()) |
| 1137 | c.writeRecord(recordTypeHandshake, certVerify.marshal()) |
| 1138 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1142 | if encryptedExtensions.extensions.channelIDRequested { |
| 1143 | channelIDHash := crypto.SHA256.New() |
| 1144 | channelIDHash.Write(hs.finishedHash.certificateVerifyInput(channelIDContextTLS13)) |
| 1145 | channelIDMsgBytes, err := hs.writeChannelIDMessage(channelIDHash.Sum(nil)) |
| 1146 | if err != nil { |
| 1147 | return err |
| 1148 | } |
| 1149 | hs.writeClientHash(channelIDMsgBytes) |
| 1150 | c.writeRecord(recordTypeHandshake, channelIDMsgBytes) |
| 1151 | } |
| 1152 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1153 | // Send a client Finished message. |
| 1154 | finished := new(finishedMsg) |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1155 | finished.verifyData = hs.finishedHash.clientSum(clientHandshakeTrafficSecret) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1156 | if c.config.Bugs.BadFinished { |
| 1157 | finished.verifyData[0]++ |
| 1158 | } |
| 1159 | hs.writeClientHash(finished.marshal()) |
| 1160 | if c.config.Bugs.PartialClientFinishedWithClientHello { |
| 1161 | // The first byte has already been sent. |
| 1162 | c.writeRecord(recordTypeHandshake, finished.marshal()[1:]) |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1163 | } else if c.config.Bugs.InterleaveEarlyData { |
| 1164 | finishedBytes := finished.marshal() |
| 1165 | c.sendFakeEarlyData(4) |
| 1166 | c.writeRecord(recordTypeHandshake, finishedBytes[:1]) |
| 1167 | c.sendFakeEarlyData(4) |
| 1168 | c.writeRecord(recordTypeHandshake, finishedBytes[1:]) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1169 | } else { |
| 1170 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
| 1171 | } |
| 1172 | if c.config.Bugs.SendExtraFinished { |
| 1173 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
| 1174 | } |
| 1175 | c.flushHandshake() |
| 1176 | |
| 1177 | // Switch to application data keys. |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 1178 | c.useOutTrafficSecret(c.wireVersion, hs.suite, clientTrafficSecret) |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 1179 | c.resumptionSecret = hs.finishedHash.deriveSecret(resumptionLabel) |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 1180 | for _, ticket := range deferredTickets { |
| 1181 | if err := c.processTLS13NewSessionTicket(ticket, hs.suite); err != nil { |
| 1182 | return err |
| 1183 | } |
| 1184 | } |
| 1185 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1186 | return nil |
| 1187 | } |
| 1188 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1189 | func (hs *clientHandshakeState) doFullHandshake() error { |
| 1190 | c := hs.c |
| 1191 | |
| 1192 | var leaf *x509.Certificate |
| 1193 | if hs.suite.flags&suitePSK == 0 { |
| 1194 | msg, err := c.readHandshake() |
| 1195 | if err != nil { |
| 1196 | return err |
| 1197 | } |
| 1198 | |
| 1199 | certMsg, ok := msg.(*certificateMsg) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1200 | if !ok { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1201 | c.sendAlert(alertUnexpectedMessage) |
| 1202 | return unexpectedMessageError(certMsg, msg) |
| 1203 | } |
| 1204 | hs.writeServerHash(certMsg.marshal()) |
| 1205 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1206 | if err := hs.verifyCertificates(certMsg); err != nil { |
| 1207 | return err |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1208 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1209 | leaf = c.peerCertificates[0] |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1212 | if hs.serverHello.extensions.ocspStapling { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1213 | msg, err := c.readHandshake() |
| 1214 | if err != nil { |
| 1215 | return err |
| 1216 | } |
| 1217 | cs, ok := msg.(*certificateStatusMsg) |
| 1218 | if !ok { |
| 1219 | c.sendAlert(alertUnexpectedMessage) |
| 1220 | return unexpectedMessageError(cs, msg) |
| 1221 | } |
| 1222 | hs.writeServerHash(cs.marshal()) |
| 1223 | |
| 1224 | if cs.statusType == statusTypeOCSP { |
| 1225 | c.ocspResponse = cs.response |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | msg, err := c.readHandshake() |
| 1230 | if err != nil { |
| 1231 | return err |
| 1232 | } |
| 1233 | |
| 1234 | keyAgreement := hs.suite.ka(c.vers) |
| 1235 | |
| 1236 | skx, ok := msg.(*serverKeyExchangeMsg) |
| 1237 | if ok { |
| 1238 | hs.writeServerHash(skx.marshal()) |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1239 | err = keyAgreement.processServerKeyExchange(c.config, hs.hello, hs.serverHello, hs.peerPublicKey, skx) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1240 | if err != nil { |
| 1241 | c.sendAlert(alertUnexpectedMessage) |
| 1242 | return err |
| 1243 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1244 | if ecdhe, ok := keyAgreement.(*ecdheKeyAgreement); ok { |
| 1245 | c.curveID = ecdhe.curveID |
| 1246 | } |
| 1247 | |
| 1248 | c.peerSignatureAlgorithm = keyAgreement.peerSignatureAlgorithm() |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1249 | |
| 1250 | msg, err = c.readHandshake() |
| 1251 | if err != nil { |
| 1252 | return err |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | var chainToSend *Certificate |
| 1257 | var certRequested bool |
| 1258 | certReq, ok := msg.(*certificateRequestMsg) |
| 1259 | if ok { |
| 1260 | certRequested = true |
Robert Sloan | 5cbb5c8 | 2018-04-24 11:35:46 -0700 | [diff] [blame] | 1261 | if err := checkRSAPSSSupport(c.config.Bugs.ExpectRSAPSSSupport, certReq.signatureAlgorithms, certReq.signatureAlgorithmsCert); err != nil { |
| 1262 | return err |
| 1263 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1264 | if c.config.Bugs.IgnorePeerSignatureAlgorithmPreferences { |
| 1265 | certReq.signatureAlgorithms = c.config.signSignatureAlgorithms() |
| 1266 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1267 | |
| 1268 | hs.writeServerHash(certReq.marshal()) |
| 1269 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1270 | chainToSend, err = selectClientCertificate(c, certReq) |
| 1271 | if err != nil { |
| 1272 | return err |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | msg, err = c.readHandshake() |
| 1276 | if err != nil { |
| 1277 | return err |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | shd, ok := msg.(*serverHelloDoneMsg) |
| 1282 | if !ok { |
| 1283 | c.sendAlert(alertUnexpectedMessage) |
| 1284 | return unexpectedMessageError(shd, msg) |
| 1285 | } |
| 1286 | hs.writeServerHash(shd.marshal()) |
| 1287 | |
| 1288 | // If the server requested a certificate then we have to send a |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1289 | // Certificate message in TLS, even if it's empty because we don't have |
| 1290 | // a certificate to send. In SSL 3.0, skip the message and send a |
| 1291 | // no_certificate warning alert. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1292 | if certRequested { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1293 | if c.vers == VersionSSL30 && chainToSend == nil { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 1294 | c.sendAlert(alertNoCertificate) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1295 | } else if !c.config.Bugs.SkipClientCertificate { |
| 1296 | certMsg := new(certificateMsg) |
| 1297 | if chainToSend != nil { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1298 | for _, certData := range chainToSend.Certificate { |
| 1299 | certMsg.certificates = append(certMsg.certificates, certificateEntry{ |
| 1300 | data: certData, |
| 1301 | }) |
| 1302 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1303 | } |
| 1304 | hs.writeClientHash(certMsg.marshal()) |
| 1305 | c.writeRecord(recordTypeHandshake, certMsg.marshal()) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1306 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | preMasterSecret, ckx, err := keyAgreement.generateClientKeyExchange(c.config, hs.hello, leaf) |
| 1310 | if err != nil { |
| 1311 | c.sendAlert(alertInternalError) |
| 1312 | return err |
| 1313 | } |
| 1314 | if ckx != nil { |
| 1315 | if c.config.Bugs.EarlyChangeCipherSpec < 2 { |
| 1316 | hs.writeClientHash(ckx.marshal()) |
| 1317 | } |
| 1318 | c.writeRecord(recordTypeHandshake, ckx.marshal()) |
| 1319 | } |
| 1320 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1321 | if hs.serverHello.extensions.extendedMasterSecret && c.vers >= VersionTLS10 { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1322 | hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 1323 | c.extendedMasterSecret = true |
| 1324 | } else { |
| 1325 | if c.config.Bugs.RequireExtendedMasterSecret { |
| 1326 | return errors.New("tls: extended master secret required but not supported by peer") |
| 1327 | } |
| 1328 | hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.hello.random, hs.serverHello.random) |
| 1329 | } |
| 1330 | |
| 1331 | if chainToSend != nil { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1332 | certVerify := &certificateVerifyMsg{ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1333 | hasSignatureAlgorithm: c.vers >= VersionTLS12, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1336 | // Determine the hash to sign. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1337 | privKey := c.config.Certificates[0].PrivateKey |
| 1338 | |
| 1339 | if certVerify.hasSignatureAlgorithm { |
| 1340 | certVerify.signatureAlgorithm, err = selectSignatureAlgorithm(c.vers, privKey, c.config, certReq.signatureAlgorithms) |
| 1341 | if err != nil { |
| 1342 | c.sendAlert(alertInternalError) |
| 1343 | return err |
| 1344 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1345 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1346 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1347 | if c.vers > VersionSSL30 { |
| 1348 | certVerify.signature, err = signMessage(c.vers, privKey, c.config, certVerify.signatureAlgorithm, hs.finishedHash.buffer) |
| 1349 | if err == nil && c.config.Bugs.SendSignatureAlgorithm != 0 { |
| 1350 | certVerify.signatureAlgorithm = c.config.Bugs.SendSignatureAlgorithm |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1351 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1352 | } else { |
| 1353 | // SSL 3.0's client certificate construction is |
| 1354 | // incompatible with signatureAlgorithm. |
| 1355 | rsaKey, ok := privKey.(*rsa.PrivateKey) |
| 1356 | if !ok { |
| 1357 | err = errors.New("unsupported signature type for client certificate") |
| 1358 | } else { |
| 1359 | digest := hs.finishedHash.hashForClientCertificateSSL3(hs.masterSecret) |
| 1360 | if c.config.Bugs.InvalidSignature { |
| 1361 | digest[0] ^= 0x80 |
| 1362 | } |
| 1363 | certVerify.signature, err = rsa.SignPKCS1v15(c.config.rand(), rsaKey, crypto.MD5SHA1, digest) |
| 1364 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1365 | } |
| 1366 | if err != nil { |
| 1367 | c.sendAlert(alertInternalError) |
| 1368 | return errors.New("tls: failed to sign handshake with client certificate: " + err.Error()) |
| 1369 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1370 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1371 | if !c.config.Bugs.SkipCertificateVerify { |
| 1372 | hs.writeClientHash(certVerify.marshal()) |
| 1373 | c.writeRecord(recordTypeHandshake, certVerify.marshal()) |
| 1374 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1375 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1376 | // flushHandshake will be called in sendFinished. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1377 | |
| 1378 | hs.finishedHash.discardHandshakeBuffer() |
| 1379 | |
| 1380 | return nil |
| 1381 | } |
| 1382 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1383 | // delegatedCredentialSignedMessage returns the bytes that are signed in order |
| 1384 | // to authenticate a delegated credential. |
| 1385 | func delegatedCredentialSignedMessage(credBytes []byte, algorithm signatureAlgorithm, leafDER []byte) []byte { |
| 1386 | // https://tools.ietf.org/html/draft-ietf-tls-subcerts-03#section-3 |
| 1387 | ret := make([]byte, 64, 128) |
| 1388 | for i := range ret { |
| 1389 | ret[i] = 0x20 |
| 1390 | } |
| 1391 | |
| 1392 | ret = append(ret, []byte("TLS, server delegated credentials\x00")...) |
| 1393 | ret = append(ret, leafDER...) |
| 1394 | ret = append(ret, byte(algorithm>>8), byte(algorithm)) |
| 1395 | ret = append(ret, credBytes...) |
| 1396 | |
| 1397 | return ret |
| 1398 | } |
| 1399 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1400 | func (hs *clientHandshakeState) verifyCertificates(certMsg *certificateMsg) error { |
| 1401 | c := hs.c |
| 1402 | |
| 1403 | if len(certMsg.certificates) == 0 { |
| 1404 | c.sendAlert(alertIllegalParameter) |
| 1405 | return errors.New("tls: no certificates sent") |
| 1406 | } |
| 1407 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1408 | var dc *delegatedCredential |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1409 | certs := make([]*x509.Certificate, len(certMsg.certificates)) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1410 | for i, certEntry := range certMsg.certificates { |
| 1411 | cert, err := x509.ParseCertificate(certEntry.data) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1412 | if err != nil { |
| 1413 | c.sendAlert(alertBadCertificate) |
| 1414 | return errors.New("tls: failed to parse certificate from server: " + err.Error()) |
| 1415 | } |
| 1416 | certs[i] = cert |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1417 | |
| 1418 | if certEntry.delegatedCredential != nil { |
| 1419 | if c.config.Bugs.FailIfDelegatedCredentials { |
| 1420 | c.sendAlert(alertIllegalParameter) |
| 1421 | return errors.New("tls: unexpected delegated credential") |
| 1422 | } |
| 1423 | if i != 0 { |
| 1424 | c.sendAlert(alertIllegalParameter) |
| 1425 | return errors.New("tls: non-leaf certificate has a delegated credential") |
| 1426 | } |
| 1427 | if c.config.Bugs.DisableDelegatedCredentials { |
| 1428 | c.sendAlert(alertIllegalParameter) |
| 1429 | return errors.New("tls: server sent delegated credential without it being requested") |
| 1430 | } |
| 1431 | dc = certEntry.delegatedCredential |
| 1432 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | if !c.config.InsecureSkipVerify { |
| 1436 | opts := x509.VerifyOptions{ |
| 1437 | Roots: c.config.RootCAs, |
| 1438 | CurrentTime: c.config.time(), |
| 1439 | DNSName: c.config.ServerName, |
| 1440 | Intermediates: x509.NewCertPool(), |
| 1441 | } |
| 1442 | |
| 1443 | for i, cert := range certs { |
| 1444 | if i == 0 { |
| 1445 | continue |
| 1446 | } |
| 1447 | opts.Intermediates.AddCert(cert) |
| 1448 | } |
| 1449 | var err error |
| 1450 | c.verifiedChains, err = certs[0].Verify(opts) |
| 1451 | if err != nil { |
| 1452 | c.sendAlert(alertBadCertificate) |
| 1453 | return err |
| 1454 | } |
| 1455 | } |
| 1456 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1457 | leafPublicKey := getCertificatePublicKey(certs[0]) |
| 1458 | switch leafPublicKey.(type) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 1459 | case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1460 | break |
| 1461 | default: |
| 1462 | c.sendAlert(alertUnsupportedCertificate) |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1463 | return fmt.Errorf("tls: server's certificate contains an unsupported type of public key: %T", leafPublicKey) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | c.peerCertificates = certs |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1467 | |
| 1468 | if dc != nil { |
| 1469 | // Note that this doesn't check a) the delegated credential temporal |
| 1470 | // validity nor b) that the certificate has the special OID asserted. |
| 1471 | hs.skxAlgo = dc.expectedCertVerifyAlgo |
| 1472 | |
| 1473 | var err error |
| 1474 | if hs.peerPublicKey, err = x509.ParsePKIXPublicKey(dc.pkixPublicKey); err != nil { |
| 1475 | c.sendAlert(alertBadCertificate) |
| 1476 | return errors.New("tls: failed to parse public key from delegated credential: " + err.Error()) |
| 1477 | } |
| 1478 | |
| 1479 | verifier, err := getSigner(c.vers, hs.peerPublicKey, c.config, dc.algorithm, true) |
| 1480 | if err != nil { |
| 1481 | c.sendAlert(alertBadCertificate) |
| 1482 | return errors.New("tls: failed to get verifier for delegated credential: " + err.Error()) |
| 1483 | } |
| 1484 | |
| 1485 | if err := verifier.verifyMessage(leafPublicKey, delegatedCredentialSignedMessage(dc.signedBytes, dc.algorithm, certs[0].Raw), dc.signature); err != nil { |
| 1486 | c.sendAlert(alertBadCertificate) |
| 1487 | return errors.New("tls: failed to verify delegated credential: " + err.Error()) |
| 1488 | } |
| 1489 | } else if c.config.Bugs.ExpectDelegatedCredentials { |
| 1490 | c.sendAlert(alertInternalError) |
| 1491 | return errors.New("tls: delegated credentials missing") |
| 1492 | } else { |
| 1493 | hs.peerPublicKey = leafPublicKey |
| 1494 | } |
| 1495 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1496 | return nil |
| 1497 | } |
| 1498 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1499 | func (hs *clientHandshakeState) establishKeys() error { |
| 1500 | c := hs.c |
| 1501 | |
| 1502 | clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1503 | keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.hello.random, hs.serverHello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen(c.vers)) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1504 | var clientCipher, serverCipher interface{} |
| 1505 | var clientHash, serverHash macFunction |
| 1506 | if hs.suite.cipher != nil { |
| 1507 | clientCipher = hs.suite.cipher(clientKey, clientIV, false /* not for reading */) |
| 1508 | clientHash = hs.suite.mac(c.vers, clientMAC) |
| 1509 | serverCipher = hs.suite.cipher(serverKey, serverIV, true /* for reading */) |
| 1510 | serverHash = hs.suite.mac(c.vers, serverMAC) |
| 1511 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1512 | clientCipher = hs.suite.aead(c.vers, clientKey, clientIV) |
| 1513 | serverCipher = hs.suite.aead(c.vers, serverKey, serverIV) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
Robert Sloan | db4251a | 2017-09-18 09:38:15 -0700 | [diff] [blame] | 1516 | c.in.prepareCipherSpec(c.wireVersion, serverCipher, serverHash) |
| 1517 | c.out.prepareCipherSpec(c.wireVersion, clientCipher, clientHash) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1518 | return nil |
| 1519 | } |
| 1520 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1521 | func (hs *clientHandshakeState) processServerExtensions(serverExtensions *serverExtensions) error { |
| 1522 | c := hs.c |
| 1523 | |
| 1524 | if c.vers < VersionTLS13 { |
| 1525 | if c.config.Bugs.RequireRenegotiationInfo && serverExtensions.secureRenegotiation == nil { |
| 1526 | return errors.New("tls: renegotiation extension missing") |
| 1527 | } |
| 1528 | |
| 1529 | if len(c.clientVerify) > 0 && !c.noRenegotiationInfo() { |
| 1530 | var expectedRenegInfo []byte |
| 1531 | expectedRenegInfo = append(expectedRenegInfo, c.clientVerify...) |
| 1532 | expectedRenegInfo = append(expectedRenegInfo, c.serverVerify...) |
| 1533 | if !bytes.Equal(serverExtensions.secureRenegotiation, expectedRenegInfo) { |
| 1534 | c.sendAlert(alertHandshakeFailure) |
| 1535 | return fmt.Errorf("tls: renegotiation mismatch") |
| 1536 | } |
| 1537 | } |
| 1538 | } else if serverExtensions.secureRenegotiation != nil { |
| 1539 | return errors.New("tls: renegotiation info sent in TLS 1.3") |
| 1540 | } |
| 1541 | |
| 1542 | if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil { |
| 1543 | if serverExtensions.customExtension != *expected { |
| 1544 | return fmt.Errorf("tls: bad custom extension contents %q", serverExtensions.customExtension) |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | clientDidNPN := hs.hello.nextProtoNeg |
| 1549 | clientDidALPN := len(hs.hello.alpnProtocols) > 0 |
| 1550 | serverHasNPN := serverExtensions.nextProtoNeg |
| 1551 | serverHasALPN := len(serverExtensions.alpnProtocol) > 0 |
| 1552 | |
| 1553 | if !clientDidNPN && serverHasNPN { |
| 1554 | c.sendAlert(alertHandshakeFailure) |
| 1555 | return errors.New("server advertised unrequested NPN extension") |
| 1556 | } |
| 1557 | |
| 1558 | if !clientDidALPN && serverHasALPN { |
| 1559 | c.sendAlert(alertHandshakeFailure) |
| 1560 | return errors.New("server advertised unrequested ALPN extension") |
| 1561 | } |
| 1562 | |
| 1563 | if serverHasNPN && serverHasALPN { |
| 1564 | c.sendAlert(alertHandshakeFailure) |
| 1565 | return errors.New("server advertised both NPN and ALPN extensions") |
| 1566 | } |
| 1567 | |
| 1568 | if serverHasALPN { |
| 1569 | c.clientProtocol = serverExtensions.alpnProtocol |
| 1570 | c.clientProtocolFallback = false |
| 1571 | c.usedALPN = true |
| 1572 | } |
| 1573 | |
| 1574 | if serverHasNPN && c.vers >= VersionTLS13 { |
| 1575 | c.sendAlert(alertHandshakeFailure) |
| 1576 | return errors.New("server advertised NPN over TLS 1.3") |
| 1577 | } |
| 1578 | |
| 1579 | if !hs.hello.channelIDSupported && serverExtensions.channelIDRequested { |
| 1580 | c.sendAlert(alertHandshakeFailure) |
| 1581 | return errors.New("server advertised unrequested Channel ID extension") |
| 1582 | } |
| 1583 | |
Robert Sloan | 978112c | 2018-01-22 12:53:01 -0800 | [diff] [blame] | 1584 | if len(serverExtensions.tokenBindingParams) == 1 { |
| 1585 | found := false |
| 1586 | for _, p := range c.config.TokenBindingParams { |
| 1587 | if p == serverExtensions.tokenBindingParams[0] { |
| 1588 | c.tokenBindingParam = p |
| 1589 | found = true |
| 1590 | break |
| 1591 | } |
| 1592 | } |
| 1593 | if !found { |
| 1594 | return errors.New("tls: server advertised unsupported Token Binding key param") |
| 1595 | } |
| 1596 | if serverExtensions.tokenBindingVersion > c.config.TokenBindingVersion { |
| 1597 | return errors.New("tls: server's Token Binding version is too new") |
| 1598 | } |
| 1599 | if c.vers < VersionTLS13 { |
| 1600 | if !serverExtensions.extendedMasterSecret || serverExtensions.secureRenegotiation == nil { |
| 1601 | return errors.New("server sent Token Binding without EMS or RI") |
| 1602 | } |
| 1603 | } |
| 1604 | c.tokenBindingNegotiated = true |
| 1605 | } |
| 1606 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1607 | if serverExtensions.extendedMasterSecret && c.vers >= VersionTLS13 { |
| 1608 | return errors.New("tls: server advertised extended master secret over TLS 1.3") |
| 1609 | } |
| 1610 | |
| 1611 | if serverExtensions.ticketSupported && c.vers >= VersionTLS13 { |
| 1612 | return errors.New("tls: server advertised ticket extension over TLS 1.3") |
| 1613 | } |
| 1614 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1615 | if serverExtensions.ocspStapling && c.vers >= VersionTLS13 { |
| 1616 | return errors.New("tls: server advertised OCSP in ServerHello over TLS 1.3") |
| 1617 | } |
| 1618 | |
| 1619 | if serverExtensions.ocspStapling && c.config.Bugs.NoOCSPStapling { |
| 1620 | return errors.New("tls: server advertised unrequested OCSP extension") |
| 1621 | } |
| 1622 | |
| 1623 | if len(serverExtensions.sctList) > 0 && c.vers >= VersionTLS13 { |
| 1624 | return errors.New("tls: server advertised SCTs in ServerHello over TLS 1.3") |
| 1625 | } |
| 1626 | |
| 1627 | if len(serverExtensions.sctList) > 0 && c.config.Bugs.NoSignedCertificateTimestamps { |
| 1628 | return errors.New("tls: server advertised unrequested SCTs") |
| 1629 | } |
| 1630 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1631 | if serverExtensions.srtpProtectionProfile != 0 { |
| 1632 | if serverExtensions.srtpMasterKeyIdentifier != "" { |
| 1633 | return errors.New("tls: server selected SRTP MKI value") |
| 1634 | } |
| 1635 | |
| 1636 | found := false |
| 1637 | for _, p := range c.config.SRTPProtectionProfiles { |
| 1638 | if p == serverExtensions.srtpProtectionProfile { |
| 1639 | found = true |
| 1640 | break |
| 1641 | } |
| 1642 | } |
| 1643 | if !found { |
| 1644 | return errors.New("tls: server advertised unsupported SRTP profile") |
| 1645 | } |
| 1646 | |
| 1647 | c.srtpProtectionProfile = serverExtensions.srtpProtectionProfile |
| 1648 | } |
| 1649 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 1650 | if c.vers >= VersionTLS13 && c.didResume { |
| 1651 | if c.config.Bugs.ExpectEarlyDataAccepted && !serverExtensions.hasEarlyData { |
| 1652 | c.sendAlert(alertHandshakeFailure) |
| 1653 | return errors.New("tls: server did not accept early data when expected") |
| 1654 | } |
| 1655 | |
| 1656 | if !c.config.Bugs.ExpectEarlyDataAccepted && serverExtensions.hasEarlyData { |
| 1657 | c.sendAlert(alertHandshakeFailure) |
| 1658 | return errors.New("tls: server accepted early data when not expected") |
| 1659 | } |
| 1660 | } |
| 1661 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 1662 | if len(serverExtensions.quicTransportParams) > 0 { |
| 1663 | if c.vers < VersionTLS13 { |
| 1664 | c.sendAlert(alertHandshakeFailure) |
| 1665 | return errors.New("tls: server sent QUIC transport params for TLS version less than 1.3") |
| 1666 | } |
| 1667 | c.quicTransportParams = serverExtensions.quicTransportParams |
| 1668 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame] | 1669 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1670 | if c.config.Bugs.ExpectPQExperimentSignal != serverExtensions.pqExperimentSignal { |
| 1671 | return fmt.Errorf("tls: PQ experiment signal presence (%t) was not what was expected", serverExtensions.pqExperimentSignal) |
| 1672 | } |
| 1673 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1674 | return nil |
| 1675 | } |
| 1676 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1677 | func (hs *clientHandshakeState) serverResumedSession() bool { |
| 1678 | // If the server responded with the same sessionId then it means the |
| 1679 | // sessionTicket is being used to resume a TLS session. |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 1680 | // |
| 1681 | // Note that, if hs.hello.sessionId is a non-nil empty array, this will |
| 1682 | // accept an empty session ID from the server as resumption. See |
| 1683 | // EmptyTicketSessionID. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1684 | return hs.session != nil && hs.hello.sessionId != nil && |
| 1685 | bytes.Equal(hs.serverHello.sessionId, hs.hello.sessionId) |
| 1686 | } |
| 1687 | |
| 1688 | func (hs *clientHandshakeState) processServerHello() (bool, error) { |
| 1689 | c := hs.c |
| 1690 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1691 | if hs.serverResumedSession() { |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1692 | // For test purposes, assert that the server never accepts the |
| 1693 | // resumption offer on renegotiation. |
| 1694 | if c.cipherSuite != nil && c.config.Bugs.FailIfResumeOnRenego { |
| 1695 | return false, errors.New("tls: server resumed session on renegotiation") |
| 1696 | } |
| 1697 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1698 | if hs.serverHello.extensions.sctList != nil { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1699 | return false, errors.New("tls: server sent SCT extension on session resumption") |
| 1700 | } |
| 1701 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1702 | if hs.serverHello.extensions.ocspStapling { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1703 | return false, errors.New("tls: server sent OCSP extension on session resumption") |
| 1704 | } |
| 1705 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1706 | // Restore masterSecret and peerCerts from previous state |
| 1707 | hs.masterSecret = hs.session.masterSecret |
| 1708 | c.peerCertificates = hs.session.serverCertificates |
| 1709 | c.extendedMasterSecret = hs.session.extendedMasterSecret |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1710 | c.sctList = hs.session.sctList |
| 1711 | c.ocspResponse = hs.session.ocspResponse |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1712 | hs.finishedHash.discardHandshakeBuffer() |
| 1713 | return true, nil |
| 1714 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1715 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1716 | if hs.serverHello.extensions.sctList != nil { |
| 1717 | c.sctList = hs.serverHello.extensions.sctList |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1720 | return false, nil |
| 1721 | } |
| 1722 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1723 | func (hs *clientHandshakeState) readFinished(out []byte) error { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1724 | c := hs.c |
| 1725 | |
| 1726 | c.readRecord(recordTypeChangeCipherSpec) |
| 1727 | if err := c.in.error(); err != nil { |
| 1728 | return err |
| 1729 | } |
| 1730 | |
| 1731 | msg, err := c.readHandshake() |
| 1732 | if err != nil { |
| 1733 | return err |
| 1734 | } |
| 1735 | serverFinished, ok := msg.(*finishedMsg) |
| 1736 | if !ok { |
| 1737 | c.sendAlert(alertUnexpectedMessage) |
| 1738 | return unexpectedMessageError(serverFinished, msg) |
| 1739 | } |
| 1740 | |
| 1741 | if c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 1742 | verify := hs.finishedHash.serverSum(hs.masterSecret) |
| 1743 | if len(verify) != len(serverFinished.verifyData) || |
| 1744 | subtle.ConstantTimeCompare(verify, serverFinished.verifyData) != 1 { |
| 1745 | c.sendAlert(alertHandshakeFailure) |
| 1746 | return errors.New("tls: server's Finished message was incorrect") |
| 1747 | } |
| 1748 | } |
| 1749 | c.serverVerify = append(c.serverVerify[:0], serverFinished.verifyData...) |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1750 | copy(out, serverFinished.verifyData) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1751 | hs.writeServerHash(serverFinished.marshal()) |
| 1752 | return nil |
| 1753 | } |
| 1754 | |
| 1755 | func (hs *clientHandshakeState) readSessionTicket() error { |
| 1756 | c := hs.c |
| 1757 | |
| 1758 | // Create a session with no server identifier. Either a |
| 1759 | // session ID or session ticket will be attached. |
| 1760 | session := &ClientSessionState{ |
| 1761 | vers: c.vers, |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 1762 | wireVersion: c.wireVersion, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1763 | cipherSuite: hs.suite.id, |
| 1764 | masterSecret: hs.masterSecret, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1765 | handshakeHash: hs.finishedHash.Sum(), |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1766 | serverCertificates: c.peerCertificates, |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1767 | sctList: c.sctList, |
| 1768 | ocspResponse: c.ocspResponse, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1769 | ticketExpiration: c.config.time().Add(time.Duration(7 * 24 * time.Hour)), |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1770 | } |
| 1771 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1772 | if !hs.serverHello.extensions.ticketSupported { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1773 | if c.config.Bugs.ExpectNewTicket { |
| 1774 | return errors.New("tls: expected new ticket") |
| 1775 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1776 | if hs.session == nil && len(hs.serverHello.sessionId) > 0 { |
| 1777 | session.sessionId = hs.serverHello.sessionId |
| 1778 | hs.session = session |
| 1779 | } |
| 1780 | return nil |
| 1781 | } |
| 1782 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1783 | if c.vers == VersionSSL30 { |
| 1784 | return errors.New("tls: negotiated session tickets in SSL 3.0") |
| 1785 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1786 | if c.config.Bugs.ExpectNoNewSessionTicket { |
| 1787 | return errors.New("tls: received unexpected NewSessionTicket") |
| 1788 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1789 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1790 | msg, err := c.readHandshake() |
| 1791 | if err != nil { |
| 1792 | return err |
| 1793 | } |
| 1794 | sessionTicketMsg, ok := msg.(*newSessionTicketMsg) |
| 1795 | if !ok { |
| 1796 | c.sendAlert(alertUnexpectedMessage) |
| 1797 | return unexpectedMessageError(sessionTicketMsg, msg) |
| 1798 | } |
| 1799 | |
| 1800 | session.sessionTicket = sessionTicketMsg.ticket |
| 1801 | hs.session = session |
| 1802 | |
| 1803 | hs.writeServerHash(sessionTicketMsg.marshal()) |
| 1804 | |
| 1805 | return nil |
| 1806 | } |
| 1807 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1808 | func (hs *clientHandshakeState) sendFinished(out []byte, isResume bool) error { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1809 | c := hs.c |
| 1810 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1811 | var postCCSMsgs [][]byte |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1812 | seqno := hs.c.sendHandshakeSeq |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1813 | if hs.serverHello.extensions.nextProtoNeg { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1814 | nextProto := new(nextProtoMsg) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1815 | proto, fallback := mutualProtocol(c.config.NextProtos, hs.serverHello.extensions.nextProtos) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1816 | nextProto.proto = proto |
| 1817 | c.clientProtocol = proto |
| 1818 | c.clientProtocolFallback = fallback |
| 1819 | |
| 1820 | nextProtoBytes := nextProto.marshal() |
| 1821 | hs.writeHash(nextProtoBytes, seqno) |
| 1822 | seqno++ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1823 | postCCSMsgs = append(postCCSMsgs, nextProtoBytes) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1826 | if hs.serverHello.extensions.channelIDRequested { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1827 | var resumeHash []byte |
| 1828 | if isResume { |
| 1829 | resumeHash = hs.session.handshakeHash |
| 1830 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1831 | channelIDMsgBytes, err := hs.writeChannelIDMessage(hs.finishedHash.hashForChannelID(resumeHash)) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1832 | if err != nil { |
| 1833 | return err |
| 1834 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1835 | hs.writeHash(channelIDMsgBytes, seqno) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1836 | seqno++ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1837 | postCCSMsgs = append(postCCSMsgs, channelIDMsgBytes) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | finished := new(finishedMsg) |
| 1841 | if c.config.Bugs.EarlyChangeCipherSpec == 2 { |
| 1842 | finished.verifyData = hs.finishedHash.clientSum(nil) |
| 1843 | } else { |
| 1844 | finished.verifyData = hs.finishedHash.clientSum(hs.masterSecret) |
| 1845 | } |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1846 | copy(out, finished.verifyData) |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1847 | if c.config.Bugs.BadFinished { |
| 1848 | finished.verifyData[0]++ |
| 1849 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1850 | c.clientVerify = append(c.clientVerify[:0], finished.verifyData...) |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1851 | hs.finishedBytes = finished.marshal() |
| 1852 | hs.writeHash(hs.finishedBytes, seqno) |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1853 | postCCSMsgs = append(postCCSMsgs, hs.finishedBytes) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1854 | |
| 1855 | if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1856 | c.writeRecord(recordTypeHandshake, postCCSMsgs[0][:5]) |
| 1857 | postCCSMsgs[0] = postCCSMsgs[0][5:] |
| 1858 | } else if c.config.Bugs.SendUnencryptedFinished { |
| 1859 | c.writeRecord(recordTypeHandshake, postCCSMsgs[0]) |
| 1860 | postCCSMsgs = postCCSMsgs[1:] |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | if !c.config.Bugs.SkipChangeCipherSpec && |
| 1864 | c.config.Bugs.EarlyChangeCipherSpec == 0 { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1865 | ccs := []byte{1} |
| 1866 | if c.config.Bugs.BadChangeCipherSpec != nil { |
| 1867 | ccs = c.config.Bugs.BadChangeCipherSpec |
| 1868 | } |
| 1869 | c.writeRecord(recordTypeChangeCipherSpec, ccs) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | if c.config.Bugs.AppDataAfterChangeCipherSpec != nil { |
| 1873 | c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec) |
| 1874 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1875 | if c.config.Bugs.AlertAfterChangeCipherSpec != 0 { |
| 1876 | c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec) |
| 1877 | return errors.New("tls: simulating post-CCS alert") |
| 1878 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1879 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1880 | if !c.config.Bugs.SkipFinished { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1881 | for _, msg := range postCCSMsgs { |
| 1882 | c.writeRecord(recordTypeHandshake, msg) |
| 1883 | } |
| 1884 | |
| 1885 | if c.config.Bugs.SendExtraFinished { |
| 1886 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
| 1887 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1888 | } |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 1889 | |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1890 | if !isResume || !c.config.Bugs.PackAppDataWithHandshake { |
| 1891 | c.flushHandshake() |
| 1892 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1893 | return nil |
| 1894 | } |
| 1895 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1896 | func (hs *clientHandshakeState) writeChannelIDMessage(channelIDHash []byte) ([]byte, error) { |
| 1897 | c := hs.c |
| 1898 | channelIDMsg := new(channelIDMsg) |
| 1899 | if c.config.ChannelID.Curve != elliptic.P256() { |
| 1900 | return nil, fmt.Errorf("tls: Channel ID is not on P-256.") |
| 1901 | } |
| 1902 | r, s, err := ecdsa.Sign(c.config.rand(), c.config.ChannelID, channelIDHash) |
| 1903 | if err != nil { |
| 1904 | return nil, err |
| 1905 | } |
| 1906 | channelID := make([]byte, 128) |
| 1907 | writeIntPadded(channelID[0:32], c.config.ChannelID.X) |
| 1908 | writeIntPadded(channelID[32:64], c.config.ChannelID.Y) |
| 1909 | writeIntPadded(channelID[64:96], r) |
| 1910 | writeIntPadded(channelID[96:128], s) |
| 1911 | if c.config.Bugs.InvalidChannelIDSignature { |
| 1912 | channelID[64] ^= 1 |
| 1913 | } |
| 1914 | channelIDMsg.channelID = channelID |
| 1915 | |
| 1916 | c.channelID = &c.config.ChannelID.PublicKey |
| 1917 | |
| 1918 | return channelIDMsg.marshal(), nil |
| 1919 | } |
| 1920 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1921 | func (hs *clientHandshakeState) writeClientHash(msg []byte) { |
| 1922 | // writeClientHash is called before writeRecord. |
| 1923 | hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 1924 | } |
| 1925 | |
| 1926 | func (hs *clientHandshakeState) writeServerHash(msg []byte) { |
| 1927 | // writeServerHash is called after readHandshake. |
| 1928 | hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 1929 | } |
| 1930 | |
| 1931 | func (hs *clientHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 1932 | if hs.c.isDTLS { |
| 1933 | // This is somewhat hacky. DTLS hashes a slightly different format. |
| 1934 | // First, the TLS header. |
| 1935 | hs.finishedHash.Write(msg[:4]) |
| 1936 | // Then the sequence number and reassembled fragment offset (always 0). |
| 1937 | hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 1938 | // Then the reassembled fragment (always equal to the message length). |
| 1939 | hs.finishedHash.Write(msg[1:4]) |
| 1940 | // And then the message body. |
| 1941 | hs.finishedHash.Write(msg[4:]) |
| 1942 | } else { |
| 1943 | hs.finishedHash.Write(msg) |
| 1944 | } |
| 1945 | } |
| 1946 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1947 | // selectClientCertificate selects a certificate for use with the given |
| 1948 | // certificate, or none if none match. It may return a particular certificate or |
| 1949 | // nil on success, or an error on internal error. |
| 1950 | func selectClientCertificate(c *Conn, certReq *certificateRequestMsg) (*Certificate, error) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1951 | if len(c.config.Certificates) == 0 { |
| 1952 | return nil, nil |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1953 | } |
| 1954 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1955 | // The test is assumed to have configured the certificate it meant to |
| 1956 | // send. |
| 1957 | if len(c.config.Certificates) > 1 { |
| 1958 | return nil, errors.New("tls: multiple certificates configured") |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1959 | } |
| 1960 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1961 | return &c.config.Certificates[0], nil |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1962 | } |
| 1963 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1964 | // clientSessionCacheKey returns a key used to cache sessionTickets that could |
| 1965 | // be used to resume previously negotiated TLS sessions with a server. |
| 1966 | func clientSessionCacheKey(serverAddr net.Addr, config *Config) string { |
| 1967 | if len(config.ServerName) > 0 { |
| 1968 | return config.ServerName |
| 1969 | } |
| 1970 | return serverAddr.String() |
| 1971 | } |
| 1972 | |
| 1973 | // mutualProtocol finds the mutual Next Protocol Negotiation or ALPN protocol |
| 1974 | // given list of possible protocols and a list of the preference order. The |
| 1975 | // first list must not be empty. It returns the resulting protocol and flag |
| 1976 | // indicating if the fallback case was reached. |
| 1977 | func mutualProtocol(protos, preferenceProtos []string) (string, bool) { |
| 1978 | for _, s := range preferenceProtos { |
| 1979 | for _, c := range protos { |
| 1980 | if s == c { |
| 1981 | return s, false |
| 1982 | } |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | return protos[0], true |
| 1987 | } |
| 1988 | |
| 1989 | // writeIntPadded writes x into b, padded up with leading zeros as |
| 1990 | // needed. |
| 1991 | func writeIntPadded(b []byte, x *big.Int) { |
| 1992 | for i := range b { |
| 1993 | b[i] = 0 |
| 1994 | } |
| 1995 | xb := x.Bytes() |
| 1996 | copy(b[len(b)-len(xb):], xb) |
| 1997 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1998 | |
Robert Sloan | b1b54b8 | 2017-11-06 13:50:02 -0800 | [diff] [blame] | 1999 | func generatePSKBinders(version uint16, hello *clientHelloMsg, pskCipherSuite *cipherSuite, psk, firstClientHello, helloRetryRequest []byte, config *Config) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 2000 | if config.Bugs.SendNoPSKBinder { |
| 2001 | return |
| 2002 | } |
| 2003 | |
| 2004 | binderLen := pskCipherSuite.hash().Size() |
| 2005 | if config.Bugs.SendShortPSKBinder { |
| 2006 | binderLen-- |
| 2007 | } |
| 2008 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 2009 | numBinders := 1 |
| 2010 | if config.Bugs.SendExtraPSKBinder { |
| 2011 | numBinders++ |
| 2012 | } |
| 2013 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 2014 | // Fill hello.pskBinders with appropriate length arrays of zeros so the |
| 2015 | // length prefixes are correct when computing the binder over the truncated |
| 2016 | // ClientHello message. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 2017 | hello.pskBinders = make([][]byte, numBinders) |
| 2018 | for i := range hello.pskBinders { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 2019 | hello.pskBinders[i] = make([]byte, binderLen) |
| 2020 | } |
| 2021 | |
| 2022 | helloBytes := hello.marshal() |
| 2023 | binderSize := len(hello.pskBinders)*(binderLen+1) + 2 |
| 2024 | truncatedHello := helloBytes[:len(helloBytes)-binderSize] |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 2025 | binder := computePSKBinder(psk, version, resumptionPSKBinderLabel, pskCipherSuite, firstClientHello, helloRetryRequest, truncatedHello) |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 2026 | if config.Bugs.SendShortPSKBinder { |
| 2027 | binder = binder[:binderLen] |
| 2028 | } |
| 2029 | if config.Bugs.SendInvalidPSKBinder { |
| 2030 | binder[0] ^= 1 |
| 2031 | } |
| 2032 | |
| 2033 | for i := range hello.pskBinders { |
| 2034 | hello.pskBinders[i] = binder |
| 2035 | } |
| 2036 | |
| 2037 | hello.raw = nil |
| 2038 | } |