-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
214 lines (192 loc) · 5.42 KB
/
main.go
File metadata and controls
214 lines (192 loc) · 5.42 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
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"text/template"
"time"
)
var binVersion = "0.1.0"
var binBuildDate = "27 Sep 2017"
func main() {
var (
err error
output = flag.String("out", "", "An output file, if empty stdout is used.")
version = flag.Bool("version", false, "Display the current version.")
)
flag.Parse()
if *version {
fmt.Println("PEM Block Reader %s %s", binVersion, binBuildDate)
os.Exit(0)
}
if len(flag.Args()) == 0 {
log.Fatal("need a valid input .pem file as the first argument.")
}
o := os.Stdout
if len(*output) > 0 {
o, err = os.Create(*output)
if err != nil {
log.Fatalf("creating file: %v", err)
}
}
f, err := os.Open(flag.Args()[0])
if err != nil {
log.Fatalf("opening file: %v", err)
}
defer f.Close()
parse(f, o)
}
func parse(in io.Reader, out io.Writer) {
var err error
var rest []byte
tf := NewTplFuncs()
funcMap := template.FuncMap{
"cert": tf.Cert,
"version": tf.Version,
"serialNumber": tf.SerialNumber,
"signatureAlgorithm": tf.SignatureAlgorithm,
"issuer": tf.Issuer,
"subject": tf.Subject,
"pkAlgorithm": tf.PublicKeyAlgorithm,
"pk": tf.PublicKey,
"pkModulus": tf.PublicKeyModulus,
"pkExponent": tf.PublicKeyExponent,
"signatureModulus": tf.SignatureModulus,
"extensionsLabel": tf.ExtensionsLabel,
"extensionsDisplay": tf.tplExtensionsDisplay,
"fmtTime": func(t time.Time) string {
loc, err := time.LoadLocation("Etc/GMT")
if err != nil {
log.Fatalf("time location: %v", err)
}
return t.In(loc).Format("Jan _2 15:04:05 2006 MST")
},
}
pemTypeMap := map[string]func(*pem.Block){
"CERTIFICATE REQUEST": func(block *pem.Block) {
crts, err := x509.ParseCertificateRequest(block.Bytes)
if err != nil {
log.Fatalf("parse template: %v", err)
}
tfr := NewTplFuncs(WithBlockType(block.Type))
funcReqMap := template.FuncMap{
"cert": tfr.CertReq,
"version": tfr.Version,
"pk": tfr.PublicKey,
"pkModulus": tfr.PublicKeyModulus,
"pkExponent": tfr.PublicKeyExponent,
}
t := template.Must(template.New("certificate-request.tmpl").
Funcs(funcMap).
Funcs(funcReqMap).
Parse(certReqTmpl))
err = t.Execute(out, crts)
if err != nil {
log.Fatalf("execute template: %s %#v", err, t)
}
},
"PRIVATE KEY": func(block *pem.Block) {
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
log.Fatalf("parse private key %v", err)
}
pKey, ok := key.(*rsa.PrivateKey)
if !ok {
log.Fatalf("key is of type %T and not *rsa.PrivateKey", key)
}
pkBytes := pKey.PublicKey.N.Bytes()
if err != nil {
log.Fatalf("parse private key hex %v", err)
}
mln := multilnFingerprint(" ", modLenPublicKey, pkBytes, "00")
fmt.Fprintln(out, "Private Key:")
fmt.Fprintf(out, "Modulus (%d bit):\n%s\n", len(pkBytes)*8, mln)
},
"RSA PRIVATE KEY": func(block *pem.Block) {
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatalf("parse private key %v", err)
}
pkBytes := key.PublicKey.N.Bytes()
mln := multilnFingerprint(" ", modLenPublicKey, pkBytes, "00")
fmt.Fprintln(out, "Private Key:")
fmt.Fprintf(out, "Modulus (%d bit):\n%s\n", len(pkBytes)*8, mln)
},
"CERTIFICATE": func(block *pem.Block) {
crts, err := x509.ParseCertificates(block.Bytes)
if err != nil {
log.Fatalf("parse template: %v", err)
}
t := template.Must(template.New("certificate.tmpl").Funcs(funcMap).Parse(certTmpl))
err = t.Execute(out, crts)
if err != nil {
log.Fatalf("execute template: %v", err)
}
},
}
rest, err = ioutil.ReadAll(in)
if err != nil {
log.Fatalf("reading in file: %v", err)
}
for i := 0; len(rest) > 0; i++ {
var block *pem.Block
block, rest = pem.Decode(rest)
fn, ok := pemTypeMap[block.Type]
if !ok {
// if we don't know the type then print out raw data
fmt.Fprintf(out, "%#v\n", block)
continue
}
fn(block)
if len(rest) > 0 {
fmt.Fprintln(out, strings.Repeat("-", 65)) // the line break between blocks
}
}
fmt.Fprintln(out, "done.")
}
var certTmpl = `{{- range . -}}{{- cert . -}}
Certificate:
Data:
{{ version .Version }}
{{ serialNumber .SerialNumber }}
{{ signatureAlgorithm .SignatureAlgorithm }}
{{ issuer .Issuer }}
Validity
Not Before: {{ fmtTime .NotBefore }}
Not After : {{ fmtTime .NotAfter }}
{{ subject .Subject }}
Subject Public Key Info:
{{ pkAlgorithm .PublicKeyAlgorithm }}
{{ pk .PublicKey }}
{{ pkModulus }}
{{ pkExponent }}
{{- extensionsLabel .Extensions -}}
{{ range .Extensions }}
{{- extensionsDisplay . -}}
{{ end }}
{{ signatureAlgorithm .SignatureAlgorithm }}
{{- signatureModulus .Signature -}}
{{ end }}
`
var certReqTmpl = `{{- cert . -}}
Certificate Request:
Data:
{{ version .Version }}
{{ subject .Subject }}
Subject Public Key Info:
{{ pkAlgorithm .PublicKeyAlgorithm }}
{{ pk .PublicKey }}
{{ pkModulus }}
{{ pkExponent }}
Attributes:
a0:00
{{ signatureAlgorithm .SignatureAlgorithm }}
{{- signatureModulus .Signature }}
`