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