Click here to Skip to main content
15,860,861 members
Articles / Desktop Programming / MFC
Article

Connecting to a HTTPS server with SSL using Wininet, sending client certificate and reading response

Rate me:
Please Sign up or sign in to vote.
4.71/5 (34 votes)
2 Apr 20032 min read 491.9K   8.6K   89   105
A sample class which illustrates how to connect to a HTTPS server, the class sends the desired client certificate and authenticates the user.

Introduction

I’ve recently needed to make a Secure Sockets Layer (SSL) connection to Visa server and send our signed client certificate in order to make our MPI application authorized. I searched many articles but could find just a few ones about the subject. I collected parts of the solution from different articles and MSDN, and implemented a simple class that performs this operation programmatically.

There’s InternetErrorDlg API for some purposes including sending client certificate modeling a selection dialog to the user. But in many cases, the programmer may require authentication without user interface (i.e. user interface requires an OK clicker J.. This might be useless for us if we want our program to do things automatically). This is done here by InternetSetOption() with INTERNET_OPTION_CLIENT_CERT_CONTEXT flag. Don’t forget that this option only works with Internet Explorer 5.01 or later (as MSDN writes).

INTERNET_OPTION_CLIENT_CERT_CONTEXT flag is not included in VC6.0 default headers. If you’ve platform SDK installed, that’s no problem, include the wininet header in sdk/include directory, else you may define it manually;

#define INTERNET_OPTION_CLIENT_CERT_CONTEXT 84

That should be ok if you don’t have old wininet.dll versions.

For readers who are not familiar with wininet, SSL or certificates:

I am not gonna tell what wininet functions do & how they are used nor about the certificates. These are generic subjects and much information can be gathered from so many resources such as MSDN. I will try to answer the questions if you send an email to me.

Well, the flow is simple. First we connect to the HTTPS server and send a HTTPS request. If the server asks for a signed client certificate, we open and dig through the system store(s) for the certificate context we need. Then resend our request but after attaching the certificate context. If the server is satisfied, we are authenticated.

  • ConnectToHttpsServer() summarizes the flow of the connection. This is the initial place.
  • SendHttpsRequest() sends a request. After that, if the server requires client certificate, we search it in the system store. If we find it, InternetSetOption() attaches the context to the connection. Then we try the SendHttpsRequest() again.

A sample usage of the class can be like this:

CSslConnection inetSec;
string sAgentName("My Firm"); 
string sServerName("207.219.70.31");//Can be any https server address 
string sUserName("");//if required 
string sPass("");//if required 
string sObjectName("/xxx.asp");//there should be an object to send a verb 
string sOrganizationUnitName("3-D Secure Compliance TestFacility"); 
string strVerb = "POST";//I chose POST verb. That’s usually done 

inetSec.SetAgentName(sAgentName); 

inetSec.SetCertStoreType(certStoreMY); 
         //The stores provided by the system 
         // are: MY, ROOT, SPC and CA 

inetSec.SetObjectName(sObjectName);      

inetSec.SetOrganizationName(sOrganizationUnitName); 

inetSec.SetPort(9660);//443 is the default HTTPS port 
inetSec.SetServerName(sServerName); 

inetSec.SetRequestID(0); 

if (!inetSec.ConnectToHttpsServer(strVerb)) { 

    cout << inetSec.GetLastErrorString()  << " Code: " 
       << inetSec.GetLastErrorCode(); << endl; 
    return 0; 
} 

if (!inetSec.SendHttpsRequest()) 
{ 
    cout << inetSec.GetLastErrorString() << " Code: " 
        << inetSec.GetLastErrorCode(); << endl
    return 0; 
} 

string response = inetSec.GetRequestResult(); 
cout << response.c_str() << endl;

The “organization name” notated functions and variables are completely sample. I chose using “O value of the issuer field” in the certificate, that’s my search criteria. You may wish to perform store search by different fields. Because, there are many fields in a certificate and a context search can be performed by any of these.

You may possibly add your own functions instead of using FindCertWithOUNITName() function. If you do that, just change the code calling this function (only in 1 place) and provide some variables and accessors which are suitable for your certificate search criteria.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: I am facing the same problem Pin
Member 9483292-Feb-05 6:05
Member 9483292-Feb-05 6:05 
AnswerRe: I am facing the same problem Pin
nbk165-Dec-06 5:44
nbk165-Dec-06 5:44 
GeneralCan work under VC6 Pin
peebrain@psipog.net22-Oct-05 17:24
peebrain@psipog.net22-Oct-05 17:24 
GeneralRe: I cannot compile Pin
Yves14-Mar-07 12:42
Yves14-Mar-07 12:42 
QuestionHow do I send POST parameters ? Pin
Thierry ICDC8-Jun-04 11:44
Thierry ICDC8-Jun-04 11:44 
AnswerRe: How do I send POST parameters ? Pin
duncan3w3d15-Jul-04 11:12
duncan3w3d15-Jul-04 11:12 
GeneralRe: How do I send POST parameters ? Pin
mohsen nourian19-Sep-04 3:06
mohsen nourian19-Sep-04 3:06 
GeneralRe: How do I send POST parameters ? Pin
fyesoft20-Sep-04 3:25
fyesoft20-Sep-04 3:25 
You can send POST Data by slightly changing the SendHttpsRequest() fnction.

Use the following line instead where a call to HttpSendRequest is made

int result = HttpSendRequest(m_hRequest, strHeaders, _tcslen(strHeaders), (LPVOID)m_strPostData.data(), m_strPostData.length());

The last two parameters are for the POST data and its leangth. m_strPostData is std::string().

The 2nd and 3rd parameters are for Http headers, you may ignore these if you dont need them.

Any confusion? please write me and I'll be glad to help you.




Faisal Yaqoob
Software Engineer
LinkedIn Inc.
GeneralRe: How do I send POST parameters ? Pin
mohsen nourian20-Sep-04 20:08
mohsen nourian20-Sep-04 20:08 
GeneralRe: How do I send POST parameters ? Pin
fyesoft22-Sep-04 8:48
fyesoft22-Sep-04 8:48 
GeneralSending a simple string by SSL link Pin
JasonDavis15-Apr-04 6:44
JasonDavis15-Apr-04 6:44 
GeneralRe: Sending a simple string by SSL link Pin
Ayhan AVCI16-Apr-04 0:46
Ayhan AVCI16-Apr-04 0:46 
GeneralSending plain strings accross SSL Pin
JasonDavis15-Apr-04 6:43
JasonDavis15-Apr-04 6:43 
Generalit failed after some thousand sending request Pin
dragon_fei5-Apr-04 9:19
dragon_fei5-Apr-04 9:19 
GeneralRe: it failed after some thousand sending request Pin
dragon_fei5-Apr-04 11:05
dragon_fei5-Apr-04 11:05 
GeneralRe: it failed after some thousand sending request Pin
toreyang12-Nov-04 4:28
toreyang12-Nov-04 4:28 
GeneralRe: it failed after some thousand sending request Pin
dchris_med19-Mar-12 7:30
dchris_med19-Mar-12 7:30 
Generalwhy lose memory Pin
Anonymous14-Mar-04 17:04
Anonymous14-Mar-04 17:04 
GeneralRe: why lose memory Pin
just_do_it13-Apr-04 22:15
just_do_it13-Apr-04 22:15 
GeneralRe: why lose memory Pin
Ayhan AVCI16-Apr-04 1:11
Ayhan AVCI16-Apr-04 1:11 
GeneralRe: why lose memory Pin
TomDuffy10-Dec-04 16:27
TomDuffy10-Dec-04 16:27 
GeneralRe: why lose memory Pin
Ayhan AVCI16-Dec-04 4:00
Ayhan AVCI16-Dec-04 4:00 
GeneralRe: why lose memory Pin
TomDuffy22-Dec-04 8:50
TomDuffy22-Dec-04 8:50 
GeneralCompilie Error Pin
3m2u4-Mar-04 15:37
3m2u4-Mar-04 15:37 
QuestionCan I use this for MSN Messenger ( SSL Auth) Pin
barTt27-Feb-04 8:43
barTt27-Feb-04 8:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.