Debian Codegraph @

command/ca/certificate.go

raw symbols SCIP
1 package ca
2
3 import (
4 "strings"
5
6 "github.com/pkg/errors"
7 "github.com/smallstep/cli/command"
8 "github.com/smallstep/cli/crypto/pemutil"
9 "github.com/smallstep/cli/errs"
10 "github.com/smallstep/cli/flags"
11 "github.com/smallstep/cli/token"
12 "github.com/smallstep/cli/ui"
13 "github.com/smallstep/cli/utils/cautils"
14 "github.com/urfave/cli"
15 )
16
17 func certificateCommand() cli.Command {
18 return cli.Command{
19 Name: "certificate",
20 Action: command.ActionFunc(certificateAction),
21 Usage: "generate a new private key and certificate signed by the root certificate",
22 UsageText: `**step ca certificate** <subject> <crt-file> <key-file>
23 [**--token**=<token>] [**--issuer**=<name>] [**--ca-url**=<uri>] [**--root**=<file>]
24 [**--not-before**=<time|duration>] [**--not-after**=<time|duration>]
25 [**--san**=<SAN>] [**--set**=<key=value>] [**--set-file**=<path>]
26 [**--acme**=<path>] [**--standalone**] [**--webroot**=<path>]
27 [**--contact**=<email>] [**--http-listen**=<address>] [**--bundle**]
28 [**--kty**=<type>] [**--curve**=<curve>] [**--size**=<size>] [**--console**]
29 [**--x5c-cert**=<path>] [**--x5c-key**=<path>] [**--k8ssa-token-path**=<file>`,
30 Description: `**step ca certificate** command generates a new certificate pair
31
32 ## POSITIONAL ARGUMENTS
33
34 <subject>
35 : The Common Name, DNS Name, or IP address that will be set as the
36 Subject Common Name for the certificate. If no Subject Alternative Names (SANs)
37 are configured (via the --san flag) then the <subject> will be set as the only SAN.
38
39 <crt-file>
40 : File to write the certificate (PEM format)
41
42 <key-file>
43 : File to write the private key (PEM format)
44
45 ## EXAMPLES
46
47 Request a new certificate for a given domain. There are no additional SANs
48 configured, therefore (by default) the <subject> will be used as the only
49 SAN extension: DNS Name internal.example.com:
50 '''
51 $ TOKEN=$(step ca token internal.example.com)
52 $ step ca certificate --token $TOKEN internal.example.com internal.crt internal.key
53 '''
54
55 Request a new certificate with multiple Subject Alternative Names. The Subject
56 Common Name of the certificate will be 'foobar'. However, because additional SANs are
57 configured using the --san flag and 'foobar' is not one of these, 'foobar' will
58 not be in the SAN extensions of the certificate. The certificate will have 2
59 IP Address extensions (1.1.1.1, 10.2.3.4) and 1 DNS Name extension (hello.example.com):
60 '''
61 $ step ca certificate --san 1.1.1.1 --san hello.example.com --san 10.2.3.4 foobar internal.crt internal.key
62 '''
63
64 Request a new certificate with a 1h validity:
65 '''
66 $ TOKEN=$(step ca token internal.example.com)
67 $ step ca certificate --token $TOKEN --not-after=1h internal.example.com internal.crt internal.key
68 '''
69
70 Request a new certificate using the offline mode, requires the configuration
71 files, certificates, and keys created with **step ca init**:
72 '''
73 $ step ca certificate --offline internal.example.com internal.crt internal.key
74 '''
75
76 Request a new certificate using an OIDC provisioner:
77 '''
78 $ step ca certificate --token $(step oauth --oidc --bare) [email protected] joe.crt joe.key
79 '''
80
81 Request a new certificate using an OIDC provisioner while remaining in the console:
82 '''
83 $ step ca certificate [email protected] joe.crt joe.key --issuer Google --console
84 '''
85
86 Request a new certificate with an RSA public key (default is ECDSA256):
87 '''
88 $ step ca certificate foo.internal foo.crt foo.key --kty RSA --size 4096
89 '''
90
91 Request a new certificate with an X5C provisioner:
92 '''
93 $ step ca certificate foo.internal foo.crt foo.key --x5c-cert x5c.cert --x5c-key x5c.key
94 '''
95
96 **Certificate Templates** - With a provisioner configured with a custom
97 template we can use the **--set** flag to pass user variables:
98 '''
99 $ step ca certificate foo.internal foo.crt foo.key --set [email protected]
100 $ step ca certificate foo.internal foo.crt foo.key --set emailAddresses='["[email protected]","[email protected]"]'
101 '''
102
103 Or you can pass them from a file using **--set-file**:
104 '''
105 $ cat path/to/data.json
106 {
107 "emailAddresses": ["[email protected]","[email protected]"]
108 }
109 $ step ca certificate foo.internal foo.crt foo.key --set-file path/to/data.json
110 '''
111
112 **step CA ACME** - In order to use the step CA ACME protocol you must add a
113 ACME provisioner to the step CA config. See **step ca provisioner add -h**.
114
115 Request a new certificate using the step CA ACME server and a standalone server
116 to serve the challenges locally (standalone mode is the default):
117 '''
118 $ step ca certificate foobar foo.crt foo.key --provisioner my-acme-provisioner --san foo.internal --san bar.internal
119 '''
120
121 Request a new certificate using the step CA ACME server and an existing server
122 along with webroot mode to serve the challenges locally:
123 '''
124 $ step ca certificate foobar foo.crt foo.key --provisioner my-acme-provisioner --webroot "./acme-www" \
125 --san foo.internal --san bar.internal
126 '''
127
128 Request a new certificate using the ACME protocol not served via the step CA
129 (e.g. letsencrypt). NOTE: Let's Encrypt requires that the Subject Common Name
130 of a requested certificate be validated as an Identifier in the ACME order along
131 with any other SANS. Therefore, the Common Name must be a valid DNS Name. The
132 step CA does not impose this requirement.
133 '''
134 $ step ca certificate foo.internal foo.crt foo.key \
135 --acme https://acme-staging-v02.api.letsencrypt.org/directory --san bar.internal
136 '''`,
137 Flags: []cli.Flag{
138 cli.StringSliceFlag{
139 Name: "san",
140 Usage: `Add <dns|ip|email|uri> Subject Alternative Name(s) (SANs)
141 that should be authorized. Use the '--san' flag multiple times to configure
142 multiple SANs. The '--san' flag and the '--token' flag are mutually exclusive.`,
143 },
144 flags.TemplateSet,
145 flags.TemplateSetFile,
146 flags.CaConfig,
147 flags.CaURL,
148 flags.Root,
149 flags.Token,
150 flags.Provisioner,
151 flags.ProvisionerPasswordFile,
152 flags.KTY,
153 flags.Curve,
154 flags.Size,
155 flags.NotAfter,
156 flags.NotBefore,
157 flags.Force,
158 flags.Offline,
159 consoleFlag,
160 flags.X5cCert,
161 flags.X5cKey,
162 acmeFlag,
163 acmeStandaloneFlag,
164 acmeWebrootFlag,
165 acmeContactFlag,
166 acmeHTTPListenFlag,
167 flags.K8sSATokenPathFlag,
168 },
169 }
170 }
171
172 func certificateAction(ctx *cli.Context) error {
173 if err := errs.NumberOfArguments(ctx, 3); err != nil {
174 return err
175 }
176
177 args := ctx.Args()
178 subject := args.Get(0)
179 crtFile, keyFile := args.Get(1), args.Get(2)
180
181 tok := ctx.String("token")
182 offline := ctx.Bool("offline")
183 sans := ctx.StringSlice("san")
184
185 // offline and token are incompatible because the token is generated before
186 // the start of the offline CA.
187 if offline && len(tok) != 0 {
188 return errs.IncompatibleFlagWithFlag(ctx, "offline", "token")
189 }
190
191 // certificate flow unifies online and offline flows on a single api
192 flow, err := cautils.NewCertificateFlow(ctx)
193 if err != nil {
194 return err
195 }
196
197 if len(tok) == 0 {
198 // Use the ACME protocol with a different certificate authority.
199 if ctx.IsSet("acme") {
200 return cautils.ACMECreateCertFlow(ctx, "")
201 }
202 if tok, err = flow.GenerateToken(ctx, subject, sans); err != nil {
203 switch k := err.(type) {
204 // Use the ACME flow with the step certificate authority.
205 case *cautils.ErrACMEToken:
206 return cautils.ACMECreateCertFlow(ctx, k.Name)
207 default:
208 return err
209 }
210 }
211 }
212
213 req, pk, err := flow.CreateSignRequest(ctx, tok, subject, sans)
214 if err != nil {
215 return err
216 }
217
218 jwt, err := token.ParseInsecure(tok)
219 if err != nil {
220 return err
221 }
222
223 switch jwt.Payload.Type() {
224 case token.JWK: // Validate that subject matches the CSR common name.
225 if ctx.String("token") != "" && len(sans) > 0 {
226 return errs.MutuallyExclusiveFlags(ctx, "token", "san")
227 }
228 if !strings.EqualFold(subject, req.CsrPEM.Subject.CommonName) {
229 return errors.Errorf("token subject '%s' and argument '%s' do not match", req.CsrPEM.Subject.CommonName, subject)
230 }
231 case token.OIDC: // Validate that the subject matches an email SAN
232 if len(req.CsrPEM.EmailAddresses) == 0 {
233 return errors.New("unexpected token: payload does not contain an email claim")
234 }
235 if email := req.CsrPEM.EmailAddresses[0]; email != subject {
236 return errors.Errorf("token email '%s' and argument '%s' do not match", email, subject)
237 }
238 case token.AWS, token.GCP, token.Azure, token.K8sSA:
239 // Common name will be validated on the server side, it depends on
240 // server configuration.
241 default:
242 return errors.New("token is not supported")
243 }
244
245 if err = flow.Sign(ctx, tok, req.CsrPEM, crtFile); err != nil {
246 return err
247 }
248
249 _, err = pemutil.Serialize(pk, pemutil.ToFile(keyFile, 0600))
250 if err != nil {
251 return err
252 }
253
254 ui.PrintSelected("Certificate", crtFile)
255 ui.PrintSelected("Private Key", keyFile)
256 return nil
257 }