int len = buffer.getInt();
buffer.wpos(buffer.rpos() + len);
PublicKey key = buffer.getRawPublicKey();
String keyAlg = (key instanceof RSAPublicKey) ? KeyPairProvider.SSH_RSA : KeyPairProvider.SSH_DSS;
Signature verif = NamedFactory.Utils.create(session.getFactoryManager().getSignatureFactories(), keyAlg);
verif.init(key, null);
buffer.wpos(oldLim);
byte[] sig = hasSig ? buffer.getBytes() : null;
PublickeyAuthenticator authenticator = session.getServerFactoryManager().getPublickeyAuthenticator();
if (authenticator == null) {
throw new Exception("No PublickeyAuthenticator configured");
}
if (!authenticator.authenticate(username, key, session)) {
return false;
}
if (!hasSig) {
Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_PK_OK, 0);
buf.putString(alg);
buf.putRawBytes(buffer.array(), oldPos, 4 + len);
session.writePacket(buf);
return null;
} else {
Buffer buf = new Buffer();
buf.putString(session.getKex().getH());
buf.putCommand(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
buf.putString(username);
buf.putString("ssh-connection");
buf.putString("publickey");
buf.putByte((byte) 1);
buf.putString(keyAlg);
buffer.rpos(oldPos);
buffer.wpos(oldPos + 4 + len);
buf.putBuffer(buffer);
verif.update(buf.array(), buf.rpos(), buf.available());
if (!verif.verify(sig)) {
throw new Exception("Key verification failed");
}
return true;
}
}