@@ -89,6 +89,11 @@ func parsePubKey(in []byte, algo string) (pubKey PublicKey, rest []byte, err err
8989 }
9090 return cert , nil , nil
9191 }
92+ if keyFormat := keyFormatForAlgorithm (algo ); keyFormat != "" {
93+ return nil , nil , fmt .Errorf ("ssh: signature algorithm %q isn't a key format; key is malformed and should be re-encoded with type %q" ,
94+ algo , keyFormat )
95+ }
96+
9297 return nil , nil , fmt .Errorf ("ssh: unknown key algorithm: %v" , algo )
9398}
9499
@@ -191,9 +196,10 @@ func ParseKnownHosts(in []byte) (marker string, hosts []string, pubKey PublicKey
191196 return "" , nil , nil , "" , nil , io .EOF
192197}
193198
194- // ParseAuthorizedKey parses a public key from an authorized_keys
195- // file used in OpenSSH according to the sshd(8) manual page.
199+ // ParseAuthorizedKey parses a public key from an authorized_keys file used in
200+ // OpenSSH according to the sshd(8) manual page. Invalid lines are ignored .
196201func ParseAuthorizedKey (in []byte ) (out PublicKey , comment string , options []string , rest []byte , err error ) {
202+ var lastErr error
197203 for len (in ) > 0 {
198204 end := bytes .IndexByte (in , '\n' )
199205 if end != - 1 {
@@ -222,6 +228,8 @@ func ParseAuthorizedKey(in []byte) (out PublicKey, comment string, options []str
222228
223229 if out , comment , err = parseAuthorizedKey (in [i :]); err == nil {
224230 return out , comment , options , rest , nil
231+ } else {
232+ lastErr = err
225233 }
226234
227235 // No key type recognised. Maybe there's an options field at
@@ -264,12 +272,18 @@ func ParseAuthorizedKey(in []byte) (out PublicKey, comment string, options []str
264272 if out , comment , err = parseAuthorizedKey (in [i :]); err == nil {
265273 options = candidateOptions
266274 return out , comment , options , rest , nil
275+ } else {
276+ lastErr = err
267277 }
268278
269279 in = rest
270280 continue
271281 }
272282
283+ if lastErr != nil {
284+ return nil , "" , nil , nil , fmt .Errorf ("ssh: no key found; last parsing error for ignored line: %w" , lastErr )
285+ }
286+
273287 return nil , "" , nil , nil , errors .New ("ssh: no key found" )
274288}
275289
0 commit comments