Debian Codegraph @

command/ca/sign.go

raw symbols SCIP
1 package ca
2
3 import (
4 "crypto/x509"
5 "strings"
6
7 "github.com/pkg/errors"
8 "github.com/smallstep/certificates/api"
9 "github.com/smallstep/cli/command"
10 "github.com/smallstep/cli/crypto/pemutil"
11 "github.com/smallstep/cli/errs"
12 "github.com/smallstep/cli/flags"
13 "github.com/smallstep/cli/token"
14 "github.com/smallstep/cli/ui"
15 "github.com/smallstep/cli/utils/cautils"
16 "github.com/urfave/cli"
17 )
18
19 func signCertificateCommand() cli.Command {
20 return cli.Command{
21 Name: "sign",
22 Action: command.ActionFunc(signCertificateAction),
23 Usage: "generate a new certificate signing a certificate request",
24 UsageText: `**step ca sign** <csr-file> <crt-file>
25 [**--token**=<token>] [**--issuer**=<name>] [**--provisioner-password-file=<file>]
26 [**--not-before**=<time|duration>] [**--not-after**=<time|duration>]
27 [**--ca-url**=<uri>] [**--root**=<path>]
28 [**--set**=<key=value>] [**--set-file**=<path>]
29 [**--acme**=<uri>] [**--standalone**] [**--webroot**=<path>]
30 [**--contact**=<email>] [**--http-listen**=<address>] [**--console**]
31 [**--x5c-cert**=<path>] [**--x5c-key**=<path>]
32 [**--k8ssa-token-path**=<path>]`,
33 Description: `**step ca sign** command signs the given csr and generates a new certificate.
34
35 ## POSITIONAL ARGUMENTS
36
37 <csr-file>
38 : File with the certificate signing request (PEM format)
39
40 <crt-file>
41 : File to write the certificate (PEM format)
42
43 ## EXAMPLES
44
45 Sign a new certificate for the given CSR:
46 '''
47 $ TOKEN=$(step ca token internal.example.com)
48 $ step ca sign --token $TOKEN internal.csr internal.crt
49 '''
50
51 Sign a new certificate with a 1h validity:
52 '''
53 $ TOKEN=$(step ca token internal.example.com)
54 $ step ca sign --token $TOKEN --not-after=1h internal.csr internal.crt
55 '''
56
57 Sign a new certificate using the offline mode, requires the configuration
58 files, certificates, and keys created with **step ca init**:
59 '''
60 $ step ca sign --offline internal internal.csr internal.crt
61 '''
62
63 Sign a new certificate using an X5C provisioner:
64 NOTE: You must have a X5C provisioner configured (using **step ca provisioner add**).
65 '''
66 $ step ca sign foo.internal foo.csr foo.crt --x5c-cert leaf-x5c.crt --x5c-key leaf-x5c.key
67 '''
68
69 **Certificate Templates** - With a provisioner configured with a custom
70 template we can use the **--set** flag to pass user variables:
71 '''
72 $ step ca sign foo.csr foo.crt --set dnsNames=foo.internal.com
73 $ step ca sign foo.csr foo.crt --set dnsNames='["foo.internal.com","bar.internal.com"]'
74 '''
75
76 Or you can pass them from a file using **--set-file**:
77 '''
78 $ cat path/to/data.json
79 {
80 "dnsNames": ["foo.internal.com","bar.internal.com"]
81 }
82 $ step ca sign foo.csr foo.crt --set-file path/to/data.json
83 '''
84
85 **step CA ACME** - In order to use the step CA ACME protocol you must add a
86 ACME provisioner to the step CA config. See **step ca provisioner add -h**.
87
88 Sign a CSR using the step CA ACME server and a standalone server
89 to serve the challenges locally (standalone mode is the default):
90 '''
91 $ step ca sign foo.csr foo.crt --provisioner my-acme-provisioner
92 '''
93
94 Sign a CSR using the step CA ACME server and an existing server
95 along with webroot mode to serve the challenges locally:
96 '''
97 $ step ca sign foo.csr foo.crt \
98 --provisioner my-acme-provisioner --webroot "./acme-www" \
99 '''
100
101 Sign a CSR using the ACME protocol served by another online CA (not step CA,
102 e.g. letsencrypt). NOTE: Let's Encrypt requires that the Subject Common Name
103 of a requested certificate be validated as an Identifier in the ACME order along
104 with any other SANS. Therefore, the Common Name must be a valid DNS Name. The
105 step CA does not impose this requirement.
106 '''
107 $ step ca sign foo.csr foo.crt \
108 --acme https://acme-staging-v02.api.letsencrypt.org/directory
109 '''`,
110 Flags: []cli.Flag{
111 flags.CaConfig,
112 flags.CaURL,
113 flags.Root,
114 flags.Token,
115 flags.Provisioner,
116 flags.ProvisionerPasswordFile,
117 flags.NotBefore,
118 flags.NotAfter,
119 flags.TemplateSet,
120 flags.TemplateSetFile,
121 flags.Force,
122 flags.Offline,
123 consoleFlag,
124 flags.X5cCert,
125 flags.X5cKey,
126 acmeFlag,
127 acmeStandaloneFlag,
128 acmeWebrootFlag,
129 acmeContactFlag,
130 acmeHTTPListenFlag,
131 flags.K8sSATokenPathFlag,
132 },
133 }
134 }
135
136 func signCertificateAction(ctx *cli.Context) error {
137 if err := errs.NumberOfArguments(ctx, 2); err != nil {
138 return err
139 }
140
141 args := ctx.Args()
142 csrFile := args.Get(0)
143 crtFile := args.Get(1)
144 tok := ctx.String("token")
145 offline := ctx.Bool("offline")
146
147 csrInt, err := pemutil.Read(csrFile)
148 if err != nil {
149 return err
150 }
151 csr, ok := csrInt.(*x509.CertificateRequest)
152 if !ok {
153 return errors.Errorf("error parsing %s: file is not a certificate request", csrFile)
154 }
155 if err = csr.CheckSignature(); err != nil {
156 return errors.Wrapf(err, "csr has invalid signature")
157 }
158
159 // offline and token are incompatible because the token is generated before
160 // the start of the offline CA.
161 if offline && len(tok) != 0 {
162 return errs.IncompatibleFlagWithFlag(ctx, "offline", "token")
163 }
164
165 // certificate flow unifies online and offline flows on a single api
166 flow, err := cautils.NewCertificateFlow(ctx)
167 if err != nil {
168 return err
169 }
170
171 if len(tok) == 0 {
172 // Use the ACME protocol with a different certificate authority.
173 if ctx.IsSet("acme") {
174 return cautils.ACMESignCSRFlow(ctx, csr, crtFile, "")
175 }
176 sans := mergeSans(ctx, csr)
177 if tok, err = flow.GenerateToken(ctx, csr.Subject.CommonName, sans); err != nil {
178 switch k := err.(type) {
179 // Use the ACME flow with the step certificate authority.
180 case *cautils.ErrACMEToken:
181 return cautils.ACMESignCSRFlow(ctx, csr, crtFile, k.Name)
182 default:
183 return err
184 }
185 }
186 }
187
188 // Validate common name
189 jwt, err := token.ParseInsecure(tok)
190 if err != nil {
191 return errors.Wrap(err, "error parsing flag '--token'")
192 }
193 switch jwt.Payload.Type() {
194 case token.AWS, token.GCP, token.Azure, token.K8sSA:
195 // Common name will be validated on the server side, it depends on
196 // server configuration.
197 default:
198 if !strings.EqualFold(jwt.Payload.Subject, csr.Subject.CommonName) {
199 return errors.Errorf("token subject '%s' and CSR CommonName '%s' do not match", jwt.Payload.Subject, csr.Subject.CommonName)
200 }
201 }
202
203 // Sign
204 if err := flow.Sign(ctx, tok, api.NewCertificateRequest(csr), crtFile); err != nil {
205 return err
206 }
207
208 ui.PrintSelected("Certificate", crtFile)
209 return nil
210 }
211
212 func mergeSans(ctx *cli.Context, csr *x509.CertificateRequest) []string {
213 uniq := make([]string, 0)
214 m := make(map[string]bool)
215 for _, s := range ctx.StringSlice("san") {
216 if _, ok := m[s]; !ok {
217 uniq = append(uniq, s)
218 m[s] = true
219 }
220 }
221 for _, s := range csr.DNSNames {
222 if _, ok := m[s]; !ok {
223 uniq = append(uniq, s)
224 m[s] = true
225 }
226 }
227 for _, ip := range csr.IPAddresses {
228 s := ip.String()
229 if _, ok := m[s]; !ok {
230 uniq = append(uniq, s)
231 m[s] = true
232 }
233 }
234 for _, s := range csr.EmailAddresses {
235 if _, ok := m[s]; !ok {
236 uniq = append(uniq, s)
237 m[s] = true
238 }
239 }
240 return uniq
241 }