aboutsummaryrefslogtreecommitdiff
path: root/public_key.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-01-14 21:42:37 +0100
committerGibheer <gibheer@gmail.com>2015-01-14 21:42:37 +0100
commit73a07e7665ceb5ea35b33091e286774e4f5ab04e (patch)
tree0b8a65ea11ccdc9d050afdd06da5df05fb498846 /public_key.go
parent0a7c8b863bee1572b90ca5d0a037efd1c6bcd54f (diff)
add api for public keys
This enables pkictl to generate public keys from private keys in the rsa and ecdsa format.
Diffstat (limited to 'public_key.go')
-rw-r--r--public_key.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/public_key.go b/public_key.go
new file mode 100644
index 0000000..fc1ea23
--- /dev/null
+++ b/public_key.go
@@ -0,0 +1,46 @@
+package main
+
+import (
+ "crypto/x509"
+ "encoding/pem"
+ "flag"
+ "fmt"
+ "io"
+ "os"
+)
+
+type (
+ PublicKeyFlags struct {
+ PrivateKeyPath string
+ Output string
+
+ output_stream io.WriteCloser // the actual stream to the output
+ }
+)
+
+func create_public_key() {
+ var err error
+ flags := parse_public_key_flags()
+ flags.output_stream, err = open_output_stream(flags.Output)
+ if err != nil {
+ crash_with_help(2, fmt.Sprintf("Error when creating file %s: %s", flags.Output, err))
+ }
+ priv_key := load_private_key(flags.PrivateKeyPath)
+ marshal, err := x509.MarshalPKIXPublicKey(priv_key.Public())
+ if err != nil {
+ crash_with_help(2, fmt.Sprintf("Problems marshalling the public key: %s", err))
+ }
+
+ block := &pem.Block{Type: TypeLabelPubKey, Bytes: marshal}
+ pem.Encode(flags.output_stream, block)
+}
+
+func parse_public_key_flags() PublicKeyFlags {
+ flags := PublicKeyFlags{}
+ fs := flag.NewFlagSet("create-public", flag.ExitOnError)
+ fs.StringVar(&flags.PrivateKeyPath, "private-key", "", "path to the private key file")
+ fs.StringVar(&flags.Output, "output", "STDOUT", "path where the generated csr should be stored")
+ fs.Parse(os.Args[2:])
+
+ return flags
+}