diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2018-03-14 18:18:31 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2018-03-17 13:49:41 -0400 |
commit | b54a1c9686eec3c1114e9b58cb67679ba59c45bd (patch) | |
tree | 0bdb2f3ed51ff077a8c3e337e4bc556aacec108e /bin-src/diff-pem2html.go | |
parent | 54feeb027d6e5a760b49769dfe695ea2591dc6fe (diff) |
directories
Diffstat (limited to 'bin-src/diff-pem2html.go')
-rw-r--r-- | bin-src/diff-pem2html.go | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/bin-src/diff-pem2html.go b/bin-src/diff-pem2html.go new file mode 100644 index 0000000..f3b25ff --- /dev/null +++ b/bin-src/diff-pem2html.go @@ -0,0 +1,109 @@ +package main + +import ( + "crypto/x509" + "encoding/pem" + "fmt" + "html/template" + "io/ioutil" + "os" + + "./util" +) + +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{ + "htmlcell": util.HTMLCellEscapeString, + }).Parse(`<table class=diff> + <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}}">{{$cert.Pfix | htmlcell}}</a></td> + <td><a href="{{$cert.Url}}">{{$cert.X509.Subject.CommonName | htmlcell}}</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> +`)) + +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) + if err != nil { + cert.X509 = new(x509.Certificate) + } + + certs = append(certs, cert) + } + + handleErr(tmpl.Execute(os.Stdout, map[string]interface{}{ + "certs": certs, + "nTLS": a, + "nCrtSh": b, + }), "Could not execute template: %v\n") +} |