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 | } |
| 55 | isResume, err := hs.processClientHello() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | // For an overview of TLS handshaking, see https://tools.ietf.org/html/rfc5246#section-7.3 |
| 61 | if isResume { |
| 62 | // The client has included a session ticket and so we do an abbreviated handshake. |
| 63 | if err := hs.doResumeHandshake(); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | if err := hs.establishKeys(); err != nil { |
| 67 | return err |
| 68 | } |
David Benjamin | bed9aae | 2014-08-07 19:13:38 -0400 | [diff] [blame] | 69 | if c.config.Bugs.RenewTicketOnResume { |
| 70 | if err := hs.sendSessionTicket(); err != nil { |
| 71 | return err |
| 72 | } |
| 73 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 74 | if err := hs.sendFinished(c.firstFinished[:]); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 75 | return err |
| 76 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 77 | // Most retransmits are triggered by a timeout, but the final |
| 78 | // leg of the handshake is retransmited upon re-receiving a |
| 79 | // Finished. |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 80 | if err := c.simulatePacketLoss(func() { |
| 81 | c.writeRecord(recordTypeHandshake, hs.finishedBytes) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 82 | c.flushHandshake() |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 83 | }); err != nil { |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 84 | return err |
| 85 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 86 | if err := hs.readFinished(nil, isResume); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | return err |
| 88 | } |
| 89 | c.didResume = true |
| 90 | } else { |
| 91 | // The client didn't include a session ticket, or it wasn't |
| 92 | // valid so we do a full handshake. |
| 93 | if err := hs.doFullHandshake(); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | if err := hs.establishKeys(); err != nil { |
| 97 | return err |
| 98 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 99 | if err := hs.readFinished(c.firstFinished[:], isResume); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 100 | return err |
| 101 | } |
David Benjamin | 1c63315 | 2015-04-02 20:19:11 -0400 | [diff] [blame] | 102 | if c.config.Bugs.AlertBeforeFalseStartTest != 0 { |
| 103 | c.sendAlert(c.config.Bugs.AlertBeforeFalseStartTest) |
| 104 | } |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 105 | if c.config.Bugs.ExpectFalseStart { |
| 106 | if err := c.readRecord(recordTypeApplicationData); err != nil { |
David Benjamin | 1c63315 | 2015-04-02 20:19:11 -0400 | [diff] [blame] | 107 | return fmt.Errorf("tls: peer did not false start: %s", err) |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 108 | } |
| 109 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | if err := hs.sendSessionTicket(); err != nil { |
| 111 | return err |
| 112 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 113 | if err := hs.sendFinished(nil); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | return err |
| 115 | } |
| 116 | } |
| 117 | c.handshakeComplete = true |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 118 | copy(c.clientRandom[:], hs.clientHello.random) |
| 119 | copy(c.serverRandom[:], hs.hello.random) |
| 120 | copy(c.masterSecret[:], hs.masterSecret) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
| 122 | return nil |
| 123 | } |
| 124 | |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 125 | // readClientHello reads a ClientHello message from the client and determines |
| 126 | // the protocol version. |
| 127 | func (hs *serverHandshakeState) readClientHello() error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | config := hs.c.config |
| 129 | c := hs.c |
| 130 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 131 | if err := c.simulatePacketLoss(nil); err != nil { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 132 | return err |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 133 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | msg, err := c.readHandshake() |
| 135 | if err != nil { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 136 | return err |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 137 | } |
| 138 | var ok bool |
| 139 | hs.clientHello, ok = msg.(*clientHelloMsg) |
| 140 | if !ok { |
| 141 | c.sendAlert(alertUnexpectedMessage) |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 142 | return unexpectedMessageError(hs.clientHello, msg) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 144 | if size := config.Bugs.RequireClientHelloSize; size != 0 && len(hs.clientHello.raw) != size { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 145 | 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] | 146 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 147 | |
| 148 | if c.isDTLS && !config.Bugs.SkipHelloVerifyRequest { |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 149 | // Per RFC 6347, the version field in HelloVerifyRequest SHOULD |
| 150 | // be always DTLS 1.0 |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 151 | helloVerifyRequest := &helloVerifyRequestMsg{ |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 152 | vers: VersionTLS10, |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 153 | cookie: make([]byte, 32), |
| 154 | } |
| 155 | if _, err := io.ReadFull(c.config.rand(), helloVerifyRequest.cookie); err != nil { |
| 156 | c.sendAlert(alertInternalError) |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 157 | return errors.New("dtls: short read from Rand: " + err.Error()) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 158 | } |
| 159 | c.writeRecord(recordTypeHandshake, helloVerifyRequest.marshal()) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 160 | c.flushHandshake() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 161 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 162 | if err := c.simulatePacketLoss(nil); err != nil { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 163 | return err |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 164 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 165 | msg, err := c.readHandshake() |
| 166 | if err != nil { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 167 | return err |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 168 | } |
| 169 | newClientHello, ok := msg.(*clientHelloMsg) |
| 170 | if !ok { |
| 171 | c.sendAlert(alertUnexpectedMessage) |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 172 | return unexpectedMessageError(hs.clientHello, msg) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 173 | } |
| 174 | if !bytes.Equal(newClientHello.cookie, helloVerifyRequest.cookie) { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 175 | return errors.New("dtls: invalid cookie") |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 176 | } |
David Benjamin | f2fedef | 2014-08-16 01:37:34 -0400 | [diff] [blame] | 177 | |
| 178 | // Apart from the cookie, the two ClientHellos must |
| 179 | // match. Note that clientHello.equal compares the |
| 180 | // serialization, so we make a copy. |
| 181 | oldClientHelloCopy := *hs.clientHello |
| 182 | oldClientHelloCopy.raw = nil |
| 183 | oldClientHelloCopy.cookie = nil |
| 184 | newClientHelloCopy := *newClientHello |
| 185 | newClientHelloCopy.raw = nil |
| 186 | newClientHelloCopy.cookie = nil |
| 187 | if !oldClientHelloCopy.equal(&newClientHelloCopy) { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 188 | return errors.New("dtls: retransmitted ClientHello does not match") |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 189 | } |
| 190 | hs.clientHello = newClientHello |
| 191 | } |
| 192 | |
David Benjamin | c44b1df | 2014-11-23 12:11:01 -0500 | [diff] [blame] | 193 | if config.Bugs.RequireSameRenegoClientVersion && c.clientVersion != 0 { |
| 194 | if c.clientVersion != hs.clientHello.vers { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 195 | return fmt.Errorf("tls: client offered different version on renego") |
David Benjamin | c44b1df | 2014-11-23 12:11:01 -0500 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | c.clientVersion = hs.clientHello.vers |
| 199 | |
David Benjamin | 6ae7f07 | 2015-01-26 10:22:13 -0500 | [diff] [blame] | 200 | // Reject < 1.2 ClientHellos with signature_algorithms. |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 201 | if c.clientVersion < VersionTLS12 && len(hs.clientHello.signatureAlgorithms) > 0 { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 202 | return fmt.Errorf("tls: client included signature_algorithms before TLS 1.2") |
David Benjamin | 72dc783 | 2015-03-16 17:49:43 -0400 | [diff] [blame] | 203 | } |
David Benjamin | 6ae7f07 | 2015-01-26 10:22:13 -0500 | [diff] [blame] | 204 | |
David Benjamin | f93995b | 2015-11-05 18:23:20 -0500 | [diff] [blame] | 205 | // Check the client cipher list is consistent with the version. |
| 206 | if hs.clientHello.vers < VersionTLS12 { |
| 207 | for _, id := range hs.clientHello.cipherSuites { |
| 208 | if isTLS12Cipher(id) { |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 209 | 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] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
David Benjamin | cecee27 | 2016-06-30 13:33:47 -0400 | [diff] [blame] | 214 | c.vers, ok = config.mutualVersion(hs.clientHello.vers, c.isDTLS) |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 215 | if !ok { |
| 216 | c.sendAlert(alertProtocolVersion) |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 217 | return fmt.Errorf("tls: client offered an unsupported, maximum protocol version of %x", hs.clientHello.vers) |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 218 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | c.haveVers = true |
| 220 | |
David Benjamin | f25dda9 | 2016-07-04 10:05:26 -0700 | [diff] [blame^] | 221 | var scsvFound bool |
| 222 | for _, cipherSuite := range hs.clientHello.cipherSuites { |
| 223 | if cipherSuite == fallbackSCSV { |
| 224 | scsvFound = true |
| 225 | break |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if !scsvFound && config.Bugs.FailIfNotFallbackSCSV { |
| 230 | return errors.New("tls: no fallback SCSV found when expected") |
| 231 | } else if scsvFound && !config.Bugs.FailIfNotFallbackSCSV { |
| 232 | return errors.New("tls: fallback SCSV found when not expected") |
| 233 | } |
| 234 | |
| 235 | if config.Bugs.IgnorePeerSignatureAlgorithmPreferences { |
| 236 | hs.clientHello.signatureAlgorithms = config.signatureAlgorithmsForServer() |
| 237 | } |
| 238 | if config.Bugs.IgnorePeerCurvePreferences { |
| 239 | hs.clientHello.supportedCurves = config.curvePreferences() |
| 240 | } |
| 241 | if config.Bugs.IgnorePeerCipherPreferences { |
| 242 | hs.clientHello.cipherSuites = config.cipherSuites() |
| 243 | } |
| 244 | |
| 245 | return nil |
| 246 | } |
| 247 | |
| 248 | // processClientHello processes the ClientHello message from the client and |
| 249 | // decides whether we will perform session resumption. |
| 250 | func (hs *serverHandshakeState) processClientHello() (isResume bool, err error) { |
| 251 | config := hs.c.config |
| 252 | c := hs.c |
| 253 | |
| 254 | hs.hello = &serverHelloMsg{ |
| 255 | isDTLS: c.isDTLS, |
| 256 | vers: c.vers, |
| 257 | compressionMethod: compressionNone, |
| 258 | } |
| 259 | |
| 260 | hs.hello.random = make([]byte, 32) |
| 261 | _, err = io.ReadFull(config.rand(), hs.hello.random) |
| 262 | if err != nil { |
| 263 | c.sendAlert(alertInternalError) |
| 264 | return false, err |
| 265 | } |
| 266 | |
| 267 | foundCompression := false |
| 268 | // We only support null compression, so check that the client offered it. |
| 269 | for _, compression := range hs.clientHello.compressionMethods { |
| 270 | if compression == compressionNone { |
| 271 | foundCompression = true |
| 272 | break |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if !foundCompression { |
| 277 | c.sendAlert(alertHandshakeFailure) |
| 278 | return false, errors.New("tls: client does not support uncompressed connections") |
| 279 | } |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 280 | |
| 281 | if err := hs.processClientExtensions(&hs.hello.extensions); err != nil { |
| 282 | return false, err |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 283 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | |
| 285 | supportedCurve := false |
| 286 | preferredCurves := config.curvePreferences() |
| 287 | Curves: |
| 288 | for _, curve := range hs.clientHello.supportedCurves { |
| 289 | for _, supported := range preferredCurves { |
| 290 | if supported == curve { |
| 291 | supportedCurve = true |
| 292 | break Curves |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | supportedPointFormat := false |
| 298 | for _, pointFormat := range hs.clientHello.supportedPoints { |
| 299 | if pointFormat == pointFormatUncompressed { |
| 300 | supportedPointFormat = true |
| 301 | break |
| 302 | } |
| 303 | } |
| 304 | hs.ellipticOk = supportedCurve && supportedPointFormat |
| 305 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 306 | _, hs.ecdsaOk = hs.cert.PrivateKey.(*ecdsa.PrivateKey) |
| 307 | |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 308 | // For test purposes, check that the peer never offers a session when |
| 309 | // renegotiating. |
| 310 | if c.cipherSuite != nil && len(hs.clientHello.sessionId) > 0 && c.config.Bugs.FailIfResumeOnRenego { |
| 311 | return false, errors.New("tls: offered resumption on renegotiation") |
| 312 | } |
| 313 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 314 | if c.config.Bugs.FailIfSessionOffered && (len(hs.clientHello.sessionTicket) > 0 || len(hs.clientHello.sessionId) > 0) { |
| 315 | return false, errors.New("tls: client offered a session ticket or ID") |
| 316 | } |
| 317 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | if hs.checkForResumption() { |
| 319 | return true, nil |
| 320 | } |
| 321 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | var preferenceList, supportedList []uint16 |
| 323 | if c.config.PreferServerCipherSuites { |
| 324 | preferenceList = c.config.cipherSuites() |
| 325 | supportedList = hs.clientHello.cipherSuites |
| 326 | } else { |
| 327 | preferenceList = hs.clientHello.cipherSuites |
| 328 | supportedList = c.config.cipherSuites() |
| 329 | } |
| 330 | |
| 331 | for _, id := range preferenceList { |
| 332 | if hs.suite = c.tryCipherSuite(id, supportedList, c.vers, hs.ellipticOk, hs.ecdsaOk); hs.suite != nil { |
| 333 | break |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if hs.suite == nil { |
| 338 | c.sendAlert(alertHandshakeFailure) |
| 339 | return false, errors.New("tls: no cipher suite supported by both client and server") |
| 340 | } |
| 341 | |
| 342 | return false, nil |
| 343 | } |
| 344 | |
David Benjamin | 7d79f83 | 2016-07-04 09:20:45 -0700 | [diff] [blame] | 345 | // processClientExtensions processes all ClientHello extensions not directly |
| 346 | // related to cipher suite negotiation and writes responses in serverExtensions. |
| 347 | func (hs *serverHandshakeState) processClientExtensions(serverExtensions *serverExtensions) error { |
| 348 | config := hs.c.config |
| 349 | c := hs.c |
| 350 | |
| 351 | if !bytes.Equal(c.clientVerify, hs.clientHello.secureRenegotiation) { |
| 352 | c.sendAlert(alertHandshakeFailure) |
| 353 | return errors.New("tls: renegotiation mismatch") |
| 354 | } |
| 355 | |
| 356 | if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo { |
| 357 | serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.clientVerify...) |
| 358 | serverExtensions.secureRenegotiation = append(serverExtensions.secureRenegotiation, c.serverVerify...) |
| 359 | if c.config.Bugs.BadRenegotiationInfo { |
| 360 | serverExtensions.secureRenegotiation[0] ^= 0x80 |
| 361 | } |
| 362 | } else { |
| 363 | serverExtensions.secureRenegotiation = hs.clientHello.secureRenegotiation |
| 364 | } |
| 365 | |
| 366 | if c.noRenegotiationInfo() { |
| 367 | serverExtensions.secureRenegotiation = nil |
| 368 | } |
| 369 | |
| 370 | serverExtensions.duplicateExtension = c.config.Bugs.DuplicateExtension |
| 371 | |
| 372 | if len(hs.clientHello.serverName) > 0 { |
| 373 | c.serverName = hs.clientHello.serverName |
| 374 | } |
| 375 | if len(config.Certificates) == 0 { |
| 376 | c.sendAlert(alertInternalError) |
| 377 | return errors.New("tls: no certificates configured") |
| 378 | } |
| 379 | hs.cert = &config.Certificates[0] |
| 380 | if len(hs.clientHello.serverName) > 0 { |
| 381 | hs.cert = config.getCertificateForName(hs.clientHello.serverName) |
| 382 | } |
| 383 | if expected := c.config.Bugs.ExpectServerName; expected != "" && expected != hs.clientHello.serverName { |
| 384 | return errors.New("tls: unexpected server name") |
| 385 | } |
| 386 | |
| 387 | if len(hs.clientHello.alpnProtocols) > 0 { |
| 388 | if proto := c.config.Bugs.ALPNProtocol; proto != nil { |
| 389 | serverExtensions.alpnProtocol = *proto |
| 390 | serverExtensions.alpnProtocolEmpty = len(*proto) == 0 |
| 391 | c.clientProtocol = *proto |
| 392 | c.usedALPN = true |
| 393 | } else if selectedProto, fallback := mutualProtocol(hs.clientHello.alpnProtocols, c.config.NextProtos); !fallback { |
| 394 | serverExtensions.alpnProtocol = selectedProto |
| 395 | c.clientProtocol = selectedProto |
| 396 | c.usedALPN = true |
| 397 | } |
| 398 | } |
| 399 | if len(hs.clientHello.alpnProtocols) == 0 || c.config.Bugs.NegotiateALPNAndNPN { |
| 400 | // Although sending an empty NPN extension is reasonable, Firefox has |
| 401 | // had a bug around this. Best to send nothing at all if |
| 402 | // config.NextProtos is empty. See |
| 403 | // https://code.google.com/p/go/issues/detail?id=5445. |
| 404 | if hs.clientHello.nextProtoNeg && len(config.NextProtos) > 0 { |
| 405 | serverExtensions.nextProtoNeg = true |
| 406 | serverExtensions.nextProtos = config.NextProtos |
| 407 | serverExtensions.npnLast = config.Bugs.SwapNPNAndALPN |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | serverExtensions.extendedMasterSecret = c.vers >= VersionTLS10 && hs.clientHello.extendedMasterSecret && !c.config.Bugs.NoExtendedMasterSecret |
| 412 | |
| 413 | if hs.clientHello.channelIDSupported && config.RequestChannelID { |
| 414 | serverExtensions.channelIDRequested = true |
| 415 | } |
| 416 | |
| 417 | if hs.clientHello.srtpProtectionProfiles != nil { |
| 418 | SRTPLoop: |
| 419 | for _, p1 := range c.config.SRTPProtectionProfiles { |
| 420 | for _, p2 := range hs.clientHello.srtpProtectionProfiles { |
| 421 | if p1 == p2 { |
| 422 | serverExtensions.srtpProtectionProfile = p1 |
| 423 | c.srtpProtectionProfile = p1 |
| 424 | break SRTPLoop |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | if c.config.Bugs.SendSRTPProtectionProfile != 0 { |
| 431 | serverExtensions.srtpProtectionProfile = c.config.Bugs.SendSRTPProtectionProfile |
| 432 | } |
| 433 | |
| 434 | if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil { |
| 435 | if hs.clientHello.customExtension != *expected { |
| 436 | return fmt.Errorf("tls: bad custom extension contents %q", hs.clientHello.customExtension) |
| 437 | } |
| 438 | } |
| 439 | serverExtensions.customExtension = config.Bugs.CustomExtension |
| 440 | |
| 441 | return nil |
| 442 | } |
| 443 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | // checkForResumption returns true if we should perform resumption on this connection. |
| 445 | func (hs *serverHandshakeState) checkForResumption() bool { |
| 446 | c := hs.c |
| 447 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 448 | if len(hs.clientHello.sessionTicket) > 0 { |
| 449 | if c.config.SessionTicketsDisabled { |
| 450 | return false |
| 451 | } |
David Benjamin | b0c8db7 | 2014-09-24 15:19:56 -0400 | [diff] [blame] | 452 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 453 | var ok bool |
| 454 | if hs.sessionState, ok = c.decryptTicket(hs.clientHello.sessionTicket); !ok { |
| 455 | return false |
| 456 | } |
| 457 | } else { |
| 458 | if c.config.ServerSessionCache == nil { |
| 459 | return false |
| 460 | } |
| 461 | |
| 462 | var ok bool |
| 463 | sessionId := string(hs.clientHello.sessionId) |
| 464 | if hs.sessionState, ok = c.config.ServerSessionCache.Get(sessionId); !ok { |
| 465 | return false |
| 466 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 467 | } |
| 468 | |
David Benjamin | e18d821 | 2014-11-10 02:37:15 -0500 | [diff] [blame] | 469 | // Never resume a session for a different SSL version. |
| 470 | if !c.config.Bugs.AllowSessionVersionMismatch && c.vers != hs.sessionState.vers { |
| 471 | return false |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | cipherSuiteOk := false |
| 475 | // Check that the client is still offering the ciphersuite in the session. |
| 476 | for _, id := range hs.clientHello.cipherSuites { |
| 477 | if id == hs.sessionState.cipherSuite { |
| 478 | cipherSuiteOk = true |
| 479 | break |
| 480 | } |
| 481 | } |
| 482 | if !cipherSuiteOk { |
| 483 | return false |
| 484 | } |
| 485 | |
| 486 | // Check that we also support the ciphersuite from the session. |
| 487 | hs.suite = c.tryCipherSuite(hs.sessionState.cipherSuite, c.config.cipherSuites(), hs.sessionState.vers, hs.ellipticOk, hs.ecdsaOk) |
| 488 | if hs.suite == nil { |
| 489 | return false |
| 490 | } |
| 491 | |
| 492 | sessionHasClientCerts := len(hs.sessionState.certificates) != 0 |
| 493 | needClientCerts := c.config.ClientAuth == RequireAnyClientCert || c.config.ClientAuth == RequireAndVerifyClientCert |
| 494 | if needClientCerts && !sessionHasClientCerts { |
| 495 | return false |
| 496 | } |
| 497 | if sessionHasClientCerts && c.config.ClientAuth == NoClientCert { |
| 498 | return false |
| 499 | } |
| 500 | |
| 501 | return true |
| 502 | } |
| 503 | |
| 504 | func (hs *serverHandshakeState) doResumeHandshake() error { |
| 505 | c := hs.c |
| 506 | |
| 507 | hs.hello.cipherSuite = hs.suite.id |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 508 | if c.config.Bugs.SendCipherSuite != 0 { |
| 509 | hs.hello.cipherSuite = c.config.Bugs.SendCipherSuite |
| 510 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | // We echo the client's session ID in the ServerHello to let it know |
| 512 | // that we're doing a resumption. |
| 513 | hs.hello.sessionId = hs.clientHello.sessionId |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 514 | hs.hello.extensions.ticketSupported = c.config.Bugs.RenewTicketOnResume |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 515 | |
David Benjamin | 80d1b35 | 2016-05-04 19:19:06 -0400 | [diff] [blame] | 516 | if c.config.Bugs.SendSCTListOnResume != nil { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 517 | hs.hello.extensions.sctList = c.config.Bugs.SendSCTListOnResume |
David Benjamin | 80d1b35 | 2016-05-04 19:19:06 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 520 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 521 | hs.finishedHash.discardHandshakeBuffer() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 522 | hs.writeClientHash(hs.clientHello.marshal()) |
| 523 | hs.writeServerHash(hs.hello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | |
| 525 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 526 | |
| 527 | if len(hs.sessionState.certificates) > 0 { |
| 528 | if _, err := hs.processCertsFromClient(hs.sessionState.certificates); err != nil { |
| 529 | return err |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | hs.masterSecret = hs.sessionState.masterSecret |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 534 | c.extendedMasterSecret = hs.sessionState.extendedMasterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | |
| 536 | return nil |
| 537 | } |
| 538 | |
| 539 | func (hs *serverHandshakeState) doFullHandshake() error { |
| 540 | config := hs.c.config |
| 541 | c := hs.c |
| 542 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 543 | isPSK := hs.suite.flags&suitePSK != 0 |
| 544 | if !isPSK && hs.clientHello.ocspStapling && len(hs.cert.OCSPStaple) > 0 { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 545 | hs.hello.extensions.ocspStapling = true |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 546 | } |
| 547 | |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 548 | if hs.clientHello.sctListSupported && len(hs.cert.SignedCertificateTimestampList) > 0 { |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 549 | hs.hello.extensions.sctList = hs.cert.SignedCertificateTimestampList |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 550 | } |
| 551 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 552 | hs.hello.extensions.ticketSupported = hs.clientHello.ticketSupported && !config.SessionTicketsDisabled && c.vers > VersionSSL30 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 553 | hs.hello.cipherSuite = hs.suite.id |
David Benjamin | 6095de8 | 2014-12-27 01:50:38 -0500 | [diff] [blame] | 554 | if config.Bugs.SendCipherSuite != 0 { |
| 555 | hs.hello.cipherSuite = config.Bugs.SendCipherSuite |
| 556 | } |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 557 | c.extendedMasterSecret = hs.hello.extensions.extendedMasterSecret |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 559 | // Generate a session ID if we're to save the session. |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 560 | if !hs.hello.extensions.ticketSupported && config.ServerSessionCache != nil { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 561 | hs.hello.sessionId = make([]byte, 32) |
| 562 | if _, err := io.ReadFull(config.rand(), hs.hello.sessionId); err != nil { |
| 563 | c.sendAlert(alertInternalError) |
| 564 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 565 | } |
| 566 | } |
| 567 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | hs.finishedHash = newFinishedHash(c.vers, hs.suite) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 569 | hs.writeClientHash(hs.clientHello.marshal()) |
| 570 | hs.writeServerHash(hs.hello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 571 | |
| 572 | c.writeRecord(recordTypeHandshake, hs.hello.marshal()) |
| 573 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 574 | if !isPSK { |
| 575 | certMsg := new(certificateMsg) |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 576 | if !config.Bugs.EmptyCertificateList { |
| 577 | certMsg.certificates = hs.cert.Certificate |
| 578 | } |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 579 | if !config.Bugs.UnauthenticatedECDH { |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 580 | certMsgBytes := certMsg.marshal() |
| 581 | if config.Bugs.WrongCertificateMessageType { |
| 582 | certMsgBytes[0] += 42 |
| 583 | } |
| 584 | hs.writeServerHash(certMsgBytes) |
| 585 | c.writeRecord(recordTypeHandshake, certMsgBytes) |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 586 | } |
David Benjamin | 1c375dd | 2014-07-12 00:48:23 -0400 | [diff] [blame] | 587 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 588 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 589 | if hs.hello.extensions.ocspStapling && !c.config.Bugs.SkipCertificateStatus { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 590 | certStatus := new(certificateStatusMsg) |
| 591 | certStatus.statusType = statusTypeOCSP |
| 592 | certStatus.response = hs.cert.OCSPStaple |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 593 | hs.writeServerHash(certStatus.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 594 | c.writeRecord(recordTypeHandshake, certStatus.marshal()) |
| 595 | } |
| 596 | |
| 597 | keyAgreement := hs.suite.ka(c.vers) |
| 598 | skx, err := keyAgreement.generateServerKeyExchange(config, hs.cert, hs.clientHello, hs.hello) |
| 599 | if err != nil { |
| 600 | c.sendAlert(alertHandshakeFailure) |
| 601 | return err |
| 602 | } |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 603 | if skx != nil && !config.Bugs.SkipServerKeyExchange { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 604 | hs.writeServerHash(skx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 605 | c.writeRecord(recordTypeHandshake, skx.marshal()) |
| 606 | } |
| 607 | |
| 608 | if config.ClientAuth >= RequestClientCert { |
| 609 | // Request a client certificate |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 610 | certReq := &certificateRequestMsg{ |
| 611 | certificateTypes: config.ClientCertificateTypes, |
| 612 | } |
| 613 | if certReq.certificateTypes == nil { |
| 614 | certReq.certificateTypes = []byte{ |
| 615 | byte(CertTypeRSASign), |
| 616 | byte(CertTypeECDSASign), |
| 617 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 618 | } |
| 619 | if c.vers >= VersionTLS12 { |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 620 | certReq.hasSignatureAlgorithm = true |
| 621 | if !config.Bugs.NoSignatureAlgorithms { |
| 622 | certReq.signatureAlgorithms = config.signatureAlgorithmsForServer() |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 623 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | // An empty list of certificateAuthorities signals to |
| 627 | // the client that it may send any certificate in response |
| 628 | // to our request. When we know the CAs we trust, then |
| 629 | // we can send them down, so that the client can choose |
| 630 | // an appropriate certificate to give to us. |
| 631 | if config.ClientCAs != nil { |
| 632 | certReq.certificateAuthorities = config.ClientCAs.Subjects() |
| 633 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 634 | hs.writeServerHash(certReq.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | c.writeRecord(recordTypeHandshake, certReq.marshal()) |
| 636 | } |
| 637 | |
| 638 | helloDone := new(serverHelloDoneMsg) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 639 | hs.writeServerHash(helloDone.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 640 | c.writeRecord(recordTypeHandshake, helloDone.marshal()) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 641 | c.flushHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 642 | |
| 643 | var pub crypto.PublicKey // public key for client auth, if any |
| 644 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 645 | if err := c.simulatePacketLoss(nil); err != nil { |
| 646 | return err |
| 647 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 648 | msg, err := c.readHandshake() |
| 649 | if err != nil { |
| 650 | return err |
| 651 | } |
| 652 | |
| 653 | var ok bool |
| 654 | // If we requested a client certificate, then the client must send a |
| 655 | // certificate message, even if it's empty. |
| 656 | if config.ClientAuth >= RequestClientCert { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 657 | var certMsg *certificateMsg |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 658 | var certificates [][]byte |
| 659 | if certMsg, ok = msg.(*certificateMsg); ok { |
| 660 | if c.vers == VersionSSL30 && len(certMsg.certificates) == 0 { |
| 661 | return errors.New("tls: empty certificate message in SSL 3.0") |
| 662 | } |
| 663 | |
| 664 | hs.writeClientHash(certMsg.marshal()) |
| 665 | certificates = certMsg.certificates |
| 666 | } else if c.vers != VersionSSL30 { |
| 667 | // In TLS, the Certificate message is required. In SSL |
| 668 | // 3.0, the peer skips it when sending no certificates. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 669 | c.sendAlert(alertUnexpectedMessage) |
| 670 | return unexpectedMessageError(certMsg, msg) |
| 671 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 672 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 673 | if len(certificates) == 0 { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 674 | // The client didn't actually send a certificate |
| 675 | switch config.ClientAuth { |
| 676 | case RequireAnyClientCert, RequireAndVerifyClientCert: |
| 677 | c.sendAlert(alertBadCertificate) |
| 678 | return errors.New("tls: client didn't provide a certificate") |
| 679 | } |
| 680 | } |
| 681 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 682 | pub, err = hs.processCertsFromClient(certificates) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 683 | if err != nil { |
| 684 | return err |
| 685 | } |
| 686 | |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 687 | if ok { |
| 688 | msg, err = c.readHandshake() |
| 689 | if err != nil { |
| 690 | return err |
| 691 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | // Get client key exchange |
| 696 | ckx, ok := msg.(*clientKeyExchangeMsg) |
| 697 | if !ok { |
| 698 | c.sendAlert(alertUnexpectedMessage) |
| 699 | return unexpectedMessageError(ckx, msg) |
| 700 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 701 | hs.writeClientHash(ckx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 702 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 703 | preMasterSecret, err := keyAgreement.processClientKeyExchange(config, hs.cert, ckx, c.vers) |
| 704 | if err != nil { |
| 705 | c.sendAlert(alertHandshakeFailure) |
| 706 | return err |
| 707 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 708 | if c.extendedMasterSecret { |
| 709 | hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 710 | } else { |
| 711 | if c.config.Bugs.RequireExtendedMasterSecret { |
| 712 | return errors.New("tls: extended master secret required but not supported by peer") |
| 713 | } |
| 714 | hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.clientHello.random, hs.hello.random) |
| 715 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 716 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 717 | // If we received a client cert in response to our certificate request message, |
| 718 | // the client will send us a certificateVerifyMsg immediately after the |
| 719 | // clientKeyExchangeMsg. This message is a digest of all preceding |
| 720 | // handshake-layer messages that is signed using the private key corresponding |
| 721 | // to the client's certificate. This allows us to verify that the client is in |
| 722 | // possession of the private key of the certificate. |
| 723 | if len(c.peerCertificates) > 0 { |
| 724 | msg, err = c.readHandshake() |
| 725 | if err != nil { |
| 726 | return err |
| 727 | } |
| 728 | certVerify, ok := msg.(*certificateVerifyMsg) |
| 729 | if !ok { |
| 730 | c.sendAlert(alertUnexpectedMessage) |
| 731 | return unexpectedMessageError(certVerify, msg) |
| 732 | } |
| 733 | |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 734 | // Determine the signature type. |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 735 | var sigAlg signatureAlgorithm |
| 736 | if certVerify.hasSignatureAlgorithm { |
| 737 | sigAlg = certVerify.signatureAlgorithm |
| 738 | if !isSupportedSignatureAlgorithm(sigAlg, config.signatureAlgorithmsForServer()) { |
| 739 | return errors.New("tls: unsupported signature algorithm for client certificate") |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 740 | } |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 741 | c.peerSignatureAlgorithm = sigAlg |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 742 | } |
| 743 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 744 | if c.vers > VersionSSL30 { |
| 745 | err = verifyMessage(c.vers, pub, sigAlg, hs.finishedHash.buffer, certVerify.signature) |
| 746 | } else { |
| 747 | // SSL 3.0's client certificate construction is |
| 748 | // incompatible with signatureAlgorithm. |
| 749 | rsaPub, ok := pub.(*rsa.PublicKey) |
| 750 | if !ok { |
| 751 | err = errors.New("unsupported key type for client certificate") |
| 752 | } else { |
| 753 | digest := hs.finishedHash.hashForClientCertificateSSL3(hs.masterSecret) |
| 754 | err = rsa.VerifyPKCS1v15(rsaPub, crypto.MD5SHA1, digest, certVerify.signature) |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 755 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 756 | } |
| 757 | if err != nil { |
| 758 | c.sendAlert(alertBadCertificate) |
| 759 | return errors.New("could not validate signature of connection nonces: " + err.Error()) |
| 760 | } |
| 761 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 762 | hs.writeClientHash(certVerify.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 763 | } |
| 764 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 765 | hs.finishedHash.discardHandshakeBuffer() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 766 | |
| 767 | return nil |
| 768 | } |
| 769 | |
| 770 | func (hs *serverHandshakeState) establishKeys() error { |
| 771 | c := hs.c |
| 772 | |
| 773 | clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 774 | 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] | 775 | |
| 776 | var clientCipher, serverCipher interface{} |
| 777 | var clientHash, serverHash macFunction |
| 778 | |
| 779 | if hs.suite.aead == nil { |
| 780 | clientCipher = hs.suite.cipher(clientKey, clientIV, true /* for reading */) |
| 781 | clientHash = hs.suite.mac(c.vers, clientMAC) |
| 782 | serverCipher = hs.suite.cipher(serverKey, serverIV, false /* not for reading */) |
| 783 | serverHash = hs.suite.mac(c.vers, serverMAC) |
| 784 | } else { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 785 | clientCipher = hs.suite.aead(c.vers, clientKey, clientIV) |
| 786 | serverCipher = hs.suite.aead(c.vers, serverKey, serverIV) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | c.in.prepareCipherSpec(c.vers, clientCipher, clientHash) |
| 790 | c.out.prepareCipherSpec(c.vers, serverCipher, serverHash) |
| 791 | |
| 792 | return nil |
| 793 | } |
| 794 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 795 | func (hs *serverHandshakeState) readFinished(out []byte, isResume bool) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 796 | c := hs.c |
| 797 | |
| 798 | c.readRecord(recordTypeChangeCipherSpec) |
| 799 | if err := c.in.error(); err != nil { |
| 800 | return err |
| 801 | } |
| 802 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 803 | if hs.hello.extensions.nextProtoNeg { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 804 | msg, err := c.readHandshake() |
| 805 | if err != nil { |
| 806 | return err |
| 807 | } |
| 808 | nextProto, ok := msg.(*nextProtoMsg) |
| 809 | if !ok { |
| 810 | c.sendAlert(alertUnexpectedMessage) |
| 811 | return unexpectedMessageError(nextProto, msg) |
| 812 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 813 | hs.writeClientHash(nextProto.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 814 | c.clientProtocol = nextProto.proto |
| 815 | } |
| 816 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 817 | if hs.hello.extensions.channelIDRequested { |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 818 | msg, err := c.readHandshake() |
| 819 | if err != nil { |
| 820 | return err |
| 821 | } |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 822 | channelIDMsg, ok := msg.(*channelIDMsg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 823 | if !ok { |
| 824 | c.sendAlert(alertUnexpectedMessage) |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 825 | return unexpectedMessageError(channelIDMsg, msg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 826 | } |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 827 | x := new(big.Int).SetBytes(channelIDMsg.channelID[0:32]) |
| 828 | y := new(big.Int).SetBytes(channelIDMsg.channelID[32:64]) |
| 829 | r := new(big.Int).SetBytes(channelIDMsg.channelID[64:96]) |
| 830 | s := new(big.Int).SetBytes(channelIDMsg.channelID[96:128]) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 831 | if !elliptic.P256().IsOnCurve(x, y) { |
| 832 | return errors.New("tls: invalid channel ID public key") |
| 833 | } |
| 834 | channelID := &ecdsa.PublicKey{elliptic.P256(), x, y} |
| 835 | var resumeHash []byte |
| 836 | if isResume { |
| 837 | resumeHash = hs.sessionState.handshakeHash |
| 838 | } |
| 839 | if !ecdsa.Verify(channelID, hs.finishedHash.hashForChannelID(resumeHash), r, s) { |
| 840 | return errors.New("tls: invalid channel ID signature") |
| 841 | } |
| 842 | c.channelID = channelID |
| 843 | |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 844 | hs.writeClientHash(channelIDMsg.marshal()) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 845 | } |
| 846 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 847 | msg, err := c.readHandshake() |
| 848 | if err != nil { |
| 849 | return err |
| 850 | } |
| 851 | clientFinished, ok := msg.(*finishedMsg) |
| 852 | if !ok { |
| 853 | c.sendAlert(alertUnexpectedMessage) |
| 854 | return unexpectedMessageError(clientFinished, msg) |
| 855 | } |
| 856 | |
| 857 | verify := hs.finishedHash.clientSum(hs.masterSecret) |
| 858 | if len(verify) != len(clientFinished.verifyData) || |
| 859 | subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 { |
| 860 | c.sendAlert(alertHandshakeFailure) |
| 861 | return errors.New("tls: client's Finished message is incorrect") |
| 862 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 863 | c.clientVerify = append(c.clientVerify[:0], clientFinished.verifyData...) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 864 | copy(out, clientFinished.verifyData) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 865 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 866 | hs.writeClientHash(clientFinished.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 867 | return nil |
| 868 | } |
| 869 | |
| 870 | func (hs *serverHandshakeState) sendSessionTicket() error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 871 | c := hs.c |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 872 | state := sessionState{ |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 873 | vers: c.vers, |
| 874 | cipherSuite: hs.suite.id, |
| 875 | masterSecret: hs.masterSecret, |
| 876 | certificates: hs.certsFromClient, |
| 877 | handshakeHash: hs.finishedHash.server.Sum(nil), |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 878 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 879 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 880 | if !hs.hello.extensions.ticketSupported || hs.c.config.Bugs.SkipNewSessionTicket { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 881 | if c.config.ServerSessionCache != nil && len(hs.hello.sessionId) != 0 { |
| 882 | c.config.ServerSessionCache.Put(string(hs.hello.sessionId), &state) |
| 883 | } |
| 884 | return nil |
| 885 | } |
| 886 | |
| 887 | m := new(newSessionTicketMsg) |
| 888 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 889 | if !c.config.Bugs.SendEmptySessionTicket { |
| 890 | var err error |
| 891 | m.ticket, err = c.encryptTicket(&state) |
| 892 | if err != nil { |
| 893 | return err |
| 894 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 895 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 896 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 897 | hs.writeServerHash(m.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 898 | c.writeRecord(recordTypeHandshake, m.marshal()) |
| 899 | |
| 900 | return nil |
| 901 | } |
| 902 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 903 | func (hs *serverHandshakeState) sendFinished(out []byte) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 904 | c := hs.c |
| 905 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 906 | finished := new(finishedMsg) |
| 907 | finished.verifyData = hs.finishedHash.serverSum(hs.masterSecret) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 908 | copy(out, finished.verifyData) |
David Benjamin | 513f0ea | 2015-04-02 19:33:31 -0400 | [diff] [blame] | 909 | if c.config.Bugs.BadFinished { |
| 910 | finished.verifyData[0]++ |
| 911 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 912 | c.serverVerify = append(c.serverVerify[:0], finished.verifyData...) |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 913 | hs.finishedBytes = finished.marshal() |
| 914 | hs.writeServerHash(hs.finishedBytes) |
| 915 | postCCSBytes := hs.finishedBytes |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 916 | |
| 917 | if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
| 918 | c.writeRecord(recordTypeHandshake, postCCSBytes[:5]) |
| 919 | postCCSBytes = postCCSBytes[5:] |
| 920 | } |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 921 | c.flushHandshake() |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 922 | |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 923 | if !c.config.Bugs.SkipChangeCipherSpec { |
David Benjamin | 8411b24 | 2015-11-26 12:07:28 -0500 | [diff] [blame] | 924 | ccs := []byte{1} |
| 925 | if c.config.Bugs.BadChangeCipherSpec != nil { |
| 926 | ccs = c.config.Bugs.BadChangeCipherSpec |
| 927 | } |
| 928 | c.writeRecord(recordTypeChangeCipherSpec, ccs) |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 929 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 930 | |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 931 | if c.config.Bugs.AppDataAfterChangeCipherSpec != nil { |
| 932 | c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec) |
| 933 | } |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 934 | if c.config.Bugs.AlertAfterChangeCipherSpec != 0 { |
| 935 | c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec) |
| 936 | return errors.New("tls: simulating post-CCS alert") |
| 937 | } |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 938 | |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 939 | if !c.config.Bugs.SkipFinished { |
| 940 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame] | 941 | c.flushHandshake() |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 942 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 943 | |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 944 | c.cipherSuite = hs.suite |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 945 | |
| 946 | return nil |
| 947 | } |
| 948 | |
| 949 | // processCertsFromClient takes a chain of client certificates either from a |
| 950 | // Certificates message or from a sessionState and verifies them. It returns |
| 951 | // the public key of the leaf certificate. |
| 952 | func (hs *serverHandshakeState) processCertsFromClient(certificates [][]byte) (crypto.PublicKey, error) { |
| 953 | c := hs.c |
| 954 | |
| 955 | hs.certsFromClient = certificates |
| 956 | certs := make([]*x509.Certificate, len(certificates)) |
| 957 | var err error |
| 958 | for i, asn1Data := range certificates { |
| 959 | if certs[i], err = x509.ParseCertificate(asn1Data); err != nil { |
| 960 | c.sendAlert(alertBadCertificate) |
| 961 | return nil, errors.New("tls: failed to parse client certificate: " + err.Error()) |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | if c.config.ClientAuth >= VerifyClientCertIfGiven && len(certs) > 0 { |
| 966 | opts := x509.VerifyOptions{ |
| 967 | Roots: c.config.ClientCAs, |
| 968 | CurrentTime: c.config.time(), |
| 969 | Intermediates: x509.NewCertPool(), |
| 970 | KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, |
| 971 | } |
| 972 | |
| 973 | for _, cert := range certs[1:] { |
| 974 | opts.Intermediates.AddCert(cert) |
| 975 | } |
| 976 | |
| 977 | chains, err := certs[0].Verify(opts) |
| 978 | if err != nil { |
| 979 | c.sendAlert(alertBadCertificate) |
| 980 | return nil, errors.New("tls: failed to verify client's certificate: " + err.Error()) |
| 981 | } |
| 982 | |
| 983 | ok := false |
| 984 | for _, ku := range certs[0].ExtKeyUsage { |
| 985 | if ku == x509.ExtKeyUsageClientAuth { |
| 986 | ok = true |
| 987 | break |
| 988 | } |
| 989 | } |
| 990 | if !ok { |
| 991 | c.sendAlert(alertHandshakeFailure) |
| 992 | return nil, errors.New("tls: client's certificate's extended key usage doesn't permit it to be used for client authentication") |
| 993 | } |
| 994 | |
| 995 | c.verifiedChains = chains |
| 996 | } |
| 997 | |
| 998 | if len(certs) > 0 { |
| 999 | var pub crypto.PublicKey |
| 1000 | switch key := certs[0].PublicKey.(type) { |
| 1001 | case *ecdsa.PublicKey, *rsa.PublicKey: |
| 1002 | pub = key |
| 1003 | default: |
| 1004 | c.sendAlert(alertUnsupportedCertificate) |
| 1005 | return nil, fmt.Errorf("tls: client's certificate contains an unsupported public key of type %T", certs[0].PublicKey) |
| 1006 | } |
| 1007 | c.peerCertificates = certs |
| 1008 | return pub, nil |
| 1009 | } |
| 1010 | |
| 1011 | return nil, nil |
| 1012 | } |
| 1013 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1014 | func (hs *serverHandshakeState) writeServerHash(msg []byte) { |
| 1015 | // writeServerHash is called before writeRecord. |
| 1016 | hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 1017 | } |
| 1018 | |
| 1019 | func (hs *serverHandshakeState) writeClientHash(msg []byte) { |
| 1020 | // writeClientHash is called after readHandshake. |
| 1021 | hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 1022 | } |
| 1023 | |
| 1024 | func (hs *serverHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 1025 | if hs.c.isDTLS { |
| 1026 | // This is somewhat hacky. DTLS hashes a slightly different format. |
| 1027 | // First, the TLS header. |
| 1028 | hs.finishedHash.Write(msg[:4]) |
| 1029 | // Then the sequence number and reassembled fragment offset (always 0). |
| 1030 | hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 1031 | // Then the reassembled fragment (always equal to the message length). |
| 1032 | hs.finishedHash.Write(msg[1:4]) |
| 1033 | // And then the message body. |
| 1034 | hs.finishedHash.Write(msg[4:]) |
| 1035 | } else { |
| 1036 | hs.finishedHash.Write(msg) |
| 1037 | } |
| 1038 | } |
| 1039 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1040 | // tryCipherSuite returns a cipherSuite with the given id if that cipher suite |
| 1041 | // is acceptable to use. |
| 1042 | func (c *Conn) tryCipherSuite(id uint16, supportedCipherSuites []uint16, version uint16, ellipticOk, ecdsaOk bool) *cipherSuite { |
| 1043 | for _, supported := range supportedCipherSuites { |
| 1044 | if id == supported { |
| 1045 | var candidate *cipherSuite |
| 1046 | |
| 1047 | for _, s := range cipherSuites { |
| 1048 | if s.id == id { |
| 1049 | candidate = s |
| 1050 | break |
| 1051 | } |
| 1052 | } |
| 1053 | if candidate == nil { |
| 1054 | continue |
| 1055 | } |
| 1056 | // Don't select a ciphersuite which we can't |
| 1057 | // support for this client. |
David Benjamin | 0407e76 | 2016-06-17 16:41:18 -0400 | [diff] [blame] | 1058 | if !c.config.Bugs.EnableAllCiphers { |
| 1059 | if (candidate.flags&suiteECDHE != 0) && !ellipticOk { |
| 1060 | continue |
| 1061 | } |
| 1062 | if (candidate.flags&suiteECDSA != 0) != ecdsaOk { |
| 1063 | continue |
| 1064 | } |
| 1065 | if version < VersionTLS12 && candidate.flags&suiteTLS12 != 0 { |
| 1066 | continue |
| 1067 | } |
| 1068 | if c.isDTLS && candidate.flags&suiteNoDTLS != 0 { |
| 1069 | continue |
| 1070 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 1071 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1072 | return candidate |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | return nil |
| 1077 | } |
David Benjamin | f93995b | 2015-11-05 18:23:20 -0500 | [diff] [blame] | 1078 | |
| 1079 | func isTLS12Cipher(id uint16) bool { |
| 1080 | for _, cipher := range cipherSuites { |
| 1081 | if cipher.id != id { |
| 1082 | continue |
| 1083 | } |
| 1084 | return cipher.flags&suiteTLS12 != 0 |
| 1085 | } |
| 1086 | // Unknown cipher. |
| 1087 | return false |
| 1088 | } |