@SuppressLint("TrulyRandom") private XMPPConnection createConnection() { ConnectionConfiguration config = new ConnectionConfiguration(PreferenceUtils.getServerHost(context), PORT); SSLContext sc = null; MemorizingTrustManager mtm = null; try { mtm = new MemorizingTrustManager(context); sc = SSLContext.getInstance("TLS"); sc.init(null, new X509TrustManager[] {mtm}, new SecureRandom()); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } catch (KeyManagementException e) { throw new IllegalStateException(e); } config.setCustomSSLContext(sc); config.setHostnameVerifier( mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier())); config.setSecurityMode(SecurityMode.required); config.setReconnectionAllowed(false); config.setSendPresence(false); return new XMPPTCPConnection(config); }
/** Creates the Activity and registers a MemorizingTrustManager. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.mtmexample); // set up gui elements findViewById(R.id.connect).setOnClickListener(this); content = (TextView) findViewById(R.id.content); urlinput = (EditText) findViewById(R.id.url); verifyhost = (CheckBox) findViewById(R.id.verifyhost); // register handler for background thread hdlr = new Handler(); // Here, the MemorizingTrustManager is activated for HTTPS try { // set location of the keystore MemorizingTrustManager.setKeyStoreFile("private", "sslkeys.bks"); // register MemorizingTrustManager for HTTPS SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, MemorizingTrustManager.getInstanceList(this), new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); defaultverifier = HttpsURLConnection.getDefaultHostnameVerifier(); // disable redirects to reduce possible confusion HttpsURLConnection.setFollowRedirects(false); } catch (Exception e) { e.printStackTrace(); } }
// this code runs a DNS resolver, might be blocking private synchronized void initXMPPConnection() { // allow custom server / custom port to override SRV record if (configuration.customServer.length() > 0) connectionConfiguration = new ConnectionConfiguration( configuration.customServer, configuration.port, configuration.server); else connectionConfiguration = new ConnectionConfiguration(configuration.server); // use SRV connectionConfiguration.setReconnectionAllowed(false); connectionConfiguration.setSendPresence(false); connectionConfiguration.setCompressionEnabled(false); // disable for now connectionConfiguration.setDebuggerEnabled(configuration.smackdebug); if (configuration.require_ssl) this.connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.required); try { SSLContext sc = SSLContext.getInstance(Constant.TLS); MemorizingTrustManager mtm = JApplication.getApp(service).memorizingTrustManager; sc.init(null, new X509TrustManager[] {mtm}, new java.security.SecureRandom()); this.connectionConfiguration.setCustomSSLContext(sc); this.connectionConfiguration.setHostnameVerifier( mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier())); } catch (java.security.GeneralSecurityException e) { Log.d(TAG, "initialize MemorizingTrustManager: " + e); } this.extXMPPConnection = new StreamHandler.ExtXMPPConnection(connectionConfiguration); this.streamHandler = new StreamHandler(extXMPPConnection, configuration.smackdebug); streamHandler.addAckReceivedListener( new StreamHandler.AckReceivedListener() { public void ackReceived(long handled, long total) { gotServerPong("" + handled); } }); configuration.reconnect_required = false; initServiceDiscovery(); }