How can I use SecurityMode.enabled? (aSmack 4.0.0)

ConnectionConfiguration config = new ConnectionConfiguration(

“192.168.8.158”, 5222);

XMPPConnection connection = new XMPPTCPConnection(config);

try {

connection.connect(); // Throw SmackException$NoReponseException here.

} catch (SmackException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (XMPPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

The openfire just use the default settings, and I can use Spark to connect the server.

But if I add the code like this:

config.setSecurityMode(SecurityMode.disabled);

the app connected to the server.But why can’t I useSecurityMode.enabled?

Download DummySSLSocketFactory.java

Use port 5223 (insure it’s enabled)

XMPPConnection mConnection;

ConnectionConfiguration config = new ConnectionConfiguration(HOST, PORT, SERVECE_NAME);

config.setSocketFactory(new DummySSLSocketFactory());

config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);

config.setCompressionEnabled(true);

mConnection = new XMPPTCPConnection(config);

try {

connection.connect();

} catch (Exception e) {

// connection failure

}

Sorry I didn’t try to use secured 5222.

But this’s not suggested in the newest smack, because it uses the old SSL that is not a standard method of the XMPP. Maybe the newest aSamck has some bugs to use SSL.

Anyway Thanks for your help!

I’m not really sure if this will work in new version of smack but you can try.

.
public void setSecurityMode(ConnectionConfiguration.SecurityMode securityMode)

Sets the TLS security mode used when making the connection. By default, the mode is ConnectionConfiguration.SecurityMode.enabled.

Parameters:

securityMode - the security mode.

It won’t work because there’re only three SecurityModes enabled/disabled/required in the new version.

This is how I did it:

public class ContextService {     public static SSLContext createContext() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        KeyStore trustStore;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            trustStore = KeyStore.getInstance("AndroidCAStore");
        } else {
            trustStore = KeyStore.getInstance("BKS");
        }         TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
        return sslContext;
    }
}
ConnectionConfiguration configuration = new ConnectionConfiguration(ExtraConstants.MSG_HOST, PORT, ExtraConstants.SERVICE);
configuration.setSecurityMode(SecurityMode.required);
configuration.setCustomSSLContext(ContextService.createContext());

I have a certificate that is signed by GoDaddy so it’s a valid one, no need to use a self signed.

Hope that helps, I’m not 100% sure if that’s the right way to do it, but after a lot of research it seems to be.

Thanks for your reply, the project is paused, so I can’t try it now.

I am using 4.0.6 asmack. If i try to enable securutymode and my snippet is,

config = new ConnectionConfiguration(DOMAIN, SERVER_PORT, DOMAIN);

config.setSendPresence(true);

config.setReconnectionAllowed(true);

config.setDebuggerEnabled(true);

config.setSecurityMode(SecurityMode.required);

Roster.setDefaultSubscriptionMode(SubscriptionMode.accept_all);

KeyStore trustStore = null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {

try {

trustStore = KeyStore.getInstance(“AndroidCAStore”);

} catch (KeyStoreException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

} else {

try {

trustStore = KeyStore.getInstance(“BKS”);

} catch (KeyStoreException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

try{

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

trustManagerFactory.init(trustStore);

SSLContext sslContext = SSLContext.getInstance(“TLS”);

sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());

config.setCustomSSLContext(sslContext);

}catch(Exception e){

}

I am getting exception like,

01-23 15:07:15.906: D/SMACK(8231): SENT (0): <stream:stream to=“framily.in” xmlns=“jabber:client” xmlns:stream=“http://etherx.jabber.org/streams” version=“1.0”>

01-23 15:07:16.246: D/SMACK(8231): RCV (0): <?xml version='1.0'?><stream:stream xmlns=‘jabber:client’ xmlns:stream=‘http://etherx.jabber.org/streams’ id=‘247861326’ from=‘framily.in’ version=‘1.0’ xml:lang=‘en’>

01-23 15:07:16.246: D/SMACK(8231): RCV (0): stream:featureszlibPLAINDIGEST-MD5SCRAM-SHA-1<register xmlns='http://jabber.org/features/iq-register’/></stream:features>

01-23 15:07:16.246: D/SMACK(8231): SENT (0):

01-23 15:07:16.707: D/SMACK(8231): RCV (0):

01-23 15:07:17.097: D/SMACK(8231): SENT (0): </stream:stream>

01-23 15:07:17.117: W/PacketWriter(8231): Exception writing closing stream element

01-23 15:07:17.117: W/PacketWriter(8231): java.net.SocketException: Socket closed

01-23 15:07:17.117: W/PacketWriter(8231): at libcore.io.Posix.sendtoBytes(Native Method)

01-23 15:07:17.117: W/PacketWriter(8231): at libcore.io.Posix.sendto(Posix.java:151)

01-23 15:07:17.117: W/PacketWriter(8231): at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)

01-23 15:07:17.117: W/PacketWriter(8231): at libcore.io.IoBridge.sendto(IoBridge.java:473)

01-23 15:07:17.117: W/PacketWriter(8231): at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)

01-23 15:07:17.117: W/PacketWriter(8231): at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)

01-23 15:07:17.117: W/PacketWriter(8231): at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269 )

01-23 15:07:17.117: W/PacketWriter(8231): at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)

01-23 15:07:17.117: W/PacketWriter(8231): at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)

01-23 15:07:17.117: W/PacketWriter(8231): at java.io.BufferedWriter.flush(BufferedWriter.java:124)

01-23 15:07:17.117: W/PacketWriter(8231): at org.jivesoftware.smack.util.ObservableWriter.flush(ObservableWriter.java:44)

01-23 15:07:17.117: W/PacketWriter(8231): at org.jivesoftware.smack.tcp.PacketWriter.writePackets(PacketWriter.java:190)

01-23 15:07:17.117: W/PacketWriter(8231): at org.jivesoftware.smack.tcp.PacketWriter.access$000(PacketWriter.java:40)

01-23 15:07:17.117: W/PacketWriter(8231): at org.jivesoftware.smack.tcp.PacketWriter$1.run(PacketWriter.java:77)

01-23 15:07:17.117: W/System.err(8231): org.jivesoftware.smack.SmackException$NoResponseException

01-23 15:07:17.137: W/System.err(8231): at org.jivesoftware.smack.XMPPConnection.throwConnectionExceptionOrNoResponse(XMPP Connection.java:548)

01-23 15:07:17.137: W/System.err(8231): at org.jivesoftware.smack.tcp.XMPPTCPConnection.throwConnectionExceptionOrNoRespon se(XMPPTCPConnection.java:867)

01-23 15:07:17.137: W/System.err(8231): at org.jivesoftware.smack.tcp.PacketReader.startup(PacketReader.java:113)

01-23 15:07:17.137: W/System.err(8231): at org.jivesoftware.smack.tcp.XMPPTCPConnection.initConnection(XMPPTCPConnection.j ava:482)

01-23 15:07:17.137: W/System.err(8231): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPC onnection.java:440)

01-23 15:07:17.137: W/System.err(8231): at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection. java:811)

01-23 15:07:17.137: W/System.err(8231): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)

01-23 15:07:17.137: W/System.err(8231): at com.rappier.closrr.MyService.onCreate(MyService.java:225)

01-23 15:07:17.137: W/System.err(8231): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2608)

01-23 15:07:17.137: W/System.err(8231): at android.app.ActivityThread.access$1700(ActivityThread.java:157)

01-23 15:07:17.137: W/System.err(8231): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1377)

01-23 15:07:17.137: W/System.err(8231): at android.os.Handler.dispatchMessage(Handler.java:99)

01-23 15:07:17.137: W/System.err(8231): at android.os.Looper.loop(Looper.java:176)

01-23 15:07:17.137: W/System.err(8231): at android.app.ActivityThread.main(ActivityThread.java:5317)

01-23 15:07:17.137: W/System.err(8231): at java.lang.reflect.Method.invokeNative(Native Method)

01-23 15:07:17.137: W/System.err(8231): at java.lang.reflect.Method.invoke(Method.java:511)

01-23 15:07:17.137: W/System.err(8231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102 )

01-23 15:07:17.137: W/System.err(8231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)

01-23 15:07:17.137: W/System.err(8231): at dalvik.system.NativeStart.main(Native Method)

01-23 15:07:17.137: E/MyService(8231): Could not connect to the Xmpp server.