Discussion:
WinHttpQueryOption() with WINHTTP_OPTION_SERVER_CERT_CONTEXT doesn't work
(too old to reply)
Lars Uhlmann
2007-01-23 18:49:59 UTC
Permalink
the code:

,-----
| DWORD dwSize = 0;
| DWORD dwDownloaded = 0;
| DWORD dwCertContextSize = sizeof(CERT_CONTEXT);
| //DWORD dwOptions =
| // SECURITY_FLAG_IGNORE_CERT_CN_INVALID
| // | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
| // | SECURITY_FLAG_IGNORE_UNKNOWN_CA;
|
| CERT_CONTEXT structCertContext;
| LPSTR pszOutBuffer;
| BOOL bResults = FALSE;
| HINTERNET hSession = NULL;
| HINTERNET hConnect = NULL;
| HINTERNET hRequest = NULL;
|
|
| hSession = WinHttpOpen( L"WinHTTP Test",
| WINHTTP_ACCESS_TYPE_NO_PROXY,
| NULL, NULL, 0);
|
| if (hSession)
| {
| hConnect = WinHttpConnect( hSession, L"localhost",
| INTERNET_DEFAULT_HTTPS_PORT, 0);
| }
|
| if (hConnect)
| {
| hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/test.htm",
| NULL, WINHTTP_NO_REFERER,
| WINHTTP_DEFAULT_ACCEPT_TYPES,
| WINHTTP_FLAG_SECURE);
| }
|
| //WinHttpSetOption( hRequest, WINHTTP_OPTION_SECURITY_FLAGS,
| // &dwOptions, sizeof (DWORD) ))
|
| if (hRequest)
| {
| bResults = WinHttpSendRequest( hRequest,
| WINHTTP_NO_ADDITIONAL_HEADERS, 0,
| WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
| }
|
| if( !WinHttpQueryOption( hRequest,
| WINHTTP_OPTION_SERVER_CERT_CONTEXT,
| &structCertContext, &dwCertContextSize)
| )
| {
| printf("WinHttpQueryOption() returned %u\n", GetLastError());
| }
|
| [...]
|
| if (hRequest) WinHttpCloseHandle(hRequest);
| if (hConnect) WinHttpCloseHandle(hConnect);
| if (hSession) WinHttpCloseHandle(hSession);
`-----

the problem:

'structCertContext' doesn't receive the server certificate. I've tried
WinHttpSetOption() because the cert is self signed and a CA isn't (and
(shouln't be) available at client side. This let's me receive the
requested "test.htm" but nevertheless 'structCertContext' stays empty.
What am I doing wrong?

regards,
Lars

PS: 'WinHttpQueryOption()' returns no error...
Marcin Domaslawski
2007-01-24 15:35:14 UTC
Permalink
Hi,

I've checked your snippet. It almost works - just one thing:

from: CERT_CONTEXT structCertContext;
to: CERT_CONTEXT * structCertContext;

I've got IIS with self signed certificate on WinXP.

Marcin Domaslawski
Post by Lars Uhlmann
,-----
| DWORD dwSize = 0;
| DWORD dwDownloaded = 0;
| DWORD dwCertContextSize = sizeof(CERT_CONTEXT);
| //DWORD dwOptions =
| // SECURITY_FLAG_IGNORE_CERT_CN_INVALID
| // | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
| // | SECURITY_FLAG_IGNORE_UNKNOWN_CA;
|
| CERT_CONTEXT structCertContext;
| LPSTR pszOutBuffer;
| BOOL bResults = FALSE;
| HINTERNET hSession = NULL;
| HINTERNET hConnect = NULL;
| HINTERNET hRequest = NULL;
|
|
| hSession = WinHttpOpen( L"WinHTTP Test",
| WINHTTP_ACCESS_TYPE_NO_PROXY,
| NULL, NULL, 0);
|
| if (hSession)
| {
| hConnect = WinHttpConnect( hSession, L"localhost",
| INTERNET_DEFAULT_HTTPS_PORT, 0);
| }
|
| if (hConnect)
| {
| hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/test.htm",
| NULL, WINHTTP_NO_REFERER,
| WINHTTP_DEFAULT_ACCEPT_TYPES,
| WINHTTP_FLAG_SECURE);
| }
|
| //WinHttpSetOption( hRequest, WINHTTP_OPTION_SECURITY_FLAGS,
| // &dwOptions, sizeof (DWORD) ))
|
| if (hRequest)
| {
| bResults = WinHttpSendRequest( hRequest,
| WINHTTP_NO_ADDITIONAL_HEADERS, 0,
| WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
| }
|
| if( !WinHttpQueryOption( hRequest,
| WINHTTP_OPTION_SERVER_CERT_CONTEXT,
| &structCertContext, &dwCertContextSize)
| )
| {
| printf("WinHttpQueryOption() returned %u\n", GetLastError());
| }
|
| [...]
|
| if (hRequest) WinHttpCloseHandle(hRequest);
| if (hConnect) WinHttpCloseHandle(hConnect);
| if (hSession) WinHttpCloseHandle(hSession);
`-----
'structCertContext' doesn't receive the server certificate. I've tried
WinHttpSetOption() because the cert is self signed and a CA isn't (and
(shouln't be) available at client side. This let's me receive the
requested "test.htm" but nevertheless 'structCertContext' stays empty.
What am I doing wrong?
regards,
Lars
PS: 'WinHttpQueryOption()' returns no error...
Loading...