Click here to Skip to main content
6,292,426 members and growing! (11,302 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate License: The Code Project Open License (CPOL)

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

By Michael Chourdakis

A simple class that allows you to convert an existing SOCKET handle to SSL under Windows
C++, Win32, Dev
Posted:14 Mar 2008
Views:13,247
Bookmarked:15 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
10 votes for this article.
Popularity: 3.83 Rating: 3.83 out of 5
1 vote, 10.0%
1

2
1 vote, 10.0%
3
3 votes, 30.0%
4
5 votes, 50.0%
5

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)

About the Author

Michael Chourdakis


Member
I am a professional in C++/PHP/DSP Developement.
http://www.turboirc.com
Occupation: Software Developer (Senior)
Location: Greece Greece

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
General[Message Deleted] Pinmemberit.ragester22:42 2 Apr '09  
Questionshort handshaking Pinmemberjayjaygrant11:51 4 Mar '09  
AnswerRe: short handshaking PinmemberMichael Chourdakis11:54 4 Mar '09  
GeneralRe: short handshaking Pinmemberjayjaygrant9:15 5 Mar '09  
GeneralRe: short handshaking PinmemberMichael Chourdakis10:29 5 Mar '09  
QuestionA problem in recv big package such as 20k message. PinmemberAndy Mao17:59 18 Dec '08  
AnswerRe: A problem in recv big package such as 20k message. PinmemberMichael Chourdakis19:25 18 Dec '08  
GeneralSSL user name and password Pinmembercoronys8:22 15 Sep '08  
QuestionWorks Well except... PinmemberRasch20:20 6 Jul '08  
GeneralSSL Socket Pinmemberms raj7:38 14 May '08  
GeneralRe: SSL Socket PinmemberMichael Chourdakis10:34 14 May '08  
GeneralSSL Socket Pinmemberms raj23:23 14 May '08  
GeneralRe: SSL Socket PinmemberMichael Chourdakis0:03 15 May '08  
GeneralRe: SSL Socket Pinmemberms raj2:46 15 May '08  
GeneralVista known problem PinmemberMichael Chourdakis12:52 14 Mar '08  
AnswerRe: Vista known problem PinmemberMohammed Anees19:21 17 Mar '08  
GeneralRe: Vista known problem PinmemberMichael Chourdakis5:38 21 Mar '08  
GeneralRe: Vista known problem PinmemberCodeBlaster987659:45 24 Jun '08  
GeneralNice work - looks very promising Pinmembercrackseller11:33 14 Mar '08  
GeneralRe: Nice work - looks very promising PinmemberMichael Chourdakis12:51 14 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Mar 2008
Editor: Sean Ewington
Copyright 2008 by Michael Chourdakis
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project