-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
182 lines (144 loc) · 3.75 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
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/cloudflare/cfssl/log"
"github.com/cloudflare/cfssl/revoke"
"github.com/dustin/go-humanize"
"gopkg.in/alecthomas/kingpin.v2"
"net"
"net/url"
"os"
"reflect"
"strings"
. "github.com/logrusorgru/aurora"
)
var (
server = kingpin.Arg("host", "Hostname, IP, or URL of the server to check.").String()
sni = kingpin.Flag("sni", "SNI server to use for the certificate (optional).").Short('s').String()
version = "dev"
)
func main() {
log.SetLogger(new(logger))
kingpin.Version(version)
kingpin.Parse()
if server == nil || *server == "" {
kingpin.Usage()
os.Exit(1)
}
input := *server
if !strings.Contains(input, "://") {
input = "https://" + input
}
parsedUrl, err := url.Parse(input)
if err != nil {
fmt.Println(Red("Invalid URL: " + err.Error()))
os.Exit(1)
}
// Hostname is used for SNI by default
hostname := parsedUrl.Hostname()
// Server is used for the underlying connection
server := hostname
port := parsedUrl.Port()
if port == "" {
port = "443"
}
// Did the user provide a different server to use for SNI?
if sni != nil && *sni != "" {
hostname = *sni
}
// Resolve the IP of the server
if addr, err := net.LookupIP(server); err == nil {
server = addr[0].String()
}
server += ":" + port
fmt.Printf("Connecting to %s as %s...\n", server, hostname)
valid := true
if err := checkIfCertValid(server, hostname); err != nil {
valid = false
log.Warningf("Failed to verify cert by connecting to the server: %s", err)
} else {
fmt.Println(Green("Successfully connected to server"))
}
fmt.Println("")
cert, err := getCert(server, hostname)
if err != nil {
log.Errorf("Failed to load the cert by connecting to the server: %s", err)
os.Exit(1)
}
fmt.Printf("Issued by: %s\n", splitLines(cert.Issuer, 12))
fmt.Printf("Issued: %s (%s)\n", cert.NotBefore.String(), humanize.Time(cert.NotBefore))
fmt.Printf("Expires: %s (%s)\n", cert.NotAfter.String(), humanize.Time(cert.NotAfter))
fmt.Printf("Subject: %s\n", cert.Subject)
fmt.Printf("DNS Names: %s\n", splitLines(cert.DNSNames, 12))
fmt.Println()
revoked, ok, err := revoke.VerifyCertificateError(cert)
if revoked {
valid = false
}
if !ok {
valid = false
log.Warning("Failed to verify certificate revocation status")
}
if err != nil {
valid = false
log.Warningf("Failed to verify certificate revocation status: %s", err)
}
if valid {
fmt.Println(Green("Certificate seems to be valid"))
os.Exit(0)
} else {
fmt.Println(Red("Certificate is invalid"))
os.Exit(1)
}
}
func checkIfCertValid(server, hostname string) error {
conf := &tls.Config{
ServerName: hostname,
}
conn, err := tls.Dial("tcp", server, conf)
if err != nil {
return err
}
conn.Close()
return nil
}
func getCert(server, hostname string) (*x509.Certificate, error) {
conf := &tls.Config{
InsecureSkipVerify: true,
ServerName: hostname,
}
conn, err := tls.Dial("tcp", server, conf)
if err != nil {
return nil, err
}
defer conn.Close()
return conn.ConnectionState().PeerCertificates[0], nil
}
func splitLines(value interface{}, padding int) string {
var lines []string
if reflect.TypeOf(value).Kind() == reflect.Slice {
s := reflect.ValueOf(value)
for i := 0; i < s.Len(); i++ {
lines = append(lines, fmt.Sprintf("%v", s.Index(i)))
}
} else {
v := fmt.Sprintf("%v", value)
for _, line := range strings.Split(v, "\n") {
lines = append(lines, line)
}
}
if len(lines) == 0 {
return ""
}
var sb strings.Builder
// No padding for the first line
sb.WriteString(fmt.Sprintf("%v", lines[0]))
for _, s := range lines[1:] {
sb.WriteString("\n")
sb.WriteString(strings.Repeat(" ", padding))
sb.WriteString(fmt.Sprintf("%v", s))
}
return sb.String()
}