| 1 |
package crypto |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/pkg/errors" |
| 5 |
"github.com/smallstep/cli/command" |
| 6 |
"github.com/smallstep/cli/crypto/keys" |
| 7 |
"github.com/smallstep/cli/crypto/pemutil" |
| 8 |
"github.com/smallstep/cli/errs" |
| 9 |
"github.com/smallstep/cli/flags" |
| 10 |
"github.com/smallstep/cli/jose" |
| 11 |
"github.com/smallstep/cli/ui" |
| 12 |
"github.com/smallstep/cli/utils" |
| 13 |
"github.com/urfave/cli" |
| 14 |
) |
| 15 |
|
| 16 |
func createKeyPairCommand() cli.Command { |
| 17 |
return cli.Command{ |
| 18 |
Name: "keypair", |
| 19 |
Action: command.ActionFunc(createAction), |
| 20 |
Usage: "generate a public / private keypair in PEM format", |
| 21 |
UsageText: `**step crypto keypair** <pub_file> <priv_file> |
| 22 |
[**--kty**=<key-type>] [**--curve**=<curve>] [**--size**=<size>] |
| 23 |
[**--password-file**=<file>] [**--no-password**]`, |
| 24 |
Description: `**step crypto keypair** generates a raw public / |
| 25 |
private keypair in PEM format. These keys can be used by other operations |
| 26 |
to sign and encrypt data, and the public key can be bound to an identity |
| 27 |
in a CSR and signed by a CA to produce a certificate. |
| 28 |
|
| 29 |
Private keys are encrypted using a password. You'll be prompted for this |
| 30 |
password automatically when the key is used. |
| 31 |
|
| 32 |
## POSITIONAL ARGUMENTS |
| 33 |
|
| 34 |
<pub_file> |
| 35 |
: The path to write the public key. |
| 36 |
|
| 37 |
<priv_file> |
| 38 |
: The path to write the private key. |
| 39 |
|
| 40 |
## EXIT CODES |
| 41 |
|
| 42 |
This command returns 0 on success and \>0 if any error occurs. |
| 43 |
|
| 44 |
## EXAMPLES |
| 45 |
|
| 46 |
Create an RSA public / private key pair with 4096 bits: |
| 47 |
|
| 48 |
''' |
| 49 |
$ step crypto keypair foo.pub foo.key --kty RSA --size 4096 |
| 50 |
''' |
| 51 |
|
| 52 |
Create an RSA public / private key with fewer than the recommended number of |
| 53 |
bits (recommended >= 2048 bits): |
| 54 |
|
| 55 |
''' |
| 56 |
$ step crypto keypair foo.pub foo.key --kty RSA --size 1024 --insecure |
| 57 |
''' |
| 58 |
|
| 59 |
Create an EC public / private key pair with curve P-521: |
| 60 |
|
| 61 |
''' |
| 62 |
$ step crypto keypair foo.pub foo.key --kty EC --curve "P-521" |
| 63 |
''' |
| 64 |
|
| 65 |
Create an EC public / private key pair but do not encrypt the private key file: |
| 66 |
|
| 67 |
''' |
| 68 |
$ step crypto keypair foo.pub foo.key --kty EC --curve "P-256" \ |
| 69 |
--no-password --insecure |
| 70 |
''' |
| 71 |
|
| 72 |
Create an Octet Key Pair with curve Ed25519: |
| 73 |
|
| 74 |
''' |
| 75 |
$ step crypto keypair foo.pub foo.key --kty OKP --curve Ed25519 |
| 76 |
''' |
| 77 |
`, |
| 78 |
Flags: []cli.Flag{ |
| 79 |
flags.KTY, |
| 80 |
flags.Size, |
| 81 |
flags.Curve, |
| 82 |
cli.StringFlag{ |
| 83 |
Name: "from-jwk", |
| 84 |
Usage: `Create a PEM representing the key encoded in an |
| 85 |
existing <jwk-file> instead of creating a new key.`, |
| 86 |
}, |
| 87 |
flags.PasswordFile, |
| 88 |
flags.NoPassword, |
| 89 |
flags.Insecure, |
| 90 |
flags.Force, |
| 91 |
}, |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func createAction(ctx *cli.Context) (err error) { |
| 96 |
if err = errs.NumberOfArguments(ctx, 2); err != nil { |
| 97 |
return err |
| 98 |
} |
| 99 |
|
| 100 |
pubFile := ctx.Args().Get(0) |
| 101 |
privFile := ctx.Args().Get(1) |
| 102 |
if pubFile == privFile { |
| 103 |
return errs.EqualArguments(ctx, "PUB_FILE", "PRIV_FILE") |
| 104 |
} |
| 105 |
|
| 106 |
insecure := ctx.Bool("insecure") |
| 107 |
noPass := ctx.Bool("no-password") |
| 108 |
passwordFile := ctx.String("password-file") |
| 109 |
if noPass && len(passwordFile) > 0 { |
| 110 |
return errs.IncompatibleFlag(ctx, "no-password", "password-file") |
| 111 |
} |
| 112 |
if noPass && !insecure { |
| 113 |
return errs.RequiredWithFlag(ctx, "insecure", "no-password") |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
var password string |
| 118 |
if len(passwordFile) > 0 { |
| 119 |
password, err = utils.ReadStringPasswordFromFile(passwordFile) |
| 120 |
if err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
var pub, priv interface{} |
| 126 |
fromJWK := ctx.String("from-jwk") |
| 127 |
if len(fromJWK) > 0 { |
| 128 |
switch { |
| 129 |
case ctx.IsSet("kty"): |
| 130 |
return errs.IncompatibleFlagWithFlag(ctx, "from-jwk", "kty") |
| 131 |
case ctx.IsSet("curve"): |
| 132 |
return errs.IncompatibleFlagWithFlag(ctx, "from-jwk", "curve") |
| 133 |
case ctx.IsSet("size"): |
| 134 |
return errs.IncompatibleFlagWithFlag(ctx, "from-jwk", "size") |
| 135 |
} |
| 136 |
|
| 137 |
var jwk *jose.JSONWebKey |
| 138 |
jwk, err = jose.ParseKey(fromJWK) |
| 139 |
if err != nil { |
| 140 |
return err |
| 141 |
} |
| 142 |
|
| 143 |
if jwk.IsPublic() { |
| 144 |
pub = jwk.Key |
| 145 |
} else { |
| 146 |
pub = jwk.Public().Key |
| 147 |
priv = jwk.Key |
| 148 |
} |
| 149 |
} else { |
| 150 |
var ( |
| 151 |
kty, crv string |
| 152 |
size int |
| 153 |
) |
| 154 |
kty, crv, size, err = utils.GetKeyDetailsFromCLI(ctx, insecure, "kty", |
| 155 |
"curve", "size") |
| 156 |
if err != nil { |
| 157 |
return err |
| 158 |
} |
| 159 |
|
| 160 |
pub, priv, err = keys.GenerateKeyPair(kty, crv, size) |
| 161 |
if err != nil { |
| 162 |
return err |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
_, err = pemutil.Serialize(pub, pemutil.ToFile(pubFile, 0600)) |
| 167 |
if err != nil { |
| 168 |
return err |
| 169 |
} |
| 170 |
|
| 171 |
if priv == nil { |
| 172 |
ui.Printf("Your public key has been saved in %s.\n", pubFile) |
| 173 |
ui.Println("Only the public PEM was generated.") |
| 174 |
ui.Println("Cannot retrieve a private key from a public one.") |
| 175 |
return nil |
| 176 |
} |
| 177 |
|
| 178 |
if noPass { |
| 179 |
_, err = pemutil.Serialize(priv, pemutil.ToFile(privFile, 0600)) |
| 180 |
if err != nil { |
| 181 |
return err |
| 182 |
} |
| 183 |
} else { |
| 184 |
var pass []byte |
| 185 |
pass, err = ui.PromptPassword("Please enter the password to encrypt the private key", ui.WithValue(password)) |
| 186 |
if err != nil { |
| 187 |
return errors.Wrap(err, "error reading password") |
| 188 |
} |
| 189 |
_, err = pemutil.Serialize(priv, pemutil.WithPassword(pass), |
| 190 |
pemutil.ToFile(privFile, 0600)) |
| 191 |
if err != nil { |
| 192 |
return err |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
ui.Printf("Your public key has been saved in %s.\n", pubFile) |
| 197 |
ui.Printf("Your private key has been saved in %s.\n", privFile) |
| 198 |
return nil |
| 199 |
} |