SSL api for winsock?

…衆ロ難τιáo~ 提交于 2019-12-29 09:11:10

问题


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..


回答1:


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:


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>


来源:https://stackoverflow.com/questions/10170861/ssl-api-for-winsock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!