Skip to content

Instantly share code, notes, and snippets.

@akechi-haruka
Created April 10, 2025 15:51
Show Gist options
  • Select an option

  • Save akechi-haruka/1135b0730595b00adf141ded916f5c1d to your computer and use it in GitHub Desktop.

Select an option

Save akechi-haruka/1135b0730595b00adf141ded916f5c1d to your computer and use it in GitHub Desktop.
WinHTTP HTTPS certificate tester
#include <stdio.h>
#include <windows.h>
#include <winhttp.h>
void CALLBACK callbackfunc(HINTERNET hInternet,
DWORD_PTR context,
DWORD code,
void* pInfo,
DWORD infoLength) {
if (code == WINHTTP_CALLBACK_STATUS_SECURE_FAILURE) {
DWORD details = (DWORD) pInfo; // do not de-reference??
printf("WINHTTP_CALLBACK_STATUS_SECURE_FAILURE: 0x%lx\n", details);
if ((details & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED) != 0) {
printf("WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED\n");
}
if ((details & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT) != 0) {
printf("WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT\n");
}
if ((details & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED) != 0) {
printf("WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED\n");
}
if ((details & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA) != 0) {
printf("WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA\n");
}
if ((details & WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID) != 0) {
printf("WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID\n");
}
if ((details & WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID) != 0) {
printf("WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID\n");
}
if ((details & WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR) != 0) {
printf("WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR\n");
}
}
}
int main(void) {
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);
if (WinHttpSetStatusCallback(hSession, callbackfunc, WINHTTP_CALLBACK_STATUS_SECURE_FAILURE, NULL) ==
WINHTTP_INVALID_STATUS_CALLBACK) {
printf("Failed to set callback: %lx\n", GetLastError());
}
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect(hSession, L"localhost", 8858, 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 = malloc(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.
free(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);
printf("Completed");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment