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