-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtlstext.go
76 lines (67 loc) · 1.71 KB
/
tlstext.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
// Package tlstext provides simple functions VersionText and
// CipherSuiteText that provide the raw value to string translations.
package tlstext
//go:generate go run generator/main.go
import (
"crypto/tls"
"fmt"
)
// reverse map of binary TLS Version to string
var versionMap = map[uint16]string{
0x0300: "SSL30",
0x0301: "TLS10",
0x0302: "TLS11",
0x0303: "TLS12",
0x0304: "TLS13",
}
// map of TLS token name to hex value
var versionStringMap = map[string]uint16{
"SSL30": 0x0300,
"TLS10": 0x0301,
"TLS11": 0x0302,
"TLS12": 0x0303,
"TLS13": 0x0304,
}
// Version maps a TLS version to a string, or the hex
// representation if unknown.
func Version(x uint16) string {
s, ok := versionMap[x]
if !ok {
return fmt.Sprintf("%04x", x)
}
return s
}
// CipherSuite maps a TLS Cipher Suite to a string or the hex
// representation if unknown
func CipherSuite(x uint16) string {
s, ok := cipherMap[x]
if !ok {
return fmt.Sprintf("%04x", x)
}
return s
}
// CipherFromString returns the uint16 value of the cipher name or 0 if unknown
func CipherFromString(s string) uint16 {
return cipherStringMap[s]
}
// VersionFromString returns the uint16 value of a TLS version string, or 0 if unknown
func VersionFromString(s string) uint16 {
return versionStringMap[s]
}
// VersionFromConnection returns a string representation of CipherSuite
// or empty string if not TLS
func VersionFromConnection(t *tls.ConnectionState) string {
if t == nil {
return ""
}
return Version(t.Version)
}
// CipherSuiteFromConnection returns a string representation of
// CipherSuite or empty string if not TLS
//
func CipherSuiteFromConnection(t *tls.ConnectionState) string {
if t == nil {
return ""
}
return CipherSuite(t.CipherSuite)
}