@@ -6,27 +6,13 @@ package asymkey
66import (
77 "context"
88 "fmt"
9- "strings"
109
1110 "code.gitea.io/gitea/models/db"
12- "code.gitea.io/gitea/modules/log"
13- "code.gitea.io/gitea/modules/process"
14- "code.gitea.io/gitea/modules/setting"
15- "code.gitea.io/gitea/modules/util"
1611
1712 "golang.org/x/crypto/ssh"
1813 "xorm.io/builder"
1914)
2015
21- // ___________.__ .__ __
22- // \_ _____/|__| ____ ____ ________________________|__| _____/ |_
23- // | __) | |/ \ / ___\_/ __ \_ __ \____ \_ __ \ |/ \ __\
24- // | \ | | | \/ /_/ > ___/| | \/ |_> > | \/ | | \ |
25- // \___ / |__|___| /\___ / \___ >__| | __/|__| |__|___| /__|
26- // \/ \//_____/ \/ |__| \/
27- //
28- // This file contains functions for fingerprinting SSH keys
29- //
3016// The database is used in checkKeyFingerprint however most of these functions probably belong in a module
3117
3218// checkKeyFingerprint only checks if key fingerprint has been used as public key,
@@ -41,29 +27,6 @@ func checkKeyFingerprint(ctx context.Context, fingerprint string) error {
4127 return nil
4228}
4329
44- func calcFingerprintSSHKeygen (publicKeyContent string ) (string , error ) {
45- // Calculate fingerprint.
46- tmpPath , err := writeTmpKeyFile (publicKeyContent )
47- if err != nil {
48- return "" , err
49- }
50- defer func () {
51- if err := util .Remove (tmpPath ); err != nil {
52- log .Warn ("Unable to remove temporary key file: %s: Error: %v" , tmpPath , err )
53- }
54- }()
55- stdout , stderr , err := process .GetManager ().Exec ("AddPublicKey" , "ssh-keygen" , "-lf" , tmpPath )
56- if err != nil {
57- if strings .Contains (stderr , "is not a public key file" ) {
58- return "" , ErrKeyUnableVerify {stderr }
59- }
60- return "" , util .NewInvalidArgumentErrorf ("'ssh-keygen -lf %s' failed with error '%s': %s" , tmpPath , err , stderr )
61- } else if len (stdout ) < 2 {
62- return "" , util .NewInvalidArgumentErrorf ("not enough output for calculating fingerprint: %s" , stdout )
63- }
64- return strings .Split (stdout , " " )[1 ], nil
65- }
66-
6730func calcFingerprintNative (publicKeyContent string ) (string , error ) {
6831 // Calculate fingerprint.
6932 pk , _ , _ , _ , err := ssh .ParseAuthorizedKey ([]byte (publicKeyContent ))
@@ -75,15 +38,12 @@ func calcFingerprintNative(publicKeyContent string) (string, error) {
7538
7639// CalcFingerprint calculate public key's fingerprint
7740func CalcFingerprint (publicKeyContent string ) (string , error ) {
78- // Call the method based on configuration
79- useNative := setting .SSH .KeygenPath == ""
80- calcFn := util .Iif (useNative , calcFingerprintNative , calcFingerprintSSHKeygen )
81- fp , err := calcFn (publicKeyContent )
41+ fp , err := calcFingerprintNative (publicKeyContent )
8242 if err != nil {
8343 if IsErrKeyUnableVerify (err ) {
8444 return "" , err
8545 }
86- return "" , fmt .Errorf ("CalcFingerprint(%s) : %w" , util . Iif ( useNative , "native" , "ssh-keygen" ) , err )
46+ return "" , fmt .Errorf ("CalcFingerprint: %w" , err )
8747 }
8848 return fp , nil
8949}
0 commit comments