2

I have windows c++ project. Need to implement both ssl client and server on top of existing winsock code.

I tried with openssl but it seems too messy. I assume there is nicer/shorter/cleaner/faster way implementeing this than openssl.. Im thankful for any suggestions..

2 Answers 2

3

You can use Windows built-in SSL stuff -- SChannel . Searching Google fo "SChannel SSL" would give you plenty of information (though SChannel itself is poorly documented and not easy to comprehend).

On the other hand, OpenSSL is not messy once you study the source code of some project, that uses OpenSSL.

2
  • Thanks.. though as much as I did found about SChannel seemed to be more complex than openssl. GnuTLS seemed quite interesting, at least method names etc were more clear (than openssl e.g.). In my project I have to implement SSL to both c++/winsock and python side . In python all I had to do was to add 1 line of code: -------------- c = ssl.wrap_socket(newsocket, server_side=True, certfile="servercert.pem", keyfile="serverkey.pem", ssl_version=ssl.PROTOCOL_SSLv3) --------------- It might sound like stupid question but isnt there really way to do it sort of same way in c++ ? Apr 17, 2012 at 11:41
  • heikkitoivonen.net/blog/2008/10/14/ssl-in-python-26 is python example. I just added line c = ssl.wrap_socket(newsocket, server_side=True, certfile="servercert.pem", keyfile="serverkey.pem", ssl_version=ssl.PROTOCOL_SSLv3) and used socket c instead of newsocket. Btw, CAsyncSocketEx seems to be solution for SSL client. But have found nothing to implement SSL server in c++/windows quickly Apr 17, 2012 at 11:45
1

Acctually .. After some time spent with openssl hacking I wouldnt say its that messy :) In case anyone anytime needs to add ssl to existing winsock code:

existing winsock code was like this:

 0: sockett.Listen etc....
    1: sockett.Accept(client, .....
    2: recv(client , ...)
    3: send(client , .....)

well in short if you want to implement SSL here.. delete lines 2 and 3 :) and add:

SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
SSL_CTX *tlsctx;
SSL *ssl;
tlsctx = SSL_CTX_new( SSLv23_method());
// search google : generate self signed certificate openssl
SSL_CTX_use_certificate_file(tlsctx, "ssl\\server1.crt" , SSL_FILETYPE_PEM);
SSL_CTX_use_PrivateKey_file(tlsctx, "ssl\\server1.key", SSL_FILETYPE_PEM);
ssl = SSL_new(tlsctx);
SSL_set_fd(ssl, client);
SSL_accept(ssl);

/* instaed recv SSL_read(ssl, ....*/
/* instaed send SSL_write(ssl, ....*/


/* not 100% sure Sleep and shutdown/free/close are entirely correct here but in my code works fine */
Sleep(3000);
SSL_shutdown(ssl);   
SSL_free(ssl);
SSL_CTX_free(tlsctx);
shutdown(client, SD_BOTH);
Sleep(10);
closesocket(client);

For testing: in command line run:

openssl s_client -host localhost -port <PORT>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.