| 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/identity" |
| 11 |
"github.com/smallstep/cli/command" |
| 12 |
"github.com/smallstep/cli/crypto/keys" |
| 13 |
"github.com/smallstep/cli/crypto/pemutil" |
| 14 |
"github.com/smallstep/cli/errs" |
| 15 |
"github.com/smallstep/cli/flags" |
| 16 |
"github.com/smallstep/cli/ui" |
| 17 |
"github.com/smallstep/cli/utils" |
| 18 |
"github.com/smallstep/cli/utils/cautils" |
| 19 |
"github.com/urfave/cli" |
| 20 |
"golang.org/x/crypto/ssh" |
| 21 |
) |
| 22 |
|
| 23 |
func rekeyCommand() cli.Command { |
| 24 |
return cli.Command{ |
| 25 |
Name: "rekey", |
| 26 |
Action: command.ActionFunc(rekeyAction), |
| 27 |
Usage: "rekey a SSH certificate using the SSH CA", |
| 28 |
UsageText: `**step ssh rekey** <ssh-cert> <ssh-key> |
| 29 |
[**--out**=<file>] [**--issuer**=<name>] [**--password-file**=<path>] |
| 30 |
[**--force**] [**--ca-url**=<uri>] [**--root**=<path>] |
| 31 |
[**--offline**] [**--ca-config**=<path>]`, |
| 32 |
Description: `**step ssh rekey** command generates a new SSH Certificate and key using |
| 33 |
an existing SSH Cerfificate and key pair to authenticate and templatize the |
| 34 |
request. It writes the new certificate to disk - either overwriting |
| 35 |
<ssh-cert> or using new files when the **--out**=<file> flag is used. |
| 36 |
|
| 37 |
## POSITIONAL ARGUMENTS |
| 38 |
|
| 39 |
<ssh-cert> |
| 40 |
: The ssh certificate to renew. |
| 41 |
|
| 42 |
<ssh-key> |
| 43 |
: The ssh certificate private key. |
| 44 |
|
| 45 |
## EXAMPLES |
| 46 |
|
| 47 |
Rekey an ssh certificate: |
| 48 |
''' |
| 49 |
$ step ssh rekey id_ecdsa-cert.pub id_ecdsa |
| 50 |
''' |
| 51 |
|
| 52 |
Rekey an ssh certificate creating id2_ecdsa, id2_ecdsa.pub, and id2_ecdsa-cert.pub: |
| 53 |
''' |
| 54 |
$ step ssh rekey --out id2_ecdsa id_ecdsa-cert.pub id_ecdsa |
| 55 |
'''`, |
| 56 |
Flags: []cli.Flag{ |
| 57 |
cli.StringFlag{ |
| 58 |
Name: "out", |
| 59 |
Usage: `The new key <file> path. Defaults to overwriting the <ssh-cert> positional argument.`, |
| 60 |
}, |
| 61 |
flags.Provisioner, |
| 62 |
sshProvisionerPasswordFlag, |
| 63 |
flags.NoPassword, |
| 64 |
flags.Insecure, |
| 65 |
flags.Force, |
| 66 |
flags.CaURL, |
| 67 |
flags.Root, |
| 68 |
flags.Offline, |
| 69 |
flags.CaConfig, |
| 70 |
flags.SSHPOPCert, |
| 71 |
flags.SSHPOPKey, |
| 72 |
}, |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func rekeyAction(ctx *cli.Context) error { |
| 77 |
if err := errs.NumberOfArguments(ctx, 2); err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
|
| 81 |
args := ctx.Args() |
| 82 |
certFile := args.Get(0) |
| 83 |
keyFile := args.Get(1) |
| 84 |
|
| 85 |
|
| 86 |
var newPubFile, newCertFile, newKeyFile string |
| 87 |
if out := ctx.String("out"); out != "" { |
| 88 |
newPubFile = out + ".pub" |
| 89 |
newCertFile = out + "-cert.pub" |
| 90 |
newKeyFile = out |
| 91 |
} else { |
| 92 |
newPubFile = keyFile + ".pub" |
| 93 |
newCertFile = certFile |
| 94 |
newKeyFile = keyFile |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
passwordFile := ctx.String("password-file") |
| 99 |
noPassword := ctx.Bool("no-password") |
| 100 |
insecure := ctx.Bool("insecure") |
| 101 |
|
| 102 |
flow, err := cautils.NewCertificateFlow(ctx) |
| 103 |
if err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
certBytes, err := ioutil.ReadFile(certFile) |
| 109 |
if err != nil { |
| 110 |
return errors.Wrapf(err, "error reading ssh certificate from %s", certFile) |
| 111 |
} |
| 112 |
sshpub, _, _, _, err := ssh.ParseAuthorizedKey(certBytes) |
| 113 |
if err != nil { |
| 114 |
return errors.Wrapf(err, "error parsing ssh public key from %s", certFile) |
| 115 |
} |
| 116 |
cert, ok := sshpub.(*ssh.Certificate) |
| 117 |
if !ok { |
| 118 |
return errors.New("error casting ssh public key to ssh certificate") |
| 119 |
} |
| 120 |
serial := strconv.FormatUint(cert.Serial, 10) |
| 121 |
|
| 122 |
ctx.Set("sshpop-cert", certFile) |
| 123 |
ctx.Set("sshpop-key", keyFile) |
| 124 |
token, err := flow.GenerateSSHToken(ctx, serial, cautils.SSHRekeyType, nil, provisioner.TimeDuration{}, provisioner.TimeDuration{}) |
| 125 |
if err != nil { |
| 126 |
return err |
| 127 |
} |
| 128 |
|
| 129 |
caClient, err := flow.GetClient(ctx, token) |
| 130 |
if err != nil { |
| 131 |
return err |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
pub, priv, err := keys.GenerateDefaultKeyPair() |
| 136 |
if err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
|
| 140 |
sshPub, err := ssh.NewPublicKey(pub) |
| 141 |
if err != nil { |
| 142 |
return errors.Wrap(err, "error creating public key") |
| 143 |
} |
| 144 |
|
| 145 |
resp, err := caClient.SSHRekey(&api.SSHRekeyRequest{ |
| 146 |
OTT: token, |
| 147 |
PublicKey: sshPub.Marshal(), |
| 148 |
}) |
| 149 |
if err != nil { |
| 150 |
return err |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
opts := []pemutil.Options{ |
| 155 |
pemutil.WithOpenSSH(true), |
| 156 |
pemutil.ToFile(newKeyFile, 0600), |
| 157 |
} |
| 158 |
switch { |
| 159 |
case noPassword && insecure: |
| 160 |
case passwordFile != "": |
| 161 |
opts = append(opts, pemutil.WithPasswordFile(passwordFile)) |
| 162 |
default: |
| 163 |
opts = append(opts, pemutil.WithPasswordPrompt("Please enter the password to encrypt the private key")) |
| 164 |
} |
| 165 |
_, err = pemutil.Serialize(priv, opts...) |
| 166 |
if err != nil { |
| 167 |
return err |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
if err := utils.WriteFile(newPubFile, marshalPublicKey(sshPub, cert.KeyId), 0644); err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if err := utils.WriteFile(newCertFile, marshalPublicKey(resp.Certificate, cert.KeyId), 0644); err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if len(resp.IdentityCertificate) > 0 { |
| 182 |
if err := identity.WriteIdentityCertificate(resp.IdentityCertificate); err != nil { |
| 183 |
return err |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
ui.PrintSelected("Private Key", newKeyFile) |
| 188 |
ui.PrintSelected("Public Key", newPubFile) |
| 189 |
ui.PrintSelected("Certificate", newCertFile) |
| 190 |
|
| 191 |
return nil |
| 192 |
} |