Debian Codegraph @

command/certificate/install.go

raw symbols SCIP
1 package certificate
2
3 import (
4 "fmt"
5 "strings"
6
7 "github.com/pkg/errors"
8 "github.com/smallstep/certinfo"
9 "github.com/smallstep/cli/command"
10 "github.com/smallstep/cli/crypto/pemutil"
11 "github.com/smallstep/cli/errs"
12 "github.com/smallstep/truststore"
13 "github.com/urfave/cli"
14 )
15
16 func installCommand() cli.Command {
17 return cli.Command{
18 Name: "install",
19 Action: command.ActionFunc(installAction),
20 Usage: "install a root certificate in the system truststore",
21 UsageText: `**step certificate install** <crt-file>
22 [**--prefix**=<name>] [**--all**]
23 [**--java**] [**--firefox**] [**--no-system**]`,
24 Description: `**step certificate install** installs a root certificate in the system
25 truststore.
26
27 Java and Firefox truststores are also supported via the respective flags.
28
29 ## POSITIONAL ARGUMENTS
30
31 <crt-file>
32 : Certificate to install in the system truststore
33
34 ## EXAMPLES
35
36 Install a certificate in the system truststore:
37 '''
38 $ step certificate install root-ca.pem
39 '''
40
41 Install a certificate in all the supported truststores:
42 '''
43 $ step certificate install --all root-ca.pem
44 '''
45
46 Install a certificate in Firefox and the system trustore:
47 '''
48 $ step certificate install --firefox root--ca.pem
49 '''
50
51 Install a certificate in Java and the system trustore:
52 '''
53 $ step certificate install --java root-ca.pem
54 '''
55
56 Install a certificate in Firefox, Java, but not in the system trustore:
57 '''
58 $ step certificate install --firefox --java --no-system root-ca.pem
59 '''`,
60 Flags: []cli.Flag{
61 cli.StringFlag{
62 Name: "prefix",
63 Usage: `The prefix used to <name> the CA in the truststore. Defaults to the
64 certificate common name.`,
65 },
66 cli.BoolFlag{
67 Name: "java",
68 Usage: "install on the Java key store",
69 },
70 cli.BoolFlag{
71 Name: "firefox",
72 Usage: "install on the Firefox NSS security database",
73 },
74 cli.BoolFlag{
75 Name: "no-system",
76 Usage: "disables the install on the system truststore",
77 },
78 cli.BoolFlag{
79 Name: "all",
80 Usage: "install on the system, Firefox and Java truststores",
81 },
82 },
83 }
84 }
85
86 func uninstallCommand() cli.Command {
87 return cli.Command{
88 Name: "uninstall",
89 Action: command.ActionFunc(uninstallAction),
90 Usage: "uninstall a root certificate from the system truststore",
91 UsageText: `**step certificate uninstall** <crt-file>
92 [**--prefix**=<name>] [**--all**]
93 [**--java**] [**--firefox**] [**--no-system**]`,
94 Description: `**step certificate install** uninstalls a root certificate from the system
95 truststore.
96
97 Java and Firefox truststores are also supported via the respective flags.
98
99 ## POSITIONAL ARGUMENTS
100
101 <crt-file>
102 : Certificate to uninstall from the system truststore
103
104 ## EXAMPLES
105
106 Uninstall from only the system truststore:
107 '''
108 $ step certificate uninstall root-ca.pem
109 '''
110
111 Uninstall a certificate from all the supported truststores:
112 '''
113 $ step certificate uninstall --all root-ca.pem
114 '''
115
116 Uninstall a certificate from Firefox and the system trustore:
117 '''
118 $ step certificate uninstall --firefox root--ca.pem
119 '''
120
121 Uninstall a certificate infrom Java and the system trustore:
122 '''
123 $ step certificate uninstall --java root-ca.pem
124 '''
125
126 Uninstall a certificate from Firefox, Java, but not from the system:
127 '''
128 $ step certificate uninstall --firefox --java --no-system root-ca.pem
129 '''`,
130 Flags: []cli.Flag{
131 cli.StringFlag{
132 Name: "prefix",
133 Usage: `The prefix used to <name> the CA in the truststore. Defaults to the
134 certificate common name.`,
135 },
136 cli.BoolFlag{
137 Name: "java",
138 Usage: "uninstall from the Java key store",
139 },
140 cli.BoolFlag{
141 Name: "firefox",
142 Usage: "uninstall from the Firefox NSS security database",
143 },
144 cli.BoolFlag{
145 Name: "no-system",
146 Usage: "disables the uninstall from the system truststore",
147 },
148 cli.BoolFlag{
149 Name: "all",
150 Usage: "uninstall from the system, Firefox and Java truststores",
151 },
152 },
153 }
154 }
155
156 func installAction(ctx *cli.Context) error {
157 if err := errs.NumberOfArguments(ctx, 1); err != nil {
158 return err
159 }
160
161 filename := ctx.Args().Get(0)
162 opts, err := getTruststoreOptions(ctx)
163 if err != nil {
164 return err
165 }
166
167 if err := truststore.InstallFile(filename, opts...); err != nil {
168 switch err := err.(type) {
169 case *truststore.CmdError:
170 return errors.Errorf("failed to execute \"%s\" failed with: %s", strings.Join(err.Cmd().Args, " "), err.Err())
171 default:
172 return errors.Wrapf(err, "failed to install %s", filename)
173 }
174 }
175
176 fmt.Printf("Certificate %s has been installed.\n", filename)
177 // Print certificate info (ignore errors)
178 if cert, err := pemutil.ReadCertificate(filename); err == nil {
179 if s, err := certinfo.CertificateShortText(cert); err == nil {
180 fmt.Print(s)
181 }
182 }
183
184 return nil
185 }
186
187 func uninstallAction(ctx *cli.Context) error {
188 if err := errs.NumberOfArguments(ctx, 1); err != nil {
189 return err
190 }
191
192 filename := ctx.Args().Get(0)
193 opts, err := getTruststoreOptions(ctx)
194 if err != nil {
195 return err
196 }
197
198 if err := truststore.UninstallFile(filename, opts...); err != nil {
199 switch err := err.(type) {
200 case *truststore.CmdError:
201 return errors.Errorf("failed to execute \"%s\" failed with: %s", strings.Join(err.Cmd().Args, " "), err.Err())
202 default:
203 return errors.Wrapf(err, "failed to uninstall %s", filename)
204 }
205 }
206
207 fmt.Printf("Certificate %s has been removed.\n", filename)
208 // Print certificate info (ignore errors)
209 if cert, err := pemutil.ReadCertificate(filename); err == nil {
210 if s, err := certinfo.CertificateShortText(cert); err == nil {
211 fmt.Print(s)
212 }
213 }
214
215 return nil
216 }
217
218 func getTruststoreOptions(ctx *cli.Context) ([]truststore.Option, error) {
219 cert, err := pemutil.ReadCertificate(ctx.Args().Get(0))
220 if err != nil {
221 return nil, err
222 }
223
224 if !cert.IsCA || cert.CheckSignatureFrom(cert) != nil {
225 return nil, errors.Errorf("certificate %s is not a root CA", ctx.Args().Get(0))
226 }
227
228 prefix := ctx.String("prefix")
229 if prefix == "" {
230 if len(cert.Subject.CommonName) > 0 {
231 prefix = cert.Subject.CommonName + " "
232 } else {
233 prefix = "Smallstep Development CA "
234 }
235 }
236
237 opts := []truststore.Option{
238 truststore.WithPrefix(prefix),
239 }
240
241 if ctx.Bool("all") {
242 opts = append(opts, truststore.WithJava(), truststore.WithFirefox())
243 } else {
244 if ctx.Bool("java") {
245 opts = append(opts, truststore.WithJava())
246 }
247 if ctx.Bool("firefox") {
248 opts = append(opts, truststore.WithFirefox())
249 }
250 }
251 if ctx.Bool("no-system") {
252 opts = append(opts, truststore.WithNoSystem())
253 }
254 return opts, nil
255 }