实现批量导出证书请求

main
Matthew 1 year ago
parent 6990895b19
commit 48aeff77ef

@ -17,8 +17,12 @@ import com.dowse.devicesdk.DsDeviceSdk;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class CertActivity extends AppCompatActivity {
@ -29,6 +33,8 @@ public class CertActivity extends AppCompatActivity {
private final static String ACTION_GEN_KEYS = "gen_keys";
private final static String ACTION_CERT_REQ = "cert_req";
private final static String ACTION_CERT_REQ_BATCH = "batch_cert_req";
private static long AUTO_CLOSE_TIMEOUT = 200;
private Handler mHandler = null;
@ -71,10 +77,17 @@ public class CertActivity extends AppCompatActivity {
mHandler = new Handler();
{
Intent intent = getIntent();
final Intent intent = getIntent();
if (intent != null) {
Thread th = new Thread(new Runnable() {
@Override
public void run() {
handleCommand(intent);
}
});
th.start();
}
}
findViewById(R.id.btnGenKeys).setOnClickListener(new View.OnClickListener() {
@ -120,12 +133,22 @@ public class CertActivity extends AppCompatActivity {
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("index", 1);
intent.putExtra("action", ACTION_CERT_REQ);
intent.putExtra("subject", "C=CN,ST=SH,L=SH,O=SGCC,OU=NC,CN=123456_0006_XY2024ICQ1110006");
intent.putExtra("action", ACTION_CERT_REQ_BATCH);
intent.putExtra("start", 701);
intent.putExtra("end", 710);
intent.putExtra("type", 0);
intent.putExtra("sn", "XY2024ICQ111");
intent.putExtra("subject", "C=CN,ST=SH,L=SH,O=SGCC,CN=123456_%%IDX%%_XY2024ICQ111%%IDX%%,OU=NC");
intent.putExtra("pubKeyFile", "device_pub.key");
intent.putExtra("priKeyFile", "device_pri.key");
intent.putExtra("dnFile", "dn.txt");
File file = CertActivity.this.getFilesDir();
file = new File(file, "cert");
String absPath = file.getAbsolutePath();
intent.putExtra("path", "/data/data/com.xypower.secapp/data/pub.key");
intent.putExtra("path", absPath);
handleCommand(intent);
}
@ -152,7 +175,7 @@ public class CertActivity extends AppCompatActivity {
}
}
protected void handleCommand(Intent intent) {
protected void handleCommand(final Intent intent) {
final String action = intent.getStringExtra("action");
if (!TextUtils.isEmpty(action)) {
@ -224,6 +247,103 @@ public class CertActivity extends AppCompatActivity {
} else {
Log.i(TAG, "Failed to export private key");
}
} else if (TextUtils.equals(action, ACTION_CERT_REQ_BATCH)) {
int index = intent.getIntExtra("index", 0);
int type = intent.getIntExtra("type", 0);
int startIdx = intent.getIntExtra("start", 0);
int endIdx = intent.getIntExtra("end", 0);
String path = intent.getStringExtra("path");
String subjectTemplate = intent.getStringExtra("subject");
String snPrefix = intent.getStringExtra("sn");
String pubKeyFile = intent.getStringExtra("pubKeyFile");
String priKeyFile = intent.getStringExtra("priKeyFile");
String dnFile = intent.getStringExtra("dnFile");
// ensureDirectoryExisted(path);
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
String subject;
String sn;
String cn;
File snPath;
File f = null;
StringBuilder logs = new StringBuilder();
File certReqPath;
certReqPath = new File(file, "CertRequest");
if (!certReqPath.exists()) {
certReqPath.mkdirs();
}
for (int idx = startIdx; idx <= endIdx; idx++) {
String idxStr = String.format("%04d", idx);
sn = snPrefix + idxStr;
subject = subjectTemplate.replaceAll("%%IDX%%", idxStr);
int pos = subject.indexOf("CN=");
cn = subject.substring(pos + 3);
pos = cn.indexOf(",");
if (pos != -1) {
cn = cn.substring(0, pos);
}
snPath = new File(file, sn);
if (!snPath.exists()) {
snPath.mkdirs();
}
boolean res = MicroPhotoService.genKeys(index);
if (res) {
f = new File(snPath, pubKeyFile);
res = MicroPhotoService.exportPublicKeyFile(index, f.getAbsolutePath());
if (!res) {
logs.append(sn);
logs.append(" failed to export public key\r\n");
}
f = new File(snPath, priKeyFile);
res = MicroPhotoService.exportPrivateFile(index, f.getAbsolutePath());
if (!res) {
logs.append(sn);
logs.append(" failed to export private key\r\n");
}
f = new File(snPath, cn + ".csr");
res = MicroPhotoService.genCertRequest(index, type, subject, f.getAbsolutePath());
if (res) {
Path src = f.toPath();
f = new File(certReqPath, cn + ".csr");
try {
Files.copy(src, f.toPath());
} catch (Exception ex) {
}
f = new File(snPath, dnFile);
writeTextFile(f.getAbsolutePath(), subject);
} else {
logs.append(sn);
logs.append(" failed to export cert request\r\n");
}
} else {
logs.append(sn);
logs.append(" failed to generate keys\r\n");
}
f = new File(path);
f = new File(f, "progress.txt");
writeTextFile(f.getAbsolutePath(), Integer.toString(idx - startIdx + 1));
}
f = new File(path);
f = new File(f, "log.txt");
writeTextFile(f.getAbsolutePath(), logs.toString());
}
final Activity activity = this;

Loading…
Cancel
Save