Click here to Skip to main content
15,891,880 members
Articles / Desktop Programming / Win32
Article

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

Rate me:
Please Sign up or sign in to vote.
4.83/5 (21 votes)
14 Mar 2008CPOL3 min read 248.2K   7K   55   82
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:

C++
// 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()

C++
// 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:

C++
// 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()

C++
// 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)


Written By
Software Developer
Greece Greece
I'm working in C++, PHP , Java, Windows, iOS, Android and Web (HTML/Javascript/CSS).

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

My home page: https://www.turbo-play.com

Comments and Discussions

 
Questionshort handshaking Pin
jayjaygrant4-Mar-09 10:51
jayjaygrant4-Mar-09 10:51 
AnswerRe: short handshaking Pin
Michael Chourdakis4-Mar-09 10:54
mvaMichael Chourdakis4-Mar-09 10:54 
GeneralRe: short handshaking Pin
jayjaygrant5-Mar-09 8:15
jayjaygrant5-Mar-09 8:15 
GeneralRe: short handshaking Pin
Michael Chourdakis5-Mar-09 9:29
mvaMichael Chourdakis5-Mar-09 9:29 
QuestionA problem in recv big package such as 20k message. Pin
Andy Mao18-Dec-08 16:59
Andy Mao18-Dec-08 16:59 
AnswerRe: A problem in recv big package such as 20k message. Pin
Michael Chourdakis18-Dec-08 18:25
mvaMichael Chourdakis18-Dec-08 18:25 
GeneralSSL user name and password Pin
coronys15-Sep-08 7:22
coronys15-Sep-08 7:22 
QuestionWorks Well except... Pin
Rasch6-Jul-08 19:20
Rasch6-Jul-08 19:20 
It works well for the most part.

I ran into a problem and I'm a bit stumped. Here is some background.

I have a java server. I also have an older .net client that uses the ssl .net libraries.

My old client can connect to test and production.

My new win32 client can connect using this library to test but not production. This eliminates a huge swath of possible problems.

when connecting to production it appears that the handshake works correctly and that the client_loop works. I do actually connect as I can see the connection on the server.

Once the thread that handles the receive issues the call to s_recv it returns with a -1. This is from the result from the call to DecryptMessage. And the code returns from:

if (ss != SEC_E_OK && ss != SEC_I_RENEGOTIATE && ss != SEC_I_CONTEXT_EXPIRED)
	return -1;


The value I get back is not one of the ten predefined constants for DecryptMessage. The actual value is -2146893008.

If I comment this line out I get some garbage and part of my expected response at the end.

So for some reason it works against a test server and fails against a production server.
Some possible reasons are:

I'm running production on Linux and perhaps there is some difference in packet sizes or timing, etc. and the cryptographic negotiation fails in some way silently.

I know it would be very difficult to figure this out from this post. If anyone can point me in the right direction or right process to troubleshoot this issue I would really appreciate it!
GeneralSSL Socket Pin
ms raj14-May-08 6:38
ms raj14-May-08 6:38 
GeneralRe: SSL Socket Pin
Michael Chourdakis14-May-08 9:34
mvaMichael Chourdakis14-May-08 9:34 
GeneralSSL Socket Pin
ms raj14-May-08 22:23
ms raj14-May-08 22:23 
GeneralRe: SSL Socket Pin
Michael Chourdakis14-May-08 23:03
mvaMichael Chourdakis14-May-08 23:03 
GeneralRe: SSL Socket Pin
ms raj15-May-08 1:46
ms raj15-May-08 1:46 
GeneralVista known problem Pin
Michael Chourdakis14-Mar-08 11:52
mvaMichael Chourdakis14-Mar-08 11:52 
AnswerRe: Vista known problem Pin
Mohammed Anees17-Mar-08 18:21
Mohammed Anees17-Mar-08 18:21 
GeneralRe: Vista known problem Pin
Michael Chourdakis21-Mar-08 4:38
mvaMichael Chourdakis21-Mar-08 4:38 
GeneralRe: Vista known problem Pin
CodeBlaster9876524-Jun-08 8:45
CodeBlaster9876524-Jun-08 8:45 
GeneralNice work - looks very promising Pin
crackseller14-Mar-08 10:33
crackseller14-Mar-08 10:33 
GeneralRe: Nice work - looks very promising Pin
Michael Chourdakis14-Mar-08 11:51
mvaMichael Chourdakis14-Mar-08 11:51 

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.