diff --git a/app/src/main/cpp/MicroPhoto.cpp b/app/src/main/cpp/MicroPhoto.cpp index 746bb171..17f922a3 100644 --- a/app/src/main/cpp/MicroPhoto.cpp +++ b/app/src/main/cpp/MicroPhoto.cpp @@ -712,35 +712,39 @@ Java_com_xypower_mpapp_MicroPhotoService_getSerialNumber( extern "C" JNIEXPORT jboolean JNICALL Java_com_xypower_mpapp_MicroPhotoService_importPublicKeyFile( - JNIEnv* env, jclass cls, jint index, jstring path, jstring md5) { + JNIEnv* env, jclass cls, jint index, jstring outputPath, jstring md5) { #ifdef USING_NRSEC - NrsecPort nrsec; - // NrsecSpiPort spi("/dev/mtkgpioctrl"); // NrsecSpiPort spi("/dev/spidevSE"); // const char *port = "/dev/mtkgpioctrl"; - const char *port = "/dev/spidev0.0"; - if (!nrsec.Open(port)) + if (env->GetStringUTFLength(outputPath) <=0) + { + return JNI_FALSE; + } + + NrsecPort nrsec; + const char *path = NRSEC_PATH; + if (!nrsec.Open(path)) { return JNI_FALSE; } - const char *pathStr = env->GetStringUTFChars(path, 0); + const char *outputPathStr = env->GetStringUTFChars(outputPath, 0); const char *md5Str = env->GetStringUTFChars(md5, 0); bool res = false; std::vector data; - if (readFile(pathStr, data) && !data.empty()) + if (readFile(outputPathStr, data) && !data.empty()) { - res = nrsec.SM2ImportPublicKey(1, &data[0]); + res = nrsec.SM2ImportPublicKey(index, &data[0]) == 0; } nrsec.Close(); - env->ReleaseStringUTFChars(path, pathStr); + env->ReleaseStringUTFChars(outputPath, outputPathStr); env->ReleaseStringUTFChars(md5, md5Str); return res ? JNI_TRUE : JNI_FALSE; @@ -768,7 +772,7 @@ Java_com_xypower_mpapp_MicroPhotoService_importPublicKey( jbyte* byteCert = env->GetByteArrayElements(cert, 0); - bool res = nrsec.SM2ImportPublicKey(index, (const uint8_t*)byteCert); + bool res = nrsec.SM2ImportPublicKey(index, (const uint8_t*)byteCert) == 0; nrsec.Close(); @@ -784,12 +788,11 @@ Java_com_xypower_mpapp_MicroPhotoService_genKeys( jclass cls, jint index) { #ifdef USING_NRSEC - // GpioControl::setRS485Enable(true); - GpioControl::setSpiMode(SPI_MODE_3); - GpioControl::setSpiBitsPerWord(8); - GpioControl::setSpiMaxSpeedHz(33000000); + //GpioControl::setSpiMode(SPI_MODE_3); + //GpioControl::setSpiBitsPerWord(8); + //GpioControl::setSpiMaxSpeedHz(33000000); const char *path = NRSEC_PATH; @@ -834,7 +837,7 @@ Java_com_xypower_mpapp_MicroPhotoService_requestCert( JNIEnv* env, jclass cls, jint type, jint index, jstring subject, jstring outputPath) { #ifdef USING_NRSEC - if (env->GetStringUTFLength(subject) || env->GetStringUTFLength(outputPath)) + if (env->GetStringUTFLength(subject) <=0 || env->GetStringUTFLength(outputPath) <=0) { return JNI_FALSE; } @@ -849,7 +852,9 @@ Java_com_xypower_mpapp_MicroPhotoService_requestCert( uint8_t output[1024] = { 0 }; uint16_t len = 0; const char* subjectStr = env->GetStringUTFChars(subject, 0); - bool res = nrsec.SM2cert(type, index, subjectStr, output, &len) == 0; + + std::string titleStr(subjectStr); + bool res = nrsec.SM2cert(type, index, titleStr, output, &len) == 0; nrsec.Close(); env->ReleaseStringUTFChars(subject, subjectStr); if (!res) @@ -874,30 +879,35 @@ Java_com_xypower_mpapp_MicroPhotoService_requestCert( extern "C" JNIEXPORT jboolean JNICALL Java_com_xypower_mpapp_MicroPhotoService_importPrivateKeyFile( - JNIEnv* env, jclass cls, jint index, jstring path, jstring md5) { + JNIEnv* env, jclass cls, jint index, jstring outputPath, jstring md5) { #ifdef USING_NRSEC - const char *port = "/dev/spidev0.0"; + + if (env->GetStringUTFLength(outputPath)<=0) + { + return JNI_FALSE; + } + const char *path = NRSEC_PATH; NrsecPort nrsec; - if (!nrsec.Open(port)) + if (!nrsec.Open(path)) { return JNI_FALSE; } - const char *pathStr = env->GetStringUTFChars(path, 0); + const char *outputPathStr = env->GetStringUTFChars(outputPath, 0); const char *md5Str = env->GetStringUTFChars(md5, 0); bool res = false; std::vector data; - if (readFile(pathStr, data) && !data.empty()) + if (readFile(outputPathStr, data) && !data.empty()) { res = nrsec.SM2ImportPrivateKey(index, &data[0]) == 0; } nrsec.Close(); - env->ReleaseStringUTFChars(path, pathStr); + env->ReleaseStringUTFChars(outputPath, outputPathStr); env->ReleaseStringUTFChars(md5, md5Str); return res ? JNI_TRUE : JNI_FALSE; @@ -907,11 +917,16 @@ Java_com_xypower_mpapp_MicroPhotoService_importPrivateKeyFile( extern "C" JNIEXPORT jboolean JNICALL Java_com_xypower_mpapp_MicroPhotoService_exportPublicKeyFile( - JNIEnv* env, jclass cls, jint index) { + JNIEnv* env, jclass cls, jint index, jstring outputPath) { #ifdef USING_NRSEC - const char *path = NRSEC_PATH; + if (env->GetStringUTFLength(outputPath)<=0) + { + return JNI_FALSE; + } + + const char *path = NRSEC_PATH; NrsecPort nrsec; if (!nrsec.Open(path)) { @@ -920,28 +935,41 @@ Java_com_xypower_mpapp_MicroPhotoService_exportPublicKeyFile( bool res = false; std::vector data(64,0); - res = nrsec.SM2ExportPublicKey(index, &data[0]) == 0; - - FILE* file = fopen("/sdcard/device_pbkey.cer", "wb"); - - fwrite(&data[0], sizeof(unsigned char), data.size(), file); - - fclose(file); - + uint8_t len=0; + res = nrsec.SM2ExportPublicKey(index, &data[0], &len) == 0; nrsec.Close(); + if (!res) + { + return JNI_FALSE; + } - return res ? JNI_TRUE : JNI_FALSE; + const char* outputPathStr = env->GetStringUTFChars(outputPath, 0); + FILE* file = fopen(outputPathStr, "wb"); + env->ReleaseStringUTFChars(outputPath, outputPathStr); + if (file == NULL) + { + return JNI_FALSE; + } + + int bytes = fwrite(&data[0], sizeof(unsigned char), len, file); + fclose(file); + return bytes == len ? JNI_TRUE : JNI_FALSE; #endif } extern "C" JNIEXPORT jboolean JNICALL Java_com_xypower_mpapp_MicroPhotoService_exportPrivateFile( - JNIEnv* env, jclass cls, jint index) { + JNIEnv* env, jclass cls, jint index, jstring outputPath) { #ifdef USING_NRSEC - const char *path = NRSEC_PATH; + if (env->GetStringUTFLength(outputPath)<=0) + { + return JNI_FALSE; + } + + const char *path = NRSEC_PATH; NrsecPort nrsec; if (!nrsec.Open(path)) { @@ -950,17 +978,25 @@ Java_com_xypower_mpapp_MicroPhotoService_exportPrivateFile( bool res = false; std::vector data(64,0); - res = nrsec.SM2ExportPrivateKey(index, &data[0]) == 0; - - FILE* Exprvkey_file = fopen("/sdcard/device_prvkey.cer", "wb"); - - fwrite(&data[0], sizeof(unsigned char), data.size(), Exprvkey_file); - - fclose(Exprvkey_file); - + uint8_t len=0; + res = nrsec.SM2ExportPrivateKey(index, &data[0],&len) == 0; nrsec.Close(); + if (!res) + { + return JNI_FALSE; + } - return res ? JNI_TRUE : JNI_FALSE; + const char* outputPathStr = env->GetStringUTFChars(outputPath, 0); + FILE* file = fopen(outputPathStr, "wb"); + env->ReleaseStringUTFChars(outputPath, outputPathStr); + if (file == NULL) + { + return JNI_FALSE; + } + + int bytes = fwrite(&data[0], sizeof(unsigned char), len, file); + fclose(file); + return bytes == len ? JNI_TRUE : JNI_FALSE; #endif } diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index e24d3850..4fd33563 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -1140,15 +1140,15 @@ cellSignalStrengthGsm.getDbm(); public static native void setOtgState(boolean enabled); public static native void setCam3V3Enable(boolean enabled); public static native String getSerialNumber(); - public static native boolean importPublicKeyFile(int index, String path, String md5); + public static native boolean importPublicKeyFile(int index, String outputPath, String md5); public static native boolean importPublicKey(int index, byte cert[]); public static native boolean genKeys(int index); public static native String querySecVersion(); public static native boolean requestCert(int type, int index, String subject, String outputPath); - public static native boolean importPrivateKeyFile(int index, String path, String md5); - public static native boolean exportPublicKeyFile(int index); - public static native boolean exportPrivateFile(int index); + public static native boolean importPrivateKeyFile(int index, String outputPath, String md5); + public static native boolean exportPublicKeyFile(int index, String outputPath); + public static native boolean exportPrivateFile(int index, String outputPath); ////////////////////////GPS////////////////////