package com.worklight.common.security;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import com.worklight.common.Logger;
import com.worklight.common.WLConfig;
import com.worklight.nativeandroid.common.WLUtils;
import com.worklight.wlclient.HttpClientManager;
import com.worklight.wlclient.WLRequest;
import com.worklight.wlclient.WLRequestListener;
import com.worklight.wlclient.api.WLClient;
import com.worklight.wlclient.api.WLFailResponse;
import com.worklight.wlclient.api.WLRequestOptions;
import com.worklight.wlclient.api.WLResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManagerFactory;
import org.json.JSONObject;

public class WLUserAuthManager extends WLCertManager
{
  private static final String CERTIFICATE_ALIAS = "com.worklight.userenrollment.certificate";
  private static String KEYSTORE_FILENAME = ".x509Keystore";
  private static final String PROVISIONING_ENTITY = "";
  private static WLUserAuthManager instance;
  private static char[] keyStorePassword = "worklight".toCharArray();
  private static Logger logger = Logger.getInstance("wl.userAuthManager");

  private WLUserAuthManager()
  {
    super(KEYSTORE_FILENAME, keyStorePassword);
  }

  private KeyStore getCertificateKeyStore()
    throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, CertificateException, IOException
  {
    KeyStore localKeyStore = loadKeyStore();
    KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()).init(localKeyStore, keyStorePassword);
    return localKeyStore;
  }

  public static WLUserAuthManager getInstance()
  {
    try
    {
      if (instance == null)
        instance = new WLUserAuthManager();
      WLUserAuthManager localWLUserAuthManager = instance;
      return localWLUserAuthManager;
    }
    finally
    {
    }
  }

  private KeyStore loadKeyStore()
    throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException
  {
    KeyStore localKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    File localFile = new File(this.context.getFilesDir().getAbsolutePath() + "/" + KEYSTORE_FILENAME);
    if (localFile.exists())
    {
      localKeyStore.load(new FileInputStream(localFile), keyStorePassword);
      return localKeyStore;
    }
    return null;
  }

  public JSONObject authenticateToTheServerWithCertificate()
    throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException
  {
    logger.debug("Establishing SSL connection with the server using the user certificate from the user certificate realm.");
    JSONObject localJSONObject = new JSONObject();
    HttpClientManager.setSSLSocketFactory(getCertificateKeyStore(), keyStorePassword);
    String str = WLConfig.getInstance().readWLPref("WL-Instance-Id");
    if (!WLUtils.isStringEmpty(str))
      WLClient.getInstance().addGlobalHeader("WL-Instance-Id", str);
    WLRequestOptions localWLRequestOptions = new WLRequestOptions();
    new WLRequest(new WLRequestListener()
    {
      public void onFailure(WLFailResponse paramAnonymousWLFailResponse)
      {
      }

      public void onSuccess(WLResponse paramAnonymousWLResponse)
      {
      }
    }
    , localWLRequestOptions, WLConfig.getInstance(), this.context).makeRequest("sslclientauth");
    return localJSONObject;
  }

  public void clearKeystore()
    throws KeyStoreException
  {
    clearKeystore("");
  }

  public String createSignedCSR(JSONObject paramJSONObject)
    throws Exception
  {
    return createSignedCSR(paramJSONObject, "");
  }

  public boolean doesValidCertificateExist()
  {
    while (true)
    {
      try
      {
        KeyStore.PrivateKeyEntry localPrivateKeyEntry = getPrivateKeyEntry("");
        if (localPrivateKeyEntry != null)
        {
          bool = true;
          int i = 1;
          if (bool);
          try
          {
            ((X509Certificate)localPrivateKeyEntry.getCertificate()).checkValidity();
            if (i == 0)
            {
              removeEntityKeyStoreValues("");
              logger.debug("doesValidCertificateExists = false (Certificate not yet valid or expired)");
              return false;
            }
          }
          catch (CertificateNotYetValidException localCertificateNotYetValidException)
          {
            logger.error("Certificate is not yet valid: " + localCertificateNotYetValidException.getMessage());
            i = 0;
            continue;
          }
          catch (CertificateExpiredException localCertificateExpiredException)
          {
            logger.error("Certificate has expired: " + localCertificateExpiredException.getMessage());
            i = 0;
            continue;
            logger.debug("doesValidCertificateExists = " + bool);
            return bool;
          }
        }
      }
      catch (Exception localException)
      {
        logger.error("Failed to determine the existence of certificate for device authentication with " + localException.getMessage(), localException);
        return false;
      }
      boolean bool = false;
    }
  }

  public KeyPair generateKeyPair(int paramInt)
    throws NoSuchAlgorithmException
  {
    return generateKeyPair("", paramInt);
  }

  protected String getAlias(String paramString)
  {
    WLConfig.createInstance(this.context);
    if (WLConfig.getInstance().isShareUserCert())
    {
      logger.debug("Using group support alias for user certificate authentication realm.");
      return "com.worklight.userenrollment.certificate:" + this.context.getApplicationInfo().uid;
    }
    return "com.worklight.userenrollment.certificate:" + this.context.getPackageName();
  }

  public void saveCertificate(String paramString1, String paramString2)
    throws Exception
  {
    logger.debug("Saving certificate for user certificate authentication realm...");
    saveCertificate("", paramString1, paramString2);
    logger.debug("Certificate saved for user certificate authentication realm.");
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.common.security.WLUserAuthManager
 * JD-Core Version:    0.6.2
 */