/
certificate.go
81 lines (66 loc) · 1.71 KB
/
certificate.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
package emaildefense
import (
"crypto/tls"
"net/smtp"
"time"
"github.com/zmap/zcrypto/x509"
)
// CertInfo struct for certificate information
type certificate struct {
CommonName string `json:"common_name,omitempty"`
Issuer string `json:"issuer,omitempty"`
Expires time.Time `json:"expires,omitempty"`
Certificate *cert `json:"certificate,omitempty"`
X509 *x509.Certificate `json:"x509,omitempty"`
SPKI *spki `json:"spki,omitempty"`
ConnectionError error `json:"connection_error,omitempty"`
}
type cert struct {
Raw string `json:"raw,omitempty"`
SHA256 string `json:"sha256,omitempty"`
SHA512 string `json:"sha512,omitempty"`
}
type spki struct {
Raw string `json:"raw,omitempty"`
SHA256 string `json:"sha256,omitempty"`
SHA512 string `json:"sha512,omitempty"`
}
func getCertificate(fqdn string) (*certificate, error) {
r := new(certificate)
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: fqdn,
}
c, err := smtp.Dial(fqdn + ":25")
if err != nil {
return nil, err
}
c.StartTLS(tlsconfig)
cs, ok := c.TLSConnectionState()
if !ok {
return nil, err
}
c.Quit()
peerChain := cs.PeerCertificates
if len(peerChain) == 0 {
return nil, err
}
parsed, err := x509.ParseCertificate(peerChain[0].Raw)
if err != nil {
return nil, err
}
// r.X509 = parsed
r.Expires = parsed.NotAfter
r.CommonName = parsed.Subject.CommonName
r.Issuer = parsed.Issuer.CommonName
/*
for _, peer := range peerChain {
parsed, err := x509.ParseCertificate(peer.Raw)
if err != nil {
return nil, err
}
r.Parsed = append(r.Parsed, parsed)
}
*/
return r, err
}