refactor swithOverToTls stuff

This commit is contained in:
Daniel Gultsch 2014-12-30 01:17:11 +01:00
parent fb8737ed9f
commit 3c5d7d4f1b
1 changed files with 43 additions and 54 deletions

View File

@ -505,35 +505,25 @@ public class XmppConnection implements Runnable {
return getPreferences().getBoolean("enable_legacy_ssl", false); return getPreferences().getBoolean("enable_legacy_ssl", false);
} }
private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
IOException {
tagReader.readTag(); tagReader.readTag();
try { try {
final SSLContext sc = SSLContext.getInstance("TLS"); final SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, sc.init(null,new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},mXmppConnectionService.getRNG());
new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},
mXmppConnectionService.getRNG());
final SSLSocketFactory factory = sc.getSocketFactory(); final SSLSocketFactory factory = sc.getSocketFactory();
if (factory == null) {
throw new IOException("SSLSocketFactory was null");
}
final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier()); final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier());
final InetAddress address = socket == null ? null : socket.getInetAddress();
if (socket == null || socket.isClosed()) { if (factory == null || address == null || verifier == null) {
throw new IOException("socket null or closed"); throw new IOException("could not setup ssl");
}
final InetAddress address = socket.getInetAddress();
if (address == null) {
throw new IOException("socket address was null");
} }
final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true); final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true);
// Support all protocols except legacy SSL. if (sslSocket == null) {
// The min SDK version prevents us having to worry about SSLv2. In throw new IOException("could not initialize ssl socket");
// future, this may be true of SSLv3 as well. }
final String[] supportProtocols; final String[] supportProtocols;
if (enableLegacySSL()) { if (enableLegacySSL()) {
supportProtocols = sslSocket.getSupportedProtocols(); supportProtocols = sslSocket.getSupportedProtocols();
@ -546,9 +536,7 @@ public class XmppConnection implements Runnable {
} }
sslSocket.setEnabledProtocols(supportProtocols); sslSocket.setEnabledProtocols(supportProtocols);
if (verifier != null if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) {
&& !verifier.verify(account.getServer().getDomainpart(),
sslSocket.getSession())) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed"); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
disconnect(true); disconnect(true);
changeStatus(Account.State.SECURITY_ERROR); changeStatus(Account.State.SECURITY_ERROR);
@ -556,13 +544,14 @@ public class XmppConnection implements Runnable {
tagReader.setInputStream(sslSocket.getInputStream()); tagReader.setInputStream(sslSocket.getInputStream());
tagWriter.setOutputStream(sslSocket.getOutputStream()); tagWriter.setOutputStream(sslSocket.getOutputStream());
sendStartStream(); sendStartStream();
Log.d(Config.LOGTAG, account.getJid().toBareJid() Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": TLS connection established");
+ ": TLS connection established");
enabledEncryption = true; enabledEncryption = true;
processStream(tagReader.readTag()); processStream(tagReader.readTag());
sslSocket.close(); sslSocket.close();
} catch (final NoSuchAlgorithmException | KeyManagementException e1) { } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
e1.printStackTrace(); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
disconnect(true);
changeStatus(Account.State.SECURITY_ERROR);
} }
} }