Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh: Use ed25519 algorithm instead ECDSA #4304

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pkg/crc/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,20 @@ func EnsureBaseDirectoriesExist() error {
}

func GetPublicKeyPath() string {
return filepath.Join(MachineInstanceDir, DefaultName, "id_ecdsa.pub")
return filepath.Join(MachineInstanceDir, DefaultName, "id_ed25519.pub")
}

func GetPrivateKeyPath() string {
return filepath.Join(MachineInstanceDir, DefaultName, "id_ecdsa")
return filepath.Join(MachineInstanceDir, DefaultName, "id_ed25519")
}

func GetHostDockerSocketPath() string {
return filepath.Join(MachineInstanceDir, DefaultName, "docker.sock")
}

// For backward compatibility to v 1.20.0
func GetRsaPrivateKeyPath() string {
return filepath.Join(MachineInstanceDir, DefaultName, "id_rsa")
// For backward compatibility to v 2.40.0
func GetECDSAPrivateKeyPath() string {
return filepath.Join(MachineInstanceDir, DefaultName, "id_ecdsa")
}

func GetKubeAdminPasswordPath() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/machine/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ func (client *client) ConnectionDetails() (*types.ConnectionDetails, error) {
IP: ip,
SSHPort: vm.SSHPort(),
SSHUsername: constants.DefaultSSHUser,
SSHKeys: []string{constants.GetPrivateKeyPath(), constants.GetRsaPrivateKeyPath(), vm.bundle.GetSSHKeyPath()},
SSHKeys: []string{constants.GetPrivateKeyPath(), constants.GetECDSAPrivateKeyPath(), vm.bundle.GetSSHKeyPath()},
}, nil
}
2 changes: 1 addition & 1 deletion pkg/crc/machine/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ func (vm *virtualMachine) SSHRunner() (*ssh.Runner, error) {
if err != nil {
return nil, err
}
return ssh.CreateRunner(ip, vm.SSHPort(), constants.GetPrivateKeyPath(), constants.GetRsaPrivateKeyPath(), vm.bundle.GetSSHKeyPath())
return ssh.CreateRunner(ip, vm.SSHPort(), constants.GetPrivateKeyPath(), constants.GetECDSAPrivateKeyPath(), vm.bundle.GetSSHKeyPath())
}
1 change: 1 addition & 0 deletions pkg/crc/ssh/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func clientConfig(user string, keys []string) (*ssh.ClientConfig, error) {

privateKey, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Debugf("Failed to parse private key: %v\n", err)
return nil, err
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/crc/ssh/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ssh
import (
"bufio"
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto"
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"os"
Expand All @@ -33,23 +33,23 @@ type KeyPair struct {
// This will return a private & public key encoded as DER.
func NewKeyPair() (keyPair *KeyPair, err error) {

priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
pub, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, ErrKeyGeneration
}

privDer, err := x509.MarshalPKCS8PrivateKey(priv)
privMar, err := gossh.MarshalPrivateKey(crypto.PrivateKey(priv), "")
if err != nil {
return nil, ErrPrivateKey
}

pubSSH, err := gossh.NewPublicKey(&priv.PublicKey)
pubSSH, err := gossh.NewPublicKey(pub)
if err != nil {
return nil, ErrPublicKey
}

return &KeyPair{
PrivateKey: privDer,
PrivateKey: pem.EncodeToMemory(privMar),
PublicKey: gossh.MarshalAuthorizedKey(pubSSH),
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/ssh/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestNewKeyPair(t *testing.T) {
t.Fatal(err)
}

if privPem := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Headers: nil, Bytes: pair.PrivateKey}); len(privPem) == 0 {
if privPem := pem.EncodeToMemory(&pem.Block{Type: "OPENSSH PRIVATE KEY", Headers: nil, Bytes: pair.PrivateKey}); len(privPem) == 0 {
t.Fatal("No PEM returned")
}
}
3 changes: 1 addition & 2 deletions pkg/crc/ssh/keys_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package ssh

import (
"encoding/pem"
"os"
)

Expand All @@ -17,7 +16,7 @@ func (kp *KeyPair) WriteToFile(privateKeyPath string, publicKeyPath string) erro
}{
{
File: privateKeyPath,
Value: pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Headers: nil, Bytes: kp.PrivateKey}),
Value: kp.PrivateKey,
},
{
File: publicKeyPath,
Expand Down
3 changes: 1 addition & 2 deletions pkg/crc/ssh/keys_windows.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ssh

import (
"encoding/pem"
"os"

"github.com/hectane/go-acl"
Expand Down Expand Up @@ -35,7 +34,7 @@ func (kp *KeyPair) WriteToFile(privateKeyPath string, publicKeyPath string) erro
}{
{
File: privateKeyPath,
Value: pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Headers: nil, Bytes: kp.PrivateKey}),
Value: kp.PrivateKey,
},
{
File: publicKeyPath,
Expand Down