package com.xypower.secapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Base64; import android.util.Log; import android.view.View; import android.widget.TextView; import com.dowse.devicesdk.DsDeviceSdk; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; public class CertActivity extends AppCompatActivity { private static String TAG = "CERTTAG"; private final static String ACTION_IMP_PUBKEY = "imp_pubkey"; private final static String ACTION_EXP_PUBKEY = "exp_pubkey"; private final static String ACTION_EXP_PRIKEY = "exp_prikey"; private final static String ACTION_GEN_KEYS = "gen_keys"; private final static String ACTION_CERT_REQ = "cert_req"; private static long AUTO_CLOSE_TIMEOUT = 200; private Handler mHandler = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cert); int res = 0; DsDeviceSdk dsSdk = DsDeviceSdk.getInstance(); dsSdk.init(); res = dsSdk.mcuInit(); res = dsSdk.mcuOpen(); boolean bres = dsSdk.mcuPowerOnCPR(); // String v = dsSdk.nrsecGetVersion(); // res = dsSdk.nrsecInit(); // res = dsSdk.nrsecOpen(); String version = MicroPhotoService.querySecVersion(); TextView textView = (TextView)findViewById(R.id.textView); textView.setText(R.string.nrsec_version + version); ActionBar actionBar = getSupportActionBar(); String text = getResources().getString(R.string.nrsec_version); actionBar.setTitle(actionBar.getTitle().toString() + " " + text + " " + version); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt = new Date(); text = format.format(dt); ((TextView) findViewById(R.id.textView)).setText(text); mHandler = new Handler(); { Intent intent = getIntent(); if (intent != null) { handleCommand(intent); } } findViewById(R.id.btnGenKeys).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.putExtra("index", 1); intent.putExtra("action", ACTION_GEN_KEYS); handleCommand(intent); } }); findViewById(R.id.btnExpPriKey).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.putExtra("index", 1); intent.putExtra("action", ACTION_EXP_PRIKEY); intent.putExtra("path", "/data/data/com.xypower.secapp/data/pri.key"); handleCommand(intent); } }); findViewById(R.id.btnExpPubKey).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.putExtra("index", 1); intent.putExtra("action", ACTION_EXP_PUBKEY); File file = CertActivity.this.getFilesDir(); String absPath = file.getAbsolutePath(); intent.putExtra("path", "/data/data/com.xypower.secapp/data/pub.key"); handleCommand(intent); } }); findViewById(R.id.btnGenCertReq).setOnClickListener(new View.OnClickListener() { @Override 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"); File file = CertActivity.this.getFilesDir(); String absPath = file.getAbsolutePath(); intent.putExtra("path", "/data/data/com.xypower.secapp/data/pub.key"); handleCommand(intent); } }); // } protected void handleCommand(Intent intent) { final String action = intent.getStringExtra("action"); if (!TextUtils.isEmpty(action)) { if (TextUtils.equals(action, ACTION_IMP_PUBKEY)) { String cert = intent.getStringExtra("cert"); String path = intent.getStringExtra("path"); int index = intent.getIntExtra("index", 1); if (!TextUtils.isEmpty(cert)) { // Import // String cert = intent.getStringExtra("md5"); byte[] content = Base64.decode(cert, Base64.DEFAULT); if (content != null) { MicroPhotoService.importPublicKey(index, content); } } else if (TextUtils.isEmpty(path)) { String md5 = intent.getStringExtra("md5"); File file = new File(path); if (file.exists() && file.isFile()) { MicroPhotoService.importPublicKeyFile(index, path, md5); } } } else if (TextUtils.equals(action, ACTION_GEN_KEYS)) { int index = intent.getIntExtra("index", 0); boolean res = MicroPhotoService.genKeys(index); } else if (TextUtils.equals(action, ACTION_CERT_REQ)) { int index = intent.getIntExtra("index", 0); int type = intent.getIntExtra("type", 0); String subject = intent.getStringExtra("subject"); String path = intent.getStringExtra("path"); ensureDirectoryExisted(path); boolean res = MicroPhotoService.genCertRequest(index, type, subject, path); if (res) { Log.i(TAG, "Succeeded to generate cert request"); } else { Log.i(TAG, "Failed to generate cert request"); } } else if (TextUtils.equals(action, ACTION_EXP_PUBKEY)) { String path = intent.getStringExtra("path"); File file = new File(path); File parentPath = file.getParentFile(); if (!parentPath.exists()) { parentPath.mkdirs(); } int index = intent.getIntExtra("index", 1); boolean res = MicroPhotoService.exportPublicKeyFile(index, path); if (res) { Log.i(TAG, "Succeeded to export public key"); } else { Log.i(TAG, "Failed to export public key"); } } else if (TextUtils.equals(action, ACTION_EXP_PRIKEY)) { String path = intent.getStringExtra("path"); File file = new File(path); File parentPath = file.getParentFile(); if (!parentPath.exists()) { parentPath.mkdirs(); } int index = intent.getIntExtra("index", 1); boolean res = MicroPhotoService.exportPrivateFile(index, path); if (res) { Log.i(TAG, "Succeeded to export private key"); } else { Log.i(TAG, "Failed to export private key"); } } final Activity activity = this; mHandler.postDelayed(new Runnable() { @Override public void run() { // activity.finish(); System.exit(0); } }, AUTO_CLOSE_TIMEOUT); } } protected void ensureDirectoryExisted(String fileName) { File file = new File(fileName); try { File parentFile = file.getParentFile(); if (!parentFile.exists()) { file.getParentFile().mkdirs(); } } catch (Exception ex) { } } @Override protected void onDestroy() { DsDeviceSdk dsSdk = DsDeviceSdk.getInstance(); int res = 0; boolean bres = dsSdk.mcuPowerOffCPR(); res = dsSdk.mcuClose(); res = dsSdk.mcuUnInit(); dsSdk.release(); super.onDestroy(); } }