优化加密处理的相关代码

hdrplus
Matthew 12 months ago
parent 6a63213ee8
commit 95c87c7467

@ -725,45 +725,49 @@ Java_com_xypower_mpapp_MicroPhotoService_getSerialNumber(
return env->NewStringUTF(value); return env->NewStringUTF(value);
} }
extern "C" JNIEXPORT jboolean JNICALL extern "C" JNIEXPORT jboolean JNICALL
Java_com_xypower_mpapp_MicroPhotoService_importPublicKeyFile( Java_com_xypower_mpapp_MicroPhotoService_importPublicKeyFile(
JNIEnv* env, jclass cls, jint index, jstring outputPath, jstring md5) { JNIEnv* env, jclass cls, jint index, jstring outputPath, jstring md5) {
#ifdef USING_NRSEC #ifdef USING_NRSEC
// NrsecSpiPort spi("/dev/mtkgpioctrl");
// NrsecSpiPort spi("/dev/spidevSE");
// const char *port = "/dev/mtkgpioctrl";
if (env->GetStringUTFLength(outputPath) <=0) if (env->GetStringUTFLength(outputPath) <=0)
{ {
return JNI_FALSE; return JNI_FALSE;
} }
NrsecPort nrsec; const char *outputPathStr = env->GetStringUTFChars(outputPath, 0);
const char *path = NRSEC_PATH;
if (!nrsec.Open(path)) std::vector<unsigned char> data;
bool res = readFile(outputPathStr, data);
env->ReleaseStringUTFChars(outputPath, outputPathStr);
if (!res || data.empty())
{ {
return JNI_FALSE; return JNI_FALSE;
} }
const char *outputPathStr = env->GetStringUTFChars(outputPath, 0);
const char *md5Str = env->GetStringUTFChars(md5, 0); const char *md5Str = env->GetStringUTFChars(md5, 0);
bool res = false; GpioControl::setCam3V3Enable(true);
std::vector<unsigned char> data; GpioControl::setSpiPower(true);
if (readFile(outputPathStr, data) && !data.empty())
NrsecPort nrsec;
const char *path = NRSEC_PATH;
res = nrsec.Open(path);
if (res)
{ {
res = nrsec.SM2ImportPublicKey(index, &data[0]) == 0; res = nrsec.SM2ImportPublicKey(index, &data[0]) == 0;
nrsec.Close();
} }
nrsec.Close(); GpioControl::setSpiPower(false);
GpioControl::setCam3V3Enable(false);
env->ReleaseStringUTFChars(outputPath, outputPathStr);
env->ReleaseStringUTFChars(md5, md5Str); env->ReleaseStringUTFChars(md5, md5Str);
return res ? JNI_TRUE : JNI_FALSE; return res ? JNI_TRUE : JNI_FALSE;
#else
return JNI_FALSE;
#endif #endif
} }
@ -779,22 +783,26 @@ Java_com_xypower_mpapp_MicroPhotoService_importPublicKey(
return JNI_FALSE; return JNI_FALSE;
} }
GpioControl::setCam3V3Enable(true);
GpioControl::setSpiPower(true);
NrsecPort nrsec; NrsecPort nrsec;
const char *path = NRSEC_PATH; const char *path = NRSEC_PATH;
if (!nrsec.Open(path)) bool res = nrsec.Open(path);
if (res)
{ {
return JNI_FALSE; jbyte* byteCert = env->GetByteArrayElements(cert, 0);
res = nrsec.SM2ImportPublicKey(index, (const uint8_t*)byteCert) == 0;
nrsec.Close();
env->ReleaseByteArrayElements(cert, byteCert, JNI_ABORT);
} }
jbyte* byteCert = env->GetByteArrayElements(cert, 0); GpioControl::setSpiPower(false);
GpioControl::setCam3V3Enable(false);
bool res = nrsec.SM2ImportPublicKey(index, (const uint8_t*)byteCert) == 0;
nrsec.Close();
env->ReleaseByteArrayElements(cert, byteCert, JNI_ABORT);
return res ? JNI_TRUE : JNI_FALSE; return res ? JNI_TRUE : JNI_FALSE;
#else
return JNI_FALSE;
#endif #endif
} }
@ -804,48 +812,52 @@ Java_com_xypower_mpapp_MicroPhotoService_genKeys(
jclass cls, jint index) { jclass cls, jint index) {
#ifdef USING_NRSEC #ifdef USING_NRSEC
// GpioControl::setRS485Enable(true); GpioControl::setCam3V3Enable(true);
GpioControl::setSpiPower(true);
//GpioControl::setSpiMode(SPI_MODE_3);
//GpioControl::setSpiBitsPerWord(8);
//GpioControl::setSpiMaxSpeedHz(33000000);
const char *path = NRSEC_PATH; const char *path = NRSEC_PATH;
NrsecPort nrsec; NrsecPort nrsec;
if (!nrsec.Open(path)) bool res = nrsec.Open(path);
if (res)
{ {
return JNI_FALSE; res = nrsec.SM2keypair(index) == 0;
nrsec.Close();
} }
bool res = nrsec.SM2keypair(index) == 0; GpioControl::setSpiPower(false);
GpioControl::setCam3V3Enable(false);
nrsec.Close();
return res ? JNI_TRUE : JNI_FALSE; return res ? JNI_TRUE : JNI_FALSE;
#else
return JNI_FALSE;
#endif #endif
} }
extern "C" JNIEXPORT jstring JNICALL extern "C" JNIEXPORT jstring JNICALL
Java_com_xypower_mpapp_MicroPhotoService_querySecVersion( Java_com_xypower_mpapp_MicroPhotoService_querySecVersion(
JNIEnv* env, JNIEnv* env,
jclass cls) { jclass cls) {
std::string version;
#ifdef USING_NRSEC #ifdef USING_NRSEC
GpioControl::setCam3V3Enable(true);
GpioControl::setSpiPower(true);
const char *path = NRSEC_PATH; const char *path = NRSEC_PATH;
NrsecPort nrsec; NrsecPort nrsec;
if (!nrsec.Open(path)) bool res = nrsec.Open(path);
if (res)
{ {
return NULL; version = nrsec.Version();
nrsec.Close();
} }
std::string version = nrsec.Version(); GpioControl::setSpiPower(false);
nrsec.Close(); GpioControl::setCam3V3Enable(false);
return env->NewStringUTF(version.c_str());
#endif #endif
return env->NewStringUTF(version.c_str());
} }
extern "C" JNIEXPORT jboolean JNICALL extern "C" JNIEXPORT jboolean JNICALL
@ -859,74 +871,80 @@ Java_com_xypower_mpapp_MicroPhotoService_genCertRequest(
} }
const char *path = NRSEC_PATH; const char *path = NRSEC_PATH;
NrsecPort nrsec; GpioControl::setCam3V3Enable(true);
if (!nrsec.Open(path)) GpioControl::setSpiPower(true);
{
return JNI_FALSE;
}
uint8_t output[1024] = { 0 }; uint8_t output[1024] = { 0 };
uint16_t len = 0; uint16_t len = 0;
const char* subjectStr = env->GetStringUTFChars(subject, 0);
bool res = nrsec.SM2cert(type, index, MakeString(subjectStr), output, &len) == 0; NrsecPort nrsec;
nrsec.Close(); bool res = nrsec.Open(path);
env->ReleaseStringUTFChars(subject, subjectStr); if (res)
if (!res)
{ {
return JNI_FALSE; const char* subjectStr = env->GetStringUTFChars(subject, 0);
res = nrsec.SM2cert(type, index, MakeString(subjectStr), output, &len) == 0;
nrsec.Close();
env->ReleaseStringUTFChars(subject, subjectStr);
} }
const char* outputPathStr = env->GetStringUTFChars(outputPath, 0); if (res)
FILE* file = fopen(outputPathStr, "wb");
env->ReleaseStringUTFChars(outputPath, outputPathStr);
if (file == NULL)
{ {
return JNI_FALSE; const char* outputPathStr = env->GetStringUTFChars(outputPath, 0);
res = writeFile(outputPathStr, output, len);
env->ReleaseStringUTFChars(outputPath, outputPathStr);
} }
int bytes = fwrite(output, sizeof(unsigned char), len, file); return res ? JNI_TRUE : JNI_FALSE;
fclose(file); #else
return JNI_FALSE;
return bytes == len ? JNI_TRUE : JNI_FALSE;
#endif #endif
} }
extern "C" JNIEXPORT jboolean JNICALL extern "C" JNIEXPORT jboolean JNICALL
Java_com_xypower_mpapp_MicroPhotoService_importPrivateKeyFile( Java_com_xypower_mpapp_MicroPhotoService_importPrivateKeyFile(
JNIEnv* env, jclass cls, jint index, jstring outputPath, jstring md5) { JNIEnv* env, jclass cls, jint index, jstring keyFilePath, jstring md5) {
#ifdef USING_NRSEC #ifdef USING_NRSEC
if (env->GetStringUTFLength(outputPath)<=0) if (env->GetStringUTFLength(keyFilePath) <= 0)
{ {
return JNI_FALSE; return JNI_FALSE;
} }
const char *path = NRSEC_PATH;
NrsecPort nrsec; bool res = false;
if (!nrsec.Open(path)) std::vector<unsigned char> data;
const char *keyFilePathStr = env->GetStringUTFChars(keyFilePath, 0);
res = readFile(keyFilePathStr, data);
env->ReleaseStringUTFChars(keyFilePath, keyFilePathStr);
if (!res || data.empty())
{ {
return JNI_FALSE; return JNI_FALSE;
} }
const char *outputPathStr = env->GetStringUTFChars(outputPath, 0); const char *path = NRSEC_PATH;
const char *md5Str = env->GetStringUTFChars(md5, 0);
bool res = false; GpioControl::setCam3V3Enable(true);
std::vector<unsigned char> data; GpioControl::setSpiPower(true);
if (readFile(outputPathStr, data) && !data.empty())
NrsecPort nrsec;
res = nrsec.Open(path);
if (res)
{ {
res = nrsec.SM2ImportPrivateKey(index, &data[0]) == 0; res = nrsec.SM2ImportPrivateKey(index, &data[0]) == 0;
nrsec.Close();
} }
nrsec.Close(); GpioControl::setSpiPower(false);
GpioControl::setCam3V3Enable(false);
env->ReleaseStringUTFChars(outputPath, outputPathStr); // const char *md5Str = env->GetStringUTFChars(md5, 0);
env->ReleaseStringUTFChars(md5, md5Str); // env->ReleaseStringUTFChars(md5, md5Str);
return res ? JNI_TRUE : JNI_FALSE; return res ? JNI_TRUE : JNI_FALSE;
#else
return JNI_FALSE;
#endif #endif
} }
@ -942,34 +960,33 @@ Java_com_xypower_mpapp_MicroPhotoService_exportPublicKeyFile(
} }
const char *path = NRSEC_PATH; const char *path = NRSEC_PATH;
NrsecPort nrsec;
if (!nrsec.Open(path))
{
return JNI_FALSE;
}
bool res = false;
std::vector<unsigned char> data(64, 0);
uint8_t len = 0; uint8_t len = 0;
res = nrsec.SM2ExportPublicKey(index, &data[0], &len) == 0; std::vector<unsigned char> data(64, 0);
nrsec.Close();
if (!res)
{
return JNI_FALSE;
}
const char* outputPathStr = env->GetStringUTFChars(outputPath, 0); GpioControl::setCam3V3Enable(true);
FILE* file = fopen(outputPathStr, "wb"); GpioControl::setSpiPower(true);
env->ReleaseStringUTFChars(outputPath, outputPathStr);
if (file == NULL) NrsecPort nrsec;
bool res = nrsec.Open(path);
if (res)
{ {
return JNI_FALSE; res = nrsec.SM2ExportPublicKey(index, &data[0], &len) == 0;
nrsec.Close();
} }
int bytes = fwrite(&data[0], sizeof(unsigned char), len, file); GpioControl::setSpiPower(false);
GpioControl::setCam3V3Enable(false);
fclose(file); if (res)
return bytes == len ? JNI_TRUE : JNI_FALSE; {
const char* outputPathStr = env->GetStringUTFChars(outputPath, 0);
res = writeFile(outputPathStr, &data[0], len);
env->ReleaseStringUTFChars(outputPath, outputPathStr);
}
return res ? JNI_TRUE : JNI_FALSE;
#else
return JNI_FALSE;
#endif #endif
} }
@ -979,12 +996,16 @@ Java_com_xypower_mpapp_MicroPhotoService_exportPrivateFile(
#ifdef USING_NRSEC #ifdef USING_NRSEC
if (env->GetStringUTFLength(outputPath)<=0) if (env->GetStringUTFLength(outputPath) <= 0)
{ {
return JNI_FALSE; return JNI_FALSE;
} }
const char *path = NRSEC_PATH; const char *path = NRSEC_PATH;
GpioControl::setCam3V3Enable(true);
GpioControl::setSpiPower(true);
NrsecPort nrsec; NrsecPort nrsec;
if (!nrsec.Open(path)) if (!nrsec.Open(path))
{ {
@ -992,26 +1013,23 @@ Java_com_xypower_mpapp_MicroPhotoService_exportPrivateFile(
} }
bool res = false; bool res = false;
std::vector<unsigned char> data(64,0); std::vector<unsigned char> data(64, 0);
uint8_t len = 0; uint8_t len = 0;
res = nrsec.SM2ExportPrivateKey(index, &data[0], &len) == 0; res = nrsec.SM2ExportPrivateKey(index, &data[0], &len) == 0;
nrsec.Close(); nrsec.Close();
if (!res)
{
return JNI_FALSE;
}
const char* outputPathStr = env->GetStringUTFChars(outputPath, 0); GpioControl::setSpiPower(false);
FILE* file = fopen(outputPathStr, "wb"); GpioControl::setCam3V3Enable(false);
env->ReleaseStringUTFChars(outputPath, outputPathStr);
if (file == NULL) if (res)
{ {
return JNI_FALSE; const char* outputPathStr = env->GetStringUTFChars(outputPath, 0);
res = writeFile(outputPathStr, &data[0], len);
env->ReleaseStringUTFChars(outputPath, outputPathStr);
} }
int bytes = fwrite(&data[0], sizeof(unsigned char), len, file); return res ? JNI_TRUE : JNI_FALSE;
#else
fclose(file); return JNI_FALSE;
return bytes == len ? JNI_TRUE : JNI_FALSE;
#endif #endif
} }

@ -9,6 +9,8 @@ import android.os.Handler;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import com.xypower.common.FileUtils;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -51,7 +53,12 @@ public class CertActivity extends AppCompatActivity {
} }
} else if (TextUtils.equals(action, ACTION_GEN_KEYS)) { } else if (TextUtils.equals(action, ACTION_GEN_KEYS)) {
int index = intent.getIntExtra("index", 0); int index = intent.getIntExtra("index", 0);
MicroPhotoService.genKeys(index); boolean res = MicroPhotoService.genKeys(index);
String path = intent.getStringExtra("path");
if (!TextUtils.isEmpty(path)) {
FileUtils.ensureParentDirectoryExisted(path);
FileUtils.writeTextFile(path, res ? "1" : "0");
}
} else if (TextUtils.equals(action, ACTION_CERT_REQ)) { } else if (TextUtils.equals(action, ACTION_CERT_REQ)) {
int index = intent.getIntExtra("index", 0); int index = intent.getIntExtra("index", 0);
int type = intent.getIntExtra("type", 0); int type = intent.getIntExtra("type", 0);

@ -1,6 +1,7 @@
package com.xypower.common; package com.xypower.common;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -80,4 +81,34 @@ public class FileUtils {
} }
public static void writeTextFile(String path, String content) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(path);
byte[] bytes = content.getBytes();
fileOutputStream.write(bytes);
} catch (Exception ex) {
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (Exception ex) {
}
}
}
}
public static void ensureParentDirectoryExisted(String fileName) {
File file = new File(fileName);
try {
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
file.getParentFile().mkdirs();
}
} catch (Exception ex) {
}
}
} }

Loading…
Cancel
Save