mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 03:19:11 +08:00
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.
This commit is contained in:
@ -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)
|
||||
|
121
android/app/src/main/cpp/keygen.c
Normal file
121
android/app/src/main/cpp/keygen.c
Normal file
@ -0,0 +1,121 @@
|
||||
#include <jni.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <libssh/libssh.h>
|
||||
#include <android/log.h>
|
||||
#include <errno.h>
|
||||
|
||||
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, "");
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
#include <jni.h>
|
||||
#include <string>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <libssh2.h>
|
||||
#include <git2.h>
|
||||
|
||||
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());
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user