Click here to Skip to main content
15,867,771 members
Articles / Desktop Programming / MFC
Article

CSslSocket - SSL/TLS enabled CSocket

Rate me:
Please Sign up or sign in to vote.
4.60/5 (12 votes)
24 Nov 20012 min read 829.9K   11.1K   83   136
CSocket derived class with SSL/TLS extension

Motivation

Having seen SSL samples from Platform SDK (WebServer and WebClient) I found, it would be useful to benefit from SSL/TLS functionality built in Windows. However, these samples are not very user friendly - you can learn from them how SSL/TLS works in Windows, but adapt it to different applications is not easy.

Description of solution

I am sure everybody knows CSocket from MFC and has seen samples CHATTER / CHATSRVR - that's place where I started - my idea was to derive CSslSocket from CSocket and extend it with SSL functionality.

It means that CSslSocket works in the same way as CSocket and there are several small differences in the declarations of Create() and Listen() methods:

BOOL Create(
	UINT nSocketPort = 443,
	LPCTSTR lpszSocketAddress = NULL,
	const TCHAR *szCertName = NULL,
	BOOL bMachineStore = FALSE,
	DWORD dwProtocol = 0);
BOOL Listen(
	int nConnectionBacklog = 5,
	BOOL bAuthClient = FALSE);

Parameters nSocketPort, lpszSocketAddress are the same parameters as CSocket has. SSL/TLS are a stream based protocols, therefore you cannot specify SOCK_DGRAM in this method as it is possible for CSocket. However, you can specify name of certificate (you must have certificate for server side), certificate store  and preferred protocol (see SCHANNEL_CRED). Client certificate is not required, but you can force SSL engine to require it. In this case set bAuthClient to TRUE in call to Listen() method.

You can find more information about SSL/TLS and Schannel at locations specified at the begging of the article.

Usage

You can use CSslSocket exactly as you are using CSocket, use it directly, or derive your new class from CSslSocket and overwrite required method. See modified samples provided with this article.

Demo program(s)

There are modified samples from Microsoft CHATTER / CHATSRVR for demonstration of CSslSocket usage and work in the zipped file. Modifications are small - just CSocket is replaced with CSslSocekt and there is code to pass proper parameters to the CSslSocket class. You need one or two certificates to test my class. Simplest way is to install Certificate services from Microsoft and request certificates for client and server identification by web forms provided by Certificate services (you need Windows NT/2000 server), or you can use OpenSSL as well. Then just specify server certificate name for CHATSRVR  in the first dialog window and user certificate name for CHATTER.

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
Software Developer (Senior)
United Kingdom United Kingdom
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: no client certificate? Pin
Wenderson27-Dec-02 6:45
Wenderson27-Dec-02 6:45 
QuestionHow to install the class in my own app ? Pin
Christian Meunier9-Oct-02 7:26
sussChristian Meunier9-Oct-02 7:26 
AnswerRe: How to install the class in my own app ? Pin
Martin Ziacek9-Oct-02 8:33
Martin Ziacek9-Oct-02 8:33 
Generaltoo complex Pin
Atrament8-Oct-02 23:36
Atrament8-Oct-02 23:36 
GeneralRe: too complex Pin
Martin Ziacek9-Oct-02 0:33
Martin Ziacek9-Oct-02 0:33 
GeneralProblems.. Pin
Anonymous2-Sep-02 23:09
Anonymous2-Sep-02 23:09 
GeneralRe: Problems.. Pin
Martin Ziacek2-Sep-02 23:48
Martin Ziacek2-Sep-02 23:48 
GeneralRe: Problems.. Pin
Anonymous3-Sep-02 22:45
Anonymous3-Sep-02 22:45 
GeneralRe: Problems.. Pin
Martin Ziacek4-Sep-02 0:57
Martin Ziacek4-Sep-02 0:57 
GeneralRe: Problems.. Pin
Anonymous4-Sep-02 20:21
Anonymous4-Sep-02 20:21 
Generalproblems with the examples Pin
Balint Jureczky6-Aug-02 20:26
Balint Jureczky6-Aug-02 20:26 
GeneralRe: problems with the examples Pin
Martin Ziacek6-Aug-02 20:42
Martin Ziacek6-Aug-02 20:42 
GeneralRe: problems with the examples Pin
Balint Jureczky6-Aug-02 21:20
Balint Jureczky6-Aug-02 21:20 
GeneralRe: problems with the examples Pin
Martin Ziacek6-Aug-02 22:22
Martin Ziacek6-Aug-02 22:22 
GeneralRe: problems with the examples Pin
Martin Ziacek6-Aug-02 22:33
Martin Ziacek6-Aug-02 22:33 
GeneralRe: problems with the examples Pin
Balint Jureczky7-Aug-02 0:28
Balint Jureczky7-Aug-02 0:28 
GeneralRe: problems with the examples Pin
Martin Ziacek7-Aug-02 0:53
Martin Ziacek7-Aug-02 0:53 
GeneralRe: help required Pin
Martin Ziacek6-Aug-02 7:46
Martin Ziacek6-Aug-02 7:46 
QuestionWhy CHATTER Failed? Pin
yon yu27-Jun-02 21:12
yon yu27-Jun-02 21:12 
AnswerRe: Why CHATTER Failed? Pin
Martin Ziacek28-Jun-02 0:37
Martin Ziacek28-Jun-02 0:37 
Questiononce built, can the same EXE run on ALL version of Windows? Pin
6-Jun-02 10:28
suss6-Jun-02 10:28 
AnswerRe: once built, can the same EXE run on ALL version of Windows? Pin
Martin Ziacek14-Jun-02 7:55
Martin Ziacek14-Jun-02 7:55 
QuestionHow to build you example code? Pin
27-May-02 15:46
suss27-May-02 15:46 
AnswerRe: How to build you example code? Pin
Martin Ziacek27-May-02 18:48
Martin Ziacek27-May-02 18:48 
GeneralRe: How to build you example code? Pin
28-May-02 22:39
suss28-May-02 22:39 

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.