Skip to content

Commit 08edff8

Browse files
committed
ssh: relax RSA signature check in SSH_MSG_USERAUTH_REQUEST
Buggy SSH clients, such as gpg-agent v2.2.4 and OpenSSH v7.6 shipped in Ubuntu 18.04, may send `ssh-rsa-512` as the public key algorithm but actually include an `rsa-sha` signature. If RFC 3808 (extension negotiation) is implemented, these clients will fail to authenticate with the error: ``` ssh: signature "ssh-rsa" came in for selected algorithm "rsa-sha2-512", public key is type ssh-rsa ``` According to RFC 8332 section 3.2: If the client includes the signature field, the client MUST encode the same algorithm name in the signature as in SSH_MSG_USERAUTH_REQUEST -- either "rsa-sha2-256" or "rsa-sha2-512". If a server receives a mismatching request, it MAY apply arbitrary authentication penalties, including but not limited to authentication failure or disconnect. ...A server MAY, but is not required to, accept this variant or another variant that corresponds to a good-faith implementation and is considered safe to accept. While the client is expected to do the right thing, in practice older clients may not fully support `ssh-rsa-256` and `ssh-rsa-512`. For example, gpg-agent v2.2.6 added support for these newer signature types. To accomodate these clients, relax the matching constraint: if the `SSH_MSG_USERAUTH_REQUEST` message specifies an RSA public key algorithm and includes an RSA public key, then allow any of the following signature types: - `rsa-sha-512` - `rsa-sha-256` - `rsa-sha` This emulates what OpenSSH does. OpenSSH only considers that the RSA family is specified and then verifies if the signature and public key match. Closes golang/go#53391
1 parent a3485e1 commit 08edff8

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

ssh/server.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,30 @@ func (l ServerAuthError) Error() string {
404404
// It is returned in ServerAuthError.Errors from NewServerConn.
405405
var ErrNoAuth = errors.New("ssh: no auth passed yet")
406406

407+
func isRSAType(algo string) bool {
408+
return algo == KeyAlgoRSASHA512 || algo == KeyAlgoRSASHA256 || algo == KeyAlgoRSA
409+
}
410+
411+
func isAlgoCompatible(algo string, pubKeyFormat string, sigFormat string) bool {
412+
algo = underlyingAlgo(algo)
413+
if algo == sigFormat {
414+
return true
415+
}
416+
417+
// Buggy SSH clients may send ssh-rsa2-512 as the public key algorithm but
418+
// actually include a rsa-sha signature.
419+
// According to RFC 8332 Section 3.2:
420+
// A server MAY, but is not required to, accept this variant or another variant that
421+
// corresponds to a good-faith implementation and is considered safe to
422+
// accept.
423+
compatibleAlgos := algorithmsForKeyFormat(pubKeyFormat)
424+
if contains(compatibleAlgos, algo) && contains(compatibleAlgos, sigFormat) {
425+
return true
426+
}
427+
428+
return false
429+
}
430+
407431
func (s *connection) serverAuthenticate(config *ServerConfig) (*Permissions, error) {
408432
sessionID := s.transport.getSessionID()
409433
var cache pubKeyCache
@@ -576,7 +600,7 @@ userAuthLoop:
576600
authErr = fmt.Errorf("ssh: algorithm %q not accepted", sig.Format)
577601
break
578602
}
579-
if underlyingAlgo(algo) != sig.Format {
603+
if !isAlgoCompatible(algo, pubKey.Type(), sig.Format) {
580604
authErr = fmt.Errorf("ssh: signature %q not compatible with selected algorithm %q", sig.Format, algo)
581605
break
582606
}

0 commit comments

Comments
 (0)