@@ -3,12 +3,9 @@ package agentssh
3
3
import (
4
4
"bufio"
5
5
"context"
6
- "crypto/rsa"
7
6
"errors"
8
7
"fmt"
9
8
"io"
10
- "math/big"
11
- "math/rand"
12
9
"net"
13
10
"os"
14
11
"os/exec"
@@ -33,6 +30,7 @@ import (
33
30
"cdr.dev/slog"
34
31
35
32
"github.com/coder/coder/v2/agent/agentexec"
33
+ "github.com/coder/coder/v2/agent/agentssh/agentsshrsa"
36
34
"github.com/coder/coder/v2/agent/usershell"
37
35
"github.com/coder/coder/v2/codersdk"
38
36
"github.com/coder/coder/v2/pty"
@@ -1120,75 +1118,7 @@ func CoderSigner(seed int64) (gossh.Signer, error) {
1120
1118
// Clients should ignore the host key when connecting.
1121
1119
// The agent needs to authenticate with coderd to SSH,
1122
1120
// so SSH authentication doesn't improve security.
1123
-
1124
- // Since the standard lib purposefully does not generate
1125
- // deterministic rsa keys, we need to do it ourselves.
1126
- coderHostKey := func () * rsa.PrivateKey {
1127
- // Create deterministic random source
1128
- // nolint: gosec
1129
- deterministicRand := rand .New (rand .NewSource (seed ))
1130
-
1131
- // Use fixed values for p and q based on the seed
1132
- p := big .NewInt (0 )
1133
- q := big .NewInt (0 )
1134
- e := big .NewInt (65537 ) // Standard RSA public exponent
1135
-
1136
- // Generate deterministic primes using the seeded random
1137
- // Each prime should be ~1024 bits to get a 2048-bit key
1138
- for {
1139
- p .SetBit (p , 1024 , 1 ) // Ensure it's large enough
1140
- for i := 0 ; i < 1024 ; i ++ {
1141
- if deterministicRand .Int63 ()% 2 == 1 {
1142
- p .SetBit (p , i , 1 )
1143
- } else {
1144
- p .SetBit (p , i , 0 )
1145
- }
1146
- }
1147
- if p .ProbablyPrime (20 ) {
1148
- break
1149
- }
1150
- }
1151
-
1152
- for {
1153
- q .SetBit (q , 1024 , 1 ) // Ensure it's large enough
1154
- for i := 0 ; i < 1024 ; i ++ {
1155
- if deterministicRand .Int63 ()% 2 == 1 {
1156
- q .SetBit (q , i , 1 )
1157
- } else {
1158
- q .SetBit (q , i , 0 )
1159
- }
1160
- }
1161
- if q .ProbablyPrime (20 ) && p .Cmp (q ) != 0 {
1162
- break
1163
- }
1164
- }
1165
-
1166
- // Calculate n = p * q
1167
- n := new (big.Int ).Mul (p , q )
1168
-
1169
- // Calculate phi = (p-1) * (q-1)
1170
- p1 := new (big.Int ).Sub (p , big .NewInt (1 ))
1171
- q1 := new (big.Int ).Sub (q , big .NewInt (1 ))
1172
- phi := new (big.Int ).Mul (p1 , q1 )
1173
-
1174
- // Calculate private exponent d
1175
- d := new (big.Int ).ModInverse (e , phi )
1176
-
1177
- // Create the private key
1178
- privateKey := & rsa.PrivateKey {
1179
- PublicKey : rsa.PublicKey {
1180
- N : n ,
1181
- E : int (e .Int64 ()),
1182
- },
1183
- D : d ,
1184
- Primes : []* big.Int {p , q },
1185
- }
1186
-
1187
- // Compute precomputed values
1188
- privateKey .Precompute ()
1189
-
1190
- return privateKey
1191
- }()
1121
+ coderHostKey := agentsshrsa .DeterministicPrivateKey (seed )
1192
1122
1193
1123
coderSigner , err := gossh .NewSignerFromKey (coderHostKey )
1194
1124
return coderSigner , err
0 commit comments