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