Debian Codegraph @

utils/cli.go

raw symbols SCIP
1 package utils
2
3 import (
4 "github.com/smallstep/cli/errs"
5 "github.com/urfave/cli"
6 )
7
8 // DefaultRSASize sets the default key size for RSA to 2048 bits.
9 const DefaultRSASize = 2048
10
11 // DefaultECCurve sets the default curve for EC to P-256.
12 const DefaultECCurve = "P-256"
13
14 // GetKeyDetailsFromCLI gets the key pair algorithm, curve, and size inputs
15 // from the CLI context.
16 func GetKeyDetailsFromCLI(ctx *cli.Context, insecure bool, ktyKey, curveKey, sizeKey string) (string, string, int, error) {
17 var (
18 crv = ctx.String("curve")
19 size = ctx.Int("size")
20 kty = ctx.String("kty")
21 )
22
23 if ctx.IsSet(ktyKey) {
24 switch kty {
25 case "RSA":
26 if !ctx.IsSet(sizeKey) {
27 size = DefaultRSASize
28 }
29 if ctx.IsSet(curveKey) {
30 return kty, crv, size, errs.IncompatibleFlagValue(ctx, curveKey, ktyKey, kty)
31 }
32 if size < 2048 && !insecure {
33 return kty, crv, size, errs.MinSizeInsecureFlag(ctx, sizeKey, "2048")
34 }
35 if size <= 0 {
36 return kty, crv, size, errs.MinSizeFlag(ctx, sizeKey, "0")
37 }
38 case "EC":
39 if ctx.IsSet("size") {
40 return kty, crv, size, errs.IncompatibleFlagValue(ctx, sizeKey, ktyKey, kty)
41 }
42 if !ctx.IsSet("curve") {
43 crv = DefaultECCurve
44 }
45 switch crv {
46 case "P-256", "P-384", "P-521": //ok
47 default:
48 return kty, crv, size, errs.IncompatibleFlagValueWithFlagValue(ctx, ktyKey, kty,
49 curveKey, crv, "P-256, P-384, P-521")
50 }
51 case "OKP":
52 if ctx.IsSet("size") {
53 return kty, crv, size, errs.IncompatibleFlagValue(ctx, sizeKey, ktyKey, kty)
54 }
55 switch crv {
56 case "Ed25519": //ok
57 case "": // ok: OKP defaults to Ed25519
58 crv = "Ed25519"
59 default:
60 return kty, crv, size, errs.IncompatibleFlagValueWithFlagValue(ctx, ktyKey, kty,
61 curveKey, crv, "Ed25519")
62 }
63 default:
64 return kty, crv, size, errs.InvalidFlagValue(ctx, ktyKey, kty, "RSA, EC, OKP")
65 }
66 } else {
67 if ctx.IsSet(curveKey) {
68 return kty, crv, size, errs.RequiredWithFlag(ctx, curveKey, ktyKey)
69 }
70 if ctx.IsSet("size") {
71 return kty, crv, size, errs.RequiredWithFlag(ctx, sizeKey, ktyKey)
72 }
73 // Set default key type | curve | size.
74 kty = "EC"
75 crv = "P-256"
76 size = 0
77 }
78 return kty, crv, size, nil
79 }