| 1 |
package jwk |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"encoding/base64" |
| 7 |
"encoding/json" |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"github.com/pkg/errors" |
| 11 |
"github.com/smallstep/cli/command" |
| 12 |
"github.com/smallstep/cli/crypto/randutil" |
| 13 |
"github.com/smallstep/cli/errs" |
| 14 |
"github.com/smallstep/cli/flags" |
| 15 |
"github.com/smallstep/cli/jose" |
| 16 |
"github.com/smallstep/cli/ui" |
| 17 |
"github.com/smallstep/cli/utils" |
| 18 |
"github.com/urfave/cli" |
| 19 |
) |
| 20 |
|
| 21 |
const ( |
| 22 |
|
| 23 |
pbkdf2SaltSize = 16 |
| 24 |
|
| 25 |
pbkdf2Iterations = 100000 |
| 26 |
) |
| 27 |
|
| 28 |
func createCommand() cli.Command { |
| 29 |
return cli.Command{ |
| 30 |
Name: "create", |
| 31 |
Action: command.ActionFunc(createAction), |
| 32 |
Usage: "create a JWK (JSON Web Key)", |
| 33 |
UsageText: `**step crypto jwk create** <public-jwk-file> <private-jwk-file> |
| 34 |
[**--kty**=<type>] [**--alg**=<algorithm>] [**--use**=<use>] |
| 35 |
[**--size**=<size>] [**--crv**=<curve>] [**--kid**=<kid>] |
| 36 |
[**--from-pem**=<pem-file>] [**--password-file**=<file>]`, |
| 37 |
Description: `**step crypto jwk create** generates a new JWK (JSON Web Key) or constructs a |
| 38 |
JWK from an existing key. The generated JWK conforms to RFC7517 and can be used |
| 39 |
to sign and encrypt data using JWT, JWS, and JWE. |
| 40 |
|
| 41 |
Files containing private keys are encrypted by default. You'll be prompted for |
| 42 |
a password. Keys are written with file mode **0600** (i.e., readable and |
| 43 |
writable only by the current user). |
| 44 |
|
| 45 |
All flags are optional. Defaults are suitable for most use cases. |
| 46 |
|
| 47 |
## POSITIONAL ARGUMENTS |
| 48 |
|
| 49 |
<public-jwk-file> |
| 50 |
: Path to which the the public JWK should be written |
| 51 |
|
| 52 |
<private-jwk-file> |
| 53 |
: Path to which the (JWE encrypted) private JWK should be written |
| 54 |
|
| 55 |
## EXIT CODES |
| 56 |
|
| 57 |
This command returns 0 on success and \>0 if any error occurs. |
| 58 |
|
| 59 |
## SECURITY CONSIDERATIONS |
| 60 |
|
| 61 |
All security considerations from **step help crypto** are relevant here. |
| 62 |
|
| 63 |
**Preventing hostile disclosure of non-public key material** |
| 64 |
|
| 65 |
: It is critical that any private and symmetric key material be protected from |
| 66 |
unauthorized disclosure or modification. This includes the private key for |
| 67 |
asymmetric key types (RSA, EC, and OKP) and the shared secret for symmetric key |
| 68 |
types (oct). One means of protection is encryption. Keys can also be stored in |
| 69 |
hardware or software "security enclaves" such as HSMs and TPMs or operating |
| 70 |
system keychain management tools. |
| 71 |
|
| 72 |
**Key provenance and bindings** |
| 73 |
|
| 74 |
: Key provenance should always be scrutinized. You should not trust a key that |
| 75 |
was obtained in an untrustworthy manner (e.g., non-TLS HTTP). |
| 76 |
|
| 77 |
: Usually applications use keys to make authorization decisions based on |
| 78 |
attributes "bound" to the key such as the key owner's name or role. In these |
| 79 |
scenarios the strength of the system's security depends on the strength of |
| 80 |
these "bindings". There are a variety of mechanisms for securely binding |
| 81 |
attributes to keys, including: |
| 82 |
|
| 83 |
* Cryptographically binding attributes to the public key using x509 |
| 84 |
certificates (e.g., as defined in PKIX / RFC2580) |
| 85 |
* Cryptographically binding attributes to the public key using JWTs |
| 86 |
* Storing the public key or (hashed) shared secret along with the bound |
| 87 |
attributes in a secure database |
| 88 |
|
| 89 |
: Cryptographic mechanisms require establishing a "root of trust" that can sign |
| 90 |
the bindings (the certificates or JWTs) asserting that the bound attributes are |
| 91 |
correct. |
| 92 |
|
| 93 |
## STANDARDS |
| 94 |
|
| 95 |
[RFC7517] |
| 96 |
: Jones, M., "JSON Web Key (JWK)", https://tools.ietf.org/html/rfc7517 |
| 97 |
|
| 98 |
[RFC7518] |
| 99 |
: Jones, M., "JSON Web Algorithms (JWA)", https://tools.ietf.org/html/rfc7518 |
| 100 |
|
| 101 |
[RFC7638] |
| 102 |
: M. Jones, N. Sakimura., "JSON Web Key (JWK) Thumbprint", |
| 103 |
https://tools.ietf.org/html/rfc7638 |
| 104 |
|
| 105 |
[RFC8037] |
| 106 |
: I. Liusvaara., "CFRG Elliptic Curve Diffie-Hellman (ECDH) and Signatures in |
| 107 |
JSON Object Signing and Encryption (JOSE)", |
| 108 |
https://tools.ietf.org/html/rfc8037 |
| 109 |
|
| 110 |
## EXAMPLES |
| 111 |
|
| 112 |
Create a new JWK using default options: |
| 113 |
|
| 114 |
''' |
| 115 |
$ step crypto jwk create jwk.pub.json jwk.json |
| 116 |
''' |
| 117 |
|
| 118 |
Create an RSA JWK: |
| 119 |
|
| 120 |
''' |
| 121 |
$ step crypto jwk create rsa.pub.json rsa.json --kty RSA |
| 122 |
''' |
| 123 |
|
| 124 |
Create a symmetric key (oct key type): |
| 125 |
|
| 126 |
''' |
| 127 |
$ step crypto jwk create oct.pub.json oct.json --kty oct |
| 128 |
''' |
| 129 |
|
| 130 |
Create a key for use with the Ed25519 cryptosystem: |
| 131 |
|
| 132 |
''' |
| 133 |
$ step crypto jwk create ed.pub.json ed.json \ |
| 134 |
--kty OKP --crv Ed25519 |
| 135 |
''' |
| 136 |
|
| 137 |
Create a key from an existing PEM file: |
| 138 |
|
| 139 |
''' |
| 140 |
$ step crypto jwk create jwk.pub.json jwk.json \ |
| 141 |
--from-pem key.pem |
| 142 |
''' |
| 143 |
|
| 144 |
Create an 4096 bit RSA encryption key: |
| 145 |
|
| 146 |
''' |
| 147 |
$ step crypto jwk create rsa-enc.pub.json rsa-enc.json \ |
| 148 |
--kty RSA --size 4096 --use enc |
| 149 |
''' |
| 150 |
|
| 151 |
Create a 192 bit symmetric encryption key for use with AES Key Wrap: |
| 152 |
|
| 153 |
''' |
| 154 |
$ step crypto jwk create kw.pub.json kw.json \ |
| 155 |
--kty oct --size 192 --use enc --alg A192GCMKW |
| 156 |
''' |
| 157 |
`, |
| 158 |
Flags: []cli.Flag{ |
| 159 |
cli.StringFlag{ |
| 160 |
Name: "kty, type", |
| 161 |
Value: "EC", |
| 162 |
Usage: `The <type> of key to create. Corresponds to the **"kty"** JWK parameter. |
| 163 |
If unset, default is EC. |
| 164 |
|
| 165 |
: <type> is a case-sensitive string and must be one of: |
| 166 |
|
| 167 |
**EC** |
| 168 |
: Create an **elliptic curve** keypair |
| 169 |
|
| 170 |
**oct** |
| 171 |
: Create a **symmetric key** (octet stream) |
| 172 |
|
| 173 |
**OKP** |
| 174 |
: Create an octet key pair (for **"Ed25519"** curve) |
| 175 |
|
| 176 |
**RSA** |
| 177 |
: Create an **RSA** keypair |
| 178 |
`, |
| 179 |
}, |
| 180 |
flags.Size, |
| 181 |
flags.Curve, |
| 182 |
cli.StringFlag{ |
| 183 |
Name: "alg, algorithm", |
| 184 |
Usage: `The <algorithm> intended for use with this key. Corresponds to the |
| 185 |
**"alg"** JWK parameter. <algorithm> is case-sensitive. If unset, the default |
| 186 |
depends on the key use, key type, and curve (for EC and OKP keys). Defaults are: |
| 187 |
|
| 188 |
: | key type | use | curve | default algorithm | |
| 189 |
|----------|-----|---------|-------------------| |
| 190 |
| EC | sig | P-256 | ES256 | |
| 191 |
| EC | sig | P-384 | ES384 | |
| 192 |
| EC | sig | P-521 | ES512 | |
| 193 |
| oct | sig | N/A | HS256 | |
| 194 |
| RSA | sig | N/A | RS256 | |
| 195 |
| OKP | sig | Ed25519 | EdDSA | |
| 196 |
| EC | enc | P-256 | ECDH-ES | |
| 197 |
| EC | enc | P-384 | ECDH-ES | |
| 198 |
| EC | enc | P-521 | ECDH-ES | |
| 199 |
| oct | enc | N/A | A256GCMKW | |
| 200 |
| RSA | enc | N/A | RSA-OAP-256 | |
| 201 |
|
| 202 |
: If the key **"use"** is **"sig"** (signing) <algorithm> must be one of: |
| 203 |
|
| 204 |
**HS256** |
| 205 |
: HMAC using SHA-256 |
| 206 |
|
| 207 |
**HS384** |
| 208 |
: HMAC using SHA-384 |
| 209 |
|
| 210 |
**HS512** |
| 211 |
: HMAC using SHA-512 |
| 212 |
|
| 213 |
**RS256** |
| 214 |
: RSASSA-PKCS1-v1_5 using SHA-256 |
| 215 |
|
| 216 |
**RS384** |
| 217 |
: RSASSA-PKCS1-v1_5 using SHA-384 |
| 218 |
|
| 219 |
**RS512** |
| 220 |
: RSASSA-PKCS1-v1_5 using SHA-512 |
| 221 |
|
| 222 |
**ES256** |
| 223 |
: ECDSA using P-256 and SHA-256 |
| 224 |
|
| 225 |
**ES384** |
| 226 |
: ECDSA using P-384 and SHA-384 |
| 227 |
|
| 228 |
**ES512** |
| 229 |
: ECDSA using P-521 and SHA-512 |
| 230 |
|
| 231 |
**PS256** |
| 232 |
: RSASSA-PSS using SHA-256 and MGF1 with SHA-256 |
| 233 |
|
| 234 |
**PS384** |
| 235 |
: RSASSA-PSS using SHA-384 and MGF1 with SHA-384 |
| 236 |
|
| 237 |
**PS512** |
| 238 |
: RSASSA-PSS using SHA-512 and MGF1 with SHA-512 |
| 239 |
|
| 240 |
**EdDSA** |
| 241 |
: EdDSA signature algorithm |
| 242 |
|
| 243 |
: If the key **"use"** is **"enc"** (encryption) <algorithm> must be one of: |
| 244 |
|
| 245 |
**RSA1_5** |
| 246 |
: RSAES-PKCS1-v1_5 |
| 247 |
|
| 248 |
**RSA-OAEP** |
| 249 |
: RSAES OAEP using default parameters |
| 250 |
|
| 251 |
**RSA-OAEP-256** |
| 252 |
: RSAES OAEP using SHA-256 and MGF1 with SHA-256 |
| 253 |
|
| 254 |
**A128KW** |
| 255 |
: AES Key Wrap with default initial value using 128-bit key |
| 256 |
|
| 257 |
**A192KW** |
| 258 |
: AES Key Wrap with default initial value using 192-bit key |
| 259 |
|
| 260 |
**A256KW** |
| 261 |
: AES Key Wrap with default initial value using 256-bit key |
| 262 |
|
| 263 |
**dir** |
| 264 |
: Direct use of a shared symmetric key as the content encryption key (CEK) |
| 265 |
|
| 266 |
**ECDH-ES** |
| 267 |
: Elliptic Curve Diffie-Hellman Ephemeral Static key agreement |
| 268 |
|
| 269 |
**ECDH-ES+A128KW** |
| 270 |
: ECDH-ES using Concat KDF and CEK wrapped with "A128KW" |
| 271 |
|
| 272 |
**ECDH-ES+A192KW** |
| 273 |
: ECDH-ES using Concat KDF and CEK wrapped with "A192KW" |
| 274 |
|
| 275 |
**ECDH-ES+A256KW** |
| 276 |
: ECDH-ES using Concat KDF and CEK wrapped with "A256KW" |
| 277 |
|
| 278 |
**A128GCMKW** |
| 279 |
: Key wrapping with AES GCM using 128-bit key |
| 280 |
|
| 281 |
**A192GCMKW** |
| 282 |
: Key wrapping with AES GCM using 192-bit key |
| 283 |
|
| 284 |
**A256GCMKW** |
| 285 |
: Key wrapping with AES GCM using 256-bit key |
| 286 |
|
| 287 |
**PBES2-HS256+A128KW** |
| 288 |
: PBES2 with HMAC SHA-256 and "A128KW" wrapping |
| 289 |
|
| 290 |
**PBES2-HS384+A192KW** |
| 291 |
: PBES2 with HMAC SHA-256 and "A192KW" wrapping |
| 292 |
|
| 293 |
**PBES2-HS512+A256KW** |
| 294 |
: PBES2 with HMAC SHA-256 and "A256KW" wrapping`, |
| 295 |
}, |
| 296 |
cli.StringFlag{ |
| 297 |
Name: "use", |
| 298 |
Value: "sig", |
| 299 |
Usage: `The intended <use> of the public key. Corresponds to the "use" JWK parameter. |
| 300 |
The "use" parameter indicates whether the public key is used for encrypting |
| 301 |
data or verifying the signature on data. |
| 302 |
|
| 303 |
: <use> is a case-sensitive string and may be one of: |
| 304 |
|
| 305 |
**sig** |
| 306 |
: The public key is used for verifying signatures. |
| 307 |
|
| 308 |
**enc** |
| 309 |
: The public key is used for encrypting data. |
| 310 |
|
| 311 |
: Other values may be used but the generated JWKs will not work for signing or |
| 312 |
encryption with this tool.`, |
| 313 |
}, |
| 314 |
cli.StringFlag{ |
| 315 |
Name: "kid", |
| 316 |
Usage: `The <kid> (key ID) for this JWK. Corresponds to the |
| 317 |
"kid" JWK parameter. Used to identify an individual key in a JWK Set, for |
| 318 |
example. <kid> is a case-sensitive string. If unset, the JWK Thumbprint |
| 319 |
[RFC7638] is used as <kid>. See **step help crypto jwk thumbprint** for more |
| 320 |
information on JWK Thumbprints.`, |
| 321 |
}, |
| 322 |
cli.StringFlag{ |
| 323 |
Name: "key-ops", |
| 324 |
Hidden: true, |
| 325 |
Usage: `The operation(s) for which the key is intended to be used. Corresponds to |
| 326 |
the "key_ops" JWK parameter. The '--key-ops' flag can be used multiple times |
| 327 |
to indicate multiple intended operations. |
| 328 |
|
| 329 |
<key-op> can be one of the values defined in RFC7517: |
| 330 |
sign |
| 331 |
Compute digital signature or MAC |
| 332 |
verify |
| 333 |
Verify digital signature or MAC |
| 334 |
encrypt |
| 335 |
Encrypt content |
| 336 |
decrypt |
| 337 |
Decrypt content and validate decryption, if applicable |
| 338 |
wrapKey |
| 339 |
Encrypt key |
| 340 |
unwrapKey |
| 341 |
Decrypt key and validate decryption, if applicable |
| 342 |
deriveKey |
| 343 |
Derive key |
| 344 |
deriveBits |
| 345 |
Derive bits not to be used as a key |
| 346 |
|
| 347 |
The key operation values are case-sensitive strings. Other values may be |
| 348 |
used, but values must not be duplicated. |
| 349 |
|
| 350 |
The '--use' and '--key-ops' flags cannot be used together without also |
| 351 |
passing the '--subtle' flag. The '--subtle' flag allows both flags to be used |
| 352 |
in a consistent way (e.g., '--key-ops=encrypt --key-ops=decrypt --use=enc'). |
| 353 |
Multiple unrelated operations (e.g., '--key-ops=encrypt --key-ops=sign') or |
| 354 |
inconsistent combinations of '--use' and '--key-ops' (e.g., '--use=enc |
| 355 |
--key-ops=sign') are not allowed without also passing the '--insecure' flag |
| 356 |
because of potential vulnerabilities associated with using the same key with |
| 357 |
multiple algorithms. |
| 358 |
|
| 359 |
Related operations include: |
| 360 |
sign + verify |
| 361 |
encrypt + decrypt |
| 362 |
wrapKey + unwrapKey |
| 363 |
If multiple values are passed and at least one is a non-standard value the |
| 364 |
'--subtle' flag is required as you must verify that the operations are |
| 365 |
related.`, |
| 366 |
}, |
| 367 |
cli.StringFlag{ |
| 368 |
Name: "from-pem", |
| 369 |
Usage: `Create a JWK representing the key encoded in an |
| 370 |
existing <pem-file> instead of creating a new key.`, |
| 371 |
}, |
| 372 |
flags.PasswordFile, |
| 373 |
flags.NoPassword, |
| 374 |
flags.Subtle, |
| 375 |
flags.Insecure, |
| 376 |
flags.Force, |
| 377 |
}, |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
func createAction(ctx *cli.Context) (err error) { |
| 382 |
|
| 383 |
if err = errs.NumberOfArguments(ctx, 2); err != nil { |
| 384 |
return err |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
usePassword := true |
| 389 |
passwordFile := ctx.String("password-file") |
| 390 |
if ctx.Bool("no-password") { |
| 391 |
if len(passwordFile) > 0 { |
| 392 |
return errs.IncompatibleFlag(ctx, "no-password", "password-file") |
| 393 |
} |
| 394 |
if ctx.Bool("insecure") { |
| 395 |
usePassword = false |
| 396 |
} else { |
| 397 |
return errs.RequiredInsecureFlag(ctx, "no-password") |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
pubFile := ctx.Args().Get(0) |
| 402 |
privFile := ctx.Args().Get(1) |
| 403 |
if pubFile == privFile { |
| 404 |
return errs.EqualArguments(ctx, "public-jwk-file", "private-jwk-file") |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
var password string |
| 409 |
if len(passwordFile) > 0 { |
| 410 |
password, err = utils.ReadStringPasswordFromFile(passwordFile) |
| 411 |
if err != nil { |
| 412 |
return err |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
kty := ctx.String("kty") |
| 417 |
crv := ctx.String("crv") |
| 418 |
alg := ctx.String("alg") |
| 419 |
use := ctx.String("use") |
| 420 |
kid := ctx.String("kid") |
| 421 |
size := ctx.Int("size") |
| 422 |
pemFile := ctx.String("from-pem") |
| 423 |
|
| 424 |
switch kty { |
| 425 |
case "EC": |
| 426 |
if ctx.IsSet("size") { |
| 427 |
return errs.IncompatibleFlag(ctx, "size", "--kty EC") |
| 428 |
} |
| 429 |
case "RSA": |
| 430 |
if ctx.IsSet("crv") { |
| 431 |
return errs.IncompatibleFlag(ctx, "crv", "--kty RSA") |
| 432 |
} |
| 433 |
|
| 434 |
if ctx.IsSet("size") { |
| 435 |
if size < 2048 && !ctx.Bool("insecure") { |
| 436 |
return errs.MinSizeInsecureFlag(ctx, "size", "2048") |
| 437 |
} |
| 438 |
if size <= 0 { |
| 439 |
return errs.MinSizeFlag(ctx, "size", "0") |
| 440 |
} |
| 441 |
} |
| 442 |
case "OKP": |
| 443 |
if ctx.IsSet("size") { |
| 444 |
return errs.IncompatibleFlag(ctx, "size", "--kty OKP") |
| 445 |
} |
| 446 |
case "oct": |
| 447 |
if ctx.IsSet("crv") { |
| 448 |
return errs.IncompatibleFlag(ctx, "crv", "--kty oct") |
| 449 |
} |
| 450 |
|
| 451 |
if ctx.IsSet("size") { |
| 452 |
if size < 16 && !ctx.Bool("insecure") { |
| 453 |
return errs.MinSizeInsecureFlag(ctx, "size", "16") |
| 454 |
} |
| 455 |
if size <= 0 { |
| 456 |
return errs.MinSizeFlag(ctx, "size", "0") |
| 457 |
} |
| 458 |
} |
| 459 |
default: |
| 460 |
return errs.InvalidFlagValue(ctx, "kty", kty, "EC, RSA, OKP, or oct") |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
var jwk *jose.JSONWebKey |
| 465 |
switch { |
| 466 |
case pemFile != "": |
| 467 |
jwk, err = jose.GenerateJWKFromPEM(pemFile, ctx.Bool("subtle")) |
| 468 |
default: |
| 469 |
jwk, err = jose.GenerateJWK(kty, crv, alg, use, kid, size) |
| 470 |
} |
| 471 |
|
| 472 |
if err != nil { |
| 473 |
return err |
| 474 |
} |
| 475 |
|
| 476 |
if ctx.IsSet("kid") { |
| 477 |
jwk.KeyID = ctx.String("kid") |
| 478 |
} else { |
| 479 |
|
| 480 |
if kty != "oct" { |
| 481 |
var hash []byte |
| 482 |
hash, err = jwk.Thumbprint(crypto.SHA256) |
| 483 |
if err != nil { |
| 484 |
return errors.Wrap(err, "error generating JWK thumbprint") |
| 485 |
} |
| 486 |
jwk.KeyID = base64.RawURLEncoding.EncodeToString(hash) |
| 487 |
} |
| 488 |
} |
| 489 |
jwk.Use = use |
| 490 |
|
| 491 |
if jwk.Algorithm == "" { |
| 492 |
jwk.Algorithm = alg |
| 493 |
} |
| 494 |
|
| 495 |
if err = jose.ValidateJWK(jwk); err != nil { |
| 496 |
return err |
| 497 |
} |
| 498 |
|
| 499 |
var jwkPub jose.JSONWebKey |
| 500 |
if jose.IsSymmetric(jwk) { |
| 501 |
jwkPub = *jwk |
| 502 |
} else { |
| 503 |
jwkPub = jwk.Public() |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
b, err := json.MarshalIndent(jwkPub, "", " ") |
| 508 |
if err != nil { |
| 509 |
return errors.Wrap(err, "error marshaling JWK") |
| 510 |
} |
| 511 |
if err = utils.WriteFile(pubFile, b, 0600); err != nil { |
| 512 |
return errs.FileError(err, pubFile) |
| 513 |
} |
| 514 |
|
| 515 |
if jwk.IsPublic() { |
| 516 |
ui.Printf("Your public key has been saved in %s.\n", pubFile) |
| 517 |
ui.Println("Only the public JWK was generated.") |
| 518 |
ui.Println("Cannot retrieve a private key from a public one.") |
| 519 |
return nil |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
if usePassword { |
| 524 |
var rcpt jose.Recipient |
| 525 |
|
| 526 |
if jose.SupportsPBKDF2 { |
| 527 |
var key []byte |
| 528 |
key, err = ui.PromptPassword("Please enter the password to encrypt the private JWK", ui.WithValue(password)) |
| 529 |
if err != nil { |
| 530 |
return errors.Wrap(err, "error reading password") |
| 531 |
} |
| 532 |
|
| 533 |
var salt []byte |
| 534 |
salt, err = randutil.Salt(pbkdf2SaltSize) |
| 535 |
if err != nil { |
| 536 |
return err |
| 537 |
} |
| 538 |
|
| 539 |
rcpt = jose.Recipient{ |
| 540 |
Algorithm: jose.PBES2_HS256_A128KW, |
| 541 |
Key: []byte(key), |
| 542 |
PBES2Count: pbkdf2Iterations, |
| 543 |
PBES2Salt: salt, |
| 544 |
} |
| 545 |
} else { |
| 546 |
var key string |
| 547 |
key, err = randutil.Alphanumeric(32) |
| 548 |
if err != nil { |
| 549 |
return errors.Wrap(err, "error generating password") |
| 550 |
} |
| 551 |
fmt.Printf("Private JWK file '%s' will be encrypted with the key:\n%s\n", privFile, key) |
| 552 |
rcpt = jose.Recipient{Algorithm: jose.A128KW, Key: []byte(key)} |
| 553 |
} |
| 554 |
|
| 555 |
b, err = json.Marshal(jwk) |
| 556 |
if err != nil { |
| 557 |
return errors.Wrap(err, "error marshaling JWK") |
| 558 |
} |
| 559 |
|
| 560 |
opts := new(jose.EncrypterOptions) |
| 561 |
opts.WithContentType(jose.ContentType("jwk+json")) |
| 562 |
var encrypter jose.Encrypter |
| 563 |
encrypter, err = jose.NewEncrypter(jose.DefaultEncAlgorithm, rcpt, opts) |
| 564 |
if err != nil { |
| 565 |
return errors.Wrap(err, "error creating cipher") |
| 566 |
} |
| 567 |
|
| 568 |
var obj *jose.JSONWebEncryption |
| 569 |
obj, err = encrypter.Encrypt(b) |
| 570 |
if err != nil { |
| 571 |
return errors.Wrap(err, "error encrypting JWK") |
| 572 |
} |
| 573 |
|
| 574 |
var out bytes.Buffer |
| 575 |
if err = json.Indent(&out, []byte(obj.FullSerialize()), "", " "); err != nil { |
| 576 |
return errors.Wrap(err, "error formatting JSON") |
| 577 |
} |
| 578 |
b = out.Bytes() |
| 579 |
} else { |
| 580 |
b, err = json.MarshalIndent(jwk, "", " ") |
| 581 |
if err != nil { |
| 582 |
return errors.Wrap(err, "error marshaling JWK") |
| 583 |
} |
| 584 |
} |
| 585 |
if err = utils.WriteFile(privFile, b, 0600); err != nil { |
| 586 |
return errs.FileError(err, privFile) |
| 587 |
} |
| 588 |
|
| 589 |
ui.Printf("Your public key has been saved in %s.\n", pubFile) |
| 590 |
ui.Printf("Your private key has been saved in %s.\n", privFile) |
| 591 |
return nil |
| 592 |
} |