blob: f14f4e9010c07bc6612d1560e33e41a444e13491 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001// 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
5package main
6
7import (
8 "container/list"
9 "crypto"
10 "crypto/rand"
11 "crypto/x509"
12 "fmt"
13 "io"
14 "math/big"
15 "strings"
16 "sync"
17 "time"
18)
19
20const (
21 VersionSSL30 = 0x0300
22 VersionTLS10 = 0x0301
23 VersionTLS11 = 0x0302
24 VersionTLS12 = 0x0303
25)
26
27const (
David Benjamin83c0bc92014-08-04 01:23:53 -040028 maxPlaintext = 16384 // maximum plaintext payload length
29 maxCiphertext = 16384 + 2048 // maximum ciphertext payload length
30 tlsRecordHeaderLen = 5 // record header length
31 dtlsRecordHeaderLen = 13
32 maxHandshake = 65536 // maximum handshake we support (protocol max is 16 MB)
Adam Langley95c29f32014-06-20 12:00:00 -070033
34 minVersion = VersionSSL30
35 maxVersion = VersionTLS12
36)
37
38// TLS record types.
39type recordType uint8
40
41const (
42 recordTypeChangeCipherSpec recordType = 20
43 recordTypeAlert recordType = 21
44 recordTypeHandshake recordType = 22
45 recordTypeApplicationData recordType = 23
46)
47
48// TLS handshake message types.
49const (
50 typeClientHello uint8 = 1
51 typeServerHello uint8 = 2
David Benjamin83c0bc92014-08-04 01:23:53 -040052 typeHelloVerifyRequest uint8 = 3
Adam Langley95c29f32014-06-20 12:00:00 -070053 typeNewSessionTicket uint8 = 4
54 typeCertificate uint8 = 11
55 typeServerKeyExchange uint8 = 12
56 typeCertificateRequest uint8 = 13
57 typeServerHelloDone uint8 = 14
58 typeCertificateVerify uint8 = 15
59 typeClientKeyExchange uint8 = 16
60 typeFinished uint8 = 20
61 typeCertificateStatus uint8 = 22
62 typeNextProtocol uint8 = 67 // Not IANA assigned
63)
64
65// TLS compression types.
66const (
67 compressionNone uint8 = 0
68)
69
70// TLS extension numbers
71const (
72 extensionServerName uint16 = 0
73 extensionStatusRequest uint16 = 5
74 extensionSupportedCurves uint16 = 10
75 extensionSupportedPoints uint16 = 11
76 extensionSignatureAlgorithms uint16 = 13
77 extensionSessionTicket uint16 = 35
78 extensionNextProtoNeg uint16 = 13172 // not IANA assigned
79 extensionRenegotiationInfo uint16 = 0xff01
80)
81
82// TLS signaling cipher suite values
83const (
84 scsvRenegotiation uint16 = 0x00ff
85)
86
87// CurveID is the type of a TLS identifier for an elliptic curve. See
88// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8
89type CurveID uint16
90
91const (
92 CurveP256 CurveID = 23
93 CurveP384 CurveID = 24
94 CurveP521 CurveID = 25
95)
96
97// TLS Elliptic Curve Point Formats
98// http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-9
99const (
100 pointFormatUncompressed uint8 = 0
101)
102
103// TLS CertificateStatusType (RFC 3546)
104const (
105 statusTypeOCSP uint8 = 1
106)
107
108// Certificate types (for certificateRequestMsg)
109const (
David Benjamin7b030512014-07-08 17:30:11 -0400110 CertTypeRSASign = 1 // A certificate containing an RSA key
111 CertTypeDSSSign = 2 // A certificate containing a DSA key
112 CertTypeRSAFixedDH = 3 // A certificate containing a static DH key
113 CertTypeDSSFixedDH = 4 // A certificate containing a static DH key
Adam Langley95c29f32014-06-20 12:00:00 -0700114
115 // See RFC4492 sections 3 and 5.5.
David Benjamin7b030512014-07-08 17:30:11 -0400116 CertTypeECDSASign = 64 // A certificate containing an ECDSA-capable public key, signed with ECDSA.
117 CertTypeRSAFixedECDH = 65 // A certificate containing an ECDH-capable public key, signed with RSA.
118 CertTypeECDSAFixedECDH = 66 // A certificate containing an ECDH-capable public key, signed with ECDSA.
Adam Langley95c29f32014-06-20 12:00:00 -0700119
120 // Rest of these are reserved by the TLS spec
121)
122
123// Hash functions for TLS 1.2 (See RFC 5246, section A.4.1)
124const (
125 hashSHA1 uint8 = 2
126 hashSHA256 uint8 = 4
127)
128
129// Signature algorithms for TLS 1.2 (See RFC 5246, section A.4.1)
130const (
131 signatureRSA uint8 = 1
132 signatureECDSA uint8 = 3
133)
134
135// signatureAndHash mirrors the TLS 1.2, SignatureAndHashAlgorithm struct. See
136// RFC 5246, section A.4.1.
137type signatureAndHash struct {
138 hash, signature uint8
139}
140
141// supportedSKXSignatureAlgorithms contains the signature and hash algorithms
142// that the code advertises as supported in a TLS 1.2 ClientHello.
143var supportedSKXSignatureAlgorithms = []signatureAndHash{
144 {hashSHA256, signatureRSA},
145 {hashSHA256, signatureECDSA},
146 {hashSHA1, signatureRSA},
147 {hashSHA1, signatureECDSA},
148}
149
150// supportedClientCertSignatureAlgorithms contains the signature and hash
151// algorithms that the code advertises as supported in a TLS 1.2
152// CertificateRequest.
153var supportedClientCertSignatureAlgorithms = []signatureAndHash{
154 {hashSHA256, signatureRSA},
155 {hashSHA256, signatureECDSA},
156}
157
158// ConnectionState records basic TLS details about the connection.
159type ConnectionState struct {
160 Version uint16 // TLS version used by the connection (e.g. VersionTLS12)
161 HandshakeComplete bool // TLS handshake is complete
162 DidResume bool // connection resumes a previous TLS connection
163 CipherSuite uint16 // cipher suite in use (TLS_RSA_WITH_RC4_128_SHA, ...)
164 NegotiatedProtocol string // negotiated next protocol (from Config.NextProtos)
165 NegotiatedProtocolIsMutual bool // negotiated protocol was advertised by server
166 ServerName string // server name requested by client, if any (server side only)
167 PeerCertificates []*x509.Certificate // certificate chain presented by remote peer
168 VerifiedChains [][]*x509.Certificate // verified chains built from PeerCertificates
169}
170
171// ClientAuthType declares the policy the server will follow for
172// TLS Client Authentication.
173type ClientAuthType int
174
175const (
176 NoClientCert ClientAuthType = iota
177 RequestClientCert
178 RequireAnyClientCert
179 VerifyClientCertIfGiven
180 RequireAndVerifyClientCert
181)
182
183// ClientSessionState contains the state needed by clients to resume TLS
184// sessions.
185type ClientSessionState struct {
186 sessionTicket []uint8 // Encrypted ticket used for session resumption with server
187 vers uint16 // SSL/TLS version negotiated for the session
188 cipherSuite uint16 // Ciphersuite negotiated for the session
189 masterSecret []byte // MasterSecret generated by client on a full handshake
190 serverCertificates []*x509.Certificate // Certificate chain presented by the server
191}
192
193// ClientSessionCache is a cache of ClientSessionState objects that can be used
194// by a client to resume a TLS session with a given server. ClientSessionCache
195// implementations should expect to be called concurrently from different
196// goroutines.
197type ClientSessionCache interface {
198 // Get searches for a ClientSessionState associated with the given key.
199 // On return, ok is true if one was found.
200 Get(sessionKey string) (session *ClientSessionState, ok bool)
201
202 // Put adds the ClientSessionState to the cache with the given key.
203 Put(sessionKey string, cs *ClientSessionState)
204}
205
206// A Config structure is used to configure a TLS client or server.
207// After one has been passed to a TLS function it must not be
208// modified. A Config may be reused; the tls package will also not
209// modify it.
210type Config struct {
211 // Rand provides the source of entropy for nonces and RSA blinding.
212 // If Rand is nil, TLS uses the cryptographic random reader in package
213 // crypto/rand.
214 // The Reader must be safe for use by multiple goroutines.
215 Rand io.Reader
216
217 // Time returns the current time as the number of seconds since the epoch.
218 // If Time is nil, TLS uses time.Now.
219 Time func() time.Time
220
221 // Certificates contains one or more certificate chains
222 // to present to the other side of the connection.
223 // Server configurations must include at least one certificate.
224 Certificates []Certificate
225
226 // NameToCertificate maps from a certificate name to an element of
227 // Certificates. Note that a certificate name can be of the form
228 // '*.example.com' and so doesn't have to be a domain name as such.
229 // See Config.BuildNameToCertificate
230 // The nil value causes the first element of Certificates to be used
231 // for all connections.
232 NameToCertificate map[string]*Certificate
233
234 // RootCAs defines the set of root certificate authorities
235 // that clients use when verifying server certificates.
236 // If RootCAs is nil, TLS uses the host's root CA set.
237 RootCAs *x509.CertPool
238
239 // NextProtos is a list of supported, application level protocols.
240 NextProtos []string
241
242 // ServerName is used to verify the hostname on the returned
243 // certificates unless InsecureSkipVerify is given. It is also included
244 // in the client's handshake to support virtual hosting.
245 ServerName string
246
247 // ClientAuth determines the server's policy for
248 // TLS Client Authentication. The default is NoClientCert.
249 ClientAuth ClientAuthType
250
251 // ClientCAs defines the set of root certificate authorities
252 // that servers use if required to verify a client certificate
253 // by the policy in ClientAuth.
254 ClientCAs *x509.CertPool
255
David Benjamin7b030512014-07-08 17:30:11 -0400256 // ClientCertificateTypes defines the set of allowed client certificate
257 // types. The default is CertTypeRSASign and CertTypeECDSASign.
258 ClientCertificateTypes []byte
259
Adam Langley95c29f32014-06-20 12:00:00 -0700260 // InsecureSkipVerify controls whether a client verifies the
261 // server's certificate chain and host name.
262 // If InsecureSkipVerify is true, TLS accepts any certificate
263 // presented by the server and any host name in that certificate.
264 // In this mode, TLS is susceptible to man-in-the-middle attacks.
265 // This should be used only for testing.
266 InsecureSkipVerify bool
267
268 // CipherSuites is a list of supported cipher suites. If CipherSuites
269 // is nil, TLS uses a list of suites supported by the implementation.
270 CipherSuites []uint16
271
272 // PreferServerCipherSuites controls whether the server selects the
273 // client's most preferred ciphersuite, or the server's most preferred
274 // ciphersuite. If true then the server's preference, as expressed in
275 // the order of elements in CipherSuites, is used.
276 PreferServerCipherSuites bool
277
278 // SessionTicketsDisabled may be set to true to disable session ticket
279 // (resumption) support.
280 SessionTicketsDisabled bool
281
282 // SessionTicketKey is used by TLS servers to provide session
283 // resumption. See RFC 5077. If zero, it will be filled with
284 // random data before the first server handshake.
285 //
286 // If multiple servers are terminating connections for the same host
287 // they should all have the same SessionTicketKey. If the
288 // SessionTicketKey leaks, previously recorded and future TLS
289 // connections using that key are compromised.
290 SessionTicketKey [32]byte
291
292 // SessionCache is a cache of ClientSessionState entries for TLS session
293 // resumption.
294 ClientSessionCache ClientSessionCache
295
296 // MinVersion contains the minimum SSL/TLS version that is acceptable.
297 // If zero, then SSLv3 is taken as the minimum.
298 MinVersion uint16
299
300 // MaxVersion contains the maximum SSL/TLS version that is acceptable.
301 // If zero, then the maximum version supported by this package is used,
302 // which is currently TLS 1.2.
303 MaxVersion uint16
304
305 // CurvePreferences contains the elliptic curves that will be used in
306 // an ECDHE handshake, in preference order. If empty, the default will
307 // be used.
308 CurvePreferences []CurveID
309
310 // Bugs specifies optional misbehaviour to be used for testing other
311 // implementations.
312 Bugs ProtocolBugs
313
314 serverInitOnce sync.Once // guards calling (*Config).serverInit
315}
316
317type BadValue int
318
319const (
320 BadValueNone BadValue = iota
321 BadValueNegative
322 BadValueZero
323 BadValueLimit
324 BadValueLarge
325 NumBadValues
326)
327
328type ProtocolBugs struct {
329 // InvalidSKXSignature specifies that the signature in a
330 // ServerKeyExchange message should be invalid.
331 InvalidSKXSignature bool
332
333 // InvalidSKXCurve causes the curve ID in the ServerKeyExchange message
334 // to be wrong.
335 InvalidSKXCurve bool
336
337 // BadECDSAR controls ways in which the 'r' value of an ECDSA signature
338 // can be invalid.
339 BadECDSAR BadValue
340 BadECDSAS BadValue
Adam Langley80842bd2014-06-20 12:00:00 -0700341
342 // MaxPadding causes CBC records to have the maximum possible padding.
343 MaxPadding bool
344 // PaddingFirstByteBad causes the first byte of the padding to be
345 // incorrect.
346 PaddingFirstByteBad bool
347 // PaddingFirstByteBadIf255 causes the first byte of padding to be
348 // incorrect if there's a maximum amount of padding (i.e. 255 bytes).
349 PaddingFirstByteBadIf255 bool
Adam Langleyac61fa32014-06-23 12:03:11 -0700350
351 // FailIfNotFallbackSCSV causes a server handshake to fail if the
352 // client doesn't send the fallback SCSV value.
353 FailIfNotFallbackSCSV bool
David Benjamin35a7a442014-07-05 00:23:20 -0400354
355 // DuplicateExtension causes an extra empty extension of bogus type to
356 // be emitted in either the ClientHello or the ServerHello.
357 DuplicateExtension bool
David Benjamin1c375dd2014-07-12 00:48:23 -0400358
359 // UnauthenticatedECDH causes the server to pretend ECDHE_RSA
360 // and ECDHE_ECDSA cipher suites are actually ECDH_anon. No
361 // Certificate message is sent and no signature is added to
362 // ServerKeyExchange.
363 UnauthenticatedECDH bool
David Benjamin9c651c92014-07-12 13:27:45 -0400364
365 // SkipServerKeyExchange causes the server to skip sending
366 // ServerKeyExchange messages.
367 SkipServerKeyExchange bool
David Benjamina0e52232014-07-19 17:39:58 -0400368
369 // SkipChangeCipherSpec causes the implementation to skip
370 // sending the ChangeCipherSpec message (and adjusting cipher
371 // state accordingly for the Finished message).
372 SkipChangeCipherSpec bool
David Benjaminf3ec83d2014-07-21 22:42:34 -0400373
374 // EarlyChangeCipherSpec causes the client to send an early
375 // ChangeCipherSpec message before the ClientKeyExchange. A value of
376 // zero disables this behavior. One and two configure variants for 0.9.8
377 // and 1.0.1 modes, respectively.
378 EarlyChangeCipherSpec int
David Benjamind23f4122014-07-23 15:09:48 -0400379
David Benjamin86271ee2014-07-21 16:14:03 -0400380 // FragmentAcrossChangeCipherSpec causes the implementation to fragment
381 // the Finished (or NextProto) message around the ChangeCipherSpec
382 // messages.
383 FragmentAcrossChangeCipherSpec bool
384
David Benjamind23f4122014-07-23 15:09:48 -0400385 // SkipNewSessionTicket causes the server to skip sending the
386 // NewSessionTicket message despite promising to in ServerHello.
387 SkipNewSessionTicket bool
David Benjamind86c7672014-08-02 04:07:12 -0400388
389 // SendV2ClientHello causes the client to send a V2ClientHello
390 // instead of a normal ClientHello.
391 SendV2ClientHello bool
David Benjaminbef270a2014-08-02 04:22:02 -0400392
393 // SendFallbackSCSV causes the client to include
394 // TLS_FALLBACK_SCSV in the ClientHello.
395 SendFallbackSCSV bool
David Benjamin43ec06f2014-08-05 02:28:57 -0400396
397 // MaxHandshakeRecordLength, if non-zero, is the maximum size of a
David Benjamin98214542014-08-07 18:02:39 -0400398 // handshake record. Handshake messages will be split into multiple
399 // records at the specified size, except that the client_version will
400 // never be fragmented.
David Benjamin43ec06f2014-08-05 02:28:57 -0400401 MaxHandshakeRecordLength int
David Benjamina8e3e0e2014-08-06 22:11:10 -0400402
David Benjamin98214542014-08-07 18:02:39 -0400403 // FragmentClientVersion will allow MaxHandshakeRecordLength to apply to
404 // the first 6 bytes of the ClientHello.
405 FragmentClientVersion bool
406
David Benjamina8e3e0e2014-08-06 22:11:10 -0400407 // RsaClientKeyExchangeVersion, if non-zero, causes the client to send a
408 // ClientKeyExchange with the specified version rather than the
409 // client_version when performing the RSA key exchange.
410 RsaClientKeyExchangeVersion uint16
David Benjaminbed9aae2014-08-07 19:13:38 -0400411
412 // RenewTicketOnResume causes the server to renew the session ticket and
413 // send a NewSessionTicket message during an abbreviated handshake.
414 RenewTicketOnResume bool
David Benjamin98e882e2014-08-08 13:24:34 -0400415
416 // SendClientVersion, if non-zero, causes the client to send a different
417 // TLS version in the ClientHello than the maximum supported version.
418 SendClientVersion uint16
David Benjamin83c0bc92014-08-04 01:23:53 -0400419
420 // SkipHelloVerifyRequest causes a DTLS server to skip the
421 // HelloVerifyRequest message.
422 SkipHelloVerifyRequest bool
Adam Langley95c29f32014-06-20 12:00:00 -0700423}
424
425func (c *Config) serverInit() {
426 if c.SessionTicketsDisabled {
427 return
428 }
429
430 // If the key has already been set then we have nothing to do.
431 for _, b := range c.SessionTicketKey {
432 if b != 0 {
433 return
434 }
435 }
436
437 if _, err := io.ReadFull(c.rand(), c.SessionTicketKey[:]); err != nil {
438 c.SessionTicketsDisabled = true
439 }
440}
441
442func (c *Config) rand() io.Reader {
443 r := c.Rand
444 if r == nil {
445 return rand.Reader
446 }
447 return r
448}
449
450func (c *Config) time() time.Time {
451 t := c.Time
452 if t == nil {
453 t = time.Now
454 }
455 return t()
456}
457
458func (c *Config) cipherSuites() []uint16 {
459 s := c.CipherSuites
460 if s == nil {
461 s = defaultCipherSuites()
462 }
463 return s
464}
465
466func (c *Config) minVersion() uint16 {
467 if c == nil || c.MinVersion == 0 {
468 return minVersion
469 }
470 return c.MinVersion
471}
472
473func (c *Config) maxVersion() uint16 {
474 if c == nil || c.MaxVersion == 0 {
475 return maxVersion
476 }
477 return c.MaxVersion
478}
479
480var defaultCurvePreferences = []CurveID{CurveP256, CurveP384, CurveP521}
481
482func (c *Config) curvePreferences() []CurveID {
483 if c == nil || len(c.CurvePreferences) == 0 {
484 return defaultCurvePreferences
485 }
486 return c.CurvePreferences
487}
488
489// mutualVersion returns the protocol version to use given the advertised
490// version of the peer.
491func (c *Config) mutualVersion(vers uint16) (uint16, bool) {
492 minVersion := c.minVersion()
493 maxVersion := c.maxVersion()
494
495 if vers < minVersion {
496 return 0, false
497 }
498 if vers > maxVersion {
499 vers = maxVersion
500 }
501 return vers, true
502}
503
504// getCertificateForName returns the best certificate for the given name,
505// defaulting to the first element of c.Certificates if there are no good
506// options.
507func (c *Config) getCertificateForName(name string) *Certificate {
508 if len(c.Certificates) == 1 || c.NameToCertificate == nil {
509 // There's only one choice, so no point doing any work.
510 return &c.Certificates[0]
511 }
512
513 name = strings.ToLower(name)
514 for len(name) > 0 && name[len(name)-1] == '.' {
515 name = name[:len(name)-1]
516 }
517
518 if cert, ok := c.NameToCertificate[name]; ok {
519 return cert
520 }
521
522 // try replacing labels in the name with wildcards until we get a
523 // match.
524 labels := strings.Split(name, ".")
525 for i := range labels {
526 labels[i] = "*"
527 candidate := strings.Join(labels, ".")
528 if cert, ok := c.NameToCertificate[candidate]; ok {
529 return cert
530 }
531 }
532
533 // If nothing matches, return the first certificate.
534 return &c.Certificates[0]
535}
536
537// BuildNameToCertificate parses c.Certificates and builds c.NameToCertificate
538// from the CommonName and SubjectAlternateName fields of each of the leaf
539// certificates.
540func (c *Config) BuildNameToCertificate() {
541 c.NameToCertificate = make(map[string]*Certificate)
542 for i := range c.Certificates {
543 cert := &c.Certificates[i]
544 x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
545 if err != nil {
546 continue
547 }
548 if len(x509Cert.Subject.CommonName) > 0 {
549 c.NameToCertificate[x509Cert.Subject.CommonName] = cert
550 }
551 for _, san := range x509Cert.DNSNames {
552 c.NameToCertificate[san] = cert
553 }
554 }
555}
556
557// A Certificate is a chain of one or more certificates, leaf first.
558type Certificate struct {
559 Certificate [][]byte
560 PrivateKey crypto.PrivateKey // supported types: *rsa.PrivateKey, *ecdsa.PrivateKey
561 // OCSPStaple contains an optional OCSP response which will be served
562 // to clients that request it.
563 OCSPStaple []byte
564 // Leaf is the parsed form of the leaf certificate, which may be
565 // initialized using x509.ParseCertificate to reduce per-handshake
566 // processing for TLS clients doing client authentication. If nil, the
567 // leaf certificate will be parsed as needed.
568 Leaf *x509.Certificate
569}
570
571// A TLS record.
572type record struct {
573 contentType recordType
574 major, minor uint8
575 payload []byte
576}
577
578type handshakeMessage interface {
579 marshal() []byte
580 unmarshal([]byte) bool
581}
582
583// lruSessionCache is a ClientSessionCache implementation that uses an LRU
584// caching strategy.
585type lruSessionCache struct {
586 sync.Mutex
587
588 m map[string]*list.Element
589 q *list.List
590 capacity int
591}
592
593type lruSessionCacheEntry struct {
594 sessionKey string
595 state *ClientSessionState
596}
597
598// NewLRUClientSessionCache returns a ClientSessionCache with the given
599// capacity that uses an LRU strategy. If capacity is < 1, a default capacity
600// is used instead.
601func NewLRUClientSessionCache(capacity int) ClientSessionCache {
602 const defaultSessionCacheCapacity = 64
603
604 if capacity < 1 {
605 capacity = defaultSessionCacheCapacity
606 }
607 return &lruSessionCache{
608 m: make(map[string]*list.Element),
609 q: list.New(),
610 capacity: capacity,
611 }
612}
613
614// Put adds the provided (sessionKey, cs) pair to the cache.
615func (c *lruSessionCache) Put(sessionKey string, cs *ClientSessionState) {
616 c.Lock()
617 defer c.Unlock()
618
619 if elem, ok := c.m[sessionKey]; ok {
620 entry := elem.Value.(*lruSessionCacheEntry)
621 entry.state = cs
622 c.q.MoveToFront(elem)
623 return
624 }
625
626 if c.q.Len() < c.capacity {
627 entry := &lruSessionCacheEntry{sessionKey, cs}
628 c.m[sessionKey] = c.q.PushFront(entry)
629 return
630 }
631
632 elem := c.q.Back()
633 entry := elem.Value.(*lruSessionCacheEntry)
634 delete(c.m, entry.sessionKey)
635 entry.sessionKey = sessionKey
636 entry.state = cs
637 c.q.MoveToFront(elem)
638 c.m[sessionKey] = elem
639}
640
641// Get returns the ClientSessionState value associated with a given key. It
642// returns (nil, false) if no value is found.
643func (c *lruSessionCache) Get(sessionKey string) (*ClientSessionState, bool) {
644 c.Lock()
645 defer c.Unlock()
646
647 if elem, ok := c.m[sessionKey]; ok {
648 c.q.MoveToFront(elem)
649 return elem.Value.(*lruSessionCacheEntry).state, true
650 }
651 return nil, false
652}
653
654// TODO(jsing): Make these available to both crypto/x509 and crypto/tls.
655type dsaSignature struct {
656 R, S *big.Int
657}
658
659type ecdsaSignature dsaSignature
660
661var emptyConfig Config
662
663func defaultConfig() *Config {
664 return &emptyConfig
665}
666
667var (
668 once sync.Once
669 varDefaultCipherSuites []uint16
670)
671
672func defaultCipherSuites() []uint16 {
673 once.Do(initDefaultCipherSuites)
674 return varDefaultCipherSuites
675}
676
677func initDefaultCipherSuites() {
678 varDefaultCipherSuites = make([]uint16, len(cipherSuites))
679 for i, suite := range cipherSuites {
680 varDefaultCipherSuites[i] = suite.id
681 }
682}
683
684func unexpectedMessageError(wanted, got interface{}) error {
685 return fmt.Errorf("tls: received unexpected handshake message of type %T when waiting for %T", got, wanted)
686}