aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-02-15 01:34:25 +0100
committerGibheer <gibheer@gmail.com>2015-02-15 01:34:25 +0100
commit16eb14db9f9b228ef88bcf1beb09cf823256dac1 (patch)
tree414ed9ba9f3e5679a7b0ae7b120e752d3f8ee2f6 /main.go
parent2f9126dc6a2eab32316ec90e21688d31159f9e80 (diff)
redesign cli
This is a major rebuilding of the CLI. The library part is split out into pkilib and the cli handles only the communication with the user, I/O and the library. The API will still look the same, but the code should be much better to grasp. Instead of repeating everything, more will be grouped together and reused.
Diffstat (limited to 'main.go')
-rw-r--r--main.go91
1 files changed, 51 insertions, 40 deletions
diff --git a/main.go b/main.go
index 7a6bc5e..7ee9396 100644
--- a/main.go
+++ b/main.go
@@ -2,69 +2,74 @@ package main
import (
"fmt"
- "io"
"os"
"path/filepath"
-)
-const (
- RsaLowerLength = 2048
- RsaUpperLength = 4096
- TypeLabelRSA = "RSA PRIVATE KEY"
- TypeLabelECDSA = "EC PRIVATE KEY"
- TypeLabelCSR = "CERTIFICATE REQUEST"
- TypeLabelPubKey = "PUBLIC KEY"
+ "github.com/gibheer/pkilib"
)
var (
- EcdsaLength = []int{224, 256, 384, 521}
+ EmptyByteArray = make([]byte, 0)
)
+//const (
+// RsaLowerLength = 2048
+// RsaUpperLength = 4096
+// TypeLabelRSA = "RSA PRIVATE KEY"
+// TypeLabelECDSA = "EC PRIVATE KEY"
+// TypeLabelCSR = "CERTIFICATE REQUEST"
+// TypeLabelPubKey = "PUBLIC KEY"
+//)
+//
+//var (
+// EcdsaLength = []int{224, 256, 384, 521}
+//)
+//
func main() {
if len(os.Args) == 1 {
crash_with_help(1, "No module selected!")
}
switch os.Args[1] {
case "create-private": create_private_key()
- case "create-cert-sign": create_sign_request()
case "create-public": create_public_key()
- case "help": print_modules()
- case "info": info_on_file()
- case "sign-request": sign_request()
- case "sign-input": sign_input()
- case "verify-signature": verify_signature()
+// case "create-cert-sign": create_sign_request()
+// case "help": print_modules()
+// case "info": info_on_file()
+// case "sign-request": sign_request()
+// case "sign-input": sign_input()
+// case "verify-signature": verify_signature()
default: crash_with_help(1, "Command not supported!")
}
}
-// get information on file (private key, sign request, certificate, ...)
-func info_on_file() {}
-// sign a certificate request to create a new certificate
-func sign_request() {}
+// create a private key
+func create_private_key() {
+ fs := NewFlags("create-private")
+ fs.AddOutput()
+ fs.AddPrivateKeyGenerationFlags()
+ err := fs.Parse(program_args())
+ if err != nil { crash_with_help(1, fmt.Sprintf("%s", err)) }
-// open stream for given path
-func open_output_stream(path string) (io.WriteCloser, error) {
- switch path {
- case "STDOUT": return os.Stdout, nil
- case "STDERR": return os.Stderr, nil
- default: return open_stream(path, os.O_WRONLY | os.O_CREATE | os.O_TRUNC)
+ var pk pkilib.Pemmer
+ switch fs.Flags.PrivateKeyGenerationFlags.Type {
+ case "ecdsa": pk, err = pkilib.NewPrivateKeyEcdsa(fs.Flags.PrivateKeyGenerationFlags.Curve)
+ case "rsa": pk, err = pkilib.NewPrivateKeyRsa(fs.Flags.PrivateKeyGenerationFlags.Size)
}
+ if err != nil { crash_with_help(2, fmt.Sprintf("%s", err)) }
+ marsh_pem, err := pk.MarshalPem()
+ if err != nil { crash_with_help(2, fmt.Sprintf("%s", err)) }
+ _, err = marsh_pem.WriteTo(fs.Flags.Output)
+ if err != nil { crash_with_help(2, fmt.Sprintf("%s", err)) }
}
-func open_input_stream(path string) (io.ReadCloser, error) {
- switch path {
- case "STDIN": return os.Stdin, nil
- default: return open_stream(path, os.O_RDONLY)
- }
-}
+// create a public key derived from a private key
+func create_public_key() {
+ fs := NewFlags("create-public")
+ fs.AddPrivateKey()
+ err := fs.Parse(program_args())
+ if err != nil { crash_with_help(1, fmt.Sprintf("%s", err)) }
-func open_stream(path string, flags int) (io.ReadWriteCloser, error) {
- var err error
- output_stream, err := os.OpenFile(path, flags, 0600)
- if err != nil {
- return nil, err
- }
- return output_stream, nil
+ fmt.Println(fs.Flags.PrivateKey.Public())
}
// print the module help
@@ -76,15 +81,21 @@ where 'command' is one of:
create-cert-sign create a new certificate sign request
help show this help
info get info on a file
- sign sign a certificate request
+ sign-request sign a certificate request
sign-input sign a message with a private key
verify-signature verify a signature
`, filepath.Base(os.Args[0]))
fmt.Println()
}
+// crash and provide a helpful message
func crash_with_help(code int, message string) {
fmt.Fprintln(os.Stderr, message)
print_modules()
os.Exit(code)
}
+
+// return the arguments to the program
+func program_args() []string {
+ return os.Args[2:]
+}