title: OpenSSL SSL_CTX_load_verify_locations Windows
body: <p>I am trying to use OpenSSL in C to make an https request.
  My code runs just fine in OSX and Linux. It has serious issues in Windows.
  I am using the example code in the download found here <a href="https://wiki.openssl.org/index.php/SSL/TLS_Client" rel="nofollow noreferrer">https://wiki.openssl.org/index.php/SSL/TLS_Client</a> (at bottom of page)</p>
  
  <p>I can get one good run out of it (maybe), all subsequent attempts fail.</p>
  
  <p>When running debug in visual studio it will fail on the <code>SSL_CTX_load_verify_locations</code> line. Visual Studio simply tells me there is a corrupted heap. I've checked file permissions, I've checked paths, checking variables when on the exception debug it looks like all the variables are fine, but it doesn't seem to get past the verify. I'm using LibreSSL SDK.</p>
  
  <pre><code>const char *file_loc = "C:\\Users\\{USER}\\Desktop\\trusted-roots.pem";
  res = SSL_CTX_load_verify_locations(ctx, file_loc, NULL);
  </code></pre>
  
  <p>I have noticed that if I can get it to pass, on those rare events, instead of fully letting it finish, just stopping the debug, I can run it to that same point again without problem.</p>
  
  <p>I have tried using a different pem file to make sure it wasn't just the one being locked, but that doesn't make a difference.</p>
  
  <p>I'm happy to debug more, but I'd need some pointers on how to get more information from VS.</p>
  
  <p>The relevant snippet of code below:</p>
  
  <pre><code>int openssl_fetch(char *request, HttpResponse *response)
  {
      char *HOST_NAME = "abc.com"; // Substituted for privacy
      char *HOST_PORT = "443";
  
      long res = 1;
      int ret = 1;
      unsigned long ssl_err = 0;
  
      SSL_CTX* ctx = NULL;
      BIO *web = NULL, *out = NULL;
      SSL *ssl = NULL;
  
      do {
  
            /* Internal function that wraps the OpenSSL init's   */
            /* Cannot fail because no OpenSSL function fails ??? */
            init_openssl_library();
  
            /* https://www.openssl.org/docs/ssl/SSL_CTX_new.html */
            const SSL_METHOD* method = SSLv23_method();
            ssl_err = ERR_get_error();
  
            //ASSERT(NULL != method);
            if (!(NULL != method))
            {
                print_error_string(ssl_err, "SSLv23_method");
                break; /* failed */
            }
  
            /* http://www.openssl.org/docs/ssl/ctx_new.html */
            ctx = SSL_CTX_new(method);
            /* ctx = SSL_CTX_new(TLSv1_method()); */
            ssl_err = ERR_get_error();
  
            //ASSERT(ctx != NULL);
            if (!(ctx != NULL))
            {
                print_error_string(ssl_err, "SSL_CTX_new");
                break; /* failed */
            }
  
            /* https://www.openssl.org/docs/ssl/ctx_set_verify.html */
            SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_callback);
            /* Cannot fail ??? */
  
            /* https://www.openssl.org/docs/ssl/ctx_set_verify.html */
            SSL_CTX_set_verify_depth(ctx, 5);
            /* Cannot fail ??? */
  
            /* Remove the most egregious. Because SSLv2 and SSLv3 have been      */
            /* removed, a TLSv1.0 handshake is used. The client accepts TLSv1.0  */
            /* and above. An added benefit of TLS 1.0 and above are TLS          */
            /* extensions like Server Name Indicatior (SNI).                     */
            const long flags = SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
            long old_opts = SSL_CTX_set_options(ctx, flags);
            UNUSED(old_opts);
  
            /* http://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html */
            //        res = SSL_CTX_load_verify_locations(ctx, "random-org-chain.pem", NULL);
            const char *file_loc = "C:\\Users\\jshackelford\\Desktop\\trusted-roots.pem";
            //const char *file_loc = "/C/Users/jshackelford/Desktop/trusted-roots.pem"; //Tried both
            res = SSL_CTX_load_verify_locations(ctx, file_loc, NULL);
            ssl_err = ERR_get_error();
      }
      return 0;
  }
  </code></pre>