1
1
package com .loopj .android .http ;
2
2
3
- import org .apache .http .conn .ssl .SSLSocketFactory ;
4
-
3
+ import java .io .BufferedInputStream ;
5
4
import java .io .IOException ;
5
+ import java .io .InputStream ;
6
6
import java .net .Socket ;
7
+ import java .net .UnknownHostException ;
7
8
import java .security .KeyManagementException ;
8
9
import java .security .KeyStore ;
9
10
import java .security .KeyStoreException ;
10
11
import java .security .NoSuchAlgorithmException ;
11
12
import 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 ;
13
19
import javax .net .ssl .SSLContext ;
14
20
import javax .net .ssl .TrustManager ;
15
21
import 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 ;
16
35
17
36
/**
18
37
* This file is introduced to fix HTTPS Post bug on API < ICS see
22
41
public class MySSLSocketFactory extends SSLSocketFactory {
23
42
SSLContext sslContext = SSLContext .getInstance ("TLS" );
24
43
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 {
28
54
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 {
33
58
}
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 {
39
61
}
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 ;
45
65
}
46
66
};
47
- sslContext .init (null , new TrustManager []{tm }, null );
67
+
68
+ sslContext .init (null , new TrustManager [] { tm }, null );
48
69
}
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 {
52
72
return sslContext .getSocketFactory ().createSocket (socket , host , port , autoClose );
53
73
}
54
-
74
+
55
75
@ Override
56
76
public Socket createSocket () throws IOException {
57
77
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 ());
58
85
}
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 () {
61
132
KeyStore trustStore = null ;
62
133
try {
63
134
trustStore = KeyStore .getInstance (KeyStore .getDefaultType ());
@@ -67,7 +138,12 @@ public static KeyStore getKeystore() {
67
138
}
68
139
return trustStore ;
69
140
}
70
-
141
+
142
+ /**
143
+ * Returns a SSlSocketFactory which trusts all certificates
144
+ *
145
+ * @return
146
+ */
71
147
public static SSLSocketFactory getFixedSocketFactory () {
72
148
SSLSocketFactory socketFactory ;
73
149
try {
@@ -79,5 +155,31 @@ public static SSLSocketFactory getFixedSocketFactory() {
79
155
}
80
156
return socketFactory ;
81
157
}
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
+ }
82
184
83
- }
185
+ }
0 commit comments