int len = buffer.getInt();
buffer.wpos(buffer.rpos() + len);
PublicKey key = buffer.getPublicKey();
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 (!hasSig) {
if (authenticator.hasKey(username, key, session)) {
Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_PK_OK);
buf.putString(alg);
buf.putRawBytes(buffer.array(), oldPos, 4 + len);
session.writePacket(buf);
return null;
} else {
throw new Exception("Unsupported key for user");
}
} else {
if (!authenticator.hasKey(username, key, session)) {
throw new Exception("Unsupported key for user");
}
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)) {
return username;
} else {
throw new Exception("Key verification failed");
}
}