| 1 |
package crypto |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io/ioutil" |
| 8 |
|
| 9 |
"github.com/pkg/errors" |
| 10 |
"github.com/urfave/cli" |
| 11 |
|
| 12 |
"github.com/smallstep/cli/command" |
| 13 |
"github.com/smallstep/cli/crypto/pemutil" |
| 14 |
"github.com/smallstep/cli/errs" |
| 15 |
"github.com/smallstep/cli/flags" |
| 16 |
"github.com/smallstep/cli/jose" |
| 17 |
"github.com/smallstep/cli/ui" |
| 18 |
"github.com/smallstep/cli/utils" |
| 19 |
) |
| 20 |
|
| 21 |
func changePassCommand() cli.Command { |
| 22 |
return cli.Command{ |
| 23 |
Name: "change-pass", |
| 24 |
Action: command.ActionFunc(changePassAction), |
| 25 |
Usage: "change password of an encrypted private key (PEM or JWK format)", |
| 26 |
UsageText: `**step crypto change-pass** <key-file> |
| 27 |
[**--out**=<path>] [**--password-file**=<path>] [**--new-password-file**=<path>] |
| 28 |
[**--insecure**] [**--no-password**]`, |
| 29 |
Description: `**step crypto change-pass** extracts and decrypts |
| 30 |
the private key from a file and encrypts and serializes the key to disk using a |
| 31 |
new password. |
| 32 |
|
| 33 |
## POSITIONAL ARGUMENTS |
| 34 |
|
| 35 |
<key-file> |
| 36 |
: The PEM or JWK file with the encrypted key. |
| 37 |
|
| 38 |
## EXAMPLES |
| 39 |
|
| 40 |
Change password for PEM formatted key: |
| 41 |
''' |
| 42 |
$ step crypto change-pass key.pem |
| 43 |
''' |
| 44 |
|
| 45 |
Remove password for PEM formatted key: |
| 46 |
''' |
| 47 |
$ step crypto change-pass key.pem --no-password --insecure |
| 48 |
''' |
| 49 |
|
| 50 |
Change password for PEM formatted key and write encrypted key to different file: |
| 51 |
''' |
| 52 |
$ step crypto change-pass key.pem --out new-key.pem |
| 53 |
''' |
| 54 |
|
| 55 |
Change password for JWK formatted key: |
| 56 |
''' |
| 57 |
$ step crypto change-pass key.jwk |
| 58 |
''' |
| 59 |
|
| 60 |
Removed password for JWK formatted key: |
| 61 |
''' |
| 62 |
$ step crypto change-pass key.jwk --no-password --insecure |
| 63 |
''' |
| 64 |
|
| 65 |
Change password for JWK formatted key: |
| 66 |
''' |
| 67 |
$ step crypto change-pass key.jwk --out new-key.jwk |
| 68 |
'''`, |
| 69 |
Flags: []cli.Flag{ |
| 70 |
cli.StringFlag{ |
| 71 |
Name: "password-file", |
| 72 |
Usage: `The path to the <file> containing the password to decrypt the private key.`, |
| 73 |
}, |
| 74 |
cli.StringFlag{ |
| 75 |
Name: "new-password-file", |
| 76 |
Usage: `The path to the <file> containing the password to encrypt the private key.`, |
| 77 |
}, |
| 78 |
cli.StringFlag{ |
| 79 |
Name: "out,output-file", |
| 80 |
Usage: "The <file> new encrypted key path. Default to overwriting the <key> positional argument", |
| 81 |
}, |
| 82 |
flags.Force, |
| 83 |
flags.Insecure, |
| 84 |
flags.NoPassword, |
| 85 |
}, |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
func changePassAction(ctx *cli.Context) error { |
| 94 |
if err := errs.NumberOfArguments(ctx, 1); err != nil { |
| 95 |
return err |
| 96 |
} |
| 97 |
|
| 98 |
insecure := ctx.Bool("insecure") |
| 99 |
noPass := ctx.Bool("no-password") |
| 100 |
decryptPassFile := ctx.String("password-file") |
| 101 |
encryptPassFile := ctx.String("new-password-file") |
| 102 |
if noPass && !insecure { |
| 103 |
return errs.RequiredWithFlag(ctx, "insecure", "no-password") |
| 104 |
} |
| 105 |
|
| 106 |
keyPath := ctx.Args().Get(0) |
| 107 |
newKeyPath := ctx.String("out") |
| 108 |
if len(newKeyPath) == 0 { |
| 109 |
newKeyPath = keyPath |
| 110 |
} |
| 111 |
|
| 112 |
b, err := ioutil.ReadFile(keyPath) |
| 113 |
if err != nil { |
| 114 |
return errs.FileError(err, keyPath) |
| 115 |
} |
| 116 |
|
| 117 |
if bytes.HasPrefix(b, []byte("-----BEGIN ")) { |
| 118 |
opts := []pemutil.Options{pemutil.WithFilename(keyPath)} |
| 119 |
if len(decryptPassFile) > 0 { |
| 120 |
opts = append(opts, pemutil.WithPasswordFile(decryptPassFile)) |
| 121 |
} |
| 122 |
key, err := pemutil.Parse(b, opts...) |
| 123 |
if err != nil { |
| 124 |
return err |
| 125 |
} |
| 126 |
opts = []pemutil.Options{} |
| 127 |
if !noPass { |
| 128 |
if len(encryptPassFile) > 0 { |
| 129 |
opts = append(opts, pemutil.WithPasswordFile(encryptPassFile)) |
| 130 |
} else { |
| 131 |
pass, err := ui.PromptPassword(fmt.Sprintf("Please enter the password to encrypt %s", newKeyPath)) |
| 132 |
if err != nil { |
| 133 |
return errors.Wrap(err, "error reading password") |
| 134 |
} |
| 135 |
opts = append(opts, pemutil.WithPassword(pass)) |
| 136 |
} |
| 137 |
} |
| 138 |
opts = append(opts, pemutil.ToFile(newKeyPath, 0644)) |
| 139 |
if _, err := pemutil.Serialize(key, opts...); err != nil { |
| 140 |
return err |
| 141 |
} |
| 142 |
} else { |
| 143 |
opts := []jose.Option{} |
| 144 |
if len(decryptPassFile) > 0 { |
| 145 |
opts = append(opts, jose.WithPasswordFile(decryptPassFile)) |
| 146 |
} |
| 147 |
jwk, err := jose.ParseKey(keyPath, opts...) |
| 148 |
if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
var b []byte |
| 152 |
if noPass { |
| 153 |
b, err = jwk.MarshalJSON() |
| 154 |
if err != nil { |
| 155 |
return err |
| 156 |
} |
| 157 |
} else { |
| 158 |
opts = []jose.Option{} |
| 159 |
if len(encryptPassFile) > 0 { |
| 160 |
opts = append(opts, jose.WithPasswordFile(encryptPassFile)) |
| 161 |
} |
| 162 |
jwe, err := jose.EncryptJWK(jwk, opts...) |
| 163 |
if err != nil { |
| 164 |
return err |
| 165 |
} |
| 166 |
b = []byte(jwe.FullSerialize()) |
| 167 |
} |
| 168 |
var out bytes.Buffer |
| 169 |
if err := json.Indent(&out, b, "", " "); err != nil { |
| 170 |
return errors.Wrap(err, "error formatting JSON") |
| 171 |
} |
| 172 |
if err := utils.WriteFile(newKeyPath, out.Bytes(), 0600); err != nil { |
| 173 |
return errs.FileError(err, newKeyPath) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
ui.Printf("Your key has been saved in %s.\n", newKeyPath) |
| 178 |
return nil |
| 179 |
} |