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