diff --git a/android/app/src/main/cpp/CMakeLists.txt b/android/app/src/main/cpp/CMakeLists.txt index 19238a4d..73d2dd0d 100644 --- a/android/app/src/main/cpp/CMakeLists.txt +++ b/android/app/src/main/cpp/CMakeLists.txt @@ -49,7 +49,7 @@ set_target_properties(git2-lib add_library(native-lib SHARED ${CMAKE_SOURCE_DIR}/../../../../../gj_common/gitjournal.c - keygen.c + ${CMAKE_SOURCE_DIR}/../../../../../gj_common/keygen.c git.c ) diff --git a/android/app/src/main/cpp/git.c b/android/app/src/main/cpp/git.c index 74428f32..35838d4c 100644 --- a/android/app/src/main/cpp/git.c +++ b/android/app/src/main/cpp/git.c @@ -160,8 +160,6 @@ Java_io_gitjournal_gitjournal_Git_add( return (*env)->NewStringUTF(env, "Error"); } - __android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Everything seems fine"); - return (*env)->NewStringUTF(env, ""); } @@ -181,8 +179,6 @@ Java_io_gitjournal_gitjournal_Git_rm( return (*env)->NewStringUTF(env, "Error"); } - __android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Everything seems fine"); - return (*env)->NewStringUTF(env, ""); } @@ -199,4 +195,23 @@ Java_io_gitjournal_gitjournal_Git_setSshKeys( const char *passphrase = (*env)->GetStringUTFChars(env, jni_passphrase, 0); gj_set_ssh_keys_paths((char *) public_key_path, (char *) private_key_path, (char *) passphrase); -} \ No newline at end of file +} + +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); + + int ret = gj_generate_ssh_keys(private_key_path, public_key_path, comment); + if (ret != 0) { + return (*env)->NewStringUTF(env, "Error Generating SSH Keys"); + } + + return (*env)->NewStringUTF(env, ""); +} diff --git a/android/app/src/main/cpp/keygen.c b/android/app/src/main/cpp/keygen.c deleted file mode 100644 index 8285751d..00000000 --- a/android/app/src/main/cpp/keygen.c +++ /dev/null @@ -1,121 +0,0 @@ -#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\n", 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/gj_common/Makefile b/gj_common/Makefile index e7f8cdc0..d1adfbfc 100644 --- a/gj_common/Makefile +++ b/gj_common/Makefile @@ -1,4 +1,4 @@ CC=clang -test: gitjournal.c test.c - $(CC) -o test -g test.c gitjournal.c -lgit2 +test: gitjournal.c test.c keygen.c + $(CC) -o test -g test.c gitjournal.c keygen.c -lgit2 -lssh diff --git a/gj_common/gitjournal.h b/gj_common/gitjournal.h index 61ad7741..940d2cc5 100644 --- a/gj_common/gitjournal.h +++ b/gj_common/gitjournal.h @@ -33,4 +33,7 @@ void gj_error_free(const gj_error *err); // This must be implemented by you void gj_log(const char *message); +int gj_generate_ssh_keys(const char *private_key_path, + const char *public_key_path, const char *comment); + #endif diff --git a/gj_common/keygen.c b/gj_common/keygen.c new file mode 100644 index 00000000..8ea227ca --- /dev/null +++ b/gj_common/keygen.c @@ -0,0 +1,82 @@ +#include "gitjournal.h" + +#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); +} + +void gj_ssh_log_callback(int priority, const char *function, const char *buffer, void *userdata) +{ + char log_str[1024]; + sprintf(log_str, "LIB_SSH P%d : %s : %s\n", priority, function, buffer); + gj_log(log_str); +} + +int gj_generate_ssh_keys(const char *private_key_path, + const char *public_key_path, const char *comment) +{ + ssh_key key; + int err; + + ssh_set_log_level(SSH_LOG_FUNCTIONS); + ssh_set_log_callback(gj_ssh_log_callback); + + err = ssh_pki_generate(SSH_KEYTYPE_RSA, 4096, &key); + if (err != SSH_OK) + { + gj_log("LIBSSH: ssh_pki_generate failed\n"); + //printf("Error: %s", ssh_get_error()); + return err; + } + + char *password = ""; + err = ssh_pki_export_privkey_file(key, password, NULL, NULL, private_key_path); + if (err != SSH_OK) + { + gj_log("LIBSSH: ssh_pki_export_privkey_file failed\n"); + return err; + } + + err = ssh_pki_export_pubkey_file(key, public_key_path); + if (err != SSH_OK) + { + gj_log("LIBSSH: ssh_pki_export_pubkey_file failed\n"); + return err; + } + + 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; +} diff --git a/gj_common/test.c b/gj_common/test.c index b0e9a039..504d40e0 100644 --- a/gj_common/test.c +++ b/gj_common/test.c @@ -41,9 +41,10 @@ int main(int argc, char *argv[]) char *author_email = "TestMan@example.com"; char *message = "Commit message for GitJournal"; + err = gj_generate_ssh_keys("/tmp/t/id_rsa", "/tmp/t/id_rsa.pub", "Cookie"); //err = gj_git_init(git_base_path); //err = gj_git_commit(git_base_path, author_name, author_email, message); - err = gj_git_clone(clone_url, git_base_path); + //err = gj_git_clone(clone_url, git_base_path); //err = gj_git_push(git_base_path); //err = gj_git_pull(git_base_path, author_name, author_email); //err = gj_git_add(git_base_path, "9.md");