Hi Fabio,
I'm using ssl without having it connected to my TCP socket, I'm not sure it's possible to connect it up so you can just call SSL_read, SSL_write and have it work on the socket for you.
My method is to pass any data I want to send through SSL, reading the encrypted data out and then writing that to the socket. Any data I read from the socket gets fed into SSL and then the unencrypted data is read out the other side and passed into my application.
Here's a rough overview of how I did it.
I call ssl init and create a context.
After that I create an X509 cert using a memory BIO and load that into SSL using SSL_CTX_use_certificate
Then I call SSL_new( sslContext )
At this point i create two memory bios, using BIO_new( BIO_s_mem() ), one for reading and one for writing.
I then call SSL_set_bio( sslHandle, readBio, writeBio ) ( I'm relatively new to openSSL so not sure if you can use the same BIO for reading and writing.)
After setting the BIOs I call SSL_set_connect_state( sslHandle ). From here the write BIO should now contain the first part of the SSL handshake.
Connect your TCP socket to your server.
Once connected the next step is to deal with the handshake. Calling BIO_read on the write BIO should get you the handshake data to send. Write that data to your TCP socket,
You should then get the response part of the handshake back which you need to push back into SSL which you do by calling BIO_write on your read BIO.
You can check SSL_do_handshake to see if you need to check if the handshake is finished (usually two rounds of send and receive ).
Once the handshake is complete it's a matter of anything you want to write to your TCP socket, you push it through SSL via the BIO first using SSL_Write and then calling BIO_read on your write BIO to get the encrypted data out.
When you get data read from your TCP socket feed that straight into SSL via BIO_write to load the data into your read BIO and then calling SSL_read to get the data out.
Reading back over this, it seems a little confusing but I hope it gives you an idea of how I've done it. It's by no means the right way/best way but it really performs well.
Hope this helps, Good luck!
Rich