-
Notifications
You must be signed in to change notification settings - Fork 1
/
expire.go
157 lines (144 loc) · 4.23 KB
/
expire.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
package certexpire
import (
"context"
"crypto/sha512"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"os/exec"
"time"
)
// // TLSTimeout is timeout for negotiating a TLS connection.
// var TLSTimeout = time.Second * 10
func deadline(timeout time.Duration) time.Time {
return time.Now().Add(timeout)
}
// CertValues contains the relevant aspects of a certificate.
type CertValues struct {
Hostname string // Hostname used for retrieval connection.
Expire time.Time // Time this certificate expires.
VerifyError error // Any TLS errors when connecting.
Hash string // Hash of the raw certificate.
Certificate *x509.Certificate
}
func hashString(d []byte) string {
return fmt.Sprintf("%x", sha512.Sum512(d))
}
func convertCertificate(hostname string, cert *x509.Certificate, prevError ...error) *CertValues {
ret := &CertValues{
Hostname: hostname,
Expire: cert.NotAfter,
Hash: hashString(cert.Raw),
Certificate: cert,
}
if hostname != "" {
ret.VerifyError = cert.VerifyHostname(hostname)
}
if ret.VerifyError == nil && len(prevError) > 0 {
ret.VerifyError = prevError[0]
}
return ret
}
// GetCertificate returns the server certificate's expiry time. conn is an established connection. hostname is the hostname of the remote server.
func GetCertificate(conn net.Conn, hostname string, timeout time.Duration) (*CertValues, error) {
c := tls.Client(conn, &tls.Config{ServerName: hostname, InsecureSkipVerify: true})
defer c.Close()
_ = c.SetDeadline(deadline(timeout))
err := c.Handshake()
if err != nil {
return &CertValues{
Hostname: hostname,
VerifyError: err,
}, nil
}
state := c.ConnectionState()
certs := state.PeerCertificates
if len(certs) == 0 {
return &CertValues{
Hostname: hostname,
}, ErrNoCert
}
roots, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
opts := x509.VerifyOptions{
Roots: roots,
CurrentTime: time.Now(),
Intermediates: x509.NewCertPool(),
}
for _, cert := range certs[1:] {
opts.Intermediates.AddCert(cert)
}
if _, err := certs[0].Verify(opts); err != nil {
return convertCertificate(hostname, certs[0], err), nil
}
return convertCertificate(hostname, certs[0]), nil
}
func verifyPEM(servername string, d []byte) (*CertValues, error) {
block, _ := pem.Decode(d)
if block == nil || block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
return &CertValues{
Hostname: servername,
}, ErrNoCert
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return &CertValues{
Hostname: servername,
}, ErrNoCert
}
return convertCertificate(servername, cert), nil
}
// GetCertFile verifies a file
func GetCertFile(servername, path string) (*CertValues, error) {
d, err := ioutil.ReadFile(path)
if err != nil {
return &CertValues{
Hostname: servername,
}, err
}
return verifyPEM(servername, d)
}
// GetCertCMD runs a command and interpretes the standard output as a certificate.
func GetCertCMD(servername, command string, timeout time.Duration) (*CertValues, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
d, err := exec.CommandContext(ctx, command).Output()
if err != nil {
return &CertValues{
Hostname: servername,
}, err
}
return verifyPEM(servername, d)
}
// GetCertTLS returns the server certificate's expiry time for a TLS server.
func GetCertTLS(servername, port string, timeout time.Duration, proxy *Proxy) (*CertValues, error) {
conn, err := TCPDailer(servername+":"+port, proxy, timeout)
if err != nil {
return nil, err
}
defer conn.Close()
conn.SetDeadline(deadline(timeout))
return GetCertificate(conn, servername, timeout)
}
// GetCert returns the server certificate's expiry time. Proto is tls/ssl, imap, smtp.
func GetCert(servername, param, proto string, timeout time.Duration, proxy *Proxy) (*CertValues, error) {
switch proto {
case "tls", "ssl", "":
return GetCertTLS(servername, param, timeout, proxy)
case "imap":
return GetCertIMAP(servername, param, timeout, proxy)
case "smtp":
return GetCertSMTP(servername, param, timeout, proxy)
case "file":
return GetCertFile(servername, param)
case "command":
return GetCertCMD(servername, param, timeout)
default:
return nil, ErrConfig
}
}