Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supplied key (org.apache.sshd.common.config.keys.OpenSshCertificateImpl) is not a RSAPublicKey instance #650

Open
liuziyu1226 opened this issue Dec 27, 2024 · 1 comment

Comments

@liuziyu1226
Copy link

liuziyu1226 commented Dec 27, 2024

Version

2.8.0

Bug description

I overwrote the authenticate function of the PublickeyAuthenticator.java and registered it with the ssh server.

sshServer.setPublickeyAuthenticator(publicKeyAuth);

But when the client provides the key and certificate, MINA SSHD displays an error:

2024-12-25 17:29:30.882 sshd-SshServer[53600a30](port=2222)-nio2-thread-5 WARN  org.apache.sshd.server.session.ServerUserAuthService.warn - handleUserAuthRequestMessage(ServerSessionImpl[null@/127.0.0.1:56562]) Failed (InvalidKeyException) to authenticate using factory method=publickey: Supplied key (org.apache.sshd.common.config.keys.OpenSshCertificateImpl) is not a RSAPublicKey instance

After debugging, I found that the problem is that when MINA SSHD initializes the signature verifier of the public key, the public key passed in is of type OpenSshCertificateImpl, while Signature expects an RSAPublicKey object. Why does this problem occur? How can I solve it?

Actual behavior

I rewrote the initVerifier function in SignatureRSA.java and now I can login correctly.
Old initVerifier function:

public void initVerifier(SessionContext session, PublicKey key) throws Exception {
      super.initVerifier(session, key);
      RSAKey rsaKey = ValidateUtils.checkInstanceOf(key, RSAKey.class, "Not an RSA key");
      verifierSignatureSize = getVerifierSignatureSize(rsaKey);
}

I overwrote the initVerifier function:

public void initVerifier(SessionContext session, PublicKey key) throws Exception {
      if (key instanceof OpenSshCertificate){
          super.initVerifier(session, ((OpenSshCertificate) key).getCertPubKey());
          RSAKey rsaKey = ValidateUtils.checkInstanceOf(((OpenSshCertificate) key).getCertPubKey(), RSAKey.class, "Not an RSA key");
          verifierSignatureSize = getVerifierSignatureSize(rsaKey);
      }else {
          super.initVerifier(session, key);
          RSAKey rsaKey = ValidateUtils.checkInstanceOf(key, RSAKey.class, "Not an RSA key");
          verifierSignatureSize = getVerifierSignatureSize(rsaKey);
      }
  }

I am still curious as to why this happens? And whether there are any hidden dangers in my modification.
`

Relevant log output

No response

Other information

No response

@tomaswolf
Copy link
Member

The server side of user authentication with OpenSshCertificates was not fully implemented, and apparently is lacking tests.

See issue SSHD-1161.

The real cause of the problem you encounter is in

and

If the key is an OpenSshCertificate, initVerifier should be called with key.getCertPubKey(), not with key.

So, to answer your questions: it happens because there is a bug. There are no dangers to your modification, but it'll work only for RSA keys. The real fix has to be in UserAuthPublicKey (server side) so that it works for all key types. Similar to how it's done in

if (serverKey instanceof OpenSshCertificate) {
OpenSshCertificate openSshKey = (OpenSshCertificate) serverKey;
serverPublicHostKey = openSshKey.getCertPubKey();
and
verif.initVerifier(session, serverPublicHostKey);
Plus there needs to be a test for this.

Note that there are other bits of authenticating with a certificate missing on the server side:

// TODO: cert.getCaKey() must be either in authorized_keys, marked as a CA key
// and not revoked, or in TrustedUserCAKeys and then also match
// AuthorizedPricipalsFile, if present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants