mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 02:07:39 +08:00
cLib: Move keygeneration code over there
This way we can easily test it outside of Android. Strangely enough this doesn't work on Android or osx for me. It only seems to work in an ubuntu linux container.
This commit is contained in:
@ -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
|
||||
)
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
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, "");
|
||||
}
|
||||
|
@ -1,121 +0,0 @@
|
||||
#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\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, "");
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
82
gj_common/keygen.c
Normal file
82
gj_common/keygen.c
Normal file
@ -0,0 +1,82 @@
|
||||
#include "gitjournal.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <git2.h>
|
||||
#include <libssh/libssh.h>
|
||||
#include <libssh/callbacks.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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
@ -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");
|
||||
|
Reference in New Issue
Block a user