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