- Notifications
You must be signed in to change notification settings - Fork913
fix(agent/agentssh): ensure RSA key generation always produces valid keys#16694
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
ThomasK33 merged 1 commit intomainfromthomask33/02-25-fix_agentssh_ensure_rsa_key_generation_always_produces_valid_keysFeb 26, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletionsagent/agentrsa/key.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package agentrsa | ||
import ( | ||
"crypto/rsa" | ||
"math/big" | ||
"math/rand" | ||
) | ||
// GenerateDeterministicKey generates an RSA private key deterministically based on the provided seed. | ||
// This function uses a deterministic random source to generate the primes p and q, ensuring that the | ||
// same seed will always produce the same private key. The generated key is 2048 bits in size. | ||
// | ||
// Reference: https://pkg.go.dev/crypto/rsa#GenerateKey | ||
func GenerateDeterministicKey(seed int64) *rsa.PrivateKey { | ||
// Since the standard lib purposefully does not generate | ||
// deterministic rsa keys, we need to do it ourselves. | ||
// Create deterministic random source | ||
// nolint: gosec | ||
deterministicRand := rand.New(rand.NewSource(seed)) | ||
// Use fixed values for p and q based on the seed | ||
p := big.NewInt(0) | ||
q := big.NewInt(0) | ||
e := big.NewInt(65537) // Standard RSA public exponent | ||
for { | ||
// Generate deterministic primes using the seeded random | ||
// Each prime should be ~1024 bits to get a 2048-bit key | ||
for { | ||
p.SetBit(p, 1024, 1) // Ensure it's large enough | ||
for i := range 1024 { | ||
if deterministicRand.Int63()%2 == 1 { | ||
p.SetBit(p, i, 1) | ||
} else { | ||
p.SetBit(p, i, 0) | ||
} | ||
} | ||
p1 := new(big.Int).Sub(p, big.NewInt(1)) | ||
if p.ProbablyPrime(20) && new(big.Int).GCD(nil, nil, e, p1).Cmp(big.NewInt(1)) == 0 { | ||
break | ||
} | ||
} | ||
for { | ||
q.SetBit(q, 1024, 1) // Ensure it's large enough | ||
for i := range 1024 { | ||
if deterministicRand.Int63()%2 == 1 { | ||
q.SetBit(q, i, 1) | ||
} else { | ||
q.SetBit(q, i, 0) | ||
} | ||
} | ||
q1 := new(big.Int).Sub(q, big.NewInt(1)) | ||
if q.ProbablyPrime(20) && p.Cmp(q) != 0 && new(big.Int).GCD(nil, nil, e, q1).Cmp(big.NewInt(1)) == 0 { | ||
break | ||
} | ||
} | ||
// Calculate phi = (p-1) * (q-1) | ||
p1 := new(big.Int).Sub(p, big.NewInt(1)) | ||
q1 := new(big.Int).Sub(q, big.NewInt(1)) | ||
phi := new(big.Int).Mul(p1, q1) | ||
// Calculate private exponent d | ||
d := new(big.Int).ModInverse(e, phi) | ||
if d != nil { | ||
// Calculate n = p * q | ||
n := new(big.Int).Mul(p, q) | ||
// Create the private key | ||
privateKey := &rsa.PrivateKey{ | ||
PublicKey: rsa.PublicKey{ | ||
N: n, | ||
E: int(e.Int64()), | ||
}, | ||
D: d, | ||
Primes: []*big.Int{p, q}, | ||
} | ||
// Compute precomputed values | ||
privateKey.Precompute() | ||
return privateKey | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletionsagent/agentrsa/key_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package agentrsa_test | ||
import ( | ||
"crypto/rsa" | ||
"math/rand/v2" | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/coder/coder/v2/agent/agentrsa" | ||
) | ||
func TestGenerateDeterministicKey(t *testing.T) { | ||
t.Parallel() | ||
key1 := agentrsa.GenerateDeterministicKey(1234) | ||
key2 := agentrsa.GenerateDeterministicKey(1234) | ||
assert.Equal(t, key1, key2) | ||
assert.EqualExportedValues(t, key1, key2) | ||
} | ||
var result *rsa.PrivateKey | ||
func BenchmarkGenerateDeterministicKey(b *testing.B) { | ||
var r *rsa.PrivateKey | ||
for range b.N { | ||
// always record the result of DeterministicPrivateKey to prevent | ||
// the compiler eliminating the function call. | ||
r = agentrsa.GenerateDeterministicKey(rand.Int64()) | ||
} | ||
// always store the result to a package level variable | ||
// so the compiler cannot eliminate the Benchmark itself. | ||
result = r | ||
} | ||
func FuzzGenerateDeterministicKey(f *testing.F) { | ||
testcases := []int64{0, 1234, 1010101010} | ||
for _, tc := range testcases { | ||
f.Add(tc) // Use f.Add to provide a seed corpus | ||
} | ||
f.Fuzz(func(t *testing.T, seed int64) { | ||
key1 := agentrsa.GenerateDeterministicKey(seed) | ||
key2 := agentrsa.GenerateDeterministicKey(seed) | ||
assert.Equal(t, key1, key2) | ||
assert.EqualExportedValues(t, key1, key2) | ||
}) | ||
} |
74 changes: 2 additions & 72 deletionsagent/agentssh/agentssh.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.