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