11package com .loopj .android .http ;
22
3- import org .apache .http .conn .ssl .SSLSocketFactory ;
4-
3+ import java .io .BufferedInputStream ;
54import java .io .IOException ;
5+ import java .io .InputStream ;
66import java .net .Socket ;
7+ import java .net .UnknownHostException ;
78import java .security .KeyManagementException ;
89import java .security .KeyStore ;
910import java .security .KeyStoreException ;
1011import java .security .NoSuchAlgorithmException ;
1112import java .security .UnrecoverableKeyException ;
12-
13+ import java .security .cert .Certificate ;
14+ import java .security .cert .CertificateException ;
15+ import java .security .cert .CertificateFactory ;
16+ import java .security .cert .X509Certificate ;
17+
18+ import javax .net .ssl .HttpsURLConnection ;
1319import javax .net .ssl .SSLContext ;
1420import javax .net .ssl .TrustManager ;
1521import javax .net .ssl .X509TrustManager ;
22+
23+ import org .apache .http .HttpVersion ;
24+ import org .apache .http .conn .ClientConnectionManager ;
25+ import org .apache .http .conn .scheme .PlainSocketFactory ;
26+ import org .apache .http .conn .scheme .Scheme ;
27+ import org .apache .http .conn .scheme .SchemeRegistry ;
28+ import org .apache .http .conn .ssl .SSLSocketFactory ;
29+ import org .apache .http .impl .client .DefaultHttpClient ;
30+ import org .apache .http .impl .conn .tsccm .ThreadSafeClientConnManager ;
31+ import org .apache .http .params .BasicHttpParams ;
32+ import org .apache .http .params .HttpParams ;
33+ import org .apache .http .params .HttpProtocolParams ;
34+ import org .apache .http .protocol .HTTP ;
1635
1736/**
1837 * This file is introduced to fix HTTPS Post bug on API < ICS see
2241public class MySSLSocketFactory extends SSLSocketFactory {
2342 SSLContext sslContext = SSLContext .getInstance ("TLS" );
2443
25- public MySSLSocketFactory (KeyStore truststore )
26- throws NoSuchAlgorithmException , KeyManagementException ,
27- KeyStoreException , UnrecoverableKeyException {
44+ /**
45+ * Creates a new SSL Socket Factory with the given KeyStore.
46+ *
47+ * @param truststore A KeyStore to create the SSL Socket Factory in context of
48+ * @throws NoSuchAlgorithmException
49+ * @throws KeyManagementException
50+ * @throws KeyStoreException
51+ * @throws UnrecoverableKeyException
52+ */
53+ public MySSLSocketFactory (KeyStore truststore ) throws NoSuchAlgorithmException , KeyManagementException , KeyStoreException , UnrecoverableKeyException {
2854 super (truststore );
29-
30- TrustManager tm = new X509TrustManager () {
31- public java .security .cert .X509Certificate [] getAcceptedIssuers () {
32- return null ;
55+
56+ X509TrustManager tm = new X509TrustManager () {
57+ public void checkClientTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
3358 }
34-
35- @ Override
36- public void checkClientTrusted (
37- java .security .cert .X509Certificate [] chain , String authType )
38- throws java .security .cert .CertificateException {
59+
60+ public void checkServerTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
3961 }
40-
41- @ Override
42- public void checkServerTrusted (
43- java .security .cert .X509Certificate [] chain , String authType )
44- throws java .security .cert .CertificateException {
62+
63+ public X509Certificate [] getAcceptedIssuers () {
64+ return null ;
4565 }
4666 };
47- sslContext .init (null , new TrustManager []{tm }, null );
67+
68+ sslContext .init (null , new TrustManager [] { tm }, null );
4869 }
49-
50- @ Override
51- public Socket createSocket (Socket socket , String host , int port , boolean autoClose ) throws IOException {
70+
71+ public Socket createSocket (Socket socket , String host , int port , boolean autoClose ) throws IOException , UnknownHostException {
5272 return sslContext .getSocketFactory ().createSocket (socket , host , port , autoClose );
5373 }
54-
74+
5575 @ Override
5676 public Socket createSocket () throws IOException {
5777 return sslContext .getSocketFactory ().createSocket ();
78+ }
79+
80+ /**
81+ * Makes HttpsURLConnection trusts a set of certificates specified by the KeyStore
82+ */
83+ public void fixHttpsURLConnection () {
84+ HttpsURLConnection .setDefaultSSLSocketFactory (sslContext .getSocketFactory ());
5885 }
59-
60- public static KeyStore getKeystore () {
86+
87+ /**
88+ * Gets a KeyStore containing the Certificate
89+ *
90+ * @param cert InputStream of the Certificate
91+ * @return KeyStore
92+ */
93+ public static KeyStore getKeystoreOfCA (InputStream cert ) {
94+
95+ // Load CAs from an InputStream
96+ InputStream caInput = null ;
97+ Certificate ca = null ;
98+ try {
99+ CertificateFactory cf = CertificateFactory .getInstance ("X.509" );
100+ caInput = new BufferedInputStream (cert );
101+ ca = (Certificate ) cf .generateCertificate (caInput );
102+ } catch (CertificateException e1 ) {
103+ e1 .printStackTrace ();
104+ } finally {
105+ try {
106+ caInput .close ();
107+ } catch (IOException e ) {
108+ e .printStackTrace ();
109+ }
110+ }
111+
112+ // Create a KeyStore containing our trusted CAs
113+ String keyStoreType = KeyStore .getDefaultType ();
114+ KeyStore keyStore = null ;
115+ try {
116+ keyStore = KeyStore .getInstance (keyStoreType );
117+ keyStore .load (null , null );
118+ keyStore .setCertificateEntry ("ca" ,
119+ (java .security .cert .Certificate ) ca );
120+ } catch (Exception e ) {
121+ e .printStackTrace ();
122+ }
123+ return keyStore ;
124+ }
125+
126+ /**
127+ * Gets a Default KeyStore
128+ *
129+ * @return KeyStore
130+ */
131+ public static KeyStore getKeystore () {
61132 KeyStore trustStore = null ;
62133 try {
63134 trustStore = KeyStore .getInstance (KeyStore .getDefaultType ());
@@ -67,7 +138,12 @@ public static KeyStore getKeystore() {
67138 }
68139 return trustStore ;
69140 }
70-
141+
142+ /**
143+ * Returns a SSlSocketFactory which trusts all certificates
144+ *
145+ * @return
146+ */
71147 public static SSLSocketFactory getFixedSocketFactory () {
72148 SSLSocketFactory socketFactory ;
73149 try {
@@ -79,5 +155,31 @@ public static SSLSocketFactory getFixedSocketFactory() {
79155 }
80156 return socketFactory ;
81157 }
158+
159+ /**
160+ * Gets a DefaultHttpClient which trusts a set of certificates specified by the KeyStore
161+ *
162+ * @param keyStore
163+ * @return
164+ */
165+ public static DefaultHttpClient getNewHttpClient (KeyStore keyStore ) {
166+
167+ try {
168+ SSLSocketFactory sf = new MySSLSocketFactory (keyStore );
169+ SchemeRegistry registry = new SchemeRegistry ();
170+ registry .register (new Scheme ("http" , PlainSocketFactory .getSocketFactory (), 80 ));
171+ registry .register (new Scheme ("https" , sf , 443 ));
172+
173+ HttpParams params = new BasicHttpParams ();
174+ HttpProtocolParams .setVersion (params , HttpVersion .HTTP_1_1 );
175+ HttpProtocolParams .setContentCharset (params , HTTP .UTF_8 );
176+
177+ ClientConnectionManager ccm = new ThreadSafeClientConnManager (params , registry );
178+
179+ return new DefaultHttpClient (ccm , params );
180+ } catch (Exception e ) {
181+ return new DefaultHttpClient ();
182+ }
183+ }
82184
83- }
185+ }
0 commit comments