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 |
Nick Harper | ab20cec | 2016-12-19 17:38:41 -0800 | [diff] [blame] | 683 | } |
| 684 | } else { |
| 685 | c.skipEarlyData = true |
| 686 | } |
| 687 | } |
| 688 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 689 | if config.Bugs.SendEarlyDataExtension { |
| 690 | encryptedExtensions.extensions.hasEarlyData = true |
| 691 | } |
| 692 | |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 693 | // Resolve ECDHE and compute the handshake secret. |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 694 | if hs.hello.hasKeyShare { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 695 | // Once a curve has been selected and a key share identified, |
| 696 | // the server needs to generate a public value and send it in |
| 697 | // the ServerHello. |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 698 | curve, ok := curveForCurveID(selectedCurve) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 699 | if !ok { |
| 700 | panic("tls: server failed to look up curve ID") |
| 701 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 702 | c.curveID = selectedCurve |
| 703 | |
| 704 | var peerKey []byte |
| 705 | if config.Bugs.SkipHelloRetryRequest { |
| 706 | // If skipping HelloRetryRequest, use a random key to |
| 707 | // avoid crashing. |
| 708 | curve2, _ := curveForCurveID(selectedCurve) |
| 709 | var err error |
| 710 | peerKey, err = curve2.offer(config.rand()) |
| 711 | if err != nil { |
| 712 | return err |
| 713 | } |
| 714 | } else { |
| 715 | peerKey = selectedKeyShare.keyExchange |
| 716 | } |
| 717 | |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 718 | publicKey, ecdheSecret, err := curve.accept(config.rand(), peerKey) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 719 | if err != nil { |
| 720 | c.sendAlert(alertHandshakeFailure) |
| 721 | return err |
| 722 | } |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 723 | hs.finishedHash.addEntropy(ecdheSecret) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 724 | hs.hello.hasKeyShare = true |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 725 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 726 | curveID := selectedCurve |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 727 | if c.config.Bugs.SendCurve != 0 { |
| 728 | curveID = config.Bugs.SendCurve |
| 729 | } |
| 730 | if c.config.Bugs.InvalidECDHPoint { |
| 731 | publicKey[0] ^= 0xff |
| 732 | } |
| 733 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 734 | hs.hello.keyShare = keyShareEntry{ |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 735 | group: curveID, |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 736 | keyExchange: publicKey, |
| 737 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 738 | |
| 739 | if config.Bugs.EncryptedExtensionsWithKeyShare { |
| 740 | encryptedExtensions.extensions.hasKeyShare = true |
| 741 | encryptedExtensions.extensions.keyShare = keyShareEntry{ |
| 742 | group: curveID, |
| 743 | keyExchange: publicKey, |
| 744 | } |
| 745 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 746 | } else { |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 747 | hs.finishedHash.addEntropy(hs.finishedHash.zeroSecret()) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // Send unencrypted ServerHello. |
| 751 | hs.writeServerHash(hs.hello.marshal()) |
David Benjamin | 7964b18 | 2016-07-14 23:36:30 -0400 | [diff] [blame] | 752 | if config.Bugs.PartialEncryptedExtensionsWithServerHello { |
| 753 | helloBytes := hs.hello.marshal() |
| 754 | toWrite := make([]byte, 0, len(helloBytes)+1) |
| 755 | toWrite = append(toWrite, helloBytes...) |
| 756 | toWrite = append(toWrite, typeEncryptedExtensions) |
| 757 | c.writeRecord(recordTypeHandshake, toWrite) |
| 758 | } else { |
| 759 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 760 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 761 | c.flushHandshake() |
| 762 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 763 | // Switch to handshake traffic keys. |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 764 | serverHandshakeTrafficSecret := hs.finishedHash.deriveSecret(serverHandshakeTrafficLabel) |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 765 | c.out.useTrafficSecret(c.vers, hs.suite, serverHandshakeTrafficSecret, serverWrite) |
Nick Harper | ab20cec | 2016-12-19 17:38:41 -0800 | [diff] [blame] | 766 | // Derive handshake traffic read key, but don't switch yet. |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 767 | clientHandshakeTrafficSecret := hs.finishedHash.deriveSecret(clientHandshakeTrafficLabel) |
David Benjamin | 615119a | 2016-07-06 19:22:55 -0700 | [diff] [blame] | 768 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 769 | // Send EncryptedExtensions. |
| 770 | hs.writeServerHash(encryptedExtensions.marshal()) |
David Benjamin | 7964b18 | 2016-07-14 23:36:30 -0400 | [diff] [blame] | 771 | if config.Bugs.PartialEncryptedExtensionsWithServerHello { |
| 772 | // The first byte has already been sent. |
| 773 | c.writeRecord(recordTypeHandshake, encryptedExtensions.marshal()[1:]) |
| 774 | } else { |
| 775 | c.writeRecord(recordTypeHandshake, encryptedExtensions.marshal()) |
| 776 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 777 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 778 | if hs.sessionState == nil { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 779 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 780 | // Request a client certificate |
| 781 | certReq := &certificateRequestMsg{ |
| 782 | hasSignatureAlgorithm: true, |
| 783 | hasRequestContext: true, |
David Benjamin | 8a8349b | 2016-08-18 02:32:23 -0400 | [diff] [blame] | 784 | requestContext: config.Bugs.SendRequestContext, |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 785 | } |
| 786 | if !config.Bugs.NoSignatureAlgorithms { |
David Benjamin | f74ec79 | 2016-07-13 21:18:49 -0400 | [diff] [blame] | 787 | certReq.signatureAlgorithms = config.verifySignatureAlgorithms() |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | // An empty list of certificateAuthorities signals to |
| 791 | // the client that it may send any certificate in response |
| 792 | // to our request. When we know the CAs we trust, then |
| 793 | // we can send them down, so that the client can choose |
| 794 | // an appropriate certificate to give to us. |
| 795 | if config.ClientCAs != nil { |
| 796 | certReq.certificateAuthorities = config.ClientCAs.Subjects() |
| 797 | } |
| 798 | hs.writeServerHash(certReq.marshal()) |
| 799 | c.writeRecord(recordTypeHandshake, certReq.marshal()) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | certMsg := &certificateMsg{ |
| 803 | hasRequestContext: true, |
| 804 | } |
| 805 | if !config.Bugs.EmptyCertificateList { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 806 | for i, certData := range hs.cert.Certificate { |
| 807 | cert := certificateEntry{ |
| 808 | data: certData, |
| 809 | } |
| 810 | if i == 0 { |
| 811 | if hs.clientHello.ocspStapling { |
| 812 | cert.ocspResponse = hs.cert.OCSPStaple |
| 813 | } |
| 814 | if hs.clientHello.sctListSupported { |
| 815 | cert.sctList = hs.cert.SignedCertificateTimestampList |
| 816 | } |
| 817 | cert.duplicateExtensions = config.Bugs.SendDuplicateCertExtensions |
| 818 | cert.extraExtension = config.Bugs.SendExtensionOnCertificate |
| 819 | } else { |
| 820 | if config.Bugs.SendOCSPOnIntermediates != nil { |
| 821 | cert.ocspResponse = config.Bugs.SendOCSPOnIntermediates |
| 822 | } |
| 823 | if config.Bugs.SendSCTOnIntermediates != nil { |
| 824 | cert.sctList = config.Bugs.SendSCTOnIntermediates |
| 825 | } |
| 826 | } |
| 827 | certMsg.certificates = append(certMsg.certificates, cert) |
| 828 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 829 | } |
David Benjamin | 1edae6b | 2016-07-13 16:58:23 -0400 | [diff] [blame] | 830 | certMsgBytes := certMsg.marshal() |
David Benjamin | 1edae6b | 2016-07-13 16:58:23 -0400 | [diff] [blame] | 831 | hs.writeServerHash(certMsgBytes) |
| 832 | c.writeRecord(recordTypeHandshake, certMsgBytes) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 833 | |
| 834 | certVerify := &certificateVerifyMsg{ |
| 835 | hasSignatureAlgorithm: true, |
| 836 | } |
| 837 | |
| 838 | // Determine the hash to sign. |
| 839 | privKey := hs.cert.PrivateKey |
| 840 | |
| 841 | var err error |
| 842 | certVerify.signatureAlgorithm, err = selectSignatureAlgorithm(c.vers, privKey, config, hs.clientHello.signatureAlgorithms) |
| 843 | if err != nil { |
| 844 | c.sendAlert(alertInternalError) |
| 845 | return err |
| 846 | } |
| 847 | |
| 848 | input := hs.finishedHash.certificateVerifyInput(serverCertificateVerifyContextTLS13) |
| 849 | certVerify.signature, err = signMessage(c.vers, privKey, c.config, certVerify.signatureAlgorithm, input) |
| 850 | if err != nil { |
| 851 | c.sendAlert(alertInternalError) |
| 852 | return err |
| 853 | } |
| 854 | |
Steven Valdez | 0ee2e11 | 2016-07-15 06:51:15 -0400 | [diff] [blame] | 855 | if config.Bugs.SendSignatureAlgorithm != 0 { |
| 856 | certVerify.signatureAlgorithm = config.Bugs.SendSignatureAlgorithm |
| 857 | } |
| 858 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 859 | hs.writeServerHash(certVerify.marshal()) |
| 860 | c.writeRecord(recordTypeHandshake, certVerify.marshal()) |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 861 | } else if hs.sessionState != nil { |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame] | 862 | // Pick up certificates from the session instead. |
David Benjamin | 5ecb88b | 2016-10-04 17:51:35 -0400 | [diff] [blame] | 863 | if len(hs.sessionState.certificates) > 0 { |
Nick Harper | 0b3625b | 2016-07-25 16:16:28 -0700 | [diff] [blame] | 864 | if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil { |
| 865 | return err |
| 866 | } |
| 867 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | finished := new(finishedMsg) |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 871 | finished.verifyData = hs.finishedHash.serverSum(serverHandshakeTrafficSecret) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 872 | if config.Bugs.BadFinished { |
| 873 | finished.verifyData[0]++ |
| 874 | } |
| 875 | hs.writeServerHash(finished.marshal()) |
| 876 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
David Benjamin | 02edcd0 | 2016-07-27 17:40:37 -0400 | [diff] [blame] | 877 | if c.config.Bugs.SendExtraFinished { |
| 878 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
| 879 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 880 | c.flushHandshake() |
| 881 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame^] | 882 | if encryptedExtensions.extensions.hasEarlyData && !c.skipEarlyData { |
| 883 | for _, expectedMsg := range config.Bugs.ExpectLateEarlyData { |
| 884 | if err := c.readRecord(recordTypeApplicationData); err != nil { |
| 885 | return err |
| 886 | } |
| 887 | if !bytes.Equal(c.input.data[c.input.off:], expectedMsg) { |
| 888 | return errors.New("ExpectLateEarlyData: did not get expected message") |
| 889 | } |
| 890 | c.in.freeBlock(c.input) |
| 891 | c.input = nil |
| 892 | } |
| 893 | } |
| 894 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 895 | // The various secrets do not incorporate the client's final leg, so |
| 896 | // derive them now before updating the handshake context. |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 897 | hs.finishedHash.addEntropy(hs.finishedHash.zeroSecret()) |
| 898 | clientTrafficSecret := hs.finishedHash.deriveSecret(clientApplicationTrafficLabel) |
| 899 | serverTrafficSecret := hs.finishedHash.deriveSecret(serverApplicationTrafficLabel) |
| 900 | c.exporterSecret = hs.finishedHash.deriveSecret(exporterLabel) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 901 | |
David Benjamin | 2aad406 | 2016-07-14 23:15:40 -0400 | [diff] [blame] | 902 | // Switch to application data keys on write. In particular, any alerts |
| 903 | // from the client certificate are sent over these keys. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 904 | c.out.useTrafficSecret(c.vers, hs.suite, serverTrafficSecret, serverWrite) |
David Benjamin | 2aad406 | 2016-07-14 23:15:40 -0400 | [diff] [blame] | 905 | |
Nick Harper | 7cd0a97 | 2016-12-02 11:08:40 -0800 | [diff] [blame] | 906 | // Send 0.5-RTT messages. |
| 907 | for _, halfRTTMsg := range config.Bugs.SendHalfRTTData { |
| 908 | if _, err := c.writeRecord(recordTypeApplicationData, halfRTTMsg); err != nil { |
| 909 | return err |
| 910 | } |
| 911 | } |
| 912 | |
Nick Harper | ab20cec | 2016-12-19 17:38:41 -0800 | [diff] [blame] | 913 | // Read end_of_early_data alert. |
| 914 | if encryptedExtensions.extensions.hasEarlyData { |
| 915 | if err := c.readRecord(recordTypeAlert); err != errEndOfEarlyDataAlert { |
| 916 | if err == nil { |
| 917 | panic("readRecord(recordTypeAlert) returned nil") |
| 918 | } |
| 919 | return err |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | // Switch input stream to handshake traffic keys. |
| 924 | c.in.useTrafficSecret(c.vers, hs.suite, clientHandshakeTrafficSecret, clientWrite) |
| 925 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 926 | // If we requested a client certificate, then the client must send a |
| 927 | // certificate message, even if it's empty. |
| 928 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 929 | msg, err := c.readHandshake() |
| 930 | if err != nil { |
| 931 | return err |
| 932 | } |
| 933 | |
| 934 | certMsg, ok := msg.(*certificateMsg) |
| 935 | if !ok { |
| 936 | c.sendAlert(alertUnexpectedMessage) |
| 937 | return unexpectedMessageError(certMsg, msg) |
| 938 | } |
| 939 | hs.writeClientHash(certMsg.marshal()) |
| 940 | |
| 941 | if len(certMsg.certificates) == 0 { |
| 942 | // The client didn't actually send a certificate |
| 943 | switch config.ClientAuth { |
| 944 | case RequireAnyClientCert, RequireAndVerifyClientCert: |
David Benjamin | 1db9e1b | 2016-10-07 20:51:43 -0400 | [diff] [blame] | 945 | c.sendAlert(alertCertificateRequired) |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 946 | return errors.New("tls: client didn't provide a certificate") |
| 947 | } |
| 948 | } |
| 949 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 950 | var certs [][]byte |
| 951 | for _, cert := range certMsg.certificates { |
| 952 | certs = append(certs, cert.data) |
| 953 | // OCSP responses and SCT lists are not negotiated in |
| 954 | // client certificates. |
| 955 | if cert.ocspResponse != nil || cert.sctList != nil { |
| 956 | c.sendAlert(alertUnsupportedExtension) |
| 957 | return errors.New("tls: unexpected extensions in the client certificate") |
| 958 | } |
| 959 | } |
| 960 | pub, err := hs.processCertsFromClient(certs) |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 961 | if err != nil { |
| 962 | return err |
| 963 | } |
| 964 | |
| 965 | if len(c.peerCertificates) > 0 { |
| 966 | msg, err = c.readHandshake() |
| 967 | if err != nil { |
| 968 | return err |
| 969 | } |
| 970 | |
| 971 | certVerify, ok := msg.(*certificateVerifyMsg) |
| 972 | if !ok { |
| 973 | c.sendAlert(alertUnexpectedMessage) |
| 974 | return unexpectedMessageError(certVerify, msg) |
| 975 | } |
| 976 | |
David Benjamin | f74ec79 | 2016-07-13 21:18:49 -0400 | [diff] [blame] | 977 | c.peerSignatureAlgorithm = certVerify.signatureAlgorithm |
David Benjamin | 8d343b4 | 2016-07-09 14:26:01 -0700 | [diff] [blame] | 978 | input := hs.finishedHash.certificateVerifyInput(clientCertificateVerifyContextTLS13) |
| 979 | if err := verifyMessage(c.vers, pub, config, certVerify.signatureAlgorithm, input, certVerify.signature); err != nil { |
| 980 | c.sendAlert(alertBadCertificate) |
| 981 | return err |
| 982 | } |
| 983 | hs.writeClientHash(certVerify.marshal()) |
| 984 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 987 | if encryptedExtensions.extensions.channelIDRequested { |
| 988 | msg, err := c.readHandshake() |
| 989 | if err != nil { |
| 990 | return err |
| 991 | } |
| 992 | channelIDMsg, ok := msg.(*channelIDMsg) |
| 993 | if !ok { |
| 994 | c.sendAlert(alertUnexpectedMessage) |
| 995 | return unexpectedMessageError(channelIDMsg, msg) |
| 996 | } |
| 997 | channelIDHash := crypto.SHA256.New() |
| 998 | channelIDHash.Write(hs.finishedHash.certificateVerifyInput(channelIDContextTLS13)) |
| 999 | channelID, err := verifyChannelIDMessage(channelIDMsg, channelIDHash.Sum(nil)) |
| 1000 | if err != nil { |
| 1001 | return err |
| 1002 | } |
| 1003 | c.channelID = channelID |
| 1004 | |
| 1005 | hs.writeClientHash(channelIDMsg.marshal()) |
| 1006 | } |
| 1007 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1008 | // Read the client Finished message. |
| 1009 | msg, err := c.readHandshake() |
| 1010 | if err != nil { |
| 1011 | return err |
| 1012 | } |
| 1013 | clientFinished, ok := msg.(*finishedMsg) |
| 1014 | if !ok { |
| 1015 | c.sendAlert(alertUnexpectedMessage) |
| 1016 | return unexpectedMessageError(clientFinished, msg) |
| 1017 | } |
| 1018 | |
Steven Valdez | c4aa727 | 2016-10-03 12:25:56 -0400 | [diff] [blame] | 1019 | verify := hs.finishedHash.clientSum(clientHandshakeTrafficSecret) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1020 | if len(verify) != len(clientFinished.verifyData) || |
| 1021 | subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 { |
| 1022 | c.sendAlert(alertHandshakeFailure) |
| 1023 | return errors.New("tls: client's Finished message was incorrect") |
| 1024 | } |
David Benjamin | 97a0a08 | 2016-07-13 17:57:35 -0400 | [diff] [blame] | 1025 | hs.writeClientHash(clientFinished.marshal()) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1026 | |
David Benjamin | 2aad406 | 2016-07-14 23:15:40 -0400 | [diff] [blame] | 1027 | // Switch to application data keys on read. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1028 | c.in.useTrafficSecret(c.vers, hs.suite, clientTrafficSecret, clientWrite) |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1029 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1030 | c.cipherSuite = hs.suite |
David Benjamin | 48891ad | 2016-12-04 00:02:43 -0500 | [diff] [blame] | 1031 | c.resumptionSecret = hs.finishedHash.deriveSecret(resumptionLabel) |
David Benjamin | 5810488 | 2016-07-18 01:25:41 +0200 | [diff] [blame] | 1032 | |
| 1033 | // TODO(davidben): Allow configuring the number of tickets sent for |
| 1034 | // testing. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1035 | if !c.config.SessionTicketsDisabled && foundKEMode { |
David Benjamin | 5810488 | 2016-07-18 01:25:41 +0200 | [diff] [blame] | 1036 | ticketCount := 2 |
| 1037 | for i := 0; i < ticketCount; i++ { |
| 1038 | c.SendNewSessionTicket() |
| 1039 | } |
| 1040 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1041 | return nil |
| 1042 | } |
| 1043 | |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 1044 | // processClientHello processes the ClientHello message from the client and |
| 1045 | // decides whether we will perform session resumption. |
| 1046 | func (hs *serverHandshakeState) processClientHello() (isResume bool, err error) { |
| 1047 | config := hs.c.config |
| 1048 | c := hs.c |
| 1049 | |
| 1050 | hs.hello = &serverHelloMsg{ |
| 1051 | isDTLS: c.isDTLS, |
David Benjamin | 3c6a1ea | 2016-09-26 18:30:05 -0400 | [diff] [blame] | 1052 | vers: versionToWire(c.vers, c.isDTLS), |
David Benjamin | b1dd8cd | 2016-09-26 19:20:48 -0400 | [diff] [blame] | 1053 | versOverride: config.Bugs.SendServerHelloVersion, |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 1054 | compressionMethod: compressionNone, |
| 1055 | } |
| 1056 | |
| 1057 | hs.hello.random = make([]byte, 32) |
| 1058 | _, err = io.ReadFull(config.rand(), hs.hello.random) |
| 1059 | if err != nil { |
| 1060 | c.sendAlert(alertInternalError) |
| 1061 | return false, err |
| 1062 | } |
David Benjamin | a128a55 | 2016-10-13 14:26:33 -0400 | [diff] [blame] | 1063 | // Signal downgrades in the server random, per draft-ietf-tls-tls13-16, |
| 1064 | // section 4.1.3. |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 1065 | if c.vers <= VersionTLS12 && config.maxVersion(c.isDTLS) >= VersionTLS13 { |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 1066 | copy(hs.hello.random[len(hs.hello.random)-8:], downgradeTLS13) |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 1067 | } |
| 1068 | if c.vers <= VersionTLS11 && config.maxVersion(c.isDTLS) == VersionTLS12 { |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 1069 | copy(hs.hello.random[len(hs.hello.random)-8:], downgradeTLS12) |
Nick Harper | 85f20c2 | 2016-07-04 10:11:59 -0700 | [diff] [blame] | 1070 | } |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame] | 1071 | |
| 1072 | foundCompression := false |
| 1073 | // We only support null compression, so check that the client offered it. |
| 1074 | for _, compression := range hs.clientHello.compressionMethods { |
| 1075 | if compression == compressionNone { |
| 1076 | foundCompression = true |
| 1077 | break |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | if !foundCompression { |
| 1082 | c.sendAlert(alertHandshakeFailure) |
| 1083 | return false, errors.New("tls: client does not support uncompressed connections") |
| 1084 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1085 | |
| 1086 | if err := hs.processClientExtensions(&hs.hello.extensions); err != nil { |
| 1087 | return false, err |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 1088 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1089 | |
| 1090 | supportedCurve := false |
| 1091 | preferredCurves := config.curvePreferences() |
| 1092 | Curves: |
| 1093 | for _, curve := range hs.clientHello.supportedCurves { |
| 1094 | for _, supported := range preferredCurves { |
| 1095 | if supported == curve { |
| 1096 | supportedCurve = true |
| 1097 | break Curves |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | supportedPointFormat := false |
| 1103 | for _, pointFormat := range hs.clientHello.supportedPoints { |
| 1104 | if pointFormat == pointFormatUncompressed { |
| 1105 | supportedPointFormat = true |
| 1106 | break |
| 1107 | } |
| 1108 | } |
| 1109 | hs.ellipticOk = supportedCurve && supportedPointFormat |
| 1110 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1111 | _, hs.ecdsaOk = hs.cert.PrivateKey.(*ecdsa.PrivateKey) |
David Benjamin | d768c5d | 2017-03-28 18:28:44 -0500 | [diff] [blame] | 1112 | // Ed25519 also uses ECDSA certificates. |
| 1113 | _, ed25519Ok := hs.cert.PrivateKey.(ed25519.PrivateKey) |
| 1114 | hs.ecdsaOk = hs.ecdsaOk || ed25519Ok |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1115 | |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 1116 | // For test purposes, check that the peer never offers a session when |
| 1117 | // renegotiating. |
| 1118 | if c.cipherSuite != nil && len(hs.clientHello.sessionId) > 0 && c.config.Bugs.FailIfResumeOnRenego { |
| 1119 | return false, errors.New("tls: offered resumption on renegotiation") |
| 1120 | } |
| 1121 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1122 | if c.config.Bugs.FailIfSessionOffered && (len(hs.clientHello.sessionTicket) > 0 || len(hs.clientHello.sessionId) > 0) { |
| 1123 | return false, errors.New("tls: client offered a session ticket or ID") |
| 1124 | } |
| 1125 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1126 | if hs.checkForResumption() { |
| 1127 | return true, nil |
| 1128 | } |
| 1129 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1130 | var preferenceList, supportedList []uint16 |
| 1131 | if c.config.PreferServerCipherSuites { |
| 1132 | preferenceList = c.config.cipherSuites() |
| 1133 | supportedList = hs.clientHello.cipherSuites |
| 1134 | } else { |
| 1135 | preferenceList = hs.clientHello.cipherSuites |
| 1136 | supportedList = c.config.cipherSuites() |
| 1137 | } |
| 1138 | |
| 1139 | for _, id := range preferenceList { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1140 | 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] | 1141 | break |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | if hs.suite == nil { |
| 1146 | c.sendAlert(alertHandshakeFailure) |
| 1147 | return false, errors.New("tls: no cipher suite supported by both client and server") |
| 1148 | } |
| 1149 | |
| 1150 | return false, nil |
| 1151 | } |
| 1152 | |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1153 | // processClientExtensions processes all ClientHello extensions not directly |
| 1154 | // related to cipher suite negotiation and writes responses in serverExtensions. |
| 1155 | func (hs *serverHandshakeState) processClientExtensions(serverExtensions *serverExtensions) error { |
| 1156 | config := hs.c.config |
| 1157 | c := hs.c |
| 1158 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 1159 | if c.vers < VersionTLS13 || config.Bugs.NegotiateRenegotiationInfoAtAllVersions { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1160 | if !bytes.Equal(c.clientVerify, hs.clientHello.secureRenegotiation) { |
| 1161 | c.sendAlert(alertHandshakeFailure) |
| 1162 | return errors.New("tls: renegotiation mismatch") |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1163 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1164 | |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1165 | if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo { |
| 1166 | serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.clientVerify...) |
| 1167 | serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.serverVerify...) |
| 1168 | if c.config.Bugs.BadRenegotiationInfo { |
| 1169 | serverExtensions.secureRenegotiation[0] ^= 0x80 |
| 1170 | } |
| 1171 | } else { |
| 1172 | serverExtensions.secureRenegotiation = hs.clientHello.secureRenegotiation |
| 1173 | } |
| 1174 | |
| 1175 | if c.noRenegotiationInfo() { |
| 1176 | serverExtensions.secureRenegotiation = nil |
| 1177 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | serverExtensions.duplicateExtension = c.config.Bugs.DuplicateExtension |
| 1181 | |
| 1182 | if len(hs.clientHello.serverName) > 0 { |
| 1183 | c.serverName = hs.clientHello.serverName |
| 1184 | } |
| 1185 | if len(config.Certificates) == 0 { |
| 1186 | c.sendAlert(alertInternalError) |
| 1187 | return errors.New("tls: no certificates configured") |
| 1188 | } |
| 1189 | hs.cert = &config.Certificates[0] |
| 1190 | if len(hs.clientHello.serverName) > 0 { |
| 1191 | hs.cert = config.getCertificateForName(hs.clientHello.serverName) |
| 1192 | } |
| 1193 | if expected := c.config.Bugs.ExpectServerName; expected != "" && expected != hs.clientHello.serverName { |
| 1194 | return errors.New("tls: unexpected server name") |
| 1195 | } |
| 1196 | |
David Benjamin | a58baaf | 2017-02-28 20:54:28 -0500 | [diff] [blame] | 1197 | if cert := config.Bugs.RenegotiationCertificate; c.cipherSuite != nil && cert != nil { |
| 1198 | hs.cert = cert |
| 1199 | } |
| 1200 | |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1201 | if len(hs.clientHello.alpnProtocols) > 0 { |
David Benjamin | bbf4246 | 2017-03-14 21:27:10 -0400 | [diff] [blame] | 1202 | // We will never offer ALPN as a client on renegotiation |
| 1203 | // handshakes. |
| 1204 | if len(c.clientVerify) > 0 { |
| 1205 | return errors.New("tls: offered ALPN on renegotiation") |
| 1206 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1207 | if proto := c.config.Bugs.ALPNProtocol; proto != nil { |
| 1208 | serverExtensions.alpnProtocol = *proto |
| 1209 | serverExtensions.alpnProtocolEmpty = len(*proto) == 0 |
| 1210 | c.clientProtocol = *proto |
| 1211 | c.usedALPN = true |
| 1212 | } else if selectedProto, fallback := mutualProtocol(hs.clientHello.alpnProtocols, c.config.NextProtos); !fallback { |
| 1213 | serverExtensions.alpnProtocol = selectedProto |
| 1214 | c.clientProtocol = selectedProto |
| 1215 | c.usedALPN = true |
| 1216 | } |
| 1217 | } |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1218 | |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 1219 | if len(c.config.Bugs.SendALPN) > 0 { |
| 1220 | serverExtensions.alpnProtocol = c.config.Bugs.SendALPN |
| 1221 | } |
| 1222 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 1223 | if c.vers < VersionTLS13 || config.Bugs.NegotiateNPNAtAllVersions { |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1224 | if len(hs.clientHello.alpnProtocols) == 0 || c.config.Bugs.NegotiateALPNAndNPN { |
| 1225 | // Although sending an empty NPN extension is reasonable, Firefox has |
| 1226 | // had a bug around this. Best to send nothing at all if |
| 1227 | // config.NextProtos is empty. See |
| 1228 | // https://code.google.com/p/go/issues/detail?id=5445. |
| 1229 | if hs.clientHello.nextProtoNeg && len(config.NextProtos) > 0 { |
| 1230 | serverExtensions.nextProtoNeg = true |
| 1231 | serverExtensions.nextProtos = config.NextProtos |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1232 | serverExtensions.npnAfterAlpn = config.Bugs.SwapNPNAndALPN |
Nick Harper | 728eed8 | 2016-07-07 17:36:52 -0700 | [diff] [blame] | 1233 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1234 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1235 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1236 | |
David Benjamin | 8d315d7 | 2016-07-18 01:03:18 +0200 | [diff] [blame] | 1237 | if c.vers < VersionTLS13 || config.Bugs.NegotiateEMSAtAllVersions { |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 1238 | disableEMS := config.Bugs.NoExtendedMasterSecret |
| 1239 | if c.cipherSuite != nil { |
| 1240 | disableEMS = config.Bugs.NoExtendedMasterSecretOnRenegotiation |
| 1241 | } |
| 1242 | serverExtensions.extendedMasterSecret = c.vers >= VersionTLS10 && hs.clientHello.extendedMasterSecret && !disableEMS |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1243 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1244 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1245 | if hs.clientHello.channelIDSupported && config.RequestChannelID { |
| 1246 | serverExtensions.channelIDRequested = true |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | if hs.clientHello.srtpProtectionProfiles != nil { |
| 1250 | SRTPLoop: |
| 1251 | for _, p1 := range c.config.SRTPProtectionProfiles { |
| 1252 | for _, p2 := range hs.clientHello.srtpProtectionProfiles { |
| 1253 | if p1 == p2 { |
| 1254 | serverExtensions.srtpProtectionProfile = p1 |
| 1255 | c.srtpProtectionProfile = p1 |
| 1256 | break SRTPLoop |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | if c.config.Bugs.SendSRTPProtectionProfile != 0 { |
| 1263 | serverExtensions.srtpProtectionProfile = c.config.Bugs.SendSRTPProtectionProfile |
| 1264 | } |
| 1265 | |
| 1266 | if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil { |
| 1267 | if hs.clientHello.customExtension != *expected { |
| 1268 | return fmt.Errorf("tls: bad custom extension contents %q", hs.clientHello.customExtension) |
| 1269 | } |
| 1270 | } |
| 1271 | serverExtensions.customExtension = config.Bugs.CustomExtension |
| 1272 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1273 | if c.config.Bugs.AdvertiseTicketExtension { |
| 1274 | serverExtensions.ticketSupported = true |
| 1275 | } |
| 1276 | |
David Benjamin | a81967b | 2016-12-22 09:16:57 -0500 | [diff] [blame] | 1277 | if c.config.Bugs.SendSupportedPointFormats != nil { |
| 1278 | serverExtensions.supportedPoints = c.config.Bugs.SendSupportedPointFormats |
| 1279 | } |
| 1280 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 1281 | if !hs.clientHello.hasGREASEExtension && config.Bugs.ExpectGREASE { |
| 1282 | return errors.New("tls: no GREASE extension found") |
| 1283 | } |
| 1284 | |
David Benjamin | 023d419 | 2017-02-06 13:49:07 -0500 | [diff] [blame] | 1285 | serverExtensions.serverNameAck = c.config.Bugs.SendServerNameAck |
| 1286 | |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 1287 | return nil |
| 1288 | } |
| 1289 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1290 | // checkForResumption returns true if we should perform resumption on this connection. |
| 1291 | func (hs *serverHandshakeState) checkForResumption() bool { |
| 1292 | c := hs.c |
| 1293 | |
David Benjamin | 405da48 | 2016-08-08 17:25:07 -0400 | [diff] [blame] | 1294 | ticket := hs.clientHello.sessionTicket |
| 1295 | 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] | 1296 | ticket = hs.clientHello.pskIdentities[0].ticket |
David Benjamin | 405da48 | 2016-08-08 17:25:07 -0400 | [diff] [blame] | 1297 | } |
| 1298 | if len(ticket) > 0 { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1299 | if c.config.SessionTicketsDisabled { |
| 1300 | return false |
| 1301 | } |
David Benjamin | b0c8db7 | 2014-09-24 15:19:56 -0400 | [diff] [blame] | 1302 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1303 | var ok bool |
David Benjamin | 405da48 | 2016-08-08 17:25:07 -0400 | [diff] [blame] | 1304 | if hs.sessionState, ok = c.decryptTicket(ticket); !ok { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1305 | return false |
| 1306 | } |
| 1307 | } else { |
| 1308 | if c.config.ServerSessionCache == nil { |
| 1309 | return false |
| 1310 | } |
| 1311 | |
| 1312 | var ok bool |
| 1313 | sessionId := string(hs.clientHello.sessionId) |
| 1314 | if hs.sessionState, ok = c.config.ServerSessionCache.Get(sessionId); !ok { |
| 1315 | return false |
| 1316 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1319 | if c.config.Bugs.AcceptAnySession { |
| 1320 | // Replace the cipher suite with one known to work, to test |
| 1321 | // cross-version resumption attempts. |
| 1322 | hs.sessionState.cipherSuite = TLS_RSA_WITH_AES_128_CBC_SHA |
| 1323 | } else { |
David Benjamin | 405da48 | 2016-08-08 17:25:07 -0400 | [diff] [blame] | 1324 | // Never resume a session for a different SSL version. |
| 1325 | if c.vers != hs.sessionState.vers { |
| 1326 | return false |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1327 | } |
David Benjamin | 405da48 | 2016-08-08 17:25:07 -0400 | [diff] [blame] | 1328 | |
| 1329 | cipherSuiteOk := false |
| 1330 | // Check that the client is still offering the ciphersuite in the session. |
| 1331 | for _, id := range hs.clientHello.cipherSuites { |
| 1332 | if id == hs.sessionState.cipherSuite { |
| 1333 | cipherSuiteOk = true |
| 1334 | break |
| 1335 | } |
| 1336 | } |
| 1337 | if !cipherSuiteOk { |
| 1338 | return false |
| 1339 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | // Check that we also support the ciphersuite from the session. |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1343 | hs.suite = c.tryCipherSuite(hs.sessionState.cipherSuite, c.config.cipherSuites(), c.vers, hs.ellipticOk, hs.ecdsaOk) |
| 1344 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1345 | if hs.suite == nil { |
| 1346 | return false |
| 1347 | } |
| 1348 | |
| 1349 | sessionHasClientCerts := len(hs.sessionState.certificates) != 0 |
| 1350 | needClientCerts := c.config.ClientAuth == RequireAnyClientCert || c.config.ClientAuth == RequireAndVerifyClientCert |
| 1351 | if needClientCerts && !sessionHasClientCerts { |
| 1352 | return false |
| 1353 | } |
| 1354 | if sessionHasClientCerts && c.config.ClientAuth == NoClientCert { |
| 1355 | return false |
| 1356 | } |
| 1357 | |
| 1358 | return true |
| 1359 | } |
| 1360 | |
| 1361 | func (hs *serverHandshakeState) doResumeHandshake() error { |
| 1362 | c := hs.c |
| 1363 | |
| 1364 | hs.hello.cipherSuite = hs.suite.id |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 1365 | if c.config.Bugs.SendCipherSuite != 0 { |
| 1366 | hs.hello.cipherSuite = c.config.Bugs.SendCipherSuite |
| 1367 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1368 | // We echo the client's session ID in the ServerHello to let it know |
| 1369 | // that we're doing a resumption. |
| 1370 | hs.hello.sessionId = hs.clientHello.sessionId |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1371 | hs.hello.extensions.ticketSupported = c.config.Bugs.RenewTicketOnResume |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1372 | |
David Benjamin | 80d1b35 | 2016-05-04 19:19:06 -0400 | [diff] [blame] | 1373 | if c.config.Bugs.SendSCTListOnResume != nil { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1374 | hs.hello.extensions.sctList = c.config.Bugs.SendSCTListOnResume |
David Benjamin | 80d1b35 | 2016-05-04 19:19:06 -0400 | [diff] [blame] | 1375 | } |
| 1376 | |
David Benjamin | daa8850 | 2016-10-04 16:32:16 -0400 | [diff] [blame] | 1377 | if c.config.Bugs.SendOCSPResponseOnResume != nil { |
| 1378 | // There is no way, syntactically, to send an OCSP response on a |
| 1379 | // resumption handshake. |
| 1380 | hs.hello.extensions.ocspStapling = true |
| 1381 | } |
| 1382 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1383 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1384 | hs.finishedHash.discardHandshakeBuffer() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1385 | hs.writeClientHash(hs.clientHello.marshal()) |
| 1386 | hs.writeServerHash(hs.hello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1387 | |
| 1388 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 1389 | |
| 1390 | if len(hs.sessionState.certificates) > 0 { |
| 1391 | if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil { |
| 1392 | return err |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | hs.masterSecret = hs.sessionState.masterSecret |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1397 | c.extendedMasterSecret = hs.sessionState.extendedMasterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1398 | |
| 1399 | return nil |
| 1400 | } |
| 1401 | |
| 1402 | func (hs *serverHandshakeState) doFullHandshake() error { |
| 1403 | config := hs.c.config |
| 1404 | c := hs.c |
| 1405 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1406 | isPSK := hs.suite.flags&suitePSK != 0 |
| 1407 | if !isPSK && hs.clientHello.ocspStapling && len(hs.cert.OCSPStaple) > 0 { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1408 | hs.hello.extensions.ocspStapling = true |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1411 | if hs.clientHello.sctListSupported && len(hs.cert.SignedCertificateTimestampList) > 0 { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1412 | hs.hello.extensions.sctList = hs.cert.SignedCertificateTimestampList |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 1413 | } |
| 1414 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1415 | hs.hello.extensions.ticketSupported = hs.clientHello.ticketSupported && !config.SessionTicketsDisabled && c.vers > VersionSSL30 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1416 | hs.hello.cipherSuite = hs.suite.id |
David Benjamin | 6095de8 | 2014-12-27 01:50:38 -0500 | [diff] [blame] | 1417 | if config.Bugs.SendCipherSuite != 0 { |
| 1418 | hs.hello.cipherSuite = config.Bugs.SendCipherSuite |
| 1419 | } |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1420 | c.extendedMasterSecret = hs.hello.extensions.extendedMasterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1421 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1422 | // Generate a session ID if we're to save the session. |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1423 | if !hs.hello.extensions.ticketSupported && config.ServerSessionCache != nil { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1424 | hs.hello.sessionId = make([]byte, 32) |
| 1425 | if _, err := io.ReadFull(config.rand(), hs.hello.sessionId); err != nil { |
| 1426 | c.sendAlert(alertInternalError) |
| 1427 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 1428 | } |
| 1429 | } |
| 1430 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1431 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1432 | hs.writeClientHash(hs.clientHello.marshal()) |
| 1433 | hs.writeServerHash(hs.hello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1434 | |
David Benjamin | abe94e3 | 2016-09-04 14:18:58 -0400 | [diff] [blame] | 1435 | if config.Bugs.SendSNIWarningAlert { |
| 1436 | c.SendAlert(alertLevelWarning, alertUnrecognizedName) |
| 1437 | } |
| 1438 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1439 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 1440 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1441 | if !isPSK { |
| 1442 | certMsg := new(certificateMsg) |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 1443 | if !config.Bugs.EmptyCertificateList { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1444 | for _, certData := range hs.cert.Certificate { |
| 1445 | certMsg.certificates = append(certMsg.certificates, certificateEntry{ |
| 1446 | data: certData, |
| 1447 | }) |
| 1448 | } |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 1449 | } |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1450 | if !config.Bugs.UnauthenticatedECDH { |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 1451 | certMsgBytes := certMsg.marshal() |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 1452 | hs.writeServerHash(certMsgBytes) |
| 1453 | c.writeRecord(recordTypeHandshake, certMsgBytes) |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1454 | } |
David Benjamin | 1c375dd | 2014-07-12 00:48:23 -0400 | [diff] [blame] | 1455 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1456 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1457 | if hs.hello.extensions.ocspStapling && !c.config.Bugs.SkipCertificateStatus { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1458 | certStatus := new(certificateStatusMsg) |
| 1459 | certStatus.statusType = statusTypeOCSP |
| 1460 | certStatus.response = hs.cert.OCSPStaple |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1461 | hs.writeServerHash(certStatus.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1462 | c.writeRecord(recordTypeHandshake, certStatus.marshal()) |
| 1463 | } |
| 1464 | |
| 1465 | keyAgreement := hs.suite.ka(c.vers) |
| 1466 | skx, err := keyAgreement.generateServerKeyExchange(config, hs.cert, hs.clientHello, hs.hello) |
| 1467 | if err != nil { |
| 1468 | c.sendAlert(alertHandshakeFailure) |
| 1469 | return err |
| 1470 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 1471 | if ecdhe, ok := keyAgreement.(*ecdheKeyAgreement); ok { |
| 1472 | c.curveID = ecdhe.curveID |
| 1473 | } |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1474 | if skx != nil && !config.Bugs.SkipServerKeyExchange { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1475 | hs.writeServerHash(skx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1476 | c.writeRecord(recordTypeHandshake, skx.marshal()) |
| 1477 | } |
| 1478 | |
| 1479 | if config.ClientAuth >= RequestClientCert { |
| 1480 | // Request a client certificate |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 1481 | certReq := &certificateRequestMsg{ |
| 1482 | certificateTypes: config.ClientCertificateTypes, |
| 1483 | } |
| 1484 | if certReq.certificateTypes == nil { |
| 1485 | certReq.certificateTypes = []byte{ |
| 1486 | byte(CertTypeRSASign), |
| 1487 | byte(CertTypeECDSASign), |
| 1488 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1489 | } |
| 1490 | if c.vers >= VersionTLS12 { |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1491 | certReq.hasSignatureAlgorithm = true |
| 1492 | if !config.Bugs.NoSignatureAlgorithms { |
David Benjamin | 7a41d37 | 2016-07-09 11:21:54 -0700 | [diff] [blame] | 1493 | certReq.signatureAlgorithms = config.verifySignatureAlgorithms() |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 1494 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | // An empty list of certificateAuthorities signals to |
| 1498 | // the client that it may send any certificate in response |
| 1499 | // to our request. When we know the CAs we trust, then |
| 1500 | // we can send them down, so that the client can choose |
| 1501 | // an appropriate certificate to give to us. |
| 1502 | if config.ClientCAs != nil { |
| 1503 | certReq.certificateAuthorities = config.ClientCAs.Subjects() |
| 1504 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1505 | hs.writeServerHash(certReq.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1506 | c.writeRecord(recordTypeHandshake, certReq.marshal()) |
| 1507 | } |
| 1508 | |
| 1509 | helloDone := new(serverHelloDoneMsg) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1510 | hs.writeServerHash(helloDone.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1511 | c.writeRecord(recordTypeHandshake, helloDone.marshal()) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1512 | c.flushHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1513 | |
| 1514 | var pub crypto.PublicKey // public key for client auth, if any |
| 1515 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1516 | if err := c.simulatePacketLoss(nil); err != nil { |
| 1517 | return err |
| 1518 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1519 | msg, err := c.readHandshake() |
| 1520 | if err != nil { |
| 1521 | return err |
| 1522 | } |
| 1523 | |
| 1524 | var ok bool |
| 1525 | // If we requested a client certificate, then the client must send a |
| 1526 | // certificate message, even if it's empty. |
| 1527 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1528 | var certMsg *certificateMsg |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1529 | var certificates [][]byte |
| 1530 | if certMsg, ok = msg.(*certificateMsg); ok { |
| 1531 | if c.vers == VersionSSL30 && len(certMsg.certificates) == 0 { |
| 1532 | return errors.New("tls: empty certificate message in SSL 3.0") |
| 1533 | } |
| 1534 | |
| 1535 | hs.writeClientHash(certMsg.marshal()) |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1536 | for _, cert := range certMsg.certificates { |
| 1537 | certificates = append(certificates, cert.data) |
| 1538 | } |
David Benjamin | 053fee9 | 2017-01-02 08:30:36 -0500 | [diff] [blame] | 1539 | } else if c.vers == VersionSSL30 { |
| 1540 | // In SSL 3.0, no certificate is signaled by a warning |
| 1541 | // alert which we translate to ssl3NoCertificateMsg. |
| 1542 | if _, ok := msg.(*ssl3NoCertificateMsg); !ok { |
| 1543 | return errors.New("tls: client provided neither a certificate nor no_certificate warning alert") |
| 1544 | } |
| 1545 | } else { |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1546 | // In TLS, the Certificate message is required. In SSL |
| 1547 | // 3.0, the peer skips it when sending no certificates. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1548 | c.sendAlert(alertUnexpectedMessage) |
| 1549 | return unexpectedMessageError(certMsg, msg) |
| 1550 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1551 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1552 | if len(certificates) == 0 { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1553 | // The client didn't actually send a certificate |
| 1554 | switch config.ClientAuth { |
| 1555 | case RequireAnyClientCert, RequireAndVerifyClientCert: |
| 1556 | c.sendAlert(alertBadCertificate) |
| 1557 | return errors.New("tls: client didn't provide a certificate") |
| 1558 | } |
| 1559 | } |
| 1560 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 1561 | pub, err = hs.processCertsFromClient(certificates) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1562 | if err != nil { |
| 1563 | return err |
| 1564 | } |
| 1565 | |
David Benjamin | 053fee9 | 2017-01-02 08:30:36 -0500 | [diff] [blame] | 1566 | msg, err = c.readHandshake() |
| 1567 | if err != nil { |
| 1568 | return err |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | // Get client key exchange |
| 1573 | ckx, ok := msg.(*clientKeyExchangeMsg) |
| 1574 | if !ok { |
| 1575 | c.sendAlert(alertUnexpectedMessage) |
| 1576 | return unexpectedMessageError(ckx, msg) |
| 1577 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1578 | hs.writeClientHash(ckx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1579 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1580 | preMasterSecret, err := keyAgreement.processClientKeyExchange(config, hs.cert, ckx, c.vers) |
| 1581 | if err != nil { |
| 1582 | c.sendAlert(alertHandshakeFailure) |
| 1583 | return err |
| 1584 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1585 | if c.extendedMasterSecret { |
| 1586 | hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 1587 | } else { |
| 1588 | if c.config.Bugs.RequireExtendedMasterSecret { |
| 1589 | return errors.New("tls: extended master secret required but not supported by peer") |
| 1590 | } |
| 1591 | hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.clientHello.random, hs.hello.random) |
| 1592 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1593 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1594 | // If we received a client cert in response to our certificate request message, |
| 1595 | // the client will send us a certificateVerifyMsg immediately after the |
| 1596 | // clientKeyExchangeMsg. This message is a digest of all preceding |
| 1597 | // handshake-layer messages that is signed using the private key corresponding |
| 1598 | // to the client's certificate. This allows us to verify that the client is in |
| 1599 | // possession of the private key of the certificate. |
| 1600 | if len(c.peerCertificates) > 0 { |
| 1601 | msg, err = c.readHandshake() |
| 1602 | if err != nil { |
| 1603 | return err |
| 1604 | } |
| 1605 | certVerify, ok := msg.(*certificateVerifyMsg) |
| 1606 | if !ok { |
| 1607 | c.sendAlert(alertUnexpectedMessage) |
| 1608 | return unexpectedMessageError(certVerify, msg) |
| 1609 | } |
| 1610 | |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1611 | // Determine the signature type. |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1612 | var sigAlg signatureAlgorithm |
| 1613 | if certVerify.hasSignatureAlgorithm { |
| 1614 | sigAlg = certVerify.signatureAlgorithm |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1615 | c.peerSignatureAlgorithm = sigAlg |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1616 | } |
| 1617 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 1618 | if c.vers > VersionSSL30 { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 1619 | 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] | 1620 | } else { |
| 1621 | // SSL 3.0's client certificate construction is |
| 1622 | // incompatible with signatureAlgorithm. |
| 1623 | rsaPub, ok := pub.(*rsa.PublicKey) |
| 1624 | if !ok { |
| 1625 | err = errors.New("unsupported key type for client certificate") |
| 1626 | } else { |
| 1627 | digest := hs.finishedHash.hashForClientCertificateSSL3(hs.masterSecret) |
| 1628 | err = rsa.VerifyPKCS1v15(rsaPub, crypto.MD5SHA1, digest, certVerify.signature) |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 1629 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1630 | } |
| 1631 | if err != nil { |
| 1632 | c.sendAlert(alertBadCertificate) |
| 1633 | return errors.New("could not validate signature of connection nonces: " + err.Error()) |
| 1634 | } |
| 1635 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1636 | hs.writeClientHash(certVerify.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 1639 | hs.finishedHash.discardHandshakeBuffer() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1640 | |
| 1641 | return nil |
| 1642 | } |
| 1643 | |
| 1644 | func (hs *serverHandshakeState) establishKeys() error { |
| 1645 | c := hs.c |
| 1646 | |
| 1647 | clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1648 | 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] | 1649 | |
| 1650 | var clientCipher, serverCipher interface{} |
| 1651 | var clientHash, serverHash macFunction |
| 1652 | |
| 1653 | if hs.suite.aead == nil { |
| 1654 | clientCipher = hs.suite.cipher(clientKey, clientIV, true /* for reading */) |
| 1655 | clientHash = hs.suite.mac(c.vers, clientMAC) |
| 1656 | serverCipher = hs.suite.cipher(serverKey, serverIV, false /* not for reading */) |
| 1657 | serverHash = hs.suite.mac(c.vers, serverMAC) |
| 1658 | } else { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 1659 | clientCipher = hs.suite.aead(c.vers, clientKey, clientIV) |
| 1660 | serverCipher = hs.suite.aead(c.vers, serverKey, serverIV) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | c.in.prepareCipherSpec(c.vers, clientCipher, clientHash) |
| 1664 | c.out.prepareCipherSpec(c.vers, serverCipher, serverHash) |
| 1665 | |
| 1666 | return nil |
| 1667 | } |
| 1668 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1669 | func (hs *serverHandshakeState) readFinished(out []byte, isResume bool) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1670 | c := hs.c |
| 1671 | |
| 1672 | c.readRecord(recordTypeChangeCipherSpec) |
| 1673 | if err := c.in.error(); err != nil { |
| 1674 | return err |
| 1675 | } |
| 1676 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1677 | if hs.hello.extensions.nextProtoNeg { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1678 | msg, err := c.readHandshake() |
| 1679 | if err != nil { |
| 1680 | return err |
| 1681 | } |
| 1682 | nextProto, ok := msg.(*nextProtoMsg) |
| 1683 | if !ok { |
| 1684 | c.sendAlert(alertUnexpectedMessage) |
| 1685 | return unexpectedMessageError(nextProto, msg) |
| 1686 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1687 | hs.writeClientHash(nextProto.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1688 | c.clientProtocol = nextProto.proto |
| 1689 | } |
| 1690 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1691 | if hs.hello.extensions.channelIDRequested { |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1692 | msg, err := c.readHandshake() |
| 1693 | if err != nil { |
| 1694 | return err |
| 1695 | } |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1696 | channelIDMsg, ok := msg.(*channelIDMsg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1697 | if !ok { |
| 1698 | c.sendAlert(alertUnexpectedMessage) |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1699 | return unexpectedMessageError(channelIDMsg, msg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1700 | } |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1701 | var resumeHash []byte |
| 1702 | if isResume { |
| 1703 | resumeHash = hs.sessionState.handshakeHash |
| 1704 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1705 | channelID, err := verifyChannelIDMessage(channelIDMsg, hs.finishedHash.hashForChannelID(resumeHash)) |
| 1706 | if err != nil { |
| 1707 | return err |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1708 | } |
| 1709 | c.channelID = channelID |
| 1710 | |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 1711 | hs.writeClientHash(channelIDMsg.marshal()) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1712 | } |
| 1713 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1714 | msg, err := c.readHandshake() |
| 1715 | if err != nil { |
| 1716 | return err |
| 1717 | } |
| 1718 | clientFinished, ok := msg.(*finishedMsg) |
| 1719 | if !ok { |
| 1720 | c.sendAlert(alertUnexpectedMessage) |
| 1721 | return unexpectedMessageError(clientFinished, msg) |
| 1722 | } |
| 1723 | |
| 1724 | verify := hs.finishedHash.clientSum(hs.masterSecret) |
| 1725 | if len(verify) != len(clientFinished.verifyData) || |
| 1726 | subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 { |
| 1727 | c.sendAlert(alertHandshakeFailure) |
| 1728 | return errors.New("tls: client's Finished message is incorrect") |
| 1729 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1730 | c.clientVerify = append(c.clientVerify[:0], clientFinished.verifyData...) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1731 | copy(out, clientFinished.verifyData) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1732 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1733 | hs.writeClientHash(clientFinished.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1734 | return nil |
| 1735 | } |
| 1736 | |
| 1737 | func (hs *serverHandshakeState) sendSessionTicket() error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1738 | c := hs.c |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1739 | state := sessionState{ |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1740 | vers: c.vers, |
| 1741 | cipherSuite: hs.suite.id, |
| 1742 | masterSecret: hs.masterSecret, |
| 1743 | certificates: hs.certsFromClient, |
Nick Harper | c984611 | 2016-10-17 15:05:35 -0700 | [diff] [blame] | 1744 | handshakeHash: hs.finishedHash.Sum(), |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1745 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1746 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 1747 | if !hs.hello.extensions.ticketSupported || hs.c.config.Bugs.SkipNewSessionTicket { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1748 | if c.config.ServerSessionCache != nil && len(hs.hello.sessionId) != 0 { |
| 1749 | c.config.ServerSessionCache.Put(string(hs.hello.sessionId), &state) |
| 1750 | } |
| 1751 | return nil |
| 1752 | } |
| 1753 | |
| 1754 | m := new(newSessionTicketMsg) |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 1755 | if c.config.Bugs.SendTicketLifetime != 0 { |
| 1756 | m.ticketLifetime = uint32(c.config.Bugs.SendTicketLifetime / time.Second) |
| 1757 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1758 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1759 | if !c.config.Bugs.SendEmptySessionTicket { |
| 1760 | var err error |
| 1761 | m.ticket, err = c.encryptTicket(&state) |
| 1762 | if err != nil { |
| 1763 | return err |
| 1764 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1765 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1766 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1767 | hs.writeServerHash(m.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1768 | c.writeRecord(recordTypeHandshake, m.marshal()) |
| 1769 | |
| 1770 | return nil |
| 1771 | } |
| 1772 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1773 | func (hs *serverHandshakeState) sendFinished(out []byte) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1774 | c := hs.c |
| 1775 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1776 | finished := new(finishedMsg) |
| 1777 | finished.verifyData = hs.finishedHash.serverSum(hs.masterSecret) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 1778 | copy(out, finished.verifyData) |
David Benjamin | 513f0ea | 2015-04-02 19:33:31 -0400 | [diff] [blame] | 1779 | if c.config.Bugs.BadFinished { |
| 1780 | finished.verifyData[0]++ |
| 1781 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 1782 | c.serverVerify = append(c.serverVerify[:0], finished.verifyData...) |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 1783 | hs.finishedBytes = finished.marshal() |
| 1784 | hs.writeServerHash(hs.finishedBytes) |
| 1785 | postCCSBytes := hs.finishedBytes |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1786 | |
| 1787 | if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
| 1788 | c.writeRecord(recordTypeHandshake, postCCSBytes[:5]) |
| 1789 | postCCSBytes = postCCSBytes[5:] |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 1790 | } else if c.config.Bugs.SendUnencryptedFinished { |
| 1791 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
| 1792 | postCCSBytes = nil |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1793 | } |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 1794 | c.flushHandshake() |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 1795 | |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 1796 | if !c.config.Bugs.SkipChangeCipherSpec { |
David Benjamin | 8411b24 | 2015-11-26 12:07:28 -0500 | [diff] [blame] | 1797 | ccs := []byte{1} |
| 1798 | if c.config.Bugs.BadChangeCipherSpec != nil { |
| 1799 | ccs = c.config.Bugs.BadChangeCipherSpec |
| 1800 | } |
| 1801 | c.writeRecord(recordTypeChangeCipherSpec, ccs) |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 1802 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1803 | |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 1804 | if c.config.Bugs.AppDataAfterChangeCipherSpec != nil { |
| 1805 | c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec) |
| 1806 | } |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 1807 | if c.config.Bugs.AlertAfterChangeCipherSpec != 0 { |
| 1808 | c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec) |
| 1809 | return errors.New("tls: simulating post-CCS alert") |
| 1810 | } |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 1811 | |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 1812 | if !c.config.Bugs.SkipFinished && len(postCCSBytes) > 0 { |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 1813 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
David Benjamin | 02edcd0 | 2016-07-27 17:40:37 -0400 | [diff] [blame] | 1814 | if c.config.Bugs.SendExtraFinished { |
| 1815 | c.writeRecord(recordTypeHandshake, finished.marshal()) |
| 1816 | } |
| 1817 | |
David Benjamin | 12d2c48 | 2016-07-24 10:56:51 -0400 | [diff] [blame] | 1818 | if !c.config.Bugs.PackHelloRequestWithFinished { |
| 1819 | // Defer flushing until renegotiation. |
| 1820 | c.flushHandshake() |
| 1821 | } |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 1822 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1823 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1824 | c.cipherSuite = hs.suite |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1825 | |
| 1826 | return nil |
| 1827 | } |
| 1828 | |
| 1829 | // processCertsFromClient takes a chain of client certificates either from a |
| 1830 | // Certificates message or from a sessionState and verifies them. It returns |
| 1831 | // the public key of the leaf certificate. |
| 1832 | func (hs *serverHandshakeState) processCertsFromClient(certificates [][]byte) (crypto.PublicKey, error) { |
| 1833 | c := hs.c |
| 1834 | |
| 1835 | hs.certsFromClient = certificates |
| 1836 | certs := make([]*x509.Certificate, len(certificates)) |
| 1837 | var err error |
| 1838 | for i, asn1Data := range certificates { |
| 1839 | if certs[i], err = x509.ParseCertificate(asn1Data); err != nil { |
| 1840 | c.sendAlert(alertBadCertificate) |
| 1841 | return nil, errors.New("tls: failed to parse client certificate: " + err.Error()) |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | if c.config.ClientAuth >= VerifyClientCertIfGiven && len(certs) > 0 { |
| 1846 | opts := x509.VerifyOptions{ |
| 1847 | Roots: c.config.ClientCAs, |
| 1848 | CurrentTime: c.config.time(), |
| 1849 | Intermediates: x509.NewCertPool(), |
| 1850 | KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, |
| 1851 | } |
| 1852 | |
| 1853 | for _, cert := range certs[1:] { |
| 1854 | opts.Intermediates.AddCert(cert) |
| 1855 | } |
| 1856 | |
| 1857 | chains, err := certs[0].Verify(opts) |
| 1858 | if err != nil { |
| 1859 | c.sendAlert(alertBadCertificate) |
| 1860 | return nil, errors.New("tls: failed to verify client's certificate: " + err.Error()) |
| 1861 | } |
| 1862 | |
| 1863 | ok := false |
| 1864 | for _, ku := range certs[0].ExtKeyUsage { |
| 1865 | if ku == x509.ExtKeyUsageClientAuth { |
| 1866 | ok = true |
| 1867 | break |
| 1868 | } |
| 1869 | } |
| 1870 | if !ok { |
| 1871 | c.sendAlert(alertHandshakeFailure) |
| 1872 | return nil, errors.New("tls: client's certificate's extended key usage doesn't permit it to be used for client authentication") |
| 1873 | } |
| 1874 | |
| 1875 | c.verifiedChains = chains |
| 1876 | } |
| 1877 | |
| 1878 | if len(certs) > 0 { |
David Benjamin | d768c5d | 2017-03-28 18:28:44 -0500 | [diff] [blame] | 1879 | pub := getCertificatePublicKey(certs[0]) |
| 1880 | switch pub.(type) { |
| 1881 | case *ecdsa.PublicKey, *rsa.PublicKey, ed25519.PublicKey: |
| 1882 | break |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1883 | default: |
| 1884 | c.sendAlert(alertUnsupportedCertificate) |
David Benjamin | d768c5d | 2017-03-28 18:28:44 -0500 | [diff] [blame] | 1885 | 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] | 1886 | } |
| 1887 | c.peerCertificates = certs |
| 1888 | return pub, nil |
| 1889 | } |
| 1890 | |
| 1891 | return nil, nil |
| 1892 | } |
| 1893 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1894 | func verifyChannelIDMessage(channelIDMsg *channelIDMsg, channelIDHash []byte) (*ecdsa.PublicKey, error) { |
| 1895 | x := new(big.Int).SetBytes(channelIDMsg.channelID[0:32]) |
| 1896 | y := new(big.Int).SetBytes(channelIDMsg.channelID[32:64]) |
| 1897 | r := new(big.Int).SetBytes(channelIDMsg.channelID[64:96]) |
| 1898 | s := new(big.Int).SetBytes(channelIDMsg.channelID[96:128]) |
| 1899 | if !elliptic.P256().IsOnCurve(x, y) { |
| 1900 | return nil, errors.New("tls: invalid channel ID public key") |
| 1901 | } |
| 1902 | channelID := &ecdsa.PublicKey{elliptic.P256(), x, y} |
| 1903 | if !ecdsa.Verify(channelID, channelIDHash, r, s) { |
| 1904 | return nil, errors.New("tls: invalid channel ID signature") |
| 1905 | } |
| 1906 | return channelID, nil |
| 1907 | } |
| 1908 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1909 | func (hs *serverHandshakeState) writeServerHash(msg []byte) { |
| 1910 | // writeServerHash is called before writeRecord. |
| 1911 | hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 1912 | } |
| 1913 | |
| 1914 | func (hs *serverHandshakeState) writeClientHash(msg []byte) { |
| 1915 | // writeClientHash is called after readHandshake. |
| 1916 | hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 1917 | } |
| 1918 | |
| 1919 | func (hs *serverHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 1920 | if hs.c.isDTLS { |
| 1921 | // This is somewhat hacky. DTLS hashes a slightly different format. |
| 1922 | // First, the TLS header. |
| 1923 | hs.finishedHash.Write(msg[:4]) |
| 1924 | // Then the sequence number and reassembled fragment offset (always 0). |
| 1925 | hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 1926 | // Then the reassembled fragment (always equal to the message length). |
| 1927 | hs.finishedHash.Write(msg[1:4]) |
| 1928 | // And then the message body. |
| 1929 | hs.finishedHash.Write(msg[4:]) |
| 1930 | } else { |
| 1931 | hs.finishedHash.Write(msg) |
| 1932 | } |
| 1933 | } |
| 1934 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1935 | // tryCipherSuite returns a cipherSuite with the given id if that cipher suite |
| 1936 | // is acceptable to use. |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1937 | 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] | 1938 | for _, supported := range supportedCipherSuites { |
| 1939 | if id == supported { |
| 1940 | var candidate *cipherSuite |
| 1941 | |
| 1942 | for _, s := range cipherSuites { |
| 1943 | if s.id == id { |
| 1944 | candidate = s |
| 1945 | break |
| 1946 | } |
| 1947 | } |
| 1948 | if candidate == nil { |
| 1949 | continue |
| 1950 | } |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1951 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1952 | // Don't select a ciphersuite which we can't |
| 1953 | // support for this client. |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1954 | if version >= VersionTLS13 || candidate.flags&suiteTLS13 != 0 { |
| 1955 | if version < VersionTLS13 || candidate.flags&suiteTLS13 == 0 { |
| 1956 | continue |
| 1957 | } |
| 1958 | return candidate |
David Benjamin | 5ecb88b | 2016-10-04 17:51:35 -0400 | [diff] [blame] | 1959 | } |
| 1960 | if (candidate.flags&suiteECDHE != 0) && !ellipticOk { |
| 1961 | continue |
| 1962 | } |
| 1963 | if (candidate.flags&suiteECDSA != 0) != ecdsaOk { |
| 1964 | continue |
| 1965 | } |
| 1966 | if version < VersionTLS12 && candidate.flags&suiteTLS12 != 0 { |
| 1967 | continue |
| 1968 | } |
David Benjamin | 5ecb88b | 2016-10-04 17:51:35 -0400 | [diff] [blame] | 1969 | if c.isDTLS && candidate.flags&suiteNoDTLS != 0 { |
| 1970 | continue |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1971 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1972 | return candidate |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | return nil |
| 1977 | } |
David Benjamin | f93995b | 2015-11-05 18:23:20 -0500 | [diff] [blame] | 1978 | |
| 1979 | func isTLS12Cipher(id uint16) bool { |
| 1980 | for _, cipher := range cipherSuites { |
| 1981 | if cipher.id != id { |
| 1982 | continue |
| 1983 | } |
| 1984 | return cipher.flags&suiteTLS12 != 0 |
| 1985 | } |
| 1986 | // Unknown cipher. |
| 1987 | return false |
| 1988 | } |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 1989 | |
| 1990 | func isGREASEValue(val uint16) bool { |
David Benjamin | 3c6a1ea | 2016-09-26 18:30:05 -0400 | [diff] [blame] | 1991 | return val&0x0f0f == 0x0a0a && val&0xff == val>>8 |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 1992 | } |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1993 | |
| 1994 | func verifyPSKBinder(clientHello *clientHelloMsg, sessionState *sessionState, binderToVerify, transcript []byte) error { |
| 1995 | binderLen := 2 |
| 1996 | for _, binder := range clientHello.pskBinders { |
| 1997 | binderLen += 1 + len(binder) |
| 1998 | } |
| 1999 | |
| 2000 | truncatedHello := clientHello.marshal() |
| 2001 | truncatedHello = truncatedHello[:len(truncatedHello)-binderLen] |
| 2002 | pskCipherSuite := cipherSuiteFromID(sessionState.cipherSuite) |
| 2003 | if pskCipherSuite == nil { |
| 2004 | return errors.New("tls: Unknown cipher suite for PSK in session") |
| 2005 | } |
| 2006 | |
| 2007 | binder := computePSKBinder(sessionState.masterSecret, resumptionPSKBinderLabel, pskCipherSuite, transcript, truncatedHello) |
| 2008 | if !bytes.Equal(binder, binderToVerify) { |
| 2009 | return errors.New("tls: PSK binder does not verify") |
| 2010 | } |
| 2011 | |
| 2012 | return nil |
| 2013 | } |