Skip to content

Commit aeee3d0

Browse files
redracmaraino
authored andcommitted
Add support for setting ssh key types
Add --kty, --curve, and --size to ssh commands (login, certificate) Implements PR #477
1 parent bf70d3e commit aeee3d0

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

command/ssh/certificate.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func certificateCommand() cli.Command {
4141
[**--not-after**=<time|duration>] [**--token**=<token>] [**--issuer**=<name>]
4242
[**--no-password**] [**--insecure**] [**--force**] [**--x5c-cert**=<file>]
4343
[**--x5c-key**=<file>] [**--k8ssa-token-path**=<file>] [**--no-agent**]
44-
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]`,
44+
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]
45+
[**--kty**=<key-type>] [**--curve**=<curve>] [**--size**=<size>]`,
4546

4647
Description: `**step ssh certificate** command generates an SSH key pair and creates a
4748
certificate using [step certificates](https://github.com/smallstep/certificates).
@@ -150,7 +151,20 @@ $ step ssh certificate --principal max --principal mariano --sign \
150151
Generate a new key pair and a certificate using a given token:
151152
'''
152153
$ step ssh certificate --token $TOKEN mariano@work id_ecdsa
154+
'''
155+
156+
Create an EC pair with curve P-521 and certificate:
157+
158+
'''
159+
$ step ssh certificate --kty EC --curve "P-521" mariano@work id_ecdsa
160+
'''
161+
162+
Create an Octet Key Pair with curve Ed25519 and certificate:
163+
164+
'''
165+
$ step ssh certificate --kty OKP --curve Ed25519 mariano@work id_ed25519
153166
'''`,
167+
154168
Flags: []cli.Flag{
155169
flags.Force,
156170
flags.Insecure,
@@ -185,6 +199,9 @@ $ step ssh certificate --token $TOKEN mariano@work id_ecdsa
185199
flags.CaURL,
186200
flags.Root,
187201
flags.Context,
202+
flags.KTY,
203+
flags.Curve,
204+
flags.Size,
188205
},
189206
}
190207
}
@@ -223,6 +240,11 @@ func certificateAction(ctx *cli.Context) error {
223240
return err
224241
}
225242

243+
kty, curve, size, err := utils.GetKeyDetailsFromCLI(ctx, insecure, "kty", "curve", "size")
244+
if err != nil {
245+
return err
246+
}
247+
226248
// Validation
227249
switch {
228250
case noPassword && !insecure:
@@ -374,7 +396,7 @@ func certificateAction(ctx *cli.Context) error {
374396
}
375397
} else {
376398
// Generate keypair
377-
pub, priv, err = keyutil.GenerateDefaultKeyPair()
399+
pub, priv, err = keyutil.GenerateKeyPair(kty, curve, size)
378400
if err != nil {
379401
return err
380402
}
@@ -389,7 +411,7 @@ func certificateAction(ctx *cli.Context) error {
389411
var sshAuPubBytes []byte
390412
var auPub, auPriv interface{}
391413
if isAddUser {
392-
auPub, auPriv, err = keyutil.GenerateDefaultKeyPair()
414+
auPub, auPriv, err = keyutil.GenerateKeyPair(kty, curve, size)
393415
if err != nil {
394416
return err
395417
}

command/ssh/login.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/smallstep/certificates/ca"
1111
"github.com/smallstep/cli/flags"
1212
"github.com/smallstep/cli/internal/sshutil"
13+
"github.com/smallstep/cli/utils"
1314
"github.com/smallstep/cli/utils/cautils"
1415
"github.com/urfave/cli"
1516
"go.step.sm/cli-utils/command"
@@ -27,9 +28,10 @@ func loginCommand() cli.Command {
2728
UsageText: `**step ssh login** [<identity>]
2829
[**--token**=<token>] [**--provisioner**=<name>] [**--provisioner-password-file**=<file>]
2930
[**--principal**=<string>] [**--not-before**=<time|duration>] [**--not-after**=<time|duration>]
30-
[**--set**=<key=value>] [**--set-file**=<file>] [**--force**]
31+
[**--set**=<key=value>] [**--set-file**=<file>] [**--force**] [**--insecure**]
3132
[**--offline**] [**--ca-config**=<file>]
32-
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]`,
33+
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]
34+
[**--kty**=<key-type>] [**--curve**=<curve>] [**--size**=<size>]`,
3335
Description: `**step ssh login** generates a new SSH key pair and send a request to [step
3436
certificates](https://github.com/smallstep/certificates) to sign a user
3537
certificate. This certificate will be automatically added to the SSH agent.
@@ -64,6 +66,17 @@ $ step ssh login --not-after 1h alice
6466
Request a new SSH certificate with multiple principals:
6567
'''
6668
$ step ssh login --principal admin --principal bob bob@smallstep.com
69+
'''
70+
71+
Request a new SSH certificate with an EC key and P-521 curve:
72+
'''
73+
$ step ssh certificate --kty EC --curve "P-521" mariano@work id_ecdsa
74+
'''
75+
76+
Request a new SSH certificate with an Octet Key Pair and Ed25519 curve:
77+
78+
'''
79+
$ step ssh certificate --kty OKP --curve Ed25519 mariano@work id_ed25519
6780
'''`,
6881
Flags: []cli.Flag{
6982
flags.Token,
@@ -82,6 +95,10 @@ $ step ssh login --principal admin --principal bob bob@smallstep.com
8295
flags.CaURL,
8396
flags.Root,
8497
flags.Context,
98+
flags.KTY,
99+
flags.Curve,
100+
flags.Size,
101+
flags.Insecure,
85102
},
86103
}
87104
}
@@ -106,6 +123,7 @@ func loginAction(ctx *cli.Context) error {
106123
token := ctx.String("token")
107124
isAddUser := ctx.Bool("add-user")
108125
force := ctx.Bool("force")
126+
insecure := ctx.Bool("insecure")
109127
validAfter, validBefore, err := flags.ParseTimeDuration(ctx)
110128
if err != nil {
111129
return err
@@ -115,6 +133,11 @@ func loginAction(ctx *cli.Context) error {
115133
return err
116134
}
117135

136+
kty, curve, size, err := utils.GetKeyDetailsFromCLI(ctx, insecure, "kty", "curve", "size")
137+
if err != nil {
138+
return err
139+
}
140+
118141
// Connect to the SSH agent.
119142
// step ssh login requires an ssh agent.
120143
agent, err := sshutil.DialAgent()
@@ -169,8 +192,7 @@ func loginAction(ctx *cli.Context) error {
169192
return err
170193
}
171194

172-
// Generate keypair
173-
pub, priv, err := keyutil.GenerateDefaultKeyPair()
195+
pub, priv, err := keyutil.GenerateKeyPair(kty, curve, size)
174196
if err != nil {
175197
return err
176198
}
@@ -184,7 +206,7 @@ func loginAction(ctx *cli.Context) error {
184206
var sshAuPubBytes []byte
185207
var auPub, auPriv interface{}
186208
if isAddUser {
187-
auPub, auPriv, err = keyutil.GenerateDefaultKeyPair()
209+
auPub, auPriv, err = keyutil.GenerateKeyPair(kty, curve, size)
188210
if err != nil {
189211
return err
190212
}

0 commit comments

Comments
 (0)