@@ -251,15 +251,15 @@ func (sc *SSHConfig) loadIDs() (fileIDs, agentCertIDs, agentCfgIDs, agentOtherID
251251 if pub , err := loadPublicKey (f + ".pub" ); err == nil {
252252 // If .pub is a certificate, fingerprint its underlying key.
253253 if c , ok := pub .(* ssh.Certificate ); ok {
254- cfgFP [ssh . FingerprintSHA256 (c .Key )] = struct {}{}
254+ cfgFP [keyFP (c .Key )] = struct {}{}
255255 } else {
256- cfgFP [ssh . FingerprintSHA256 (pub )] = struct {}{}
256+ cfgFP [keyFP (pub )] = struct {}{}
257257 }
258258 }
259259 continue
260260 }
261261 fileIDs = append (fileIDs , identity {signer : s , path : f })
262- cfgFP [ssh . FingerprintSHA256 (s .PublicKey ())] = struct {}{}
262+ cfgFP [keyFP (s .PublicKey ())] = struct {}{}
263263 }
264264
265265 if agSigs , err := agent .GetSigners (); err != nil {
@@ -268,20 +268,20 @@ func (sc *SSHConfig) loadIDs() (fileIDs, agentCertIDs, agentCfgIDs, agentOtherID
268268 for _ , s := range agSigs {
269269 // Agent may return certificate identities (public key is a cert)
270270 if c , ok := s .PublicKey ().(* ssh.Certificate ); ok {
271- fp := ssh . FingerprintSHA256 (c .Key )
271+ fp := keyFP (c .Key )
272272 if _ , ok := cfgFP [fp ]; ok || ! sc .IdentitiesOnly {
273273 agentCertIDs = append (agentCertIDs , identity {signer : s })
274274 }
275275 continue
276276 }
277277
278278 id := identity {signer : s }
279- fp := ssh . FingerprintSHA256 (s .PublicKey ())
279+ fp := keyFP (s .PublicKey ())
280280 if _ , ok := cfgFP [fp ]; ok {
281281 agentCfgIDs = append (agentCfgIDs , id )
282282 // Remove id from fileIDs if existing
283283 for i , fid := range fileIDs {
284- if ssh . FingerprintSHA256 (fid .signer .PublicKey ()) == fp {
284+ if keyFP (fid .signer .PublicKey ()) == fp {
285285 fileIDs = append (fileIDs [:i ], fileIDs [i + 1 :]... )
286286 break
287287 }
@@ -356,23 +356,13 @@ func (sc *SSHConfig) makeSigners() ([]ssh.Signer, error) {
356356 return nil , fmt .Errorf ("%s: no key files found" , sc .Alias )
357357 }
358358
359+ sigs = dedupeSigners (sigs )
360+
359361 for _ , sig := range sigs {
360362 log .Debugf ("%s: will try key %s" , sc .Alias , sig )
361363 }
362364
363- // Dedupe
364- seen := make (map [string ]struct {}, len (sigs ))
365- out := make ([]ssh.Signer , 0 , len (sigs ))
366- for _ , s := range sigs {
367- fp := ssh .FingerprintSHA256 (s .PublicKey ())
368- if _ , ok := seen [fp ]; ok {
369- continue
370- }
371- seen [fp ] = struct {}{}
372- out = append (out , s )
373- }
374-
375- return out , nil
365+ return sigs , nil
376366}
377367
378368func (sc * SSHConfig ) makeCallbackAndAlgos () (cb ssh.HostKeyCallback , algs []string , err error ) {
@@ -477,7 +467,7 @@ func certify(cert *ssh.Certificate, sig ssh.Signer) (ssh.Signer, error) {
477467 if _ , ok := sig .PublicKey ().(* ssh.Certificate ); ok {
478468 return nil , fmt .Errorf ("signer is already a certificate identity" )
479469 }
480- if ssh . FingerprintSHA256 (sig .PublicKey ()) != ssh . FingerprintSHA256 (cert .Key ) {
470+ if keyFP (sig .PublicKey ()) != keyFP (cert .Key ) {
481471 return nil , fmt .Errorf ("signer does not match certificate key" )
482472 }
483473 certSig , err := ssh .NewCertSigner (cert , sig )
@@ -487,6 +477,20 @@ func certify(cert *ssh.Certificate, sig ssh.Signer) (ssh.Signer, error) {
487477 return certSig , nil
488478}
489479
480+ func dedupeSigners (sigs []ssh.Signer ) []ssh.Signer {
481+ seen := make (map [string ]struct {}, len (sigs ))
482+ out := make ([]ssh.Signer , 0 , len (sigs ))
483+ for _ , s := range sigs {
484+ fp := keyFP (s .PublicKey ())
485+ if _ , ok := seen [fp ]; ok {
486+ continue
487+ }
488+ seen [fp ] = struct {}{}
489+ out = append (out , s )
490+ }
491+ return out
492+ }
493+
490494func split (s string ) []string {
491495 return strings .Split (s , "," )
492496}
@@ -505,3 +509,9 @@ func filter(alist, allowed []string) []string {
505509 }
506510 return out
507511}
512+
513+ // keyFP returns a fingerprint string for a public key
514+ // we can make this more sophisticated later if needed
515+ func keyFP (k ssh.PublicKey ) string {
516+ return string (k .Marshal ())
517+ }
0 commit comments