aboutsummaryrefslogtreecommitdiff
path: root/load_private_key.go
diff options
context:
space:
mode:
authorGibheer <gibheer+git@zero-knowledge.org>2016-10-01 21:56:29 +0200
committerGibheer <gibheer+git@zero-knowledge.org>2016-10-01 21:56:29 +0200
commitd01892150eed9d58210eb40b7c005d5fa8e93238 (patch)
treef9d37f3d5b4f0d9afd01755801826713f47d83c3 /load_private_key.go
parentfaaf7d8859895767b5e64d32c14d561d6fdb5a14 (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 'load_private_key.go')
-rw-r--r--load_private_key.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/load_private_key.go b/load_private_key.go
new file mode 100644
index 0000000..4678862
--- /dev/null
+++ b/load_private_key.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/gibheer/pki"
+)
+
+func loadPrivateKey(path string) (pki.PrivateKey, error) {
+ if path == "" {
+ return nil, fmt.Errorf("no path given")
+ }
+ var err error
+ file, err := os.Open(path)
+ if err != nil {
+ return nil, err
+ }
+ defer file.Close()
+ info, err := file.Stat()
+ if err != nil {
+ return nil, err
+ }
+ if info.Mode().Perm().String()[4:] != "------" {
+ return nil, fmt.Errorf("private key must not be readable for group or world")
+ }
+
+ pems, err := parseFile(file)
+ if err != nil {
+ return nil, err
+ }
+ if len(pems) > 1 {
+ return nil, fmt.Errorf("more than one object in file")
+ }
+
+ var pk pki.PrivateKey
+ for key, parts := range pems {
+ if len(parts) > 1 {
+ return nil, fmt.Errorf("more than one object found")
+ }
+ switch key {
+ case pki.PemLabelRsa:
+ pk, err = pki.LoadPrivateKeyRsa(parts[0])
+ case pki.PemLabelEd25519:
+ pk, err = pki.LoadPrivateKeyEd25519(parts[0])
+ case pki.PemLabelEcdsa:
+ pk, err = pki.LoadPrivateKeyEcdsa(parts[0])
+ default:
+ return nil, fmt.Errorf("unknown private key format %s", key)
+ }
+ }
+ return pk, err
+}