Advertisement
duedeath

server.cpp

Apr 19th, 2024
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.48 KB | Source Code | 0 0
  1. #include <errno.h>
  2. #include <unistd.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <arpa/inet.h>
  6. #include <sys/socket.h>
  7. #include <sys/types.h>
  8. #include <netinet/in.h>
  9. #include <resolv.h>
  10. #include "openssl/ssl.h"
  11. #include "openssl/err.h"
  12. #define FAIL    -1
  13. // Create the SSL socket and intialize the socket address structure
  14. int OpenListener(int port)
  15. {
  16.     int sd;
  17.     struct sockaddr_in addr;
  18.     sd = socket(PF_INET, SOCK_STREAM, 0);
  19.     bzero(&addr, sizeof(addr));
  20.     addr.sin_family = AF_INET;
  21.     addr.sin_port = htons(port);
  22.     addr.sin_addr.s_addr = INADDR_ANY;
  23.     if (bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
  24.     {
  25.         perror("can't bind port");
  26.         abort();
  27.     }
  28.     if ( listen(sd, 10) != 0 )
  29.     {
  30.         perror("Can't configure listening port");
  31.         abort();
  32.     }
  33.     return sd;
  34. }
  35. int isRoot()
  36. {
  37.     if (getuid() != 0)
  38.     {
  39.         return 0;
  40.     }
  41.     else
  42.     {
  43.         return 1;
  44.     }
  45. }
  46. SSL_CTX* InitServerCTX(void)
  47. {
  48.     const SSL_METHOD *method;
  49.     SSL_CTX *ctx;
  50.     OpenSSL_add_all_algorithms();  /* load & register all cryptos, etc. */
  51.     SSL_load_error_strings();   /* load all error messages */
  52.     method = TLS_server_method();  /* create new server-method instance */
  53.     ctx = SSL_CTX_new(method);   /* create new context from method */
  54.     if ( ctx == NULL )
  55.     {
  56.         ERR_print_errors_fp(stderr);
  57.         abort();
  58.     }
  59.     return ctx;
  60. }
  61. void LoadCertificates(SSL_CTX* ctx, char* CertFile, char* KeyFile)
  62. {
  63.     /* set the local certificate from CertFile */
  64.     if ( SSL_CTX_use_certificate_file(ctx, CertFile, SSL_FILETYPE_PEM) <= 0 )
  65.     {
  66.         ERR_print_errors_fp(stderr);
  67.         abort();
  68.     }
  69.     /* set the private key from KeyFile (may be the same as CertFile) */
  70.     if ( SSL_CTX_use_PrivateKey_file(ctx, KeyFile, SSL_FILETYPE_PEM) <= 0 )
  71.     {
  72.         ERR_print_errors_fp(stderr);
  73.         abort();
  74.     }
  75.     /* verify private key */
  76.     if ( !SSL_CTX_check_private_key(ctx) )
  77.     {
  78.         fprintf(stderr, "Private key does not match the public certificate\n");
  79.         abort();
  80.     }
  81. }
  82. void ShowCerts(SSL* ssl)
  83. {
  84.     X509 *cert;
  85.     char *line;
  86.     cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */
  87.     if ( cert != NULL )
  88.     {
  89.         printf("Server certificates:\n");
  90.         line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
  91.         printf("Subject: %s\n", line);
  92.         free(line);
  93.         line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
  94.         printf("Issuer: %s\n", line);
  95.         free(line);
  96.         X509_free(cert);
  97.     }
  98.     else
  99.         printf("No certificates.\n");
  100. }
  101. void Servlet(SSL* ssl) /* Serve the connection -- threadable */
  102. {
  103.     char buf[1024] = {0};
  104.     int sd, bytes;
  105.     const char* ServerResponse="<Body>\
  106.                                <Name>aticleworld.com</Name>\
  107.                                <year>1.5</year>\
  108.                                <BlogType>Embedede and c\\c++</BlogType>\
  109.                                <Author>amlendra</Author>\
  110.                                </Body>";
  111.     const char *cpValidMessage = "<Body>\
  112.                                  <UserName>aticle</UserName>\
  113.                                  <Password>123</Password>\
  114.                                  </Body>";
  115.     if ( SSL_accept(ssl) == FAIL )     /* do SSL-protocol accept */
  116.         ERR_print_errors_fp(stderr);
  117.     else
  118.     {
  119.         ShowCerts(ssl);        /* get any certificates */
  120.         bytes = SSL_read(ssl, buf, sizeof(buf)); /* get request */
  121.         buf[bytes] = '\0';
  122.         printf("Client msg: \"%s\"\n", buf);
  123.         if ( bytes > 0 )
  124.         {
  125.             if(strcmp(cpValidMessage,buf) == 0)
  126.             {
  127.                 SSL_write(ssl, ServerResponse, strlen(ServerResponse)); /* send reply */
  128.             }
  129.             else
  130.             {
  131.                 SSL_write(ssl, "Invalid Message", strlen("Invalid Message")); /* send reply */
  132.             }
  133.         }
  134.         else
  135.         {
  136.             ERR_print_errors_fp(stderr);
  137.         }
  138.     }
  139.  
  140.  
  141.  
  142.     sd = SSL_get_fd(ssl);       /* get socket connection */
  143.     SSL_free(ssl);         /* release SSL state */
  144.     close(sd);          /* close connection */
  145. }
  146. int main(int count, char *Argc[])
  147. {
  148.     SSL_CTX *ctx;
  149.     int server;
  150.     char *portnum;
  151. //Only root user have the permsion to run the server
  152.     if(!isRoot())
  153.     {
  154.         printf("This program must be run as root/sudo user!!");
  155.         exit(0);
  156.     }
  157.     if ( count != 2 )
  158.     {
  159.         printf("Usage: %s <portnum>\n", Argc[0]);
  160.         exit(0);
  161.     }
  162.     // Initialize the SSL library
  163.     SSL_library_init();
  164.     portnum = Argc[1];
  165.     ctx = InitServerCTX();        /* initialize SSL */
  166.     LoadCertificates(ctx, "mycert.pem", "mycert.pem"); /* load certs */
  167.     server = OpenListener(atoi(portnum));    /* create server socket */
  168.     while (1)
  169.     {
  170.         struct sockaddr_in addr;
  171.         socklen_t len = sizeof(addr);
  172.         SSL *ssl;
  173.         int client = accept(server, (struct sockaddr*)&addr, &len);  /* accept connection as usual */
  174.         printf("Connection: %s:%d\n",inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
  175.         ssl = SSL_new(ctx);              /* get new SSL state with context */
  176.         SSL_set_fd(ssl, client);      /* set connection socket to SSL state */
  177.         Servlet(ssl);         /* service connection */
  178.     }
  179.     close(server);          /* close server socket */
  180.     SSL_CTX_free(ctx);         /* release context */
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement