-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
main.go
146 lines (124 loc) · 2.83 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
package main
import (
"fmt"
"github.com/urfave/cli"
"github.com/zakjan/cert-chain-resolver/certUtil"
"io/ioutil"
"os"
)
func openInputFile(filename string) (*os.File, error) {
if filename == "" {
return os.Stdin, nil
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
return file, nil
}
func openOutputFile(filename string) (*os.File, error) {
if filename == "" {
return os.Stdout, nil
}
file, err := os.Create(filename)
if err != nil {
return nil, err
}
return file, nil
}
func run(inputFilename string, outputFilename string, outputIntermediateOnly bool,
outputDerFormat bool, includeSystem bool) error {
inputFile, err := openInputFile(inputFilename)
if err != nil {
return err
}
outputFile, err := openOutputFile(outputFilename)
if err != nil {
return err
}
data, err := ioutil.ReadAll(inputFile)
if err != nil {
return err
}
cert, err := certUtil.DecodeCertificate(data)
if err != nil {
return err
}
certs, err := certUtil.FetchCertificateChain(cert)
if err != nil {
return err
}
if includeSystem {
certs, err = certUtil.AddRootCA(certs)
if err != nil {
return err
}
}
if outputIntermediateOnly {
certs = certs[1:]
}
if !outputDerFormat {
data = certUtil.EncodeCertificates(certs)
} else {
data = certUtil.EncodeCertificatesDER(certs)
}
_, err = outputFile.Write(data)
if err != nil {
return err
}
for i, cert := range certs {
fmt.Fprintf(os.Stderr, "%d: %s\n", i+1, cert.Subject.CommonName)
}
fmt.Fprintf(os.Stderr, "Certificate chain complete.\n")
fmt.Fprintf(os.Stderr, "Total %d certificate(s) found.\n", len(certs))
return nil
}
func main() {
var (
inputFilename string
outputFilename string
outputIntermediateOnly bool
outputDerFormat bool
includeSystem bool
)
app := cli.NewApp()
app.Usage = "SSL certificate chain resolver"
app.ArgsUsage = "[INPUT_FILE]"
app.Version = "1.0.4"
app.HideHelp = true
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "output, o",
Usage: "output to `OUTPUT_FILE` (default: stdout)",
Destination: &outputFilename,
},
cli.BoolFlag{
Name: "intermediate-only, i",
Usage: "output intermediate certificates only",
Destination: &outputIntermediateOnly,
},
cli.BoolFlag{
Name: "der, d",
Usage: "output DER format",
Destination: &outputDerFormat,
},
cli.BoolFlag{
Name: "include-system, s",
Usage: "include root CA from system in output",
Destination: &includeSystem,
},
}
app.Action = func(c *cli.Context) error {
args := c.Args()
if len(args) > 0 {
inputFilename = args[0]
}
err := run(inputFilename, outputFilename, outputIntermediateOnly, outputDerFormat, includeSystem)
return err
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}