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 ( |
| 8 | "bytes" |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 9 | "crypto" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 18 | "math/big" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 19 | "net" |
| 20 | "strconv" |
| 21 | ) |
| 22 | |
| 23 | type clientHandshakeState struct { |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 24 | c *Conn |
| 25 | serverHello *serverHelloMsg |
| 26 | hello *clientHelloMsg |
| 27 | suite *cipherSuite |
| 28 | finishedHash finishedHash |
| 29 | masterSecret []byte |
| 30 | session *ClientSessionState |
| 31 | finishedBytes []byte |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | func (c *Conn) clientHandshake() error { |
| 35 | if c.config == nil { |
| 36 | c.config = defaultConfig() |
| 37 | } |
| 38 | |
| 39 | if len(c.config.ServerName) == 0 && !c.config.InsecureSkipVerify { |
| 40 | return errors.New("tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config") |
| 41 | } |
| 42 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 43 | c.sendHandshakeSeq = 0 |
| 44 | c.recvHandshakeSeq = 0 |
| 45 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 46 | nextProtosLength := 0 |
| 47 | for _, proto := range c.config.NextProtos { |
Adam Langley | efb0e16 | 2015-07-09 11:35:04 -0700 | [diff] [blame] | 48 | if l := len(proto); l > 255 { |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 49 | return errors.New("tls: invalid NextProtos value") |
| 50 | } else { |
| 51 | nextProtosLength += 1 + l |
| 52 | } |
| 53 | } |
| 54 | if nextProtosLength > 0xffff { |
| 55 | return errors.New("tls: NextProtos values too large") |
| 56 | } |
| 57 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | hello := &clientHelloMsg{ |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 59 | isDTLS: c.isDTLS, |
David Benjamin | cecee27 | 2016-06-30 13:33:47 -0400 | [diff] [blame] | 60 | vers: c.config.maxVersion(c.isDTLS), |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 61 | compressionMethods: []uint8{compressionNone}, |
| 62 | random: make([]byte, 32), |
| 63 | ocspStapling: true, |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 64 | sctListSupported: true, |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 65 | serverName: c.config.ServerName, |
| 66 | supportedCurves: c.config.curvePreferences(), |
| 67 | supportedPoints: []uint8{pointFormatUncompressed}, |
| 68 | nextProtoNeg: len(c.config.NextProtos) > 0, |
| 69 | secureRenegotiation: []byte{}, |
| 70 | alpnProtocols: c.config.NextProtos, |
| 71 | duplicateExtension: c.config.Bugs.DuplicateExtension, |
| 72 | channelIDSupported: c.config.ChannelID != nil, |
| 73 | npnLast: c.config.Bugs.SwapNPNAndALPN, |
David Benjamin | cecee27 | 2016-06-30 13:33:47 -0400 | [diff] [blame] | 74 | extendedMasterSecret: c.config.maxVersion(c.isDTLS) >= VersionTLS10, |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 75 | srtpProtectionProfiles: c.config.SRTPProtectionProfiles, |
| 76 | srtpMasterKeyIdentifier: c.config.Bugs.SRTPMasterKeyIdentifer, |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 77 | customExtension: c.config.Bugs.CustomExtension, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 80 | if c.config.Bugs.SendClientVersion != 0 { |
| 81 | hello.vers = c.config.Bugs.SendClientVersion |
| 82 | } |
| 83 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 84 | if c.config.Bugs.NoExtendedMasterSecret { |
| 85 | hello.extendedMasterSecret = false |
| 86 | } |
| 87 | |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 88 | if c.config.Bugs.NoSupportedCurves { |
| 89 | hello.supportedCurves = nil |
| 90 | } |
| 91 | |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 92 | if len(c.clientVerify) > 0 && !c.config.Bugs.EmptyRenegotiationInfo { |
| 93 | if c.config.Bugs.BadRenegotiationInfo { |
| 94 | hello.secureRenegotiation = append(hello.secureRenegotiation, c.clientVerify...) |
| 95 | hello.secureRenegotiation[0] ^= 0x80 |
| 96 | } else { |
| 97 | hello.secureRenegotiation = c.clientVerify |
| 98 | } |
| 99 | } |
| 100 | |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 101 | if c.noRenegotiationInfo() { |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 102 | hello.secureRenegotiation = nil |
| 103 | } |
| 104 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 105 | possibleCipherSuites := c.config.cipherSuites() |
| 106 | hello.cipherSuites = make([]uint16, 0, len(possibleCipherSuites)) |
| 107 | |
| 108 | NextCipherSuite: |
| 109 | for _, suiteId := range possibleCipherSuites { |
| 110 | for _, suite := range cipherSuites { |
| 111 | if suite.id != suiteId { |
| 112 | continue |
| 113 | } |
David Benjamin | 0407e76 | 2016-06-17 16:41:18 -0400 | [diff] [blame] | 114 | if !c.config.Bugs.EnableAllCiphers { |
| 115 | // Don't advertise TLS 1.2-only cipher suites unless |
| 116 | // we're attempting TLS 1.2. |
| 117 | if hello.vers < VersionTLS12 && suite.flags&suiteTLS12 != 0 { |
| 118 | continue |
| 119 | } |
| 120 | // Don't advertise non-DTLS cipher suites in DTLS. |
| 121 | if c.isDTLS && suite.flags&suiteNoDTLS != 0 { |
| 122 | continue |
| 123 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 124 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | hello.cipherSuites = append(hello.cipherSuites, suiteId) |
| 126 | continue NextCipherSuite |
| 127 | } |
| 128 | } |
| 129 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 130 | if c.config.Bugs.SendRenegotiationSCSV { |
| 131 | hello.cipherSuites = append(hello.cipherSuites, renegotiationSCSV) |
| 132 | } |
| 133 | |
David Benjamin | bef270a | 2014-08-02 04:22:02 -0400 | [diff] [blame] | 134 | if c.config.Bugs.SendFallbackSCSV { |
| 135 | hello.cipherSuites = append(hello.cipherSuites, fallbackSCSV) |
| 136 | } |
| 137 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 138 | _, err := io.ReadFull(c.config.rand(), hello.random) |
| 139 | if err != nil { |
| 140 | c.sendAlert(alertInternalError) |
| 141 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 142 | } |
| 143 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 144 | if hello.vers >= VersionTLS12 && !c.config.Bugs.NoSignatureAlgorithms { |
| 145 | hello.signatureAlgorithms = c.config.signatureAlgorithmsForClient() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | var session *ClientSessionState |
| 149 | var cacheKey string |
| 150 | sessionCache := c.config.ClientSessionCache |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | |
| 152 | if sessionCache != nil { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 153 | hello.ticketSupported = !c.config.SessionTicketsDisabled |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
| 155 | // Try to resume a previously negotiated TLS session, if |
| 156 | // available. |
| 157 | cacheKey = clientSessionCacheKey(c.conn.RemoteAddr(), c.config) |
| 158 | candidateSession, ok := sessionCache.Get(cacheKey) |
| 159 | if ok { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 160 | ticketOk := !c.config.SessionTicketsDisabled || candidateSession.sessionTicket == nil |
| 161 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | // Check that the ciphersuite/version used for the |
| 163 | // previous session are still valid. |
| 164 | cipherSuiteOk := false |
| 165 | for _, id := range hello.cipherSuites { |
| 166 | if id == candidateSession.cipherSuite { |
| 167 | cipherSuiteOk = true |
| 168 | break |
| 169 | } |
| 170 | } |
| 171 | |
David Benjamin | cecee27 | 2016-06-30 13:33:47 -0400 | [diff] [blame] | 172 | versOk := candidateSession.vers >= c.config.minVersion(c.isDTLS) && |
| 173 | candidateSession.vers <= c.config.maxVersion(c.isDTLS) |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 174 | if ticketOk && versOk && cipherSuiteOk { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | session = candidateSession |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if session != nil { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 181 | if session.sessionTicket != nil { |
| 182 | hello.sessionTicket = session.sessionTicket |
| 183 | if c.config.Bugs.CorruptTicket { |
| 184 | hello.sessionTicket = make([]byte, len(session.sessionTicket)) |
| 185 | copy(hello.sessionTicket, session.sessionTicket) |
| 186 | if len(hello.sessionTicket) > 0 { |
| 187 | offset := 40 |
| 188 | if offset > len(hello.sessionTicket) { |
| 189 | offset = len(hello.sessionTicket) - 1 |
| 190 | } |
| 191 | hello.sessionTicket[offset] ^= 0x40 |
Adam Langley | 3831173 | 2014-10-16 19:04:35 -0700 | [diff] [blame] | 192 | } |
Adam Langley | 3831173 | 2014-10-16 19:04:35 -0700 | [diff] [blame] | 193 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 194 | // A random session ID is used to detect when the |
| 195 | // server accepted the ticket and is resuming a session |
| 196 | // (see RFC 5077). |
| 197 | sessionIdLen := 16 |
| 198 | if c.config.Bugs.OversizedSessionId { |
| 199 | sessionIdLen = 33 |
| 200 | } |
| 201 | hello.sessionId = make([]byte, sessionIdLen) |
| 202 | if _, err := io.ReadFull(c.config.rand(), hello.sessionId); err != nil { |
| 203 | c.sendAlert(alertInternalError) |
| 204 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 205 | } |
| 206 | } else { |
| 207 | hello.sessionId = session.sessionId |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 211 | var helloBytes []byte |
| 212 | if c.config.Bugs.SendV2ClientHello { |
David Benjamin | 94d701b | 2014-11-30 13:54:41 -0500 | [diff] [blame] | 213 | // Test that the peer left-pads random. |
| 214 | hello.random[0] = 0 |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 215 | v2Hello := &v2ClientHelloMsg{ |
| 216 | vers: hello.vers, |
| 217 | cipherSuites: hello.cipherSuites, |
| 218 | // No session resumption for V2ClientHello. |
| 219 | sessionId: nil, |
David Benjamin | 94d701b | 2014-11-30 13:54:41 -0500 | [diff] [blame] | 220 | challenge: hello.random[1:], |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 221 | } |
| 222 | helloBytes = v2Hello.marshal() |
| 223 | c.writeV2Record(helloBytes) |
| 224 | } else { |
| 225 | helloBytes = hello.marshal() |
| 226 | c.writeRecord(recordTypeHandshake, helloBytes) |
| 227 | } |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame^] | 228 | c.flushHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 229 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 230 | if err := c.simulatePacketLoss(nil); err != nil { |
| 231 | return err |
| 232 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | msg, err := c.readHandshake() |
| 234 | if err != nil { |
| 235 | return err |
| 236 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 237 | |
| 238 | if c.isDTLS { |
| 239 | helloVerifyRequest, ok := msg.(*helloVerifyRequestMsg) |
| 240 | if ok { |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 241 | if helloVerifyRequest.vers != VersionTLS10 { |
| 242 | // Per RFC 6347, the version field in |
| 243 | // HelloVerifyRequest SHOULD be always DTLS |
| 244 | // 1.0. Enforce this for testing purposes. |
| 245 | return errors.New("dtls: bad HelloVerifyRequest version") |
| 246 | } |
| 247 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 248 | hello.raw = nil |
| 249 | hello.cookie = helloVerifyRequest.cookie |
| 250 | helloBytes = hello.marshal() |
| 251 | c.writeRecord(recordTypeHandshake, helloBytes) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame^] | 252 | c.flushHandshake() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 253 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 254 | if err := c.simulatePacketLoss(nil); err != nil { |
| 255 | return err |
| 256 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 257 | msg, err = c.readHandshake() |
| 258 | if err != nil { |
| 259 | return err |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 264 | serverHello, ok := msg.(*serverHelloMsg) |
| 265 | if !ok { |
| 266 | c.sendAlert(alertUnexpectedMessage) |
| 267 | return unexpectedMessageError(serverHello, msg) |
| 268 | } |
| 269 | |
David Benjamin | cecee27 | 2016-06-30 13:33:47 -0400 | [diff] [blame] | 270 | c.vers, ok = c.config.mutualVersion(serverHello.vers, c.isDTLS) |
David Benjamin | 76d8abe | 2014-08-14 16:25:34 -0400 | [diff] [blame] | 271 | if !ok { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | c.sendAlert(alertProtocolVersion) |
| 273 | return fmt.Errorf("tls: server selected unsupported protocol version %x", serverHello.vers) |
| 274 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 275 | c.haveVers = true |
| 276 | |
| 277 | suite := mutualCipherSuite(c.config.cipherSuites(), serverHello.cipherSuite) |
| 278 | if suite == nil { |
| 279 | c.sendAlert(alertHandshakeFailure) |
| 280 | return fmt.Errorf("tls: server selected an unsupported cipher suite") |
| 281 | } |
| 282 | |
| 283 | hs := &clientHandshakeState{ |
| 284 | c: c, |
| 285 | serverHello: serverHello, |
| 286 | hello: hello, |
| 287 | suite: suite, |
| 288 | finishedHash: newFinishedHash(c.vers, suite), |
| 289 | session: session, |
| 290 | } |
| 291 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 292 | hs.writeHash(helloBytes, hs.c.sendHandshakeSeq-1) |
| 293 | hs.writeServerHash(hs.serverHello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 295 | if c.config.Bugs.EarlyChangeCipherSpec > 0 { |
| 296 | hs.establishKeys() |
| 297 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 298 | } |
| 299 | |
David Benjamin | 7510140 | 2016-07-01 13:40:23 -0400 | [diff] [blame] | 300 | if hs.serverHello.compressionMethod != compressionNone { |
| 301 | c.sendAlert(alertUnexpectedMessage) |
| 302 | return errors.New("tls: server selected unsupported compression format") |
| 303 | } |
| 304 | |
| 305 | err = hs.processServerExtensions(&serverHello.extensions) |
| 306 | if err != nil { |
| 307 | return err |
| 308 | } |
| 309 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | isResume, err := hs.processServerHello() |
| 311 | if err != nil { |
| 312 | return err |
| 313 | } |
| 314 | |
| 315 | if isResume { |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 316 | if c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 317 | if err := hs.establishKeys(); err != nil { |
| 318 | return err |
| 319 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 320 | } |
| 321 | if err := hs.readSessionTicket(); err != nil { |
| 322 | return err |
| 323 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 324 | if err := hs.readFinished(c.firstFinished[:]); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | return err |
| 326 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 327 | if err := hs.sendFinished(nil, isResume); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 328 | return err |
| 329 | } |
| 330 | } else { |
| 331 | if err := hs.doFullHandshake(); err != nil { |
| 332 | return err |
| 333 | } |
| 334 | if err := hs.establishKeys(); err != nil { |
| 335 | return err |
| 336 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 337 | if err := hs.sendFinished(c.firstFinished[:], isResume); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 338 | return err |
| 339 | } |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 340 | // Most retransmits are triggered by a timeout, but the final |
| 341 | // leg of the handshake is retransmited upon re-receiving a |
| 342 | // Finished. |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 343 | if err := c.simulatePacketLoss(func() { |
| 344 | c.writeRecord(recordTypeHandshake, hs.finishedBytes) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame^] | 345 | c.flushHandshake() |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 346 | }); err != nil { |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 347 | return err |
| 348 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | if err := hs.readSessionTicket(); err != nil { |
| 350 | return err |
| 351 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 352 | if err := hs.readFinished(nil); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | return err |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if sessionCache != nil && hs.session != nil && session != hs.session { |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 358 | if c.config.Bugs.RequireSessionTickets && len(hs.session.sessionTicket) == 0 { |
| 359 | return errors.New("tls: new session used session IDs instead of tickets") |
| 360 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 361 | sessionCache.Put(cacheKey, hs.session) |
| 362 | } |
| 363 | |
| 364 | c.didResume = isResume |
| 365 | c.handshakeComplete = true |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 366 | c.cipherSuite = suite |
| 367 | copy(c.clientRandom[:], hs.hello.random) |
| 368 | copy(c.serverRandom[:], hs.serverHello.random) |
| 369 | copy(c.masterSecret[:], hs.masterSecret) |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 370 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | return nil |
| 372 | } |
| 373 | |
| 374 | func (hs *clientHandshakeState) doFullHandshake() error { |
| 375 | c := hs.c |
| 376 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 377 | var leaf *x509.Certificate |
| 378 | if hs.suite.flags&suitePSK == 0 { |
| 379 | msg, err := c.readHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | if err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | return err |
| 382 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 384 | certMsg, ok := msg.(*certificateMsg) |
David Benjamin | 7505144 | 2016-07-01 18:58:51 -0400 | [diff] [blame] | 385 | if !ok { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 386 | c.sendAlert(alertUnexpectedMessage) |
| 387 | return unexpectedMessageError(certMsg, msg) |
| 388 | } |
| 389 | hs.writeServerHash(certMsg.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | |
David Benjamin | 7505144 | 2016-07-01 18:58:51 -0400 | [diff] [blame] | 391 | if err := hs.verifyCertificates(certMsg); err != nil { |
| 392 | return err |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 393 | } |
David Benjamin | 7505144 | 2016-07-01 18:58:51 -0400 | [diff] [blame] | 394 | leaf = c.peerCertificates[0] |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 395 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 396 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 397 | if hs.serverHello.extensions.ocspStapling { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 398 | msg, err := c.readHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 399 | if err != nil { |
| 400 | return err |
| 401 | } |
| 402 | cs, ok := msg.(*certificateStatusMsg) |
| 403 | if !ok { |
| 404 | c.sendAlert(alertUnexpectedMessage) |
| 405 | return unexpectedMessageError(cs, msg) |
| 406 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 407 | hs.writeServerHash(cs.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 408 | |
| 409 | if cs.statusType == statusTypeOCSP { |
| 410 | c.ocspResponse = cs.response |
| 411 | } |
| 412 | } |
| 413 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 414 | msg, err := c.readHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | if err != nil { |
| 416 | return err |
| 417 | } |
| 418 | |
| 419 | keyAgreement := hs.suite.ka(c.vers) |
| 420 | |
| 421 | skx, ok := msg.(*serverKeyExchangeMsg) |
| 422 | if ok { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 423 | hs.writeServerHash(skx.marshal()) |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 424 | err = keyAgreement.processServerKeyExchange(c.config, hs.hello, hs.serverHello, leaf, skx) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | if err != nil { |
| 426 | c.sendAlert(alertUnexpectedMessage) |
| 427 | return err |
| 428 | } |
| 429 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 430 | c.peerSignatureAlgorithm = keyAgreement.peerSignatureAlgorithm() |
| 431 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | msg, err = c.readHandshake() |
| 433 | if err != nil { |
| 434 | return err |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | var chainToSend *Certificate |
| 439 | var certRequested bool |
| 440 | certReq, ok := msg.(*certificateRequestMsg) |
| 441 | if ok { |
| 442 | certRequested = true |
| 443 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 444 | hs.writeServerHash(certReq.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 445 | |
David Benjamin | a6f8263 | 2016-07-01 18:44:02 -0400 | [diff] [blame] | 446 | chainToSend, err = selectClientCertificate(c, certReq) |
| 447 | if err != nil { |
| 448 | return err |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | msg, err = c.readHandshake() |
| 452 | if err != nil { |
| 453 | return err |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | shd, ok := msg.(*serverHelloDoneMsg) |
| 458 | if !ok { |
| 459 | c.sendAlert(alertUnexpectedMessage) |
| 460 | return unexpectedMessageError(shd, msg) |
| 461 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 462 | hs.writeServerHash(shd.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 463 | |
| 464 | // If the server requested a certificate then we have to send a |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 465 | // Certificate message in TLS, even if it's empty because we don't have |
| 466 | // a certificate to send. In SSL 3.0, skip the message and send a |
| 467 | // no_certificate warning alert. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 468 | if certRequested { |
David Benjamin | 0b7ca7d | 2016-03-10 15:44:22 -0500 | [diff] [blame] | 469 | if c.vers == VersionSSL30 && chainToSend == nil { |
| 470 | c.sendAlert(alertNoCertficate) |
| 471 | } else if !c.config.Bugs.SkipClientCertificate { |
| 472 | certMsg := new(certificateMsg) |
| 473 | if chainToSend != nil { |
| 474 | certMsg.certificates = chainToSend.Certificate |
| 475 | } |
| 476 | hs.writeClientHash(certMsg.marshal()) |
| 477 | c.writeRecord(recordTypeHandshake, certMsg.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 478 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 479 | } |
| 480 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 481 | preMasterSecret, ckx, err := keyAgreement.generateClientKeyExchange(c.config, hs.hello, leaf) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | if err != nil { |
| 483 | c.sendAlert(alertInternalError) |
| 484 | return err |
| 485 | } |
| 486 | if ckx != nil { |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 487 | if c.config.Bugs.EarlyChangeCipherSpec < 2 { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 488 | hs.writeClientHash(ckx.marshal()) |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | c.writeRecord(recordTypeHandshake, ckx.marshal()) |
| 491 | } |
| 492 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 493 | if hs.serverHello.extensions.extendedMasterSecret && c.vers >= VersionTLS10 { |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 494 | hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 495 | c.extendedMasterSecret = true |
| 496 | } else { |
| 497 | if c.config.Bugs.RequireExtendedMasterSecret { |
| 498 | return errors.New("tls: extended master secret required but not supported by peer") |
| 499 | } |
| 500 | hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.hello.random, hs.serverHello.random) |
| 501 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 502 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | if chainToSend != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 504 | certVerify := &certificateVerifyMsg{ |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 505 | hasSignatureAlgorithm: c.vers >= VersionTLS12, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 506 | } |
| 507 | |
David Benjamin | 72dc783 | 2015-03-16 17:49:43 -0400 | [diff] [blame] | 508 | // Determine the hash to sign. |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 509 | privKey := c.config.Certificates[0].PrivateKey |
David Benjamin | 72dc783 | 2015-03-16 17:49:43 -0400 | [diff] [blame] | 510 | if c.config.Bugs.IgnorePeerSignatureAlgorithmPreferences { |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 511 | certReq.signatureAlgorithms = c.config.signatureAlgorithmsForClient() |
David Benjamin | 6de0e53 | 2015-07-28 22:43:19 -0400 | [diff] [blame] | 512 | } |
David Benjamin | 72dc783 | 2015-03-16 17:49:43 -0400 | [diff] [blame] | 513 | |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 514 | if certVerify.hasSignatureAlgorithm { |
| 515 | certVerify.signatureAlgorithm, err = selectSignatureAlgorithm(c.vers, privKey, certReq.signatureAlgorithms, c.config.signatureAlgorithmsForClient()) |
| 516 | if err != nil { |
| 517 | c.sendAlert(alertInternalError) |
| 518 | return err |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | } |
Nick Harper | 60edffd | 2016-06-21 15:19:24 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if c.vers > VersionSSL30 { |
| 523 | msg := hs.finishedHash.buffer |
| 524 | if c.config.Bugs.InvalidCertVerifySignature { |
| 525 | msg = make([]byte, len(hs.finishedHash.buffer)) |
| 526 | copy(msg, hs.finishedHash.buffer) |
| 527 | msg[0] ^= 0x80 |
| 528 | } |
| 529 | certVerify.signature, err = signMessage(c.vers, privKey, c.config, certVerify.signatureAlgorithm, msg) |
| 530 | } else { |
| 531 | // SSL 3.0's client certificate construction is |
| 532 | // incompatible with signatureAlgorithm. |
| 533 | rsaKey, ok := privKey.(*rsa.PrivateKey) |
| 534 | if !ok { |
| 535 | err = errors.New("unsupported signature type for client certificate") |
| 536 | } else { |
| 537 | digest := hs.finishedHash.hashForClientCertificateSSL3(hs.masterSecret) |
| 538 | if c.config.Bugs.InvalidCertVerifySignature { |
| 539 | digest[0] ^= 0x80 |
| 540 | } |
| 541 | certVerify.signature, err = rsa.SignPKCS1v15(c.config.rand(), rsaKey, crypto.MD5SHA1, digest) |
| 542 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | } |
| 544 | if err != nil { |
| 545 | c.sendAlert(alertInternalError) |
| 546 | return errors.New("tls: failed to sign handshake with client certificate: " + err.Error()) |
| 547 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 548 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 549 | hs.writeClientHash(certVerify.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 550 | c.writeRecord(recordTypeHandshake, certVerify.marshal()) |
| 551 | } |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame^] | 552 | c.flushHandshake() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 553 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 554 | hs.finishedHash.discardHandshakeBuffer() |
| 555 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | return nil |
| 557 | } |
| 558 | |
David Benjamin | 7505144 | 2016-07-01 18:58:51 -0400 | [diff] [blame] | 559 | func (hs *clientHandshakeState) verifyCertificates(certMsg *certificateMsg) error { |
| 560 | c := hs.c |
| 561 | |
| 562 | if len(certMsg.certificates) == 0 { |
| 563 | c.sendAlert(alertIllegalParameter) |
| 564 | return errors.New("tls: no certificates sent") |
| 565 | } |
| 566 | |
| 567 | certs := make([]*x509.Certificate, len(certMsg.certificates)) |
| 568 | for i, asn1Data := range certMsg.certificates { |
| 569 | cert, err := x509.ParseCertificate(asn1Data) |
| 570 | if err != nil { |
| 571 | c.sendAlert(alertBadCertificate) |
| 572 | return errors.New("tls: failed to parse certificate from server: " + err.Error()) |
| 573 | } |
| 574 | certs[i] = cert |
| 575 | } |
| 576 | |
| 577 | if !c.config.InsecureSkipVerify { |
| 578 | opts := x509.VerifyOptions{ |
| 579 | Roots: c.config.RootCAs, |
| 580 | CurrentTime: c.config.time(), |
| 581 | DNSName: c.config.ServerName, |
| 582 | Intermediates: x509.NewCertPool(), |
| 583 | } |
| 584 | |
| 585 | for i, cert := range certs { |
| 586 | if i == 0 { |
| 587 | continue |
| 588 | } |
| 589 | opts.Intermediates.AddCert(cert) |
| 590 | } |
| 591 | var err error |
| 592 | c.verifiedChains, err = certs[0].Verify(opts) |
| 593 | if err != nil { |
| 594 | c.sendAlert(alertBadCertificate) |
| 595 | return err |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | switch certs[0].PublicKey.(type) { |
| 600 | case *rsa.PublicKey, *ecdsa.PublicKey: |
| 601 | break |
| 602 | default: |
| 603 | c.sendAlert(alertUnsupportedCertificate) |
| 604 | return fmt.Errorf("tls: server's certificate contains an unsupported type of public key: %T", certs[0].PublicKey) |
| 605 | } |
| 606 | |
| 607 | c.peerCertificates = certs |
| 608 | return nil |
| 609 | } |
| 610 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 611 | func (hs *clientHandshakeState) establishKeys() error { |
| 612 | c := hs.c |
| 613 | |
| 614 | clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 615 | keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.hello.random, hs.serverHello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen(c.vers)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 616 | var clientCipher, serverCipher interface{} |
| 617 | var clientHash, serverHash macFunction |
| 618 | if hs.suite.cipher != nil { |
| 619 | clientCipher = hs.suite.cipher(clientKey, clientIV, false /* not for reading */) |
| 620 | clientHash = hs.suite.mac(c.vers, clientMAC) |
| 621 | serverCipher = hs.suite.cipher(serverKey, serverIV, true /* for reading */) |
| 622 | serverHash = hs.suite.mac(c.vers, serverMAC) |
| 623 | } else { |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 624 | clientCipher = hs.suite.aead(c.vers, clientKey, clientIV) |
| 625 | serverCipher = hs.suite.aead(c.vers, serverKey, serverIV) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | c.in.prepareCipherSpec(c.vers, serverCipher, serverHash) |
| 629 | c.out.prepareCipherSpec(c.vers, clientCipher, clientHash) |
| 630 | return nil |
| 631 | } |
| 632 | |
David Benjamin | 7510140 | 2016-07-01 13:40:23 -0400 | [diff] [blame] | 633 | func (hs *clientHandshakeState) processServerExtensions(serverExtensions *serverExtensions) error { |
| 634 | c := hs.c |
| 635 | |
| 636 | if c.config.Bugs.RequireRenegotiationInfo && serverExtensions.secureRenegotiation == nil { |
| 637 | return errors.New("tls: renegotiation extension missing") |
| 638 | } |
| 639 | |
| 640 | if len(c.clientVerify) > 0 && !c.noRenegotiationInfo() { |
| 641 | var expectedRenegInfo []byte |
| 642 | expectedRenegInfo = append(expectedRenegInfo, c.clientVerify...) |
| 643 | expectedRenegInfo = append(expectedRenegInfo, c.serverVerify...) |
| 644 | if !bytes.Equal(serverExtensions.secureRenegotiation, expectedRenegInfo) { |
| 645 | c.sendAlert(alertHandshakeFailure) |
| 646 | return fmt.Errorf("tls: renegotiation mismatch") |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | if expected := c.config.Bugs.ExpectedCustomExtension; expected != nil { |
| 651 | if serverExtensions.customExtension != *expected { |
| 652 | return fmt.Errorf("tls: bad custom extension contents %q", serverExtensions.customExtension) |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | clientDidNPN := hs.hello.nextProtoNeg |
| 657 | clientDidALPN := len(hs.hello.alpnProtocols) > 0 |
| 658 | serverHasNPN := serverExtensions.nextProtoNeg |
| 659 | serverHasALPN := len(serverExtensions.alpnProtocol) > 0 |
| 660 | |
| 661 | if !clientDidNPN && serverHasNPN { |
| 662 | c.sendAlert(alertHandshakeFailure) |
| 663 | return errors.New("server advertised unrequested NPN extension") |
| 664 | } |
| 665 | |
| 666 | if !clientDidALPN && serverHasALPN { |
| 667 | c.sendAlert(alertHandshakeFailure) |
| 668 | return errors.New("server advertised unrequested ALPN extension") |
| 669 | } |
| 670 | |
| 671 | if serverHasNPN && serverHasALPN { |
| 672 | c.sendAlert(alertHandshakeFailure) |
| 673 | return errors.New("server advertised both NPN and ALPN extensions") |
| 674 | } |
| 675 | |
| 676 | if serverHasALPN { |
| 677 | c.clientProtocol = serverExtensions.alpnProtocol |
| 678 | c.clientProtocolFallback = false |
| 679 | c.usedALPN = true |
| 680 | } |
| 681 | |
| 682 | if !hs.hello.channelIDSupported && serverExtensions.channelIDRequested { |
| 683 | c.sendAlert(alertHandshakeFailure) |
| 684 | return errors.New("server advertised unrequested Channel ID extension") |
| 685 | } |
| 686 | |
| 687 | if serverExtensions.srtpProtectionProfile != 0 { |
| 688 | if serverExtensions.srtpMasterKeyIdentifier != "" { |
| 689 | return errors.New("tls: server selected SRTP MKI value") |
| 690 | } |
| 691 | |
| 692 | found := false |
| 693 | for _, p := range c.config.SRTPProtectionProfiles { |
| 694 | if p == serverExtensions.srtpProtectionProfile { |
| 695 | found = true |
| 696 | break |
| 697 | } |
| 698 | } |
| 699 | if !found { |
| 700 | return errors.New("tls: server advertised unsupported SRTP profile") |
| 701 | } |
| 702 | |
| 703 | c.srtpProtectionProfile = serverExtensions.srtpProtectionProfile |
| 704 | } |
| 705 | |
| 706 | return nil |
| 707 | } |
| 708 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 709 | func (hs *clientHandshakeState) serverResumedSession() bool { |
| 710 | // If the server responded with the same sessionId then it means the |
| 711 | // sessionTicket is being used to resume a TLS session. |
| 712 | return hs.session != nil && hs.hello.sessionId != nil && |
| 713 | bytes.Equal(hs.serverHello.sessionId, hs.hello.sessionId) |
| 714 | } |
| 715 | |
| 716 | func (hs *clientHandshakeState) processServerHello() (bool, error) { |
| 717 | c := hs.c |
| 718 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 719 | if hs.serverResumedSession() { |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 720 | // For test purposes, assert that the server never accepts the |
| 721 | // resumption offer on renegotiation. |
| 722 | if c.cipherSuite != nil && c.config.Bugs.FailIfResumeOnRenego { |
| 723 | return false, errors.New("tls: server resumed session on renegotiation") |
| 724 | } |
| 725 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 726 | if hs.serverHello.extensions.sctList != nil { |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 727 | return false, errors.New("tls: server sent SCT extension on session resumption") |
| 728 | } |
| 729 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 730 | if hs.serverHello.extensions.ocspStapling { |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 731 | return false, errors.New("tls: server sent OCSP extension on session resumption") |
| 732 | } |
| 733 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 734 | // Restore masterSecret and peerCerts from previous state |
| 735 | hs.masterSecret = hs.session.masterSecret |
| 736 | c.peerCertificates = hs.session.serverCertificates |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 737 | c.extendedMasterSecret = hs.session.extendedMasterSecret |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 738 | c.sctList = hs.session.sctList |
| 739 | c.ocspResponse = hs.session.ocspResponse |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 740 | hs.finishedHash.discardHandshakeBuffer() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 741 | return true, nil |
| 742 | } |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 743 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 744 | if hs.serverHello.extensions.sctList != nil { |
| 745 | c.sctList = hs.serverHello.extensions.sctList |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 748 | return false, nil |
| 749 | } |
| 750 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 751 | func (hs *clientHandshakeState) readFinished(out []byte) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 752 | c := hs.c |
| 753 | |
| 754 | c.readRecord(recordTypeChangeCipherSpec) |
| 755 | if err := c.in.error(); err != nil { |
| 756 | return err |
| 757 | } |
| 758 | |
| 759 | msg, err := c.readHandshake() |
| 760 | if err != nil { |
| 761 | return err |
| 762 | } |
| 763 | serverFinished, ok := msg.(*finishedMsg) |
| 764 | if !ok { |
| 765 | c.sendAlert(alertUnexpectedMessage) |
| 766 | return unexpectedMessageError(serverFinished, msg) |
| 767 | } |
| 768 | |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 769 | if c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 770 | verify := hs.finishedHash.serverSum(hs.masterSecret) |
| 771 | if len(verify) != len(serverFinished.verifyData) || |
| 772 | subtle.ConstantTimeCompare(verify, serverFinished.verifyData) != 1 { |
| 773 | c.sendAlert(alertHandshakeFailure) |
| 774 | return errors.New("tls: server's Finished message was incorrect") |
| 775 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 776 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 777 | c.serverVerify = append(c.serverVerify[:0], serverFinished.verifyData...) |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 778 | copy(out, serverFinished.verifyData) |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 779 | hs.writeServerHash(serverFinished.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 780 | return nil |
| 781 | } |
| 782 | |
| 783 | func (hs *clientHandshakeState) readSessionTicket() error { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 784 | c := hs.c |
| 785 | |
| 786 | // Create a session with no server identifier. Either a |
| 787 | // session ID or session ticket will be attached. |
| 788 | session := &ClientSessionState{ |
| 789 | vers: c.vers, |
| 790 | cipherSuite: hs.suite.id, |
| 791 | masterSecret: hs.masterSecret, |
| 792 | handshakeHash: hs.finishedHash.server.Sum(nil), |
| 793 | serverCertificates: c.peerCertificates, |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 794 | sctList: c.sctList, |
| 795 | ocspResponse: c.ocspResponse, |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 796 | } |
| 797 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 798 | if !hs.serverHello.extensions.ticketSupported { |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 799 | if c.config.Bugs.ExpectNewTicket { |
| 800 | return errors.New("tls: expected new ticket") |
| 801 | } |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 802 | if hs.session == nil && len(hs.serverHello.sessionId) > 0 { |
| 803 | session.sessionId = hs.serverHello.sessionId |
| 804 | hs.session = session |
| 805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | return nil |
| 807 | } |
| 808 | |
David Benjamin | c7ce977 | 2015-10-09 19:32:41 -0400 | [diff] [blame] | 809 | if c.vers == VersionSSL30 { |
| 810 | return errors.New("tls: negotiated session tickets in SSL 3.0") |
| 811 | } |
| 812 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 813 | msg, err := c.readHandshake() |
| 814 | if err != nil { |
| 815 | return err |
| 816 | } |
| 817 | sessionTicketMsg, ok := msg.(*newSessionTicketMsg) |
| 818 | if !ok { |
| 819 | c.sendAlert(alertUnexpectedMessage) |
| 820 | return unexpectedMessageError(sessionTicketMsg, msg) |
| 821 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 822 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 823 | session.sessionTicket = sessionTicketMsg.ticket |
| 824 | hs.session = session |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 825 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 826 | hs.writeServerHash(sessionTicketMsg.marshal()) |
| 827 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 828 | return nil |
| 829 | } |
| 830 | |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 831 | func (hs *clientHandshakeState) sendFinished(out []byte, isResume bool) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 832 | c := hs.c |
| 833 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 834 | var postCCSBytes []byte |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 835 | seqno := hs.c.sendHandshakeSeq |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 836 | if hs.serverHello.extensions.nextProtoNeg { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 837 | nextProto := new(nextProtoMsg) |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 838 | proto, fallback := mutualProtocol(c.config.NextProtos, hs.serverHello.extensions.nextProtos) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 839 | nextProto.proto = proto |
| 840 | c.clientProtocol = proto |
| 841 | c.clientProtocolFallback = fallback |
| 842 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 843 | nextProtoBytes := nextProto.marshal() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 844 | hs.writeHash(nextProtoBytes, seqno) |
| 845 | seqno++ |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 846 | postCCSBytes = append(postCCSBytes, nextProtoBytes...) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Nick Harper | b3d51be | 2016-07-01 11:43:18 -0400 | [diff] [blame] | 849 | if hs.serverHello.extensions.channelIDRequested { |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 850 | channelIDMsg := new(channelIDMsg) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 851 | if c.config.ChannelID.Curve != elliptic.P256() { |
| 852 | return fmt.Errorf("tls: Channel ID is not on P-256.") |
| 853 | } |
| 854 | var resumeHash []byte |
| 855 | if isResume { |
| 856 | resumeHash = hs.session.handshakeHash |
| 857 | } |
| 858 | r, s, err := ecdsa.Sign(c.config.rand(), c.config.ChannelID, hs.finishedHash.hashForChannelID(resumeHash)) |
| 859 | if err != nil { |
| 860 | return err |
| 861 | } |
| 862 | channelID := make([]byte, 128) |
| 863 | writeIntPadded(channelID[0:32], c.config.ChannelID.X) |
| 864 | writeIntPadded(channelID[32:64], c.config.ChannelID.Y) |
| 865 | writeIntPadded(channelID[64:96], r) |
| 866 | writeIntPadded(channelID[96:128], s) |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 867 | channelIDMsg.channelID = channelID |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 868 | |
| 869 | c.channelID = &c.config.ChannelID.PublicKey |
| 870 | |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 871 | channelIDMsgBytes := channelIDMsg.marshal() |
| 872 | hs.writeHash(channelIDMsgBytes, seqno) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 873 | seqno++ |
David Benjamin | 24599a8 | 2016-06-30 18:56:53 -0400 | [diff] [blame] | 874 | postCCSBytes = append(postCCSBytes, channelIDMsgBytes...) |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 875 | } |
| 876 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 877 | finished := new(finishedMsg) |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 878 | if c.config.Bugs.EarlyChangeCipherSpec == 2 { |
| 879 | finished.verifyData = hs.finishedHash.clientSum(nil) |
| 880 | } else { |
| 881 | finished.verifyData = hs.finishedHash.clientSum(hs.masterSecret) |
| 882 | } |
Adam Langley | af0e32c | 2015-06-03 09:57:23 -0700 | [diff] [blame] | 883 | copy(out, finished.verifyData) |
David Benjamin | 513f0ea | 2015-04-02 19:33:31 -0400 | [diff] [blame] | 884 | if c.config.Bugs.BadFinished { |
| 885 | finished.verifyData[0]++ |
| 886 | } |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 887 | c.clientVerify = append(c.clientVerify[:0], finished.verifyData...) |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 888 | hs.finishedBytes = finished.marshal() |
| 889 | hs.writeHash(hs.finishedBytes, seqno) |
| 890 | postCCSBytes = append(postCCSBytes, hs.finishedBytes...) |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 891 | |
| 892 | if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
| 893 | c.writeRecord(recordTypeHandshake, postCCSBytes[:5]) |
| 894 | postCCSBytes = postCCSBytes[5:] |
| 895 | } |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame^] | 896 | c.flushHandshake() |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 897 | |
| 898 | if !c.config.Bugs.SkipChangeCipherSpec && |
| 899 | c.config.Bugs.EarlyChangeCipherSpec == 0 { |
David Benjamin | 8411b24 | 2015-11-26 12:07:28 -0500 | [diff] [blame] | 900 | ccs := []byte{1} |
| 901 | if c.config.Bugs.BadChangeCipherSpec != nil { |
| 902 | ccs = c.config.Bugs.BadChangeCipherSpec |
| 903 | } |
| 904 | c.writeRecord(recordTypeChangeCipherSpec, ccs) |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 905 | } |
| 906 | |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 907 | if c.config.Bugs.AppDataAfterChangeCipherSpec != nil { |
| 908 | c.writeRecord(recordTypeApplicationData, c.config.Bugs.AppDataAfterChangeCipherSpec) |
| 909 | } |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 910 | if c.config.Bugs.AlertAfterChangeCipherSpec != 0 { |
| 911 | c.sendAlert(c.config.Bugs.AlertAfterChangeCipherSpec) |
| 912 | return errors.New("tls: simulating post-CCS alert") |
| 913 | } |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 914 | |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 915 | if !c.config.Bugs.SkipFinished { |
| 916 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
David Benjamin | 582ba04 | 2016-07-07 12:33:25 -0700 | [diff] [blame^] | 917 | c.flushHandshake() |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 918 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 919 | return nil |
| 920 | } |
| 921 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 922 | func (hs *clientHandshakeState) writeClientHash(msg []byte) { |
| 923 | // writeClientHash is called before writeRecord. |
| 924 | hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 925 | } |
| 926 | |
| 927 | func (hs *clientHandshakeState) writeServerHash(msg []byte) { |
| 928 | // writeServerHash is called after readHandshake. |
| 929 | hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 930 | } |
| 931 | |
| 932 | func (hs *clientHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 933 | if hs.c.isDTLS { |
| 934 | // This is somewhat hacky. DTLS hashes a slightly different format. |
| 935 | // First, the TLS header. |
| 936 | hs.finishedHash.Write(msg[:4]) |
| 937 | // Then the sequence number and reassembled fragment offset (always 0). |
| 938 | hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 939 | // Then the reassembled fragment (always equal to the message length). |
| 940 | hs.finishedHash.Write(msg[1:4]) |
| 941 | // And then the message body. |
| 942 | hs.finishedHash.Write(msg[4:]) |
| 943 | } else { |
| 944 | hs.finishedHash.Write(msg) |
| 945 | } |
| 946 | } |
| 947 | |
David Benjamin | a6f8263 | 2016-07-01 18:44:02 -0400 | [diff] [blame] | 948 | // selectClientCertificate selects a certificate for use with the given |
| 949 | // certificate, or none if none match. It may return a particular certificate or |
| 950 | // nil on success, or an error on internal error. |
| 951 | func selectClientCertificate(c *Conn, certReq *certificateRequestMsg) (*Certificate, error) { |
| 952 | // RFC 4346 on the certificateAuthorities field: |
| 953 | // A list of the distinguished names of acceptable certificate |
| 954 | // authorities. These distinguished names may specify a desired |
| 955 | // distinguished name for a root CA or for a subordinate CA; thus, this |
| 956 | // message can be used to describe both known roots and a desired |
| 957 | // authorization space. If the certificate_authorities list is empty |
| 958 | // then the client MAY send any certificate of the appropriate |
| 959 | // ClientCertificateType, unless there is some external arrangement to |
| 960 | // the contrary. |
| 961 | |
| 962 | var rsaAvail, ecdsaAvail bool |
| 963 | for _, certType := range certReq.certificateTypes { |
| 964 | switch certType { |
| 965 | case CertTypeRSASign: |
| 966 | rsaAvail = true |
| 967 | case CertTypeECDSASign: |
| 968 | ecdsaAvail = true |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // We need to search our list of client certs for one |
| 973 | // where SignatureAlgorithm is RSA and the Issuer is in |
| 974 | // certReq.certificateAuthorities |
| 975 | findCert: |
| 976 | for i, chain := range c.config.Certificates { |
| 977 | if !rsaAvail && !ecdsaAvail { |
| 978 | continue |
| 979 | } |
| 980 | |
| 981 | // Ensure the private key supports one of the advertised |
| 982 | // signature algorithms. |
| 983 | if certReq.hasSignatureAlgorithm { |
| 984 | if _, err := selectSignatureAlgorithm(c.vers, chain.PrivateKey, certReq.signatureAlgorithms, c.config.signatureAlgorithmsForClient()); err != nil { |
| 985 | continue |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | for j, cert := range chain.Certificate { |
| 990 | x509Cert := chain.Leaf |
| 991 | // parse the certificate if this isn't the leaf |
| 992 | // node, or if chain.Leaf was nil |
| 993 | if j != 0 || x509Cert == nil { |
| 994 | var err error |
| 995 | if x509Cert, err = x509.ParseCertificate(cert); err != nil { |
| 996 | c.sendAlert(alertInternalError) |
| 997 | return nil, errors.New("tls: failed to parse client certificate #" + strconv.Itoa(i) + ": " + err.Error()) |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | switch { |
| 1002 | case rsaAvail && x509Cert.PublicKeyAlgorithm == x509.RSA: |
| 1003 | case ecdsaAvail && x509Cert.PublicKeyAlgorithm == x509.ECDSA: |
| 1004 | default: |
| 1005 | continue findCert |
| 1006 | } |
| 1007 | |
| 1008 | if len(certReq.certificateAuthorities) == 0 { |
| 1009 | // They gave us an empty list, so just take the |
| 1010 | // first certificate of valid type from |
| 1011 | // c.config.Certificates. |
| 1012 | return &chain, nil |
| 1013 | } |
| 1014 | |
| 1015 | for _, ca := range certReq.certificateAuthorities { |
| 1016 | if bytes.Equal(x509Cert.RawIssuer, ca) { |
| 1017 | return &chain, nil |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | return nil, nil |
| 1024 | } |
| 1025 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1026 | // clientSessionCacheKey returns a key used to cache sessionTickets that could |
| 1027 | // be used to resume previously negotiated TLS sessions with a server. |
| 1028 | func clientSessionCacheKey(serverAddr net.Addr, config *Config) string { |
| 1029 | if len(config.ServerName) > 0 { |
| 1030 | return config.ServerName |
| 1031 | } |
| 1032 | return serverAddr.String() |
| 1033 | } |
| 1034 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 1035 | // mutualProtocol finds the mutual Next Protocol Negotiation or ALPN protocol |
| 1036 | // given list of possible protocols and a list of the preference order. The |
| 1037 | // first list must not be empty. It returns the resulting protocol and flag |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1038 | // indicating if the fallback case was reached. |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 1039 | func mutualProtocol(protos, preferenceProtos []string) (string, bool) { |
| 1040 | for _, s := range preferenceProtos { |
| 1041 | for _, c := range protos { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1042 | if s == c { |
| 1043 | return s, false |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 1048 | return protos[0], true |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1049 | } |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 1050 | |
| 1051 | // writeIntPadded writes x into b, padded up with leading zeros as |
| 1052 | // needed. |
| 1053 | func writeIntPadded(b []byte, x *big.Int) { |
| 1054 | for i := range b { |
| 1055 | b[i] = 0 |
| 1056 | } |
| 1057 | xb := x.Bytes() |
| 1058 | copy(b[len(b)-len(xb):], xb) |
| 1059 | } |