aboutsummaryrefslogtreecommitdiff
path: root/client/keys.go
diff options
context:
space:
mode:
Diffstat (limited to 'client/keys.go')
-rw-r--r--client/keys.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/client/keys.go b/client/keys.go
index 73983a8..b488ea2 100644
--- a/client/keys.go
+++ b/client/keys.go
@@ -6,12 +6,16 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
+ "crypto/x509"
+ "encoding/pem"
"fmt"
"github.com/pkg/errors"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/ssh"
+
+ "github.com/mikesmitty/edkey"
)
// Key is a private key.
@@ -32,6 +36,24 @@ var defaultOptions = options{
// A KeyOption is used to generate keys of different types and sizes.
type KeyOption func(*options)
+func pemBlockForKey(priv interface{}) (*pem.Block, error) {
+ switch k := priv.(type) {
+ case *rsa.PrivateKey:
+ return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}, nil
+ case *ecdsa.PrivateKey:
+ b, err := x509.MarshalECPrivateKey(k)
+ if err != nil {
+ return nil, err
+ }
+ return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}, nil
+ case *ed25519.PrivateKey:
+ b := edkey.MarshalED25519PrivateKey(*k)
+ return &pem.Block{Type: "OPENSSH PRIVATE KEY", Bytes: b}, nil
+ default:
+ return nil, fmt.Errorf("Unable to create PEM blck from key")
+ }
+}
+
// KeyType sets the type of key to generate.
// Valid types are: "rsa", "ecdsa", "ed25519".
// Default is "rsa"