From 4eefd56b2a900cb92248ee6cc79236235e2c143f Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 5 Jun 2024 22:52:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8E=A5=E5=8F=A3=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=92=8C=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/MicroPhoto.cpp | 97 +++++++++---------- .../com/xypower/mpapp/MicroPhotoService.java | 4 +- 2 files changed, 49 insertions(+), 52 deletions(-) diff --git a/app/src/main/cpp/MicroPhoto.cpp b/app/src/main/cpp/MicroPhoto.cpp index 88c70479..746bb171 100644 --- a/app/src/main/cpp/MicroPhoto.cpp +++ b/app/src/main/cpp/MicroPhoto.cpp @@ -12,6 +12,8 @@ #include +#define NRSEC_PATH "/dev/spidev0.0" + #ifdef USING_BREAK_PAD #include "client/linux/handler/exception_handler.h" #include "client/linux/handler/minidump_descriptor.h" @@ -751,25 +753,19 @@ Java_com_xypower_mpapp_MicroPhotoService_importPublicKey( #ifdef USING_NRSEC - NrsecPort nrsec; - // const char *port = "/dev/mtkgpioctrl"; - const char *port = "/dev/spidev0.0"; int byteCertLen = env->GetArrayLength(cert); if (byteCertLen <= 0) { return JNI_FALSE; } - if (!nrsec.Open(port)) + NrsecPort nrsec; + const char *path = NRSEC_PATH; + if (!nrsec.Open(path)) { return JNI_FALSE; } - std::string version = nrsec.Version(); - - char buf[128] = { 0 }; - strcpy(buf, version.c_str()); - jbyte* byteCert = env->GetByteArrayElements(cert, 0); bool res = nrsec.SM2ImportPublicKey(index, (const uint8_t*)byteCert); @@ -795,20 +791,14 @@ Java_com_xypower_mpapp_MicroPhotoService_genKeys( GpioControl::setSpiBitsPerWord(8); GpioControl::setSpiMaxSpeedHz(33000000); - const char *port = "/dev/mtkgpioctrl"; - // const char *port = "/dev/spidevSE"; + const char *path = NRSEC_PATH; NrsecPort nrsec; - if (!nrsec.Open(port)) + if (!nrsec.Open(path)) { return JNI_FALSE; } - std::string version = nrsec.Version(); - - char buf[128] = { 0 }; - strcpy(buf, version.c_str()); - bool res = nrsec.SM2keypair(index) == 0; nrsec.Close(); @@ -818,61 +808,68 @@ Java_com_xypower_mpapp_MicroPhotoService_genKeys( } -extern "C" JNIEXPORT jboolean JNICALL -Java_com_xypower_mpapp_MicroPhotoService_queryVersion( +extern "C" JNIEXPORT jstring JNICALL +Java_com_xypower_mpapp_MicroPhotoService_querySecVersion( JNIEnv* env, jclass cls) { #ifdef USING_NRSEC - const char *port = "/dev/spidev0.0"; + const char *path = NRSEC_PATH; NrsecPort nrsec; - if (!nrsec.Open(port)) + if (!nrsec.Open(path)) { - return JNI_FALSE; + return NULL; } std::string version = nrsec.Version(); - - char buf[128] = { 0 }; - strcpy(buf, version.c_str()); - nrsec.Close(); - return JNI_TRUE; + return env->NewStringUTF(version.c_str()); #endif } extern "C" JNIEXPORT jboolean JNICALL Java_com_xypower_mpapp_MicroPhotoService_requestCert( - JNIEnv* env, jclass cls, jint type, jint index, jstring subject) { + JNIEnv* env, jclass cls, jint type, jint index, jstring subject, jstring outputPath) { #ifdef USING_NRSEC - const char *port = "/dev/spidev0.0"; + if (env->GetStringUTFLength(subject) || env->GetStringUTFLength(outputPath)) + { + return JNI_FALSE; + } + const char *path = NRSEC_PATH; NrsecPort nrsec; - if (!nrsec.Open(port)) + if (!nrsec.Open(path)) { return JNI_FALSE; } - std::string title_subject = env->GetStringUTFChars(subject, 0); - - uint8_t output[1024]; - uint16_t len; - bool res = nrsec.SM2cert(type,index,title_subject,output,&len) == 0; - - FILE* csr_file = fopen("/sdcard/com.xypower.mpapp/temp/rqcert.csr", "wb"); - - fwrite(output, sizeof(unsigned char), len, csr_file); + 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; + nrsec.Close(); + env->ReleaseStringUTFChars(subject, subjectStr); + if (!res) + { + return JNI_FALSE; + } - fclose(csr_file); + const char* outputPathStr = env->GetStringUTFChars(outputPath, 0); + FILE* file = fopen(outputPathStr, "wb"); + env->ReleaseStringUTFChars(outputPath, outputPathStr); + if (file == NULL) + { + return JNI_FALSE; + } - nrsec.Close(); + int bytes = fwrite(output, sizeof(unsigned char), len, file); + fclose(file); - return res ? JNI_TRUE : JNI_FALSE; + return bytes == len ? JNI_TRUE : JNI_FALSE; #endif - } extern "C" JNIEXPORT jboolean JNICALL @@ -913,10 +910,10 @@ Java_com_xypower_mpapp_MicroPhotoService_exportPublicKeyFile( JNIEnv* env, jclass cls, jint index) { #ifdef USING_NRSEC - const char *port = "/dev/spidev0.0"; + const char *path = NRSEC_PATH; NrsecPort nrsec; - if (!nrsec.Open(port)) + if (!nrsec.Open(path)) { return JNI_FALSE; } @@ -925,11 +922,11 @@ Java_com_xypower_mpapp_MicroPhotoService_exportPublicKeyFile( std::vector data(64,0); res = nrsec.SM2ExportPublicKey(index, &data[0]) == 0; - FILE* Expbkey_file = fopen("/sdcard/device_pbkey.cer", "wb"); + FILE* file = fopen("/sdcard/device_pbkey.cer", "wb"); - fwrite(&data[0], sizeof(unsigned char), data.size(), Expbkey_file); + fwrite(&data[0], sizeof(unsigned char), data.size(), file); - fclose(Expbkey_file); + fclose(file); nrsec.Close(); @@ -943,10 +940,10 @@ Java_com_xypower_mpapp_MicroPhotoService_exportPrivateFile( JNIEnv* env, jclass cls, jint index) { #ifdef USING_NRSEC - const char *port = "/dev/spidev0.0"; + const char *path = NRSEC_PATH; NrsecPort nrsec; - if (!nrsec.Open(port)) + if (!nrsec.Open(path)) { return JNI_FALSE; } diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 4841f453..e24d3850 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -1144,8 +1144,8 @@ cellSignalStrengthGsm.getDbm(); public static native boolean importPublicKey(int index, byte cert[]); public static native boolean genKeys(int index); - public static native boolean queryVersion(); - public static native boolean requestCert(int type, int index, String subject); + 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);