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