diff options
author | Gibheer <gibheer+git@zero-knowledge.org> | 2016-10-01 21:56:29 +0200 |
---|---|---|
committer | Gibheer <gibheer+git@zero-knowledge.org> | 2016-10-01 21:56:29 +0200 |
commit | d01892150eed9d58210eb40b7c005d5fa8e93238 (patch) | |
tree | f9d37f3d5b4f0d9afd01755801826713f47d83c3 /main.go | |
parent | faaf7d8859895767b5e64d32c14d561d6fdb5a14 (diff) |
rework program flow
This commit is a complete rebuild of pkictl. Before everything was all
over the place and adding new commands was kind of a hassle.
Now each command has its own file and can be adjusted on a command
basis. Options are still used by the same name, but can now use
different descriptions.
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 153 |
1 files changed, 38 insertions, 115 deletions
@@ -4,130 +4,53 @@ package main import ( - "crypto" - "encoding/base64" "fmt" - "io" - "io/ioutil" "os" - - "github.com/gibheer/pki" ) const ( - ErrorProgram int = iota - ErrorFlagInput - ErrorInput -) - -var ( - EmptyByteArray = make([]byte, 0) + COMMAND = "pkictl" ) func main() { - InitFlags() - CmdRoot.Execute() -} - -// create a public key derived from a private key -func create_public_key(cmd *Command, args []string) { - err := checkFlags(checkPrivateKey, checkOutput) - if err != nil { - crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err) - } - - var pub_key pki.Pemmer - pub_key = FlagPrivateKey.Public() - marsh_pem, err := pub_key.MarshalPem() - if err != nil { - crash_with_help(cmd, ErrorProgram, "Error when marshalling to pem: %s", err) - } - _, err = marsh_pem.WriteTo(FlagOutput) - if err != nil { - crash_with_help(cmd, ErrorProgram, "Error when writing output: %s", err) - } -} - -// sign a message using he private key -func sign_input(cmd *Command, args []string) { - err := checkFlags(checkPrivateKey, checkInput, checkOutput) - if err != nil { - crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err) - } - - message, err := ioutil.ReadAll(FlagInput) - if err != nil { - crash_with_help(cmd, ErrorProgram, "Error reading input: %s", err) - } - signature, err := FlagPrivateKey.Sign(message, crypto.SHA256) - if err != nil { - crash_with_help(cmd, ErrorProgram, "Could not compute signature: %s", err) - } - _, err = io.WriteString(FlagOutput, base64.StdEncoding.EncodeToString(signature)) - if err != nil { - crash_with_help(cmd, ErrorProgram, "Could not write to output: %s", err) - } - - // if we print to stderr, send a final line break to make the output nice - if FlagOutput == os.Stdout { - // we can ignore the result, as either Stdout did work or not - _, _ = io.WriteString(FlagOutput, "\n") + if len(os.Args) == 1 { + printHelp() + return + } + command, args := os.Args[1], os.Args[2:] + var err error + switch command { + case "create-private": + err = CreatePrivateKey(args) + case "create-public": + err = CreatePublicKey(args) + case "sign-input": + err = SignInput(args) + case "verify-input": + err = VerifyInput(args) + case "create-sign-request": + err = CreateSignRequest(args) + case "create-cert": + err = CreateCert(args) + default: + printHelp() + } + if err != nil { + fmt.Printf("error: %s\n", err) + os.Exit(2) } } -// verify a message using a signature and a public key -func verify_input(cmd *Command, args []string) { - err := checkFlags(checkPublicKey, checkInput, checkOutput, checkSignature) - if err != nil { - crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err) - } - - signature := FlagSignature - message, err := ioutil.ReadAll(FlagInput) - if err != nil { - crash_with_help(cmd, ErrorProgram, "Error reading input: %s", err) - } - valid, err := FlagPublicKey.Verify(message, signature, crypto.SHA256) - if err != nil { - crash_with_help(cmd, ErrorProgram, "Could not verify message using signature: %s", err) - } - if valid { - fmt.Println("valid") - os.Exit(0) - } - fmt.Println("invalid") - os.Exit(1) -} - -// create a certificate sign request -func create_sign_request(cmd *Command, args []string) { - err := checkFlags(checkPrivateKey, checkOutput, checkCertificateFields) - if err != nil { - crash_with_help(cmd, ErrorFlagInput, "Flags invalid: %s", err) - } - - csr, err := FlagCertificateRequestData.ToCertificateRequest(FlagPrivateKey) - if err != nil { - crash_with_help(cmd, ErrorProgram, "Could not create certificate sign request: %s", err) - } - pem_block, err := csr.MarshalPem() - if err != nil { - crash_with_help(cmd, ErrorProgram, "Error when marshalling to pem: %s", err) - } - _, err = pem_block.WriteTo(FlagOutput) - if err != nil { - crash_with_help(cmd, ErrorProgram, "Could not write to output: %s", err) - } -} - -// crash and provide a helpful message -func crash_with_help(cmd *Command, code int, message string, args ...interface{}) { - fmt.Fprintf(os.Stderr, message+"\n", args...) - cmd.Usage() - os.Exit(code) -} - -// return the arguments to the program -func program_args() []string { - return os.Args[2:] +func printHelp() { + fmt.Printf(`Usage: %s command [flags] + +where 'command' is one of: + create-private create a private key + create-public create a public key derived from a private key + sign-input sign a message using a private key + verify-input verify a message using a signature and a public key + create-sign-request create a certificate sign request + create-cert create a certificate from a certificate sign request + diff show the differences between two certificates +`, COMMAND) } |