@@ -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/agentrsa"
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"
@@ -1092,75 +1090,7 @@ func CoderSigner(seed int64) (gossh.Signer, error) {
1092
1090
// Clients should ignore the host key when connecting.
1093
1091
// The agent needs to authenticate with coderd to SSH,
1094
1092
// so SSH authentication doesn't improve security.
1095
-
1096
- // Since the standard lib purposefully does not generate
1097
- // deterministic rsa keys, we need to do it ourselves.
1098
- coderHostKey := func () * rsa.PrivateKey {
1099
- // Create deterministic random source
1100
- // nolint: gosec
1101
- deterministicRand := rand .New (rand .NewSource (seed ))
1102
-
1103
- // Use fixed values for p and q based on the seed
1104
- p := big .NewInt (0 )
1105
- q := big .NewInt (0 )
1106
- e := big .NewInt (65537 ) // Standard RSA public exponent
1107
-
1108
- // Generate deterministic primes using the seeded random
1109
- // Each prime should be ~1024 bits to get a 2048-bit key
1110
- for {
1111
- p .SetBit (p , 1024 , 1 ) // Ensure it's large enough
1112
- for i := 0 ; i < 1024 ; i ++ {
1113
- if deterministicRand .Int63 ()% 2 == 1 {
1114
- p .SetBit (p , i , 1 )
1115
- } else {
1116
- p .SetBit (p , i , 0 )
1117
- }
1118
- }
1119
- if p .ProbablyPrime (20 ) {
1120
- break
1121
- }
1122
- }
1123
-
1124
- for {
1125
- q .SetBit (q , 1024 , 1 ) // Ensure it's large enough
1126
- for i := 0 ; i < 1024 ; i ++ {
1127
- if deterministicRand .Int63 ()% 2 == 1 {
1128
- q .SetBit (q , i , 1 )
1129
- } else {
1130
- q .SetBit (q , i , 0 )
1131
- }
1132
- }
1133
- if q .ProbablyPrime (20 ) && p .Cmp (q ) != 0 {
1134
- break
1135
- }
1136
- }
1137
-
1138
- // Calculate n = p * q
1139
- n := new (big.Int ).Mul (p , q )
1140
-
1141
- // Calculate phi = (p-1) * (q-1)
1142
- p1 := new (big.Int ).Sub (p , big .NewInt (1 ))
1143
- q1 := new (big.Int ).Sub (q , big .NewInt (1 ))
1144
- phi := new (big.Int ).Mul (p1 , q1 )
1145
-
1146
- // Calculate private exponent d
1147
- d := new (big.Int ).ModInverse (e , phi )
1148
-
1149
- // Create the private key
1150
- privateKey := & rsa.PrivateKey {
1151
- PublicKey : rsa.PublicKey {
1152
- N : n ,
1153
- E : int (e .Int64 ()),
1154
- },
1155
- D : d ,
1156
- Primes : []* big.Int {p , q },
1157
- }
1158
-
1159
- // Compute precomputed values
1160
- privateKey .Precompute ()
1161
-
1162
- return privateKey
1163
- }()
1093
+ coderHostKey := agentrsa .GenerateDeterministicKey (seed )
1164
1094
1165
1095
coderSigner , err := gossh .NewSignerFromKey (coderHostKey )
1166
1096
return coderSigner , err
0 commit comments