// Path: src/android/com/silkimen/http/KeyChainKeyManager.java
// public class KeyChainKeyManager extends X509ExtendedKeyManager {
//   private final String alias;
//   private final X509Certificate[] chain;
//   private final PrivateKey key;
// 
//   public KeyChainKeyManager(String alias, PrivateKey key, X509Certificate[] chain) {
//     this.alias = alias;
//     this.key = key;
//     this.chain = chain;
//   }
// 
//   @Override
//   public String chooseClientAlias(String[] keyTypes, Principal[] issuers, Socket socket) {
//     return this.alias;
//   }
// 
//   @Override
//   public X509Certificate[] getCertificateChain(String alias) {
//     return chain;
//   }
// 
//   @Override
//   public PrivateKey getPrivateKey(String alias) {
//     return key;
//   }
// 
//   @Override
//   public final String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
//     // not a client SSLSocket callback
//     throw new UnsupportedOperationException();
//   }
// 
//   @Override
//   public final String[] getClientAliases(String keyType, Principal[] issuers) {
//     // not a client SSLSocket callback
//     throw new UnsupportedOperationException();
//   }
// 
//   @Override
//   public final String[] getServerAliases(String keyType, Principal[] issuers) {
//     // not a client SSLSocket callback
//     throw new UnsupportedOperationException();
//   }
// }
// 
// Path: src/android/com/silkimen/http/TLSConfiguration.java
// public class TLSConfiguration {
//   private TrustManager[] trustManagers = null;
//   private KeyManager[] keyManagers = null;
//   private HostnameVerifier hostnameVerifier = null;
//   private String[] blacklistedProtocols = {};
// 
//   private SSLSocketFactory socketFactory;
// 
//   public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
//     this.hostnameVerifier = hostnameVerifier;
//   }
// 
//   public void setKeyManagers(KeyManager[] keyManagers) {
//     this.keyManagers = keyManagers;
//     this.socketFactory = null;
//   }
// 
//   public void setTrustManagers(TrustManager[] trustManagers) {
//     this.trustManagers = trustManagers;
//     this.socketFactory = null;
//   }
// 
//   public void setBlacklistedProtocols(String[] protocols) {
//     this.blacklistedProtocols = protocols;
//     this.socketFactory = null;
//   }
// 
//   public HostnameVerifier getHostnameVerifier() {
//     return this.hostnameVerifier;
//   }
// 
//   public SSLSocketFactory getTLSSocketFactory() throws IOException {
//     if (this.socketFactory != null) {
//       return this.socketFactory;
//     }
// 
//     try {
//       SSLContext context = SSLContext.getInstance("TLS");
// 
//       context.init(this.keyManagers, this.trustManagers, new SecureRandom());
//       this.socketFactory = new TLSSocketFactory(context, this.blacklistedProtocols);
// 
//       return this.socketFactory;
//     } catch (GeneralSecurityException e) {
//       IOException ioException = new IOException("Security exception occured while configuring TLS context");
//       ioException.initCause(e);
//       throw ioException;
//     }
//   }
// }


import android.app.Activity;
import android.content.Context;
import android.security.KeyChain;
import android.security.KeyChainAliasCallback;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import org.apache.cordova.CallbackContext;
import com.silkimen.http.KeyChainKeyManager;
import com.silkimen.http.TLSConfiguration;
  private Context context;
  private TLSConfiguration tlsConfiguration;
  private CallbackContext callbackContext;

  public CordovaClientAuth(final String mode, final String aliasString, final byte[] rawPkcs,
      final String pkcsPassword, final Activity activity, final Context context, final TLSConfiguration configContainer,
      final CallbackContext callbackContext) {

    this.mode = mode;
    this.aliasString = aliasString;
    this.rawPkcs = rawPkcs;
    this.pkcsPassword = pkcsPassword;
    this.activity = activity;
    this.tlsConfiguration = configContainer;
    this.context = context;
    this.callbackContext = callbackContext;
  }

  @Override
  public void run() {
    if ("systemstore".equals(this.mode)) {
      this.loadFromSystemStore();
    } else if ("buffer".equals(this.mode)) {
      this.loadFromBuffer();
    } else {
      this.disableClientAuth();
    }
  }

  private void loadFromSystemStore() {
// Path: src/android/com/silkimen/http/KeyChainKeyManager.java
// public class KeyChainKeyManager extends X509ExtendedKeyManager {
//   private final String alias;
//   private final X509Certificate[] chain;
//   private final PrivateKey key;
// 
//   public KeyChainKeyManager(String alias, PrivateKey key, X509Certificate[] chain) {
//     this.alias = alias;
//     this.key = key;
//     this.chain = chain;
//   }
// 
//   @Override
//   public String chooseClientAlias(String[] keyTypes, Principal[] issuers, Socket socket) {
//     return this.alias;
//   }
// 
//   @Override
//   public X509Certificate[] getCertificateChain(String alias) {
//     return chain;
//   }
// 
//   @Override
//   public PrivateKey getPrivateKey(String alias) {
//     return key;
//   }
// 
//   @Override
//   public final String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
//     // not a client SSLSocket callback
//     throw new UnsupportedOperationException();
//   }
// 
//   @Override
//   public final String[] getClientAliases(String keyType, Principal[] issuers) {
//     // not a client SSLSocket callback
//     throw new UnsupportedOperationException();
//   }
// 
//   @Override
//   public final String[] getServerAliases(String keyType, Principal[] issuers) {
//     // not a client SSLSocket callback
//     throw new UnsupportedOperationException();
//   }
// }
// 
// Path: src/android/com/silkimen/http/TLSConfiguration.java
// public class TLSConfiguration {
//   private TrustManager[] trustManagers = null;
//   private KeyManager[] keyManagers = null;
//   private HostnameVerifier hostnameVerifier = null;
//   private String[] blacklistedProtocols = {};
// 
//   private SSLSocketFactory socketFactory;
// 
//   public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
//     this.hostnameVerifier = hostnameVerifier;
//   }
// 
//   public void setKeyManagers(KeyManager[] keyManagers) {
//     this.keyManagers = keyManagers;
//     this.socketFactory = null;
//   }
// 
//   public void setTrustManagers(TrustManager[] trustManagers) {
//     this.trustManagers = trustManagers;
//     this.socketFactory = null;
//   }
// 
//   public void setBlacklistedProtocols(String[] protocols) {
//     this.blacklistedProtocols = protocols;
//     this.socketFactory = null;
//   }
// 
//   public HostnameVerifier getHostnameVerifier() {
//     return this.hostnameVerifier;
//   }
// 
//   public SSLSocketFactory getTLSSocketFactory() throws IOException {
//     if (this.socketFactory != null) {
//       return this.socketFactory;
//     }
// 
//     try {
//       SSLContext context = SSLContext.getInstance("TLS");
// 
//       context.init(this.keyManagers, this.trustManagers, new SecureRandom());
//       this.socketFactory = new TLSSocketFactory(context, this.blacklistedProtocols);
// 
//       return this.socketFactory;
//     } catch (GeneralSecurityException e) {
//       IOException ioException = new IOException("Security exception occured while configuring TLS context");
//       ioException.initCause(e);
//       throw ioException;
//     }
//   }
// }


// Path: src/android/com/silkimen/cordovahttp/CordovaClientAuth.java
import android.app.Activity;
import android.content.Context;
import android.security.KeyChain;
import android.security.KeyChainAliasCallback;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import org.apache.cordova.CallbackContext;
import com.silkimen.http.KeyChainKeyManager;
import com.silkimen.http.TLSConfiguration;

  private Context context;
  private TLSConfiguration tlsConfiguration;
  private CallbackContext callbackContext;

  public CordovaClientAuth(final String mode, final String aliasString, final byte[] rawPkcs,
      final String pkcsPassword, final Activity activity, final Context context, final TLSConfiguration configContainer,
      final CallbackContext callbackContext) {

    this.mode = mode;
    this.aliasString = aliasString;
    this.rawPkcs = rawPkcs;
    this.pkcsPassword = pkcsPassword;
    this.activity = activity;
    this.tlsConfiguration = configContainer;
    this.context = context;
    this.callbackContext = callbackContext;
  }

  @Override
  public void run() {
    if ("systemstore".equals(this.mode)) {
      this.loadFromSystemStore();
    } else if ("buffer".equals(this.mode)) {
      this.loadFromBuffer();
    } else {
      this.disableClientAuth();
    }
  }

  private void loadFromSystemStore() {
    if (this.aliasString == null) {