-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.go
More file actions
283 lines (240 loc) · 7.15 KB
/
text.go
File metadata and controls
283 lines (240 loc) · 7.15 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"strings"
"text/tabwriter"
"time"
)
const (
headerWidth = 80
ansiBold = "\033[1m"
ansiGreen = "\033[32m"
ansiRed = "\033[31m"
ansiReset = "\033[0m"
maxCompactSANs = 3
maxVerboseSANs = 20
)
type TextFormatter struct {
Verbosity OutputLevel
}
func (f *TextFormatter) Format(report Report) (string, error) {
var s strings.Builder
w := tabwriter.NewWriter(&s, 0, 0, 1, ' ', 0)
for _, record := range report {
// Flush tabwriter before writing header directly to s,
// so the header line isn't mangled by tab alignment.
if err := w.Flush(); err != nil {
return "", fmt.Errorf("tabwriter failed: %w", err)
}
f.formatHeader(&s, record)
f.formatFields(w, record)
}
if err := w.Flush(); err != nil {
return "", fmt.Errorf("tabwriter failed: %w", err)
}
return s.String(), nil
}
func (f *TextFormatter) formatHeader(s *strings.Builder, record *Record) {
name := record.Cert.inner.Subject.CommonName
if name == "" {
// Some root CAs don't have a CN but OU
if len(record.Cert.inner.Subject.OrganizationalUnit) > 0 {
name = record.Cert.inner.Subject.OrganizationalUnit[0]
} else {
name = record.Cert.inner.Subject.String()
}
}
status := printBool(record.Error == nil)
prefix := fmt.Sprintf("--- %s%s%s %s ", ansiBold, name, ansiReset, status)
pad := max(headerWidth-len(prefix), 3)
fmt.Fprintf(s, "%s%s\n", prefix, strings.Repeat("-", pad))
}
func (f *TextFormatter) formatFields(w *tabwriter.Writer, record *Record) {
cert := record.Cert.inner
fmt.Fprintf(w, "Subject:\t%s\n", f.formatName(cert.Subject))
if sans := f.formatSANs(cert); sans != "" {
fmt.Fprintf(w, "SANs:\t%s\n", sans)
}
fmt.Fprintf(w, "Issuer:\t%s\n", f.formatName(cert.Issuer))
if record.Error != nil {
fmt.Fprintf(w, "Error:\t%v\n", record.Error)
}
if f.Verbosity >= VerboseOutput {
fmt.Fprintf(w, "Not Before:\t%s %s\n", cert.NotBefore.String(), printBool(record.Validity.NotBeforeOK))
fmt.Fprintf(w, "Not After:\t%s %s\n", cert.NotAfter.String(), printBool(record.Validity.NotAfterOK))
} else {
fmt.Fprintf(w, "Valid:\t%s\n", f.formatValidity(record))
}
if f.Verbosity >= VerboseOutput {
fmt.Fprintf(w, "Fingerprint:\t%X\n", record.Cert.fingerprint)
fmt.Fprintf(w, "Key:\t%s\n", formatKeyInfo(cert))
fmt.Fprintf(w, "Signature:\t%s\n", cert.SignatureAlgorithm)
}
if f.Verbosity >= FullOutput {
if ku := formatKeyUsage(cert.KeyUsage); ku != "" {
fmt.Fprintf(w, "Key Usage:\t%s\n", ku)
}
if eku := formatExtKeyUsage(cert.ExtKeyUsage); eku != "" {
fmt.Fprintf(w, "Ext Key Usage:\t%s\n", eku)
}
}
fmt.Fprintf(w, "\n")
}
func (f *TextFormatter) formatSANs(cert *x509.Certificate) string {
sans := f.collectSANs(cert)
if len(sans) == 0 {
return ""
}
var maxSANs int
switch f.Verbosity {
case FullOutput:
maxSANs = len(sans)
case VerboseOutput:
maxSANs = maxVerboseSANs
default:
maxSANs = maxCompactSANs
}
n := min(len(sans), maxSANs)
s := strings.Join(sans[:n], ", ")
if extra := len(sans) - n; extra > 0 {
s += fmt.Sprintf(" (+%d more)", extra)
}
return s
}
func (f *TextFormatter) collectSANs(cert *x509.Certificate) []string {
if f.Verbosity == CompactOutput {
return cert.DNSNames
}
var sans []string
for _, dns := range cert.DNSNames {
sans = append(sans, "DNS:"+dns)
}
for _, ip := range cert.IPAddresses {
sans = append(sans, "IP:"+ip.String())
}
for _, email := range cert.EmailAddresses {
sans = append(sans, "Email:"+email)
}
for _, uri := range cert.URIs {
sans = append(sans, "URI:"+uri.String())
}
return sans
}
func formatKeyInfo(cert *x509.Certificate) string {
switch pub := cert.PublicKey.(type) {
case *rsa.PublicKey:
return fmt.Sprintf("RSA %d bits", pub.N.BitLen())
case *ecdsa.PublicKey:
return fmt.Sprintf("ECDSA %s", pub.Curve.Params().Name)
case ed25519.PublicKey:
return "Ed25519"
default:
return fmt.Sprintf("%T", pub)
}
}
var keyUsageNames = [...]struct {
bit x509.KeyUsage
name string
}{
{x509.KeyUsageDigitalSignature, "Digital Signature"},
{x509.KeyUsageContentCommitment, "Content Commitment"},
{x509.KeyUsageKeyEncipherment, "Key Encipherment"},
{x509.KeyUsageDataEncipherment, "Data Encipherment"},
{x509.KeyUsageKeyAgreement, "Key Agreement"},
{x509.KeyUsageCertSign, "Certificate Sign"},
{x509.KeyUsageCRLSign, "CRL Sign"},
{x509.KeyUsageEncipherOnly, "Encipher Only"},
{x509.KeyUsageDecipherOnly, "Decipher Only"},
}
func formatKeyUsage(ku x509.KeyUsage) string {
var names []string
for _, entry := range keyUsageNames {
if ku&entry.bit != 0 {
names = append(names, entry.name)
}
}
return strings.Join(names, ", ")
}
var extKeyUsageNames = map[x509.ExtKeyUsage]string{
x509.ExtKeyUsageAny: "Any",
x509.ExtKeyUsageServerAuth: "Server Authentication",
x509.ExtKeyUsageClientAuth: "Client Authentication",
x509.ExtKeyUsageCodeSigning: "Code Signing",
x509.ExtKeyUsageEmailProtection: "Email Protection",
x509.ExtKeyUsageIPSECEndSystem: "IPSEC End System",
x509.ExtKeyUsageIPSECTunnel: "IPSEC Tunnel",
x509.ExtKeyUsageIPSECUser: "IPSEC User",
x509.ExtKeyUsageTimeStamping: "Time Stamping",
x509.ExtKeyUsageOCSPSigning: "OCSP Signing",
x509.ExtKeyUsageMicrosoftServerGatedCrypto: "Microsoft Server Gated Crypto",
x509.ExtKeyUsageNetscapeServerGatedCrypto: "Netscape Server Gated Crypto",
}
func formatExtKeyUsage(eku []x509.ExtKeyUsage) string {
var names []string
for _, usage := range eku {
if name, ok := extKeyUsageNames[usage]; ok {
names = append(names, name)
} else {
names = append(names, fmt.Sprintf("Unknown(%d)", usage))
}
}
return strings.Join(names, ", ")
}
func (f *TextFormatter) formatValidity(rec *Record) string {
v := rec.Validity
inner := rec.Cert.inner
if v.ExpiresIn < 0 {
return fmt.Sprintf("%v, expired on %v %s", v.Period, inner.NotAfter.Format("2006-01-02"), printBool(v.OK))
}
return fmt.Sprintf("%v, expires in %v (%v) %s", v.Period, v.ExpiresIn, inner.NotAfter.Format("2006-01-02"), printBool(v.OK))
}
func (f *TextFormatter) formatName(name pkix.Name) string {
if f.Verbosity >= VerboseOutput {
return name.String()
}
s := []string{name.CommonName}
if len(name.OrganizationalUnit) > 0 {
s = append(s, name.OrganizationalUnit...)
}
if len(name.Organization) > 0 {
s = append(s, name.Organization...)
}
if len(name.Country) > 0 {
s = append(s, name.Country...)
}
return strings.Join(s, ", ")
}
func printBool(b bool) string {
if b {
return ansiGreen + "[OK]" + ansiReset
}
return ansiRed + "[ERR]" + ansiReset
}
// Duration provides custom time.Duration string serialization
type Duration time.Duration
func (d Duration) String() string {
const (
day = 24
week = 7 * day
month = 30 * day
year = 365 * day
)
h := time.Duration(d).Hours()
switch {
case h >= year:
return fmt.Sprintf("%.1f years", h/year)
case h >= month:
return fmt.Sprintf("%.1f months", h/month)
case h >= week:
return fmt.Sprintf("%.1f weeks", h/week)
case h >= day:
return fmt.Sprintf("%.1f days", h/day)
default:
return time.Duration(d).String()
}
}