title: Android Smack SSL/TLS connection to XMPP Ejabberd server with CA Certificate
body: <p>i'm developing my first XMPP Android application, i've not a lot practice in XMPP but actually i'm able to connect my Smack client to my Ejabberd server successfully, the problem comes out when i try to do the same using TLS (with CA Certificate).</p>
  
  <p>Here the piece of <strong>ejabberd.yml</strong> config about TLS:</p>
  
  <pre><code>hosts:
    - "localhost"
    - "mydomain.com"
  
  listen:
    - 
      port: 5222
      module: ejabberd_c2s
      ##
      ## If TLS is compiled in and you installed a SSL
      ## certificate, specify the full path to the
      ## file and uncomment these lines:
      ##
      certfile: "/home/matt/ssl-cert/stunnel.pem"
      starttls: true
  </code></pre>
  
  <p>The pem file must be valid because i use it for a SSL WebSocket connection without problems.</p>
  
  <p>Here the methods used in my XMPP Java class to initialise the TLS connection:</p>
  
  <pre><code>private void initialiseConnection() {
  
      XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
              .builder();
      config.setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible);
      config.setServiceName(serverAddress); //mydomain.com
      config.setHost(serverAddress);
      config.setPort(5222);
      config.setDebuggerEnabled(true);
  
      SSLContext sslContext = null;
  
      try {
          sslContext = createSSLContext(context);
      } catch (KeyStoreException e) {
          e.printStackTrace();
      } catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
      } catch (KeyManagementException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (CertificateException e) {
          e.printStackTrace();
      }
  
      config.setCustomSSLContext(sslContext);
      config.setSocketFactory(sslContext.getSocketFactory());
  
      XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
      XMPPTCPConnection.setUseStreamManagementDefault(true);
  
      connection = new XMPPTCPConnection(config.build());
      XMPPConnectionListener connectionListener = new XMPPConnectionListener();
      connection.addConnectionListener(connectionListener);
  }
  
  private SSLContext createSSLContext(Context context) throws KeyStoreException,
          NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException {
  
      KeyStore trustStore;
      InputStream in = null;
      trustStore = KeyStore.getInstance("BKS");
  
      in = context.getResources().openRawResource(R.raw.my_keystore);
  
      trustStore.load(in, "MyPassword123".toCharArray());
  
      TrustManagerFactory trustManagerFactory = TrustManagerFactory
              .getInstance(KeyManagerFactory.getDefaultAlgorithm());
      trustManagerFactory.init(trustStore);
      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
      return sslContext;
  }
  </code></pre>
  
  <p>Note that without the SSL/TLS parts (and without the SSL/TLS parts in Ejabberd config) everything works.</p>
  
  <p>p.s For the keystore creation and the SSL methods integration i've followed the lqbal tutorial in this <a href="https://stackoverflow.com/questions/545082/how-to-create-an-ssl-connection-using-the-smack-xmpp-library">page</a>.</p>
  
  <p>Now, the Android Monitor log (Android Studio), gives me only one row about the connection problem.</p>
  
  <pre><code>E/(onCreate): IOException: Handshake failed
  </code></pre>
  
  <p>And no more, but on the Ejabberd server log i've the following rows:</p>
  
  <pre><code>2016-06-14 15:57:26.461 [info] &lt;0.14993.0&gt;@ejabberd_listener:accept:333 (#Port&lt;0.73878&gt;) Accepted connection xx.xx.xx.xx:xxxxx -&gt; xx.xxx.xx.xx:5222
  2016-06-14 15:57:26.466 [debug] &lt;0.15099.0&gt;@ejabberd_receiver:process_data:284 Received XML on stream = &lt;&lt;22,3,1,0,133,1,0,0,129,3,3,159,29,211,249,221,88,135,177,183,150,98,234,76,6,91,52,30,26,186,202,176,199,127,245,56,211,198,43,66,35,237,140,0,0,40,192,43,192,44,192,47,192,48,0,158,0,159,192,9,192,10,192,19,192,20,0,51,0,57,192,7,192,17,0,156,0,157,0,47,0,53,0,5,0,255,1,0,0,48,0,23,0,0,0,13,0,22,0,20,6,1,6,3,5,1,5,3,4,1,4,3,3,1,3,3,2,1,2,3,0,11,0,2,1,0,0,10,0,8,0,6,0,23,0,24,0,25&gt;&gt;
  2016-06-14 15:57:26.466 [debug] &lt;0.15100.0&gt;@ejabberd_c2s:send_text:1832 Send XML on stream = &lt;&lt;"&lt;?xml version='1.0'?&gt;&lt;stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='17298480576042278904' from='mydomain.com' version='1.0'&gt;"&gt;&gt;
  2016-06-14 15:57:26.466 [debug] &lt;0.15100.0&gt;@ejabberd_c2s:send_text:1832 Send XML on stream = &lt;&lt;"&lt;stream:error&gt;&lt;xml-not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp-streams'&gt;&lt;/xml-not-well-formed&gt;&lt;/stream:error&gt;"&gt;&gt;
  2016-06-14 15:57:26.466 [debug] &lt;0.15100.0&gt;@ejabberd_c2s:send_text:1832 Send XML on stream = &lt;&lt;"&lt;/stream:stream&gt;"&gt;&gt;
  </code></pre>
  
  <p>I can't understand this received "&lt;&lt;22,3,1..." tag (???)</p>
  
  <p>What's wrong?</p>