0

I am trying to establish a HTTPS connection between my HTTPS python server to my HTTPS c++ client. I tested the server using a browser and it worked fine, but I am having trouble with the winhttp library of the c++ client and I can't establish a proper connection.
I tried to read more on the problem and the winhttp library online and I figured that the problem is with the configuration of the server certificate in the client side, but I couldn't find a proper solution and I gotten lost when I tried to read on the different functions in the winhtttp library. How can I properly establish a HTTPS connection with my server and client?
(Both programs run on windows. When running my server and after that running my client I get the message Error 12175 has occurred.)

my server code is:

from http.server import HTTPServer, BaseHTTPRequestHandler
import ssl
import pathlib


class myHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('content-type', 'text/html')
        self.end_headers()
        self.wfile.write(self.path.encode())


def run(server_class=HTTPServer):

    server_address = ('', 8000)
    httpd = server_class(server_address, myHandler)

    currPath = str(pathlib.Path(__file__).parent.absolute())
    keyPath = currPath + '\\server.pem'
    httpd.socket = ssl.wrap_socket(
        httpd.socket, certfile=keyPath, server_side=True)

    httpd.serve_forever()


run()

My client code:

#include <iostream>
#include <ws2tcpip.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
using namespace std;

void main()
{
    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL bResults = FALSE;
    HINTERNET hSession = NULL,
              hConnect = NULL,
              hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(L"WinHTTP Example/1.0",
                           WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                           WINHTTP_NO_PROXY_NAME,
                           WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    if (hSession)
    {
        hConnect = WinHttpConnect(hSession, L"localhost",
                                  8000, 0);
    }

    // Create an HTTP request handle.
    if (hConnect)
    {
        hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL,
                                      NULL, WINHTTP_NO_REFERER,
                                      WINHTTP_DEFAULT_ACCEPT_TYPES,
                                      WINHTTP_FLAG_SECURE);
    }

    // Send a request.
    if (hRequest)
    {
        bResults = WinHttpSendRequest(hRequest,
                                      WINHTTP_NO_ADDITIONAL_HEADERS, 0,
                                      WINHTTP_NO_REQUEST_DATA, 0,
                                      0, 0);
    }

    // End the request.
    if (bResults)
    {
        bResults = WinHttpReceiveResponse(hRequest, NULL);
    }

    // Keep checking for data until there is nothing left.
    if (bResults)
    {
        do
        {
            // Check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
                printf("Error %u in WinHttpQueryDataAvailable.\n",
                       GetLastError());

            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize + 1];
            if (!pszOutBuffer)
            {
                printf("Out of memory\n");
                dwSize = 0;
            }
            else
            {
                // Read the data.
                ZeroMemory(pszOutBuffer, dwSize + 1);

                if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
                                     dwSize, &dwDownloaded))
                    printf("Error %u in WinHttpReadData.\n", GetLastError());
                else
                    printf("%s", pszOutBuffer);

                // Free the memory allocated to the buffer.
                delete[] pszOutBuffer;
            }
        } while (dwSize > 0);
    }

    // Report any errors.
    if (!bResults)
        printf("Error %d has occurred.\n", GetLastError());

    // Close any open handles.
    if (hRequest)
        WinHttpCloseHandle(hRequest);
    if (hConnect)
        WinHttpCloseHandle(hConnect);
    if (hSession)
        WinHttpCloseHandle(hSession);
}

SH22
  • 21
  • 1
  • 5
  • How do the programming languages the server abd client are written in matter for that problem? – πάντα ῥεῖ Sep 04 '20 at 21:52
  • @πάνταῥεῖ what do you mean? I have written the languages that the client and server are written in because I believe the solution is related to changing the code with functions in those languages. Is there a way to solve the the problem without directly changing the code of the client/server? – SH22 Sep 05 '20 at 17:50

0 Answers0