diff options
| author | Paul Buetow <paul@buetow.org> | 2021-10-29 08:16:03 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-10-29 08:16:03 +0300 |
| commit | da05e08ad5ef57a29609397d81327c92ce848652 (patch) | |
| tree | c21acd111e5bd012ff0aaa7c91f6e9d688ee9ad9 /internal/ssh/client/clientkeypair.go | |
| parent | d556c13d430f291b615d538c35ebdaf9b53aa15d (diff) | |
| parent | dadbaab24d66685db0a2a6655bd75cdbb19eb929 (diff) | |
merge
Diffstat (limited to 'internal/ssh/client/clientkeypair.go')
| -rw-r--r-- | internal/ssh/client/clientkeypair.go | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/internal/ssh/client/clientkeypair.go b/internal/ssh/client/clientkeypair.go new file mode 100644 index 0000000..0e21d0c --- /dev/null +++ b/internal/ssh/client/clientkeypair.go @@ -0,0 +1,91 @@ +package client + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/pem" + "fmt" + "io/ioutil" + "os" + + "github.com/mimecast/dtail/internal/io/dlog" + "golang.org/x/crypto/ssh" +) + +// GeneratePrivatePublicKeyPairIfNotExists generates a SSH key pair (used by the integration tests) +func GeneratePrivatePublicKeyPairIfNotExists(keyPath string, bitSize int) { + if _, err := os.Stat(keyPath); err == nil { + dlog.Common.Debug("Private/public key pair already exists", keyPath) + return + } + GeneratePrivatePublicKeyPair(keyPath, bitSize) +} + +// GeneratePrivatePublicKeyPair generates a SSH key pair (used by the integration tests) +func GeneratePrivatePublicKeyPair(keyPath string, bitSize int) { + privateKeyPath := keyPath + publicKeyPath := fmt.Sprintf("%s.pub", keyPath) + + dlog.Common.Debug("Generating private/public key pair", privateKeyPath, publicKeyPath) + + privateKey, err := generatePrivateKey(bitSize) + if err != nil { + dlog.Common.FatalPanic(err) + } + publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey) + if err != nil { + dlog.Common.FatalPanic(err) + } + privateKeyBytes := encodePrivateKeyToPEM(privateKey) + err = writeKey(privateKeyBytes, privateKeyPath) + if err != nil { + dlog.Common.FatalPanic(err) + } + err = writeKey([]byte(publicKeyBytes), publicKeyPath) + if err != nil { + dlog.Common.FatalPanic(err) + } + + dlog.Common.Debug("Done generating private/public key pair", privateKeyPath, publicKeyPath) +} + +func generatePrivateKey(bitSize int) (*rsa.PrivateKey, error) { + privateKey, err := rsa.GenerateKey(rand.Reader, bitSize) + if err != nil { + return nil, err + } + err = privateKey.Validate() + if err != nil { + return nil, err + } + return privateKey, nil +} + +func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte { + privDER := x509.MarshalPKCS1PrivateKey(privateKey) + privBlock := pem.Block{ + Type: "RSA PRIVATE KEY", + Headers: nil, + Bytes: privDER, + } + privatePEM := pem.EncodeToMemory(&privBlock) + return privatePEM +} + +func generatePublicKey(privatekey *rsa.PublicKey) ([]byte, error) { + publicRsaKey, err := ssh.NewPublicKey(privatekey) + if err != nil { + return nil, err + } + pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey) + return pubKeyBytes, nil +} + +func writeKey(keyBytes []byte, saveFileTo string) error { + err := ioutil.WriteFile(saveFileTo, keyBytes, 0600) + if err != nil { + return err + } + return nil +} |
