2
2
//
3
3
// go run certcheck.go https://chrisshort.net
4
4
//
5
- // go run certcheck.go https://chrisshort.net 30
6
- //
7
-
5
+ // go run certcheck.go https://chrisshort.net
8
6
package main
9
7
10
8
import (
@@ -29,9 +27,11 @@ func main() {
29
27
os .Exit (1 )
30
28
}
31
29
30
+ // Parse the URL and number of days
32
31
url := os .Args [1 ]
33
32
var days int = - 1 // Default value if days argument is not provided
34
33
34
+ // Check if the number of days argument is provided
35
35
if len (os .Args ) == 3 {
36
36
daysStr := os .Args [2 ]
37
37
var err error
@@ -44,19 +44,22 @@ func main() {
44
44
45
45
tr := & http.Transport {
46
46
TLSClientConfig : & tls.Config {
47
- InsecureSkipVerify : true , // allow self-signed certificates
47
+ // This is required to allow self-signed certificates
48
+ InsecureSkipVerify : true ,
48
49
},
49
50
}
50
51
51
52
client := & http.Client {Transport : tr }
52
53
54
+ // Check if the URL is valid
53
55
resp , err := client .Get (url )
54
56
if err != nil {
55
57
fmt .Printf ("Error connecting to %s: %s\n " , url , err )
56
58
os .Exit (1 )
57
59
}
58
60
defer resp .Body .Close ()
59
61
62
+ // Check if the response was successful
60
63
certs := resp .TLS .PeerCertificates
61
64
var validChain bool = true
62
65
for i := 0 ; i < len (certs )- 1 ; i ++ {
@@ -72,6 +75,7 @@ func main() {
72
75
fmt .Printf ("Valid from: %s\n " , cert .NotBefore )
73
76
fmt .Printf ("Valid until: %s" , cert .NotAfter )
74
77
78
+ // Check if the certificate is expired
75
79
if days != - 1 {
76
80
daysLeft := int (time .Until (cert .NotAfter ).Hours () / 24 )
77
81
if daysLeft <= days {
@@ -90,6 +94,12 @@ func main() {
90
94
fmt .Printf ("IP Addresses: %v\n " , cert .IPAddresses )
91
95
fmt .Printf ("Signature algorithm: %s\n " , cert .SignatureAlgorithm .String ())
92
96
97
+ // Obtain the cipher information
98
+ state := resp .TLS
99
+ if state != nil {
100
+ fmt .Printf ("Cipher in use: %s\n " , tls .CipherSuiteName (state .CipherSuite ))
101
+ }
102
+
93
103
// Print KeyUsage information if available
94
104
if cert .KeyUsage != 0 {
95
105
fmt .Println ("KeyUsage:" )
@@ -100,9 +110,16 @@ func main() {
100
110
fingerprint := sha256 .Sum256 (cert .Raw )
101
111
fmt .Printf ("Fingerprint (SHA-256): %s\n " , hex .EncodeToString (fingerprint [:]))
102
112
113
+ // Check if the HSTS header is present
114
+ hstsHeader := resp .Header .Get ("Strict-Transport-Security" )
115
+ if hstsHeader != "" {
116
+ fmt .Println ("HSTS Header:" , hstsHeader )
117
+ }
118
+
103
119
fmt .Println ("-----" )
104
120
}
105
121
122
+ // Print the validity of the certificate chain
106
123
if validChain {
107
124
color .Set (color .Bold , color .FgGreen )
108
125
fmt .Println ("Certificate chain is valid and in the correct order." )
@@ -112,6 +129,7 @@ func main() {
112
129
}
113
130
}
114
131
132
+ // printKeyUsage prints the key usage flags of a certificate.
115
133
func printKeyUsage (keyUsage x509.KeyUsage ) {
116
134
usageStrings := []string {
117
135
"Digital Signature" ,
@@ -125,13 +143,15 @@ func printKeyUsage(keyUsage x509.KeyUsage) {
125
143
"Decipher Only" ,
126
144
}
127
145
146
+ // Print the key usage flags of a certificate.
128
147
for i , usage := range usageStrings {
129
148
if keyUsage & (1 << i ) != 0 {
130
149
fmt .Printf ("- %s\n " , usage )
131
150
}
132
151
}
133
152
}
134
153
154
+ // parseDays parses the number of days from a string.
135
155
func parseDays (daysStr string ) (int , error ) {
136
156
days , err := strconv .Atoi (daysStr )
137
157
if err != nil {
0 commit comments