Android Open Source - RestLib Custom S S L Socket Factory






From Project

Back to project page RestLib.

License

The source code is released under:

MIT License

If you think the Android project RestLib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package github.crazymumu.restlib.core;
/*w w w.  j av  a  2  s .com*/
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ssl.SSLSocketFactory;


/**
 * CustomSSLSocketFactory can be used to validate the identity of the HTTPS
 * server against a list of trusted certificates and to authenticate to the
 * HTTPS server using a private key.
 * 
 * @author Crazy MuMu
 */
public class CustomSSLSocketFactory extends SSLSocketFactory {
  private SSLContext sslContext = SSLContext.getInstance(SSLContextAlgorithm.TLS);

  public CustomSSLSocketFactory(KeyStore truststore) throws
    NoSuchAlgorithmException, KeyManagementException, KeyStoreException,
    UnrecoverableKeyException {

    super(truststore);

    TrustManager tm = new X509TrustManager() {
      public X509Certificate[] getAcceptedIssuers() {
        return null;
      }

      @Override
      public void checkClientTrusted(X509Certificate[] chain, String authType)
          throws java.security.cert.CertificateException {
      }

      @Override
      public void checkServerTrusted(X509Certificate[] chain, String authType)
          throws java.security.cert.CertificateException {
      }
    };

    sslContext.init(null, new TrustManager[] { tm }, null);
  }

  public CustomSSLSocketFactory(SSLContext context) throws
    KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
    UnrecoverableKeyException {
    super(null);
    sslContext = context;
  }

  @Override
  public Socket createSocket(Socket socket, String host, int port,
      boolean autoClose) throws IOException, UnknownHostException {
    return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
  }

  @Override
  public Socket createSocket() throws IOException {
    return sslContext.getSocketFactory().createSocket();
  }
}




Java Source Code List

github.crazymumu.restlib.RestRequest.java
github.crazymumu.restlib.RestResponse.java
github.crazymumu.restlib.RestResultReceiver.java
github.crazymumu.restlib.RestResult.java
github.crazymumu.restlib.RestService.java
github.crazymumu.restlib.core.CustomSSLSocketFactory.java
github.crazymumu.restlib.core.HttpsClient.java
github.crazymumu.restlib.core.SSLContextAlgorithm.java
github.crazymumu.restlib.http.RequestMethod.java
github.crazymumu.restlib.http.StatusCode.java
github.crazymumu.restlib.http.UserAgent.java
github.crazymumu.restlib.sample.MainActivity.java