2019-02-04 12:21:58 -05:00
|
|
|
package ssh
|
2019-01-30 21:59:56 -05:00
|
|
|
|
|
|
|
import (
|
2019-02-03 10:36:06 -05:00
|
|
|
"bytes"
|
2019-02-28 09:56:11 -05:00
|
|
|
"crypto"
|
2019-02-06 13:46:55 -05:00
|
|
|
"crypto/dsa"
|
2019-01-30 21:59:56 -05:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2019-02-06 13:46:55 -05:00
|
|
|
"fmt"
|
2019-02-04 12:07:32 -05:00
|
|
|
"strings"
|
2019-01-30 21:59:56 -05:00
|
|
|
|
2019-02-04 12:29:47 -05:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
2019-01-30 21:59:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-01 12:17:13 -05:00
|
|
|
// defaultRsaBits is the default bits of entropy for a new RSA
|
|
|
|
// key pair. That's a lot of bits.
|
2019-01-30 21:59:56 -05:00
|
|
|
defaultRsaBits = 4096
|
|
|
|
|
2019-02-06 15:05:56 -05:00
|
|
|
// Markers for various SSH key pair types.
|
2019-02-06 13:46:55 -05:00
|
|
|
Default KeyPairType = ""
|
|
|
|
Rsa KeyPairType = "RSA"
|
|
|
|
Ecdsa KeyPairType = "ECDSA"
|
|
|
|
Dsa KeyPairType = "DSA"
|
|
|
|
Ed25519 KeyPairType = "ED25519"
|
2019-01-30 21:59:56 -05:00
|
|
|
)
|
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
// KeyPairType represents different types of SSH key pairs.
|
2019-02-04 12:23:42 -05:00
|
|
|
// See the 'const' block for details.
|
2019-02-04 12:21:58 -05:00
|
|
|
type KeyPairType string
|
2019-01-30 21:59:56 -05:00
|
|
|
|
2019-02-04 12:21:58 -05:00
|
|
|
func (o KeyPairType) String() string {
|
2019-01-30 21:59:56 -05:00
|
|
|
return string(o)
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
// KeyPair represents an SSH key pair.
|
|
|
|
type KeyPair struct {
|
|
|
|
// PrivateKeyPemBlock represents the key pair's private key in
|
|
|
|
// ASN.1 Distinguished Encoding Rules (DER) format in a
|
|
|
|
// Privacy-Enhanced Mail (PEM) block.
|
|
|
|
PrivateKeyPemBlock []byte
|
|
|
|
|
|
|
|
// PublicKeyAuthorizedKeysLine represents the key pair's public key
|
|
|
|
// as a line in OpenSSH authorized_keys.
|
|
|
|
PublicKeyAuthorizedKeysLine []byte
|
|
|
|
|
2019-03-01 10:00:29 -05:00
|
|
|
// Comment is the key pair's comment. This is typically used
|
|
|
|
// to identify the key pair's owner in the SSH user's
|
|
|
|
// 'authorized_keys' file.
|
|
|
|
Comment string
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
// KeyPairFromPrivateKey returns a KeyPair loaded from an existing private key.
|
|
|
|
//
|
|
|
|
// Supported key pair types include:
|
|
|
|
// - DSA
|
|
|
|
// - ECDSA
|
|
|
|
// - ED25519
|
|
|
|
// - RSA
|
|
|
|
func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) {
|
|
|
|
privateKey, err := gossh.ParseRawPrivateKey(config.RawPrivateKeyPemBlock)
|
2019-02-06 13:46:55 -05:00
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-06 13:46:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch pk := privateKey.(type) {
|
2019-02-28 09:56:11 -05:00
|
|
|
case crypto.Signer:
|
|
|
|
// crypto.Signer is implemented by ecdsa.PrivateKey,
|
|
|
|
// ed25519.PrivateKey, and rsa.PrivateKey - separate cases
|
|
|
|
// for each PrivateKey type would be redundant.
|
|
|
|
publicKey, err := gossh.NewPublicKey(pk.Public())
|
2019-02-06 13:46:55 -05:00
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-06 13:46:55 -05:00
|
|
|
}
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{
|
2019-07-25 17:09:32 -04:00
|
|
|
Comment: config.Comment,
|
2019-02-25 17:16:24 -05:00
|
|
|
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
|
2019-03-01 10:00:29 -05:00
|
|
|
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment),
|
2019-02-06 13:46:55 -05:00
|
|
|
}, nil
|
|
|
|
case *dsa.PrivateKey:
|
|
|
|
publicKey, err := gossh.NewPublicKey(&pk.PublicKey)
|
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-06 13:46:55 -05:00
|
|
|
}
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{
|
2019-07-25 17:09:32 -04:00
|
|
|
Comment: config.Comment,
|
2019-02-25 17:16:24 -05:00
|
|
|
PrivateKeyPemBlock: config.RawPrivateKeyPemBlock,
|
2019-03-01 10:00:29 -05:00
|
|
|
PublicKeyAuthorizedKeysLine: authorizedKeysLine(publicKey, config.Comment),
|
2019-02-06 13:46:55 -05:00
|
|
|
}, nil
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, fmt.Errorf("Cannot parse existing SSH key pair - unknown key pair type")
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:42:20 -05:00
|
|
|
// FromPrivateKeyConfig describes how an SSH key pair should be loaded from an
|
|
|
|
// existing private key.
|
|
|
|
type FromPrivateKeyConfig struct {
|
|
|
|
// RawPrivateKeyPemBlock is the raw private key that the key pair
|
|
|
|
// should be loaded from.
|
|
|
|
RawPrivateKeyPemBlock []byte
|
|
|
|
|
2019-03-01 10:00:29 -05:00
|
|
|
// Comment is the key pair's comment. This is typically used
|
|
|
|
// to identify the key pair's owner in the SSH user's
|
|
|
|
// 'authorized_keys' file.
|
|
|
|
Comment string
|
2019-02-28 09:42:20 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
// NewKeyPair generates a new SSH key pair using the specified
|
|
|
|
// CreateKeyPairConfig.
|
|
|
|
func NewKeyPair(config CreateKeyPairConfig) (KeyPair, error) {
|
|
|
|
if config.Type == Default {
|
|
|
|
config.Type = Ecdsa
|
|
|
|
}
|
|
|
|
|
|
|
|
switch config.Type {
|
|
|
|
case Ecdsa:
|
|
|
|
return newEcdsaKeyPair(config)
|
|
|
|
case Rsa:
|
|
|
|
return newRsaKeyPair(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
return KeyPair{}, fmt.Errorf("Unable to generate new key pair, type %s is not supported",
|
|
|
|
config.Type.String())
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 14:12:18 -05:00
|
|
|
// newEcdsaKeyPair returns a new ECDSA SSH key pair.
|
2019-02-25 17:16:24 -05:00
|
|
|
func newEcdsaKeyPair(config CreateKeyPairConfig) (KeyPair, error) {
|
2019-02-04 12:09:23 -05:00
|
|
|
var curve elliptic.Curve
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
switch config.Bits {
|
2019-02-04 12:09:23 -05:00
|
|
|
case 0:
|
2019-02-25 17:16:24 -05:00
|
|
|
config.Bits = 521
|
2019-02-04 12:09:23 -05:00
|
|
|
fallthrough
|
|
|
|
case 521:
|
|
|
|
curve = elliptic.P521()
|
|
|
|
case 384:
|
2019-02-04 14:25:37 -05:00
|
|
|
curve = elliptic.P384()
|
2019-02-04 12:09:23 -05:00
|
|
|
case 256:
|
2019-02-04 14:25:37 -05:00
|
|
|
curve = elliptic.P256()
|
2019-02-04 12:09:23 -05:00
|
|
|
case 224:
|
|
|
|
// Not supported by "golang.org/x/crypto/ssh".
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, fmt.Errorf("golang.org/x/crypto/ssh does not support %d bits", config.Bits)
|
2019-02-04 12:09:23 -05:00
|
|
|
default:
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, fmt.Errorf("crypto/elliptic does not support %d bits", config.Bits)
|
2019-02-04 12:09:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
privateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
|
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-04 12:09:23 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:29:47 -05:00
|
|
|
sshPublicKey, err := gossh.NewPublicKey(&privateKey.PublicKey)
|
2019-02-04 12:09:23 -05:00
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-04 12:09:23 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 11:53:12 -05:00
|
|
|
privateRaw, err := x509.MarshalECPrivateKey(privateKey)
|
2019-02-04 12:09:23 -05:00
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-04 12:09:23 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 11:53:12 -05:00
|
|
|
privatePem, err := rawPemBlock(&pem.Block{
|
|
|
|
Type: "EC PRIVATE KEY",
|
|
|
|
Headers: nil,
|
|
|
|
Bytes: privateRaw,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-05 11:53:12 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{
|
|
|
|
PrivateKeyPemBlock: privatePem,
|
2019-03-01 10:00:29 -05:00
|
|
|
PublicKeyAuthorizedKeysLine: authorizedKeysLine(sshPublicKey, config.Comment),
|
|
|
|
Comment: config.Comment,
|
2019-02-04 12:09:23 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 14:12:18 -05:00
|
|
|
// newRsaKeyPair returns a new RSA SSH key pair.
|
2019-02-25 17:16:24 -05:00
|
|
|
func newRsaKeyPair(config CreateKeyPairConfig) (KeyPair, error) {
|
|
|
|
if config.Bits == 0 {
|
|
|
|
config.Bits = defaultRsaBits
|
2019-02-04 12:09:23 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, config.Bits)
|
2019-02-04 12:09:23 -05:00
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-04 12:09:23 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:29:47 -05:00
|
|
|
sshPublicKey, err := gossh.NewPublicKey(&privateKey.PublicKey)
|
2019-02-04 12:09:23 -05:00
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-05 11:53:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
privatePemBlock, err := rawPemBlock(&pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Headers: nil,
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{}, err
|
2019-02-04 12:09:23 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:16:24 -05:00
|
|
|
return KeyPair{
|
|
|
|
PrivateKeyPemBlock: privatePemBlock,
|
2019-03-01 10:00:29 -05:00
|
|
|
PublicKeyAuthorizedKeysLine: authorizedKeysLine(sshPublicKey, config.Comment),
|
|
|
|
Comment: config.Comment,
|
2019-02-04 12:09:23 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:44:25 -05:00
|
|
|
// CreateKeyPairConfig describes how an SSH key pair should be created.
|
|
|
|
type CreateKeyPairConfig struct {
|
|
|
|
// Type describes the key pair's type.
|
|
|
|
Type KeyPairType
|
|
|
|
|
|
|
|
// Bits represents the key pair's bits of entropy. E.g., 4096 for
|
|
|
|
// a 4096 bit RSA key pair, or 521 for a ECDSA key pair with a
|
|
|
|
// 521-bit curve.
|
|
|
|
Bits int
|
|
|
|
|
2019-03-01 10:00:29 -05:00
|
|
|
// Comment is the resulting key pair's comment. This is typically
|
|
|
|
// used to identify the key pair's owner in the SSH user's
|
|
|
|
// 'authorized_keys' file.
|
|
|
|
Comment string
|
2019-02-28 09:44:25 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 11:53:12 -05:00
|
|
|
// rawPemBlock encodes a pem.Block to a slice of bytes.
|
|
|
|
func rawPemBlock(block *pem.Block) ([]byte, error) {
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
2019-01-30 21:59:56 -05:00
|
|
|
|
2019-02-05 11:53:12 -05:00
|
|
|
err := pem.Encode(buffer, block)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2019-02-04 12:07:32 -05:00
|
|
|
|
2019-02-05 11:53:12 -05:00
|
|
|
return buffer.Bytes(), nil
|
2019-02-03 09:17:18 -05:00
|
|
|
}
|
|
|
|
|
2019-02-28 09:40:00 -05:00
|
|
|
// authorizedKeysLine serializes key for inclusion in an OpenSSH
|
|
|
|
// authorized_keys file. The return value ends without newline so
|
2019-03-01 10:00:29 -05:00
|
|
|
// a comment can be appended to the end.
|
|
|
|
func authorizedKeysLine(key gossh.PublicKey, comment string) []byte {
|
2019-02-28 09:40:00 -05:00
|
|
|
marshaledPublicKey := gossh.MarshalAuthorizedKey(key)
|
|
|
|
|
|
|
|
// Remove the mandatory unix new line. Awful, but the go
|
|
|
|
// ssh library automatically appends a unix new line.
|
2019-03-01 10:00:29 -05:00
|
|
|
// We remove it so a key comment can be safely appended to the
|
2019-02-28 09:40:00 -05:00
|
|
|
// end of the string.
|
|
|
|
marshaledPublicKey = bytes.TrimSpace(marshaledPublicKey)
|
2019-02-04 14:27:59 -05:00
|
|
|
|
2019-03-01 10:00:29 -05:00
|
|
|
if len(strings.TrimSpace(comment)) > 0 {
|
2019-02-28 09:40:00 -05:00
|
|
|
marshaledPublicKey = append(marshaledPublicKey, ' ')
|
2019-03-01 10:00:29 -05:00
|
|
|
marshaledPublicKey = append(marshaledPublicKey, comment...)
|
2019-02-04 12:07:32 -05:00
|
|
|
}
|
|
|
|
|
2019-02-28 09:40:00 -05:00
|
|
|
return marshaledPublicKey
|
2019-01-30 21:59:56 -05:00
|
|
|
}
|