-
Notifications
You must be signed in to change notification settings - Fork 13
/
print.go
121 lines (101 loc) · 3.1 KB
/
print.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
package main
import (
"crypto/tls"
"fmt"
"github.com/icza/gox/timex"
"github.com/pete911/certinfo/pkg/cert"
"time"
)
func PrintCertificatesLocations(certificateLocations []cert.CertificateLocation, printChains, printPem bool) {
for _, certificateLocation := range certificateLocations {
fmt.Printf("--- [%s] ---\n", nameFormat(certificateLocation.Path, certificateLocation.TLSVersion))
printCertificates(certificateLocation.Certificates, printPem)
if certificateLocation.VerifiedChains != nil {
fmt.Printf("--- %d verified chains ---\n", len(certificateLocation.VerifiedChains))
}
if printChains {
for i, chain := range certificateLocation.VerifiedChains {
fmt.Printf("--- chain %d ---\n", i+1)
printCertificates(chain, printPem)
}
}
}
}
func printCertificates(certificates cert.Certificates, printPem bool) {
for _, certificate := range certificates {
fmt.Println(certificate)
fmt.Println()
if printPem {
fmt.Println(string(certificate.ToPEM()))
}
}
}
func PrintPemOnly(certificateLocations []cert.CertificateLocation, printChains bool) {
for _, certificateLocation := range certificateLocations {
for _, certificate := range certificateLocation.Certificates {
fmt.Print(string(certificate.ToPEM()))
}
if printChains {
for _, chains := range certificateLocation.VerifiedChains {
fmt.Println()
for _, chain := range chains {
fmt.Print(string(chain.ToPEM()))
}
}
}
}
}
func PrintCertificatesExpiry(certificateLocations []cert.CertificateLocation) {
for _, certificateLocation := range certificateLocations {
fmt.Printf("--- [%s] ---\n", nameFormat(certificateLocation.Path, certificateLocation.TLSVersion))
for _, certificate := range certificateLocation.Certificates {
expiry := expiryFormat(certificate.X509Certificate.NotAfter)
if certificate.IsExpired() {
expiry = fmt.Sprintf("EXPIRED %s ago", expiry)
}
fmt.Printf("Subject: %s\n", certificate.X509Certificate.Subject)
fmt.Printf("Expiry: %s\n", expiry)
fmt.Println()
}
}
}
func nameFormat(name string, tlsVersion uint16) string {
if tlsVersion == 0 {
return name
}
return fmt.Sprintf("%s %s", name, tlsFormat(tlsVersion))
}
func tlsFormat(tlsVersion uint16) string {
switch tlsVersion {
case 0:
return ""
case tls.VersionSSL30:
return "SSLv3 - Deprecated!"
case tls.VersionTLS10:
return "TLS 1.0 - Deprecated!"
case tls.VersionTLS11:
return "TLS 1.1 - Deprecated!"
case tls.VersionTLS12:
return "TLS 1.2"
case tls.VersionTLS13:
return "TLS 1.3"
default:
return fmt.Sprintf("TLS Version %d (unknown)", tlsVersion)
}
}
func expiryFormat(t time.Time) string {
year, month, day, hour, minute, _ := timex.Diff(time.Now(), t)
if year != 0 {
return fmt.Sprintf("%d years %d months %d days %d hours %d minutes", year, month, day, hour, minute)
}
if month != 0 {
return fmt.Sprintf("%d months %d days %d hours %d minutes", month, day, hour, minute)
}
if day != 0 {
return fmt.Sprintf("%d days %d hours %d minutes", day, hour, minute)
}
if hour != 0 {
return fmt.Sprintf("%d hours %d minutes", hour, minute)
}
return fmt.Sprintf("%d minutes", minute)
}