From 6794f9943ce73ae59695023adf6f89c5f6aa3820 Mon Sep 17 00:00:00 2001 From: Aries Date: Wed, 5 Jun 2024 11:28:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=A0=E5=AF=86=E8=8A=AF?= =?UTF-8?q?=E7=89=87=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/MicroPhoto.cpp | 152 +++++++++++++++++- .../com/xypower/mpapp/MicroPhotoService.java | 7 + 2 files changed, 158 insertions(+), 1 deletion(-) diff --git a/app/src/main/cpp/MicroPhoto.cpp b/app/src/main/cpp/MicroPhoto.cpp index 7077c5cd..88c70479 100644 --- a/app/src/main/cpp/MicroPhoto.cpp +++ b/app/src/main/cpp/MicroPhoto.cpp @@ -816,4 +816,154 @@ Java_com_xypower_mpapp_MicroPhotoService_genKeys( return res ? JNI_TRUE : JNI_FALSE; #endif -} \ No newline at end of file +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_xypower_mpapp_MicroPhotoService_queryVersion( + JNIEnv* env, + jclass cls) { +#ifdef USING_NRSEC + + const char *port = "/dev/spidev0.0"; + + NrsecPort nrsec; + if (!nrsec.Open(port)) + { + return JNI_FALSE; + } + + std::string version = nrsec.Version(); + + char buf[128] = { 0 }; + strcpy(buf, version.c_str()); + + nrsec.Close(); + + return JNI_TRUE; +#endif +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_xypower_mpapp_MicroPhotoService_requestCert( + JNIEnv* env, jclass cls, jint type, jint index, jstring subject) { + +#ifdef USING_NRSEC + const char *port = "/dev/spidev0.0"; + + NrsecPort nrsec; + if (!nrsec.Open(port)) + { + 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); + + fclose(csr_file); + + nrsec.Close(); + + return res ? JNI_TRUE : JNI_FALSE; +#endif + +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_xypower_mpapp_MicroPhotoService_importPrivateKeyFile( + JNIEnv* env, jclass cls, jint index, jstring path, jstring md5) { + +#ifdef USING_NRSEC + const char *port = "/dev/spidev0.0"; + + NrsecPort nrsec; + if (!nrsec.Open(port)) + { + return JNI_FALSE; + } + + const char *pathStr = env->GetStringUTFChars(path, 0); + const char *md5Str = env->GetStringUTFChars(md5, 0); + + bool res = false; + std::vector data; + if (readFile(pathStr, data) && !data.empty()) + { + res = nrsec.SM2ImportPrivateKey(index, &data[0]) == 0; + } + + nrsec.Close(); + + env->ReleaseStringUTFChars(path, pathStr); + env->ReleaseStringUTFChars(md5, md5Str); + + return res ? JNI_TRUE : JNI_FALSE; + +#endif +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_xypower_mpapp_MicroPhotoService_exportPublicKeyFile( + JNIEnv* env, jclass cls, jint index) { + +#ifdef USING_NRSEC + const char *port = "/dev/spidev0.0"; + + NrsecPort nrsec; + if (!nrsec.Open(port)) + { + return JNI_FALSE; + } + + bool res = false; + std::vector data(64,0); + res = nrsec.SM2ExportPublicKey(index, &data[0]) == 0; + + FILE* Expbkey_file = fopen("/sdcard/device_pbkey.cer", "wb"); + + fwrite(&data[0], sizeof(unsigned char), data.size(), Expbkey_file); + + fclose(Expbkey_file); + + nrsec.Close(); + + return res ? JNI_TRUE : JNI_FALSE; + +#endif +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_xypower_mpapp_MicroPhotoService_exportPrivateFile( + JNIEnv* env, jclass cls, jint index) { + +#ifdef USING_NRSEC + const char *port = "/dev/spidev0.0"; + + NrsecPort nrsec; + if (!nrsec.Open(port)) + { + return JNI_FALSE; + } + + 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); + + nrsec.Close(); + + return res ? 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 9275c2d3..4841f453 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -1144,6 +1144,13 @@ 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 boolean importPrivateKeyFile(int index, String path, String md5); + public static native boolean exportPublicKeyFile(int index); + public static native boolean exportPrivateFile(int index); + + ////////////////////////GPS//////////////////// // private static final String GPS_LOCATION_NAME = android.location.LocationManager.GPS_PROVIDER; private LocationManager mLocationManager;