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