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