优化加密处理的相关代码

hdrplus
Matthew 12 months ago
parent 6a63213ee8
commit 95c87c7467

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

@ -9,6 +9,8 @@ import android.os.Handler;
import android.text.TextUtils;
import android.util.Base64;
import com.xypower.common.FileUtils;
import java.io.File;
import java.nio.charset.StandardCharsets;
@ -51,7 +53,12 @@ public class CertActivity extends AppCompatActivity {
}
} else if (TextUtils.equals(action, ACTION_GEN_KEYS)) {
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)) {
int index = intent.getIntExtra("index", 0);
int type = intent.getIntExtra("type", 0);

@ -1,6 +1,7 @@
package com.xypower.common;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
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