Skip to content

Commit 219d146

Browse files
author
Federico Fissore
committed
using https instead of http but ignoring self signed certificates
1 parent 2de631c commit 219d146

File tree

3 files changed

+352
-1
lines changed

3 files changed

+352
-1
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
* $HeadURL$
3+
* $Revision$
4+
* $Date$
5+
*
6+
* ====================================================================
7+
*
8+
* Licensed to the Apache Software Foundation (ASF) under one or more
9+
* contributor license agreements. See the NOTICE file distributed with
10+
* this work for additional information regarding copyright ownership.
11+
* The ASF licenses this file to You under the Apache License, Version 2.0
12+
* (the "License"); you may not use this file except in compliance with
13+
* the License. You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
* ====================================================================
23+
*
24+
* This software consists of voluntary contributions made by many
25+
* individuals on behalf of the Apache Software Foundation. For more
26+
* information on the Apache Software Foundation, please see
27+
* <http://www.apache.org/>.
28+
*
29+
*/
30+
31+
package processing.app.debug;
32+
33+
import java.io.IOException;
34+
import java.net.InetAddress;
35+
import java.net.InetSocketAddress;
36+
import java.net.Socket;
37+
import java.net.SocketAddress;
38+
import java.net.UnknownHostException;
39+
40+
import javax.net.SocketFactory;
41+
import javax.net.ssl.SSLContext;
42+
import javax.net.ssl.TrustManager;
43+
44+
import org.apache.commons.httpclient.ConnectTimeoutException;
45+
import org.apache.commons.httpclient.HttpClientError;
46+
import org.apache.commons.httpclient.params.HttpConnectionParams;
47+
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
48+
import org.apache.commons.logging.Log;
49+
import org.apache.commons.logging.LogFactory;
50+
51+
/**
52+
* <p>
53+
* EasySSLProtocolSocketFactory can be used to creats SSL {@link Socket}s
54+
* that accept self-signed certificates.
55+
* </p>
56+
* <p>
57+
* This socket factory SHOULD NOT be used for productive systems
58+
* due to security reasons, unless it is a concious decision and
59+
* you are perfectly aware of security implications of accepting
60+
* self-signed certificates
61+
* </p>
62+
*
63+
* <p>
64+
* Example of using custom protocol socket factory for a specific host:
65+
* <pre>
66+
* Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
67+
*
68+
* URI uri = new URI("https://localhost/", true);
69+
* // use relative url only
70+
* GetMethod httpget = new GetMethod(uri.getPathQuery());
71+
* HostConfiguration hc = new HostConfiguration();
72+
* hc.setHost(uri.getHost(), uri.getPort(), easyhttps);
73+
* HttpClient client = new HttpClient();
74+
* client.executeMethod(hc, httpget);
75+
* </pre>
76+
* </p>
77+
* <p>
78+
* Example of using custom protocol socket factory per default instead of the standard one:
79+
* <pre>
80+
* Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
81+
* Protocol.registerProtocol("https", easyhttps);
82+
*
83+
* HttpClient client = new HttpClient();
84+
* GetMethod httpget = new GetMethod("https://localhost/");
85+
* client.executeMethod(httpget);
86+
* </pre>
87+
* </p>
88+
*
89+
* @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
90+
*
91+
* <p>
92+
* DISCLAIMER: HttpClient developers DO NOT actively support this component.
93+
* The component is provided as a reference material, which may be inappropriate
94+
* for use without additional customization.
95+
* </p>
96+
*/
97+
98+
public class EasySSLProtocolSocketFactory implements SecureProtocolSocketFactory {
99+
100+
/** Log object for this class. */
101+
private static final Log LOG = LogFactory.getLog(EasySSLProtocolSocketFactory.class);
102+
103+
private SSLContext sslcontext = null;
104+
105+
/**
106+
* Constructor for EasySSLProtocolSocketFactory.
107+
*/
108+
public EasySSLProtocolSocketFactory() {
109+
super();
110+
}
111+
112+
private static SSLContext createEasySSLContext() {
113+
try {
114+
SSLContext context = SSLContext.getInstance("SSL");
115+
context.init(
116+
null,
117+
new TrustManager[] {new EasyX509TrustManager(null)},
118+
null);
119+
return context;
120+
} catch (Exception e) {
121+
LOG.error(e.getMessage(), e);
122+
throw new HttpClientError(e.toString());
123+
}
124+
}
125+
126+
private SSLContext getSSLContext() {
127+
if (this.sslcontext == null) {
128+
this.sslcontext = createEasySSLContext();
129+
}
130+
return this.sslcontext;
131+
}
132+
133+
/**
134+
* @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java.net.InetAddress,int)
135+
*/
136+
public Socket createSocket(
137+
String host,
138+
int port,
139+
InetAddress clientHost,
140+
int clientPort)
141+
throws IOException, UnknownHostException {
142+
143+
return getSSLContext().getSocketFactory().createSocket(
144+
host,
145+
port,
146+
clientHost,
147+
clientPort
148+
);
149+
}
150+
151+
/**
152+
* Attempts to get a new socket connection to the given host within the given time limit.
153+
* <p>
154+
* To circumvent the limitations of older JREs that do not support connect timeout a
155+
* controller thread is executed. The controller thread attempts to create a new socket
156+
* within the given limit of time. If socket constructor does not return until the
157+
* timeout expires, the controller terminates and throws an {@link ConnectTimeoutException}
158+
* </p>
159+
*
160+
* @param host the host name/IP
161+
* @param port the port on the host
162+
* @param clientHost the local host name/IP to bind the socket to
163+
* @param clientPort the port on the local machine
164+
* @param params {@link HttpConnectionParams Http connection parameters}
165+
*
166+
* @return Socket a new socket
167+
*
168+
* @throws IOException if an I/O error occurs while creating the socket
169+
* @throws UnknownHostException if the IP address of the host cannot be
170+
* determined
171+
*/
172+
public Socket createSocket(
173+
final String host,
174+
final int port,
175+
final InetAddress localAddress,
176+
final int localPort,
177+
final HttpConnectionParams params
178+
) throws IOException, UnknownHostException, ConnectTimeoutException {
179+
if (params == null) {
180+
throw new IllegalArgumentException("Parameters may not be null");
181+
}
182+
int timeout = params.getConnectionTimeout();
183+
SocketFactory socketfactory = getSSLContext().getSocketFactory();
184+
if (timeout == 0) {
185+
return socketfactory.createSocket(host, port, localAddress, localPort);
186+
} else {
187+
Socket socket = socketfactory.createSocket();
188+
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
189+
SocketAddress remoteaddr = new InetSocketAddress(host, port);
190+
socket.bind(localaddr);
191+
socket.connect(remoteaddr, timeout);
192+
return socket;
193+
}
194+
}
195+
196+
/**
197+
* @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
198+
*/
199+
public Socket createSocket(String host, int port)
200+
throws IOException, UnknownHostException {
201+
return getSSLContext().getSocketFactory().createSocket(
202+
host,
203+
port
204+
);
205+
}
206+
207+
/**
208+
* @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
209+
*/
210+
public Socket createSocket(
211+
Socket socket,
212+
String host,
213+
int port,
214+
boolean autoClose)
215+
throws IOException, UnknownHostException {
216+
return getSSLContext().getSocketFactory().createSocket(
217+
socket,
218+
host,
219+
port,
220+
autoClose
221+
);
222+
}
223+
224+
public boolean equals(Object obj) {
225+
return ((obj != null) && obj.getClass().equals(EasySSLProtocolSocketFactory.class));
226+
}
227+
228+
public int hashCode() {
229+
return EasySSLProtocolSocketFactory.class.hashCode();
230+
}
231+
232+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* ====================================================================
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership.
7+
* The ASF licenses this file to You under the Apache License, Version 2.0
8+
* (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ====================================================================
19+
*
20+
* This software consists of voluntary contributions made by many
21+
* individuals on behalf of the Apache Software Foundation. For more
22+
* information on the Apache Software Foundation, please see
23+
* <http://www.apache.org/>.
24+
*
25+
*/
26+
27+
package processing.app.debug;
28+
29+
import java.security.KeyStore;
30+
import java.security.KeyStoreException;
31+
import java.security.NoSuchAlgorithmException;
32+
import java.security.cert.CertificateException;
33+
import java.security.cert.X509Certificate;
34+
35+
import javax.net.ssl.TrustManagerFactory;
36+
import javax.net.ssl.TrustManager;
37+
import javax.net.ssl.X509TrustManager;
38+
import org.apache.commons.logging.Log;
39+
import org.apache.commons.logging.LogFactory;
40+
41+
/**
42+
* <p>
43+
* EasyX509TrustManager unlike default {@link X509TrustManager} accepts
44+
* self-signed certificates.
45+
* </p>
46+
* <p>
47+
* This trust manager SHOULD NOT be used for productive systems
48+
* due to security reasons, unless it is a concious decision and
49+
* you are perfectly aware of security implications of accepting
50+
* self-signed certificates
51+
* </p>
52+
*
53+
* @author <a href="mailto:adrian.sutton@ephox.com">Adrian Sutton</a>
54+
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
55+
*
56+
* <p>
57+
* DISCLAIMER: HttpClient developers DO NOT actively support this component.
58+
* The component is provided as a reference material, which may be inappropriate
59+
* for use without additional customization.
60+
* </p>
61+
*/
62+
63+
public class EasyX509TrustManager implements X509TrustManager
64+
{
65+
private X509TrustManager standardTrustManager = null;
66+
67+
/** Log object for this class. */
68+
private static final Log LOG = LogFactory.getLog(EasyX509TrustManager.class);
69+
70+
/**
71+
* Constructor for EasyX509TrustManager.
72+
*/
73+
public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
74+
super();
75+
TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
76+
factory.init(keystore);
77+
TrustManager[] trustmanagers = factory.getTrustManagers();
78+
if (trustmanagers.length == 0) {
79+
throw new NoSuchAlgorithmException("no trust manager found");
80+
}
81+
this.standardTrustManager = (X509TrustManager)trustmanagers[0];
82+
}
83+
84+
/**
85+
* @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],String authType)
86+
*/
87+
public void checkClientTrusted(X509Certificate[] certificates,String authType) throws CertificateException {
88+
standardTrustManager.checkClientTrusted(certificates,authType);
89+
}
90+
91+
/**
92+
* @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],String authType)
93+
*/
94+
public void checkServerTrusted(X509Certificate[] certificates,String authType) throws CertificateException {
95+
if ((certificates != null) && LOG.isDebugEnabled()) {
96+
LOG.debug("Server certificate chain:");
97+
for (int i = 0; i < certificates.length; i++) {
98+
LOG.debug("X509Certificate[" + i + "]=" + certificates[i]);
99+
}
100+
}
101+
if ((certificates != null) && (certificates.length == 1)) {
102+
certificates[0].checkValidity();
103+
} else {
104+
standardTrustManager.checkServerTrusted(certificates,authType);
105+
}
106+
}
107+
108+
/**
109+
* @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
110+
*/
111+
public X509Certificate[] getAcceptedIssuers() {
112+
return this.standardTrustManager.getAcceptedIssuers();
113+
}
114+
}

app/src/processing/app/debug/HttpUploader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
88
import org.apache.commons.httpclient.methods.multipart.Part;
99
import org.apache.commons.httpclient.methods.multipart.StringPart;
10+
import org.apache.commons.httpclient.protocol.Protocol;
1011
import processing.app.Base;
1112
import processing.app.Preferences;
1213
import processing.app.SerialException;
@@ -22,6 +23,10 @@ public class HttpUploader extends Uploader {
2223

2324
private static final Pattern IPV4_ADDRESS = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})");
2425

26+
static {
27+
Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443));
28+
}
29+
2530
private final HttpClient client;
2631
private final String ipAddress;
2732

@@ -84,7 +89,7 @@ public boolean uploadUsingPreferences(String buildPath, String className, boolea
8489
}
8590

8691
protected PostMethod newPostMethod() {
87-
return new PostMethod("http://" + ipAddress + "/upload");
92+
return new PostMethod("https://" + ipAddress + "/upload");
8893
}
8994

9095
@Override

0 commit comments

Comments
 (0)