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 | |
| 5 | package main |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [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" |
| 15 | "encoding/asn1" |
| 16 | "errors" |
| 17 | "fmt" |
| 18 | "io" |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 19 | "math/big" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 20 | "net" |
| 21 | "strconv" |
| 22 | ) |
| 23 | |
| 24 | type clientHandshakeState struct { |
| 25 | c *Conn |
| 26 | serverHello *serverHelloMsg |
| 27 | hello *clientHelloMsg |
| 28 | suite *cipherSuite |
| 29 | finishedHash finishedHash |
| 30 | masterSecret []byte |
| 31 | session *ClientSessionState |
| 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 { |
| 48 | if l := len(proto); l == 0 || l > 255 { |
| 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{ |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 59 | isDTLS: c.isDTLS, |
| 60 | vers: c.config.maxVersion(), |
| 61 | compressionMethods: []uint8{compressionNone}, |
| 62 | random: make([]byte, 32), |
| 63 | ocspStapling: true, |
| 64 | serverName: c.config.ServerName, |
| 65 | supportedCurves: c.config.curvePreferences(), |
| 66 | supportedPoints: []uint8{pointFormatUncompressed}, |
| 67 | nextProtoNeg: len(c.config.NextProtos) > 0, |
| 68 | secureRenegotiation: true, |
| 69 | alpnProtocols: c.config.NextProtos, |
| 70 | duplicateExtension: c.config.Bugs.DuplicateExtension, |
| 71 | channelIDSupported: c.config.ChannelID != nil, |
| 72 | npnLast: c.config.Bugs.SwapNPNAndALPN, |
| 73 | extendedMasterSecret: c.config.maxVersion() >= VersionTLS10, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 76 | if c.config.Bugs.SendClientVersion != 0 { |
| 77 | hello.vers = c.config.Bugs.SendClientVersion |
| 78 | } |
| 79 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 80 | if c.config.Bugs.NoExtendedMasterSecret { |
| 81 | hello.extendedMasterSecret = false |
| 82 | } |
| 83 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 84 | possibleCipherSuites := c.config.cipherSuites() |
| 85 | hello.cipherSuites = make([]uint16, 0, len(possibleCipherSuites)) |
| 86 | |
| 87 | NextCipherSuite: |
| 88 | for _, suiteId := range possibleCipherSuites { |
| 89 | for _, suite := range cipherSuites { |
| 90 | if suite.id != suiteId { |
| 91 | continue |
| 92 | } |
| 93 | // Don't advertise TLS 1.2-only cipher suites unless |
| 94 | // we're attempting TLS 1.2. |
| 95 | if hello.vers < VersionTLS12 && suite.flags&suiteTLS12 != 0 { |
| 96 | continue |
| 97 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 98 | // Don't advertise non-DTLS cipher suites on DTLS. |
| 99 | if c.isDTLS && suite.flags&suiteNoDTLS != 0 { |
| 100 | continue |
| 101 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 102 | hello.cipherSuites = append(hello.cipherSuites, suiteId) |
| 103 | continue NextCipherSuite |
| 104 | } |
| 105 | } |
| 106 | |
David Benjamin | bef270a | 2014-08-02 04:22:02 -0400 | [diff] [blame] | 107 | if c.config.Bugs.SendFallbackSCSV { |
| 108 | hello.cipherSuites = append(hello.cipherSuites, fallbackSCSV) |
| 109 | } |
| 110 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | _, err := io.ReadFull(c.config.rand(), hello.random) |
| 112 | if err != nil { |
| 113 | c.sendAlert(alertInternalError) |
| 114 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 115 | } |
| 116 | |
| 117 | if hello.vers >= VersionTLS12 { |
| 118 | hello.signatureAndHashes = supportedSKXSignatureAlgorithms |
| 119 | } |
| 120 | |
| 121 | var session *ClientSessionState |
| 122 | var cacheKey string |
| 123 | sessionCache := c.config.ClientSessionCache |
| 124 | if c.config.SessionTicketsDisabled { |
| 125 | sessionCache = nil |
| 126 | } |
| 127 | |
| 128 | if sessionCache != nil { |
| 129 | hello.ticketSupported = true |
| 130 | |
| 131 | // Try to resume a previously negotiated TLS session, if |
| 132 | // available. |
| 133 | cacheKey = clientSessionCacheKey(c.conn.RemoteAddr(), c.config) |
| 134 | candidateSession, ok := sessionCache.Get(cacheKey) |
| 135 | if ok { |
| 136 | // Check that the ciphersuite/version used for the |
| 137 | // previous session are still valid. |
| 138 | cipherSuiteOk := false |
| 139 | for _, id := range hello.cipherSuites { |
| 140 | if id == candidateSession.cipherSuite { |
| 141 | cipherSuiteOk = true |
| 142 | break |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | versOk := candidateSession.vers >= c.config.minVersion() && |
| 147 | candidateSession.vers <= c.config.maxVersion() |
| 148 | if versOk && cipherSuiteOk { |
| 149 | session = candidateSession |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if session != nil { |
| 155 | hello.sessionTicket = session.sessionTicket |
Adam Langley | 3831173 | 2014-10-16 19:04:35 -0700 | [diff] [blame] | 156 | if c.config.Bugs.CorruptTicket { |
| 157 | hello.sessionTicket = make([]byte, len(session.sessionTicket)) |
| 158 | copy(hello.sessionTicket, session.sessionTicket) |
| 159 | if len(hello.sessionTicket) > 0 { |
| 160 | offset := 40 |
| 161 | if offset > len(hello.sessionTicket) { |
| 162 | offset = len(hello.sessionTicket) - 1 |
| 163 | } |
| 164 | hello.sessionTicket[offset] ^= 0x40 |
| 165 | } |
| 166 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | // A random session ID is used to detect when the |
| 168 | // server accepted the ticket and is resuming a session |
| 169 | // (see RFC 5077). |
Adam Langley | 3831173 | 2014-10-16 19:04:35 -0700 | [diff] [blame] | 170 | sessionIdLen := 16 |
| 171 | if c.config.Bugs.OversizedSessionId { |
| 172 | sessionIdLen = 33 |
| 173 | } |
| 174 | hello.sessionId = make([]byte, sessionIdLen) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | if _, err := io.ReadFull(c.config.rand(), hello.sessionId); err != nil { |
| 176 | c.sendAlert(alertInternalError) |
| 177 | return errors.New("tls: short read from Rand: " + err.Error()) |
| 178 | } |
| 179 | } |
| 180 | |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 181 | var helloBytes []byte |
| 182 | if c.config.Bugs.SendV2ClientHello { |
| 183 | v2Hello := &v2ClientHelloMsg{ |
| 184 | vers: hello.vers, |
| 185 | cipherSuites: hello.cipherSuites, |
| 186 | // No session resumption for V2ClientHello. |
| 187 | sessionId: nil, |
| 188 | challenge: hello.random, |
| 189 | } |
| 190 | helloBytes = v2Hello.marshal() |
| 191 | c.writeV2Record(helloBytes) |
| 192 | } else { |
| 193 | helloBytes = hello.marshal() |
| 194 | c.writeRecord(recordTypeHandshake, helloBytes) |
| 195 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 196 | |
| 197 | msg, err := c.readHandshake() |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 201 | |
| 202 | if c.isDTLS { |
| 203 | helloVerifyRequest, ok := msg.(*helloVerifyRequestMsg) |
| 204 | if ok { |
David Benjamin | 8bc38f5 | 2014-08-16 12:07:27 -0400 | [diff] [blame] | 205 | if helloVerifyRequest.vers != VersionTLS10 { |
| 206 | // Per RFC 6347, the version field in |
| 207 | // HelloVerifyRequest SHOULD be always DTLS |
| 208 | // 1.0. Enforce this for testing purposes. |
| 209 | return errors.New("dtls: bad HelloVerifyRequest version") |
| 210 | } |
| 211 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 212 | hello.raw = nil |
| 213 | hello.cookie = helloVerifyRequest.cookie |
| 214 | helloBytes = hello.marshal() |
| 215 | c.writeRecord(recordTypeHandshake, helloBytes) |
| 216 | |
| 217 | msg, err = c.readHandshake() |
| 218 | if err != nil { |
| 219 | return err |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 224 | serverHello, ok := msg.(*serverHelloMsg) |
| 225 | if !ok { |
| 226 | c.sendAlert(alertUnexpectedMessage) |
| 227 | return unexpectedMessageError(serverHello, msg) |
| 228 | } |
| 229 | |
David Benjamin | 76d8abe | 2014-08-14 16:25:34 -0400 | [diff] [blame] | 230 | c.vers, ok = c.config.mutualVersion(serverHello.vers) |
| 231 | if !ok { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | c.sendAlert(alertProtocolVersion) |
| 233 | return fmt.Errorf("tls: server selected unsupported protocol version %x", serverHello.vers) |
| 234 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 235 | c.haveVers = true |
| 236 | |
| 237 | suite := mutualCipherSuite(c.config.cipherSuites(), serverHello.cipherSuite) |
| 238 | if suite == nil { |
| 239 | c.sendAlert(alertHandshakeFailure) |
| 240 | return fmt.Errorf("tls: server selected an unsupported cipher suite") |
| 241 | } |
| 242 | |
| 243 | hs := &clientHandshakeState{ |
| 244 | c: c, |
| 245 | serverHello: serverHello, |
| 246 | hello: hello, |
| 247 | suite: suite, |
| 248 | finishedHash: newFinishedHash(c.vers, suite), |
| 249 | session: session, |
| 250 | } |
| 251 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 252 | hs.writeHash(helloBytes, hs.c.sendHandshakeSeq-1) |
| 253 | hs.writeServerHash(hs.serverHello.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 254 | |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 255 | if c.config.Bugs.EarlyChangeCipherSpec > 0 { |
| 256 | hs.establishKeys() |
| 257 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 258 | } |
| 259 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | isResume, err := hs.processServerHello() |
| 261 | if err != nil { |
| 262 | return err |
| 263 | } |
| 264 | |
| 265 | if isResume { |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 266 | if c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 267 | if err := hs.establishKeys(); err != nil { |
| 268 | return err |
| 269 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | } |
| 271 | if err := hs.readSessionTicket(); err != nil { |
| 272 | return err |
| 273 | } |
| 274 | if err := hs.readFinished(); err != nil { |
| 275 | return err |
| 276 | } |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 277 | if err := hs.sendFinished(isResume); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 278 | return err |
| 279 | } |
| 280 | } else { |
| 281 | if err := hs.doFullHandshake(); err != nil { |
| 282 | return err |
| 283 | } |
| 284 | if err := hs.establishKeys(); err != nil { |
| 285 | return err |
| 286 | } |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 287 | if err := hs.sendFinished(isResume); err != nil { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | return err |
| 289 | } |
| 290 | if err := hs.readSessionTicket(); err != nil { |
| 291 | return err |
| 292 | } |
| 293 | if err := hs.readFinished(); err != nil { |
| 294 | return err |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if sessionCache != nil && hs.session != nil && session != hs.session { |
| 299 | sessionCache.Put(cacheKey, hs.session) |
| 300 | } |
| 301 | |
| 302 | c.didResume = isResume |
| 303 | c.handshakeComplete = true |
| 304 | c.cipherSuite = suite.id |
| 305 | return nil |
| 306 | } |
| 307 | |
| 308 | func (hs *clientHandshakeState) doFullHandshake() error { |
| 309 | c := hs.c |
| 310 | |
| 311 | msg, err := c.readHandshake() |
| 312 | if err != nil { |
| 313 | return err |
| 314 | } |
| 315 | certMsg, ok := msg.(*certificateMsg) |
| 316 | if !ok || len(certMsg.certificates) == 0 { |
| 317 | c.sendAlert(alertUnexpectedMessage) |
| 318 | return unexpectedMessageError(certMsg, msg) |
| 319 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 320 | hs.writeServerHash(certMsg.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | |
| 322 | certs := make([]*x509.Certificate, len(certMsg.certificates)) |
| 323 | for i, asn1Data := range certMsg.certificates { |
| 324 | cert, err := x509.ParseCertificate(asn1Data) |
| 325 | if err != nil { |
| 326 | c.sendAlert(alertBadCertificate) |
| 327 | return errors.New("tls: failed to parse certificate from server: " + err.Error()) |
| 328 | } |
| 329 | certs[i] = cert |
| 330 | } |
| 331 | |
| 332 | if !c.config.InsecureSkipVerify { |
| 333 | opts := x509.VerifyOptions{ |
| 334 | Roots: c.config.RootCAs, |
| 335 | CurrentTime: c.config.time(), |
| 336 | DNSName: c.config.ServerName, |
| 337 | Intermediates: x509.NewCertPool(), |
| 338 | } |
| 339 | |
| 340 | for i, cert := range certs { |
| 341 | if i == 0 { |
| 342 | continue |
| 343 | } |
| 344 | opts.Intermediates.AddCert(cert) |
| 345 | } |
| 346 | c.verifiedChains, err = certs[0].Verify(opts) |
| 347 | if err != nil { |
| 348 | c.sendAlert(alertBadCertificate) |
| 349 | return err |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | switch certs[0].PublicKey.(type) { |
| 354 | case *rsa.PublicKey, *ecdsa.PublicKey: |
| 355 | break |
| 356 | default: |
| 357 | c.sendAlert(alertUnsupportedCertificate) |
| 358 | return fmt.Errorf("tls: server's certificate contains an unsupported type of public key: %T", certs[0].PublicKey) |
| 359 | } |
| 360 | |
| 361 | c.peerCertificates = certs |
| 362 | |
| 363 | if hs.serverHello.ocspStapling { |
| 364 | msg, err = c.readHandshake() |
| 365 | if err != nil { |
| 366 | return err |
| 367 | } |
| 368 | cs, ok := msg.(*certificateStatusMsg) |
| 369 | if !ok { |
| 370 | c.sendAlert(alertUnexpectedMessage) |
| 371 | return unexpectedMessageError(cs, msg) |
| 372 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 373 | hs.writeServerHash(cs.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 374 | |
| 375 | if cs.statusType == statusTypeOCSP { |
| 376 | c.ocspResponse = cs.response |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | msg, err = c.readHandshake() |
| 381 | if err != nil { |
| 382 | return err |
| 383 | } |
| 384 | |
| 385 | keyAgreement := hs.suite.ka(c.vers) |
| 386 | |
| 387 | skx, ok := msg.(*serverKeyExchangeMsg) |
| 388 | if ok { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 389 | hs.writeServerHash(skx.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | err = keyAgreement.processServerKeyExchange(c.config, hs.hello, hs.serverHello, certs[0], skx) |
| 391 | if err != nil { |
| 392 | c.sendAlert(alertUnexpectedMessage) |
| 393 | return err |
| 394 | } |
| 395 | |
| 396 | msg, err = c.readHandshake() |
| 397 | if err != nil { |
| 398 | return err |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | var chainToSend *Certificate |
| 403 | var certRequested bool |
| 404 | certReq, ok := msg.(*certificateRequestMsg) |
| 405 | if ok { |
| 406 | certRequested = true |
| 407 | |
| 408 | // RFC 4346 on the certificateAuthorities field: |
| 409 | // A list of the distinguished names of acceptable certificate |
| 410 | // authorities. These distinguished names may specify a desired |
| 411 | // distinguished name for a root CA or for a subordinate CA; |
| 412 | // thus, this message can be used to describe both known roots |
| 413 | // and a desired authorization space. If the |
| 414 | // certificate_authorities list is empty then the client MAY |
| 415 | // send any certificate of the appropriate |
| 416 | // ClientCertificateType, unless there is some external |
| 417 | // arrangement to the contrary. |
| 418 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 419 | hs.writeServerHash(certReq.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 420 | |
| 421 | var rsaAvail, ecdsaAvail bool |
| 422 | for _, certType := range certReq.certificateTypes { |
| 423 | switch certType { |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 424 | case CertTypeRSASign: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | rsaAvail = true |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 426 | case CertTypeECDSASign: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 427 | ecdsaAvail = true |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // We need to search our list of client certs for one |
| 432 | // where SignatureAlgorithm is RSA and the Issuer is in |
| 433 | // certReq.certificateAuthorities |
| 434 | findCert: |
| 435 | for i, chain := range c.config.Certificates { |
| 436 | if !rsaAvail && !ecdsaAvail { |
| 437 | continue |
| 438 | } |
| 439 | |
| 440 | for j, cert := range chain.Certificate { |
| 441 | x509Cert := chain.Leaf |
| 442 | // parse the certificate if this isn't the leaf |
| 443 | // node, or if chain.Leaf was nil |
| 444 | if j != 0 || x509Cert == nil { |
| 445 | if x509Cert, err = x509.ParseCertificate(cert); err != nil { |
| 446 | c.sendAlert(alertInternalError) |
| 447 | return errors.New("tls: failed to parse client certificate #" + strconv.Itoa(i) + ": " + err.Error()) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | switch { |
| 452 | case rsaAvail && x509Cert.PublicKeyAlgorithm == x509.RSA: |
| 453 | case ecdsaAvail && x509Cert.PublicKeyAlgorithm == x509.ECDSA: |
| 454 | default: |
| 455 | continue findCert |
| 456 | } |
| 457 | |
| 458 | if len(certReq.certificateAuthorities) == 0 { |
| 459 | // they gave us an empty list, so just take the |
| 460 | // first RSA cert from c.config.Certificates |
| 461 | chainToSend = &chain |
| 462 | break findCert |
| 463 | } |
| 464 | |
| 465 | for _, ca := range certReq.certificateAuthorities { |
| 466 | if bytes.Equal(x509Cert.RawIssuer, ca) { |
| 467 | chainToSend = &chain |
| 468 | break findCert |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | msg, err = c.readHandshake() |
| 475 | if err != nil { |
| 476 | return err |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | shd, ok := msg.(*serverHelloDoneMsg) |
| 481 | if !ok { |
| 482 | c.sendAlert(alertUnexpectedMessage) |
| 483 | return unexpectedMessageError(shd, msg) |
| 484 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 485 | hs.writeServerHash(shd.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 486 | |
| 487 | // If the server requested a certificate then we have to send a |
| 488 | // Certificate message, even if it's empty because we don't have a |
| 489 | // certificate to send. |
| 490 | if certRequested { |
| 491 | certMsg = new(certificateMsg) |
| 492 | if chainToSend != nil { |
| 493 | certMsg.certificates = chainToSend.Certificate |
| 494 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 495 | hs.writeClientHash(certMsg.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 496 | c.writeRecord(recordTypeHandshake, certMsg.marshal()) |
| 497 | } |
| 498 | |
| 499 | preMasterSecret, ckx, err := keyAgreement.generateClientKeyExchange(c.config, hs.hello, certs[0]) |
| 500 | if err != nil { |
| 501 | c.sendAlert(alertInternalError) |
| 502 | return err |
| 503 | } |
| 504 | if ckx != nil { |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 505 | if c.config.Bugs.EarlyChangeCipherSpec < 2 { |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 506 | hs.writeClientHash(ckx.marshal()) |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 507 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 508 | c.writeRecord(recordTypeHandshake, ckx.marshal()) |
| 509 | } |
| 510 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 511 | if hs.serverHello.extendedMasterSecret && c.vers >= VersionTLS10 { |
| 512 | hs.masterSecret = extendedMasterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.finishedHash) |
| 513 | c.extendedMasterSecret = true |
| 514 | } else { |
| 515 | if c.config.Bugs.RequireExtendedMasterSecret { |
| 516 | return errors.New("tls: extended master secret required but not supported by peer") |
| 517 | } |
| 518 | hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.hello.random, hs.serverHello.random) |
| 519 | } |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 520 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | if chainToSend != nil { |
| 522 | var signed []byte |
| 523 | certVerify := &certificateVerifyMsg{ |
| 524 | hasSignatureAndHash: c.vers >= VersionTLS12, |
| 525 | } |
| 526 | |
| 527 | switch key := c.config.Certificates[0].PrivateKey.(type) { |
| 528 | case *ecdsa.PrivateKey: |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 529 | certVerify.signatureAndHash, err = hs.finishedHash.selectClientCertSignatureAlgorithm(certReq.signatureAndHashes, signatureECDSA) |
| 530 | if err != nil { |
| 531 | break |
| 532 | } |
| 533 | var digest []byte |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 534 | digest, _, err = hs.finishedHash.hashForClientCertificate(certVerify.signatureAndHash, hs.masterSecret) |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 535 | if err != nil { |
| 536 | break |
| 537 | } |
| 538 | var r, s *big.Int |
| 539 | r, s, err = ecdsa.Sign(c.config.rand(), key, digest) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 540 | if err == nil { |
| 541 | signed, err = asn1.Marshal(ecdsaSignature{r, s}) |
| 542 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | case *rsa.PrivateKey: |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 544 | certVerify.signatureAndHash, err = hs.finishedHash.selectClientCertSignatureAlgorithm(certReq.signatureAndHashes, signatureRSA) |
| 545 | if err != nil { |
| 546 | break |
| 547 | } |
| 548 | var digest []byte |
| 549 | var hashFunc crypto.Hash |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 550 | digest, hashFunc, err = hs.finishedHash.hashForClientCertificate(certVerify.signatureAndHash, hs.masterSecret) |
David Benjamin | de620d9 | 2014-07-18 15:03:41 -0400 | [diff] [blame] | 551 | if err != nil { |
| 552 | break |
| 553 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 554 | signed, err = rsa.SignPKCS1v15(c.config.rand(), key, hashFunc, digest) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 555 | default: |
| 556 | err = errors.New("unknown private key type") |
| 557 | } |
| 558 | if err != nil { |
| 559 | c.sendAlert(alertInternalError) |
| 560 | return errors.New("tls: failed to sign handshake with client certificate: " + err.Error()) |
| 561 | } |
| 562 | certVerify.signature = signed |
| 563 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 564 | hs.writeClientHash(certVerify.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 565 | c.writeRecord(recordTypeHandshake, certVerify.marshal()) |
| 566 | } |
| 567 | |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 568 | hs.finishedHash.discardHandshakeBuffer() |
| 569 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 570 | return nil |
| 571 | } |
| 572 | |
| 573 | func (hs *clientHandshakeState) establishKeys() error { |
| 574 | c := hs.c |
| 575 | |
| 576 | clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := |
| 577 | keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.hello.random, hs.serverHello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen) |
| 578 | var clientCipher, serverCipher interface{} |
| 579 | var clientHash, serverHash macFunction |
| 580 | if hs.suite.cipher != nil { |
| 581 | clientCipher = hs.suite.cipher(clientKey, clientIV, false /* not for reading */) |
| 582 | clientHash = hs.suite.mac(c.vers, clientMAC) |
| 583 | serverCipher = hs.suite.cipher(serverKey, serverIV, true /* for reading */) |
| 584 | serverHash = hs.suite.mac(c.vers, serverMAC) |
| 585 | } else { |
| 586 | clientCipher = hs.suite.aead(clientKey, clientIV) |
| 587 | serverCipher = hs.suite.aead(serverKey, serverIV) |
| 588 | } |
| 589 | |
| 590 | c.in.prepareCipherSpec(c.vers, serverCipher, serverHash) |
| 591 | c.out.prepareCipherSpec(c.vers, clientCipher, clientHash) |
| 592 | return nil |
| 593 | } |
| 594 | |
| 595 | func (hs *clientHandshakeState) serverResumedSession() bool { |
| 596 | // If the server responded with the same sessionId then it means the |
| 597 | // sessionTicket is being used to resume a TLS session. |
| 598 | return hs.session != nil && hs.hello.sessionId != nil && |
| 599 | bytes.Equal(hs.serverHello.sessionId, hs.hello.sessionId) |
| 600 | } |
| 601 | |
| 602 | func (hs *clientHandshakeState) processServerHello() (bool, error) { |
| 603 | c := hs.c |
| 604 | |
| 605 | if hs.serverHello.compressionMethod != compressionNone { |
| 606 | c.sendAlert(alertUnexpectedMessage) |
| 607 | return false, errors.New("tls: server selected unsupported compression format") |
| 608 | } |
| 609 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 610 | clientDidNPN := hs.hello.nextProtoNeg |
| 611 | clientDidALPN := len(hs.hello.alpnProtocols) > 0 |
| 612 | serverHasNPN := hs.serverHello.nextProtoNeg |
| 613 | serverHasALPN := len(hs.serverHello.alpnProtocol) > 0 |
| 614 | |
| 615 | if !clientDidNPN && serverHasNPN { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 616 | c.sendAlert(alertHandshakeFailure) |
| 617 | return false, errors.New("server advertised unrequested NPN extension") |
| 618 | } |
| 619 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 620 | if !clientDidALPN && serverHasALPN { |
| 621 | c.sendAlert(alertHandshakeFailure) |
| 622 | return false, errors.New("server advertised unrequested ALPN extension") |
| 623 | } |
| 624 | |
| 625 | if serverHasNPN && serverHasALPN { |
| 626 | c.sendAlert(alertHandshakeFailure) |
| 627 | return false, errors.New("server advertised both NPN and ALPN extensions") |
| 628 | } |
| 629 | |
| 630 | if serverHasALPN { |
| 631 | c.clientProtocol = hs.serverHello.alpnProtocol |
| 632 | c.clientProtocolFallback = false |
David Benjamin | fc7b086 | 2014-09-06 13:21:53 -0400 | [diff] [blame] | 633 | c.usedALPN = true |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 634 | } |
| 635 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 636 | if !hs.hello.channelIDSupported && hs.serverHello.channelIDRequested { |
| 637 | c.sendAlert(alertHandshakeFailure) |
| 638 | return false, errors.New("server advertised unrequested Channel ID extension") |
| 639 | } |
| 640 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 641 | if hs.serverResumedSession() { |
| 642 | // Restore masterSecret and peerCerts from previous state |
| 643 | hs.masterSecret = hs.session.masterSecret |
| 644 | c.peerCertificates = hs.session.serverCertificates |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 645 | c.extendedMasterSecret = hs.session.extendedMasterSecret |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 646 | hs.finishedHash.discardHandshakeBuffer() |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 647 | return true, nil |
| 648 | } |
| 649 | return false, nil |
| 650 | } |
| 651 | |
| 652 | func (hs *clientHandshakeState) readFinished() error { |
| 653 | c := hs.c |
| 654 | |
| 655 | c.readRecord(recordTypeChangeCipherSpec) |
| 656 | if err := c.in.error(); err != nil { |
| 657 | return err |
| 658 | } |
| 659 | |
| 660 | msg, err := c.readHandshake() |
| 661 | if err != nil { |
| 662 | return err |
| 663 | } |
| 664 | serverFinished, ok := msg.(*finishedMsg) |
| 665 | if !ok { |
| 666 | c.sendAlert(alertUnexpectedMessage) |
| 667 | return unexpectedMessageError(serverFinished, msg) |
| 668 | } |
| 669 | |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 670 | if c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 671 | verify := hs.finishedHash.serverSum(hs.masterSecret) |
| 672 | if len(verify) != len(serverFinished.verifyData) || |
| 673 | subtle.ConstantTimeCompare(verify, serverFinished.verifyData) != 1 { |
| 674 | c.sendAlert(alertHandshakeFailure) |
| 675 | return errors.New("tls: server's Finished message was incorrect") |
| 676 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 677 | } |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 678 | hs.writeServerHash(serverFinished.marshal()) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 679 | return nil |
| 680 | } |
| 681 | |
| 682 | func (hs *clientHandshakeState) readSessionTicket() error { |
| 683 | if !hs.serverHello.ticketSupported { |
| 684 | return nil |
| 685 | } |
| 686 | |
| 687 | c := hs.c |
| 688 | msg, err := c.readHandshake() |
| 689 | if err != nil { |
| 690 | return err |
| 691 | } |
| 692 | sessionTicketMsg, ok := msg.(*newSessionTicketMsg) |
| 693 | if !ok { |
| 694 | c.sendAlert(alertUnexpectedMessage) |
| 695 | return unexpectedMessageError(sessionTicketMsg, msg) |
| 696 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 697 | |
| 698 | hs.session = &ClientSessionState{ |
| 699 | sessionTicket: sessionTicketMsg.ticket, |
| 700 | vers: c.vers, |
| 701 | cipherSuite: hs.suite.id, |
| 702 | masterSecret: hs.masterSecret, |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 703 | handshakeHash: hs.finishedHash.server.Sum(nil), |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 704 | serverCertificates: c.peerCertificates, |
| 705 | } |
| 706 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 707 | hs.writeServerHash(sessionTicketMsg.marshal()) |
| 708 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 709 | return nil |
| 710 | } |
| 711 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 712 | func (hs *clientHandshakeState) sendFinished(isResume bool) error { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 713 | c := hs.c |
| 714 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 715 | var postCCSBytes []byte |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 716 | seqno := hs.c.sendHandshakeSeq |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 717 | if hs.serverHello.nextProtoNeg { |
| 718 | nextProto := new(nextProtoMsg) |
| 719 | proto, fallback := mutualProtocol(c.config.NextProtos, hs.serverHello.nextProtos) |
| 720 | nextProto.proto = proto |
| 721 | c.clientProtocol = proto |
| 722 | c.clientProtocolFallback = fallback |
| 723 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 724 | nextProtoBytes := nextProto.marshal() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 725 | hs.writeHash(nextProtoBytes, seqno) |
| 726 | seqno++ |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 727 | postCCSBytes = append(postCCSBytes, nextProtoBytes...) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 728 | } |
| 729 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 730 | if hs.serverHello.channelIDRequested { |
| 731 | encryptedExtensions := new(encryptedExtensionsMsg) |
| 732 | if c.config.ChannelID.Curve != elliptic.P256() { |
| 733 | return fmt.Errorf("tls: Channel ID is not on P-256.") |
| 734 | } |
| 735 | var resumeHash []byte |
| 736 | if isResume { |
| 737 | resumeHash = hs.session.handshakeHash |
| 738 | } |
| 739 | r, s, err := ecdsa.Sign(c.config.rand(), c.config.ChannelID, hs.finishedHash.hashForChannelID(resumeHash)) |
| 740 | if err != nil { |
| 741 | return err |
| 742 | } |
| 743 | channelID := make([]byte, 128) |
| 744 | writeIntPadded(channelID[0:32], c.config.ChannelID.X) |
| 745 | writeIntPadded(channelID[32:64], c.config.ChannelID.Y) |
| 746 | writeIntPadded(channelID[64:96], r) |
| 747 | writeIntPadded(channelID[96:128], s) |
| 748 | encryptedExtensions.channelID = channelID |
| 749 | |
| 750 | c.channelID = &c.config.ChannelID.PublicKey |
| 751 | |
| 752 | encryptedExtensionsBytes := encryptedExtensions.marshal() |
| 753 | hs.writeHash(encryptedExtensionsBytes, seqno) |
| 754 | seqno++ |
| 755 | postCCSBytes = append(postCCSBytes, encryptedExtensionsBytes...) |
| 756 | } |
| 757 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 758 | finished := new(finishedMsg) |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 759 | if c.config.Bugs.EarlyChangeCipherSpec == 2 { |
| 760 | finished.verifyData = hs.finishedHash.clientSum(nil) |
| 761 | } else { |
| 762 | finished.verifyData = hs.finishedHash.clientSum(hs.masterSecret) |
| 763 | } |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 764 | finishedBytes := finished.marshal() |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 765 | hs.writeHash(finishedBytes, seqno) |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 766 | postCCSBytes = append(postCCSBytes, finishedBytes...) |
| 767 | |
| 768 | if c.config.Bugs.FragmentAcrossChangeCipherSpec { |
| 769 | c.writeRecord(recordTypeHandshake, postCCSBytes[:5]) |
| 770 | postCCSBytes = postCCSBytes[5:] |
| 771 | } |
| 772 | |
| 773 | if !c.config.Bugs.SkipChangeCipherSpec && |
| 774 | c.config.Bugs.EarlyChangeCipherSpec == 0 { |
| 775 | c.writeRecord(recordTypeChangeCipherSpec, []byte{1}) |
| 776 | } |
| 777 | |
| 778 | c.writeRecord(recordTypeHandshake, postCCSBytes) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 779 | return nil |
| 780 | } |
| 781 | |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 782 | func (hs *clientHandshakeState) writeClientHash(msg []byte) { |
| 783 | // writeClientHash is called before writeRecord. |
| 784 | hs.writeHash(msg, hs.c.sendHandshakeSeq) |
| 785 | } |
| 786 | |
| 787 | func (hs *clientHandshakeState) writeServerHash(msg []byte) { |
| 788 | // writeServerHash is called after readHandshake. |
| 789 | hs.writeHash(msg, hs.c.recvHandshakeSeq-1) |
| 790 | } |
| 791 | |
| 792 | func (hs *clientHandshakeState) writeHash(msg []byte, seqno uint16) { |
| 793 | if hs.c.isDTLS { |
| 794 | // This is somewhat hacky. DTLS hashes a slightly different format. |
| 795 | // First, the TLS header. |
| 796 | hs.finishedHash.Write(msg[:4]) |
| 797 | // Then the sequence number and reassembled fragment offset (always 0). |
| 798 | hs.finishedHash.Write([]byte{byte(seqno >> 8), byte(seqno), 0, 0, 0}) |
| 799 | // Then the reassembled fragment (always equal to the message length). |
| 800 | hs.finishedHash.Write(msg[1:4]) |
| 801 | // And then the message body. |
| 802 | hs.finishedHash.Write(msg[4:]) |
| 803 | } else { |
| 804 | hs.finishedHash.Write(msg) |
| 805 | } |
| 806 | } |
| 807 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 808 | // clientSessionCacheKey returns a key used to cache sessionTickets that could |
| 809 | // be used to resume previously negotiated TLS sessions with a server. |
| 810 | func clientSessionCacheKey(serverAddr net.Addr, config *Config) string { |
| 811 | if len(config.ServerName) > 0 { |
| 812 | return config.ServerName |
| 813 | } |
| 814 | return serverAddr.String() |
| 815 | } |
| 816 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 817 | // mutualProtocol finds the mutual Next Protocol Negotiation or ALPN protocol |
| 818 | // given list of possible protocols and a list of the preference order. The |
| 819 | // 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] | 820 | // indicating if the fallback case was reached. |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 821 | func mutualProtocol(protos, preferenceProtos []string) (string, bool) { |
| 822 | for _, s := range preferenceProtos { |
| 823 | for _, c := range protos { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 824 | if s == c { |
| 825 | return s, false |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
David Benjamin | fa055a2 | 2014-09-15 16:51:51 -0400 | [diff] [blame] | 830 | return protos[0], true |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 831 | } |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 832 | |
| 833 | // writeIntPadded writes x into b, padded up with leading zeros as |
| 834 | // needed. |
| 835 | func writeIntPadded(b []byte, x *big.Int) { |
| 836 | for i := range b { |
| 837 | b[i] = 0 |
| 838 | } |
| 839 | xb := x.Bytes() |
| 840 | copy(b[len(b)-len(xb):], xb) |
| 841 | } |