Skip to content

Commit

Permalink
manual backport
Browse files Browse the repository at this point in the history
  • Loading branch information
loshz committed Jan 10, 2024
1 parent cb99e33 commit a0ebdd1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/20112.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:security
Update RSA key generation to use a key size of at least 2048 bits.
```
40 changes: 40 additions & 0 deletions agent/auto-config/auto_encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -107,6 +108,45 @@ func TestAutoEncrypt_generateCSR(t *testing.T) {
}
}

func TestAutoEncrypt_generateCSR_RSA(t *testing.T) {
testCases := []struct {
name string
keySize int
expectedKeySize int
}{
{
name: "DefaultKeySize",
keySize: 0,
expectedKeySize: 4096,
},
{
name: "KeySize2048",
keySize: 2048,
expectedKeySize: 2048,
},
}

for _, tcase := range testCases {
t.Run(tcase.name, func(t *testing.T) {
ac := AutoConfig{config: &config.RuntimeConfig{
ConnectCAConfig: map[string]interface{}{
"PrivateKeyType": "rsa",
"PrivateKeyBits": tcase.keySize,
},
}}

// Generate a private RSA key.
_, key, err := ac.generateCSR()
require.NoError(t, err)

// Parse the private key and check it's length.
pemBlock, _ := pem.Decode([]byte(key))
priv, _ := x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
require.Equal(t, tcase.expectedKeySize, priv.N.BitLen())
})
}
}

func TestAutoEncrypt_hosts(t *testing.T) {
type testCase struct {
serverProvider ServerProvider
Expand Down
4 changes: 2 additions & 2 deletions agent/connect/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func generateECDSAKey(keyBits int) (crypto.Signer, string, error) {
// GeneratePrivateKey generates a new Private key
func GeneratePrivateKeyWithConfig(keyType string, keyBits int) (crypto.Signer, string, error) {
switch strings.ToLower(keyType) {
case "rsa":
case PrivateKeyTypeRSA:
return generateRSAKey(keyBits)
case "ec":
case DefaultPrivateKeyType:
return generateECDSAKey(keyBits)
default:
return nil, "", fmt.Errorf("unknown private key type requested: %s", keyType)
Expand Down

0 comments on commit a0ebdd1

Please sign in to comment.