forked from square/certigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
235 lines (205 loc) · 7.01 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
/*-
* 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 (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"os"
"strings"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/square/certigo/lib"
"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 from a file/stdin.")
dumpFiles = dump.Arg("file", "Certificate file to dump (or stdin if not specified).").ExistingFiles()
dumpType = dump.Flag("format", "Format of given input (PEM, DER, JCEKS, PKCS12; heuristic if missing).").String()
dumpPem = dump.Flag("pem", "Write output as PEM blocks instead of human-readable format.").Bool()
dumpPassword = dump.Flag("password", "Password for PKCS12/JCEKS key stores (if required).").String()
dumpJSON = dump.Flag("json", "Write output as machine-readable JSON format.").Bool()
connect = app.Command("connect", "Connect to a server and print its certificate(s).")
connectTo = connect.Arg("server:port", "Hostname or IP to connect to.").String()
connectName = connect.Flag("name", "Override the server name used for Server Name Indication (SNI).").String()
connectCaPath = connect.Flag("ca", "Path to CA bundle (system default if unspecified).").ExistingFile()
connectPem = connect.Flag("pem", "Write output as PEM blocks instead of human-readable format.").Bool()
connectJSON = connect.Flag("json", "Write output as machine-readable JSON format.").Bool()
connectCert = connect.Flag("cert", "Client certificate chain for connecting to server (PEM).").ExistingFile()
connectKey = connect.Flag("key", "Private key for client certificate, if not in same file (PEM).").ExistingFile()
verify = app.Command("verify", "Verify a certificate chain from file/stdin against a name.")
verifyFile = verify.Arg("file", "Certificate file to dump (or stdin if not specified).").ExistingFile()
verifyName = verify.Flag("name", "Server name to verify certificate against.").Required().String()
verifyCaPath = verify.Flag("ca", "Path to CA bundle (system default if unspecified).").ExistingFile()
verifyType = verify.Flag("format", "Format of given input (PEM, DER, JCEKS, PKCS12; heuristic if missing).").String()
verifyJSON = verify.Flag("json", "Write output as machine-readable JSON format.").Bool()
)
func main() {
app.Version("1.4.0")
result := simpleResult{}
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case dump.FullCommand(): // Dump certificate
files := inputFiles(*dumpFiles)
defer func() {
for _, file := range files {
file.Close()
}
}()
if *dumpPem {
lib.ReadAsPEMFromFiles(files, *dumpType, readPassword, func(block *pem.Block) {
block.Headers = nil
pem.Encode(os.Stdout, block)
})
} else {
lib.ReadAsX509FromFiles(files, *dumpType, readPassword, func(cert *x509.Certificate) {
result.Certificates = append(result.Certificates, cert)
})
if *dumpJSON {
blob, _ := json.Marshal(result)
fmt.Println(string(blob))
} else {
for i, cert := range result.Certificates {
fmt.Printf("** CERTIFICATE %d **\n", i+1)
fmt.Printf("%s\n\n", lib.EncodeX509ToText(cert))
}
}
}
case connect.FullCommand(): // Get certs by connecting to a server
conn, err := tls.Dial("tcp", *connectTo, tlsConfigForConnect())
if err != nil {
fmt.Fprintf(os.Stderr, "error connecting: %v\n", err)
os.Exit(1)
}
defer conn.Close()
for _, cert := range conn.ConnectionState().PeerCertificates {
if *connectPem {
pem.Encode(os.Stdout, lib.EncodeX509ToPEM(cert, nil))
} else {
result.Certificates = append(result.Certificates, cert)
}
}
if !*connectPem {
var hostname string
if *connectName != "" {
hostname = *connectName
} else {
hostname = strings.Split(*connectTo, ":")[0]
}
verifyResult := verifyChain(conn.ConnectionState().PeerCertificates, hostname, *connectCaPath)
result.VerifyResult = &verifyResult
}
if *connectJSON {
blob, _ := json.Marshal(result)
fmt.Println(string(blob))
} else if !*connectPem {
for i, cert := range result.Certificates {
fmt.Printf("** CERTIFICATE %d **\n", i+1)
fmt.Printf("%s\n\n", lib.EncodeX509ToText(cert))
}
printVerifyResult(*result.VerifyResult)
}
case verify.FullCommand():
file := inputFile(*verifyFile)
defer file.Close()
chain := []*x509.Certificate{}
lib.ReadAsX509FromFiles([]*os.File{file}, *verifyType, readPassword, func(cert *x509.Certificate) {
chain = append(chain, cert)
})
verifyResult := verifyChain(chain, *verifyName, *verifyCaPath)
if *verifyJSON {
blob, _ := json.Marshal(verifyResult)
fmt.Println(string(blob))
} else {
printVerifyResult(verifyResult)
}
if verifyResult.Error != "" {
os.Exit(1)
}
}
}
func inputFile(fileName string) *os.File {
if fileName == "" {
return os.Stdin
}
rawFile, err := os.Open(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to open file: %s\n", err)
os.Exit(1)
}
return rawFile
}
func inputFiles(fileNames []string) []*os.File {
files := []*os.File{}
if fileNames != nil {
for _, filename := range fileNames {
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)
}
} else {
files = append(files, os.Stdin)
}
return files
}
func tlsConfigForConnect() *tls.Config {
conf := &tls.Config{
// We verify later manually so we can print results
InsecureSkipVerify: true,
ServerName: *connectName,
}
if *connectCert != "" {
keyFile := *connectCert
if *connectKey != "" {
keyFile = *connectKey
}
cert, err := tls.LoadX509KeyPair(*connectCert, keyFile)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to read client certificate/key: %s\n", err)
os.Exit(1)
}
conf.Certificates = []tls.Certificate{cert}
}
return conf
}
func readPassword(alias string) string {
if *dumpPassword != "" {
return *dumpPassword
}
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("Enter password")
if alias != "" {
tty.WriteString(fmt.Sprintf(" for entry [%s]", alias))
}
tty.WriteString(": ")
password, err := terminal.ReadPassword(int(tty.Fd()))
tty.WriteString("\n")
if err != nil {
fmt.Fprintf(os.Stderr, "error reading password: %s\n", err)
os.Exit(1)
}
return strings.TrimSuffix(string(password), "\n")
}