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 ( |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 8 | "bytes" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 9 | "crypto" |
| 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 | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 18 | "math/big" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | // serverHandshakeState contains details of a server handshake in progress. |
| 22 | // It's discarded once the handshake has completed. |
| 23 | type serverHandshakeState struct { |
| 24 | c *Conn |
| 25 | clientHello *clientHelloMsg |
| 26 | hello *serverHelloMsg |
| 27 | suite *cipherSuite |
| 28 | ellipticOk bool |
| 29 | ecdsaOk bool |
| 30 | sessionState *sessionState |
| 31 | finishedHash finishedHash |
| 32 | masterSecret []byte |
| 33 | certsFromClient [][]byte |
| 34 | cert *Certificate |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 35 | finishedBytes []byte |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // serverHandshake performs a TLS handshake as a server. |
| 39 | func (c *Conn) serverHandshake() error { |
| 40 | config := c.config |
| 41 | |
| 42 | // If this is the first server handshake, we generate a random key to |
| 43 | // encrypt the tickets with. |
| 44 | config.serverInitOnce.Do(config.serverInit) |
| 45 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 46 | c.sendHandshakeSeq = 0 |
| 47 | c.recvHandshakeSeq = 0 |
| 48 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 49 | hs := serverHandshakeState{ |
| 50 | c: c, |
| 51 | } |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 52 | if err := hs.readClientHello(); err != nil { |
| 53 | return err |
| 54 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 55 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 56 | if c.vers >= VersionTLS13 { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 57 | if err := hs.doTLS13Handshake(); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | return err |
| 59 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 60 | } else { |
| 61 | isResume, err := hs.processClientHello() |
| 62 | if err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 63 | return err |
| 64 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 65 | |
| 66 | // For an overview of TLS handshaking, see https://tools.ietf.org/html/rfc5246#section-7.3 |
| 67 | if isResume { |
| 68 | // The client has included a session ticket and so we do an abbreviated handshake. |
| 69 | if err := hs.doResumeHandshake(); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if err := hs.establishKeys(); err != nil { |
| 73 | return err |
| 74 | } |
| 75 | if c.config.Bugs.RenewTicketOnResume { |
| 76 | if err := hs.sendSessionTicket(); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | } |
| 80 | if err := hs.sendFinished(c.firstFinished[:]); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | // Most retransmits are triggered by a timeout, but the final |
| 84 | // leg of the handshake is retransmited upon re-receiving a |
| 85 | // Finished. |
| 86 | if err := c.simulatePacketLoss(func() { |
David Benjamin | 02edcd0 | 2016-07-27 17:40:37 -0400 | [diff] [blame] | 87 | c.sendHandshakeSeq-- |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 88 | c.writeRecord(recordTypeHandshake, hs.finishedBytes) |
| 89 | c.flushHandshake() |
| 90 | }); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | if err := hs.readFinished(nil, isResume); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | c.didResume = true |
| 97 | } else { |
| 98 | // The client didn't include a session ticket, or it wasn't |
| 99 | // valid so we do a full handshake. |
| 100 | if err := hs.doFullHandshake(); err != nil { |
| 101 | return err |
| 102 | } |
| 103 | if err := hs.establishKeys(); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | if err := hs.readFinished(c.firstFinished[:], isResume); err != nil { |
| 107 | return err |
| 108 | } |
| 109 | if c.config.Bugs.AlertBeforeFalseStartTest != 0 { |
| 110 | c.sendAlert(c.config.Bugs.AlertBeforeFalseStartTest) |
| 111 | } |
| 112 | if c.config.Bugs.ExpectFalseStart { |
| 113 | if err := c.readRecord(recordTypeApplicationData); err != nil { |
| 114 | return fmt.Errorf("tls: peer did not false start: %s", err) |
| 115 | } |
| 116 | } |
David Benjamin | bed9aae | 2014-08-07 19:13:38 -0400 | [diff] [blame] | 117 | if err := hs.sendSessionTicket(); err != nil { |
| 118 | return err |
| 119 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 120 | if err := hs.sendFinished(nil); err != nil { |
| 121 | return err |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 122 | } |
| 123 | } |
David Benjamin | 97a0a08 | 2016-07-13 17:57:35 -0400 | [diff] [blame] | 124 | |
| 125 | c.exporterSecret = hs.masterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | } |
| 127 | c.handshakeComplete = true |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 128 | copy(c.clientRandom[:], hs.clientHello.random) |
| 129 | copy(c.serverRandom[:], hs.hello.random) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | |
| 131 | return nil |
| 132 | } |
| 133 | |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 134 | // readClientHello reads a ClientHello message from the client and determines |
| 135 | // the protocol version. |
| 136 | func (hs *serverHandshakeState) readClientHello() error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 137 | config := hs.c.config |
| 138 | c := hs.c |
| 139 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 140 | if err := c.simulatePacketLoss(nil); err != nil { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 141 | return err |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 142 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | msg, err := c.readHandshake() |
| 144 | if err != nil { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 145 | return err |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | } |
| 147 | var ok bool |
| 148 | hs.clientHello, ok = msg.(*clientHelloMsg) |
| 149 | if !ok { |
| 150 | c.sendAlert(alertUnexpectedMessage) |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 151 | return unexpectedMessageError(hs.clientHello, msg) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 153 | if size := config.Bugs.RequireClientHelloSize; size != 0 && len(hs.clientHello.raw) != size { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 154 | return fmt.Errorf("tls: ClientHello record size is %d, but expected %d", len(hs.clientHello.raw), size) |
Feng Lu | 41aa325 | 2014-11-21 22:47:56 -0800 | [diff] [blame] | 155 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 156 | |
| 157 | if c.isDTLS && !config.Bugs.SkipHelloVerifyRequest { |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 158 | // Per RFC 6347, the version field in HelloVerifyRequest SHOULD |
| 159 | // be always DTLS 1.0 |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 160 | helloVerifyRequest := &helloVerifyRequestMsg{ |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 161 | vers: VersionTLS10, |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 162 | cookie: make([]byte, 32), |
| 163 | } |
| 164 | if _, err := io.ReadFull(c.config.rand(), helloVerifyRequest.cookie); err != nil { |
| 165 | c.sendAlert(alertInternalError) |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 166 | return errors.New("dtls: short read from Rand: " + err.Error()) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 167 | } |
| 168 | c.writeRecord(recordTypeHandshake, helloVerifyRequest.marshal()) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 169 | c.flushHandshake() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 170 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 171 | if err := c.simulatePacketLoss(nil); err != nil { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 172 | return err |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 173 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 174 | msg, err := c.readHandshake() |
| 175 | if err != nil { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 176 | return err |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 177 | } |
| 178 | newClientHello, ok := msg.(*clientHelloMsg) |
| 179 | if !ok { |
| 180 | c.sendAlert(alertUnexpectedMessage) |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 181 | return unexpectedMessageError(hs.clientHello, msg) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 182 | } |
| 183 | if !bytes.Equal(newClientHello.cookie, helloVerifyRequest.cookie) { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 184 | return errors.New("dtls: invalid cookie") |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 185 | } |
David Benjamin | f2fedef | 2014-08-16 01:37:34 -0400 | [diff] [blame] | 186 | |
| 187 | // Apart from the cookie, the two ClientHellos must |
| 188 | // match. Note that clientHello.equal compares the |
| 189 | // serialization, so we make a copy. |
| 190 | oldClientHelloCopy := *hs.clientHello |
| 191 | oldClientHelloCopy.raw = nil |
| 192 | oldClientHelloCopy.cookie = nil |
| 193 | newClientHelloCopy := *newClientHello |
| 194 | newClientHelloCopy.raw = nil |
| 195 | newClientHelloCopy.cookie = nil |
| 196 | if !oldClientHelloCopy.equal(&newClientHelloCopy) { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 197 | return errors.New("dtls: retransmitted ClientHello does not match") |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 198 | } |
| 199 | hs.clientHello = newClientHello |
| 200 | } |
| 201 | |
David Benjamin | c44b1df | 2014-11-23 12:11:01 -0500 | [diff] [blame] | 202 | if config.Bugs.RequireSameRenegoClientVersion && c.clientVersion != 0 { |
| 203 | if c.clientVersion != hs.clientHello.vers { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 204 | return fmt.Errorf("tls: client offered different version on renego") |
David Benjamin | c44b1df | 2014-11-23 12:11:01 -0500 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | c.clientVersion = hs.clientHello.vers |
| 208 | |
David Benjamin | 6ae7f07 | 2015-01-26 10:22:13 -0500 | [diff] [blame] | 209 | // Reject < 1.2 ClientHellos with signature_algorithms. |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 210 | if c.clientVersion < VersionTLS12 && len(hs.clientHello.signatureAlgorithms) > 0 { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 211 | return fmt.Errorf("tls: client included signature_algorithms before TLS 1.2") |
David Benjamin | 72dc783 | 2015-03-16 17:49:43 -0400 | [diff] [blame] | 212 | } |
David Benjamin | 6ae7f07 | 2015-01-26 10:22:13 -0500 | [diff] [blame] | 213 | |
David Benjamin | f93995b | 2015-11-05 18:23:20 -0500 | [diff] [blame] | 214 | // Check the client cipher list is consistent with the version. |
| 215 | if hs.clientHello.vers < VersionTLS12 { |
| 216 | for _, id := range hs.clientHello.cipherSuites { |
| 217 | if isTLS12Cipher(id) { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 218 | return fmt.Errorf("tls: client offered TLS 1.2 cipher before TLS 1.2") |
David Benjamin | f93995b | 2015-11-05 18:23:20 -0500 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 223 | if config.Bugs.NegotiateVersion != 0 { |
| 224 | c.vers = config.Bugs.NegotiateVersion |
| 225 | } else { |
| 226 | c.vers, ok = config.mutualVersion(hs.clientHello.vers, c.isDTLS) |
| 227 | if !ok { |
| 228 | c.sendAlert(alertProtocolVersion) |
| 229 | return fmt.Errorf("tls: client offered an unsupported, maximum protocol version of %x", hs.clientHello.vers) |
| 230 | } |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | c.haveVers = true |
| 233 | |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 234 | var scsvFound bool |
| 235 | for _, cipherSuite := range hs.clientHello.cipherSuites { |
| 236 | if cipherSuite == fallbackSCSV { |
| 237 | scsvFound = true |
| 238 | break |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if !scsvFound && config.Bugs.FailIfNotFallbackSCSV { |
| 243 | return errors.New("tls: no fallback SCSV found when expected") |
| 244 | } else if scsvFound && !config.Bugs.FailIfNotFallbackSCSV { |
| 245 | return errors.New("tls: fallback SCSV found when not expected") |
| 246 | } |
| 247 | |
| 248 | if config.Bugs.IgnorePeerSignatureAlgorithmPreferences { |
David Benjamin | 7a41d37 | 2016-07-09 11:21:54 -0700 | [diff] [blame] | 249 | hs.clientHello.signatureAlgorithms = config.signSignatureAlgorithms() |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 250 | } |
| 251 | if config.Bugs.IgnorePeerCurvePreferences { |
| 252 | hs.clientHello.supportedCurves = config.curvePreferences() |
| 253 | } |
| 254 | if config.Bugs.IgnorePeerCipherPreferences { |
| 255 | hs.clientHello.cipherSuites = config.cipherSuites() |
| 256 | } |
| 257 | |
| 258 | return nil |
| 259 | } |
| 260 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 261 | func (hs *serverHandshakeState) doTLS13Handshake() error { |
| 262 | c := hs.c |
| 263 | config := c.config |
| 264 | |
| 265 | hs.hello = &serverHelloMsg{ |
| 266 | isDTLS: c.isDTLS, |
| 267 | vers: c.vers, |
| 268 | } |
| 269 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 270 | if config.Bugs.SendServerHelloVersion != 0 { |
| 271 | hs.hello.vers = config.Bugs.SendServerHelloVersion |
| 272 | } |
| 273 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 274 | hs.hello.random = make([]byte, 32) |
| 275 | if _, err := io.ReadFull(config.rand(), hs.hello.random); err != nil { |
| 276 | c.sendAlert(alertInternalError) |
| 277 | return err |
| 278 | } |
| 279 | |
| 280 | // TLS 1.3 forbids clients from advertising any non-null compression. |
| 281 | if len(hs.clientHello.compressionMethods) != 1 || hs.clientHello.compressionMethods[0] != compressionNone { |
| 282 | return errors.New("tls: client sent compression method other than null for TLS 1.3") |
| 283 | } |
| 284 | |
| 285 | // Prepare an EncryptedExtensions message, but do not send it yet. |
| 286 | encryptedExtensions := new(encryptedExtensionsMsg) |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 287 | encryptedExtensions.empty = config.Bugs.EmptyEncryptedExtensions |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 288 | if err := hs.processClientExtensions(&encryptedExtensions.extensions); err != nil { |
| 289 | return err |
| 290 | } |
| 291 | |
| 292 | supportedCurve := false |
| 293 | var selectedCurve CurveID |
| 294 | preferredCurves := config.curvePreferences() |
| 295 | Curves: |
| 296 | for _, curve := range hs.clientHello.supportedCurves { |
| 297 | for _, supported := range preferredCurves { |
| 298 | if supported == curve { |
| 299 | supportedCurve = true |
| 300 | selectedCurve = curve |
| 301 | break Curves |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | _, ecdsaOk := hs.cert.PrivateKey.(*ecdsa.PrivateKey) |
| 307 | |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame^] | 308 | for i, pskIdentity := range hs.clientHello.pskIdentities { |
| 309 | sessionState, ok := c.decryptTicket(pskIdentity) |
| 310 | if !ok { |
| 311 | continue |
| 312 | } |
| 313 | if sessionState.vers != c.vers { |
| 314 | continue |
| 315 | } |
| 316 | if sessionState.ticketFlags&ticketAllowDHEResumption == 0 { |
| 317 | continue |
| 318 | } |
| 319 | if sessionState.ticketExpiration.Before(c.config.time()) { |
| 320 | continue |
| 321 | } |
| 322 | suiteId := ecdhePSKSuite(sessionState.cipherSuite) |
| 323 | suite := mutualCipherSuite(hs.clientHello.cipherSuites, suiteId) |
| 324 | var found bool |
| 325 | for _, id := range config.cipherSuites() { |
| 326 | if id == sessionState.cipherSuite { |
| 327 | found = true |
| 328 | break |
| 329 | } |
| 330 | } |
| 331 | if suite != nil && found { |
| 332 | hs.sessionState = sessionState |
| 333 | hs.suite = suite |
| 334 | hs.hello.hasPSKIdentity = true |
| 335 | hs.hello.pskIdentity = uint16(i) |
| 336 | c.didResume = true |
| 337 | break |
| 338 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame^] | 341 | // If not resuming, select the cipher suite. |
| 342 | if hs.suite == nil { |
| 343 | var preferenceList, supportedList []uint16 |
| 344 | if config.PreferServerCipherSuites { |
| 345 | preferenceList = config.cipherSuites() |
| 346 | supportedList = hs.clientHello.cipherSuites |
| 347 | } else { |
| 348 | preferenceList = hs.clientHello.cipherSuites |
| 349 | supportedList = config.cipherSuites() |
| 350 | } |
| 351 | |
| 352 | for _, id := range preferenceList { |
| 353 | if hs.suite = c.tryCipherSuite(id, supportedList, c.vers, supportedCurve, ecdsaOk, false); hs.suite != nil { |
| 354 | break |
| 355 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | if hs.suite == nil { |
| 360 | c.sendAlert(alertHandshakeFailure) |
| 361 | return errors.New("tls: no cipher suite supported by both client and server") |
| 362 | } |
| 363 | |
| 364 | hs.hello.cipherSuite = hs.suite.id |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 365 | if c.config.Bugs.SendCipherSuite != 0 { |
| 366 | hs.hello.cipherSuite = c.config.Bugs.SendCipherSuite |
| 367 | } |
| 368 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 369 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
| 370 | hs.finishedHash.discardHandshakeBuffer() |
| 371 | hs.writeClientHash(hs.clientHello.marshal()) |
| 372 | |
| 373 | // Resolve PSK and compute the early secret. |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame^] | 374 | var psk []byte |
| 375 | // The only way for hs.suite to be a PSK suite yet for there to be |
| 376 | // no sessionState is if config.Bugs.EnableAllCiphers is true and |
| 377 | // the test runner forced us to negotiated a PSK suite. It doesn't |
| 378 | // really matter what we do here so long as we continue the |
| 379 | // handshake and let the client error out. |
| 380 | if hs.suite.flags&suitePSK != 0 && hs.sessionState != nil { |
| 381 | psk = deriveResumptionPSK(hs.suite, hs.sessionState.masterSecret) |
| 382 | hs.finishedHash.setResumptionContext(deriveResumptionContext(hs.suite, hs.sessionState.masterSecret)) |
| 383 | } else { |
| 384 | psk = hs.finishedHash.zeroSecret() |
| 385 | hs.finishedHash.setResumptionContext(hs.finishedHash.zeroSecret()) |
| 386 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 387 | |
| 388 | earlySecret := hs.finishedHash.extractKey(hs.finishedHash.zeroSecret(), psk) |
| 389 | |
| 390 | // Resolve ECDHE and compute the handshake secret. |
| 391 | var ecdheSecret []byte |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 392 | if hs.suite.flags&suiteECDHE != 0 && !config.Bugs.MissingKeyShare { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 393 | // Look for the key share corresponding to our selected curve. |
| 394 | var selectedKeyShare *keyShareEntry |
| 395 | for i := range hs.clientHello.keyShares { |
| 396 | if hs.clientHello.keyShares[i].group == selectedCurve { |
| 397 | selectedKeyShare = &hs.clientHello.keyShares[i] |
| 398 | break |
| 399 | } |
| 400 | } |
| 401 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 402 | sendHelloRetryRequest := selectedKeyShare == nil |
| 403 | if config.Bugs.UnnecessaryHelloRetryRequest { |
| 404 | sendHelloRetryRequest = true |
| 405 | } |
| 406 | if config.Bugs.SkipHelloRetryRequest { |
| 407 | sendHelloRetryRequest = false |
| 408 | } |
| 409 | if sendHelloRetryRequest { |
| 410 | firstTime := true |
| 411 | ResendHelloRetryRequest: |
Nick Harper | dcfbc67 | 2016-07-16 17:47:31 +0200 | [diff] [blame] | 412 | // Send HelloRetryRequest. |
| 413 | helloRetryRequestMsg := helloRetryRequestMsg{ |
| 414 | vers: c.vers, |
| 415 | cipherSuite: hs.hello.cipherSuite, |
| 416 | selectedGroup: selectedCurve, |
| 417 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 418 | if config.Bugs.SendHelloRetryRequestCurve != 0 { |
| 419 | helloRetryRequestMsg.selectedGroup = config.Bugs.SendHelloRetryRequestCurve |
| 420 | } |
Nick Harper | dcfbc67 | 2016-07-16 17:47:31 +0200 | [diff] [blame] | 421 | hs.writeServerHash(helloRetryRequestMsg.marshal()) |
| 422 | c.writeRecord(recordTypeHandshake, helloRetryRequestMsg.marshal()) |
| 423 | |
| 424 | // Read new ClientHello. |
| 425 | newMsg, err := c.readHandshake() |
| 426 | if err != nil { |
| 427 | return err |
| 428 | } |
| 429 | newClientHello, ok := newMsg.(*clientHelloMsg) |
| 430 | if !ok { |
| 431 | c.sendAlert(alertUnexpectedMessage) |
| 432 | return unexpectedMessageError(newClientHello, newMsg) |
| 433 | } |
| 434 | hs.writeClientHash(newClientHello.marshal()) |
| 435 | |
| 436 | // Check that the new ClientHello matches the old ClientHello, except for |
| 437 | // the addition of the new KeyShareEntry at the end of the list, and |
| 438 | // removing the EarlyDataIndication extension (if present). |
| 439 | newKeyShares := newClientHello.keyShares |
| 440 | if len(newKeyShares) == 0 || newKeyShares[len(newKeyShares)-1].group != selectedCurve { |
| 441 | return errors.New("tls: KeyShare from HelloRetryRequest not present in new ClientHello") |
| 442 | } |
| 443 | oldClientHelloCopy := *hs.clientHello |
| 444 | oldClientHelloCopy.raw = nil |
| 445 | oldClientHelloCopy.hasEarlyData = false |
| 446 | oldClientHelloCopy.earlyDataContext = nil |
| 447 | newClientHelloCopy := *newClientHello |
| 448 | newClientHelloCopy.raw = nil |
| 449 | newClientHelloCopy.keyShares = newKeyShares[:len(newKeyShares)-1] |
| 450 | if !oldClientHelloCopy.equal(&newClientHelloCopy) { |
| 451 | return errors.New("tls: new ClientHello does not match") |
| 452 | } |
| 453 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 454 | if firstTime && config.Bugs.SecondHelloRetryRequest { |
| 455 | firstTime = false |
| 456 | goto ResendHelloRetryRequest |
| 457 | } |
| 458 | |
Nick Harper | dcfbc67 | 2016-07-16 17:47:31 +0200 | [diff] [blame] | 459 | selectedKeyShare = &newKeyShares[len(newKeyShares)-1] |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // Once a curve has been selected and a key share identified, |
| 463 | // the server needs to generate a public value and send it in |
| 464 | // the ServerHello. |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 465 | curve, ok := curveForCurveID(selectedCurve) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 466 | if !ok { |
| 467 | panic("tls: server failed to look up curve ID") |
| 468 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 469 | c.curveID = selectedCurve |
| 470 | |
| 471 | var peerKey []byte |
| 472 | if config.Bugs.SkipHelloRetryRequest { |
| 473 | // If skipping HelloRetryRequest, use a random key to |
| 474 | // avoid crashing. |
| 475 | curve2, _ := curveForCurveID(selectedCurve) |
| 476 | var err error |
| 477 | peerKey, err = curve2.offer(config.rand()) |
| 478 | if err != nil { |
| 479 | return err |
| 480 | } |
| 481 | } else { |
| 482 | peerKey = selectedKeyShare.keyExchange |
| 483 | } |
| 484 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 485 | var publicKey []byte |
| 486 | var err error |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 487 | publicKey, ecdheSecret, err = curve.accept(config.rand(), peerKey) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 488 | if err != nil { |
| 489 | c.sendAlert(alertHandshakeFailure) |
| 490 | return err |
| 491 | } |
| 492 | hs.hello.hasKeyShare = true |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 493 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 494 | curveID := selectedCurve |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 495 | if c.config.Bugs.SendCurve != 0 { |
| 496 | curveID = config.Bugs.SendCurve |
| 497 | } |
| 498 | if c.config.Bugs.InvalidECDHPoint { |
| 499 | publicKey[0] ^= 0xff |
| 500 | } |
| 501 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 502 | hs.hello.keyShare = keyShareEntry{ |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 503 | group: curveID, |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 504 | keyExchange: publicKey, |
| 505 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 506 | |
| 507 | if config.Bugs.EncryptedExtensionsWithKeyShare { |
| 508 | encryptedExtensions.extensions.hasKeyShare = true |
| 509 | encryptedExtensions.extensions.keyShare = keyShareEntry{ |
| 510 | group: curveID, |
| 511 | keyExchange: publicKey, |
| 512 | } |
| 513 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 514 | } else { |
| 515 | ecdheSecret = hs.finishedHash.zeroSecret() |
| 516 | } |
| 517 | |
| 518 | // Send unencrypted ServerHello. |
| 519 | hs.writeServerHash(hs.hello.marshal()) |
David Benjamin | 7964b18 | 2016-07-14 23:36:30 -0400 | [diff] [blame] | 520 | if config.Bugs.PartialEncryptedExtensionsWithServerHello { |
| 521 | helloBytes := hs.hello.marshal() |
| 522 | toWrite := make([]byte, 0, len(helloBytes)+1) |
| 523 | toWrite = append(toWrite, helloBytes...) |
| 524 | toWrite = append(toWrite, typeEncryptedExtensions) |
| 525 | c.writeRecord(recordTypeHandshake, toWrite) |
| 526 | } else { |
| 527 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 528 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 529 | c.flushHandshake() |
| 530 | |
| 531 | // Compute the handshake secret. |
| 532 | handshakeSecret := hs.finishedHash.extractKey(earlySecret, ecdheSecret) |
| 533 | |
| 534 | // Switch to handshake traffic keys. |
| 535 | handshakeTrafficSecret := hs.finishedHash.deriveSecret(handshakeSecret, handshakeTrafficLabel) |
David Benjamin | 21c0028 | 2016-07-18 21:56:23 +0200 | [diff] [blame] | 536 | c.out.useTrafficSecret(c.vers, hs.suite, handshakeTrafficSecret, handshakePhase, serverWrite) |
| 537 | c.in.useTrafficSecret(c.vers, hs.suite, handshakeTrafficSecret, handshakePhase, clientWrite) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 538 | |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame^] | 539 | if hs.suite.flags&suitePSK == 0 { |
David Benjamin | 615119a | 2016-07-06 19:22:55 -0700 | [diff] [blame] | 540 | if hs.clientHello.ocspStapling { |
| 541 | encryptedExtensions.extensions.ocspResponse = hs.cert.OCSPStaple |
| 542 | } |
| 543 | if hs.clientHello.sctListSupported { |
| 544 | encryptedExtensions.extensions.sctList = hs.cert.SignedCertificateTimestampList |
| 545 | } |
| 546 | } |
| 547 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 548 | // Send EncryptedExtensions. |
| 549 | hs.writeServerHash(encryptedExtensions.marshal()) |
David Benjamin | 7964b18 | 2016-07-14 23:36:30 -0400 | [diff] [blame] | 550 | if config.Bugs.PartialEncryptedExtensionsWithServerHello { |
| 551 | // The first byte has already been sent. |
| 552 | c.writeRecord(recordTypeHandshake, encryptedExtensions.marshal()[1:]) |
| 553 | } else { |
| 554 | c.writeRecord(recordTypeHandshake, encryptedExtensions.marshal()) |
| 555 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 556 | |
| 557 | if hs.suite.flags&suitePSK == 0 { |
| 558 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 559 | // Request a client certificate |
| 560 | certReq := &certificateRequestMsg{ |
| 561 | hasSignatureAlgorithm: true, |
| 562 | hasRequestContext: true, |
| 563 | } |
| 564 | if !config.Bugs.NoSignatureAlgorithms { |
David Benjamin | f74ec79 | 2016-07-13 21:18:49 -0400 | [diff] [blame] | 565 | certReq.signatureAlgorithms = config.verifySignatureAlgorithms() |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // An empty list of certificateAuthorities signals to |
| 569 | // the client that it may send any certificate in response |
| 570 | // to our request. When we know the CAs we trust, then |
| 571 | // we can send them down, so that the client can choose |
| 572 | // an appropriate certificate to give to us. |
| 573 | if config.ClientCAs != nil { |
| 574 | certReq.certificateAuthorities = config.ClientCAs.Subjects() |
| 575 | } |
| 576 | hs.writeServerHash(certReq.marshal()) |
| 577 | c.writeRecord(recordTypeHandshake, certReq.marshal()) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | certMsg := &certificateMsg{ |
| 581 | hasRequestContext: true, |
| 582 | } |
| 583 | if !config.Bugs.EmptyCertificateList { |
| 584 | certMsg.certificates = hs.cert.Certificate |
| 585 | } |
David Benjamin | 1edae6b | 2016-07-13 16:58:23 -0400 | [diff] [blame] | 586 | certMsgBytes := certMsg.marshal() |
David Benjamin | 1edae6b | 2016-07-13 16:58:23 -0400 | [diff] [blame] | 587 | hs.writeServerHash(certMsgBytes) |
| 588 | c.writeRecord(recordTypeHandshake, certMsgBytes) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 589 | |
| 590 | certVerify := &certificateVerifyMsg{ |
| 591 | hasSignatureAlgorithm: true, |
| 592 | } |
| 593 | |
| 594 | // Determine the hash to sign. |
| 595 | privKey := hs.cert.PrivateKey |
| 596 | |
| 597 | var err error |
| 598 | certVerify.signatureAlgorithm, err = selectSignatureAlgorithm(c.vers, privKey, config, hs.clientHello.signatureAlgorithms) |
| 599 | if err != nil { |
| 600 | c.sendAlert(alertInternalError) |
| 601 | return err |
| 602 | } |
| 603 | |
| 604 | input := hs.finishedHash.certificateVerifyInput(serverCertificateVerifyContextTLS13) |
| 605 | certVerify.signature, err = signMessage(c.vers, privKey, c.config, certVerify.signatureAlgorithm, input) |
| 606 | if err != nil { |
| 607 | c.sendAlert(alertInternalError) |
| 608 | return err |
| 609 | } |
| 610 | |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 611 | if config.Bugs.SendSignatureAlgorithm != 0 { |
| 612 | certVerify.signatureAlgorithm = config.Bugs.SendSignatureAlgorithm |
| 613 | } |
| 614 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 615 | hs.writeServerHash(certVerify.marshal()) |
| 616 | c.writeRecord(recordTypeHandshake, certVerify.marshal()) |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame^] | 617 | } else { |
| 618 | // Pick up certificates from the session instead. |
| 619 | // hs.sessionState may be nil if config.Bugs.EnableAllCiphers is |
| 620 | // true. |
| 621 | if hs.sessionState != nil && len(hs.sessionState.certificates) > 0 { |
| 622 | if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil { |
| 623 | return err |
| 624 | } |
| 625 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | finished := new(finishedMsg) |
| 629 | finished.verifyData = hs.finishedHash.serverSum(handshakeTrafficSecret) |
| 630 | if config.Bugs.BadFinished { |
| 631 | finished.verifyData[0]++ |
| 632 | } |
| 633 | hs.writeServerHash(finished.marshal()) |
| 634 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
David Benjamin | 02edcd0 | 2016-07-27 17:40:37 -0400 | [diff] [blame] | 635 | if c.config.Bugs.SendExtraFinished { |
| 636 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
| 637 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 638 | c.flushHandshake() |
| 639 | |
| 640 | // The various secrets do not incorporate the client's final leg, so |
| 641 | // derive them now before updating the handshake context. |
| 642 | masterSecret := hs.finishedHash.extractKey(handshakeSecret, hs.finishedHash.zeroSecret()) |
| 643 | trafficSecret := hs.finishedHash.deriveSecret(masterSecret, applicationTrafficLabel) |
| 644 | |
David Benjamin | 2aad406 | 2016-07-14 23:15:40 -0400 | [diff] [blame] | 645 | // Switch to application data keys on write. In particular, any alerts |
| 646 | // from the client certificate are sent over these keys. |
David Benjamin | 21c0028 | 2016-07-18 21:56:23 +0200 | [diff] [blame] | 647 | c.out.useTrafficSecret(c.vers, hs.suite, trafficSecret, applicationPhase, serverWrite) |
David Benjamin | 2aad406 | 2016-07-14 23:15:40 -0400 | [diff] [blame] | 648 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 649 | // If we requested a client certificate, then the client must send a |
| 650 | // certificate message, even if it's empty. |
| 651 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 652 | msg, err := c.readHandshake() |
| 653 | if err != nil { |
| 654 | return err |
| 655 | } |
| 656 | |
| 657 | certMsg, ok := msg.(*certificateMsg) |
| 658 | if !ok { |
| 659 | c.sendAlert(alertUnexpectedMessage) |
| 660 | return unexpectedMessageError(certMsg, msg) |
| 661 | } |
| 662 | hs.writeClientHash(certMsg.marshal()) |
| 663 | |
| 664 | if len(certMsg.certificates) == 0 { |
| 665 | // The client didn't actually send a certificate |
| 666 | switch config.ClientAuth { |
| 667 | case RequireAnyClientCert, RequireAndVerifyClientCert: |
| 668 | c.sendAlert(alertBadCertificate) |
| 669 | return errors.New("tls: client didn't provide a certificate") |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | pub, err := hs.processCertsFromClient(certMsg.certificates) |
| 674 | if err != nil { |
| 675 | return err |
| 676 | } |
| 677 | |
| 678 | if len(c.peerCertificates) > 0 { |
| 679 | msg, err = c.readHandshake() |
| 680 | if err != nil { |
| 681 | return err |
| 682 | } |
| 683 | |
| 684 | certVerify, ok := msg.(*certificateVerifyMsg) |
| 685 | if !ok { |
| 686 | c.sendAlert(alertUnexpectedMessage) |
| 687 | return unexpectedMessageError(certVerify, msg) |
| 688 | } |
| 689 | |
David Benjamin | f74ec79 | 2016-07-13 21:18:49 -0400 | [diff] [blame] | 690 | c.peerSignatureAlgorithm = certVerify.signatureAlgorithm |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 691 | input := hs.finishedHash.certificateVerifyInput(clientCertificateVerifyContextTLS13) |
| 692 | if err := verifyMessage(c.vers, pub, config, certVerify.signatureAlgorithm, input, certVerify.signature); err != nil { |
| 693 | c.sendAlert(alertBadCertificate) |
| 694 | return err |
| 695 | } |
| 696 | hs.writeClientHash(certVerify.marshal()) |
| 697 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | // Read the client Finished message. |
| 701 | msg, err := c.readHandshake() |
| 702 | if err != nil { |
| 703 | return err |
| 704 | } |
| 705 | clientFinished, ok := msg.(*finishedMsg) |
| 706 | if !ok { |
| 707 | c.sendAlert(alertUnexpectedMessage) |
| 708 | return unexpectedMessageError(clientFinished, msg) |
| 709 | } |
| 710 | |
| 711 | verify := hs.finishedHash.clientSum(handshakeTrafficSecret) |
| 712 | if len(verify) != len(clientFinished.verifyData) || |
| 713 | subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 { |
| 714 | c.sendAlert(alertHandshakeFailure) |
| 715 | return errors.New("tls: client's Finished message was incorrect") |
| 716 | } |
David Benjamin | 97a0a08 | 2016-07-13 17:57:35 -0400 | [diff] [blame] | 717 | hs.writeClientHash(clientFinished.marshal()) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 718 | |
David Benjamin | 2aad406 | 2016-07-14 23:15:40 -0400 | [diff] [blame] | 719 | // Switch to application data keys on read. |
David Benjamin | 21c0028 | 2016-07-18 21:56:23 +0200 | [diff] [blame] | 720 | c.in.useTrafficSecret(c.vers, hs.suite, trafficSecret, applicationPhase, clientWrite) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 721 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 722 | c.cipherSuite = hs.suite |
David Benjamin | 97a0a08 | 2016-07-13 17:57:35 -0400 | [diff] [blame] | 723 | c.exporterSecret = hs.finishedHash.deriveSecret(masterSecret, exporterLabel) |
David Benjamin | 5810488 | 2016-07-18 01:25:41 +0200 | [diff] [blame] | 724 | c.resumptionSecret = hs.finishedHash.deriveSecret(masterSecret, resumptionLabel) |
| 725 | |
| 726 | // TODO(davidben): Allow configuring the number of tickets sent for |
| 727 | // testing. |
| 728 | if !c.config.SessionTicketsDisabled { |
| 729 | ticketCount := 2 |
| 730 | for i := 0; i < ticketCount; i++ { |
| 731 | c.SendNewSessionTicket() |
| 732 | } |
| 733 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 734 | return nil |
| 735 | } |
| 736 | |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 737 | // processClientHello processes the ClientHello message from the client and |
| 738 | // decides whether we will perform session resumption. |
| 739 | func (hs *serverHandshakeState) processClientHello() (isResume bool, err error) { |
| 740 | config := hs.c.config |
| 741 | c := hs.c |
| 742 | |
| 743 | hs.hello = &serverHelloMsg{ |
| 744 | isDTLS: c.isDTLS, |
| 745 | vers: c.vers, |
| 746 | compressionMethod: compressionNone, |
| 747 | } |
| 748 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 749 | if config.Bugs.SendServerHelloVersion != 0 { |
| 750 | hs.hello.vers = config.Bugs.SendServerHelloVersion |
| 751 | } |
| 752 | |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 753 | hs.hello.random = make([]byte, 32) |
| 754 | _, err = io.ReadFull(config.rand(), hs.hello.random) |
| 755 | if err != nil { |
| 756 | c.sendAlert(alertInternalError) |
| 757 | return false, err |
| 758 | } |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 759 | // Signal downgrades in the server random, per draft-ietf-tls-tls13-14, |
| 760 | // section 6.3.1.2. |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 761 | if c.vers <= VersionTLS12 && config.maxVersion(c.isDTLS) >= VersionTLS13 { |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 762 | copy(hs.hello.random[len(hs.hello.random)-8:], downgradeTLS13) |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 763 | } |
| 764 | if c.vers <= VersionTLS11 && config.maxVersion(c.isDTLS) == VersionTLS12 { |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 765 | copy(hs.hello.random[len(hs.hello.random)-8:], downgradeTLS12) |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 766 | } |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 767 | |
| 768 | foundCompression := false |
| 769 | // We only support null compression, so check that the client offered it. |
| 770 | for _, compression := range hs.clientHello.compressionMethods { |
| 771 | if compression == compressionNone { |
| 772 | foundCompression = true |
| 773 | break |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | if !foundCompression { |
| 778 | c.sendAlert(alertHandshakeFailure) |
| 779 | return false, errors.New("tls: client does not support uncompressed connections") |
| 780 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 781 | |
| 782 | if err := hs.processClientExtensions(&hs.hello.extensions); err != nil { |
| 783 | return false, err |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 784 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 785 | |
| 786 | supportedCurve := false |
| 787 | preferredCurves := config.curvePreferences() |
| 788 | Curves: |
| 789 | for _, curve := range hs.clientHello.supportedCurves { |
| 790 | for _, supported := range preferredCurves { |
| 791 | if supported == curve { |
| 792 | supportedCurve = true |
| 793 | break Curves |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | supportedPointFormat := false |
| 799 | for _, pointFormat := range hs.clientHello.supportedPoints { |
| 800 | if pointFormat == pointFormatUncompressed { |
| 801 | supportedPointFormat = true |
| 802 | break |
| 803 | } |
| 804 | } |
| 805 | hs.ellipticOk = supportedCurve && supportedPointFormat |
| 806 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 807 | _, hs.ecdsaOk = hs.cert.PrivateKey.(*ecdsa.PrivateKey) |
| 808 | |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 809 | // For test purposes, check that the peer never offers a session when |
| 810 | // renegotiating. |
| 811 | if c.cipherSuite != nil && len(hs.clientHello.sessionId) > 0 && c.config.Bugs.FailIfResumeOnRenego { |
| 812 | return false, errors.New("tls: offered resumption on renegotiation") |
| 813 | } |
| 814 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 815 | if c.config.Bugs.FailIfSessionOffered && (len(hs.clientHello.sessionTicket) > 0 || len(hs.clientHello.sessionId) > 0) { |
| 816 | return false, errors.New("tls: client offered a session ticket or ID") |
| 817 | } |
| 818 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 819 | if hs.checkForResumption() { |
| 820 | return true, nil |
| 821 | } |
| 822 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 823 | var preferenceList, supportedList []uint16 |
| 824 | if c.config.PreferServerCipherSuites { |
| 825 | preferenceList = c.config.cipherSuites() |
| 826 | supportedList = hs.clientHello.cipherSuites |
| 827 | } else { |
| 828 | preferenceList = hs.clientHello.cipherSuites |
| 829 | supportedList = c.config.cipherSuites() |
| 830 | } |
| 831 | |
| 832 | for _, id := range preferenceList { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 833 | if hs.suite = c.tryCipherSuite(id, supportedList, c.vers, hs.ellipticOk, hs.ecdsaOk, true); hs.suite != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 834 | break |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | if hs.suite == nil { |
| 839 | c.sendAlert(alertHandshakeFailure) |
| 840 | return false, errors.New("tls: no cipher suite supported by both client and server") |
| 841 | } |
| 842 | |
| 843 | return false, nil |
| 844 | } |
| 845 | |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 846 | // processClientExtensions processes all ClientHello extensions not directly |
| 847 | // related to cipher suite negotiation and writes responses in serverExtensions. |
| 848 | func (hs *serverHandshakeState) processClientExtensions(serverExtensions *serverExtensions) error { |
| 849 | config := hs.c.config |
| 850 | c := hs.c |
| 851 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 852 | if c.vers < VersionTLS13 || config.Bugs.NegotiateRenegotiationInfoAtAllVersions { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 853 | if !bytes.Equal(c.clientVerify, hs.clientHello.secureRenegotiation) { |
| 854 | c.sendAlert(alertHandshakeFailure) |
| 855 | return errors.New("tls: renegotiation mismatch") |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 856 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 857 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 858 | if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo { |
| 859 | serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.clientVerify...) |
| 860 | serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.serverVerify...) |
| 861 | if c.config.Bugs.BadRenegotiationInfo { |
| 862 | serverExtensions.secureRenegotiation[0] ^= 0x80 |
| 863 | } |
| 864 | } else { |
| 865 | serverExtensions.secureRenegotiation = hs.clientHello.secureRenegotiation |
| 866 | } |
| 867 | |
| 868 | if c.noRenegotiationInfo() { |
| 869 | serverExtensions.secureRenegotiation = nil |
| 870 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | serverExtensions.duplicateExtension = c.config.Bugs.DuplicateExtension |
| 874 | |
| 875 | if len(hs.clientHello.serverName) > 0 { |
| 876 | c.serverName = hs.clientHello.serverName |
| 877 | } |
| 878 | if len(config.Certificates) == 0 { |
| 879 | c.sendAlert(alertInternalError) |
| 880 | return errors.New("tls: no certificates configured") |
| 881 | } |
| 882 | hs.cert = &config.Certificates[0] |
| 883 | if len(hs.clientHello.serverName) > 0 { |
| 884 | hs.cert = config.getCertificateForName(hs.clientHello.serverName) |
| 885 | } |
| 886 | if expected := c.config.Bugs.ExpectServerName; expected != "" && expected != hs.clientHello.serverName { |
| 887 | return errors.New("tls: unexpected server name") |
| 888 | } |
| 889 | |
| 890 | if len(hs.clientHello.alpnProtocols) > 0 { |
| 891 | if proto := c.config.Bugs.ALPNProtocol; proto != nil { |
| 892 | serverExtensions.alpnProtocol = *proto |
| 893 | serverExtensions.alpnProtocolEmpty = len(*proto) == 0 |
| 894 | c.clientProtocol = *proto |
| 895 | c.usedALPN = true |
| 896 | } else if selectedProto, fallback := mutualProtocol(hs.clientHello.alpnProtocols, c.config.NextProtos); !fallback { |
| 897 | serverExtensions.alpnProtocol = selectedProto |
| 898 | c.clientProtocol = selectedProto |
| 899 | c.usedALPN = true |
| 900 | } |
| 901 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 902 | |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 903 | if len(c.config.Bugs.SendALPN) > 0 { |
| 904 | serverExtensions.alpnProtocol = c.config.Bugs.SendALPN |
| 905 | } |
| 906 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 907 | if c.vers < VersionTLS13 || config.Bugs.NegotiateNPNAtAllVersions { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 908 | if len(hs.clientHello.alpnProtocols) == 0 || c.config.Bugs.NegotiateALPNAndNPN { |
| 909 | // Although sending an empty NPN extension is reasonable, Firefox has |
| 910 | // had a bug around this. Best to send nothing at all if |
| 911 | // config.NextProtos is empty. See |
| 912 | // https://code.google.com/p/go/issues/detail?id=5445. |
| 913 | if hs.clientHello.nextProtoNeg && len(config.NextProtos) > 0 { |
| 914 | serverExtensions.nextProtoNeg = true |
| 915 | serverExtensions.nextProtos = config.NextProtos |
| 916 | serverExtensions.npnLast = config.Bugs.SwapNPNAndALPN |
| 917 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 918 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 919 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 920 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 921 | if c.vers < VersionTLS13 || config.Bugs.NegotiateEMSAtAllVersions { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 922 | serverExtensions.extendedMasterSecret = c.vers >= VersionTLS10 && hs.clientHello.extendedMasterSecret && !c.config.Bugs.NoExtendedMasterSecret |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 923 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 924 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 925 | if c.vers < VersionTLS13 || config.Bugs.NegotiateChannelIDAtAllVersions { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 926 | if hs.clientHello.channelIDSupported && config.RequestChannelID { |
| 927 | serverExtensions.channelIDRequested = true |
| 928 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if hs.clientHello.srtpProtectionProfiles != nil { |
| 932 | SRTPLoop: |
| 933 | for _, p1 := range c.config.SRTPProtectionProfiles { |
| 934 | for _, p2 := range hs.clientHello.srtpProtectionProfiles { |
| 935 | if p1 == p2 { |
| 936 | serverExtensions.srtpProtectionProfile = p1 |
| 937 | c.srtpProtectionProfile = p1 |
| 938 | break SRTPLoop |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | if c.config.Bugs.SendSRTPProtectionProfile != 0 { |
| 945 | serverExtensions.srtpProtectionProfile = c.config.Bugs.SendSRTPProtectionProfile |
| 946 | } |
| 947 | |
| 948 | if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil { |
| 949 | if hs.clientHello.customExtension != *expected { |
| 950 | return fmt.Errorf("tls: bad custom extension contents %q", hs.clientHello.customExtension) |
| 951 | } |
| 952 | } |
| 953 | serverExtensions.customExtension = config.Bugs.CustomExtension |
| 954 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 955 | if c.config.Bugs.AdvertiseTicketExtension { |
| 956 | serverExtensions.ticketSupported = true |
| 957 | } |
| 958 | |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 959 | return nil |
| 960 | } |
| 961 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 962 | // checkForResumption returns true if we should perform resumption on this connection. |
| 963 | func (hs *serverHandshakeState) checkForResumption() bool { |
| 964 | c := hs.c |
| 965 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 966 | if len(hs.clientHello.sessionTicket) > 0 { |
| 967 | if c.config.SessionTicketsDisabled { |
| 968 | return false |
| 969 | } |
David Benjamin | b0c8db7 | 2014-09-24 15:19:56 -0400 | [diff] [blame] | 970 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 971 | var ok bool |
| 972 | if hs.sessionState, ok = c.decryptTicket(hs.clientHello.sessionTicket); !ok { |
| 973 | return false |
| 974 | } |
| 975 | } else { |
| 976 | if c.config.ServerSessionCache == nil { |
| 977 | return false |
| 978 | } |
| 979 | |
| 980 | var ok bool |
| 981 | sessionId := string(hs.clientHello.sessionId) |
| 982 | if hs.sessionState, ok = c.config.ServerSessionCache.Get(sessionId); !ok { |
| 983 | return false |
| 984 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 985 | } |
| 986 | |
David Benjamin | e18d821 | 2014-11-10 02:37:15 -0500 | [diff] [blame] | 987 | // Never resume a session for a different SSL version. |
| 988 | if !c.config.Bugs.AllowSessionVersionMismatch && c.vers != hs.sessionState.vers { |
| 989 | return false |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | cipherSuiteOk := false |
| 993 | // Check that the client is still offering the ciphersuite in the session. |
| 994 | for _, id := range hs.clientHello.cipherSuites { |
| 995 | if id == hs.sessionState.cipherSuite { |
| 996 | cipherSuiteOk = true |
| 997 | break |
| 998 | } |
| 999 | } |
| 1000 | if !cipherSuiteOk { |
| 1001 | return false |
| 1002 | } |
| 1003 | |
| 1004 | // Check that we also support the ciphersuite from the session. |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1005 | hs.suite = c.tryCipherSuite(hs.sessionState.cipherSuite, c.config.cipherSuites(), hs.sessionState.vers, hs.ellipticOk, hs.ecdsaOk, true) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1006 | if hs.suite == nil { |
| 1007 | return false |
| 1008 | } |
| 1009 | |
| 1010 | sessionHasClientCerts := len(hs.sessionState.certificates) != 0 |
| 1011 | needClientCerts := c.config.ClientAuth == RequireAnyClientCert || c.config.ClientAuth == RequireAndVerifyClientCert |
| 1012 | if needClientCerts && !sessionHasClientCerts { |
| 1013 | return false |
| 1014 | } |
| 1015 | if sessionHasClientCerts && c.config.ClientAuth == NoClientCert { |
| 1016 | return false |
| 1017 | } |
| 1018 | |
| 1019 | return true |
| 1020 | } |
| 1021 | |
| 1022 | func (hs *serverHandshakeState) doResumeHandshake() error { |
| 1023 | c := hs.c |
| 1024 | |
| 1025 | hs.hello.cipherSuite = hs.suite.id |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 1026 | if c.config.Bugs.SendCipherSuite != 0 { |
| 1027 | hs.hello.cipherSuite = c.config.Bugs.SendCipherSuite |
| 1028 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1029 | // We echo the client's session ID in the ServerHello to let it know |
| 1030 | // that we're doing a resumption. |
| 1031 | hs.hello.sessionId = hs.clientHello.sessionId |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1032 | hs.hello.extensions.ticketSupported = c.config.Bugs.RenewTicketOnResume |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1033 | |
David Benjamin | 80d1b35 | 2016-05-04 19:19:06 -0400 | [diff] [blame] | 1034 | if c.config.Bugs.SendSCTListOnResume != nil { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1035 | hs.hello.extensions.sctList = c.config.Bugs.SendSCTListOnResume |
David Benjamin | 80d1b35 | 2016-05-04 19:19:06 -0400 | [diff] [blame] | 1036 | } |
| 1037 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1038 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1039 | hs.finishedHash.discardHandshakeBuffer() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1040 | hs.writeClientHash(hs.clientHello.marshal()) |
| 1041 | hs.writeServerHash(hs.hello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1042 | |
| 1043 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 1044 | |
| 1045 | if len(hs.sessionState.certificates) > 0 { |
| 1046 | if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil { |
| 1047 | return err |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | hs.masterSecret = hs.sessionState.masterSecret |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1052 | c.extendedMasterSecret = hs.sessionState.extendedMasterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1053 | |
| 1054 | return nil |
| 1055 | } |
| 1056 | |
| 1057 | func (hs *serverHandshakeState) doFullHandshake() error { |
| 1058 | config := hs.c.config |
| 1059 | c := hs.c |
| 1060 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1061 | isPSK := hs.suite.flags&suitePSK != 0 |
| 1062 | if !isPSK && hs.clientHello.ocspStapling && len(hs.cert.OCSPStaple) > 0 { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1063 | hs.hello.extensions.ocspStapling = true |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1066 | if hs.clientHello.sctListSupported && len(hs.cert.SignedCertificateTimestampList) > 0 { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1067 | hs.hello.extensions.sctList = hs.cert.SignedCertificateTimestampList |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1068 | } |
| 1069 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1070 | hs.hello.extensions.ticketSupported = hs.clientHello.ticketSupported && !config.SessionTicketsDisabled && c.vers > VersionSSL30 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1071 | hs.hello.cipherSuite = hs.suite.id |
David Benjamin | 6095de8 | 2014-12-27 01:50:38 -0500 | [diff] [blame] | 1072 | if config.Bugs.SendCipherSuite != 0 { |
| 1073 | hs.hello.cipherSuite = config.Bugs.SendCipherSuite |
| 1074 | } |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1075 | c.extendedMasterSecret = hs.hello.extensions.extendedMasterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1076 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1077 | // Generate a session ID if we're to save the session. |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1078 | if !hs.hello.extensions.ticketSupported && config.ServerSessionCache != nil { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1079 | hs.hello.sessionId = make([]byte, 32) |
| 1080 | if _, err := io.ReadFull(config.rand(), hs.hello.sessionId); err != nil { |
| 1081 | c.sendAlert(alertInternalError) |
| 1082 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 1083 | } |
| 1084 | } |
| 1085 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1086 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1087 | hs.writeClientHash(hs.clientHello.marshal()) |
| 1088 | hs.writeServerHash(hs.hello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1089 | |
| 1090 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 1091 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1092 | if !isPSK { |
| 1093 | certMsg := new(certificateMsg) |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 1094 | if !config.Bugs.EmptyCertificateList { |
| 1095 | certMsg.certificates = hs.cert.Certificate |
| 1096 | } |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1097 | if !config.Bugs.UnauthenticatedECDH { |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 1098 | certMsgBytes := certMsg.marshal() |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 1099 | hs.writeServerHash(certMsgBytes) |
| 1100 | c.writeRecord(recordTypeHandshake, certMsgBytes) |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1101 | } |
David Benjamin | 1c375dd | 2014-07-12 00:48:23 -0400 | [diff] [blame] | 1102 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1103 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1104 | if hs.hello.extensions.ocspStapling && !c.config.Bugs.SkipCertificateStatus { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1105 | certStatus := new(certificateStatusMsg) |
| 1106 | certStatus.statusType = statusTypeOCSP |
| 1107 | certStatus.response = hs.cert.OCSPStaple |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1108 | hs.writeServerHash(certStatus.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1109 | c.writeRecord(recordTypeHandshake, certStatus.marshal()) |
| 1110 | } |
| 1111 | |
| 1112 | keyAgreement := hs.suite.ka(c.vers) |
| 1113 | skx, err := keyAgreement.generateServerKeyExchange(config, hs.cert, hs.clientHello, hs.hello) |
| 1114 | if err != nil { |
| 1115 | c.sendAlert(alertHandshakeFailure) |
| 1116 | return err |
| 1117 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 1118 | if ecdhe, ok := keyAgreement.(*ecdheKeyAgreement); ok { |
| 1119 | c.curveID = ecdhe.curveID |
| 1120 | } |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1121 | if skx != nil && !config.Bugs.SkipServerKeyExchange { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1122 | hs.writeServerHash(skx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1123 | c.writeRecord(recordTypeHandshake, skx.marshal()) |
| 1124 | } |
| 1125 | |
| 1126 | if config.ClientAuth >= RequestClientCert { |
| 1127 | // Request a client certificate |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 1128 | certReq := &certificateRequestMsg{ |
| 1129 | certificateTypes: config.ClientCertificateTypes, |
| 1130 | } |
| 1131 | if certReq.certificateTypes == nil { |
| 1132 | certReq.certificateTypes = []byte{ |
| 1133 | byte(CertTypeRSASign), |
| 1134 | byte(CertTypeECDSASign), |
| 1135 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1136 | } |
| 1137 | if c.vers >= VersionTLS12 { |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1138 | certReq.hasSignatureAlgorithm = true |
| 1139 | if !config.Bugs.NoSignatureAlgorithms { |
David Benjamin | 7a41d37 | 2016-07-09 11:21:54 -0700 | [diff] [blame] | 1140 | certReq.signatureAlgorithms = config.verifySignatureAlgorithms() |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 1141 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | // An empty list of certificateAuthorities signals to |
| 1145 | // the client that it may send any certificate in response |
| 1146 | // to our request. When we know the CAs we trust, then |
| 1147 | // we can send them down, so that the client can choose |
| 1148 | // an appropriate certificate to give to us. |
| 1149 | if config.ClientCAs != nil { |
| 1150 | certReq.certificateAuthorities = config.ClientCAs.Subjects() |
| 1151 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1152 | hs.writeServerHash(certReq.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1153 | c.writeRecord(recordTypeHandshake, certReq.marshal()) |
| 1154 | } |
| 1155 | |
| 1156 | helloDone := new(serverHelloDoneMsg) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1157 | hs.writeServerHash(helloDone.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1158 | c.writeRecord(recordTypeHandshake, helloDone.marshal()) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1159 | c.flushHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1160 | |
| 1161 | var pub crypto.PublicKey // public key for client auth, if any |
| 1162 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1163 | if err := c.simulatePacketLoss(nil); err != nil { |
| 1164 | return err |
| 1165 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1166 | msg, err := c.readHandshake() |
| 1167 | if err != nil { |
| 1168 | return err |
| 1169 | } |
| 1170 | |
| 1171 | var ok bool |
| 1172 | // If we requested a client certificate, then the client must send a |
| 1173 | // certificate message, even if it's empty. |
| 1174 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1175 | var certMsg *certificateMsg |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1176 | var certificates [][]byte |
| 1177 | if certMsg, ok = msg.(*certificateMsg); ok { |
| 1178 | if c.vers == VersionSSL30 && len(certMsg.certificates) == 0 { |
| 1179 | return errors.New("tls: empty certificate message in SSL 3.0") |
| 1180 | } |
| 1181 | |
| 1182 | hs.writeClientHash(certMsg.marshal()) |
| 1183 | certificates = certMsg.certificates |
| 1184 | } else if c.vers != VersionSSL30 { |
| 1185 | // In TLS, the Certificate message is required. In SSL |
| 1186 | // 3.0, the peer skips it when sending no certificates. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1187 | c.sendAlert(alertUnexpectedMessage) |
| 1188 | return unexpectedMessageError(certMsg, msg) |
| 1189 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1190 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1191 | if len(certificates) == 0 { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1192 | // The client didn't actually send a certificate |
| 1193 | switch config.ClientAuth { |
| 1194 | case RequireAnyClientCert, RequireAndVerifyClientCert: |
| 1195 | c.sendAlert(alertBadCertificate) |
| 1196 | return errors.New("tls: client didn't provide a certificate") |
| 1197 | } |
| 1198 | } |
| 1199 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1200 | pub, err = hs.processCertsFromClient(certificates) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1201 | if err != nil { |
| 1202 | return err |
| 1203 | } |
| 1204 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1205 | if ok { |
| 1206 | msg, err = c.readHandshake() |
| 1207 | if err != nil { |
| 1208 | return err |
| 1209 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | // Get client key exchange |
| 1214 | ckx, ok := msg.(*clientKeyExchangeMsg) |
| 1215 | if !ok { |
| 1216 | c.sendAlert(alertUnexpectedMessage) |
| 1217 | return unexpectedMessageError(ckx, msg) |
| 1218 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1219 | hs.writeClientHash(ckx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1220 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1221 | preMasterSecret, err := keyAgreement.processClientKeyExchange(config, hs.cert, ckx, c.vers) |
| 1222 | if err != nil { |
| 1223 | c.sendAlert(alertHandshakeFailure) |
| 1224 | return err |
| 1225 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1226 | if c.extendedMasterSecret { |
| 1227 | hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 1228 | } else { |
| 1229 | if c.config.Bugs.RequireExtendedMasterSecret { |
| 1230 | return errors.New("tls: extended master secret required but not supported by peer") |
| 1231 | } |
| 1232 | hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.clientHello.random, hs.hello.random) |
| 1233 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1234 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1235 | // If we received a client cert in response to our certificate request message, |
| 1236 | // the client will send us a certificateVerifyMsg immediately after the |
| 1237 | // clientKeyExchangeMsg. This message is a digest of all preceding |
| 1238 | // handshake-layer messages that is signed using the private key corresponding |
| 1239 | // to the client's certificate. This allows us to verify that the client is in |
| 1240 | // possession of the private key of the certificate. |
| 1241 | if len(c.peerCertificates) > 0 { |
| 1242 | msg, err = c.readHandshake() |
| 1243 | if err != nil { |
| 1244 | return err |
| 1245 | } |
| 1246 | certVerify, ok := msg.(*certificateVerifyMsg) |
| 1247 | if !ok { |
| 1248 | c.sendAlert(alertUnexpectedMessage) |
| 1249 | return unexpectedMessageError(certVerify, msg) |
| 1250 | } |
| 1251 | |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1252 | // Determine the signature type. |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1253 | var sigAlg signatureAlgorithm |
| 1254 | if certVerify.hasSignatureAlgorithm { |
| 1255 | sigAlg = certVerify.signatureAlgorithm |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1256 | c.peerSignatureAlgorithm = sigAlg |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1257 | } |
| 1258 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1259 | if c.vers > VersionSSL30 { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 1260 | err = verifyMessage(c.vers, pub, c.config, sigAlg, hs.finishedHash.buffer, certVerify.signature) |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1261 | } else { |
| 1262 | // SSL 3.0's client certificate construction is |
| 1263 | // incompatible with signatureAlgorithm. |
| 1264 | rsaPub, ok := pub.(*rsa.PublicKey) |
| 1265 | if !ok { |
| 1266 | err = errors.New("unsupported key type for client certificate") |
| 1267 | } else { |
| 1268 | digest := hs.finishedHash.hashForClientCertificateSSL3(hs.masterSecret) |
| 1269 | err = rsa.VerifyPKCS1v15(rsaPub, crypto.MD5SHA1, digest, certVerify.signature) |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1270 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1271 | } |
| 1272 | if err != nil { |
| 1273 | c.sendAlert(alertBadCertificate) |
| 1274 | return errors.New("could not validate signature of connection nonces: " + err.Error()) |
| 1275 | } |
| 1276 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1277 | hs.writeClientHash(certVerify.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1280 | hs.finishedHash.discardHandshakeBuffer() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1281 | |
| 1282 | return nil |
| 1283 | } |
| 1284 | |
| 1285 | func (hs *serverHandshakeState) establishKeys() error { |
| 1286 | c := hs.c |
| 1287 | |
| 1288 | clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1289 | keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.clientHello.random, hs.hello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen(c.vers)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1290 | |
| 1291 | var clientCipher, serverCipher interface{} |
| 1292 | var clientHash, serverHash macFunction |
| 1293 | |
| 1294 | if hs.suite.aead == nil { |
| 1295 | clientCipher = hs.suite.cipher(clientKey, clientIV, true /* for reading */) |
| 1296 | clientHash = hs.suite.mac(c.vers, clientMAC) |
| 1297 | serverCipher = hs.suite.cipher(serverKey, serverIV, false /* not for reading */) |
| 1298 | serverHash = hs.suite.mac(c.vers, serverMAC) |
| 1299 | } else { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1300 | clientCipher = hs.suite.aead(c.vers, clientKey, clientIV) |
| 1301 | serverCipher = hs.suite.aead(c.vers, serverKey, serverIV) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | c.in.prepareCipherSpec(c.vers, clientCipher, clientHash) |
| 1305 | c.out.prepareCipherSpec(c.vers, serverCipher, serverHash) |
| 1306 | |
| 1307 | return nil |
| 1308 | } |
| 1309 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1310 | func (hs *serverHandshakeState) readFinished(out []byte, isResume bool) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1311 | c := hs.c |
| 1312 | |
| 1313 | c.readRecord(recordTypeChangeCipherSpec) |
| 1314 | if err := c.in.error(); err != nil { |
| 1315 | return err |
| 1316 | } |
| 1317 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1318 | if hs.hello.extensions.nextProtoNeg { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1319 | msg, err := c.readHandshake() |
| 1320 | if err != nil { |
| 1321 | return err |
| 1322 | } |
| 1323 | nextProto, ok := msg.(*nextProtoMsg) |
| 1324 | if !ok { |
| 1325 | c.sendAlert(alertUnexpectedMessage) |
| 1326 | return unexpectedMessageError(nextProto, msg) |
| 1327 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1328 | hs.writeClientHash(nextProto.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1329 | c.clientProtocol = nextProto.proto |
| 1330 | } |
| 1331 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1332 | if hs.hello.extensions.channelIDRequested { |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1333 | msg, err := c.readHandshake() |
| 1334 | if err != nil { |
| 1335 | return err |
| 1336 | } |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1337 | channelIDMsg, ok := msg.(*channelIDMsg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1338 | if !ok { |
| 1339 | c.sendAlert(alertUnexpectedMessage) |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1340 | return unexpectedMessageError(channelIDMsg, msg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1341 | } |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1342 | x := new(big.Int).SetBytes(channelIDMsg.channelID[0:32]) |
| 1343 | y := new(big.Int).SetBytes(channelIDMsg.channelID[32:64]) |
| 1344 | r := new(big.Int).SetBytes(channelIDMsg.channelID[64:96]) |
| 1345 | s := new(big.Int).SetBytes(channelIDMsg.channelID[96:128]) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1346 | if !elliptic.P256().IsOnCurve(x, y) { |
| 1347 | return errors.New("tls: invalid channel ID public key") |
| 1348 | } |
| 1349 | channelID := &ecdsa.PublicKey{elliptic.P256(), x, y} |
| 1350 | var resumeHash []byte |
| 1351 | if isResume { |
| 1352 | resumeHash = hs.sessionState.handshakeHash |
| 1353 | } |
| 1354 | if !ecdsa.Verify(channelID, hs.finishedHash.hashForChannelID(resumeHash), r, s) { |
| 1355 | return errors.New("tls: invalid channel ID signature") |
| 1356 | } |
| 1357 | c.channelID = channelID |
| 1358 | |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1359 | hs.writeClientHash(channelIDMsg.marshal()) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1360 | } |
| 1361 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1362 | msg, err := c.readHandshake() |
| 1363 | if err != nil { |
| 1364 | return err |
| 1365 | } |
| 1366 | clientFinished, ok := msg.(*finishedMsg) |
| 1367 | if !ok { |
| 1368 | c.sendAlert(alertUnexpectedMessage) |
| 1369 | return unexpectedMessageError(clientFinished, msg) |
| 1370 | } |
| 1371 | |
| 1372 | verify := hs.finishedHash.clientSum(hs.masterSecret) |
| 1373 | if len(verify) != len(clientFinished.verifyData) || |
| 1374 | subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 { |
| 1375 | c.sendAlert(alertHandshakeFailure) |
| 1376 | return errors.New("tls: client's Finished message is incorrect") |
| 1377 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1378 | c.clientVerify = append(c.clientVerify[:0], clientFinished.verifyData...) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1379 | copy(out, clientFinished.verifyData) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1380 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1381 | hs.writeClientHash(clientFinished.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1382 | return nil |
| 1383 | } |
| 1384 | |
| 1385 | func (hs *serverHandshakeState) sendSessionTicket() error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1386 | c := hs.c |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1387 | state := sessionState{ |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1388 | vers: c.vers, |
| 1389 | cipherSuite: hs.suite.id, |
| 1390 | masterSecret: hs.masterSecret, |
| 1391 | certificates: hs.certsFromClient, |
| 1392 | handshakeHash: hs.finishedHash.server.Sum(nil), |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1393 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1394 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1395 | if !hs.hello.extensions.ticketSupported || hs.c.config.Bugs.SkipNewSessionTicket { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1396 | if c.config.ServerSessionCache != nil && len(hs.hello.sessionId) != 0 { |
| 1397 | c.config.ServerSessionCache.Put(string(hs.hello.sessionId), &state) |
| 1398 | } |
| 1399 | return nil |
| 1400 | } |
| 1401 | |
| 1402 | m := new(newSessionTicketMsg) |
| 1403 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1404 | if !c.config.Bugs.SendEmptySessionTicket { |
| 1405 | var err error |
| 1406 | m.ticket, err = c.encryptTicket(&state) |
| 1407 | if err != nil { |
| 1408 | return err |
| 1409 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1410 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1411 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1412 | hs.writeServerHash(m.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1413 | c.writeRecord(recordTypeHandshake, m.marshal()) |
| 1414 | |
| 1415 | return nil |
| 1416 | } |
| 1417 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1418 | func (hs *serverHandshakeState) sendFinished(out []byte) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1419 | c := hs.c |
| 1420 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1421 | finished := new(finishedMsg) |
| 1422 | finished.verifyData = hs.finishedHash.serverSum(hs.masterSecret) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1423 | copy(out, finished.verifyData) |
David Benjamin | 513f0ea | 2015-04-02 19:33:31 -0400 | [diff] [blame] | 1424 | if c.config.Bugs.BadFinished { |
| 1425 | finished.verifyData[0]++ |
| 1426 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1427 | c.serverVerify = append(c.serverVerify[:0], finished.verifyData...) |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1428 | hs.finishedBytes = finished.marshal() |
| 1429 | hs.writeServerHash(hs.finishedBytes) |
| 1430 | postCCSBytes := hs.finishedBytes |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1431 | |
| 1432 | if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
| 1433 | c.writeRecord(recordTypeHandshake, postCCSBytes[:5]) |
| 1434 | postCCSBytes = postCCSBytes[5:] |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 1435 | } else if c.config.Bugs.SendUnencryptedFinished { |
| 1436 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
| 1437 | postCCSBytes = nil |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1438 | } |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1439 | c.flushHandshake() |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1440 | |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 1441 | if !c.config.Bugs.SkipChangeCipherSpec { |
David Benjamin | 8411b24 | 2015-11-26 12:07:28 -0500 | [diff] [blame] | 1442 | ccs := []byte{1} |
| 1443 | if c.config.Bugs.BadChangeCipherSpec != nil { |
| 1444 | ccs = c.config.Bugs.BadChangeCipherSpec |
| 1445 | } |
| 1446 | c.writeRecord(recordTypeChangeCipherSpec, ccs) |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 1447 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1448 | |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 1449 | if c.config.Bugs.AppDataAfterChangeCipherSpec != nil { |
| 1450 | c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec) |
| 1451 | } |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 1452 | if c.config.Bugs.AlertAfterChangeCipherSpec != 0 { |
| 1453 | c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec) |
| 1454 | return errors.New("tls: simulating post-CCS alert") |
| 1455 | } |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 1456 | |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 1457 | if !c.config.Bugs.SkipFinished && len(postCCSBytes) > 0 { |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 1458 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
David Benjamin | 02edcd0 | 2016-07-27 17:40:37 -0400 | [diff] [blame] | 1459 | if c.config.Bugs.SendExtraFinished { |
| 1460 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
| 1461 | } |
| 1462 | |
David Benjamin | 12d2c48 | 2016-07-24 10:56:51 -0400 | [diff] [blame] | 1463 | if !c.config.Bugs.PackHelloRequestWithFinished { |
| 1464 | // Defer flushing until renegotiation. |
| 1465 | c.flushHandshake() |
| 1466 | } |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 1467 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1468 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1469 | c.cipherSuite = hs.suite |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1470 | |
| 1471 | return nil |
| 1472 | } |
| 1473 | |
| 1474 | // processCertsFromClient takes a chain of client certificates either from a |
| 1475 | // Certificates message or from a sessionState and verifies them. It returns |
| 1476 | // the public key of the leaf certificate. |
| 1477 | func (hs *serverHandshakeState) processCertsFromClient(certificates [][]byte) (crypto.PublicKey, error) { |
| 1478 | c := hs.c |
| 1479 | |
| 1480 | hs.certsFromClient = certificates |
| 1481 | certs := make([]*x509.Certificate, len(certificates)) |
| 1482 | var err error |
| 1483 | for i, asn1Data := range certificates { |
| 1484 | if certs[i], err = x509.ParseCertificate(asn1Data); err != nil { |
| 1485 | c.sendAlert(alertBadCertificate) |
| 1486 | return nil, errors.New("tls: failed to parse client certificate: " + err.Error()) |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | if c.config.ClientAuth >= VerifyClientCertIfGiven && len(certs) > 0 { |
| 1491 | opts := x509.VerifyOptions{ |
| 1492 | Roots: c.config.ClientCAs, |
| 1493 | CurrentTime: c.config.time(), |
| 1494 | Intermediates: x509.NewCertPool(), |
| 1495 | KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, |
| 1496 | } |
| 1497 | |
| 1498 | for _, cert := range certs[1:] { |
| 1499 | opts.Intermediates.AddCert(cert) |
| 1500 | } |
| 1501 | |
| 1502 | chains, err := certs[0].Verify(opts) |
| 1503 | if err != nil { |
| 1504 | c.sendAlert(alertBadCertificate) |
| 1505 | return nil, errors.New("tls: failed to verify client's certificate: " + err.Error()) |
| 1506 | } |
| 1507 | |
| 1508 | ok := false |
| 1509 | for _, ku := range certs[0].ExtKeyUsage { |
| 1510 | if ku == x509.ExtKeyUsageClientAuth { |
| 1511 | ok = true |
| 1512 | break |
| 1513 | } |
| 1514 | } |
| 1515 | if !ok { |
| 1516 | c.sendAlert(alertHandshakeFailure) |
| 1517 | return nil, errors.New("tls: client's certificate's extended key usage doesn't permit it to be used for client authentication") |
| 1518 | } |
| 1519 | |
| 1520 | c.verifiedChains = chains |
| 1521 | } |
| 1522 | |
| 1523 | if len(certs) > 0 { |
| 1524 | var pub crypto.PublicKey |
| 1525 | switch key := certs[0].PublicKey.(type) { |
| 1526 | case *ecdsa.PublicKey, *rsa.PublicKey: |
| 1527 | pub = key |
| 1528 | default: |
| 1529 | c.sendAlert(alertUnsupportedCertificate) |
| 1530 | return nil, fmt.Errorf("tls: client's certificate contains an unsupported public key of type %T", certs[0].PublicKey) |
| 1531 | } |
| 1532 | c.peerCertificates = certs |
| 1533 | return pub, nil |
| 1534 | } |
| 1535 | |
| 1536 | return nil, nil |
| 1537 | } |
| 1538 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1539 | func (hs *serverHandshakeState) writeServerHash(msg []byte) { |
| 1540 | // writeServerHash is called before writeRecord. |
| 1541 | hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 1542 | } |
| 1543 | |
| 1544 | func (hs *serverHandshakeState) writeClientHash(msg []byte) { |
| 1545 | // writeClientHash is called after readHandshake. |
| 1546 | hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 1547 | } |
| 1548 | |
| 1549 | func (hs *serverHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 1550 | if hs.c.isDTLS { |
| 1551 | // This is somewhat hacky. DTLS hashes a slightly different format. |
| 1552 | // First, the TLS header. |
| 1553 | hs.finishedHash.Write(msg[:4]) |
| 1554 | // Then the sequence number and reassembled fragment offset (always 0). |
| 1555 | hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 1556 | // Then the reassembled fragment (always equal to the message length). |
| 1557 | hs.finishedHash.Write(msg[1:4]) |
| 1558 | // And then the message body. |
| 1559 | hs.finishedHash.Write(msg[4:]) |
| 1560 | } else { |
| 1561 | hs.finishedHash.Write(msg) |
| 1562 | } |
| 1563 | } |
| 1564 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1565 | // tryCipherSuite returns a cipherSuite with the given id if that cipher suite |
| 1566 | // is acceptable to use. |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1567 | func (c *Conn) tryCipherSuite(id uint16, supportedCipherSuites []uint16, version uint16, ellipticOk, ecdsaOk, pskOk bool) *cipherSuite { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1568 | for _, supported := range supportedCipherSuites { |
| 1569 | if id == supported { |
| 1570 | var candidate *cipherSuite |
| 1571 | |
| 1572 | for _, s := range cipherSuites { |
| 1573 | if s.id == id { |
| 1574 | candidate = s |
| 1575 | break |
| 1576 | } |
| 1577 | } |
| 1578 | if candidate == nil { |
| 1579 | continue |
| 1580 | } |
| 1581 | // Don't select a ciphersuite which we can't |
| 1582 | // support for this client. |
David Benjamin | 0407e76 | 2016-06-17 16:41:18 -0400 | [diff] [blame] | 1583 | if !c.config.Bugs.EnableAllCiphers { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1584 | if (candidate.flags&suitePSK != 0) && !pskOk { |
| 1585 | continue |
| 1586 | } |
David Benjamin | 0407e76 | 2016-06-17 16:41:18 -0400 | [diff] [blame] | 1587 | if (candidate.flags&suiteECDHE != 0) && !ellipticOk { |
| 1588 | continue |
| 1589 | } |
| 1590 | if (candidate.flags&suiteECDSA != 0) != ecdsaOk { |
| 1591 | continue |
| 1592 | } |
| 1593 | if version < VersionTLS12 && candidate.flags&suiteTLS12 != 0 { |
| 1594 | continue |
| 1595 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1596 | if version >= VersionTLS13 && candidate.flags&suiteTLS13 == 0 { |
| 1597 | continue |
| 1598 | } |
David Benjamin | 0407e76 | 2016-06-17 16:41:18 -0400 | [diff] [blame] | 1599 | if c.isDTLS && candidate.flags&suiteNoDTLS != 0 { |
| 1600 | continue |
| 1601 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1602 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1603 | return candidate |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | return nil |
| 1608 | } |
David Benjamin | f93995b | 2015-11-05 18:23:20 -0500 | [diff] [blame] | 1609 | |
| 1610 | func isTLS12Cipher(id uint16) bool { |
| 1611 | for _, cipher := range cipherSuites { |
| 1612 | if cipher.id != id { |
| 1613 | continue |
| 1614 | } |
| 1615 | return cipher.flags&suiteTLS12 != 0 |
| 1616 | } |
| 1617 | // Unknown cipher. |
| 1618 | return false |
| 1619 | } |