...
1
16
17 package flag
18
19 import (
20 "crypto/tls"
21 "fmt"
22
23 "k8s.io/apimachinery/pkg/util/sets"
24 )
25
26 var (
27
28
29 ciphers = map[string]uint16{}
30 insecureCiphers = map[string]uint16{}
31 )
32
33 func init() {
34 for _, suite := range tls.CipherSuites() {
35 ciphers[suite.Name] = suite.ID
36 }
37
38 ciphers["TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305"] = tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
39 ciphers["TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305"] = tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
40
41 for _, suite := range tls.InsecureCipherSuites() {
42 insecureCiphers[suite.Name] = suite.ID
43 }
44 }
45
46
47
48 func InsecureTLSCiphers() map[string]uint16 {
49 cipherKeys := make(map[string]uint16, len(insecureCiphers))
50 for k, v := range insecureCiphers {
51 cipherKeys[k] = v
52 }
53 return cipherKeys
54 }
55
56
57
58 func InsecureTLSCipherNames() []string {
59 cipherKeys := sets.NewString()
60 for key := range insecureCiphers {
61 cipherKeys.Insert(key)
62 }
63 return cipherKeys.List()
64 }
65
66
67 func PreferredTLSCipherNames() []string {
68 cipherKeys := sets.NewString()
69 for key := range ciphers {
70 cipherKeys.Insert(key)
71 }
72 return cipherKeys.List()
73 }
74
75 func allCiphers() map[string]uint16 {
76 acceptedCiphers := make(map[string]uint16, len(ciphers)+len(insecureCiphers))
77 for k, v := range ciphers {
78 acceptedCiphers[k] = v
79 }
80 for k, v := range insecureCiphers {
81 acceptedCiphers[k] = v
82 }
83 return acceptedCiphers
84 }
85
86
87
88 func TLSCipherPossibleValues() []string {
89 cipherKeys := sets.NewString()
90 acceptedCiphers := allCiphers()
91 for key := range acceptedCiphers {
92 cipherKeys.Insert(key)
93 }
94 return cipherKeys.List()
95 }
96
97
98 func TLSCipherSuites(cipherNames []string) ([]uint16, error) {
99 if len(cipherNames) == 0 {
100 return nil, nil
101 }
102 ciphersIntSlice := make([]uint16, 0)
103 possibleCiphers := allCiphers()
104 for _, cipher := range cipherNames {
105 intValue, ok := possibleCiphers[cipher]
106 if !ok {
107 return nil, fmt.Errorf("Cipher suite %s not supported or doesn't exist", cipher)
108 }
109 ciphersIntSlice = append(ciphersIntSlice, intValue)
110 }
111 return ciphersIntSlice, nil
112 }
113
114 var versions = map[string]uint16{
115 "VersionTLS10": tls.VersionTLS10,
116 "VersionTLS11": tls.VersionTLS11,
117 "VersionTLS12": tls.VersionTLS12,
118 "VersionTLS13": tls.VersionTLS13,
119 }
120
121
122 func TLSPossibleVersions() []string {
123 versionsKeys := sets.NewString()
124 for key := range versions {
125 versionsKeys.Insert(key)
126 }
127 return versionsKeys.List()
128 }
129
130
131 func TLSVersion(versionName string) (uint16, error) {
132 if len(versionName) == 0 {
133 return DefaultTLSVersion(), nil
134 }
135 if version, ok := versions[versionName]; ok {
136 return version, nil
137 }
138 return 0, fmt.Errorf("unknown tls version %q", versionName)
139 }
140
141
142 func DefaultTLSVersion() uint16 {
143
144
145
146 return tls.VersionTLS12
147 }
148
View as plain text