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