public static void main(String[] args) throws Exception { SSLContext sslContext = createSSLContext(); SSLServerSocketFactory fact = sslContext.getServerSocketFactory(); SSLServerSocket sSock = (SSLServerSocket) fact.createServerSocket(Utils.PORT_NO); // client authenticate where possible sSock.setWantClientAuth(true); for (; ; ) { SSLSocket sslSock = (SSLSocket) sSock.accept(); try { sslSock.startHandshake(); } catch (IOException e) { continue; } readRequest(sslSock.getInputStream()); SSLSession session = sslSock.getSession(); try { Principal clientID = session.getPeerPrincipal(); System.out.println("client identified as: " + clientID); } catch (SSLPeerUnverifiedException e) { System.out.println("client not authenticated"); } sendResponse(sslSock.getOutputStream()); sslSock.close(); } }
/** Bypass host name verification */ public boolean verify(String hostname, SSLSession session) { System.out.println("Bypassing verification of hostname: " + hostname); try { System.out.println("Peer principal: " + session.getPeerPrincipal().toString()); } catch (SSLPeerUnverifiedException e) { System.err.println("Unable to get peer principal"); e.printStackTrace(); } return true; }
@Override public void handshakeCompleted(HandshakeCompletedEvent event) { SSLSession session = event.getSession(); String protocol = session.getProtocol(); String cipherSuite = session.getCipherSuite(); String peerName = null; try { peerName = session.getPeerPrincipal().getName(); Log.d(TAG, "peerName: " + peerName); } catch (SSLPeerUnverifiedException e) { e.printStackTrace(); } Log.d(TAG, "session: " + session); Log.d(TAG, "protocol: " + protocol); Log.d(TAG, "cipherSuite: " + cipherSuite); }