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