added hostname verifier to httpconnection

This commit is contained in:
iNPUTmice 2014-10-22 00:00:01 +02:00
parent 40c74b432e
commit df2257aed6
2 changed files with 32 additions and 11 deletions

View File

@ -9,15 +9,20 @@ import java.net.URL;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import android.content.Intent; import android.content.Intent;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Downloadable; import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
@ -32,7 +37,6 @@ public class HttpConnection implements Downloadable {
private Message message; private Message message;
private DownloadableFile file; private DownloadableFile file;
private int mStatus = Downloadable.STATUS_UNKNOWN; private int mStatus = Downloadable.STATUS_UNKNOWN;
private boolean mAutostart = true;
public HttpConnection(HttpConnectionManager manager) { public HttpConnection(HttpConnectionManager manager) {
this.mHttpConnectionManager = manager; this.mHttpConnectionManager = manager;
@ -45,8 +49,7 @@ public class HttpConnection implements Downloadable {
if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) { if (this.mStatus == STATUS_OFFER_CHECK_FILESIZE) {
checkFileSize(true); checkFileSize(true);
} else { } else {
changeStatus(STATUS_DOWNLOADING); new Thread(new FileDownloader(true)).start();
new Thread(new FileDownloader()).start();
} }
return true; return true;
} else { } else {
@ -61,7 +64,6 @@ public class HttpConnection implements Downloadable {
mUrl = new URL(message.getBody()); mUrl = new URL(message.getBody());
this.file = mXmppConnectionService.getFileBackend().getFile( this.file = mXmppConnectionService.getFileBackend().getFile(
message, false); message, false);
this.mAutostart = true;
checkFileSize(false); checkFileSize(false);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
this.cancel(); this.cancel();
@ -69,7 +71,6 @@ public class HttpConnection implements Downloadable {
} }
private void checkFileSize(boolean interactive) { private void checkFileSize(boolean interactive) {
changeStatus(STATUS_CHECKING);
new Thread(new FileSizeChecker(interactive)).start(); new Thread(new FileSizeChecker(interactive)).start();
} }
@ -95,17 +96,26 @@ public class HttpConnection implements Downloadable {
private void setupTrustManager(HttpsURLConnection connection, private void setupTrustManager(HttpsURLConnection connection,
boolean interactive) { boolean interactive) {
X509TrustManager trustManager; X509TrustManager trustManager;
HostnameVerifier hostnameVerifier;
if (interactive) { if (interactive) {
trustManager = mXmppConnectionService.getMemorizingTrustManager(); trustManager = mXmppConnectionService.getMemorizingTrustManager();
hostnameVerifier = mXmppConnectionService
.getMemorizingTrustManager().wrapHostnameVerifier(
new StrictHostnameVerifier());
} else { } else {
trustManager = mXmppConnectionService.getMemorizingTrustManager() trustManager = mXmppConnectionService.getMemorizingTrustManager()
.getNonInteractive(); .getNonInteractive();
hostnameVerifier = mXmppConnectionService
.getMemorizingTrustManager()
.wrapHostnameVerifierNonInteractive(
new StrictHostnameVerifier());
} }
try { try {
SSLContext sc = SSLContext.getInstance("TLS"); SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new X509TrustManager[] { trustManager }, sc.init(null, new X509TrustManager[] { trustManager },
mXmppConnectionService.getRNG()); mXmppConnectionService.getRNG());
connection.setSSLSocketFactory(sc.getSocketFactory()); connection.setSSLSocketFactory(sc.getSocketFactory());
connection.setHostnameVerifier(hostnameVerifier);
} catch (KeyManagementException e) { } catch (KeyManagementException e) {
return; return;
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
@ -134,9 +144,8 @@ public class HttpConnection implements Downloadable {
return; return;
} }
file.setExpectedSize(size); file.setExpectedSize(size);
if (size <= mHttpConnectionManager.getAutoAcceptFileSize() if (size <= mHttpConnectionManager.getAutoAcceptFileSize()) {
&& mAutostart) { new Thread(new FileDownloader(interactive)).start();
start();
} else { } else {
changeStatus(STATUS_OFFER); changeStatus(STATUS_OFFER);
} }
@ -144,6 +153,7 @@ public class HttpConnection implements Downloadable {
private long retrieveFileSize() throws IOException, private long retrieveFileSize() throws IOException,
SSLHandshakeException { SSLHandshakeException {
changeStatus(STATUS_CHECKING);
HttpURLConnection connection = (HttpURLConnection) mUrl HttpURLConnection connection = (HttpURLConnection) mUrl
.openConnection(); .openConnection();
connection.setRequestMethod("HEAD"); connection.setRequestMethod("HEAD");
@ -166,23 +176,33 @@ public class HttpConnection implements Downloadable {
private class FileDownloader implements Runnable { private class FileDownloader implements Runnable {
private boolean interactive = false;
public FileDownloader(boolean interactive) {
this.interactive = interactive;
}
@Override @Override
public void run() { public void run() {
try { try {
changeStatus(STATUS_DOWNLOADING);
download(); download();
updateImageBounds(); updateImageBounds();
finish(); finish();
} catch (SSLHandshakeException e) {
changeStatus(STATUS_OFFER);
} catch (IOException e) { } catch (IOException e) {
cancel(); cancel();
} }
} }
private void download() throws IOException { private void download() throws SSLHandshakeException, IOException {
HttpURLConnection connection = (HttpURLConnection) mUrl HttpURLConnection connection = (HttpURLConnection) mUrl
.openConnection(); .openConnection();
if (connection instanceof HttpsURLConnection) { if (connection instanceof HttpsURLConnection) {
setupTrustManager((HttpsURLConnection) connection, true); setupTrustManager((HttpsURLConnection) connection, interactive);
} }
connection.connect();
BufferedInputStream is = new BufferedInputStream( BufferedInputStream is = new BufferedInputStream(
connection.getInputStream()); connection.getInputStream());
OutputStream os = file.createOutputStream(); OutputStream os = file.createOutputStream();

View File

@ -24,6 +24,7 @@ import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import de.duenndns.ssl.MemorizingTrustManager; import de.duenndns.ssl.MemorizingTrustManager;
@ -554,7 +555,7 @@ public class XmppConnection implements Runnable {
SSLSocketFactory factory = sc.getSocketFactory(); SSLSocketFactory factory = sc.getSocketFactory();
HostnameVerifier verifier = this.mMemorizingTrustManager HostnameVerifier verifier = this.mMemorizingTrustManager
.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()); .wrapHostnameVerifier(new StrictHostnameVerifier());
SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket, SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,
socket.getInetAddress().getHostAddress(), socket.getPort(), socket.getInetAddress().getHostAddress(), socket.getPort(),
true); true);