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