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