Skip to content

Commit 2070685

Browse files
committed
fix: generate keys using ecdsa instead of rsa
fixes linear issue id ENG-689
1 parent b64ce55 commit 2070685

3 files changed

Lines changed: 40 additions & 18 deletions

File tree

pkg/devspace/services/proxycommands/commands.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package proxycommands
33
import (
44
"encoding/base64"
55
"fmt"
6+
"strings"
7+
68
sshpkg "github.com/gliderlabs/ssh"
79
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
810
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
@@ -13,7 +15,6 @@ import (
1315
"github.com/loft-sh/devspace/pkg/devspace/services/targetselector"
1416
"github.com/loft-sh/devspace/pkg/util/tomb"
1517
"github.com/pkg/errors"
16-
"strings"
1718
)
1819

1920
var DefaultRemotePort = 10567

pkg/devspace/services/ssh/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package ssh
22

33
import (
4-
"github.com/loft-sh/devspace/pkg/util/log"
5-
"github.com/loft-sh/devspace/pkg/util/scanner"
6-
"github.com/mitchellh/go-homedir"
7-
"github.com/pkg/errors"
84
"io"
95
"os"
106
"path/filepath"
117
"strconv"
128
"strings"
139
"sync"
10+
11+
"github.com/loft-sh/devspace/pkg/util/log"
12+
"github.com/loft-sh/devspace/pkg/util/scanner"
13+
"github.com/mitchellh/go-homedir"
14+
"github.com/pkg/errors"
1415
)
1516

1617
var configLock sync.Mutex

pkg/devspace/services/ssh/keys.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
package ssh
22

33
import (
4+
"crypto/ecdsa"
5+
"crypto/elliptic"
46
"crypto/rand"
5-
"crypto/rsa"
67
"crypto/x509"
78
"encoding/base64"
89
"encoding/pem"
9-
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
10-
"github.com/mitchellh/go-homedir"
11-
"github.com/pkg/errors"
12-
"golang.org/x/crypto/ssh"
1310
"os"
1411
"path/filepath"
1512
"strings"
1613
"sync"
14+
15+
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
16+
"github.com/mitchellh/go-homedir"
17+
"github.com/pkg/errors"
18+
"golang.org/x/crypto/ssh"
1719
)
1820

1921
var (
2022
DevSpaceSSHFolder = "ssh"
21-
DevSpaceSSHHostKeyFile = "id_devspace_host_rsa"
22-
DevSpaceSSHPrivateKeyFile = "id_devspace_rsa"
23-
DevSpaceSSHPublicKeyFile = "id_devspace_rsa.pub"
23+
DevSpaceSSHHostKeyFile = "id_devspace_host_ecdsa"
24+
DevSpaceSSHPrivateKeyFile = "id_devspace_ecdsa"
25+
DevSpaceSSHPublicKeyFile = "id_devspace_ecdsa.pub"
2426
)
2527

2628
func init() {
@@ -34,14 +36,18 @@ func init() {
3436
var keyLock sync.Mutex
3537

3638
func MakeHostKey() (string, error) {
37-
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
39+
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
3840
if err != nil {
3941
return "", err
4042
}
4143

4244
// generate and write private key as PEM
4345
var privKeyBuf strings.Builder
44-
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
46+
privateKeyPEM, err := pemBlock(privateKey)
47+
if err != nil {
48+
return "", err
49+
}
50+
4551
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil {
4652
return "", err
4753
}
@@ -50,14 +56,17 @@ func MakeHostKey() (string, error) {
5056
}
5157

5258
func MakeSSHKeyPair() (string, string, error) {
53-
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
59+
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
5460
if err != nil {
5561
return "", "", err
5662
}
57-
5863
// generate and write private key as PEM
5964
var privKeyBuf strings.Builder
60-
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
65+
privateKeyPEM, err := pemBlock(privateKey)
66+
if err != nil {
67+
return "", "", err
68+
}
69+
6170
if err := pem.Encode(&privKeyBuf, privateKeyPEM); err != nil {
6271
return "", "", err
6372
}
@@ -147,3 +156,14 @@ func getPublicKey() (string, error) {
147156

148157
return base64.StdEncoding.EncodeToString(out), nil
149158
}
159+
160+
func pemBlock(privateKey *ecdsa.PrivateKey) (*pem.Block, error) {
161+
if b, err := x509.MarshalPKCS8PrivateKey(privateKey); err == nil {
162+
return &pem.Block{
163+
Type: "PRIVATE KEY",
164+
Bytes: b,
165+
}, nil
166+
} else {
167+
return nil, err
168+
}
169+
}

0 commit comments

Comments
 (0)