Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android support #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,13 @@
<source-file src="src/ios/QNSURLSessionDemux.m" />

</platform>

<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="ClientCertificate">
<param name="android-package" value="org.apache.cordova.plugin.clientcert.ClientCertificate"/>
<param name="onload" value="true" />
</feature>
</config-file>
<source-file src="src/android/ClientCertificate.java" target-dir="src/org/apache/cordova/plugin/"/>
</platform>
</plugin>
93 changes: 93 additions & 0 deletions src/android/ClientCertificate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

package org.apache.cordova.plugin.clientcert;

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 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 {


public String p12path = "";
public String p12password = "";


@Override
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
public boolean onReceivedClientCertRequest(CordovaWebView view, ICordovaClientCertRequest request) {
try {
KeyStore keystore = KeyStore.getInstance("PKCS12");

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());
java.security.cert.Certificate[] chain = keystore.getCertificateChain(ealias);
X509Certificate[] certs = Arrays.copyOf(chain, chain.length, X509Certificate[].class);
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;
}


}