We help IT Professionals succeed at work.
Get Started

WinHttp - Which SSL client cert to use? - error 12044 (C++)

Jax Logan
Jax Logan asked
on
9,100 Views
Last Modified: 2014-04-19
Experts,

I'm trying to use Microsoft's WinHttp to Post to the server via https. Unfortunately I get a 12044 -
Client Certificate needed. I'm working on Win XP SP 3. I'm not sure how to select the correct client cert for the SSL. I found the sample code for CertFindCertificateInStore below but it's not returning a valid certificate.

Any ideas? Thanks!

#include <stdio.h>
#include <windows.h>
#include <winhttp.h>
#include <Wincrypt.h>
#pragma comment(lib, "crypt32.lib")

//#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "winhttp.lib")

int main(int argc, char *argv[])
{
    //DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL, 
               hConnect = NULL,
               hRequest = NULL;
    LPCWSTR acceptTypes[2] = {TEXT("text/xml ; charset=utf-8"), NULL};
    DWORD data;
    DWORD dwSize = sizeof(DWORD);
&#9;LPCWSTR additionalHeaders = L"";
&#9;DWORD hLen   = sizeof(LPCWSTR) * wcslen(additionalHeaders)+1;
&#9;LPVOID dwCert = NULL;
    DWORD dwCertLen = NULL;

&#9;LPSTR  msg = "<Request>12345</Request>";
&#9;//const void* msgToSend = (const void*)msg;
&#9;DWORD data_len = sizeof(LPSTR) * strlen(msg);

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen( L"my user agent",  
                            WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                            WINHTTP_NO_PROXY_NAME, 
                            WINHTTP_NO_PROXY_BYPASS, 0);

        // Use WinHttpQueryOption to retrieve internet options.
        if (WinHttpQueryOption( hSession, 
                                WINHTTP_OPTION_CONNECT_TIMEOUT, 
                                &data, &dwSize))
        {
            printf("Connection timeout: %u ms\n\n",data);
        }
        else
        {
            printf( "Error %u in WinHttpQueryOption.\n", 
                    GetLastError());
        }  

    // Specify an HTTP server.
    if (hSession)
&#9;&#9;hConnect = WinHttpConnect( hSession, L"172.155.155.122",
                                   443, 0);

    // Create an HTTP request handle.
    if (hConnect)
        hRequest = WinHttpOpenRequest( hConnect, L"POST", L"/TEST",
                                       NULL, WINHTTP_NO_REFERER, 
                                       acceptTypes, 
                                       WINHTTP_FLAG_SECURE);



    // Send a request.
    if (hRequest)
        bResults = WinHttpSendRequest( hRequest,
                                       NULL,
                                       0, (LPVOID)msg, data_len, 
                                       data_len, WINHTTP_FLAG_SECURE);

&#9;DWORD dwError = GetLastError();
    if ( dwError == ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED )
    {
&#9;&#9;
      //MY is the store the certificate is in.
&#9;PCCERT_CONTEXT  pCertContext = NULL; 
&#9;LPVOID szCertName = NULL;
      HCERTSTORE hMyStore = CertOpenSystemStore( 0, TEXT("MY") );
      if( hMyStore )
      {
        pCertContext = CertFindCertificateInStore( hMyStore,
             X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
             0,
             CERT_FIND_SUBJECT_STR,
             (LPVOID) szCertName, //Subject string in the certificate.
             NULL );
        if( pCertContext )
        {
          WinHttpSetOption( hRequest, 
                            WINHTTP_OPTION_CLIENT_CERT_CONTEXT,
                            (LPVOID) pCertContext, 
                            sizeof(CERT_CONTEXT) );
          CertFreeCertificateContext( pCertContext );
        }
        CertCloseStore( hMyStore, 0 );

        // NOTE: Application should now resend the request.
&#9;  }
       
    }
    // Send a request.
    if (hRequest)
        bResults = WinHttpSendRequest( hRequest,
                                       WINHTTP_NO_ADDITIONAL_HEADERS,
                                       0, (LPVOID)msg, data_len, 
                                       data_len, WINHTTP_FLAG_SECURE);


&#9;if (!bResults)
        printf( "Error %d has occurred - WinHttpSendRequest.\n", GetLastError());
 
    // End the request.
    if (bResults)
        bResults = WinHttpReceiveResponse( hRequest, NULL);

    // Continue to verify data until there is nothing left.
    if (bResults)
        do 
        {

            // Verify 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\n", pszOutBuffer);
            
                // Free the memory allocated to the buffer.
                delete [] pszOutBuffer;
            }

        } while (dwSize > 0);


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

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

&#9;system("pause");
   return( 0 );
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE