title: SSL Handshaking Using Self-Signed Certs and SSLEngine (JSSE)
body: <p>I have been tasked to implement a custom/standalone Java webserver that can process SSL and non-SSL messages on the same port. </p>
  
  <p>I have implemented an NIO server and its working quite well for non-SSL requests. I am having a heck of a time with the SSL piece and could really use some guidance.</p>
  
  <p>Here's what I have done so far.</p>
  
  <p>In order to distinguish between SSL and non-SSL messages, I check the first byte of the inbound request to see if it is a SSL/TLS message. Example:</p>
  
  <pre><code>   byte a = read(buf);
     if (totalBytesRead==1 &amp;&amp; (a&gt;19 &amp;&amp; a&lt;25)){
         parseTLS(buf);
     }
  </code></pre>
  
  <p>In the parseTLS() method I instantiate an SSLEngine like this:</p>
  
  <pre><code>   java.security.KeyStore ks = java.security.KeyStore.getInstance("JKS");
     java.security.KeyStore ts = java.security.KeyStore.getInstance("JKS");
  
     ks.load(new java.io.FileInputStream(keyStoreFile), passphrase);
     ts.load(new java.io.FileInputStream(trustStoreFile), passphrase);
  
     KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
     kmf.init(ks, passphrase);
  
     TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
     tmf.init(ts);
  
     SSLContext sslc = SSLContext.getInstance("TLS");     
     sslc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
  
  
     SSLEngine serverEngine = sslc.createSSLEngine();
     serverEngine.setUseClientMode(false);
     serverEngine.setEnableSessionCreation(true);
     serverEngine.setWantClientAuth(true);
  </code></pre>
  
  <p>Once the SSLEngine is instantiated, I process the inbound data using the unwrap/wrap methods using code straight out of the official <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/samples/jssesamples.zip">JSSE Samples</a>:</p>
  
  <pre><code>   log("----");
  
     serverResult = serverEngine.unwrap(inNetData, inAppData);
     log("server unwrap: ", serverResult);
     runDelegatedTasks(serverResult, serverEngine);
  
     log("----");
  
     serverResult = serverEngine.wrap(outAppData, outNetData);
     log("server wrap: ", serverResult);
     runDelegatedTasks(serverResult, serverEngine);
  </code></pre>
  
  <p>The first part of the handshake seems to work just fine. The client sends a handshake message and the server responds with a message with 4 records:  </p>
  
  <pre><code>handshake (22)
  - server_hello (2) 
  - certificate (11) 
  - server_key_exchange (12)
  - certificate_request (13) 
  - server_hello_done (14)
  </code></pre>
  
  <p>Next, the client sends a message with three parts:  </p>
  
  <pre><code>handshake (22)
   - certificate (11)
   - client_key_exchange (16)
  
  change_cipher_spec (20)
   - client_hello (1)
  
  handshake (22)
   *** Encrypted Message ****
  </code></pre>
  
  <p>The SSLEngine unwraps the client request and parses the records but the wrap method produces 0 bytes with a handshake status of OK/NEED_UNWRAP. In other words, there's nothing for me to send back to the client and the handshake comes to a screeching halt.  </p>
  
  <p>This is where I am stuck. </p>
  
  <p>In the debugger, I can see that the SSLEngine, specifically the ServerHandshaker, doesn't find any peer certs. This is rather obvious when I look at the certificate record from the client which is 0 bytes long. But why?</p>
  
  <p>I can only assume that there's something wrong with the HelloServer response but I can't seem to put my finger on it. The server seems to be sending a valid cert but the client isn't sending anything back. Is there a problem with my keystore? Or is it the truststore? Or does it have something to do with the way I'm instantiating the SSLEngine? I'm stumped.</p>
  
  <p>Couple other points:</p>
  
  <ul>
  <li>The keystore and truststore referenced in the code snippit above were created using the following tutorial:
  <a href="http://www.techbrainwave.com/?p=953">http://www.techbrainwave.com/?p=953</a></li>
  <li>I'm using Firefox 10 and IE 9 as the client to test the server. Same results for both web clients.</li>
  <li>I'm using Sun/Oracle JDK 6 and Java Secure Socket Extension (JSSE) that comes bundled with it.</li>
  </ul>
  
  <p>I look forward to any guidance you might have but please don't tell me I'm nuts or to use Netty or Grizzly or some other existing solution. Its just not an option at this time. I just want to understand what I'm doing wrong.</p>
  
  <p>Thanks in Advance!</p>