-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
426 lines (389 loc) · 17.6 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"crypto/tls"
"crypto/x509"
"errors"
"flag"
"fmt"
"io"
"net"
"net/http"
"github.com/grantae/certinfo"
"golang.org/x/net/http2"
)
func tlsVersionToString(tlsVersion uint16) string {
switch tlsVersion {
case tls.VersionTLS10:
return "1.0"
case tls.VersionTLS11:
return "1.1"
case tls.VersionTLS12:
return "1.2"
case tls.VersionTLS13:
return "1.3"
case tls.VersionSSL30:
return "3.0"
default:
return "UNKNOWN"
}
}
func cipherSuiteToString(cipherSuite uint16) string {
switch cipherSuite {
// TLS 1.0 - 1.2 cipher suites.
case tls.TLS_RSA_WITH_RC4_128_SHA:
return "TLS_RSA_WITH_RC4_128_SHA"
case tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA:
return "TLS_RSA_WITH_3DES_EDE_CBC_SHA"
case tls.TLS_RSA_WITH_AES_128_CBC_SHA:
return "TLS_RSA_WITH_AES_128_CBC_SHA"
case tls.TLS_RSA_WITH_AES_256_CBC_SHA:
return "TLS_RSA_WITH_AES_256_CBC_SHA"
case tls.TLS_RSA_WITH_AES_128_CBC_SHA256:
return "TLS_RSA_WITH_AES_128_CBC_SHA256"
case tls.TLS_RSA_WITH_AES_128_GCM_SHA256:
return "TLS_RSA_WITH_AES_128_GCM_SHA256"
case tls.TLS_RSA_WITH_AES_256_GCM_SHA384:
return "TLS_RSA_WITH_AES_256_GCM_SHA384"
case tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA"
case tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA"
case tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA"
case tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA:
return "TLS_ECDHE_RSA_WITH_RC4_128_SHA"
case tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"
case tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"
case tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"
case tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"
case tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"
case tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"
case tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"
case tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"
case tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"
case tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
return "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256"
case tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
return "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"
// TLS 1.3 cipher suites.
case tls.TLS_AES_128_GCM_SHA256:
return "TLS_AES_128_GCM_SHA256"
case tls.TLS_AES_256_GCM_SHA384:
return "TLS_AES_256_GCM_SHA384"
case tls.TLS_CHACHA20_POLY1305_SHA256:
return "TLS_CHACHA20_POLY1305_SHA256"
// TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
// that the client is doing version fallback. See RFC 7507.
case tls.TLS_FALLBACK_SCSV:
return "TLS_FALLBACK_SCSV"
default:
return "UNKNOWN"
}
}
func tlsVersionsToString(tlsVersions []uint16) string {
output := "["
for i, tlsVersion := range tlsVersions {
output += tlsVersionToString(tlsVersion) + "(" + fmt.Sprintf("%d", tlsVersion) + ")"
if i < len(tlsVersions)-1 {
output += " "
}
}
output += "]"
return output
}
func cipherSuitesToString(cipherSuites []uint16) string {
output := "["
for i, cipherSuite := range cipherSuites {
output += cipherSuiteToString(cipherSuite) + "(" + fmt.Sprintf("%d", cipherSuite) + ")"
if i < len(cipherSuites)-1 {
output += " "
}
}
output += "]"
return output
}
func x509ToString(x509 *x509.Certificate) string {
if x509 == nil {
return "Unavailable"
} else {
str, err := certinfo.CertificateText(x509)
if err != nil {
return "Unparsable"
}
return str
}
}
var defaultTlsKeyPEM = "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIJKQIBAAKCAgEAsM0gklnSFVwV87ylxqeObB60xYdoRKh+vIxvzID3/VuiJYWz\n" +
"Sbw8RrGCsiQA926nAjYZWtfJsN+7S4N5eCTj8ofysjMmfPbZp4S4aXZA9M36E3DM\n" +
"HOjJmFW4nxaXznY1gKNjeSyyLvnHeFK+X/6gt8d732+2+1wgQBFfAamaRH0shEIM\n" +
"wpxc2sxbpLUpuKbq/sugkuLCXH/hGHsxnbwMIySpbUGOG+mAn/rjGJC+H+MWsn2/\n" +
"y8ZJf8D+VOhhCJqrpZ9WePz8ef2QiZHcAO1DSARU+cF1ODFZs3/aoCVNneXHOmzM\n" +
"5acU+xEUlsFle9ptE5X8CFcmFt2RchEhy/3C3/pseyW3X8xGr9JcUslCv8Fr+utx\n" +
"nrzVOk+PugcfY5UaOaZZOHrZUeqvnklTGxn75sJlxMCnEGOClsk70t/HOFpc+FHL\n" +
"6SnhqE/LQK+Y3nJOpCKDRv+/VjGeIHQwJYOzV01aQdRVcJ0U2L1duG4SfXQ3YrSv\n" +
"yU1+rlQhKsqP9n6I9d4rS+ipPlwuWimBlVQwyWM302aC/z0uWROclkN0D+qUGy8b\n" +
"HpwEoY+AY3RIPlrsuhSwe6V6QYpVH5gY1ZXsJ5Io3IDaN1nsK5qZ0dy9ijaAYJby\n" +
"mzfL5DwR4p2OrG/ExHK2+/P4Zz9T8Aa4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4HCZ3RK5r6wzBbOJPAl\n" +
"TUiXi9GolFQTLTVUzHbhT+Ztqyn3JJEWidNxWEY3GhP3KMopVOQSZCDuLQBaAG5n\n" +
"UU9NSEohuFLx+qKn5+nkl5YppbV1TkkN3Xm06rq8hezCU7/AeAMisrFl6ODA3VSa\n" +
"X0xsG++xbxpyJyAbFOPNruOUGG6S5XIQ6evpRNHb5h7LVWqTvr9gu7H4gNnsHn+F\n" +
"w3yvPdXftpB8bQBf3RMwLHhZjvly/xRdJQ4JFdgHycKRSCK5lc2ryp5NcdbOSkfP\n" +
"fV0CCfJ+wpeZgdqc6uvXzdwbYBd3slBl23WhSLLgWBfK7+n/KKrTauP6b110h6qH\n" +
"+LEENKgqS6xvEitdnSVp7qPhREpTUIea1bVS6fweCdGGLWweUtBNRG5wULVrDa+c\n" +
"HDpCHCwQXsFz82P3KA==\n" +
"-----END CERTIFICATE-----\n"
func main() {
var cert tls.Certificate
var err error
tlsVersion := uint16(tls.VersionTLS10)
tlsVersionFlag := flag.String("tlsversion", "1.2", "TLS version of the server")
serverPort := flag.Int("port", 443, "Server port")
pathToServerKey := flag.String("key", "", "Path to server's private key in PEM format")
pathToServerCert := flag.String("cert", "", "Path to server's certificate in PEM format")
flag.Parse()
switch *tlsVersionFlag {
case "1.0":
tlsVersion = tls.VersionTLS10
case "1.1":
tlsVersion = tls.VersionTLS11
case "1.2":
tlsVersion = tls.VersionTLS12
case "1.3":
tlsVersion = tls.VersionTLS13
default:
fmt.Printf("ERROR: Value %v for -tlsversion not supported. Possible values are 1.0, 1.1, 1.2 and 1.3.\n", *tlsVersionFlag)
flag.Usage()
return
}
if *pathToServerCert == "" && *pathToServerKey != "" {
fmt.Printf("ERROR: If -key is set, -cert must also be set.\n")
flag.Usage()
return
}
if *pathToServerKey == "" && *pathToServerCert != "" {
fmt.Printf("ERROR: If -cert is set, -key must also be set.\n")
flag.Usage()
return
}
if *pathToServerKey == "" || *pathToServerCert == "" {
cert, err = tls.X509KeyPair([]byte(defaultTlsCertPEM), []byte(defaultTlsKeyPEM))
if err != nil {
fmt.Printf("ERROR: tls.X509KeyPair() failed: %s\n", err)
return
}
} else {
cert, err = tls.LoadX509KeyPair(*pathToServerCert, *pathToServerKey)
if err != nil {
fmt.Printf("ERROR: tls.LoadX509KeyPair() failed: %s\n", err)
return
}
}
config := &tls.Config{
// Set ClientAuth to require client certificates (or
// VerifyPeerCertificate will run anyway and panic accessing certs[0])
// but not verify them with the default verifier.
ClientAuth: tls.RequireAnyClientCert,
MinVersion: tlsVersion,
MaxVersion: tlsVersion,
}
// Server configurations must set one of config.Certificates, config.GetCertificate or
// config.GetConfigForClient.
// We chose to set config.GetCertificate.
//
// GetCertificate returns a Certificate based on the given
// ClientHelloInfo. It will only be called if the client supplies SNI
// information or if Certificates is empty.
//
// If GetCertificate is nil or returns nil, then the certificate is
// retrieved from NameToCertificate. If NameToCertificate is nil, the
// best element of Certificates will be used.
config.GetCertificate = func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
fmt.Printf("\n======== ClientHello ========\n")
fmt.Printf("CipherSuites : %s\n", cipherSuitesToString(clientHello.CipherSuites))
fmt.Printf("ServerName : %s\n", clientHello.ServerName)
fmt.Printf("SupportedCurves : %v\n", clientHello.SupportedCurves)
fmt.Printf("SupportedPoints : %v\n", clientHello.SupportedPoints)
fmt.Printf("SignatureSchemes : %v\n", clientHello.SignatureSchemes)
fmt.Printf("SupportedProtos : %v\n", clientHello.SupportedProtos)
fmt.Printf("SupportedVersions : %s\n\n", tlsVersionsToString(clientHello.SupportedVersions))
return &cert, nil
}
// VerifyPeerCertificate, if not nil, is called after normal
// certificate verification by either a TLS client or server. It
// receives the raw ASN.1 certificates provided by the peer and also
// any verified chains that normal processing found. If it returns a
// non-nil error, the handshake is aborted and that error results.
//
// If normal verification fails then the handshake will abort before
// considering this callback. If normal verification is disabled by
// setting InsecureSkipVerify, or (for a server) when ClientAuth is
// RequestClientCert or RequireAnyClientCert, then this callback will
// be considered but the verifiedChains argument will always be nil.
config.VerifyPeerCertificate = func(certificates [][]byte, _ [][]*x509.Certificate) error {
fmt.Printf("\n======== VerifyPeerCertificate ========\n")
certs := make([]*x509.Certificate, len(certificates))
for i, asn1Data := range certificates {
cert, err := x509.ParseCertificate(asn1Data)
if err != nil {
fmt.Printf("ERROR: x509.ParseCertificate() failed: %s\n\n", err)
return errors.New("tls: failed to parse certificate from server: " + err.Error())
}
certs[i] = cert
}
// We accept all certs, so we only verify that we got a client certificate.
if len(certs) == 0 {
fmt.Printf("ERROR: No client certificate\n\n")
return fmt.Errorf("no client certificate")
}
fmt.Printf("%s\n\n", x509ToString(certs[0]))
return nil
}
// VerifyConnection, if not nil, is called after normal certificate
// verification and after VerifyPeerCertificate by either a TLS client
// or server. If it returns a non-nil error, the handshake is aborted
// and that error results.
//
// If normal verification fails then the handshake will abort before
// considering this callback. This callback will run for all connections
// regardless of InsecureSkipVerify or ClientAuth settings.
config.VerifyConnection = func(state tls.ConnectionState) error {
fmt.Printf("\n======== VerifyConnection ========\n")
fmt.Printf("Version : %s(%v)\n", tlsVersionToString(state.Version), state.Version)
fmt.Printf("HandshakeComplete : %v\n", state.HandshakeComplete)
fmt.Printf("DidResume : %v\n", state.DidResume)
fmt.Printf("CipherSuite : %s(%v)\n", cipherSuiteToString(state.CipherSuite), state.CipherSuite)
fmt.Printf("NegotiatedProtocol : %v\n", state.NegotiatedProtocol)
fmt.Printf("ServerName : %v\n\n", state.ServerName)
return nil
}
address := "localhost:" + fmt.Sprintf("%d", *serverPort)
server := &http.Server{
Addr: address,
TLSConfig: config,
}
server.ConnState = func(conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
fmt.Printf("INFO: Connection State: NEW\n")
case http.StateActive:
fmt.Printf("INFO: Connection State: ACTIVE\n")
case http.StateIdle:
fmt.Printf("INFO: Connection State: IDLE\n")
case http.StateHijacked:
fmt.Printf("INFO: Connection State: HIJACKED\n")
case http.StateClosed:
fmt.Printf("INFO: Connection State: CLOSED\n")
default:
fmt.Printf("INFO: Connection State: UNKNOWN\n")
}
_, ok := conn.(*tls.Conn)
if ok {
fmt.Printf("INFO: Connection is TLS\n")
}
fmt.Print("\n")
}
http2.ConfigureServer(server, nil)
http.HandleFunc("/", handler)
fmt.Printf("INFO: Server is running at https://localhost:%v\n", *serverPort)
fmt.Printf("INFO: Server listening...\n")
server.ListenAndServeTLS("", "")
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text")
state := r.TLS
io.WriteString(w, "Connected!\n\n")
io.WriteString(w, "=========== TLS Connection State ===========\n")
io.WriteString(w, "Version : "+tlsVersionToString(state.Version)+"("+fmt.Sprintf("%v", state.Version)+")\n")
io.WriteString(w, "HandshakeComplete : "+fmt.Sprintf("%v", state.HandshakeComplete)+"\n")
io.WriteString(w, "DidResume : "+fmt.Sprintf("%v", state.DidResume)+"\n")
io.WriteString(w, "CipherSuite : "+cipherSuiteToString(state.CipherSuite)+"("+fmt.Sprintf("%v", state.CipherSuite)+")\n")
io.WriteString(w, "NegotiatedProtocol : "+state.NegotiatedProtocol+"\n")
io.WriteString(w, "ServerName : "+state.ServerName+"\n\n")
io.WriteString(w, "=========== Request State ===========\n")
io.WriteString(w, "Method : "+r.Method+"\n")
io.WriteString(w, "Protocol : "+r.Proto+"\n")
io.WriteString(w, "Header : "+fmt.Sprintf("%v", r.Header)+"\n")
io.WriteString(w, "Host : "+r.Host+"\n")
io.WriteString(w, "Form : "+fmt.Sprintf("%v", r.Form)+"\n")
io.WriteString(w, "PostForm : "+fmt.Sprintf("%v", r.PostForm)+"\n")
io.WriteString(w, "Trailer : "+fmt.Sprintf("%v", r.Trailer)+"\n")
io.WriteString(w, "RemoteAddr : "+r.RemoteAddr+"\n")
io.WriteString(w, "RequestURI : "+r.RequestURI+"\n\n")
io.WriteString(w, "=========== Peer Certificate ===========\n")
if len(state.PeerCertificates) > 0 {
io.WriteString(w, "Subject : "+state.PeerCertificates[0].Subject.CommonName+"\n")
io.WriteString(w, "Cert : "+x509ToString(state.PeerCertificates[0])+"\n\n")
}
}