2008-10-31 13:12:30 -04:00
|
|
|
package davmail.http;
|
|
|
|
|
2009-04-06 17:53:43 -04:00
|
|
|
import davmail.Settings;
|
2009-04-23 10:54:06 -04:00
|
|
|
import davmail.BundleMessage;
|
2009-04-06 17:53:43 -04:00
|
|
|
import davmail.ui.tray.DavGatewayTray;
|
|
|
|
import org.apache.commons.httpclient.HttpsURL;
|
|
|
|
import org.apache.commons.httpclient.params.HttpConnectionParams;
|
2008-10-31 13:12:30 -04:00
|
|
|
import org.apache.commons.httpclient.protocol.Protocol;
|
2009-04-01 18:06:53 -04:00
|
|
|
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
|
2009-04-06 17:53:43 -04:00
|
|
|
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
|
2008-10-31 13:12:30 -04:00
|
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
2009-04-06 17:53:43 -04:00
|
|
|
import java.net.MalformedURLException;
|
2008-10-31 13:12:30 -04:00
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manual Socket Factory.
|
|
|
|
* Let user choose to accept or reject certificate
|
|
|
|
*/
|
2009-04-06 17:53:43 -04:00
|
|
|
public class DavGatewaySSLProtocolSocketFactory implements SecureProtocolSocketFactory {
|
2008-10-31 13:12:30 -04:00
|
|
|
/**
|
|
|
|
* Register custom Socket Factory to let user accept or reject certificate
|
|
|
|
*/
|
|
|
|
public static void register() {
|
|
|
|
String urlString = Settings.getProperty("davmail.url");
|
|
|
|
try {
|
|
|
|
URL url = new URL(urlString);
|
|
|
|
String protocol = url.getProtocol();
|
|
|
|
if ("https".equals(protocol)) {
|
|
|
|
int port = url.getPort();
|
|
|
|
if (port < 0) {
|
2008-12-17 10:31:08 -05:00
|
|
|
port = HttpsURL.DEFAULT_PORT;
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
Protocol.registerProtocol(url.getProtocol(),
|
2009-04-01 18:06:53 -04:00
|
|
|
new Protocol(protocol, (ProtocolSocketFactory)new DavGatewaySSLProtocolSocketFactory(), port));
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
} catch (MalformedURLException e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.error(new BundleMessage("LOG_INVALID_URL", urlString));
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-16 18:20:30 -04:00
|
|
|
private SSLContext sslcontext ;
|
2008-10-31 13:12:30 -04:00
|
|
|
|
|
|
|
private SSLContext createSSLContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
|
|
|
|
SSLContext context = SSLContext.getInstance("SSL");
|
2008-11-03 05:56:57 -05:00
|
|
|
context.init(null, new TrustManager[]{new DavGatewayX509TrustManager()}, null);
|
2008-10-31 13:12:30 -04:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SSLContext getSSLContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
|
|
|
|
if (this.sslcontext == null) {
|
|
|
|
this.sslcontext = createSSLContext();
|
|
|
|
}
|
|
|
|
return this.sslcontext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException {
|
|
|
|
try {
|
2008-11-03 05:56:57 -05:00
|
|
|
return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (KeyManagementException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (KeyStoreException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-06 17:53:43 -04:00
|
|
|
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort, HttpConnectionParams params) throws IOException {
|
|
|
|
try {
|
|
|
|
return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
throw new IOException(e+" "+e.getMessage());
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
throw new IOException(e+" "+e.getMessage());
|
|
|
|
} catch (KeyStoreException e) {
|
|
|
|
throw new IOException(e+" "+e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-31 13:12:30 -04:00
|
|
|
|
|
|
|
public Socket createSocket(String host, int port) throws IOException {
|
|
|
|
try {
|
2008-11-03 05:56:57 -05:00
|
|
|
return getSSLContext().getSocketFactory().createSocket(host, port);
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (KeyManagementException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (KeyStoreException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
|
|
|
|
try {
|
2008-11-03 05:56:57 -05:00
|
|
|
return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (KeyManagementException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
} catch (KeyStoreException e) {
|
2008-11-03 05:56:57 -05:00
|
|
|
throw new IOException(e+" "+e.getMessage());
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|
|
|
|
}
|
2008-12-08 07:53:55 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* All instances of SSLProtocolSocketFactory are the same.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return ((obj != null) && obj.getClass().equals(this.getClass()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All instances of SSLProtocolSocketFactory have the same hash code.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return this.getClass().hashCode();
|
|
|
|
}
|
2008-10-31 13:12:30 -04:00
|
|
|
}
|