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