From facbde5c30b1febed39c3b2964b4375adac01d84 Mon Sep 17 00:00:00 2001 From: Andrei Burdulescu Date: Fri, 27 Jan 2023 18:53:49 +0200 Subject: [PATCH] Better command usage handling --- internal/cmd/aes/cbc.go | 4 +++- internal/cmd/aes/ecb.go | 3 ++- internal/cmd/aes/gcm.go | 4 +++- internal/cmd/aes/keygen.go | 4 +++- internal/cmd/aes/keywrap.go | 3 ++- internal/cmd/hash/sha.go | 3 ++- internal/cmd/kdf/pbkdf2.go | 6 +++++- internal/cmd/rsa/der_pem.go | 2 +- internal/cmd/rsa/der_raw.go | 3 ++- internal/cmd/rsa/kem.go | 4 +++- internal/cmd/rsa/keygen.go | 4 +++- internal/cmd/rsa/pem_der.go | 1 - internal/cmd/rsa/pub_from_priv.go | 1 - internal/cmd/rsa/raw_der.go | 9 ++++++++- 14 files changed, 37 insertions(+), 14 deletions(-) diff --git a/internal/cmd/aes/cbc.go b/internal/cmd/aes/cbc.go index 387f155..fc5f4ed 100644 --- a/internal/cmd/aes/cbc.go +++ b/internal/cmd/aes/cbc.go @@ -26,7 +26,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.") @@ -42,13 +41,16 @@ Options: } if *fKey == "" && *fKeyFile == "" { + fset.Usage() return errors.New("no key specified, use -k or --key-file to specify it") } if *fKey != "" && *fKeyFile != "" { + fset.Usage() return errors.New("cannot use -k and --key-file at the same time") } if *fIV == "" { + fset.Usage() return errors.New("no IV specified, use -iv to specify it") } diff --git a/internal/cmd/aes/ecb.go b/internal/cmd/aes/ecb.go index 21b3007..85f2945 100644 --- a/internal/cmd/aes/ecb.go +++ b/internal/cmd/aes/ecb.go @@ -25,7 +25,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.") @@ -40,10 +39,12 @@ Options: } if *fKey == "" && *fKeyFile == "" { + fset.Usage() return errors.New("no key specified, use -key or -key-file to specify it") } if *fKey != "" && *fKeyFile != "" { + fset.Usage() return errors.New("cannot use -key and -key-file at the same time") } diff --git a/internal/cmd/aes/gcm.go b/internal/cmd/aes/gcm.go index 461440c..c7d41c4 100644 --- a/internal/cmd/aes/gcm.go +++ b/internal/cmd/aes/gcm.go @@ -25,7 +25,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.") @@ -42,14 +41,17 @@ Options: } if *fKey == "" && *fKeyFile == "" { + fset.Usage() return errors.New("no key specified, use -key or -key-file to specify it") } if *fKey != "" && *fKeyFile != "" { + fset.Usage() return errors.New("cannot use -key and -key-file at the same time") } if *fIV == "" { + fset.Usage() return errors.New("no IV specified, use -iv to specify it") } diff --git a/internal/cmd/aes/keygen.go b/internal/cmd/aes/keygen.go index 8e09841..17dc563 100644 --- a/internal/cmd/aes/keygen.go +++ b/internal/cmd/aes/keygen.go @@ -18,13 +18,13 @@ func KeyGen(args []string) error { fmt.Fprint(os.Stderr, `Usage: pocryp aes-keygen [-out OUTPUT] NUM_BITS Generate AES key. +Valid NUM_BITS: 128, 192, 256. If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.") @@ -34,6 +34,7 @@ Options: } if fset.NArg() == 0 { + fset.Usage() return errors.New("number of bits not specified") } @@ -43,6 +44,7 @@ Options: } if !(numBits == 128 || numBits == 192 || numBits == 256) { + fset.Usage() return errors.New("invalid num bits requested") } diff --git a/internal/cmd/aes/keywrap.go b/internal/cmd/aes/keywrap.go index 2674159..0508156 100644 --- a/internal/cmd/aes/keywrap.go +++ b/internal/cmd/aes/keywrap.go @@ -25,7 +25,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fWrap := fset.Bool("w", false, "Wrap the input to the output. Default if omitted.") @@ -40,9 +39,11 @@ Options: } if *fKey == "" && *fKeyFile == "" { + fset.Usage() return errors.New("no key specified, use -k or --key-file to specify it") } if *fKey != "" && *fKeyFile != "" { + fset.Usage() return errors.New("cannot use -k and --key-file at the same time") } diff --git a/internal/cmd/hash/sha.go b/internal/cmd/hash/sha.go index 898f238..a8ccf24 100644 --- a/internal/cmd/hash/sha.go +++ b/internal/cmd/hash/sha.go @@ -24,7 +24,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.") @@ -37,11 +36,13 @@ Options: } if *fAlg == "" { + fset.Usage() return errors.New("hash alg not specified, use -alg") } hashFunc, err := common.HashFuncFrom(*fAlg) if err != nil { + fset.Usage() return err } diff --git a/internal/cmd/kdf/pbkdf2.go b/internal/cmd/kdf/pbkdf2.go index 6051369..edb7be8 100644 --- a/internal/cmd/kdf/pbkdf2.go +++ b/internal/cmd/kdf/pbkdf2.go @@ -27,7 +27,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.") @@ -48,21 +47,26 @@ Options: } if *fKey == "" && *fKeyFile == "" { + fset.Usage() return errors.New("no key specified, use -key or -key-file to specify it") } if *fKey != "" && *fKeyFile != "" { + fset.Usage() return errors.New("cannot use -key and -key-file at the same time") } if *fSalt == "" && *fSaltFile == "" { + fset.Usage() return errors.New("no salt specified, use -salt or -salt-file to specify it") } if *fSalt != "" && *fSaltFile != "" { + fset.Usage() return errors.New("cannot use -salt and -salt-file at the same time") } hashFunc, err := common.HashFuncFrom(*fHashFunc) if err != nil { + fset.Usage() return err } diff --git a/internal/cmd/rsa/der_pem.go b/internal/cmd/rsa/der_pem.go index 0ee2a1c..709536b 100644 --- a/internal/cmd/rsa/der_pem.go +++ b/internal/cmd/rsa/der_pem.go @@ -25,7 +25,6 @@ DER input must be specified in binary form. Options: `) fset.PrintDefaults() - os.Exit(1) } fPriv := fset.Bool("priv", false, "Encode PrivateKey from given input.") @@ -73,6 +72,7 @@ Options: case *fPub: blockType = "RSA PUBLIC KEY" default: + fset.Usage() return errors.New("need to specify one of -priv or -pub") } diff --git a/internal/cmd/rsa/der_raw.go b/internal/cmd/rsa/der_raw.go index 66b2d65..1f455b9 100644 --- a/internal/cmd/rsa/der_raw.go +++ b/internal/cmd/rsa/der_raw.go @@ -21,7 +21,6 @@ DER must be specified in hex form. Options: `) fset.PrintDefaults() - os.Exit(1) } fPriv := fset.Bool("priv", false, "Encode PrivateKey from given input.") @@ -32,6 +31,7 @@ Options: } if fset.NArg() != 1 { + fset.Usage() return errors.New("DER hex string not specified") } @@ -59,6 +59,7 @@ Options: fmt.Printf("n=%s\n", hex.EncodeToString(key.N.Bytes())) fmt.Printf("e=%x\n", key.E) default: + fset.Usage() return errors.New("need to specify one of -priv or -pub") } diff --git a/internal/cmd/rsa/kem.go b/internal/cmd/rsa/kem.go index f3db35c..9072278 100644 --- a/internal/cmd/rsa/kem.go +++ b/internal/cmd/rsa/kem.go @@ -27,7 +27,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fEncapsulate := fset.Bool("e", false, "Encapsulate the input to the output. Default if omitted.") @@ -49,6 +48,7 @@ Options: } if *fKey == "" { + fset.Usage() return errors.New("no key specified, use -key to specify it") } @@ -71,6 +71,7 @@ Options: } if *fKdfSalt == "" { + fset.Usage() return errors.New("KDF salt cannot be empty") } kdfSalt, err := hex.DecodeString(*fKdfSalt) @@ -80,6 +81,7 @@ Options: kdfHashFunc, err := common.HashFuncFrom(*fKdfHashFunc) if err != nil { + fset.Usage() return err } diff --git a/internal/cmd/rsa/keygen.go b/internal/cmd/rsa/keygen.go index f3fa79a..23d1ddd 100644 --- a/internal/cmd/rsa/keygen.go +++ b/internal/cmd/rsa/keygen.go @@ -19,13 +19,13 @@ func KeyGen(args []string) error { fmt.Fprint(os.Stderr, `Usage: pocryp rsa-keygen [-out OUTPUT] NUM_BITS Generate RSA key. +Valid NUM_BITS: 2048, 3072, 4096. If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.") @@ -35,6 +35,7 @@ Options: } if fset.NArg() == 0 { + fset.Usage() return errors.New("number of bits not specified") } @@ -44,6 +45,7 @@ Options: } if !(numBits == 2048 || numBits == 3072 || numBits == 4096) { + fset.Usage() return errors.New("invalid num bits requested") } diff --git a/internal/cmd/rsa/pem_der.go b/internal/cmd/rsa/pem_der.go index cc1945c..37524e1 100644 --- a/internal/cmd/rsa/pem_der.go +++ b/internal/cmd/rsa/pem_der.go @@ -24,7 +24,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.") diff --git a/internal/cmd/rsa/pub_from_priv.go b/internal/cmd/rsa/pub_from_priv.go index 982cc66..c891926 100644 --- a/internal/cmd/rsa/pub_from_priv.go +++ b/internal/cmd/rsa/pub_from_priv.go @@ -25,7 +25,6 @@ If -out is not specified, the output will be printed to stdout. Options: `) fset.PrintDefaults() - os.Exit(1) } fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.") diff --git a/internal/cmd/rsa/raw_der.go b/internal/cmd/rsa/raw_der.go index 5055d9f..bd8ddd2 100644 --- a/internal/cmd/rsa/raw_der.go +++ b/internal/cmd/rsa/raw_der.go @@ -21,7 +21,6 @@ Convert RSA key from raw values(n, e, d, p, q) to PKCS#1 ASN.1 DER. Options: `) fset.PrintDefaults() - os.Exit(1) } fPriv := fset.Bool("priv", false, "Encode PrivateKey from given inputs.") @@ -37,10 +36,12 @@ Options: } if *fMod == "" { + fset.Usage() return errors.New("modulus not specified, use -n to specify it") } if *fPub && *fPriv { + fset.Usage() return errors.New("cannot specify -priv and -pub at the same time, choose one") } @@ -55,15 +56,19 @@ Options: switch { case *fPriv: if *fPubExp == 0 { + fset.Usage() return errors.New("-e is needed") } if *fPrivExp == "" { + fset.Usage() return errors.New("-d is needed") } if *fPrime1 == "" { + fset.Usage() return errors.New("-p is needed") } if *fPrime2 == "" { + fset.Usage() return errors.New("-q is needed") } dBytes, err := hex.DecodeString(*fPrivExp) @@ -95,6 +100,7 @@ Options: result = x509.MarshalPKCS1PrivateKey(key) case *fPub: if *fPubExp == 0 { + fset.Usage() return errors.New("-e is needed") } key := &rsa.PublicKey{ @@ -103,6 +109,7 @@ Options: } result = x509.MarshalPKCS1PublicKey(key) default: + fset.Usage() return errors.New("need to specify one of -priv or -pub") }