Created
November 28, 2012 22:39
-
-
Save treinberger/4165243 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file was auto-generated by the Titanium Module SDK helper for Android | |
* Appcelerator Titanium Mobile | |
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved. | |
* Licensed under the terms of the Apache Public License | |
* Please see the LICENSE included with this distribution for details. | |
* | |
*/ | |
package com.certtest; | |
import java.io.InputStream; | |
import java.security.KeyStore; | |
import java.util.ArrayList; | |
import javax.net.ssl.KeyManager; | |
import javax.net.ssl.X509KeyManager; | |
import javax.net.ssl.X509TrustManager; | |
import org.appcelerator.kroll.KrollDict; | |
import org.appcelerator.kroll.KrollModule; | |
import org.appcelerator.kroll.KrollProxy; | |
import org.appcelerator.kroll.annotations.Kroll; | |
import org.appcelerator.titanium.TiApplication; | |
import org.appcelerator.titanium.TiFileProxy; | |
import org.appcelerator.titanium.io.TiFile; | |
import org.appcelerator.titanium.util.TiFileHelper; | |
import org.appcelerator.kroll.common.Log; | |
import org.appcelerator.kroll.common.TiConfig; | |
import javax.net.ssl.KeyManagerFactory; | |
import ti.modules.titanium.network.HTTPClientProxy; | |
@Kroll.module(name = "Certtest2", id = "com.certtest") | |
public class Certtest2Module extends KrollModule { | |
// Standard Debugging variables | |
private static final String LCAT = "Certtest2Module"; | |
private static final boolean DBG = TiConfig.LOGD; | |
private ArrayList<KeyStore> certKeystores; | |
// You can define constants with @Kroll.constant, for example: | |
// @Kroll.constant public static final String EXTERNAL_NAME = value; | |
public Certtest2Module() { | |
super(); | |
} | |
@Kroll.onAppCreate | |
public static void onAppCreate(TiApplication app) { | |
// Log.d(LCAT, "inside onAppCreate"); | |
} | |
@Kroll.method | |
public KrollDict loadP12Cert(HTTPClientProxy client, TiFileProxy certFile) { | |
return loadP12Cert(client, certFile, null); | |
} | |
@Kroll.method | |
public KrollDict loadP12Cert(HTTPClientProxy client, TiFileProxy certFile, | |
String password) { | |
KrollDict ret = new KrollDict(); | |
char[] thePass = null; | |
if (password != null) { | |
thePass = password.toCharArray(); | |
} | |
try { | |
KeyStore theStore = KeyStore.getInstance("pkcs12"); | |
String fullPath = certFile.resolve(); | |
Log.i(LCAT, "Loading cert from " + fullPath); | |
TiFileHelper tfh = TiFileHelper.getInstance(); | |
InputStream stream = tfh.openInputStream(fullPath, false); | |
theStore.load(stream, thePass); | |
certKeystores = new ArrayList<KeyStore>(); | |
certKeystores.add(theStore); | |
Log.i(LCAT, "Loaded cert from " + fullPath); | |
ret.put("Status", new Integer(0)); | |
client.addTrustManager(new AdditionalKeyStoresTrustManager( | |
certKeystores)); | |
// final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | |
// kmf.init(theStore, thePass); | |
// | |
// Log.i(LCAT, "getKeyManagers.length = " + kmf.getKeyManagers().length); | |
// KeyManager keyManager = new AdditionalKeyStoresKeyManager(kmf.getKeyManagers()); | |
// | |
// client.addKeyManager(keyManager); | |
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | |
kmf.init(theStore, thePass); | |
for (KeyManager km : kmf.getKeyManagers()) | |
if (km instanceof X509KeyManager) { | |
client.addKeyManager((X509KeyManager) km); | |
Log.i(LCAT, "added KeyManager to httpClient."); | |
} | |
} catch (Exception e) { | |
Log.e(LCAT, "Error loading certficate " + e.getMessage()); | |
ret.put("Status", new Integer(1)); | |
ret.put("Error", e.getMessage()); | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment