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
|
package main
import (
"crypto/x509"
"encoding/pem"
"fmt"
"html/template"
"io/ioutil"
"os"
"sort"
"time"
)
func handleErr(err error, str string, a ...interface{}) {
a = append([]interface{}{err}, a...)
if err != nil {
fmt.Fprintf(os.Stderr, str, a...)
os.Exit(1)
}
}
func handleBool(ok bool, str string, a ...interface{}) {
if !ok {
fmt.Fprintf(os.Stderr, str, a...)
os.Exit(1)
}
}
var tmpl = template.Must(template.New("pem2html").
Funcs(template.FuncMap{
"red": red,
"green": green,
"date": fDate,
"datetime": fDateTime,
}).Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Live Certs</title>
<style>
html {
height: 100%;
}
body {
font-size: 10px;
font-family: monospace;
height: 100%;
margin: 0;
display: flex;
align-items: center;
}
body > * {
margin: auto;
}
table {
border-collapse: collapse;
}
caption p {
margin: 0;
}
td, th {
padding: 0;
border: solid 1px black;
white-space: nowrap;
}
td {
background-color: #F3F3F3;
}
tr.invalid td {
background-color: #F30000 !important;
}
tr:hover td, tr.invalid:hover td {
background-color: #AAAAF3 !important;
}
td a {
padding: 0.1em 0.25em;
display: block;
width: 100%;
height: 100%;
color: black;
}
time .time {
display: none
}
</style>
<script src="sorttable.js"></script>
<base target="_parent" />
</head>
<body>
<table class=sortable>
<caption>
<p>Live Certs (Updated {{.now | datetime}})</p>
</caption>
<tr>
<th>NotBefore</th>
<th>NotAfter</th>
<th>Subject.CN</th>
<th>Socket</th>
</tr>
{{range $cert := .certs}}
<tr class="{{$cert.Class}}">
<td style="background-color: {{$cert.X509.NotBefore | green}}"><a href="{{$cert.Url}}" title="{{$cert.Error}}">{{$cert.X509.NotBefore | date}}</a></td>
<td style="background-color: {{$cert.X509.NotAfter | red }}"><a href="{{$cert.Url}}" title="{{$cert.Error}}">{{$cert.X509.NotAfter | date}}</a></td>
<td><a href="{{$cert.Url}}" title="{{$cert.Error}}">{{$cert.X509.Subject.CommonName | html}}</a></td>
<td><a href="{{$cert.Url}}" title="{{$cert.Error}}">{{$cert.Socket | html}}</a></td>
</tr>
{{end}}
</table>
</body>
</html>
`))
func fDate(t time.Time) template.HTML {
return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\" title=\"2006-01-02 15:04:05\">2006-01-02 <span class=time>15:04:05</span></time>"))
}
func fDateTime(t time.Time) template.HTML {
return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
}
func getNow() time.Time {
stat, err := os.Stdin.Stat()
if err == nil {
return stat.ModTime()
} else {
return time.Now()
}
}
var now = getNow()
type interpolation struct {
ta, tb time.Time
ba, bb byte
}
func (i interpolation) interpolate(tc time.Time) byte {
db := i.tb.Sub(i.ta)
dc := tc.Sub(i.ta)
pct := float64(dc) / float64(db)
if pct < 0 {
pct = 0
} else if pct > 1 {
pct = 1
}
sb := int16(i.bb) - int16(i.ba)
sc := int16(pct * float64(sb))
return byte(int16(i.ba) + sc)
}
var daysago = interpolation{
ta: now.AddDate(0, 0, -30),
tb: now,
ba: 0xF3,
bb: 0x00,
}
var daysuntil = interpolation{
ta: now,
tb: now.AddDate(0, 0, 30),
ba: 0x00,
bb: 0xF3,
}
func green(t time.Time) string {
b := daysago.interpolate(t)
return fmt.Sprintf("#%02X%02X%02X", b, 0xF3, b)
}
func red(t time.Time) string {
b := daysuntil.interpolate(t)
return fmt.Sprintf("#%02X%02X%02X", 0xF3, b, b)
}
type Cert struct {
Socket string
Error string
X509 *x509.Certificate
}
func (cert Cert) Url() string {
return fmt.Sprintf("https://crt.sh/?serial=%036x", cert.X509.SerialNumber)
}
func (cert Cert) Class() string {
if cert.Error == "" {
return ""
} else {
return "invalid"
}
}
type Certs []Cert
// Len is the number of elements in the collection.
func (l Certs) Len() int {
return len(l)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (l Certs) Less(i, j int) bool {
return l[i].X509.NotAfter.After(l[j].X509.NotAfter)
}
// Swap swaps the elements with indexes i and j.
func (l Certs) Swap(i, j int) {
tmp := l[i]
l[i] = l[j]
l[j] = tmp
}
func main() {
data, err := ioutil.ReadAll(os.Stdin)
handleErr(err, "Error reading stdin: %v\n")
var certs Certs
for len(data) > 0 {
var certPem *pem.Block
certPem, data = pem.Decode(data)
var ok bool
var cert Cert
cert.Socket, ok = certPem.Headers["X-Socket"]
handleBool(ok, "Did not get X-Socket\n")
cert.Error, ok = certPem.Headers["X-Error"]
cert.X509, err = x509.ParseCertificate(certPem.Bytes)
handleErr(err, "Error parsing cert: %v\n")
certs = append(certs, cert)
}
sort.Sort(certs)
handleErr(tmpl.Execute(os.Stdout, map[string]interface{}{"certs":certs, "now": now}), "Could not execute template: %v\n")
}
|