Click here to Skip to main content
13,400,969 members (39,274 online)
Click here to Skip to main content
Add your own
alternative version


75 bookmarked
Posted 24 Nov 2001

CSslSocket - SSL/TLS enabled CSocket

, 24 Nov 2001
Rate this:
Please Sign up or sign in to vote.
CSocket derived class with SSL/TLS extension
<!-- Download Links -->


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.


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.


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


About the Author

Martin Ziacek
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

AnswerRe: Tried Suggestions - Wont Build ChatSrv Pin
Sudantha Athauda15-Feb-07 18:29
memberSudantha Athauda15-Feb-07 18:29 
QuestionHow to build this project thanks Pin
wyoo24-Feb-04 2:23
memberwyoo24-Feb-04 2:23 
AnswerRe: How to build this project thanks Pin
nguyenguyen9-Mar-07 0:22
membernguyenguyen9-Mar-07 0:22 
Generalmore remark Pin
plfff7-May-03 18:34
memberplfff7-May-03 18:34 
GeneralClient Side failures Pin
Raymond Donaldson22-Nov-02 8:49
memberRaymond Donaldson22-Nov-02 8:49 
GeneralRe: Client Side failures Pin
Raymond Donaldson22-Nov-02 12:54
memberRaymond Donaldson22-Nov-02 12:54 
GeneralAttach and Detach Pin
Anonymous20-Nov-02 7:25
sussAnonymous20-Nov-02 7:25 
GeneralCan't acquire credentials Pin
Alan Gardiner13-Nov-02 7:02
memberAlan Gardiner13-Nov-02 7:02 
On the server side, the AcquireCredentialsHandle function is returing: SEC_E_INTERNAL_ERROR - "The Local Security Authority (LSA) cannot be contacted." and the event log records the following: "The SSL server credential's certificate does not have a private key information property attached to it. This most often occurs when a certificate is backed up incorrectly and then later restored. This message can also indicate a certificate enrollment failure."

How do I create a test certificate with a private key? I have not been able to do this with MakeCert, or with Verisign's test SSL certificate service.

Thanks in advance for any suggestions.
GeneralIssue when the client and the server does not run on the same computer Pin
Christian Meunier2-Nov-02 7:15
sussChristian Meunier2-Nov-02 7:15 
GeneralQuestions to Martin :) Pin
WillyNT30-Oct-02 0:00
sussWillyNT30-Oct-02 0:00 
GeneralRe: Questions to Martin :) Pin
Martin Ziacek30-Oct-02 4:47
memberMartin Ziacek30-Oct-02 4:47 
GeneralRe: Questions to Martin :) Pin
francescopetruzzi25-Nov-02 3:21
memberfrancescopetruzzi25-Nov-02 3:21 
Generalcompatibility Pin
Balint Jureczky22-Oct-02 0:02
memberBalint Jureczky22-Oct-02 0:02 
GeneralRe: compatibility Pin
Martin Ziacek31-Oct-02 0:03
memberMartin Ziacek31-Oct-02 0:03 
Generalcertificates Pin
Finelame12-Oct-02 12:55
memberFinelame12-Oct-02 12:55 
GeneralRe: certificates Pin
Balint Jureczky21-Oct-02 23:57
memberBalint Jureczky21-Oct-02 23:57 
GeneralThe Chatsrv doesn't run Pin
GUESDON Vincent11-Oct-02 6:36
sussGUESDON Vincent11-Oct-02 6:36 
GeneralRe: The Chatsrv doesn't run Pin
GUESDON Vincent11-Oct-02 7:31
sussGUESDON Vincent11-Oct-02 7:31 
GeneralRe: The Chatsrv doesn't run Pin
Martin Ziacek11-Oct-02 11:13
memberMartin Ziacek11-Oct-02 11:13 
GeneralRe: The Chatsrv doesn't run Pin
Anonymous11-Oct-02 11:21
sussAnonymous11-Oct-02 11:21 
GeneralRe: The Chatsrv doesn't run Pin
Martin Ziacek11-Oct-02 11:32
memberMartin Ziacek11-Oct-02 11:32 
GeneralRe: The Chatsrv doesn't run Pin
GUESDON Vincent11-Oct-02 11:37
sussGUESDON Vincent11-Oct-02 11:37 
GeneralRe: The Chatsrv doesn't run Pin
Martin Ziacek11-Oct-02 11:51
memberMartin Ziacek11-Oct-02 11:51 
GeneralRe: The Chatsrv doesn't run Pin
GUESDON Vincent11-Oct-02 11:50
sussGUESDON Vincent11-Oct-02 11:50 
Questionno client certificate? Pin
Anonymous10-Oct-02 11:40
sussAnonymous10-Oct-02 11:40 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180218.2 | Last Updated 25 Nov 2001
Article Copyright 2001 by Martin Ziacek
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid