From dc3bcda323092bfb424e6f44e4d62436eee4fbb8 Mon Sep 17 00:00:00 2001 From: zibin yang Date: Sun, 21 Feb 2016 14:28:38 -0500 Subject: [PATCH 1/2] add android initial commit --- src/android/ClientCertificate.java | 80 ++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/android/ClientCertificate.java diff --git a/src/android/ClientCertificate.java b/src/android/ClientCertificate.java new file mode 100644 index 0000000..9ffb1a4 --- /dev/null +++ b/src/android/ClientCertificate.java @@ -0,0 +1,80 @@ + +package org.apache.cordova.plugin.clientcertificate; + +import android.annotation.TargetApi; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Build; +import android.preference.PreferenceManager; +import android.security.KeyChain; +import android.security.KeyChainAliasCallback; +import android.security.KeyChainException; +import android.util.Log; +import android.widget.Toast; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaWebView; +import org.apache.cordova.ICordovaClientCertRequest; + +import java.security.PrivateKey; +import java.security.cert.X509Certificate; +import java.util.concurrent.ExecutorService; + + +@TargetApi(Build.VERSION_CODES.LOLLIPOP) +public class ClientCertificate extends CordovaPlugin { + + + public String p12path = ""; + public String p12password = ""; + + + @Override + public Boolean shouldAllowBridgeAccess(String url) { + return super.shouldAllowBridgeAccess(url); + } + + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + @Override + public boolean onReceivedClientCertRequest(CordovaWebView view, ICordovaClientCertRequest request) { + try { + KeyStore keystore = KeyStore.getInstance("PKCS12"); + + InputStream astream = getAssets().open(p12path); + keystore.load(astream, p12password.toCharArray()); + astream.close(); + Enumeration e = keystore.aliases(); + if (e.hasMoreElements()) { + String ealias = (String) e.nextElement(); + PrivateKey key = (PrivateKey) keystore.getKey(ealias, p12password.toCharArray()); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + InputStream bstream = getAssets().open(p12path); + Collection c = cf.generateCertificates(bstream); + bstream.close(); + X509Certificate[] certs = (X509Certificate[])(c.toArray(new X509Certificate[c.size()])); + request.proceed(key,certs); + } else + { + request.ignore(); + } + + } catch (Exception ex) + { + request.ignore(); + } + return true; + } + + @Override + public boolean execute(String action, JSONArray a, CallbackContext c) throws JSONException { + if (action.equals("register")) + { + p12path = "www/" + a.getString(0); + p12password = a.getString(1); + return true; + } + return false; + } + + +} \ No newline at end of file From 98ec9a8c165d40a527f64949cbabb47a28db9a49 Mon Sep 17 00:00:00 2001 From: zibin yang Date: Tue, 23 Feb 2016 17:28:42 -0500 Subject: [PATCH 2/2] android read pk12 file --- plugin.xml | 10 ++++++++- src/android/ClientCertificate.java | 33 +++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/plugin.xml b/plugin.xml index 5e7bfd7..66b49d2 100644 --- a/plugin.xml +++ b/plugin.xml @@ -54,5 +54,13 @@ - + + + + + + + + + diff --git a/src/android/ClientCertificate.java b/src/android/ClientCertificate.java index 9ffb1a4..8825005 100644 --- a/src/android/ClientCertificate.java +++ b/src/android/ClientCertificate.java @@ -1,5 +1,5 @@ -package org.apache.cordova.plugin.clientcertificate; +package org.apache.cordova.plugin.clientcert; import android.annotation.TargetApi; import android.content.Context; @@ -14,11 +14,23 @@ import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaWebView; import org.apache.cordova.ICordovaClientCertRequest; - +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaInterface; +import org.json.JSONObject; +import org.json.JSONArray; +import org.json.JSONException; import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.util.concurrent.ExecutorService; - +import java.security.KeyStore; +import java.security.PrivateKey; +import java.security.cert.CertificateFactory; +import java.util.Collection; +import java.util.Arrays; +import java.util.Enumeration; +import java.io.FileInputStream; +import java.io.InputStream; @TargetApi(Build.VERSION_CODES.LOLLIPOP) public class ClientCertificate extends CordovaPlugin { @@ -32,7 +44,11 @@ public class ClientCertificate extends CordovaPlugin { public Boolean shouldAllowBridgeAccess(String url) { return super.shouldAllowBridgeAccess(url); } - + @Override + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + super.initialize(cordova, webView); + + } @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override @@ -40,18 +56,15 @@ public boolean onReceivedClientCertRequest(CordovaWebView view, ICordovaClientCe try { KeyStore keystore = KeyStore.getInstance("PKCS12"); - InputStream astream = getAssets().open(p12path); + InputStream astream = cordova.getActivity().getApplicationContext().getAssets().open(p12path); keystore.load(astream, p12password.toCharArray()); astream.close(); Enumeration e = keystore.aliases(); if (e.hasMoreElements()) { String ealias = (String) e.nextElement(); PrivateKey key = (PrivateKey) keystore.getKey(ealias, p12password.toCharArray()); - CertificateFactory cf = CertificateFactory.getInstance("X.509"); - InputStream bstream = getAssets().open(p12path); - Collection c = cf.generateCertificates(bstream); - bstream.close(); - X509Certificate[] certs = (X509Certificate[])(c.toArray(new X509Certificate[c.size()])); + java.security.cert.Certificate[] chain = keystore.getCertificateChain(ealias); + X509Certificate[] certs = Arrays.copyOf(chain, chain.length, X509Certificate[].class); request.proceed(key,certs); } else {