@@ -13,12 +13,9 @@ import (
13
13
"errors"
14
14
"fmt"
15
15
"math/big"
16
- "os"
17
- "strconv"
18
16
"strings"
19
17
20
18
"code.gitea.io/gitea/modules/log"
21
- "code.gitea.io/gitea/modules/process"
22
19
"code.gitea.io/gitea/modules/setting"
23
20
"code.gitea.io/gitea/modules/util"
24
21
@@ -175,20 +172,9 @@ func CheckPublicKeyString(content string) (_ string, err error) {
175
172
return content , nil
176
173
}
177
174
178
- var (
179
- fnName string
180
- keyType string
181
- length int
182
- )
183
- if len (setting .SSH .KeygenPath ) == 0 {
184
- fnName = "SSHNativeParsePublicKey"
185
- keyType , length , err = SSHNativeParsePublicKey (content )
186
- } else {
187
- fnName = "SSHKeyGenParsePublicKey"
188
- keyType , length , err = SSHKeyGenParsePublicKey (content )
189
- }
175
+ keyType , length , err := SSHNativeParsePublicKey (content )
190
176
if err != nil {
191
- return "" , fmt .Errorf ("%s : %w" , fnName , err )
177
+ return "" , fmt .Errorf ("SSHNativeParsePublicKey : %w" , err )
192
178
}
193
179
log .Trace ("Key info [native: %v]: %s-%d" , setting .SSH .StartBuiltinServer , keyType , length )
194
180
@@ -258,56 +244,3 @@ func SSHNativeParsePublicKey(keyLine string) (string, int, error) {
258
244
}
259
245
return "" , 0 , fmt .Errorf ("unsupported key length detection for type: %s" , pkey .Type ())
260
246
}
261
-
262
- // writeTmpKeyFile writes key content to a temporary file
263
- // and returns the name of that file, along with any possible errors.
264
- func writeTmpKeyFile (content string ) (string , error ) {
265
- tmpFile , err := os .CreateTemp (setting .GetSSHKeyTestPath (), "gitea_keytest" )
266
- if err != nil {
267
- return "" , fmt .Errorf ("TempFile: %w" , err )
268
- }
269
- defer tmpFile .Close ()
270
-
271
- if _ , err = tmpFile .WriteString (content ); err != nil {
272
- return "" , fmt .Errorf ("WriteString: %w" , err )
273
- }
274
- return tmpFile .Name (), nil
275
- }
276
-
277
- // SSHKeyGenParsePublicKey extracts key type and length using ssh-keygen.
278
- func SSHKeyGenParsePublicKey (key string ) (string , int , error ) {
279
- tmpName , err := writeTmpKeyFile (key )
280
- if err != nil {
281
- return "" , 0 , fmt .Errorf ("writeTmpKeyFile: %w" , err )
282
- }
283
- defer func () {
284
- if err := util .Remove (tmpName ); err != nil {
285
- log .Warn ("Unable to remove temporary key file: %s: Error: %v" , tmpName , err )
286
- }
287
- }()
288
-
289
- keygenPath := setting .SSH .KeygenPath
290
- if len (keygenPath ) == 0 {
291
- keygenPath = "ssh-keygen"
292
- }
293
-
294
- stdout , stderr , err := process .GetManager ().Exec ("SSHKeyGenParsePublicKey" , keygenPath , "-lf" , tmpName )
295
- if err != nil {
296
- return "" , 0 , fmt .Errorf ("fail to parse public key: %s - %s" , err , stderr )
297
- }
298
- if strings .Contains (stdout , "is not a public key file" ) {
299
- return "" , 0 , ErrKeyUnableVerify {stdout }
300
- }
301
-
302
- fields := strings .Split (stdout , " " )
303
- if len (fields ) < 4 {
304
- return "" , 0 , fmt .Errorf ("invalid public key line: %s" , stdout )
305
- }
306
-
307
- keyType := strings .Trim (fields [len (fields )- 1 ], "()\r \n " )
308
- length , err := strconv .ParseInt (fields [0 ], 10 , 32 )
309
- if err != nil {
310
- return "" , 0 , err
311
- }
312
- return strings .ToLower (keyType ), int (length ), nil
313
- }
0 commit comments