1

I'm trying to communicate in https with a server using the Win32 API.

Here is a very minimalist code :

HINTERNET ses = WinHttpOpen(L"test",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0 ) ;
HINTERNET con = WinHttpConnect(ses,L"stackoverflow.com",INTERNET_DEFAULT_HTTPS_PORT,0 ) ;
HINTERNET req = WinHttpOpenRequest(con,L"GET",NULL,NULL,WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,WINHTTP_FLAG_SECURE ) ;
WinHttpSendRequest( req,WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0,0,0 ) ;
WinHttpReceiveResponse( req,NULL ) ;
char buffer [10000] ;
unsigned long size ;
WinHttpReadData( req,reinterpret_cast<void*>( buffer ),sizeof( buffer )-1,&size ) ;
buffer[size] = 0 ;
cout << buffer << endl ;

As long as I communicate with a "classic" https server like stackoverflow.com everything goes well. The problem is when I try to communicate with a server that requests an authentication of the client.

I have 3 .pem files : a certificate and a private key for my client, and a root certificate that authenticates my client certificate (i.e. a certificate chain of length 2).

For information, I can connect my server using this cULR command line :

curl https://my.server --cert Client_cert.pem --key Client_key.pem --cacert Root_cert.pem So I kown it's possible!

Reading the win32 API documentation, I figured out that the key is to call WinHttpSetOption but it's not clear between the options WINHTTP_OPTION_CLIENT_CERT_CONTEXT and WINHTTP_OPTION_CLIENT_CERT_ISSUER_LIST... And I did not find out how to pass my data (cert+key files).

Any wise advice ? Thanks in advance !

zett42
  • 25,437
  • 3
  • 35
  • 72
Captain'Flam
  • 479
  • 4
  • 12
  • Why not WinInet? – Michael Chourdakis Mar 20 '19 at 13:31
  • Indeed, WinInet seems to be more high level. But microsoft's documentation is the same confusing : I still can't figure out how to pass my cert+key... – Captain'Flam Mar 20 '19 at 16:42
  • 1
    WinHTTP is highly obscure, by contrast, WinInet is well documented. In your case you need InternetSetOption with https://learn.microsoft.com/en-us/windows/desktop/wininet/option-flags INTERNET_OPTION_CLIENT_CERT_CONTEXT – Michael Chourdakis Mar 20 '19 at 19:02
  • Ok, WinInet seems to be better, so I rewrote my question here : https://stackoverflow.com/questions/55297953/client-authentication-certificat-private-key-using-wininet I hope this was the right way to do it (instead of edit this one)... – Captain'Flam Mar 22 '19 at 10:47

0 Answers0