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