src/main/java/org/jivesoftware/smack/XMPPConnection.java
void proceedTLSReceived() throws Exception {
        SSLContext context = this.config.getCustomSSLContext();
        KeyStore ks = null;
        KeyManager[] kms = null;
        PasswordCallback pcb = null;

        if (config.getCallbackHandler() == null) {
            ks = null;
        } else if (context == null) {
            if (config.getKeystoreType().equals("NONE")) {
                ks = null;
                pcb = null;
            } else if (config.getKeystoreType().equals("PKCS11")) {
                try {
                    Constructor<?> c = Class.forName(
                            "sun.security.pkcs11.SunPKCS11").getConstructor(
                            InputStream.class);
                    String pkcs11Config = "name = SmartCard\nlibrary = "
                            + config.getPKCS11Library();
                    ByteArrayInputStream config = new ByteArrayInputStream(
                            pkcs11Config.getBytes());
                    Provider p = (Provider) c.newInstance(config);
                    Security.addProvider(p);
                    ks = KeyStore.getInstance("PKCS11", p);
                    pcb = new PasswordCallback("PKCS11 Password: ", false);
                    this.config.getCallbackHandler().handle(
                            new Callback[] { pcb });
                    ks.load(null, pcb.getPassword());
                } catch (Exception e) {
                    ks = null;
                    pcb = null;
                }
            } else if (config.getKeystoreType().equals("Apple")) {
                ks = KeyStore.getInstance("KeychainStore", "Apple");
                ks.load(null, null);
                // pcb = new PasswordCallback("Apple Keychain",false);
                // pcb.setPassword(null);
            } else {
                ks = KeyStore.getInstance(config.getKeystoreType());
                try {
                    pcb = new PasswordCallback("Keystore Password: ", false);
                    config.getCallbackHandler().handle(new Callback[] { pcb });
                    ks.load(new FileInputStream(config.getKeystorePath()),
                            pcb.getPassword());
                } catch (Exception e) {
                    ks = null;
                    pcb = null;
                }
            }
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            try {
                if (pcb == null) {
                    kmf.init(ks, null);
                } else {
                    kmf.init(ks, pcb.getPassword());
                    pcb.clearPassword();
                }
                kms = kmf.getKeyManagers();
            } catch (NullPointerException npe) {
                kms = null;
            }
        }

        // Verify certificate presented by the server
        if (context == null) {
            context = SSLContext.getInstance("TLS");
            context.init(kms, null, new java.security.SecureRandom());
        }
        Socket plain = socket;
        // Secure the plain connection
        socket = context.getSocketFactory().createSocket(plain,
                plain.getInetAddress().getHostAddress(), plain.getPort(), true);
        socket.setSoTimeout(0);
        socket.setKeepAlive(true);
        // Initialize the reader and writer with the new secured version
        initReaderAndWriter();
        // Proceed to do the handshake
        ((SSLSocket) socket).startHandshake();
        // if (((SSLSocket) socket).getWantClientAuth()) {
        // System.err.println("Connection wants client auth");
        // }
        // else if (((SSLSocket) socket).getNeedClientAuth()) {
        // System.err.println("Connection needs client auth");
        // }
        // else {
        // System.err.println("Connection does not require client auth");
        // }
        // Set that TLS was successful
        usingTLS = true;

        // Set the new writer to use
        packetWriter.setWriter(writer);
        // Send a new opening stream to the server
        packetWriter.openStream();
    }
void proceedTLSReceived() throws Exception {
        SSLContext context = this.config.getCustomSSLContext();
        KeyStore ks = null;
        KeyManager[] kms = null;
        PasswordCallback pcb = null;

        if (config.getCallbackHandler() == null) {
            ks = null;
        } else if (context == null) {
            if (config.getKeystoreType().equals("NONE")) {
                ks = null;
                pcb = null;
            } else if (config.getKeystoreType().equals("PKCS11")) {
                try {
                    Constructor<?> c = Class.forName(
                            "sun.security.pkcs11.SunPKCS11").getConstructor(
                            InputStream.class);
                    String pkcs11Config = "name = SmartCard\nlibrary = "
                            + config.getPKCS11Library();
                    ByteArrayInputStream config = new ByteArrayInputStream(
                            pkcs11Config.getBytes());
                    Provider p = (Provider) c.newInstance(config);
                    Security.addProvider(p);
                    ks = KeyStore.getInstance("PKCS11", p);
                    pcb = new PasswordCallback("PKCS11 Password: ", false);
                    this.config.getCallbackHandler().handle(
                            new Callback[] { pcb });
                    ks.load(null, pcb.getPassword());
                } catch (Exception e) {
                    ks = null;
                    pcb = null;
                }
            } else if (config.getKeystoreType().equals("Apple")) {
                ks = KeyStore.getInstance("KeychainStore", "Apple");
                ks.load(null, null);
                // pcb = new PasswordCallback("Apple Keychain",false);
                // pcb.setPassword(null);
            } else {
                ks = KeyStore.getInstance(config.getKeystoreType());
                try {
                    pcb = new PasswordCallback("Keystore Password: ", false);
                    config.getCallbackHandler().handle(new Callback[] { pcb });
                    ks.load(new FileInputStream(config.getKeystorePath()),
                            pcb.getPassword());
                } catch (Exception e) {
                    ks = null;
                    pcb = null;
                }
            }
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            try {
                if (pcb == null) {
                    kmf.init(ks, null);
                } else {
                    kmf.init(ks, pcb.getPassword());
                    pcb.clearPassword();
                }
                kms = kmf.getKeyManagers();
            } catch (NullPointerException npe) {
                kms = null;
            }
        }

        // Verify certificate presented by the server
        if (context == null) {
            context = SSLContext.getInstance("TLS");
            context.init(kms, null, new java.security.SecureRandom());
        }
        Socket plain = socket;
        // Secure the plain connection
        socket = context.getSocketFactory().createSocket(plain,
                plain.getInetAddress().getHostAddress(), plain.getPort(), true);
        socket.setSoTimeout(0);
        socket.setKeepAlive(true);
        // Initialize the reader and writer with the new secured version
        initReaderAndWriter();
        // Proceed to do the handshake
        ((SSLSocket) socket).startHandshake();
        // if (((SSLSocket) socket).getWantClientAuth()) {
        // System.err.println("Connection wants client auth");
        // }
        // else if (((SSLSocket) socket).getNeedClientAuth()) {
        // System.err.println("Connection needs client auth");
        // }
        // else {
        // System.err.println("Connection does not require client auth");
        // }
        // Set that TLS was successful
        usingTLS = true;

        // Set the new writer to use
        packetWriter.setWriter(writer);
        // Send a new opening stream to the server
        packetWriter.openStream();
    }
The server has indicated that TLS negotiation can start. We now need to
secure the existing plain connection and perform a handshake. This method
won't return until the connection has finished the handshake or an error
occured while securing the connection.

@throws Exception
if an exception occurs.