title: C++ convert TCP Socket to BIO (OpenSSL)
body: <p>First of all I did search for my problem, but it's not easy to find answers to exactly this topic.</p>
  
  <p>I wrote a small C++ program that connects through a SOCKS5 Proxy to a HTTP website. That's all working well. Now I want to connect to a HTTPS website, and that's causing some trouble..</p>
  
  <p>The problem is that I cant directly create a BIO* and connect to the SOCKS5 Proxy, I need to do the SOCKS5 protocol stuff first with a unencrypted socket, and then convert that to a BIO*. Am I right with this?</p>
  
  <p>So i got this code snipped for the normal TCP->SOCKS5->Website stuff:</p>
  
  <pre><code>SOCKS5_Greeting_Response socks_gresp;
  SOCKS5_Command_Response socks_cresp;
  int sock = createSocket();
  
  socketConnect(sock, "127.0.0.1", 9050);
  
  socks_gresp = socketWriteSOCKS5Greeting_TOR(sock);
  
  if(socks_gresp.Version == 5){
  
      std::cout &lt;&lt; "[i] Version match!" &lt;&lt; std::endl;
  
  }
  
  socks_cresp = SOCKS5_Connect(sock, get_IPv4("www.google.de"), 80);
  
  if(socks_cresp.Reply == 0){
  
      std::cout &lt;&lt; "[i] Connection success!" &lt;&lt; std::endl;
  
  }
  else if(socks_cresp.Reply == 1){
  
      std::cout &lt;&lt; "[i] General server error" &lt;&lt; std::endl;
  
  }
  else if(socks_cresp.Reply == 3){
  
      std::cout &lt;&lt; "[i] Network not reachable" &lt;&lt; std::endl;
  
  }
  else if(socks_cresp.Reply == 5){
  
      std::cout &lt;&lt; "[i] Connection refused!" &lt;&lt; std::endl;
  
  }
  </code></pre>
  
  <p>After that I can read/write to/from sock, I don't want to post all the sub-functions cause it would be too much...</p>
  
  <p>Well I read here: <a href="https://stackoverflow.com/questions/16205813/convert-existing-tcp-socket-into-tls-using-openssl">Link 1</a> that I should use <code>BIO_new_socket()</code> and <code>SSL_set_bio()</code>
  I found here a code snipped: <a href="https://stackoverflow.com/questions/9359896/openssl-rsa-and-winsock-and-c">Link 2</a></p>
  
  <p>So here is my current code snipped, trying to convert the TCP Socket to BIO* (without all the error handling stuff, I include that when all is working):</p>
  
  <pre><code>SSL_library_init();
  
  SOCKS5_Greeting_Response socks_gresp;
  SOCKS5_Command_Response socks_cresp;
  int sock = createSocket();
  
  socketConnect(sock, "127.0.0.1", 9050);
  
  socks_gresp = socketWriteSOCKS5Greeting_TOR(sock);
  
  if(socks_gresp.Version == 5){
  
      std::cout &lt;&lt; "[i] Version match!" &lt;&lt; std::endl;
  
  }
  
  socks_cresp = SOCKS5_Connect(sock, get_IPv4("www.google.de"), 443);
  
  if(socks_cresp.Reply == 0){
  
      std::cout &lt;&lt; "[i] Connection success!" &lt;&lt; std::endl;
  
  }
  else if(socks_cresp.Reply == 1){
  
      std::cout &lt;&lt; "[i] General server error" &lt;&lt; std::endl;
  
  }
  else if(socks_cresp.Reply == 3){
  
      std::cout &lt;&lt; "[i] Network not reachable" &lt;&lt; std::endl;
  
  }
  else if(socks_cresp.Reply == 5){
  
      std::cout &lt;&lt; "[i] Connection refused!" &lt;&lt; std::endl;
  
  }
  
  //--
  
  BIO* sslsock = BIO_new_socket(sock, BIO_NOCLOSE);
  SSL_CTX * ctx = SSL_CTX_new(SSLv23_client_method());
  SSL_CTX_load_verify_locations(ctx, "/path/to/cacert.pem", NULL);
  SSL* ssl = SSL_new(ctx);
  
  SSL_set_bio(ssl, sslsock, sslsock);
  
  std::cout &lt;&lt; "[i] SSL Write: " &lt;&lt; SSL_write(sslsock, generate_basic_GET_request("www.google.de","Mozilla","")) &lt;&lt; std::endl;
  
  std::cout &lt;&lt; SSL_read(sslsock) &lt;&lt; std::endl;
  
  SSL_Close_Connection(sslsock);
  
  //--
  
  closeSocket(sock);
  </code></pre>
  
  <p>My SSL_write() will print out &lt; 0 on error and else the bytes send. This is my output:</p>
  
  <pre><code>[i] Version match!
  [i] Connection success!
  [i] SSL Write: 191
  F
  </code></pre>
  
  <p>Instead of the 'F' I should see the HTML source or HTTP response there.. I'm pretty sure I'm doing something false at the creation of the BIO*, but I cant tell what. Anyone here can?</p>