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