title: SSL3_READ_BYTES:tlsv1 alert internal error, secure sockets, OpenSSL, C++, getting HTTPS page
body: <p>I want to write a simple application, which grabs the content of the HTTPS page: <a href="https://httpbin.org/html" rel="noreferrer">https://httpbin.org/html</a>.</p>
  
  <p>I know how to access its insecure (HTTP) version (<a href="http://httpbin.org/html" rel="noreferrer">http://httpbin.org/html</a>), I just need to send a request using sockets:</p>
  
  <pre><code>std::stringstream ss;
  ss &lt;&lt; "GET /html HTTP/1.1" &lt;&lt; "\r\n"
  &lt;&lt; "Host: httpbin.org\r\n"
  &lt;&lt; "Connection: close"
  &lt;&lt; "\r\n\r\n";
  std::string request = ss.str();
  
  // socket creation, connect
  
  send(sock, request.c_str(), request.size(), 0);
  </code></pre>
  
  <p>And it works. However, when using secure sockets (<code>#include &lt;openssl/ssl.h&gt;</code>) I got the following error: <code>error:14094438:SSL routines:SSL3_READ_BYTES:tlsv1 alert internal error</code> when trying <code>result = SSL_connect(ssl);</code></p>
  
  <p>Here's the code:</p>
  
  <pre><code>#include &lt;sys/stat.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;openssl/ssl.h&gt;
  #include &lt;openssl/err.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  
  int main(int argc, char **argv)
  {
      int    sock;
      char   buff[1024];
  
      struct sockaddr_in  address;
      struct hostent     *host = NULL;
  
      std::string servername = "httpbin.org";
  
      if (!inet_aton(servername.c_str(), &amp;address.sin_addr))
          if (host = gethostbyname(servername.c_str()))
              address.sin_addr = *(struct in_addr*)host-&gt;h_addr;
          else
              return -1;
  
      address.sin_port   = htons(443);
      address.sin_family = AF_INET;
  
      std::stringstream ss;
      ss &lt;&lt; "GET /html HTTP/1.1" &lt;&lt; "\r\n"
      &lt;&lt; "Host: httpbin.org\r\n"
      &lt;&lt; "Connection: close"
      &lt;&lt; "\r\n\r\n";
      std::string request = ss.str();
  
  
      if ((sock = socket(AF_INET, SOCK_STREAM, 0)) != -1)
      {
          if (connect(sock, (struct sockaddr *)&amp;address, sizeof(struct sockaddr_in)) != -1)
          {
              SSL_library_init();
              OpenSSL_add_all_algorithms();
              SSL_load_error_strings();
              SSL_CTX *ctx = SSL_CTX_new(TLSv1_2_method());
              if (!ctx)
                  return -1;
  
              int result = -1;
              SSL *ssl = SSL_new(ctx);
              SSL_set_fd(ssl, sock);
              result = SSL_connect(ssl);
  
              if (result == 0)
              {
                  long error = ERR_get_error();
                  const char* error_str = ERR_error_string(error, NULL);
                  printf("%s\n", error_str);
                  return 0;
              }
  
              SSL_write(ssl, request.c_str(), request.size());
              SSL_read(ssl, buff, 1024);
              printf("%s\n", buff);
              memset(buff, 0, 1024);
  
              SSL_free(ssl);
              SSL_CTX_free(ctx);
              close(sock);
          }
      }
  
      return 0;
  }
  </code></pre>