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 | } |
| 436 | hs.writeServerHash(certMsg.marshal()) |
| 437 | c.writeRecord(recordTypeHandshake, certMsg.marshal()) |
| 438 | |
| 439 | certVerify := &certificateVerifyMsg{ |
| 440 | hasSignatureAlgorithm: true, |
| 441 | } |
| 442 | |
| 443 | // Determine the hash to sign. |
| 444 | privKey := hs.cert.PrivateKey |
| 445 | |
| 446 | var err error |
| 447 | certVerify.signatureAlgorithm, err = selectSignatureAlgorithm(c.vers, privKey, config, hs.clientHello.signatureAlgorithms) |
| 448 | if err != nil { |
| 449 | c.sendAlert(alertInternalError) |
| 450 | return err |
| 451 | } |
| 452 | |
| 453 | input := hs.finishedHash.certificateVerifyInput(serverCertificateVerifyContextTLS13) |
| 454 | certVerify.signature, err = signMessage(c.vers, privKey, c.config, certVerify.signatureAlgorithm, input) |
| 455 | if err != nil { |
| 456 | c.sendAlert(alertInternalError) |
| 457 | return err |
| 458 | } |
| 459 | |
| 460 | hs.writeServerHash(certVerify.marshal()) |
| 461 | c.writeRecord(recordTypeHandshake, certVerify.marshal()) |
| 462 | } |
| 463 | |
| 464 | finished := new(finishedMsg) |
| 465 | finished.verifyData = hs.finishedHash.serverSum(handshakeTrafficSecret) |
| 466 | if config.Bugs.BadFinished { |
| 467 | finished.verifyData[0]++ |
| 468 | } |
| 469 | hs.writeServerHash(finished.marshal()) |
| 470 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
| 471 | c.flushHandshake() |
| 472 | |
| 473 | // The various secrets do not incorporate the client's final leg, so |
| 474 | // derive them now before updating the handshake context. |
| 475 | masterSecret := hs.finishedHash.extractKey(handshakeSecret, hs.finishedHash.zeroSecret()) |
| 476 | trafficSecret := hs.finishedHash.deriveSecret(masterSecret, applicationTrafficLabel) |
| 477 | |
| 478 | // If we requested a client certificate, then the client must send a |
| 479 | // certificate message, even if it's empty. |
| 480 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame^] | 481 | msg, err := c.readHandshake() |
| 482 | if err != nil { |
| 483 | return err |
| 484 | } |
| 485 | |
| 486 | certMsg, ok := msg.(*certificateMsg) |
| 487 | if !ok { |
| 488 | c.sendAlert(alertUnexpectedMessage) |
| 489 | return unexpectedMessageError(certMsg, msg) |
| 490 | } |
| 491 | hs.writeClientHash(certMsg.marshal()) |
| 492 | |
| 493 | if len(certMsg.certificates) == 0 { |
| 494 | // The client didn't actually send a certificate |
| 495 | switch config.ClientAuth { |
| 496 | case RequireAnyClientCert, RequireAndVerifyClientCert: |
| 497 | c.sendAlert(alertBadCertificate) |
| 498 | return errors.New("tls: client didn't provide a certificate") |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | pub, err := hs.processCertsFromClient(certMsg.certificates) |
| 503 | if err != nil { |
| 504 | return err |
| 505 | } |
| 506 | |
| 507 | if len(c.peerCertificates) > 0 { |
| 508 | msg, err = c.readHandshake() |
| 509 | if err != nil { |
| 510 | return err |
| 511 | } |
| 512 | |
| 513 | certVerify, ok := msg.(*certificateVerifyMsg) |
| 514 | if !ok { |
| 515 | c.sendAlert(alertUnexpectedMessage) |
| 516 | return unexpectedMessageError(certVerify, msg) |
| 517 | } |
| 518 | |
| 519 | input := hs.finishedHash.certificateVerifyInput(clientCertificateVerifyContextTLS13) |
| 520 | if err := verifyMessage(c.vers, pub, config, certVerify.signatureAlgorithm, input, certVerify.signature); err != nil { |
| 521 | c.sendAlert(alertBadCertificate) |
| 522 | return err |
| 523 | } |
| 524 | hs.writeClientHash(certVerify.marshal()) |
| 525 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // Read the client Finished message. |
| 529 | msg, err := c.readHandshake() |
| 530 | if err != nil { |
| 531 | return err |
| 532 | } |
| 533 | clientFinished, ok := msg.(*finishedMsg) |
| 534 | if !ok { |
| 535 | c.sendAlert(alertUnexpectedMessage) |
| 536 | return unexpectedMessageError(clientFinished, msg) |
| 537 | } |
| 538 | |
| 539 | verify := hs.finishedHash.clientSum(handshakeTrafficSecret) |
| 540 | if len(verify) != len(clientFinished.verifyData) || |
| 541 | subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 { |
| 542 | c.sendAlert(alertHandshakeFailure) |
| 543 | return errors.New("tls: client's Finished message was incorrect") |
| 544 | } |
| 545 | |
| 546 | // Switch to application data keys. |
| 547 | c.out.updateKeys(deriveTrafficAEAD(c.vers, hs.suite, trafficSecret, applicationPhase, serverWrite), c.vers) |
| 548 | c.in.updateKeys(deriveTrafficAEAD(c.vers, hs.suite, trafficSecret, applicationPhase, clientWrite), c.vers) |
| 549 | |
| 550 | // TODO(davidben): Derive and save the exporter master secret for key exporters. Swap out the masterSecret field. |
| 551 | // TODO(davidben): Derive and save the resumption master secret for receiving tickets. |
| 552 | // TODO(davidben): Save the traffic secret for KeyUpdate. |
| 553 | c.cipherSuite = hs.suite |
| 554 | return nil |
| 555 | } |
| 556 | |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 557 | // processClientHello processes the ClientHello message from the client and |
| 558 | // decides whether we will perform session resumption. |
| 559 | func (hs *serverHandshakeState) processClientHello() (isResume bool, err error) { |
| 560 | config := hs.c.config |
| 561 | c := hs.c |
| 562 | |
| 563 | hs.hello = &serverHelloMsg{ |
| 564 | isDTLS: c.isDTLS, |
| 565 | vers: c.vers, |
| 566 | compressionMethod: compressionNone, |
| 567 | } |
| 568 | |
| 569 | hs.hello.random = make([]byte, 32) |
| 570 | _, err = io.ReadFull(config.rand(), hs.hello.random) |
| 571 | if err != nil { |
| 572 | c.sendAlert(alertInternalError) |
| 573 | return false, err |
| 574 | } |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 575 | // Signal downgrades in the server random, per draft-ietf-tls-tls13-14, |
| 576 | // section 6.3.1.2. |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 577 | if c.vers <= VersionTLS12 && config.maxVersion(c.isDTLS) >= VersionTLS13 { |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 578 | copy(hs.hello.random[len(hs.hello.random)-8:], downgradeTLS13) |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 579 | } |
| 580 | if c.vers <= VersionTLS11 && config.maxVersion(c.isDTLS) == VersionTLS12 { |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 581 | copy(hs.hello.random[len(hs.hello.random)-8:], downgradeTLS12) |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 582 | } |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 583 | |
| 584 | foundCompression := false |
| 585 | // We only support null compression, so check that the client offered it. |
| 586 | for _, compression := range hs.clientHello.compressionMethods { |
| 587 | if compression == compressionNone { |
| 588 | foundCompression = true |
| 589 | break |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if !foundCompression { |
| 594 | c.sendAlert(alertHandshakeFailure) |
| 595 | return false, errors.New("tls: client does not support uncompressed connections") |
| 596 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 597 | |
| 598 | if err := hs.processClientExtensions(&hs.hello.extensions); err != nil { |
| 599 | return false, err |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 600 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 601 | |
| 602 | supportedCurve := false |
| 603 | preferredCurves := config.curvePreferences() |
| 604 | Curves: |
| 605 | for _, curve := range hs.clientHello.supportedCurves { |
| 606 | for _, supported := range preferredCurves { |
| 607 | if supported == curve { |
| 608 | supportedCurve = true |
| 609 | break Curves |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | supportedPointFormat := false |
| 615 | for _, pointFormat := range hs.clientHello.supportedPoints { |
| 616 | if pointFormat == pointFormatUncompressed { |
| 617 | supportedPointFormat = true |
| 618 | break |
| 619 | } |
| 620 | } |
| 621 | hs.ellipticOk = supportedCurve && supportedPointFormat |
| 622 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | _, hs.ecdsaOk = hs.cert.PrivateKey.(*ecdsa.PrivateKey) |
| 624 | |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 625 | // For test purposes, check that the peer never offers a session when |
| 626 | // renegotiating. |
| 627 | if c.cipherSuite != nil && len(hs.clientHello.sessionId) > 0 && c.config.Bugs.FailIfResumeOnRenego { |
| 628 | return false, errors.New("tls: offered resumption on renegotiation") |
| 629 | } |
| 630 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 631 | if c.config.Bugs.FailIfSessionOffered && (len(hs.clientHello.sessionTicket) > 0 || len(hs.clientHello.sessionId) > 0) { |
| 632 | return false, errors.New("tls: client offered a session ticket or ID") |
| 633 | } |
| 634 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | if hs.checkForResumption() { |
| 636 | return true, nil |
| 637 | } |
| 638 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 639 | var preferenceList, supportedList []uint16 |
| 640 | if c.config.PreferServerCipherSuites { |
| 641 | preferenceList = c.config.cipherSuites() |
| 642 | supportedList = hs.clientHello.cipherSuites |
| 643 | } else { |
| 644 | preferenceList = hs.clientHello.cipherSuites |
| 645 | supportedList = c.config.cipherSuites() |
| 646 | } |
| 647 | |
| 648 | for _, id := range preferenceList { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 649 | 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] | 650 | break |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if hs.suite == nil { |
| 655 | c.sendAlert(alertHandshakeFailure) |
| 656 | return false, errors.New("tls: no cipher suite supported by both client and server") |
| 657 | } |
| 658 | |
| 659 | return false, nil |
| 660 | } |
| 661 | |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 662 | // processClientExtensions processes all ClientHello extensions not directly |
| 663 | // related to cipher suite negotiation and writes responses in serverExtensions. |
| 664 | func (hs *serverHandshakeState) processClientExtensions(serverExtensions *serverExtensions) error { |
| 665 | config := hs.c.config |
| 666 | c := hs.c |
| 667 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 668 | if c.vers < VersionTLS13 || !enableTLS13Handshake { |
| 669 | if !bytes.Equal(c.clientVerify, hs.clientHello.secureRenegotiation) { |
| 670 | c.sendAlert(alertHandshakeFailure) |
| 671 | return errors.New("tls: renegotiation mismatch") |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 672 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 673 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 674 | if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo { |
| 675 | serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.clientVerify...) |
| 676 | serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.serverVerify...) |
| 677 | if c.config.Bugs.BadRenegotiationInfo { |
| 678 | serverExtensions.secureRenegotiation[0] ^= 0x80 |
| 679 | } |
| 680 | } else { |
| 681 | serverExtensions.secureRenegotiation = hs.clientHello.secureRenegotiation |
| 682 | } |
| 683 | |
| 684 | if c.noRenegotiationInfo() { |
| 685 | serverExtensions.secureRenegotiation = nil |
| 686 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | serverExtensions.duplicateExtension = c.config.Bugs.DuplicateExtension |
| 690 | |
| 691 | if len(hs.clientHello.serverName) > 0 { |
| 692 | c.serverName = hs.clientHello.serverName |
| 693 | } |
| 694 | if len(config.Certificates) == 0 { |
| 695 | c.sendAlert(alertInternalError) |
| 696 | return errors.New("tls: no certificates configured") |
| 697 | } |
| 698 | hs.cert = &config.Certificates[0] |
| 699 | if len(hs.clientHello.serverName) > 0 { |
| 700 | hs.cert = config.getCertificateForName(hs.clientHello.serverName) |
| 701 | } |
| 702 | if expected := c.config.Bugs.ExpectServerName; expected != "" && expected != hs.clientHello.serverName { |
| 703 | return errors.New("tls: unexpected server name") |
| 704 | } |
| 705 | |
| 706 | if len(hs.clientHello.alpnProtocols) > 0 { |
| 707 | if proto := c.config.Bugs.ALPNProtocol; proto != nil { |
| 708 | serverExtensions.alpnProtocol = *proto |
| 709 | serverExtensions.alpnProtocolEmpty = len(*proto) == 0 |
| 710 | c.clientProtocol = *proto |
| 711 | c.usedALPN = true |
| 712 | } else if selectedProto, fallback := mutualProtocol(hs.clientHello.alpnProtocols, c.config.NextProtos); !fallback { |
| 713 | serverExtensions.alpnProtocol = selectedProto |
| 714 | c.clientProtocol = selectedProto |
| 715 | c.usedALPN = true |
| 716 | } |
| 717 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 718 | |
| 719 | if c.vers < VersionTLS13 || !enableTLS13Handshake { |
| 720 | if len(hs.clientHello.alpnProtocols) == 0 || c.config.Bugs.NegotiateALPNAndNPN { |
| 721 | // Although sending an empty NPN extension is reasonable, Firefox has |
| 722 | // had a bug around this. Best to send nothing at all if |
| 723 | // config.NextProtos is empty. See |
| 724 | // https://code.google.com/p/go/issues/detail?id=5445. |
| 725 | if hs.clientHello.nextProtoNeg && len(config.NextProtos) > 0 { |
| 726 | serverExtensions.nextProtoNeg = true |
| 727 | serverExtensions.nextProtos = config.NextProtos |
| 728 | serverExtensions.npnLast = config.Bugs.SwapNPNAndALPN |
| 729 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 730 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 731 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 732 | serverExtensions.extendedMasterSecret = c.vers >= VersionTLS10 && hs.clientHello.extendedMasterSecret && !c.config.Bugs.NoExtendedMasterSecret |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 733 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 734 | if hs.clientHello.channelIDSupported && config.RequestChannelID { |
| 735 | serverExtensions.channelIDRequested = true |
| 736 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | if hs.clientHello.srtpProtectionProfiles != nil { |
| 740 | SRTPLoop: |
| 741 | for _, p1 := range c.config.SRTPProtectionProfiles { |
| 742 | for _, p2 := range hs.clientHello.srtpProtectionProfiles { |
| 743 | if p1 == p2 { |
| 744 | serverExtensions.srtpProtectionProfile = p1 |
| 745 | c.srtpProtectionProfile = p1 |
| 746 | break SRTPLoop |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if c.config.Bugs.SendSRTPProtectionProfile != 0 { |
| 753 | serverExtensions.srtpProtectionProfile = c.config.Bugs.SendSRTPProtectionProfile |
| 754 | } |
| 755 | |
| 756 | if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil { |
| 757 | if hs.clientHello.customExtension != *expected { |
| 758 | return fmt.Errorf("tls: bad custom extension contents %q", hs.clientHello.customExtension) |
| 759 | } |
| 760 | } |
| 761 | serverExtensions.customExtension = config.Bugs.CustomExtension |
| 762 | |
| 763 | return nil |
| 764 | } |
| 765 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 766 | // checkForResumption returns true if we should perform resumption on this connection. |
| 767 | func (hs *serverHandshakeState) checkForResumption() bool { |
| 768 | c := hs.c |
| 769 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 770 | if len(hs.clientHello.sessionTicket) > 0 { |
| 771 | if c.config.SessionTicketsDisabled { |
| 772 | return false |
| 773 | } |
David Benjamin | b0c8db7 | 2014-09-24 15:19:56 -0400 | [diff] [blame] | 774 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 775 | var ok bool |
| 776 | if hs.sessionState, ok = c.decryptTicket(hs.clientHello.sessionTicket); !ok { |
| 777 | return false |
| 778 | } |
| 779 | } else { |
| 780 | if c.config.ServerSessionCache == nil { |
| 781 | return false |
| 782 | } |
| 783 | |
| 784 | var ok bool |
| 785 | sessionId := string(hs.clientHello.sessionId) |
| 786 | if hs.sessionState, ok = c.config.ServerSessionCache.Get(sessionId); !ok { |
| 787 | return false |
| 788 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 789 | } |
| 790 | |
David Benjamin | e18d821 | 2014-11-10 02:37:15 -0500 | [diff] [blame] | 791 | // Never resume a session for a different SSL version. |
| 792 | if !c.config.Bugs.AllowSessionVersionMismatch && c.vers != hs.sessionState.vers { |
| 793 | return false |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | cipherSuiteOk := false |
| 797 | // Check that the client is still offering the ciphersuite in the session. |
| 798 | for _, id := range hs.clientHello.cipherSuites { |
| 799 | if id == hs.sessionState.cipherSuite { |
| 800 | cipherSuiteOk = true |
| 801 | break |
| 802 | } |
| 803 | } |
| 804 | if !cipherSuiteOk { |
| 805 | return false |
| 806 | } |
| 807 | |
| 808 | // Check that we also support the ciphersuite from the session. |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 809 | 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] | 810 | if hs.suite == nil { |
| 811 | return false |
| 812 | } |
| 813 | |
| 814 | sessionHasClientCerts := len(hs.sessionState.certificates) != 0 |
| 815 | needClientCerts := c.config.ClientAuth == RequireAnyClientCert || c.config.ClientAuth == RequireAndVerifyClientCert |
| 816 | if needClientCerts && !sessionHasClientCerts { |
| 817 | return false |
| 818 | } |
| 819 | if sessionHasClientCerts && c.config.ClientAuth == NoClientCert { |
| 820 | return false |
| 821 | } |
| 822 | |
| 823 | return true |
| 824 | } |
| 825 | |
| 826 | func (hs *serverHandshakeState) doResumeHandshake() error { |
| 827 | c := hs.c |
| 828 | |
| 829 | hs.hello.cipherSuite = hs.suite.id |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 830 | if c.config.Bugs.SendCipherSuite != 0 { |
| 831 | hs.hello.cipherSuite = c.config.Bugs.SendCipherSuite |
| 832 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 833 | // We echo the client's session ID in the ServerHello to let it know |
| 834 | // that we're doing a resumption. |
| 835 | hs.hello.sessionId = hs.clientHello.sessionId |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 836 | hs.hello.extensions.ticketSupported = c.config.Bugs.RenewTicketOnResume |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 837 | |
David Benjamin | 80d1b35 | 2016-05-04 19:19:06 -0400 | [diff] [blame] | 838 | if c.config.Bugs.SendSCTListOnResume != nil { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 839 | hs.hello.extensions.sctList = c.config.Bugs.SendSCTListOnResume |
David Benjamin | 80d1b35 | 2016-05-04 19:19:06 -0400 | [diff] [blame] | 840 | } |
| 841 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 842 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 843 | hs.finishedHash.discardHandshakeBuffer() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 844 | hs.writeClientHash(hs.clientHello.marshal()) |
| 845 | hs.writeServerHash(hs.hello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 846 | |
| 847 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 848 | |
| 849 | if len(hs.sessionState.certificates) > 0 { |
| 850 | if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil { |
| 851 | return err |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | hs.masterSecret = hs.sessionState.masterSecret |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 856 | c.extendedMasterSecret = hs.sessionState.extendedMasterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 857 | |
| 858 | return nil |
| 859 | } |
| 860 | |
| 861 | func (hs *serverHandshakeState) doFullHandshake() error { |
| 862 | config := hs.c.config |
| 863 | c := hs.c |
| 864 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 865 | isPSK := hs.suite.flags&suitePSK != 0 |
| 866 | if !isPSK && hs.clientHello.ocspStapling && len(hs.cert.OCSPStaple) > 0 { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 867 | hs.hello.extensions.ocspStapling = true |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 868 | } |
| 869 | |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 870 | if hs.clientHello.sctListSupported && len(hs.cert.SignedCertificateTimestampList) > 0 { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 871 | hs.hello.extensions.sctList = hs.cert.SignedCertificateTimestampList |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 872 | } |
| 873 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 874 | hs.hello.extensions.ticketSupported = hs.clientHello.ticketSupported && !config.SessionTicketsDisabled && c.vers > VersionSSL30 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 875 | hs.hello.cipherSuite = hs.suite.id |
David Benjamin | 6095de8 | 2014-12-27 01:50:38 -0500 | [diff] [blame] | 876 | if config.Bugs.SendCipherSuite != 0 { |
| 877 | hs.hello.cipherSuite = config.Bugs.SendCipherSuite |
| 878 | } |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 879 | c.extendedMasterSecret = hs.hello.extensions.extendedMasterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 880 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 881 | // Generate a session ID if we're to save the session. |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 882 | if !hs.hello.extensions.ticketSupported && config.ServerSessionCache != nil { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 883 | hs.hello.sessionId = make([]byte, 32) |
| 884 | if _, err := io.ReadFull(config.rand(), hs.hello.sessionId); err != nil { |
| 885 | c.sendAlert(alertInternalError) |
| 886 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 887 | } |
| 888 | } |
| 889 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 890 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 891 | hs.writeClientHash(hs.clientHello.marshal()) |
| 892 | hs.writeServerHash(hs.hello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 893 | |
| 894 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 895 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 896 | if !isPSK { |
| 897 | certMsg := new(certificateMsg) |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 898 | if !config.Bugs.EmptyCertificateList { |
| 899 | certMsg.certificates = hs.cert.Certificate |
| 900 | } |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 901 | if !config.Bugs.UnauthenticatedECDH { |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 902 | certMsgBytes := certMsg.marshal() |
| 903 | if config.Bugs.WrongCertificateMessageType { |
| 904 | certMsgBytes[0] += 42 |
| 905 | } |
| 906 | hs.writeServerHash(certMsgBytes) |
| 907 | c.writeRecord(recordTypeHandshake, certMsgBytes) |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 908 | } |
David Benjamin | 1c375dd | 2014-07-12 00:48:23 -0400 | [diff] [blame] | 909 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 910 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 911 | if hs.hello.extensions.ocspStapling && !c.config.Bugs.SkipCertificateStatus { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 912 | certStatus := new(certificateStatusMsg) |
| 913 | certStatus.statusType = statusTypeOCSP |
| 914 | certStatus.response = hs.cert.OCSPStaple |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 915 | hs.writeServerHash(certStatus.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 916 | c.writeRecord(recordTypeHandshake, certStatus.marshal()) |
| 917 | } |
| 918 | |
| 919 | keyAgreement := hs.suite.ka(c.vers) |
| 920 | skx, err := keyAgreement.generateServerKeyExchange(config, hs.cert, hs.clientHello, hs.hello) |
| 921 | if err != nil { |
| 922 | c.sendAlert(alertHandshakeFailure) |
| 923 | return err |
| 924 | } |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 925 | if skx != nil && !config.Bugs.SkipServerKeyExchange { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 926 | hs.writeServerHash(skx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 927 | c.writeRecord(recordTypeHandshake, skx.marshal()) |
| 928 | } |
| 929 | |
| 930 | if config.ClientAuth >= RequestClientCert { |
| 931 | // Request a client certificate |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 932 | certReq := &certificateRequestMsg{ |
| 933 | certificateTypes: config.ClientCertificateTypes, |
| 934 | } |
| 935 | if certReq.certificateTypes == nil { |
| 936 | certReq.certificateTypes = []byte{ |
| 937 | byte(CertTypeRSASign), |
| 938 | byte(CertTypeECDSASign), |
| 939 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 940 | } |
| 941 | if c.vers >= VersionTLS12 { |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 942 | certReq.hasSignatureAlgorithm = true |
| 943 | if !config.Bugs.NoSignatureAlgorithms { |
David Benjamin | 7a41d37 | 2016-07-09 11:21:54 -0700 | [diff] [blame] | 944 | certReq.signatureAlgorithms = config.verifySignatureAlgorithms() |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 945 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | // An empty list of certificateAuthorities signals to |
| 949 | // the client that it may send any certificate in response |
| 950 | // to our request. When we know the CAs we trust, then |
| 951 | // we can send them down, so that the client can choose |
| 952 | // an appropriate certificate to give to us. |
| 953 | if config.ClientCAs != nil { |
| 954 | certReq.certificateAuthorities = config.ClientCAs.Subjects() |
| 955 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 956 | hs.writeServerHash(certReq.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 957 | c.writeRecord(recordTypeHandshake, certReq.marshal()) |
| 958 | } |
| 959 | |
| 960 | helloDone := new(serverHelloDoneMsg) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 961 | hs.writeServerHash(helloDone.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 962 | c.writeRecord(recordTypeHandshake, helloDone.marshal()) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 963 | c.flushHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 964 | |
| 965 | var pub crypto.PublicKey // public key for client auth, if any |
| 966 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 967 | if err := c.simulatePacketLoss(nil); err != nil { |
| 968 | return err |
| 969 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 970 | msg, err := c.readHandshake() |
| 971 | if err != nil { |
| 972 | return err |
| 973 | } |
| 974 | |
| 975 | var ok bool |
| 976 | // If we requested a client certificate, then the client must send a |
| 977 | // certificate message, even if it's empty. |
| 978 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 979 | var certMsg *certificateMsg |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 980 | var certificates [][]byte |
| 981 | if certMsg, ok = msg.(*certificateMsg); ok { |
| 982 | if c.vers == VersionSSL30 && len(certMsg.certificates) == 0 { |
| 983 | return errors.New("tls: empty certificate message in SSL 3.0") |
| 984 | } |
| 985 | |
| 986 | hs.writeClientHash(certMsg.marshal()) |
| 987 | certificates = certMsg.certificates |
| 988 | } else if c.vers != VersionSSL30 { |
| 989 | // In TLS, the Certificate message is required. In SSL |
| 990 | // 3.0, the peer skips it when sending no certificates. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 991 | c.sendAlert(alertUnexpectedMessage) |
| 992 | return unexpectedMessageError(certMsg, msg) |
| 993 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 994 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 995 | if len(certificates) == 0 { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 996 | // The client didn't actually send a certificate |
| 997 | switch config.ClientAuth { |
| 998 | case RequireAnyClientCert, RequireAndVerifyClientCert: |
| 999 | c.sendAlert(alertBadCertificate) |
| 1000 | return errors.New("tls: client didn't provide a certificate") |
| 1001 | } |
| 1002 | } |
| 1003 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1004 | pub, err = hs.processCertsFromClient(certificates) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1005 | if err != nil { |
| 1006 | return err |
| 1007 | } |
| 1008 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1009 | if ok { |
| 1010 | msg, err = c.readHandshake() |
| 1011 | if err != nil { |
| 1012 | return err |
| 1013 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | // Get client key exchange |
| 1018 | ckx, ok := msg.(*clientKeyExchangeMsg) |
| 1019 | if !ok { |
| 1020 | c.sendAlert(alertUnexpectedMessage) |
| 1021 | return unexpectedMessageError(ckx, msg) |
| 1022 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1023 | hs.writeClientHash(ckx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1024 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1025 | preMasterSecret, err := keyAgreement.processClientKeyExchange(config, hs.cert, ckx, c.vers) |
| 1026 | if err != nil { |
| 1027 | c.sendAlert(alertHandshakeFailure) |
| 1028 | return err |
| 1029 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1030 | if c.extendedMasterSecret { |
| 1031 | hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 1032 | } else { |
| 1033 | if c.config.Bugs.RequireExtendedMasterSecret { |
| 1034 | return errors.New("tls: extended master secret required but not supported by peer") |
| 1035 | } |
| 1036 | hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.clientHello.random, hs.hello.random) |
| 1037 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1038 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1039 | // If we received a client cert in response to our certificate request message, |
| 1040 | // the client will send us a certificateVerifyMsg immediately after the |
| 1041 | // clientKeyExchangeMsg. This message is a digest of all preceding |
| 1042 | // handshake-layer messages that is signed using the private key corresponding |
| 1043 | // to the client's certificate. This allows us to verify that the client is in |
| 1044 | // possession of the private key of the certificate. |
| 1045 | if len(c.peerCertificates) > 0 { |
| 1046 | msg, err = c.readHandshake() |
| 1047 | if err != nil { |
| 1048 | return err |
| 1049 | } |
| 1050 | certVerify, ok := msg.(*certificateVerifyMsg) |
| 1051 | if !ok { |
| 1052 | c.sendAlert(alertUnexpectedMessage) |
| 1053 | return unexpectedMessageError(certVerify, msg) |
| 1054 | } |
| 1055 | |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1056 | // Determine the signature type. |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1057 | var sigAlg signatureAlgorithm |
| 1058 | if certVerify.hasSignatureAlgorithm { |
| 1059 | sigAlg = certVerify.signatureAlgorithm |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1060 | c.peerSignatureAlgorithm = sigAlg |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1061 | } |
| 1062 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1063 | if c.vers > VersionSSL30 { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 1064 | 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] | 1065 | } else { |
| 1066 | // SSL 3.0's client certificate construction is |
| 1067 | // incompatible with signatureAlgorithm. |
| 1068 | rsaPub, ok := pub.(*rsa.PublicKey) |
| 1069 | if !ok { |
| 1070 | err = errors.New("unsupported key type for client certificate") |
| 1071 | } else { |
| 1072 | digest := hs.finishedHash.hashForClientCertificateSSL3(hs.masterSecret) |
| 1073 | err = rsa.VerifyPKCS1v15(rsaPub, crypto.MD5SHA1, digest, certVerify.signature) |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1074 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1075 | } |
| 1076 | if err != nil { |
| 1077 | c.sendAlert(alertBadCertificate) |
| 1078 | return errors.New("could not validate signature of connection nonces: " + err.Error()) |
| 1079 | } |
| 1080 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1081 | hs.writeClientHash(certVerify.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1084 | hs.finishedHash.discardHandshakeBuffer() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1085 | |
| 1086 | return nil |
| 1087 | } |
| 1088 | |
| 1089 | func (hs *serverHandshakeState) establishKeys() error { |
| 1090 | c := hs.c |
| 1091 | |
| 1092 | clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1093 | 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] | 1094 | |
| 1095 | var clientCipher, serverCipher interface{} |
| 1096 | var clientHash, serverHash macFunction |
| 1097 | |
| 1098 | if hs.suite.aead == nil { |
| 1099 | clientCipher = hs.suite.cipher(clientKey, clientIV, true /* for reading */) |
| 1100 | clientHash = hs.suite.mac(c.vers, clientMAC) |
| 1101 | serverCipher = hs.suite.cipher(serverKey, serverIV, false /* not for reading */) |
| 1102 | serverHash = hs.suite.mac(c.vers, serverMAC) |
| 1103 | } else { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1104 | clientCipher = hs.suite.aead(c.vers, clientKey, clientIV) |
| 1105 | serverCipher = hs.suite.aead(c.vers, serverKey, serverIV) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | c.in.prepareCipherSpec(c.vers, clientCipher, clientHash) |
| 1109 | c.out.prepareCipherSpec(c.vers, serverCipher, serverHash) |
| 1110 | |
| 1111 | return nil |
| 1112 | } |
| 1113 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1114 | func (hs *serverHandshakeState) readFinished(out []byte, isResume bool) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1115 | c := hs.c |
| 1116 | |
| 1117 | c.readRecord(recordTypeChangeCipherSpec) |
| 1118 | if err := c.in.error(); err != nil { |
| 1119 | return err |
| 1120 | } |
| 1121 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1122 | if hs.hello.extensions.nextProtoNeg { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1123 | msg, err := c.readHandshake() |
| 1124 | if err != nil { |
| 1125 | return err |
| 1126 | } |
| 1127 | nextProto, ok := msg.(*nextProtoMsg) |
| 1128 | if !ok { |
| 1129 | c.sendAlert(alertUnexpectedMessage) |
| 1130 | return unexpectedMessageError(nextProto, msg) |
| 1131 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1132 | hs.writeClientHash(nextProto.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1133 | c.clientProtocol = nextProto.proto |
| 1134 | } |
| 1135 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1136 | if hs.hello.extensions.channelIDRequested { |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1137 | msg, err := c.readHandshake() |
| 1138 | if err != nil { |
| 1139 | return err |
| 1140 | } |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1141 | channelIDMsg, ok := msg.(*channelIDMsg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1142 | if !ok { |
| 1143 | c.sendAlert(alertUnexpectedMessage) |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1144 | return unexpectedMessageError(channelIDMsg, msg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1145 | } |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1146 | x := new(big.Int).SetBytes(channelIDMsg.channelID[0:32]) |
| 1147 | y := new(big.Int).SetBytes(channelIDMsg.channelID[32:64]) |
| 1148 | r := new(big.Int).SetBytes(channelIDMsg.channelID[64:96]) |
| 1149 | s := new(big.Int).SetBytes(channelIDMsg.channelID[96:128]) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1150 | if !elliptic.P256().IsOnCurve(x, y) { |
| 1151 | return errors.New("tls: invalid channel ID public key") |
| 1152 | } |
| 1153 | channelID := &ecdsa.PublicKey{elliptic.P256(), x, y} |
| 1154 | var resumeHash []byte |
| 1155 | if isResume { |
| 1156 | resumeHash = hs.sessionState.handshakeHash |
| 1157 | } |
| 1158 | if !ecdsa.Verify(channelID, hs.finishedHash.hashForChannelID(resumeHash), r, s) { |
| 1159 | return errors.New("tls: invalid channel ID signature") |
| 1160 | } |
| 1161 | c.channelID = channelID |
| 1162 | |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1163 | hs.writeClientHash(channelIDMsg.marshal()) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1164 | } |
| 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 | clientFinished, ok := msg.(*finishedMsg) |
| 1171 | if !ok { |
| 1172 | c.sendAlert(alertUnexpectedMessage) |
| 1173 | return unexpectedMessageError(clientFinished, msg) |
| 1174 | } |
| 1175 | |
| 1176 | verify := hs.finishedHash.clientSum(hs.masterSecret) |
| 1177 | if len(verify) != len(clientFinished.verifyData) || |
| 1178 | subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 { |
| 1179 | c.sendAlert(alertHandshakeFailure) |
| 1180 | return errors.New("tls: client's Finished message is incorrect") |
| 1181 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1182 | c.clientVerify = append(c.clientVerify[:0], clientFinished.verifyData...) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1183 | copy(out, clientFinished.verifyData) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1184 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1185 | hs.writeClientHash(clientFinished.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1186 | return nil |
| 1187 | } |
| 1188 | |
| 1189 | func (hs *serverHandshakeState) sendSessionTicket() error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1190 | c := hs.c |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1191 | state := sessionState{ |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1192 | vers: c.vers, |
| 1193 | cipherSuite: hs.suite.id, |
| 1194 | masterSecret: hs.masterSecret, |
| 1195 | certificates: hs.certsFromClient, |
| 1196 | handshakeHash: hs.finishedHash.server.Sum(nil), |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1197 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1198 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1199 | if !hs.hello.extensions.ticketSupported || hs.c.config.Bugs.SkipNewSessionTicket { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1200 | if c.config.ServerSessionCache != nil && len(hs.hello.sessionId) != 0 { |
| 1201 | c.config.ServerSessionCache.Put(string(hs.hello.sessionId), &state) |
| 1202 | } |
| 1203 | return nil |
| 1204 | } |
| 1205 | |
| 1206 | m := new(newSessionTicketMsg) |
| 1207 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1208 | if !c.config.Bugs.SendEmptySessionTicket { |
| 1209 | var err error |
| 1210 | m.ticket, err = c.encryptTicket(&state) |
| 1211 | if err != nil { |
| 1212 | return err |
| 1213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1214 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1215 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1216 | hs.writeServerHash(m.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1217 | c.writeRecord(recordTypeHandshake, m.marshal()) |
| 1218 | |
| 1219 | return nil |
| 1220 | } |
| 1221 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1222 | func (hs *serverHandshakeState) sendFinished(out []byte) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1223 | c := hs.c |
| 1224 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1225 | finished := new(finishedMsg) |
| 1226 | finished.verifyData = hs.finishedHash.serverSum(hs.masterSecret) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1227 | copy(out, finished.verifyData) |
David Benjamin | 513f0ea | 2015-04-02 19:33:31 -0400 | [diff] [blame] | 1228 | if c.config.Bugs.BadFinished { |
| 1229 | finished.verifyData[0]++ |
| 1230 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1231 | c.serverVerify = append(c.serverVerify[:0], finished.verifyData...) |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1232 | hs.finishedBytes = finished.marshal() |
| 1233 | hs.writeServerHash(hs.finishedBytes) |
| 1234 | postCCSBytes := hs.finishedBytes |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1235 | |
| 1236 | if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
| 1237 | c.writeRecord(recordTypeHandshake, postCCSBytes[:5]) |
| 1238 | postCCSBytes = postCCSBytes[5:] |
| 1239 | } |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1240 | c.flushHandshake() |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1241 | |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 1242 | if !c.config.Bugs.SkipChangeCipherSpec { |
David Benjamin | 8411b24 | 2015-11-26 12:07:28 -0500 | [diff] [blame] | 1243 | ccs := []byte{1} |
| 1244 | if c.config.Bugs.BadChangeCipherSpec != nil { |
| 1245 | ccs = c.config.Bugs.BadChangeCipherSpec |
| 1246 | } |
| 1247 | c.writeRecord(recordTypeChangeCipherSpec, ccs) |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 1248 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1249 | |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 1250 | if c.config.Bugs.AppDataAfterChangeCipherSpec != nil { |
| 1251 | c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec) |
| 1252 | } |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 1253 | if c.config.Bugs.AlertAfterChangeCipherSpec != 0 { |
| 1254 | c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec) |
| 1255 | return errors.New("tls: simulating post-CCS alert") |
| 1256 | } |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 1257 | |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 1258 | if !c.config.Bugs.SkipFinished { |
| 1259 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1260 | c.flushHandshake() |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 1261 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1262 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1263 | c.cipherSuite = hs.suite |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1264 | |
| 1265 | return nil |
| 1266 | } |
| 1267 | |
| 1268 | // processCertsFromClient takes a chain of client certificates either from a |
| 1269 | // Certificates message or from a sessionState and verifies them. It returns |
| 1270 | // the public key of the leaf certificate. |
| 1271 | func (hs *serverHandshakeState) processCertsFromClient(certificates [][]byte) (crypto.PublicKey, error) { |
| 1272 | c := hs.c |
| 1273 | |
| 1274 | hs.certsFromClient = certificates |
| 1275 | certs := make([]*x509.Certificate, len(certificates)) |
| 1276 | var err error |
| 1277 | for i, asn1Data := range certificates { |
| 1278 | if certs[i], err = x509.ParseCertificate(asn1Data); err != nil { |
| 1279 | c.sendAlert(alertBadCertificate) |
| 1280 | return nil, errors.New("tls: failed to parse client certificate: " + err.Error()) |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | if c.config.ClientAuth >= VerifyClientCertIfGiven && len(certs) > 0 { |
| 1285 | opts := x509.VerifyOptions{ |
| 1286 | Roots: c.config.ClientCAs, |
| 1287 | CurrentTime: c.config.time(), |
| 1288 | Intermediates: x509.NewCertPool(), |
| 1289 | KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, |
| 1290 | } |
| 1291 | |
| 1292 | for _, cert := range certs[1:] { |
| 1293 | opts.Intermediates.AddCert(cert) |
| 1294 | } |
| 1295 | |
| 1296 | chains, err := certs[0].Verify(opts) |
| 1297 | if err != nil { |
| 1298 | c.sendAlert(alertBadCertificate) |
| 1299 | return nil, errors.New("tls: failed to verify client's certificate: " + err.Error()) |
| 1300 | } |
| 1301 | |
| 1302 | ok := false |
| 1303 | for _, ku := range certs[0].ExtKeyUsage { |
| 1304 | if ku == x509.ExtKeyUsageClientAuth { |
| 1305 | ok = true |
| 1306 | break |
| 1307 | } |
| 1308 | } |
| 1309 | if !ok { |
| 1310 | c.sendAlert(alertHandshakeFailure) |
| 1311 | return nil, errors.New("tls: client's certificate's extended key usage doesn't permit it to be used for client authentication") |
| 1312 | } |
| 1313 | |
| 1314 | c.verifiedChains = chains |
| 1315 | } |
| 1316 | |
| 1317 | if len(certs) > 0 { |
| 1318 | var pub crypto.PublicKey |
| 1319 | switch key := certs[0].PublicKey.(type) { |
| 1320 | case *ecdsa.PublicKey, *rsa.PublicKey: |
| 1321 | pub = key |
| 1322 | default: |
| 1323 | c.sendAlert(alertUnsupportedCertificate) |
| 1324 | return nil, fmt.Errorf("tls: client's certificate contains an unsupported public key of type %T", certs[0].PublicKey) |
| 1325 | } |
| 1326 | c.peerCertificates = certs |
| 1327 | return pub, nil |
| 1328 | } |
| 1329 | |
| 1330 | return nil, nil |
| 1331 | } |
| 1332 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1333 | func (hs *serverHandshakeState) writeServerHash(msg []byte) { |
| 1334 | // writeServerHash is called before writeRecord. |
| 1335 | hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 1336 | } |
| 1337 | |
| 1338 | func (hs *serverHandshakeState) writeClientHash(msg []byte) { |
| 1339 | // writeClientHash is called after readHandshake. |
| 1340 | hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 1341 | } |
| 1342 | |
| 1343 | func (hs *serverHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 1344 | if hs.c.isDTLS { |
| 1345 | // This is somewhat hacky. DTLS hashes a slightly different format. |
| 1346 | // First, the TLS header. |
| 1347 | hs.finishedHash.Write(msg[:4]) |
| 1348 | // Then the sequence number and reassembled fragment offset (always 0). |
| 1349 | hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 1350 | // Then the reassembled fragment (always equal to the message length). |
| 1351 | hs.finishedHash.Write(msg[1:4]) |
| 1352 | // And then the message body. |
| 1353 | hs.finishedHash.Write(msg[4:]) |
| 1354 | } else { |
| 1355 | hs.finishedHash.Write(msg) |
| 1356 | } |
| 1357 | } |
| 1358 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1359 | // tryCipherSuite returns a cipherSuite with the given id if that cipher suite |
| 1360 | // is acceptable to use. |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1361 | 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] | 1362 | for _, supported := range supportedCipherSuites { |
| 1363 | if id == supported { |
| 1364 | var candidate *cipherSuite |
| 1365 | |
| 1366 | for _, s := range cipherSuites { |
| 1367 | if s.id == id { |
| 1368 | candidate = s |
| 1369 | break |
| 1370 | } |
| 1371 | } |
| 1372 | if candidate == nil { |
| 1373 | continue |
| 1374 | } |
| 1375 | // Don't select a ciphersuite which we can't |
| 1376 | // support for this client. |
David Benjamin | 0407e76 | 2016-06-17 16:41:18 -0400 | [diff] [blame] | 1377 | if !c.config.Bugs.EnableAllCiphers { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1378 | if (candidate.flags&suitePSK != 0) && !pskOk { |
| 1379 | continue |
| 1380 | } |
David Benjamin | 0407e76 | 2016-06-17 16:41:18 -0400 | [diff] [blame] | 1381 | if (candidate.flags&suiteECDHE != 0) && !ellipticOk { |
| 1382 | continue |
| 1383 | } |
| 1384 | if (candidate.flags&suiteECDSA != 0) != ecdsaOk { |
| 1385 | continue |
| 1386 | } |
| 1387 | if version < VersionTLS12 && candidate.flags&suiteTLS12 != 0 { |
| 1388 | continue |
| 1389 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1390 | if version >= VersionTLS13 && candidate.flags&suiteTLS13 == 0 { |
| 1391 | continue |
| 1392 | } |
David Benjamin | 0407e76 | 2016-06-17 16:41:18 -0400 | [diff] [blame] | 1393 | if c.isDTLS && candidate.flags&suiteNoDTLS != 0 { |
| 1394 | continue |
| 1395 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1396 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1397 | return candidate |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | return nil |
| 1402 | } |
David Benjamin | f93995b | 2015-11-05 18:23:20 -0500 | [diff] [blame] | 1403 | |
| 1404 | func isTLS12Cipher(id uint16) bool { |
| 1405 | for _, cipher := range cipherSuites { |
| 1406 | if cipher.id != id { |
| 1407 | continue |
| 1408 | } |
| 1409 | return cipher.flags&suiteTLS12 != 0 |
| 1410 | } |
| 1411 | // Unknown cipher. |
| 1412 | return false |
| 1413 | } |