@ SuppressWarnings ( "LiteralClassName" ) private void proceedTLSReceived ( ) throws NoSuchAlgorithmException , CertificateException , IOException , KeyStoreException , NoSuchProviderException , UnrecoverableKeyException , KeyManagementException , SmackException { SmackTlsContext smackTlsContext = getSmackTlsContext ( ) ; Socket plain = socket ; // Secure the plain connection socket = smackTlsContext . sslContext . getSocketFactory ( ) . createSocket ( plain , config . getXMPPServiceDomain ( ) . toString ( ) , plain . getPort ( ) , true ) ; final SSLSocket sslSocket = ( SSLSocket ) socket ; // Immediately set the enabled SSL protocols and ciphers. See SMACK-712 why this is // important (at least on certain platforms) and it seems to be a good idea anyways to // prevent an accidental implicit handshake. TLSUtils . setEnabledProtocolsAndCiphers ( sslSocket , config . getEnabledSSLProtocols ( ) , config . getEnabledSSLCiphers ( ) ) ; // Initialize the reader and writer with the new secured version initReaderAndWriter ( ) ; // Proceed to do the handshake sslSocket . startHandshake ( ) ; if ( smackTlsContext . daneVerifier != null ) { smackTlsContext . daneVerifier . finish ( sslSocket . getSession ( ) ) ; } final HostnameVerifier verifier = getConfiguration ( ) . getHostnameVerifier ( ) ; if ( verifier == null ) { throw new IllegalStateException ( "No HostnameVerifier set. Use connectionConfiguration.setHostnameVerifier() to configure." ) ; } else if ( ! verifier . verify ( getXMPPServiceDomain ( ) . toString ( ) , sslSocket . getSession ( ) ) ) { throw new CertificateException ( "Hostname verification of certificate failed. Certificate does not authenticate " + getXMPPServiceDomain ( ) ) ; } // Set that TLS was successful secureSocket = sslSocket ; }
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 occurred while securing the connection .