-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtls_check.go
195 lines (166 loc) · 4.27 KB
/
tls_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"context"
"crypto/tls"
"errors"
"flag"
"fmt"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
// Config holds the application configuration
type Config struct {
target string
timeout time.Duration
verbose bool
}
// TLSChecker handles TLS connection analysis
type TLSChecker struct {
config Config
mu sync.RWMutex
results map[uint16]bool
errCount int
tlsVersion uint16
}
// NewTLSChecker creates a new instance of TLSChecker
func NewTLSChecker(cfg Config) *TLSChecker {
return &TLSChecker{
config: cfg,
results: make(map[uint16]bool),
}
}
// Run executes the TLS checking process
func (tc *TLSChecker) Run(ctx context.Context) error {
// Initial connection to get TLS version
if err := tc.checkTLSVersion(ctx); err != nil {
return fmt.Errorf("initial TLS check failed: %w", err)
}
// Test cipher suites concurrently
return tc.testCipherSuites(ctx)
}
func (tc *TLSChecker) checkTLSVersion(ctx context.Context) error {
conn, err := tc.connect(ctx, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
return err
}
defer conn.Close()
tc.tlsVersion = conn.ConnectionState().Version
return nil
}
func (tc *TLSChecker) connect(ctx context.Context, cfg *tls.Config) (*tls.Conn, error) {
dialer := &net.Dialer{
Timeout: tc.config.timeout,
KeepAlive: tc.config.timeout,
}
conn, err := tls.DialWithDialer(dialer, "tcp", tc.config.target, cfg)
if err != nil {
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
return nil, fmt.Errorf("connection timeout: %w", err)
}
return nil, fmt.Errorf("connection failed: %w", err)
}
return conn, nil
}
func (tc *TLSChecker) testCipherSuites(ctx context.Context) error {
var wg sync.WaitGroup
semaphore := make(chan struct{}, 10) // Limit concurrent connections
for _, suite := range tls.CipherSuites() {
select {
case <-ctx.Done():
return ctx.Err()
case semaphore <- struct{}{}:
}
wg.Add(1)
go func(suite *tls.CipherSuite) {
defer wg.Done()
defer func() { <-semaphore }()
cfg := &tls.Config{
InsecureSkipVerify: true,
CipherSuites: []uint16{suite.ID},
MinVersion: tc.tlsVersion,
MaxVersion: tc.tlsVersion,
}
conn, err := tc.connect(ctx, cfg)
if err != nil {
if tc.config.verbose {
fmt.Printf("Failed testing %s: %v\n", suite.Name, err)
}
tc.recordResult(suite.ID, false)
return
}
defer conn.Close()
tc.recordResult(suite.ID, true)
}(suite)
}
wg.Wait()
return nil
}
func (tc *TLSChecker) recordResult(suiteID uint16, supported bool) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.results[suiteID] = supported
if !supported {
tc.errCount++
}
}
func (tc *TLSChecker) printResults() {
fmt.Printf("\nTLS Connection Information for %s:\n", tc.config.target)
fmt.Printf("TLS Version: %s\n\n", getTLSVersionString(tc.tlsVersion))
fmt.Println("Supported Cipher Suites:")
for _, suite := range tls.CipherSuites() {
supported := tc.results[suite.ID]
if supported {
fmt.Printf("✓ %s\n", suite.Name)
} else if tc.config.verbose {
fmt.Printf("✗ %s\n", suite.Name)
}
}
fmt.Printf("\nSummary: %d supported, %d unsupported cipher suites\n",
len(tc.results)-tc.errCount, tc.errCount)
}
func getTLSVersionString(version uint16) string {
versions := map[uint16]string{
tls.VersionTLS10: "TLS 1.0",
tls.VersionTLS11: "TLS 1.1",
tls.VersionTLS12: "TLS 1.2",
tls.VersionTLS13: "TLS 1.3",
}
if v, ok := versions[version]; ok {
return v
}
return fmt.Sprintf("Unknown (0x%04x)", version)
}
func main() {
cfg := Config{}
flag.StringVar(&cfg.target, "url", "", "Target URL (e.g., example.com:443)")
flag.DurationVar(&cfg.timeout, "timeout", 5*time.Second, "Connection timeout")
flag.BoolVar(&cfg.verbose, "verbose", false, "Show detailed output including failures")
flag.Parse()
if cfg.target == "" {
flag.Usage()
os.Exit(1)
}
// Create context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle OS signals
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
cancel()
}()
checker := NewTLSChecker(cfg)
if err := checker.Run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
checker.printResults()
}