Commit e78559b1 authored by Guus der Kinderen's avatar Guus der Kinderen

OF-946: Should have single point of SSLContext instantiation

The code to get an instance of SSLContext is duplicated all over the codebase. Code gets more
maintainable by replacing all duplicate code blocks with one utility method.
This already fixes an issue where Clearspace integration used a SSL instead of TLS algorithm.
parent cc6be12e
...@@ -63,7 +63,7 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory { ...@@ -63,7 +63,7 @@ public class SSLProtocolSocketFactory implements SecureProtocolSocketFactory {
private SSLContext createSSLContext(String host) { private SSLContext createSSLContext(String host) {
try { try {
SSLContext context = SSLContext.getInstance("SSL"); final SSLContext context = SSLConfig.getSSLContext();
context.init( context.init(
null, null,
new TrustManager[] { new TrustManager[] {
......
...@@ -21,10 +21,12 @@ ...@@ -21,10 +21,12 @@
package org.jivesoftware.openfire.net; package org.jivesoftware.openfire.net;
import org.jivesoftware.openfire.keystore.*; import org.jivesoftware.openfire.keystore.*;
import org.jivesoftware.openfire.session.ConnectionSettings;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import java.io.*; import java.io.*;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -368,4 +370,11 @@ public class SSLConfig ...@@ -368,4 +370,11 @@ public class SSLConfig
return file.getCanonicalPath(); return file.getCanonicalPath();
} }
public static SSLContext getSSLContext() throws NoSuchAlgorithmException
{
// TODO: allow different algorithms for differetn connection types (eg client/server/bosh etc)
final String algorithm = JiveGlobals.getProperty( ConnectionSettings.Client.TLS_ALGORITHM, "TLS" );
return SSLContext.getInstance( algorithm );
}
} }
...@@ -105,8 +105,7 @@ public class SSLConfigSocketFactory ...@@ -105,8 +105,7 @@ public class SSLConfigSocketFactory
final IdentityStoreConfig identityStoreConfig = (IdentityStoreConfig) SSLConfig.getInstance().getStoreConfig( Purpose.SOCKETBASED_IDENTITYSTORE ); final IdentityStoreConfig identityStoreConfig = (IdentityStoreConfig) SSLConfig.getInstance().getStoreConfig( Purpose.SOCKETBASED_IDENTITYSTORE );
final TrustStoreConfig trustStoreConfig = (TrustStoreConfig) SSLConfig.getInstance().getStoreConfig( Purpose.SOCKETBASED_C2S_TRUSTSTORE ); final TrustStoreConfig trustStoreConfig = (TrustStoreConfig) SSLConfig.getInstance().getStoreConfig( Purpose.SOCKETBASED_C2S_TRUSTSTORE );
final String algorithm = JiveGlobals.getProperty( ConnectionSettings.Client.TLS_ALGORITHM, "TLS" ); final SSLContext context = SSLConfig.getSSLContext();
final SSLContext context = SSLContext.getInstance( algorithm );
context.init( identityStoreConfig.getKeyManagers(), trustStoreConfig.getTrustManagers(), new java.security.SecureRandom() ); context.init( identityStoreConfig.getKeyManagers(), trustStoreConfig.getTrustManagers(), new java.security.SecureRandom() );
return context.getServerSocketFactory(); return context.getServerSocketFactory();
...@@ -117,8 +116,7 @@ public class SSLConfigSocketFactory ...@@ -117,8 +116,7 @@ public class SSLConfigSocketFactory
final IdentityStoreConfig identityStoreConfig = (IdentityStoreConfig) SSLConfig.getInstance().getStoreConfig( Purpose.SOCKETBASED_IDENTITYSTORE ); final IdentityStoreConfig identityStoreConfig = (IdentityStoreConfig) SSLConfig.getInstance().getStoreConfig( Purpose.SOCKETBASED_IDENTITYSTORE );
final TrustStoreConfig trustStoreConfig = (TrustStoreConfig) SSLConfig.getInstance().getStoreConfig( Purpose.SOCKETBASED_S2S_TRUSTSTORE ); final TrustStoreConfig trustStoreConfig = (TrustStoreConfig) SSLConfig.getInstance().getStoreConfig( Purpose.SOCKETBASED_S2S_TRUSTSTORE );
final String algorithm = JiveGlobals.getProperty( ConnectionSettings.Client.TLS_ALGORITHM, "TLS" ); final SSLContext context = SSLConfig.getSSLContext();
final SSLContext context = SSLContext.getInstance( algorithm );
context.init( identityStoreConfig.getKeyManagers(), trustStoreConfig.getTrustManagers(), new java.security.SecureRandom() ); context.init( identityStoreConfig.getKeyManagers(), trustStoreConfig.getTrustManagers(), new java.security.SecureRandom() );
return context.getServerSocketFactory(); return context.getServerSocketFactory();
......
...@@ -97,8 +97,7 @@ public class TLSWrapper { ...@@ -97,8 +97,7 @@ public class TLSWrapper {
} }
final IdentityStoreConfig identityStoreConfig = (IdentityStoreConfig) sslConfig.getStoreConfig( Purpose.SOCKETBASED_IDENTITYSTORE ); final IdentityStoreConfig identityStoreConfig = (IdentityStoreConfig) sslConfig.getStoreConfig( Purpose.SOCKETBASED_IDENTITYSTORE );
final String algorithm = JiveGlobals.getProperty( ConnectionSettings.Client.TLS_ALGORITHM, "TLS" ); final SSLContext tlsContext = SSLConfig.getSSLContext();
final SSLContext tlsContext = SSLContext.getInstance(algorithm);
tlsContext.init( identityStoreConfig.getKeyManagers(), tm, null); tlsContext.init( identityStoreConfig.getKeyManagers(), tm, null);
/* /*
......
...@@ -397,8 +397,7 @@ public class NIOConnection implements Connection { ...@@ -397,8 +397,7 @@ public class NIOConnection implements Connection {
tm = storeConfig.getTrustManagers(); tm = storeConfig.getTrustManagers();
} }
String algorithm = JiveGlobals.getProperty(ConnectionSettings.Client.TLS_ALGORITHM, "TLS"); final SSLContext tlsContext = SSLConfig.getSSLContext();
SSLContext tlsContext = SSLContext.getInstance( algorithm );
final IdentityStoreConfig identityStoreConfig = (IdentityStoreConfig) sslConfig.getStoreConfig( Purpose.SOCKETBASED_IDENTITYSTORE ); final IdentityStoreConfig identityStoreConfig = (IdentityStoreConfig) sslConfig.getStoreConfig( Purpose.SOCKETBASED_IDENTITYSTORE );
tlsContext.init( identityStoreConfig.getKeyManagers(), tm, null); tlsContext.init( identityStoreConfig.getKeyManagers(), tm, null);
......
...@@ -40,6 +40,7 @@ import java.security.cert.X509Certificate; ...@@ -40,6 +40,7 @@ import java.security.cert.X509Certificate;
import java.util.Comparator; import java.util.Comparator;
import org.jivesoftware.openfire.net.SSLConfig;
import org.jivesoftware.openfire.session.ConnectionSettings; import org.jivesoftware.openfire.session.ConnectionSettings;
/** /**
...@@ -57,12 +58,11 @@ public class SimpleSSLSocketFactory extends SSLSocketFactory implements Comparat ...@@ -57,12 +58,11 @@ public class SimpleSSLSocketFactory extends SSLSocketFactory implements Comparat
public SimpleSSLSocketFactory() { public SimpleSSLSocketFactory() {
try { try {
String algorithm = JiveGlobals.getProperty( ConnectionSettings.Client.TLS_ALGORITHM, "TLS" ); final SSLContext sslContext = SSLConfig.getSSLContext();
SSLContext sslcontent = SSLContext.getInstance(algorithm); sslContext.init(null, // KeyManager not required
sslcontent.init(null, // KeyManager not required
new TrustManager[] { new DummyTrustManager() }, new TrustManager[] { new DummyTrustManager() },
new java.security.SecureRandom()); new java.security.SecureRandom());
factory = sslcontent.getSocketFactory(); factory = sslContext.getSocketFactory();
} }
catch (NoSuchAlgorithmException | KeyManagementException e) { catch (NoSuchAlgorithmException | KeyManagementException e) {
Log.error(e.getMessage(), e); Log.error(e.getMessage(), e);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment