Click here to Skip to main content
11,492,385 members (63,093 online)
Click here to Skip to main content

SSL : Convert your Plain Sockets to SSL Sockets in an Easy Way

, 14 Mar 2008 CPOL 115.3K 3.9K 49
Rate this:
Please Sign up or sign in to vote.
A simple class that allows you to convert an existing SOCKET handle to SSL under Windows

Includes:

  • SSL class soucre files (SSL.CPP, Z.H, SSL.H)
  • Testing project TEL, telnet client and server with SSL ability.

Introduction

A lot of SSL stuff already exists, but it is in either MFC, NET or some other non-native format. Here is a simple class SSL_SOCKET that allows you to convert an existing SOCKET handle to SSL under Windows. I got much information from the great CSslSocket - SSL/TLS enabled CSocket MFC article, but I need a plain Win32 one

Features

  • x86 / x64 compatible.
  • HTML help.
  • Supports Server and Client.

License

Free, for any kind or freeware, shareware, commercial, or whateverware project, as long as you give me credit for the library in your 'about box' or your application's documentation.

Creating the SSL Client

First, create and connect your socket using the normal socket functions (socket(), and connect()). Then construct an SSL_SOCKET:

// Say that X is a socket
SSL_SOCKET* SX = new SSL_SOCKET(X,0,0);

This creates an SSL_SOCKET object for an SSL_CLIENT. The last parameter to the constructor indicates that the object will create a tempora self-signed certificate to authenticate itself with the SSL server. If you want, you can pass your own PCERT_CONTEXT.

Next step is to call SSL_SOCKET::ClientInit()

// Initialize the Security Session
sX->ClientInit();

This also calls SSL_SOCKET::ClientLoop() to initialize the SSL Session. (If you don't want to initialize the SSL session at this time, call ClientInit(true) and then later call ClientLoop()). Once the loop returns 0 (success), you can then use the following functions:

  • int SSL_SOCKET:: s_send(char* b, int sz); // Sends data, returns 0 or -1 on error (like normal send()).
  • int SSL_SOCKET:: s_ssend(char* b, int sz); // Sends data, returns 0 or -1 on error (like normal send()). Does not return until all the bytes have been sent or an error occurs.
  • int SSL_SOCKET:: s_recv(char* b, int sz); // Receives data, returns 0 or -1 on error (like normal recv()).
  • int SSL_SOCKET:: s_ssend(char* b, int sz); // Receives data, returns 0 or -1 on error (like normal recv()). Does not return until all the bytes have been received or an error occurs.

If you like, you can call also send_p, ssend_p, recv_p, rrecv_p to send/receive raw bytes (without messaging encryption/decryption), if you can encrypt/decrypt the stuff yourself.

Polite shutdown of the client connection is calling SSL_SOCKET :: ClientOff() before calling closesocket().

Creating the SSL Server

First, create and accept your socket using the normal socket functions (socket(), bind(), listen() and accept()). Then construct a SSL_SOCKET:

// Say that X is a socket
SSL_SOCKET* SX = new SSL_SOCKET(X,1,0);

This creates an SSL_SOCKET object for a SSL_CLIENT. The last parameter to the constructor indicates that the object will create a tempora self-signed certificate to authenticate itself with the SSL server. If you want, you can pass your own PCERT_CONTEXT. Note that some clients will test the certificate and reject it or warn it, so you may want to pass a trusted certificate.

Next step is to call SSL_SOCKET::ServerInit()

// Initialize the Security Session
sX->ServerInit();

This also calls SSL_SOCKET::ServerLoop() to initialize the SSL Session. (If you don't want to initialize the SSL session at this time, call ServerInit(true) and then later call ServerLoop()). Once the loop returns 0 (success), you can then use the send/recv functions discussed above.

Shutdown the server by calling SSL_Socket :: ServerOff().

Other Features

These are some features I'd like to implement in the future:

  • Certificate verification (not yet completed)
  • Documentation (SSL.CHM) is pending.

Please leave your questions and comments!

History

  • March 13, 2007 - Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael Chourdakis
Engineer
Greece Greece
I'm working in C++, PHP , Java, Windows, iOS and Android.

I 've a PhD in Digital Signal Processing and I specialize in Pro Audio applications.

My home page: http://www.michaelchourdakis.com

Comments and Discussions

 
QuestionReverse shell using SSL Pin
Al3487625-Nov-14 9:53
memberAl3487625-Nov-14 9:53 
QuestionCertificate verification Pin
Member 1000013317-Apr-14 12:13
memberMember 1000013317-Apr-14 12:13 
QuestionCryptGenKey in ssl.cpp causes access denied error Pin
nat32_support20-Aug-13 23:15
membernat32_support20-Aug-13 23:15 
QuestionSize of "t" variable on ClientLoop function Pin
fvalerin22-Apr-13 11:42
memberfvalerin22-Apr-13 11:42 
AnswerRe: Size of "t" variable on ClientLoop function Pin
Michael Chourdakis22-Apr-13 12:01
memberMichael Chourdakis22-Apr-13 12:01 
GeneralRe: Size of "t" variable on ClientLoop function Pin
fvalerin22-Apr-13 12:13
memberfvalerin22-Apr-13 12:13 
QuestionWhy InitializeSecurityContext() fails? Pin
xjyang15-Nov-12 5:09
memberxjyang15-Nov-12 5:09 
QuestionThe ZIP file is broken? Pin
xjyang14-Nov-12 5:16
memberxjyang14-Nov-12 5:16 
AnswerRe: The ZIP file is broken? Pin
Michael Chourdakis14-Nov-12 6:38
memberMichael Chourdakis14-Nov-12 6:38 
GeneralRe: The ZIP file is broken? Pin
xjyang14-Nov-12 7:28
memberxjyang14-Nov-12 7:28 
QuestionHow often ServerInit/ClientInit can be invoked Pin
cin197921-Mar-12 4:26
membercin197921-Mar-12 4:26 
AnswerRe: How often ServerInit/ClientInit can be invoked Pin
Michael Chourdakis21-Mar-12 4:30
memberMichael Chourdakis21-Mar-12 4:30 
GeneralRe: How often ServerInit/ClientInit can be invoked Pin
cin197921-Mar-12 5:55
membercin197921-Mar-12 5:55 
GeneralRe: How often ServerInit/ClientInit can be invoked Pin
Michael Chourdakis21-Mar-12 8:51
memberMichael Chourdakis21-Mar-12 8:51 
GeneralRe: How often ServerInit/ClientInit can be invoked Pin
cin197921-Mar-12 22:57
membercin197921-Mar-12 22:57 
GeneralRe: How often ServerInit/ClientInit can be invoked Pin
Michael Chourdakis22-Mar-12 4:13
memberMichael Chourdakis22-Mar-12 4:13 
QuestionNice work! Michael. Pin
strWork@g.com13-Jan-12 9:14
memberstrWork@g.com13-Jan-12 9:14 
GeneralEvent driven sockets? Pin
rix_17-Nov-11 13:25
memberrix_17-Nov-11 13:25 
GeneralRe: Event driven sockets? Pin
Michael Chourdakis17-Nov-11 21:36
memberMichael Chourdakis17-Nov-11 21:36 
Questioncan i using this class for https ? Pin
Member 161414030-Oct-11 6:25
memberMember 161414030-Oct-11 6:25 
QuestionThanks and another doubt Pin
dyvim22-Aug-11 4:42
memberdyvim22-Aug-11 4:42 
AnswerRe: Thanks and another doubt Pin
Michael Chourdakis22-Aug-11 21:26
memberMichael Chourdakis22-Aug-11 21:26 
GeneralRe: Thanks and another doubt Pin
dyvim22-Aug-11 22:35
memberdyvim22-Aug-11 22:35 
GeneralRe: Thanks and another doubt Pin
Michael Chourdakis23-Aug-11 1:57
memberMichael Chourdakis23-Aug-11 1:57 
GeneralRe: Thanks and another doubt Pin
dyvim23-Aug-11 3:49
memberdyvim23-Aug-11 3:49 
GeneralRe: Thanks and another doubt Pin
Michael Chourdakis23-Aug-11 4:16
memberMichael Chourdakis23-Aug-11 4:16 
GeneralRe: Thanks and another doubt Pin
dyvim23-Aug-11 4:35
memberdyvim23-Aug-11 4:35 
GeneralRe: Thanks and another doubt [modified] Pin
dyvim30-Aug-11 2:21
memberdyvim30-Aug-11 2:21 
QuestionSome implementation missing Pin
dyvim19-Aug-11 0:05
memberdyvim19-Aug-11 0:05 
AnswerRe: Some implementation missing Pin
Michael Chourdakis19-Aug-11 10:16
memberMichael Chourdakis19-Aug-11 10:16 
QuestionLink for Windows 7 version no longer works Pin
dyvim18-Aug-11 5:05
memberdyvim18-Aug-11 5:05 
AnswerRe: Link for Windows 7 version no longer works Pin
Michael Chourdakis19-Aug-11 10:14
memberMichael Chourdakis19-Aug-11 10:14 
GeneralCorrection for buggy Microsoft TLS implimentation Pin
Member 371720410-Jun-11 11:57
memberMember 371720410-Jun-11 11:57 
GeneralRe: Correction for buggy Microsoft TLS implimentation Pin
cin197921-Mar-12 6:05
membercin197921-Mar-12 6:05 
GeneralRe: Correction for buggy Microsoft TLS implimentation Pin
Member 371720422-Mar-12 0:11
memberMember 371720422-Mar-12 0:11 
GeneralCorrection of bug under Windows 7/Windows Server 2008 Pin
IDRASSI Mounir24-Oct-10 14:00
memberIDRASSI Mounir24-Oct-10 14:00 
GeneralRe: Correction of bug under Windows 7/Windows Server 2008 Pin
Michael Chourdakis25-Oct-10 5:46
memberMichael Chourdakis25-Oct-10 5:46 
GeneralProblem with Windows 7 Pin
ksutariya14-Oct-10 2:09
memberksutariya14-Oct-10 2:09 
QuestionCould i use this on mobile? Pin
latell12-Oct-10 22:10
memberlatell12-Oct-10 22:10 
AnswerRe: Could i use this on mobile? Pin
Michael Chourdakis13-Oct-10 20:11
memberMichael Chourdakis13-Oct-10 20:11 
GeneralVerifySessionCertificate Pin
Mark Tutt17-Jun-10 5:32
memberMark Tutt17-Jun-10 5:32 
GeneralRe: VerifySessionCertificate Pin
Michael Chourdakis17-Jun-10 5:54
memberMichael Chourdakis17-Jun-10 5:54 
GeneralRe: VerifySessionCertificate Pin
Mark Tutt17-Jun-10 6:31
memberMark Tutt17-Jun-10 6:31 
GeneralRe: VerifySessionCertificate Pin
Michael Chourdakis17-Jun-10 7:59
memberMichael Chourdakis17-Jun-10 7:59 
GeneralWindows 7 Pin
Ville Kangas7-Apr-10 8:48
memberVille Kangas7-Apr-10 8:48 
GeneralRe: Windows 7 Pin
Michael Chourdakis7-Apr-10 9:13
memberMichael Chourdakis7-Apr-10 9:13 
GeneralRe: Windows 7 Pin
fvalerin10-May-13 13:50
memberfvalerin10-May-13 13:50 
QuestionQuestions about: AcquireCredentialsHandle Pin
piecesofcheese5-Mar-10 10:19
memberpiecesofcheese5-Mar-10 10:19 
GeneralDocumentation Pin
vitorsei29-Oct-09 4:46
membervitorsei29-Oct-09 4:46 
QuestionFor Mobile ??? Pin
STentando8-Sep-09 8:44
memberSTentando8-Sep-09 8:44 

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
Web03 | 2.8.150520.1 | Last Updated 14 Mar 2008
Article Copyright 2008 by Michael Chourdakis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid