From 443b8cb475cc16514b864a61d02fef52fbb2ae2d Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 15 Jun 2022 13:54:50 -0700 Subject: [PATCH] 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 https://github.com/golang/go/issues/53391 --- ssh/server.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ssh/server.go b/ssh/server.go index 70045bdfd8..8a3f14ecd1 100644 --- a/ssh/server.go +++ b/ssh/server.go @@ -396,6 +396,28 @@ func (l ServerAuthError) Error() string { // It is returned in ServerAuthError.Errors from NewServerConn. var ErrNoAuth = errors.New("ssh: no auth passed yet") +func isRSAType(algo string) bool { + return algo == KeyAlgoRSASHA512 || algo == KeyAlgoRSASHA256 || algo == KeyAlgoRSA +} + +func algoCompatible(algo string, publicKey PublicKey, sig *Signature) bool { + if underlyingAlgo(algo) == sig.Format { + return true + } + + // Buggy SSH clients may send ssh-rsa2-512 as the public key algorithm but + // actually include a rsa-sha signature. + // According to RFC 8832 Section 3.2: + // 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. + if publicKey.Type() == KeyAlgoRSA && isRSAType(algo) && isRSAType(sig.Format) { + return true + } + + return false +} + func (s *connection) serverAuthenticate(config *ServerConfig) (*Permissions, error) { sessionID := s.transport.getSessionID() var cache pubKeyCache @@ -564,7 +586,7 @@ userAuthLoop: authErr = fmt.Errorf("ssh: algorithm %q not accepted", sig.Format) break } - if underlyingAlgo(algo) != sig.Format { + if !algoCompatible(algo, pubKey, sig) { authErr = fmt.Errorf("ssh: signature %q not compatible with selected algorithm %q", sig.Format, algo) break }