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