SSL api for winsock?

后端 未结 2 558
轻奢々
轻奢々 2020-12-21 12:23

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

相关标签:
2条回答
  • 2020-12-21 12:48

    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.

    0 讨论(0)
  • 2020-12-21 12:55

    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>
    
    0 讨论(0)
提交回复
热议问题