Source file src/pkg/crypto/tls/common.go
1
2
3
4
5 package tls
6
7 import (
8 "container/list"
9 "crypto"
10 "crypto/rand"
11 "crypto/sha512"
12 "crypto/x509"
13 "errors"
14 "fmt"
15 "internal/cpu"
16 "io"
17 "math/big"
18 "net"
19 "os"
20 "strings"
21 "sync"
22 "time"
23 )
24
25 const (
26 VersionTLS10 = 0x0301
27 VersionTLS11 = 0x0302
28 VersionTLS12 = 0x0303
29 VersionTLS13 = 0x0304
30
31
32
33 VersionSSL30 = 0x0300
34 )
35
36 const (
37 maxPlaintext = 16384
38 maxCiphertext = 16384 + 2048
39 maxCiphertextTLS13 = 16384 + 256
40 recordHeaderLen = 5
41 maxHandshake = 65536
42 maxUselessRecords = 16
43 )
44
45
46 type recordType uint8
47
48 const (
49 recordTypeChangeCipherSpec recordType = 20
50 recordTypeAlert recordType = 21
51 recordTypeHandshake recordType = 22
52 recordTypeApplicationData recordType = 23
53 )
54
55
56 const (
57 typeHelloRequest uint8 = 0
58 typeClientHello uint8 = 1
59 typeServerHello uint8 = 2
60 typeNewSessionTicket uint8 = 4
61 typeEndOfEarlyData uint8 = 5
62 typeEncryptedExtensions uint8 = 8
63 typeCertificate uint8 = 11
64 typeServerKeyExchange uint8 = 12
65 typeCertificateRequest uint8 = 13
66 typeServerHelloDone uint8 = 14
67 typeCertificateVerify uint8 = 15
68 typeClientKeyExchange uint8 = 16
69 typeFinished uint8 = 20
70 typeCertificateStatus uint8 = 22
71 typeKeyUpdate uint8 = 24
72 typeNextProtocol uint8 = 67
73 typeMessageHash uint8 = 254
74 )
75
76
77 const (
78 compressionNone uint8 = 0
79 )
80
81
82 const (
83 extensionServerName uint16 = 0
84 extensionStatusRequest uint16 = 5
85 extensionSupportedCurves uint16 = 10
86 extensionSupportedPoints uint16 = 11
87 extensionSignatureAlgorithms uint16 = 13
88 extensionALPN uint16 = 16
89 extensionSCT uint16 = 18
90 extensionSessionTicket uint16 = 35
91 extensionPreSharedKey uint16 = 41
92 extensionEarlyData uint16 = 42
93 extensionSupportedVersions uint16 = 43
94 extensionCookie uint16 = 44
95 extensionPSKModes uint16 = 45
96 extensionCertificateAuthorities uint16 = 47
97 extensionSignatureAlgorithmsCert uint16 = 50
98 extensionKeyShare uint16 = 51
99 extensionNextProtoNeg uint16 = 13172
100 extensionRenegotiationInfo uint16 = 0xff01
101 )
102
103
104 const (
105 scsvRenegotiation uint16 = 0x00ff
106 )
107
108
109
110
111
112
113 type CurveID uint16
114
115 const (
116 CurveP256 CurveID = 23
117 CurveP384 CurveID = 24
118 CurveP521 CurveID = 25
119 X25519 CurveID = 29
120 )
121
122
123 type keyShare struct {
124 group CurveID
125 data []byte
126 }
127
128
129 const (
130 pskModePlain uint8 = 0
131 pskModeDHE uint8 = 1
132 )
133
134
135
136 type pskIdentity struct {
137 label []byte
138 obfuscatedTicketAge uint32
139 }
140
141
142
143 const (
144 pointFormatUncompressed uint8 = 0
145 )
146
147
148 const (
149 statusTypeOCSP uint8 = 1
150 )
151
152
153 const (
154 certTypeRSASign = 1
155 certTypeECDSASign = 64
156 )
157
158
159
160 const (
161 signaturePKCS1v15 uint8 = iota + 225
162 signatureRSAPSS
163 signatureECDSA
164 signatureEd25519
165 )
166
167
168
169
170 var directSigning crypto.Hash = 0
171
172
173
174
175
176 var supportedSignatureAlgorithms = []SignatureScheme{
177 PSSWithSHA256,
178 ECDSAWithP256AndSHA256,
179 Ed25519,
180 PSSWithSHA384,
181 PSSWithSHA512,
182 PKCS1WithSHA256,
183 PKCS1WithSHA384,
184 PKCS1WithSHA512,
185 ECDSAWithP384AndSHA384,
186 ECDSAWithP521AndSHA512,
187 PKCS1WithSHA1,
188 ECDSAWithSHA1,
189 }
190
191
192
193
194 var supportedSignatureAlgorithmsTLS12 = []SignatureScheme{
195 PKCS1WithSHA256,
196 ECDSAWithP256AndSHA256,
197 Ed25519,
198 PKCS1WithSHA384,
199 PKCS1WithSHA512,
200 ECDSAWithP384AndSHA384,
201 ECDSAWithP521AndSHA512,
202 PKCS1WithSHA1,
203 ECDSAWithSHA1,
204 }
205
206
207
208 var helloRetryRequestRandom = []byte{
209 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
210 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
211 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
212 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C,
213 }
214
215 const (
216
217
218
219 downgradeCanaryTLS12 = "DOWNGRD\x01"
220 downgradeCanaryTLS11 = "DOWNGRD\x00"
221 )
222
223
224 type ConnectionState struct {
225 Version uint16
226 HandshakeComplete bool
227 DidResume bool
228 CipherSuite uint16
229 NegotiatedProtocol string
230 NegotiatedProtocolIsMutual bool
231 ServerName string
232 PeerCertificates []*x509.Certificate
233 VerifiedChains [][]*x509.Certificate
234 SignedCertificateTimestamps [][]byte
235 OCSPResponse []byte
236
237
238 ekm func(label string, context []byte, length int) ([]byte, error)
239
240
241
242
243
244
245
246 TLSUnique []byte
247 }
248
249
250
251
252
253 func (cs *ConnectionState) ExportKeyingMaterial(label string, context []byte, length int) ([]byte, error) {
254 return cs.ekm(label, context, length)
255 }
256
257
258
259 type ClientAuthType int
260
261 const (
262 NoClientCert ClientAuthType = iota
263 RequestClientCert
264 RequireAnyClientCert
265 VerifyClientCertIfGiven
266 RequireAndVerifyClientCert
267 )
268
269
270
271 func requiresClientCert(c ClientAuthType) bool {
272 switch c {
273 case RequireAnyClientCert, RequireAndVerifyClientCert:
274 return true
275 default:
276 return false
277 }
278 }
279
280
281
282 type ClientSessionState struct {
283 sessionTicket []uint8
284 vers uint16
285 cipherSuite uint16
286 masterSecret []byte
287 serverCertificates []*x509.Certificate
288 verifiedChains [][]*x509.Certificate
289 receivedAt time.Time
290
291
292 nonce []byte
293 useBy time.Time
294 ageAdd uint32
295 }
296
297
298
299
300
301
302
303 type ClientSessionCache interface {
304
305
306 Get(sessionKey string) (session *ClientSessionState, ok bool)
307
308
309
310
311
312 Put(sessionKey string, cs *ClientSessionState)
313 }
314
315
316
317 type SignatureScheme uint16
318
319 const (
320
321 PKCS1WithSHA256 SignatureScheme = 0x0401
322 PKCS1WithSHA384 SignatureScheme = 0x0501
323 PKCS1WithSHA512 SignatureScheme = 0x0601
324
325
326 PSSWithSHA256 SignatureScheme = 0x0804
327 PSSWithSHA384 SignatureScheme = 0x0805
328 PSSWithSHA512 SignatureScheme = 0x0806
329
330
331 ECDSAWithP256AndSHA256 SignatureScheme = 0x0403
332 ECDSAWithP384AndSHA384 SignatureScheme = 0x0503
333 ECDSAWithP521AndSHA512 SignatureScheme = 0x0603
334
335
336 Ed25519 SignatureScheme = 0x0807
337
338
339 PKCS1WithSHA1 SignatureScheme = 0x0201
340 ECDSAWithSHA1 SignatureScheme = 0x0203
341 )
342
343
344
345 type ClientHelloInfo struct {
346
347
348 CipherSuites []uint16
349
350
351
352
353 ServerName string
354
355
356
357
358 SupportedCurves []CurveID
359
360
361
362
363 SupportedPoints []uint8
364
365
366
367
368 SignatureSchemes []SignatureScheme
369
370
371
372
373
374
375
376 SupportedProtos []string
377
378
379
380
381
382 SupportedVersions []uint16
383
384
385
386
387 Conn net.Conn
388 }
389
390
391
392
393 type CertificateRequestInfo struct {
394
395
396
397
398 AcceptableCAs [][]byte
399
400
401
402 SignatureSchemes []SignatureScheme
403 }
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419 type RenegotiationSupport int
420
421 const (
422
423 RenegotiateNever RenegotiationSupport = iota
424
425
426
427 RenegotiateOnceAsClient
428
429
430
431 RenegotiateFreelyAsClient
432 )
433
434
435
436
437
438 type Config struct {
439
440
441
442
443 Rand io.Reader
444
445
446
447 Time func() time.Time
448
449
450
451
452
453
454 Certificates []Certificate
455
456
457
458
459
460
461
462 NameToCertificate map[string]*Certificate
463
464
465
466
467
468
469
470
471 GetCertificate func(*ClientHelloInfo) (*Certificate, error)
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486 GetClientCertificate func(*CertificateRequestInfo) (*Certificate, error)
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 GetConfigForClient func(*ClientHelloInfo) (*Config, error)
507
508
509
510
511
512
513
514
515
516
517
518
519 VerifyPeerCertificate func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error
520
521
522
523
524 RootCAs *x509.CertPool
525
526
527
528 NextProtos []string
529
530
531
532
533
534 ServerName string
535
536
537
538 ClientAuth ClientAuthType
539
540
541
542
543 ClientCAs *x509.CertPool
544
545
546
547
548
549
550
551 InsecureSkipVerify bool
552
553
554
555
556
557
558 CipherSuites []uint16
559
560
561
562
563
564 PreferServerCipherSuites bool
565
566
567
568
569 SessionTicketsDisabled bool
570
571
572
573
574
575
576
577
578
579 SessionTicketKey [32]byte
580
581
582
583 ClientSessionCache ClientSessionCache
584
585
586
587 MinVersion uint16
588
589
590
591
592 MaxVersion uint16
593
594
595
596
597
598 CurvePreferences []CurveID
599
600
601
602
603
604 DynamicRecordSizingDisabled bool
605
606
607
608 Renegotiation RenegotiationSupport
609
610
611
612
613
614
615
616 KeyLogWriter io.Writer
617
618 serverInitOnce sync.Once
619
620
621 mutex sync.RWMutex
622
623
624
625
626 sessionTicketKeys []ticketKey
627 }
628
629
630
631 const ticketKeyNameLen = 16
632
633
634 type ticketKey struct {
635
636
637 keyName [ticketKeyNameLen]byte
638 aesKey [16]byte
639 hmacKey [16]byte
640 }
641
642
643
644
645 func ticketKeyFromBytes(b [32]byte) (key ticketKey) {
646 hashed := sha512.Sum512(b[:])
647 copy(key.keyName[:], hashed[:ticketKeyNameLen])
648 copy(key.aesKey[:], hashed[ticketKeyNameLen:ticketKeyNameLen+16])
649 copy(key.hmacKey[:], hashed[ticketKeyNameLen+16:ticketKeyNameLen+32])
650 return key
651 }
652
653
654
655 const maxSessionTicketLifetime = 7 * 24 * time.Hour
656
657
658
659 func (c *Config) Clone() *Config {
660
661
662 c.serverInitOnce.Do(func() { c.serverInit(nil) })
663
664 var sessionTicketKeys []ticketKey
665 c.mutex.RLock()
666 sessionTicketKeys = c.sessionTicketKeys
667 c.mutex.RUnlock()
668
669 return &Config{
670 Rand: c.Rand,
671 Time: c.Time,
672 Certificates: c.Certificates,
673 NameToCertificate: c.NameToCertificate,
674 GetCertificate: c.GetCertificate,
675 GetClientCertificate: c.GetClientCertificate,
676 GetConfigForClient: c.GetConfigForClient,
677 VerifyPeerCertificate: c.VerifyPeerCertificate,
678 RootCAs: c.RootCAs,
679 NextProtos: c.NextProtos,
680 ServerName: c.ServerName,
681 ClientAuth: c.ClientAuth,
682 ClientCAs: c.ClientCAs,
683 InsecureSkipVerify: c.InsecureSkipVerify,
684 CipherSuites: c.CipherSuites,
685 PreferServerCipherSuites: c.PreferServerCipherSuites,
686 SessionTicketsDisabled: c.SessionTicketsDisabled,
687 SessionTicketKey: c.SessionTicketKey,
688 ClientSessionCache: c.ClientSessionCache,
689 MinVersion: c.MinVersion,
690 MaxVersion: c.MaxVersion,
691 CurvePreferences: c.CurvePreferences,
692 DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled,
693 Renegotiation: c.Renegotiation,
694 KeyLogWriter: c.KeyLogWriter,
695 sessionTicketKeys: sessionTicketKeys,
696 }
697 }
698
699
700
701
702 func (c *Config) serverInit(originalConfig *Config) {
703 if c.SessionTicketsDisabled || len(c.ticketKeys()) != 0 {
704 return
705 }
706
707 alreadySet := false
708 for _, b := range c.SessionTicketKey {
709 if b != 0 {
710 alreadySet = true
711 break
712 }
713 }
714
715 if !alreadySet {
716 if originalConfig != nil {
717 copy(c.SessionTicketKey[:], originalConfig.SessionTicketKey[:])
718 } else if _, err := io.ReadFull(c.rand(), c.SessionTicketKey[:]); err != nil {
719 c.SessionTicketsDisabled = true
720 return
721 }
722 }
723
724 if originalConfig != nil {
725 originalConfig.mutex.RLock()
726 c.sessionTicketKeys = originalConfig.sessionTicketKeys
727 originalConfig.mutex.RUnlock()
728 } else {
729 c.sessionTicketKeys = []ticketKey{ticketKeyFromBytes(c.SessionTicketKey)}
730 }
731 }
732
733 func (c *Config) ticketKeys() []ticketKey {
734 c.mutex.RLock()
735
736
737 ret := c.sessionTicketKeys
738 c.mutex.RUnlock()
739 return ret
740 }
741
742
743
744
745
746
747 func (c *Config) SetSessionTicketKeys(keys [][32]byte) {
748 if len(keys) == 0 {
749 panic("tls: keys must have at least one key")
750 }
751
752 newKeys := make([]ticketKey, len(keys))
753 for i, bytes := range keys {
754 newKeys[i] = ticketKeyFromBytes(bytes)
755 }
756
757 c.mutex.Lock()
758 c.sessionTicketKeys = newKeys
759 c.mutex.Unlock()
760 }
761
762 func (c *Config) rand() io.Reader {
763 r := c.Rand
764 if r == nil {
765 return rand.Reader
766 }
767 return r
768 }
769
770 func (c *Config) time() time.Time {
771 t := c.Time
772 if t == nil {
773 t = time.Now
774 }
775 return t()
776 }
777
778 func (c *Config) cipherSuites() []uint16 {
779 s := c.CipherSuites
780 if s == nil {
781 s = defaultCipherSuites()
782 }
783 return s
784 }
785
786 var supportedVersions = []uint16{
787 VersionTLS13,
788 VersionTLS12,
789 VersionTLS11,
790 VersionTLS10,
791 VersionSSL30,
792 }
793
794 func (c *Config) supportedVersions(isClient bool) []uint16 {
795 versions := make([]uint16, 0, len(supportedVersions))
796 for _, v := range supportedVersions {
797
798 if (c == nil || c.MinVersion == 0) && v < VersionTLS10 {
799 continue
800 }
801 if c != nil && c.MinVersion != 0 && v < c.MinVersion {
802 continue
803 }
804 if c != nil && c.MaxVersion != 0 && v > c.MaxVersion {
805 continue
806 }
807
808 if isClient && v < VersionTLS10 {
809 continue
810 }
811
812 if v == VersionTLS13 && !isTLS13Supported() {
813 continue
814 }
815 versions = append(versions, v)
816 }
817 return versions
818 }
819
820
821 var tls13Support struct {
822 sync.Once
823 cached bool
824 }
825
826
827
828 func isTLS13Supported() bool {
829 tls13Support.Do(func() {
830 tls13Support.cached = goDebugString("tls13") != "0"
831 })
832 return tls13Support.cached
833 }
834
835
836
837 func goDebugString(key string) string {
838 s := os.Getenv("GODEBUG")
839 for i := 0; i < len(s)-len(key)-1; i++ {
840 if i > 0 && s[i-1] != ',' {
841 continue
842 }
843 afterKey := s[i+len(key):]
844 if afterKey[0] != '=' || s[i:i+len(key)] != key {
845 continue
846 }
847 val := afterKey[1:]
848 for i, b := range val {
849 if b == ',' {
850 return val[:i]
851 }
852 }
853 return val
854 }
855 return ""
856 }
857
858 func (c *Config) maxSupportedVersion(isClient bool) uint16 {
859 supportedVersions := c.supportedVersions(isClient)
860 if len(supportedVersions) == 0 {
861 return 0
862 }
863 return supportedVersions[0]
864 }
865
866
867
868
869 func supportedVersionsFromMax(maxVersion uint16) []uint16 {
870 versions := make([]uint16, 0, len(supportedVersions))
871 for _, v := range supportedVersions {
872 if v > maxVersion {
873 continue
874 }
875 versions = append(versions, v)
876 }
877 return versions
878 }
879
880 var defaultCurvePreferences = []CurveID{X25519, CurveP256, CurveP384, CurveP521}
881
882 func (c *Config) curvePreferences() []CurveID {
883 if c == nil || len(c.CurvePreferences) == 0 {
884 return defaultCurvePreferences
885 }
886 return c.CurvePreferences
887 }
888
889
890
891 func (c *Config) mutualVersion(isClient bool, peerVersions []uint16) (uint16, bool) {
892 supportedVersions := c.supportedVersions(isClient)
893 for _, peerVersion := range peerVersions {
894 for _, v := range supportedVersions {
895 if v == peerVersion {
896 return v, true
897 }
898 }
899 }
900 return 0, false
901 }
902
903
904
905 func (c *Config) getCertificate(clientHello *ClientHelloInfo) (*Certificate, error) {
906 if c.GetCertificate != nil &&
907 (len(c.Certificates) == 0 || len(clientHello.ServerName) > 0) {
908 cert, err := c.GetCertificate(clientHello)
909 if cert != nil || err != nil {
910 return cert, err
911 }
912 }
913
914 if len(c.Certificates) == 0 {
915 return nil, errors.New("tls: no certificates configured")
916 }
917
918 if len(c.Certificates) == 1 || c.NameToCertificate == nil {
919
920 return &c.Certificates[0], nil
921 }
922
923 name := strings.ToLower(clientHello.ServerName)
924 for len(name) > 0 && name[len(name)-1] == '.' {
925 name = name[:len(name)-1]
926 }
927
928 if cert, ok := c.NameToCertificate[name]; ok {
929 return cert, nil
930 }
931
932
933
934 labels := strings.Split(name, ".")
935 for i := range labels {
936 labels[i] = "*"
937 candidate := strings.Join(labels, ".")
938 if cert, ok := c.NameToCertificate[candidate]; ok {
939 return cert, nil
940 }
941 }
942
943
944 return &c.Certificates[0], nil
945 }
946
947
948
949
950 func (c *Config) BuildNameToCertificate() {
951 c.NameToCertificate = make(map[string]*Certificate)
952 for i := range c.Certificates {
953 cert := &c.Certificates[i]
954 x509Cert := cert.Leaf
955 if x509Cert == nil {
956 var err error
957 x509Cert, err = x509.ParseCertificate(cert.Certificate[0])
958 if err != nil {
959 continue
960 }
961 }
962 if len(x509Cert.Subject.CommonName) > 0 {
963 c.NameToCertificate[x509Cert.Subject.CommonName] = cert
964 }
965 for _, san := range x509Cert.DNSNames {
966 c.NameToCertificate[san] = cert
967 }
968 }
969 }
970
971 const (
972 keyLogLabelTLS12 = "CLIENT_RANDOM"
973 keyLogLabelClientHandshake = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"
974 keyLogLabelServerHandshake = "SERVER_HANDSHAKE_TRAFFIC_SECRET"
975 keyLogLabelClientTraffic = "CLIENT_TRAFFIC_SECRET_0"
976 keyLogLabelServerTraffic = "SERVER_TRAFFIC_SECRET_0"
977 )
978
979 func (c *Config) writeKeyLog(label string, clientRandom, secret []byte) error {
980 if c.KeyLogWriter == nil {
981 return nil
982 }
983
984 logLine := []byte(fmt.Sprintf("%s %x %x\n", label, clientRandom, secret))
985
986 writerMutex.Lock()
987 _, err := c.KeyLogWriter.Write(logLine)
988 writerMutex.Unlock()
989
990 return err
991 }
992
993
994
995 var writerMutex sync.Mutex
996
997
998 type Certificate struct {
999 Certificate [][]byte
1000
1001
1002
1003
1004 PrivateKey crypto.PrivateKey
1005
1006
1007 OCSPStaple []byte
1008
1009
1010 SignedCertificateTimestamps [][]byte
1011
1012
1013
1014
1015 Leaf *x509.Certificate
1016 }
1017
1018 type handshakeMessage interface {
1019 marshal() []byte
1020 unmarshal([]byte) bool
1021 }
1022
1023
1024
1025 type lruSessionCache struct {
1026 sync.Mutex
1027
1028 m map[string]*list.Element
1029 q *list.List
1030 capacity int
1031 }
1032
1033 type lruSessionCacheEntry struct {
1034 sessionKey string
1035 state *ClientSessionState
1036 }
1037
1038
1039
1040
1041 func NewLRUClientSessionCache(capacity int) ClientSessionCache {
1042 const defaultSessionCacheCapacity = 64
1043
1044 if capacity < 1 {
1045 capacity = defaultSessionCacheCapacity
1046 }
1047 return &lruSessionCache{
1048 m: make(map[string]*list.Element),
1049 q: list.New(),
1050 capacity: capacity,
1051 }
1052 }
1053
1054
1055
1056 func (c *lruSessionCache) Put(sessionKey string, cs *ClientSessionState) {
1057 c.Lock()
1058 defer c.Unlock()
1059
1060 if elem, ok := c.m[sessionKey]; ok {
1061 if cs == nil {
1062 c.q.Remove(elem)
1063 delete(c.m, sessionKey)
1064 } else {
1065 entry := elem.Value.(*lruSessionCacheEntry)
1066 entry.state = cs
1067 c.q.MoveToFront(elem)
1068 }
1069 return
1070 }
1071
1072 if c.q.Len() < c.capacity {
1073 entry := &lruSessionCacheEntry{sessionKey, cs}
1074 c.m[sessionKey] = c.q.PushFront(entry)
1075 return
1076 }
1077
1078 elem := c.q.Back()
1079 entry := elem.Value.(*lruSessionCacheEntry)
1080 delete(c.m, entry.sessionKey)
1081 entry.sessionKey = sessionKey
1082 entry.state = cs
1083 c.q.MoveToFront(elem)
1084 c.m[sessionKey] = elem
1085 }
1086
1087
1088
1089 func (c *lruSessionCache) Get(sessionKey string) (*ClientSessionState, bool) {
1090 c.Lock()
1091 defer c.Unlock()
1092
1093 if elem, ok := c.m[sessionKey]; ok {
1094 c.q.MoveToFront(elem)
1095 return elem.Value.(*lruSessionCacheEntry).state, true
1096 }
1097 return nil, false
1098 }
1099
1100
1101 type dsaSignature struct {
1102 R, S *big.Int
1103 }
1104
1105 type ecdsaSignature dsaSignature
1106
1107 var emptyConfig Config
1108
1109 func defaultConfig() *Config {
1110 return &emptyConfig
1111 }
1112
1113 var (
1114 once sync.Once
1115 varDefaultCipherSuites []uint16
1116 varDefaultCipherSuitesTLS13 []uint16
1117 )
1118
1119 func defaultCipherSuites() []uint16 {
1120 once.Do(initDefaultCipherSuites)
1121 return varDefaultCipherSuites
1122 }
1123
1124 func defaultCipherSuitesTLS13() []uint16 {
1125 once.Do(initDefaultCipherSuites)
1126 return varDefaultCipherSuitesTLS13
1127 }
1128
1129 func initDefaultCipherSuites() {
1130 var topCipherSuites []uint16
1131
1132
1133
1134 var (
1135 hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
1136 hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
1137
1138 hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR && (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
1139
1140 hasGCMAsm = hasGCMAsmAMD64 || hasGCMAsmARM64 || hasGCMAsmS390X
1141 )
1142
1143 if hasGCMAsm {
1144
1145
1146 topCipherSuites = []uint16{
1147 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1148 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
1149 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
1150 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
1151 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
1152 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
1153 }
1154 varDefaultCipherSuitesTLS13 = []uint16{
1155 TLS_AES_128_GCM_SHA256,
1156 TLS_CHACHA20_POLY1305_SHA256,
1157 TLS_AES_256_GCM_SHA384,
1158 }
1159 } else {
1160
1161
1162 topCipherSuites = []uint16{
1163 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
1164 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
1165 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
1166 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
1167 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
1168 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
1169 }
1170 varDefaultCipherSuitesTLS13 = []uint16{
1171 TLS_CHACHA20_POLY1305_SHA256,
1172 TLS_AES_128_GCM_SHA256,
1173 TLS_AES_256_GCM_SHA384,
1174 }
1175 }
1176
1177 varDefaultCipherSuites = make([]uint16, 0, len(cipherSuites))
1178 varDefaultCipherSuites = append(varDefaultCipherSuites, topCipherSuites...)
1179
1180 NextCipherSuite:
1181 for _, suite := range cipherSuites {
1182 if suite.flags&suiteDefaultOff != 0 {
1183 continue
1184 }
1185 for _, existing := range varDefaultCipherSuites {
1186 if existing == suite.id {
1187 continue NextCipherSuite
1188 }
1189 }
1190 varDefaultCipherSuites = append(varDefaultCipherSuites, suite.id)
1191 }
1192 }
1193
1194 func unexpectedMessageError(wanted, got interface{}) error {
1195 return fmt.Errorf("tls: received unexpected handshake message of type %T when waiting for %T", got, wanted)
1196 }
1197
1198 func isSupportedSignatureAlgorithm(sigAlg SignatureScheme, supportedSignatureAlgorithms []SignatureScheme) bool {
1199 for _, s := range supportedSignatureAlgorithms {
1200 if s == sigAlg {
1201 return true
1202 }
1203 }
1204 return false
1205 }
1206
1207
1208
1209 func signatureFromSignatureScheme(signatureAlgorithm SignatureScheme) uint8 {
1210 switch signatureAlgorithm {
1211 case PKCS1WithSHA1, PKCS1WithSHA256, PKCS1WithSHA384, PKCS1WithSHA512:
1212 return signaturePKCS1v15
1213 case PSSWithSHA256, PSSWithSHA384, PSSWithSHA512:
1214 return signatureRSAPSS
1215 case ECDSAWithSHA1, ECDSAWithP256AndSHA256, ECDSAWithP384AndSHA384, ECDSAWithP521AndSHA512:
1216 return signatureECDSA
1217 case Ed25519:
1218 return signatureEd25519
1219 default:
1220 return 0
1221 }
1222 }
1223
View as plain text