forked from square/certigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
260 lines (241 loc) · 6.86 KB
/
main.go
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
/*-
* Copyright 2016 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"bufio"
"crypto/x509"
"encoding/binary"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/square/certigo/jceks"
"golang.org/x/crypto/pkcs12"
"golang.org/x/crypto/ssh/terminal"
)
var (
app = kingpin.New("certigo", "A command line certificate examination utility.")
dump = app.Command("dump", "Display information about a certificate.")
dumpFiles = dump.Arg("file", "Certificate file to dump (or stdin if not specified).").ExistingFiles()
dumpType = dump.Flag("format", "Format of given input (heuristic guess if not specified).").String()
)
var fileExtToFormat = map[string]string{
".pem": "PEM",
".crt": "PEM",
".p12": "PKCS12",
".pfx": "PKCS12",
".jceks": "JCEKS",
".jks": "JCEKS", // Only partially supported
".der": "DER",
}
type certWithAlias struct {
alias string
file string
cert *x509.Certificate
}
func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case dump.FullCommand(): // Dump certificate
files := []*os.File{}
if *dumpFiles != nil {
for _, filename := range *dumpFiles {
rawFile, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to open file: %s\n", err)
os.Exit(1)
}
files = append(files, rawFile)
defer rawFile.Close()
}
} else {
files = append(files, os.Stdin)
}
var certs []certWithAlias
for _, file := range files {
reader := bufio.NewReader(file)
format, ok := formatForFile(reader, file.Name(), *dumpType)
if !ok {
fmt.Fprintf(os.Stderr, "unable to guess file type (for file %s)\n", file.Name())
os.Exit(1)
}
parsed, err := getCerts(reader, file.Name(), format)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
certs = append(certs, parsed...)
}
for i, cert := range certs {
fmt.Printf("** CERTIFICATE %d **\n", i+1)
if cert.file != "" && len(files) > 1 {
fmt.Printf("File : %s\n", path.Base(cert.file))
}
displayCert(cert)
fmt.Println()
}
}
}
func readPassword(prompt string) (string, error) {
var tty *os.File
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
tty = os.Stdin
} else {
defer tty.Close()
}
tty.WriteString(prompt)
password, err := terminal.ReadPassword(int(tty.Fd()))
tty.WriteString("\n")
if err != nil {
return "", err
}
return string(password), err
}
// formatForFile returns the file format (either from flags or
// based on file extension).
func formatForFile(file *bufio.Reader, filename, format string) (string, bool) {
// First, honor --format flag we got from user
if format != "" {
return format, true
}
// Second, attempt to guess based on extension
guess, ok := fileExtToFormat[strings.ToLower(filepath.Ext(filename))]
if ok {
return guess, true
}
// Third, attempt to guess based on first 4 bytes of input
data, err := file.Peek(4)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// Heuristics for guessing -- best effort.
magic := binary.BigEndian.Uint32(data)
if magic == 0xCECECECE || magic == 0xFEEDFEED {
// JCEKS/JKS files always start with this prefix
return "JCEKS", true
}
if magic == 0x2D2D2D2D || magic == 0x434f4e4e {
// Starts with '----' or 'CONN' (what s_client prints...)
return "PEM", true
}
if magic&0xFFFF0000 == 0x30820000 {
// Looks like the input is DER-encoded, so it's either PKCS12 or X.509.
if magic&0x0000FF00 == 0x0300 {
// Probably X.509
return "DER", true
}
return "PKCS12", true
}
return "", false
}
// getCerts takes in a filename and format type and returns an
// array of all the certificates found in that file along with aliases
// for each cert if the format of the input was jceks. If no format
// is specified for the file, getCerts guesses what format was used
// based on the file extension used in the file name. If it can't
// guess based on this it returns and error.
func getCerts(reader io.Reader, filename string, format string) ([]certWithAlias, error) {
var certs []certWithAlias
switch format {
case "PEM":
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
block, data := pem.Decode(data)
for block != nil {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
certs = append(certs, certWithAlias{file: filename, cert: cert})
block, data = pem.Decode(data)
}
case "DER":
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
cert, err := x509.ParseCertificate(data)
if err != nil {
return nil, err
}
certs = append(certs, certWithAlias{file: filename, cert: cert})
case "PKCS12":
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
password, err := readPassword("Enter password: ")
if err != nil {
return nil, err
}
blocks, err := pkcs12.ToPEM(data, strings.TrimSuffix(password, "\n"))
if err != nil {
return nil, err
}
if len(blocks) == 0 {
return nil, fmt.Errorf("keystore appears to be empty or password was incorrect")
}
for _, block := range blocks {
if block.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
certs = append(certs, certWithAlias{alias: block.Headers["friendlyName"], file: filename, cert: cert})
}
}
case "JCEKS":
password, err := readPassword("Enter password: ")
if err != nil {
return nil, err
}
keyStore, err := jceks.LoadFromReader(reader, []byte(strings.TrimSuffix(password, "\n")))
if err != nil {
return nil, err
}
for _, alias := range keyStore.ListCerts() {
cert, _ := keyStore.GetCert(alias)
if err != nil {
return nil, err
}
certs = append(certs, certWithAlias{alias: alias, file: filename, cert: cert})
}
for _, alias := range keyStore.ListPrivateKeys() {
password, err := readPassword(fmt.Sprintf("Enter password for alias [%s]: ", alias))
if err != nil {
return nil, err
}
_, certArr, err := keyStore.GetPrivateKeyAndCerts(alias, []byte(strings.TrimSuffix(password, "\n")))
if err != nil {
return nil, err
}
for _, cert := range certArr {
certs = append(certs, certWithAlias{alias: alias, file: filename, cert: cert})
}
}
default:
return nil, fmt.Errorf("unknown file type: %s", format)
}
return certs, nil
}