Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <winsock2.h>
- // #include <Windows.h>
- #include <openssl/ssl.h>
- #include <openssl/err.h>
- #include <openssl/applink.c>
- #define zero {}
- #define func static
- #include "generated\generated_main.cpp"
- int main()
- {
- WSADATA wsa_data = zero;
- int err = 0;
- err = WSAStartup(MAKEWORD(2, 2), &wsa_data);
- if(err != 0) {
- error("BAD\n");
- }
- SSL_library_init();
- SSL_load_error_strings();
- OpenSSL_add_all_algorithms();
- SSL_CTX* ctx = SSL_CTX_new(TLS_server_method());
- if(!ctx) {
- error("BAD ctx\n");
- }
- if(SSL_CTX_use_certificate_file(ctx, "localhost.crt", SSL_FILETYPE_PEM) <= 0) {
- ERR_print_errors_fp(stderr);
- error("bad cert");
- }
- if(SSL_CTX_use_PrivateKey_file(ctx, "localhost.key", SSL_FILETYPE_PEM) <= 0) {
- ERR_print_errors_fp(stderr);
- error("bad key");
- }
- SOCKET server_fd = socket(AF_INET, SOCK_STREAM, 0);
- if(server_fd == INVALID_SOCKET) {
- error("failed to create socket");
- }
- sockaddr_in server_addr = zero;
- server_addr.sin_family = AF_INET;
- server_addr.sin_addr.s_addr = INADDR_ANY;
- server_addr.sin_port = htons(443);
- err = bind(server_fd, (sockaddr*)&server_addr, sizeof(server_addr));
- if(err == SOCKET_ERROR) {
- error("bad bind");
- }
- err = listen(server_fd, SOMAXCONN);
- if(err == SOCKET_ERROR) {
- error("bad listen");
- }
- sockaddr_in client_addr = zero;
- int client_len = sizeof(client_addr);
- printf("Waiting for connection...\n");
- SOCKET client_fd = accept(server_fd, (sockaddr*)&client_addr, &client_len);
- if(client_fd == INVALID_SOCKET) {
- error("bad accept");
- }
- printf("FOOBAR\n");
- SSL* ssl = SSL_new(ctx);
- SSL_set_fd(ssl, (int)client_fd);
- err = SSL_accept(ssl);
- if(err <= 0) {
- // err = SSL_get_error(ssl, err);
- // switch (err) {
- // case SSL_ERROR_WANT_READ:
- // case SSL_ERROR_WANT_WRITE:
- // {
- // printf("try again\n");
- // } break;
- // case SSL_ERROR_ZERO_RETURN: {
- // printf("tcp closed\n");
- // } break;
- // default: {
- // printf("fatal %i\n", err);
- // } break;
- // }
- printf("bad ssl_accept\n");
- }
- char* reply = "it's a cheat code";
- // SSL_write(ssl, reply, (int)strlen(reply));
- while(true) {
- char buffer[1024] = zero;
- int bytes_read = SSL_read(ssl, buffer, sizeof(buffer));
- if(bytes_read > 0) {
- printf("received: %s\n", buffer);
- }
- else {
- ERR_print_errors_fp(stderr);
- }
- }
- // Print OpenSSL version
- printf("OpenSSL version: %s\n", OpenSSL_version(OPENSSL_VERSION));
- getchar();
- }
- func void error(char* str)
- {
- printf("%s\n", str);
- getchar();
- exit(1);
- }
Add Comment
Please, Sign In to add comment