Click here to Skip to main content
Click here to Skip to main content

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

Comments and Discussions

QuestionLicense Pinmembertalbot4-Jul-11 1:21 
QuestionHow to enable TLS? PinmemberStefanKittel13-Dec-09 12:29 
AnswerRe: How to enable TLS? PinmemberArt Joyce3-Feb-12 5:04 
QuestionDoes Schannel support DTLS protocol? PinmemberD L Deepthi7-Dec-08 20:37 
Please let me know if Schannel library support DTLS (Datagram Transport Layer Security) protocol?


QuestionDecryptMessage fails with SEC_E_DECRYPT_FAILURE (0x80090330L) error Pinmemberdeepthi_dl23-Sep-08 5:16 
GeneralLarge message will hangs the server Pinmemberpip.chan6-May-08 22:20 
GeneralRe: Large message will hangs the server Pinmemberpip.chan6-May-08 23:43 
GeneralRe: Large message will hangs the server Pinmemberpip.chan7-May-08 1:09 
QuestionServer requests client cert problem ? PinmemberMichael Chourdakis24-Nov-07 6:56 
QuestionServer-auth-only with exportable public cert. & .PVK file PinmemberBritannicAndy2-Aug-07 2:45 
Questionerror 0x80090304 Pinmemberdpfairchild4-May-07 12:19 
AnswerRe: error 0x80090304 Pinmemberisapiyan16-May-07 16:53 
Generalerror in CSslSocket::ClientHandshakeLoop. PinmemberMazhar Nazeer22-Nov-06 3:04 
GeneralSchannel offline decryption Pinmemberabhalve28-Sep-06 22:11 
Questionalways return find not found Pinmembertin nguyen trung24-Sep-06 7:27 
QuestionHow to implement on windows 2003 Pinmembertin nguyen trung23-Sep-06 6:02 
GeneralStack Corruption PinmemberI.C.Wiener19-Mar-06 12:32 
GeneralRe: Stack Corruption Pinmemberncranger6-Apr-06 23:50 
QuestionMartin - You Still Around? Pinmembermjxnjx21-Mar-05 13:35 
GeneralLDAP TLS connection Pinmembermeghan20-Jan-05 1:50 
GeneralRoot CA certificate Pinmemberstanr14-Sep-04 4:49 
GeneralBug in extra data handligh PinmemberIrek Zielinski24-Jul-04 23:38 
GeneralRe: Bug in extra data handligh PinmemberDarkstructures4-Apr-05 23:01 
GeneralRe: Bug in extra data handligh PinmemberI.C.Wiener19-Mar-06 19:14 
GeneralCrashes when sending multiple packets PinmemberGNZ4-May-04 9:08 
QuestionDoes it works on Win98? PinsussAnonymous29-Apr-04 0:22 
AnswerRe: Does it works on Win98? PinmemberPetr Prazak24-Nov-05 4:03 
GeneralTried Suggestions - Wont Build ChatSrv Pinmembermjxnjx214-Apr-04 14:13 
GeneralRe: Tried Suggestions - Wont Build ChatSrv PinmemberGNZ20-Apr-04 10:28 
GeneralRe: Tried Suggestions - Wont Build ChatSrv PinmemberGNZ21-Apr-04 10:14 
GeneralRe: Tried Suggestions - Wont Build ChatSrv Pinmemberwashinglee18-Apr-06 1:32 
AnswerRe: Tried Suggestions - Wont Build ChatSrv PinmemberSudantha Athauda15-Feb-07 18:29 
QuestionHow to build this project thanks Pinmemberwyoo24-Feb-04 2:23 
AnswerRe: How to build this project thanks Pinmembernguyenguyen9-Mar-07 0:22 
Generalmore remark Pinmemberplfff7-May-03 18:34 
GeneralClient Side failures PinmemberRaymond Donaldson22-Nov-02 8:49 
GeneralRe: Client Side failures PinmemberRaymond Donaldson22-Nov-02 12:54 
GeneralAttach and Detach PinsussAnonymous20-Nov-02 7:25 
GeneralCan't acquire credentials PinmemberAlan Gardiner13-Nov-02 7:02 
GeneralIssue when the client and the server does not run on the same computer PinsussChristian Meunier2-Nov-02 7:15 
GeneralQuestions to Martin :) PinsussWillyNT30-Oct-02 0:00 
GeneralRe: Questions to Martin :) PinmemberMartin Ziacek30-Oct-02 4:47 
GeneralRe: Questions to Martin :) Pinmemberfrancescopetruzzi25-Nov-02 3:21 
Generalcompatibility PinmemberBalint Jureczky22-Oct-02 0:02 
GeneralRe: compatibility PinmemberMartin Ziacek31-Oct-02 0:03 
Generalcertificates PinmemberFinelame12-Oct-02 12:55 
GeneralRe: certificates PinmemberBalint Jureczky21-Oct-02 23:57 
GeneralThe Chatsrv doesn't run PinsussGUESDON Vincent11-Oct-02 6:36 
GeneralRe: The Chatsrv doesn't run PinsussGUESDON Vincent11-Oct-02 7:31 
GeneralRe: The Chatsrv doesn't run PinmemberMartin Ziacek11-Oct-02 11:13 

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

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

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