From 5dddeed54d209938f4dc7764aae417fdb55f4f85 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 14 May 2019 14:28:43 +0200 Subject: [PATCH] Add a generateKeys JNI function It goes via Dart -> Java -> JNI -> OurLib -> LibSSH. Also, it still doesn't seem to work, and I'm not sure why. I kinda giveup for now. --- android/app/src/main/cpp/CMakeLists.txt | 14 +- android/app/src/main/cpp/keygen.c | 121 ++++++++++++++++++ android/app/src/main/cpp/native-lib.cpp | 36 ------ .../java/io/gitjournal/gitjournal/Git.java | 6 +- .../gitjournal/gitjournal/MainActivity.java | 3 - 5 files changed, 132 insertions(+), 48 deletions(-) create mode 100644 android/app/src/main/cpp/keygen.c delete mode 100644 android/app/src/main/cpp/native-lib.cpp diff --git a/android/app/src/main/cpp/CMakeLists.txt b/android/app/src/main/cpp/CMakeLists.txt index e9df7e9f..12d9c1c2 100644 --- a/android/app/src/main/cpp/CMakeLists.txt +++ b/android/app/src/main/cpp/CMakeLists.txt @@ -21,7 +21,6 @@ set_target_properties(openssl-lib PROPERTIES IMPORTED_LOCATION ${lib_src_DIR}/lib/libssl.a) - add_library(crypto-lib STATIC IMPORTED) @@ -30,6 +29,13 @@ set_target_properties(crypto-lib PROPERTIES IMPORTED_LOCATION ${lib_src_DIR}/lib/libcrypto.a) +add_library(ssh-lib + STATIC + IMPORTED) + +set_target_properties(ssh-lib + PROPERTIES IMPORTED_LOCATION + ${lib_src_DIR}/lib/libssh.a) add_library(ssh2-lib STATIC @@ -39,7 +45,6 @@ set_target_properties(ssh2-lib PROPERTIES IMPORTED_LOCATION ${lib_src_DIR}/lib/libssh2.a) - add_library(git2-lib STATIC IMPORTED) @@ -50,8 +55,9 @@ set_target_properties(git2-lib add_library(native-lib SHARED - native-lib.cpp) + keygen.c +) # The order of these libraries is super dooper important # Otherwise you'll get linker errors -target_link_libraries(native-lib git2-lib ssh2-lib openssl-lib crypto-lib) +target_link_libraries(native-lib git2-lib ssh2-lib ssh-lib openssl-lib crypto-lib android log) diff --git a/android/app/src/main/cpp/keygen.c b/android/app/src/main/cpp/keygen.c new file mode 100644 index 00000000..effdeed9 --- /dev/null +++ b/android/app/src/main/cpp/keygen.c @@ -0,0 +1,121 @@ +#include +#include +#include +#include + +#include +#include +#include + +void change_pubickey_comment(const char *filename, const char *comment) +{ + FILE *fp = fopen(filename, "r"); + char buff[10000]; + fgets(buff, 10000, fp); + fclose(fp); + + // Remove the comment + char *end = strchr(strchr(buff, ' ') + 1, ' '); + int len = end - buff + 1; + buff[len] = 0; + + // Add custom comment + strcat(buff, comment); + strcat(buff, "\n"); + + // Write the file back + fp = fopen(filename, "w"); + fputs(buff, fp); + fclose(fp); +} + +int generate_keys(const char* private_key_path, + const char* public_key_path, + const char* comment) +{ + ssh_key key; + + int ret = ssh_pki_generate(SSH_KEYTYPE_RSA, 4096, &key); + if (ret != SSH_OK) + { + return ret; + } + + __android_log_write(ANDROID_LOG_ERROR, "HOWDY", "KEY GENERATED"); + + /* + FILE* file = fopen(private_key_path, "wb"); + if (file == NULL) { + __android_log_print(ANDROID_LOG_ERROR, "HOWDY", "file is null %d", errno); + return 1; + } + + struct stat sb; + + int rc = fstat(fileno(file), &sb); + if (rc < 0) { + __android_log_write(ANDROID_LOG_ERROR, "HOWDY", "stat problems"); + + } + */ + + char *password = ""; + ret = ssh_pki_export_privkey_file(key, password, NULL, NULL, private_key_path); + if (ret != SSH_OK) { + __android_log_print(ANDROID_LOG_ERROR, "HOWDY", "Could not write private key %d", ret); + return ret; + } + __android_log_write(ANDROID_LOG_ERROR, "HOWDY", "Private key exported"); + + ret = ssh_pki_export_pubkey_file(key, public_key_path); + if (ret != SSH_OK) { + return ret; + } + __android_log_write(ANDROID_LOG_ERROR, "HOWDY", "Public key exported"); + + ssh_key_free(key); + + change_pubickey_comment(public_key_path, comment); + + // Change file permissions + char mode[] = "0600"; + int modeInt = strtol(mode, 0, 8); + chmod(private_key_path, modeInt); + + return 0; +} + +void ssh_log_jni_callback(int priority, const char *function, const char *buffer, void *userdata) +{ + __android_log_print(ANDROID_LOG_ERROR, "SSH_HOWDY", "P%d : %s : %s", priority, function, buffer); +} + +JNIEXPORT jstring JNICALL +Java_io_gitjournal_gitjournal_Git_generateKeys( + JNIEnv *env, + jobject this_obj, + jstring jni_private_key_path, + jstring jni_public_key_path, + jstring jni_comment) +{ + const char *private_key_path = (*env)->GetStringUTFChars(env, jni_private_key_path, 0); + const char *public_key_path = (*env)->GetStringUTFChars(env, jni_public_key_path, 0); + const char *comment = (*env)->GetStringUTFChars(env, jni_comment, 0); + + __android_log_write(ANDROID_LOG_ERROR, "HOWDY", "Error msg"); + __android_log_write(ANDROID_LOG_ERROR, "HOWDY", private_key_path); + __android_log_write(ANDROID_LOG_ERROR, "HOWDY", public_key_path); + __android_log_write(ANDROID_LOG_ERROR, "HOWDY", comment); + + // Debugging + ssh_set_log_level(SSH_LOG_FUNCTIONS); + ssh_set_log_callback(ssh_log_jni_callback); + ssh_set_log_level(SSH_LOG_FUNCTIONS); + + int ret = generate_keys(private_key_path, public_key_path, comment); + if (ret != 0) { + return (*env)->NewStringUTF(env, "Error Generating PublicKeys"); + } + + return (*env)->NewStringUTF(env, ""); +} diff --git a/android/app/src/main/cpp/native-lib.cpp b/android/app/src/main/cpp/native-lib.cpp deleted file mode 100644 index 7044109f..00000000 --- a/android/app/src/main/cpp/native-lib.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - -extern "C" JNIEXPORT jstring JNICALL -Java_io_gitjournal_gitjournal_Git_stringFromJNI( - JNIEnv *env, - jobject /* this */) { - - ERR_load_crypto_strings(); - const char *openssl_version = OpenSSL_version(0); - const char *ssh2_version = libssh2_version(0); - - std::string hello = "Hello from C++: "; - hello += openssl_version; - hello += " SSH Version: "; - hello += ssh2_version; - - git_libgit2_init(); - int major; - int minor; - int patch; - git_libgit2_version(&major, &minor, &patch); - - hello += " Git version: "; - char n_str[20]; - sprintf(n_str, "%d.%d.%d", major, minor, patch); - hello += n_str; - - return env->NewStringUTF(hello.c_str()); -} diff --git a/android/app/src/main/java/io/gitjournal/gitjournal/Git.java b/android/app/src/main/java/io/gitjournal/gitjournal/Git.java index 1b802d81..91033453 100644 --- a/android/app/src/main/java/io/gitjournal/gitjournal/Git.java +++ b/android/app/src/main/java/io/gitjournal/gitjournal/Git.java @@ -5,9 +5,5 @@ public class Git { System.loadLibrary("native-lib"); } - /** - * A native method that is implemented by the 'native-lib' native library, - * which is packaged with this application. - */ - public native String stringFromJNI(); + public native String generateKeys(String privateKeyPath, String publicKeyPath, String comment); } diff --git a/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java b/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java index 87e560d5..5f0a8b12 100644 --- a/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java +++ b/android/app/src/main/java/io/gitjournal/gitjournal/MainActivity.java @@ -26,9 +26,6 @@ public class MainActivity extends FlutterActivity implements MethodCallHandler { @Override protected void onCreate(Bundle savedInstanceState) { - Git git = new Git(); - Log.d("VISH", git.stringFromJNI()); - super.onCreate(savedInstanceState); GeneratedPluginRegistrant.registerWith(this);