Skip to content

Commit 384f5fc

Browse files
Merge pull request #44 from taskadapter/bug43-ignore-invalid-ssl
#43 ignore invalid (e.g. self-issued) SSL certificates
2 parents 8e10a4d + db05c98 commit 384f5fc

File tree

5 files changed

+92
-80
lines changed

5 files changed

+92
-80
lines changed

pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<groupId>com.taskadapter</groupId>
1010
<artifactId>redmine-java-api</artifactId>
11-
<version>1.13</version>
11+
<version>1.14-SNAPSHOT</version>
1212

1313
<description>Free open-source Java API for Redmine and Chiliproject bug/task management systems.
1414
This project was originally a part of Task Adapter application (http://www.taskadapter.com)
@@ -95,7 +95,12 @@
9595
<dependency>
9696
<groupId>org.apache.httpcomponents</groupId>
9797
<artifactId>httpclient</artifactId>
98-
<version>4.1.2</version>
98+
<version>4.2</version>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.apache.httpcomponents</groupId>
102+
<artifactId>httpcore</artifactId>
103+
<version>4.2</version>
99104
</dependency>
100105
<dependency>
101106
<groupId>junit</groupId>

src/main/java/com/taskadapter/redmineapi/internal/comm/FakeSSLSocketFactory.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/main/java/com/taskadapter/redmineapi/internal/comm/HttpUtil.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package com.taskadapter.redmineapi.internal.comm;
22

3-
import java.io.IOException;
4-
import java.security.KeyManagementException;
5-
import java.security.KeyStore;
6-
import java.security.KeyStoreException;
7-
import java.security.NoSuchAlgorithmException;
8-
import java.security.UnrecoverableKeyException;
9-
import java.security.cert.CertificateException;
10-
3+
import com.taskadapter.redmineapi.RedmineConfigurationException;
4+
import com.taskadapter.redmineapi.internal.comm.naivessl.NaiveSSLFactory;
115
import org.apache.http.Header;
126
import org.apache.http.HttpEntity;
137
import org.apache.http.HttpHost;
@@ -20,13 +14,20 @@
2014
import org.apache.http.conn.scheme.SchemeRegistry;
2115
import org.apache.http.conn.ssl.SSLSocketFactory;
2216
import org.apache.http.impl.client.DefaultHttpClient;
17+
import org.apache.http.impl.conn.PoolingClientConnectionManager;
2318
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
2419
import org.apache.http.params.BasicHttpParams;
2520
import org.apache.http.params.HttpParams;
2621
import org.apache.http.params.HttpProtocolParams;
2722
import org.apache.http.protocol.HTTP;
2823
import org.apache.http.util.EntityUtils;
29-
import com.taskadapter.redmineapi.RedmineConfigurationException;
24+
25+
import java.io.IOException;
26+
import java.security.KeyManagementException;
27+
import java.security.KeyStoreException;
28+
import java.security.NoSuchAlgorithmException;
29+
import java.security.UnrecoverableKeyException;
30+
import java.security.cert.CertificateException;
3031

3132
class HttpUtil {
3233
public static DefaultHttpClient getNewHttpClient(
@@ -46,26 +47,20 @@ public static DefaultHttpClient getNewHttpClient(
4647
}
4748
}
4849

49-
@SuppressWarnings("deprecation")
50-
static ThreadSafeClientConnManager createConnectionManager(
50+
static PoolingClientConnectionManager createConnectionManager(
5151
int maxConnections) throws KeyStoreException,
5252
NoSuchAlgorithmException, CertificateException, IOException,
5353
KeyManagementException, UnrecoverableKeyException {
54-
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
55-
trustStore.load(null, null);
56-
SSLSocketFactory sf = new FakeSSLSocketFactory(trustStore);
57-
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
54+
SSLSocketFactory factory = NaiveSSLFactory.createNaiveSSLSocketFactory();
5855

59-
SchemeRegistry registry = new SchemeRegistry();
60-
registry.register(new Scheme("http", 80, PlainSocketFactory
61-
.getSocketFactory()));
62-
registry.register(new Scheme("https", 443, sf));
56+
SchemeRegistry registry = new SchemeRegistry();
57+
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
58+
registry.register(new Scheme("https", 443, factory));
6359

64-
ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(
65-
registry);
66-
ccm.setMaxTotal(maxConnections);
67-
ccm.setDefaultMaxPerRoute(maxConnections);
68-
return ccm;
60+
PoolingClientConnectionManager manager = new PoolingClientConnectionManager(registry);
61+
manager.setMaxTotal(maxConnections);
62+
manager.setDefaultMaxPerRoute(maxConnections);
63+
return manager;
6964
}
7065

7166
private static void configureProxy(DefaultHttpClient httpclient) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.taskadapter.redmineapi.internal.comm.naivessl;
2+
3+
import java.security.KeyManagementException;
4+
import java.security.NoSuchAlgorithmException;
5+
6+
import javax.net.ssl.SSLContext;
7+
import javax.net.ssl.TrustManager;
8+
import javax.net.ssl.X509TrustManager;
9+
10+
import org.apache.http.conn.ssl.SSLSocketFactory;
11+
12+
13+
/**
14+
* Create naive SSLSocket factory which will authorize any TSL/SSL host.
15+
*
16+
* @author Bartosz Firyn (SarXos)
17+
*/
18+
public class NaiveSSLFactory {
19+
20+
/**
21+
* @return Return naive SSL socket factory (authorize any SSL/TSL host)
22+
*/
23+
public static SSLSocketFactory createNaiveSSLSocketFactory() {
24+
X509TrustManager manager = new NaiveX509TrustManager();
25+
SSLContext sslcontext = null;
26+
try {
27+
TrustManager[] managers = new TrustManager[] { manager };
28+
sslcontext = SSLContext.getInstance("SSL");
29+
sslcontext.init(null, managers, null);
30+
} catch (NoSuchAlgorithmException e) {
31+
e.printStackTrace();
32+
} catch (KeyManagementException e) {
33+
e.printStackTrace();
34+
}
35+
return new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
36+
}
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.taskadapter.redmineapi.internal.comm.naivessl;
2+
3+
import java.security.cert.CertificateException;
4+
import java.security.cert.X509Certificate;
5+
6+
import javax.net.ssl.X509TrustManager;
7+
8+
9+
/**
10+
* The goal of this trust manager is to do nothing - it will authorize
11+
* any TSL/SSL secure connection.
12+
*
13+
* @author Bartosz Firyn (SarXos)
14+
*/
15+
public class NaiveX509TrustManager implements X509TrustManager {
16+
17+
@Override
18+
public void checkClientTrusted(X509Certificate[] certs, String str) throws CertificateException {
19+
}
20+
21+
@Override
22+
public void checkServerTrusted(X509Certificate[] certs, String str) throws CertificateException {
23+
}
24+
25+
@Override
26+
public X509Certificate[] getAcceptedIssuers() {
27+
return null;
28+
}
29+
}

0 commit comments

Comments
 (0)