title: How to use openSSL with memory BIOs and non blocking sockets
body: <p>I'm new to openSSL and I trying to figure out what the best / good solution is 
  to create a https connection when using non blocking sockets, and libraries such 
  as libevent, libev or libuv in combination with memory BIOs. </p>
  
  <p>I'm trying to figure out how to manage openSSL calls/data and application data. 
  In short my understanding of how a ssl client application should work is something 
  like this:</p>
  
  <ul>
  <li>create SSL_CTX</li>
  <li>create a new socket connection (e.g. I'm using libuv)</li>
  <li>create two memory BIOs:
  <ul>
  <li>one is filled with data I receive from the server (readBio)</li>
  <li>the other one is used to in the application code to read from. (writeBio)</li>
  </ul></li>
  <li>create a SSL* and set state to SSL_connect_state</li>
  <li>start the handshake process with SSL_do_handshake</li>
  <li>[loop] receive / send data </li>
  </ul>
  
  <p>As I'm using libuv (but this could be any other async/non-blocking library), I
  have a <em>read</em> callback that gets called when data is received on the socket. When 
  I have data which must be written to the socket, I pass this data into a <em>write</em> 
  function of the library (in this <code>uv_write()</code>), but in between this I need to put 
  the calls to SSL.</p>
  
  <p>So after calling SSL_do_handshake(...), SSL stores some data into the writeBIO which 
  I must read and pass into the socket. One question I was thinking about, how do I 
  know that SSL stores data into this BIO, and secondly how do I know when I should 
  send this over the socket.</p>
  
  <p>After looking at some code, I figured out that I had to consume from the writeBIO 
  after calling SSL_do_handshake(). But the next steps are not clear to me.  After s
  ending the first bytes from the handshake the 'event' loop of libuv sets everything in motion; when new data arrives on the socket my '<code>onread()</code> callback is called.  But 
  how do I handle this incoming data? (e.g. do I keep SSL state myself (&lt;-- something 
  which some people have advised me <em>not</em> do to)).</p>
  
  <p>Although I've seen lots of examples which use blocking sockets and the core SSL 
  functions to make a connection I haven't found a good clean/minimalistic example 
  which shows how to use memory BIOs as a client. </p>
  
  <p>I've pasted some code I'm using to test openSSL here: <a href="https://gist.github.com/3989091" rel="noreferrer">https://gist.github.com/3989091</a></p>
  
  <p>Someone around who can describe the process of using async/non-blocking sockets 
  and memory BIOs with SSL? </p>
  
  <p>Thanks
  R</p>