From 284a09b19bc3be8849fc71acd0ad407c43ec7380 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 31 Oct 2024 22:38:03 +0100 Subject: sftp: Simplify authentication with private key Let ssh2 derive the public key from the private key. Much easier. --- libs/sftp/src/main/cpp/sftp.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'libs/sftp/src/main/cpp/sftp.cpp') diff --git a/libs/sftp/src/main/cpp/sftp.cpp b/libs/sftp/src/main/cpp/sftp.cpp index 1bc4fcb..3368c53 100644 --- a/libs/sftp/src/main/cpp/sftp.cpp +++ b/libs/sftp/src/main/cpp/sftp.cpp @@ -364,11 +364,10 @@ class SshSession { nullptr) == 0; } - bool Authenticate(const std::string& username, const std::vector& public_key, - const std::vector& private_key, const std::string& passphrase) { + bool Authenticate(const std::string& username, const std::vector& private_key, const std::string& passphrase) { return libssh2_userauth_publickey_frommemory( session_.get(), username.data(), username.size(), - reinterpret_cast(public_key.data()), public_key.size(), + nullptr, 0, reinterpret_cast(private_key.data()), private_key.size(), passphrase.c_str()) == 0; } @@ -425,13 +424,11 @@ jbyteArray nativeSshSessionHandshake(JNIEnv* env, jclass, jlong ptr) { } jboolean nativeSshSessionAuthenticate(JNIEnv* env, jclass, jlong ptr, jstring j_username, - jstring password, jbyteArray public_key, - jbyteArray private_key) { + jstring password, jbyteArray private_key) { auto username = jni::StringToUTF8(env, jni::ParamRef(env, j_username)); - if (public_key != nullptr && private_key != nullptr) { + if (private_key != nullptr) { return reinterpret_cast(ptr)->Authenticate( username, - jni::ByteArrayToVector(env, jni::ParamRef(env, public_key)), jni::ByteArrayToVector(env, jni::ParamRef(env, private_key)), password != nullptr ? jni::StringToUTF8(env, jni::ParamRef(env, password)) : "") ? JNI_TRUE : JNI_FALSE; @@ -576,7 +573,7 @@ void RegisterSftp(JNIEnv* env) { { "nativeSshSessionGetLastError", "(J)Ljava/lang/String;", reinterpret_cast(&nativeSshSessionGetLastError) }, { "nativeSshSessionConnect", "(JLjava/lang/String;I)Z", reinterpret_cast(&nativeSshSessionConnect) }, { "nativeSshSessionHandshake", "(J)[B", reinterpret_cast(&nativeSshSessionHandshake) }, - { "nativeSshSessionAuthenticate", "(JLjava/lang/String;Ljava/lang/String;[B[B)Z", reinterpret_cast(&nativeSshSessionAuthenticate) }, + { "nativeSshSessionAuthenticate", "(JLjava/lang/String;Ljava/lang/String;[B)Z", reinterpret_cast(&nativeSshSessionAuthenticate) }, { "nativeSshSessionNewSftpSession", "(J)J", reinterpret_cast(&nativeSshSessionNewSftpSession) }, { "nativeSftpSessionDestroy", "(J)V", reinterpret_cast(&nativeSftpSessionDestroy) }, -- cgit v1.2.3-70-g09d2