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
|
package main
import (
"crypto/x509"
"encoding/pem"
"fmt"
"html/template"
"io/ioutil"
"os"
)
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("2html").Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CT log accuracy</title>
<style>
html {
height: 100%;
}
body {
font-size: 10px;
font-family: monospace;
height: 100%;
margin: 0;
display: flex;
align-items: center;
}
body > * {
margin: auto;
}
.diff-del, .diff-del a { color: red; }
.diff-add, .diff-add a { color: green; }
.diff-dat, .diff-dat a { color: blue; }
.diff-ctx, .diff-ctx a { color: black; }
tr:hover a {
background-color: #AAAAF3;
}
td a {
text-decoration: none;
padding: 0.1em 0.25em;
display: block;
width: 100%;
height: 100%;
color: black;
}
</style>
<script src="sorttable.js"></script>
<base target="_parent" />
</head>
<body>
<table>
<tr class="diff-del"><td colspan=4>--- tls.pem</td></tr>
<tr class="diff-add"><td colspan=4>+++ crtsh.pem</td></tr>
<tr class="diff-dat"><td colspan=4>@@ -1,{{.nTLS}} +1,{{.nCrtSh}} @@</td></tr>
{{range $cert := .certs}}
<tr class={{$cert.Class}}>
<td><a href="{{$cert.Url}}">{{if eq $cert.Pfix " "}} {{else}}{{$cert.Pfix}}{{end}}</a></td>
<td><a href="{{$cert.Url}}">{{$cert.X509.Subject.CommonName}}</a></td>
<td><a href="{{$cert.Url}}">{{$cert.X509.NotBefore.Local.Format "2006-01-02 15:04:05"}}</a></td>
<td><a href="{{$cert.Url}}">{{$cert.X509.NotAfter.Local.Format "2006-01-02 15:04:05"}}</a></td>
</tr>
{{end}}
</table>
</body>
</html>
`))
type Cert struct {
Url string
action string
X509 *x509.Certificate
}
func (cert Cert) Pfix() string {
return map[string]string {
"add": "+",
"del": "-",
"ctx": " ",
}[cert.action]
}
func (cert Cert) Class() string {
return "diff-" + cert.action
}
func main() {
data, err := ioutil.ReadAll(os.Stdin)
handleErr(err, "Error reading stdin: %v\n")
var certs []Cert
a := 0
b := 0
for len(data) > 0 {
var certPem *pem.Block
certPem, data = pem.Decode(data)
var ok bool
var cert Cert
cert.Url, ok = certPem.Headers["X-Crt-Sh-Url"]
handleBool(ok, "Did not get X-Crt-Sh-Url\n")
cert.action, ok = certPem.Headers["X-Diff-Action"]
handleBool(ok, "Did not get X-Diff-Action\n")
switch cert.action {
case "add":
b++
case "del":
a++
case "ctx":
a++
b++
default:
handleBool(false, "Unknown X-Diff-Action: %q\n", cert.action)
}
cert.X509, err = x509.ParseCertificate(certPem.Bytes)
handleErr(err, "Error parsing cert: %v\n")
certs = append(certs, cert)
}
handleErr(tmpl.Execute(os.Stdout, map[string]interface{}{
"certs":certs,
"nTLS": a,
"nCrtSh": b,
}), "Could not execute template: %v\n")
}
|