Debian Codegraph @

command/ssh/revoke.go

raw symbols SCIP
1 package ssh
2
3 import (
4 "io/ioutil"
5 "strconv"
6
7 "github.com/pkg/errors"
8 "github.com/smallstep/certificates/api"
9 "github.com/smallstep/certificates/authority/provisioner"
10 "github.com/smallstep/certificates/ca"
11 "github.com/smallstep/cli/command"
12 cmdca "github.com/smallstep/cli/command/ca"
13 "github.com/smallstep/cli/errs"
14 "github.com/smallstep/cli/flags"
15 "github.com/smallstep/cli/ui"
16 "github.com/smallstep/cli/utils/cautils"
17 "github.com/urfave/cli"
18 "golang.org/x/crypto/ssh"
19 )
20
21 func revokeCommand() cli.Command {
22 return cli.Command{
23 Name: "revoke",
24 Action: command.ActionFunc(revokeAction),
25 Usage: "revoke a SSH certificate using the SSH CA",
26 UsageText: `**step ssh revoke** <serial-number>
27 [**--token**=<token>] [**--issuer**=<name>]
28 [**--set**=<key=value>] [**--set-file**=<path>]
29 [**--ca-url**=<uri>] [**--root**=<path>]
30 [**--ca-config**=<path>] [**--password-file**=<path>] [**--offline**]
31 [**--reason**=<string>] [**--reasonCode**=<code>]
32 [**--sshpop-cert**=<path>] [**--sshpop-key**=<key>]`,
33 Description: `**step ssh revoke** command revokes an SSH Cerfificate
34 using [step certificates](https://github.com/smallstep/certificates).
35
36 ## POSITIONAL ARGUMENTS
37
38 <serial-number>
39 : The serial number of the SSH certificate to revoke.
40
41 ## EXAMPLES
42
43 revoke an ssh certificate:
44 '''
45 $ step ssh revoke 3997477584487736496
46 '''`,
47 Flags: []cli.Flag{
48 flags.Token,
49 sshProvisionerPasswordFlag,
50 flags.Provisioner,
51 flags.TemplateSet,
52 flags.TemplateSetFile,
53 flags.CaURL,
54 flags.Root,
55 flags.Offline,
56 flags.CaConfig,
57 flags.SSHPOPCert,
58 flags.SSHPOPKey,
59 cli.StringFlag{
60 Name: "reason",
61 Usage: `The <string> representing the reason for which the cert is being revoked.`,
62 },
63 cli.StringFlag{
64 Name: "reasonCode",
65 Value: "",
66 Usage: `The <reasonCode> specifies the reason for revocation - chose from a list of
67 common revocation reasons. If unset, the default is Unspecified.
68
69 : <reasonCode> can be a number from 0-9 or a case insensitive string matching
70 one of the following options:
71
72 **Unspecified**
73 : No reason given (Default -- reasonCode=0).
74
75 **KeyCompromise**
76 : The key is believed to have been compromised (reasonCode=1).
77
78 **CACompromise**
79 : The issuing Certificate Authority itself has been compromised (reasonCode=2).
80
81 **AffiliationChanged**
82 : The certificate contained affiliation information, for example, it may
83 have been an EV certificate and the associated business is no longer owned by
84 the same entity (reasonCode=3).
85
86 **Superseded**
87 : The certificate is being replaced (reasonCode=4).
88
89 **CessationOfOperation**
90 : If a CA is decommissioned, no longer to be used, the CA's certificate
91 should be revoked with this reason code. Do not revoke the CA's certificate if
92 the CA no longer issues new certificates, yet still publishes CRLs for the
93 currently issued certificates (reasonCode=5).
94
95 **CertificateHold**
96 : A temporary revocation that indicates that a CA will not vouch for a
97 certificate at a specific point in time. Once a certificate is revoked with a
98 CertificateHold reason code, the certificate can then be revoked with another
99 Reason Code, or unrevoked and returned to use (reasonCode=6).
100
101 **RemoveFromCRL**
102 : If a certificate is revoked with the CertificateHold reason code, it is
103 possible to "unrevoke" a certificate. The unrevoking process still lists the
104 certificate in the CRL, but with the reason code set to RemoveFromCRL.
105 Note: This is specific to the CertificateHold reason and is only used in DeltaCRLs
106 (reasonCode=8).
107
108 **PrivilegeWithdrawn**
109 : The right to represent the given entity was revoked for some reason
110 (reasonCode=9).
111
112 **AACompromise**
113 : It is known or suspected that aspects of the AA validated in the
114 attribute certificate have been compromised (reasonCode=10).
115 `,
116 },
117 },
118 }
119 }
120
121 func revokeAction(ctx *cli.Context) error {
122 args := ctx.Args()
123 token := ctx.String("token")
124 var serial string
125
126 switch ctx.NArg() {
127 case 0:
128 certFile := ctx.String("sshpop-cert")
129 keyFile := ctx.String("sshpop-key")
130 if len(certFile) == 0 || len(keyFile) == 0 {
131 return errors.New("--sshpop-cert and --sshpop-key must be supplied if serial number is not supplied as first argument")
132 }
133 // Load the cert, because we need the serial number.
134 certBytes, err := ioutil.ReadFile(certFile)
135 if err != nil {
136 return errors.Wrapf(err, "error reading ssh certificate from %s", certFile)
137 }
138 sshpub, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
139 if err != nil {
140 return errors.Wrapf(err, "error parsing ssh public key from %s", certFile)
141 }
142 cert, ok := sshpub.(*ssh.Certificate)
143 if !ok {
144 return errors.New("error casting ssh public key to ssh certificate")
145 }
146 serial = strconv.FormatUint(cert.Serial, 10)
147 case 1:
148 serial = args.Get(0)
149 default:
150 return errs.TooManyArguments(ctx)
151 }
152
153 reason := ctx.String("reason")
154 // Convert the reasonCode flag to an OCSP revocation code.
155 reasonCode, err := cmdca.ReasonCodeToNum(ctx.String("reasonCode"))
156 if err != nil {
157 return err
158 }
159
160 flow, err := cautils.NewCertificateFlow(ctx)
161 if err != nil {
162 return err
163 }
164
165 if len(token) == 0 {
166 token, err = flow.GenerateSSHToken(ctx, serial, cautils.SSHRevokeType, nil, provisioner.TimeDuration{}, provisioner.TimeDuration{})
167 if err != nil {
168 return err
169 }
170 }
171
172 // Prepare retry function
173 retryFunc, err := loginOnUnauthorized(ctx)
174 if err != nil {
175 return err
176 }
177
178 caClient, err := flow.GetClient(ctx, token, ca.WithRetryFunc(retryFunc))
179 if err != nil {
180 return err
181 }
182
183 _, err = caClient.SSHRevoke(&api.SSHRevokeRequest{
184 Serial: serial,
185 Reason: reason,
186 ReasonCode: reasonCode,
187 OTT: token,
188 Passive: true,
189 })
190 if err != nil {
191 return err
192 }
193
194 ui.Printf("SSH Certificate with Serial Number %s has been revoked.\n", serial)
195 return nil
196 }