项目作者: ElectronSz

项目描述 :
This is a client - server user-level application using sockets Programming in C. Server accepts strings from client and can reply to client. Both server and client(s) output's of chat shows on terminal.The server and client processes can run on same or different machines. Server and client connection is encrypted, send and receive messages can't be traced by any intruder as we are using OpenSSL certificates for encryption. In this post only we will have a brief overview of creating Openssl certificates using OpenSSL tool.
高级语言: C
项目地址: git://github.com/ElectronSz/Encrypted-Chat-Server-Using-C-Programming-and-OpenSSL-AES-DES.git


Encrypted-Chat-Server-Using-C++-Programming-and-OpenSSL-AES-DES

This is a client - server user-level application using sockets Programming in C. Server accepts strings from client and can reply to client. Both server and client(s) output’s of chat shows on terminal.The server and client processes can run on same or different machines. Server and client connection is encrypted, send and receive messages can’t be traced by any intruder as we are using OpenSSL certificates for encryption. In this post only we will have a brief overview of creating Openssl certificates using OpenSSL tool.

Various Features Included :

  1. * Usage of Socket Programming for creating Client & Server program
  2. * Multi-threading for Full Duplex Communication between Client and Server
  3. * Encrypted Communication between Client and Server
  4. * Usage of Basic Networking Concepts & Linux Os for executing client and server programs

Future Improvements :

  1. - Accept multiple Clients
  2. - Create Chat Logs between client and server
  3. - Add file transfer + Video conferencing (need to use Java | Python programming)
  4. - Using C graphics library add GUI (graphics user interface)
  5. - Name and rename users | Block clients

Server.c

  1. #include <unistd.h> /*FOR USING FORK for at a time send and receive messages*/
  2. #include <errno.h> /*USING THE ERROR LIBRARY FOR FINDING ERRORS*/
  3. #include <malloc.h> /*FOR MEMORY ALLOCATION */
  4. #include <string.h> /*using fgets funtions for geting input from user*/
  5. #include <arpa/inet.h> /*for using ascii to network bit*/
  6. #include <sys/socket.h> /*for creating sockets*/
  7. #include <sys/types.h> /*for using sockets*/
  8. #include <netinet/in.h> /* network to asii bit */
  9. #include <resolv.h> /*server to find out the runner's IP address*/
  10. #include "openssl/ssl.h" /*using openssl function's and certificates and configuring them*/
  11. #include "openssl/err.h" /* helps in finding out openssl errors*/
  12. #include <stdio.h> /*standard i/o*/
  13. #define FAIL -1 /*for error output == -1 */
  14. #define BUFFER 1024 /*buffer for reading messages*/
  15. int OpenListener(int port)
  16. { int sd;
  17. struct sockaddr_in addr; /*creating the sockets*/
  18. sd = socket(PF_INET, SOCK_STREAM, 0);
  19. bzero(&addr, sizeof(addr)); /*free output the garbage space in memory*/
  20. addr.sin_family = AF_INET; /*getting ip address form machine */
  21. addr.sin_port = htons(port); /* converting host bit to n/w bit */
  22. addr.sin_addr.s_addr = INADDR_ANY;
  23. if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) /* assiging the ip address and port*/
  24. {
  25. perror("can't bind port"); /* reporting error using errno.h library */
  26. abort(); /*if error will be there then abort the process */
  27. }
  28. if ( listen(sd, 10) != 0 ) /*for listening to max of 10 clients in the queue*/
  29. {
  30. perror("Can't configure listening port"); /* reporting error using errno.h library */
  31. abort(); /*if erroor will be there then abort the process */
  32. }
  33. return sd;
  34. }
  35. int isRoot() /*for checking if the root user is executing the server*/
  36. {
  37. if (getuid() != 0)
  38. {
  39. return 0;
  40. }
  41. else
  42. {
  43. return 1; /* if root user is not executing report must be user */
  44. }
  45. }
  46. SSL_CTX* InitServerCTX(void) /*creating and setting up ssl context structure*/
  47. { SSL_METHOD *method;
  48. SSL_CTX *ctx;
  49. OpenSSL_add_all_algorithms(); /* load & register all cryptos, etc. */
  50. SSL_load_error_strings(); /* load all error messages */
  51. method = TLSv1_2_server_method(); /* create new server-method instance */
  52. ctx = SSL_CTX_new(method); /* create new context from method */
  53. if ( ctx == NULL )
  54. {
  55. ERR_print_errors_fp(stderr);
  56. abort();
  57. }
  58. return ctx;
  59. }
  60. void LoadCertificates(SSL_CTX* ctx, char* CertFile, char* KeyFile) /* to load a certificate into an SSL_CTX structure*/
  61. {
  62. /* set the local certificate from CertFile */
  63. if ( SSL_CTX_use_certificate_file(ctx, CertFile, SSL_FILETYPE_PEM) <= 0 )
  64. {
  65. ERR_print_errors_fp(stderr);
  66. abort();
  67. }
  68. /* set the private key from KeyFile (may be the same as CertFile) */
  69. if ( SSL_CTX_use_PrivateKey_file(ctx, KeyFile, SSL_FILETYPE_PEM) <= 0 )
  70. {
  71. ERR_print_errors_fp(stderr);
  72. abort();
  73. }
  74. /* verify private key */
  75. if ( !SSL_CTX_check_private_key(ctx) )
  76. {
  77. fprintf(stderr, "Private key does not match the public certificate\n");
  78. abort();
  79. }
  80. }
  81. void ShowCerts(SSL* ssl) /*show the ceritficates to client and match them*/
  82. { X509 *cert;
  83. char *line;
  84. cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */
  85. if ( cert != NULL )
  86. {
  87. printf("Server certificates:\n");
  88. line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
  89. printf("Server: %s\n", line); /*server certifcates*/
  90. free(line);
  91. line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
  92. printf("client: %s\n", line); /*client certificates*/
  93. free(line);
  94. X509_free(cert);
  95. }
  96. else
  97. printf("No certificates.\n");
  98. }
  99. void Servlet(SSL* ssl) /* Serve the connection -- threadable */
  100. { char buf[1024];
  101. int sd, bytes;
  102. char input[BUFFER];
  103. pid_t cpid;
  104. if ( SSL_accept(ssl) == FAIL ) /* do SSL-protocol accept */
  105. ERR_print_errors_fp(stderr);
  106. else
  107. {
  108. ShowCerts(ssl); /* get any certificates */
  109. /*Fork system call is used to create a new process*/
  110. cpid=fork();
  111. if(cpid==0)
  112. {
  113. while(1){
  114. bytes = SSL_read(ssl, buf, sizeof(buf)); /* get request and read message from server*/
  115. if ( bytes > 0 )
  116. {
  117. buf[bytes] = 0;
  118. printf("\nMESSAGE FROM SERVER:%s\n", buf);
  119. }
  120. else
  121. ERR_print_errors_fp(stderr);
  122. } }
  123. else {
  124. while(1){
  125. printf("\nMESSAGE TO CLIENT:");
  126. fgets(input, BUFFER, stdin); /* get request and reply to client*/
  127. SSL_write(ssl, input, strlen(input));
  128. }
  129. }
  130. }
  131. sd = SSL_get_fd(ssl); /* get socket connection */
  132. SSL_free(ssl); /* release SSL state */
  133. close(sd); /* close connection */
  134. }
  135. int main(int count, char *strings[]) /* getting port as a argument*/
  136. { SSL_CTX *ctx;
  137. int server;
  138. char *portnum;
  139. if(!isRoot()) /* if root user is not executing server report must be root user */
  140. {
  141. printf("This program must be run as root/sudo user!!");
  142. exit(0);
  143. }
  144. if ( count != 2 )
  145. {
  146. printf("Usage: %s \n", strings[0]); /*send the usage guide if syntax of setting port is different*/
  147. exit(0);
  148. }
  149. SSL_library_init(); /*load encryption and hash algo's in ssl*/
  150. portnum = strings[1];
  151. ctx = InitServerCTX(); /* initialize SSL */
  152. LoadCertificates(ctx, "certi.pem", "certi.pem"); /* load certs */
  153. server = OpenListener(atoi(portnum)); /* create server socket */
  154. struct sockaddr_in addr; /*socket for server*/
  155. socklen_t len = sizeof(addr);
  156. SSL *ssl;
  157. listen(server,5); /*setting 5 clients at a time to queue*/
  158. int client = accept(server, (struct sockaddr*)&addr, &len); /* accept connection as usual */
  159. printf("Connection: %s:%d\n",inet_ntoa(addr.sin_addr), ntohs(addr.sin_port)); /*printing connected client information*/
  160. ssl = SSL_new(ctx); /* get new SSL state with context */
  161. SSL_set_fd(ssl, client); /* set connection socket to SSL state */
  162. Servlet(ssl); /* service connection */
  163. close(server); /* close server socket */
  164. SSL_CTX_free(ctx); /* release context */
  165. }

Client.c

  1. #include <errno.h> /*USING THE ERROR LIBRARY FOR FINDING ERRORS*/
  2. #include <stdio.h> /*standard i/o*/
  3. #include <unistd.h> /*FOR USING FORK for at a time send and receive messages*/
  4. #include <malloc.h> /*FOR MEMORY ALLOCATION */
  5. #include <string.h> /*using fgets funtions for geting input from user*/
  6. #include <sys/socket.h> /*for creating sockets*/
  7. #include <resolv.h> /*server to find out the runner's IP address*/
  8. #include <netdb.h> /*definitions for network database operations */
  9. #include <openssl/ssl.h> /*using openssl function's and certificates and configuring them*/
  10. #include <openssl/err.h> /* helps in finding out openssl errors*/
  11. #include <unistd.h> /*FOR USING FORK for at a time send and receive messages*/
  12. #define FAIL -1 /*for error output == -1 */
  13. #define BUFFER 1024 /*buffer for reading messages*/
  14. int OpenConnection(const char *hostname, int port)
  15. { int sd;
  16. struct hostent *host;
  17. struct sockaddr_in addr; /*creating the sockets*/
  18. if ( (host = gethostbyname(hostname)) == NULL )
  19. {
  20. perror(hostname);
  21. abort();
  22. }
  23. sd = socket(PF_INET, SOCK_STREAM, 0); /* setting the connection as tcp it creates endpoint for connection */
  24. bzero(&addr, sizeof(addr));
  25. addr.sin_family = AF_INET;
  26. addr.sin_port = htons(port);
  27. addr.sin_addr.s_addr = *(long*)(host->h_addr);
  28. if ( connect(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) /*initiate a connection on a socket*/
  29. {
  30. close(sd);
  31. perror(hostname);
  32. abort();
  33. }
  34. return sd;
  35. }
  36. SSL_CTX* InitCTX(void) /*creating and setting up ssl context structure*/
  37. { SSL_METHOD *method;
  38. SSL_CTX *ctx;
  39. OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */
  40. SSL_load_error_strings(); /* Bring in and register error messages */
  41. method = TLSv1_2_client_method(); /* Create new client-method instance */
  42. ctx = SSL_CTX_new(method); /* Create new context */
  43. if ( ctx == NULL )
  44. {
  45. ERR_print_errors_fp(stderr);
  46. abort();
  47. }
  48. return ctx;
  49. }
  50. void ShowCerts(SSL* ssl) /*show the ceritficates to server and match them but here we are not using any client certificate*/
  51. { X509 *cert;
  52. char *line;
  53. cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
  54. if ( cert != NULL )
  55. {
  56. printf("Server certificates:\n");
  57. line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
  58. printf("Subject: %s\n", line);
  59. free(line); /* free the malloc'ed string */
  60. line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
  61. printf("Issuer: %s\n", line);
  62. free(line); /* free the malloc'ed string */
  63. X509_free(cert); /* free the malloc'ed certificate copy */
  64. }
  65. else
  66. printf("Info: No client certificates configured.\n");
  67. }
  68. int main(int count, char *strings[]) /* getting port and ip as an argument*/
  69. { SSL_CTX *ctx;
  70. int server;
  71. SSL *ssl;
  72. char buf[1024];
  73. char input[BUFFER];
  74. int bytes;
  75. char *hostname, *portnum;
  76. pid_t cpid; /* fork variable*/
  77. if ( count != 3 )
  78. {
  79. printf("usage: %s \n", strings[0]);
  80. exit(0);
  81. }
  82. SSL_library_init(); /*load encryption and hash algo's in ssl*/
  83. hostname=strings[1];
  84. portnum=strings[2];
  85. ctx = InitCTX();
  86. server = OpenConnection(hostname, atoi(portnum)); /*converting ascii port to interger */
  87. ssl = SSL_new(ctx); /* create new SSL connection state */
  88. SSL_set_fd(ssl, server); /* attach the socket descriptor */
  89. if ( SSL_connect(ssl) == FAIL ) /* perform the connection */
  90. ERR_print_errors_fp(stderr);
  91. else
  92. {
  93. printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
  94. ShowCerts(ssl);
  95. /* get any certs */
  96. cpid=fork();
  97. /*Fork system call is used to create a new process*/
  98. if(cpid==0)
  99. {
  100. while(1){
  101. printf("\nMESSAGE TO SERVER:");
  102. fgets(input, BUFFER, stdin);
  103. SSL_write(ssl, input, strlen(input)); /* encrypt & send message */}}
  104. else {
  105. while(1)
  106. {
  107. bytes = SSL_read(ssl, buf, sizeof(buf)); /* get request */
  108. if ( bytes > 0 )
  109. {
  110. buf[bytes] = 0;
  111. printf("\nMESSAGE FROM CLIENT: %s\n", buf);
  112. }
  113. } }
  114. SSL_free(ssl); /* release connection state */
  115. } close(server); /* close socket */
  116. SSL_CTX_free(ctx); /* release context */
  117. return 0;
  118. }

Executing Commands :

Creating OpenSSL certificates :

+openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

Creating certificate with Authentication:

+OpenSSL Guide: https://help.ubuntu.com/community/OpenSS

Compiling Server.c :

  • Command Line : gcc -Wall -o ssl-server ssl-server.c -L/usr/lib -lssl -lcrypto

Executing Server :

  • Command Line : sudo ./ssl-server || Ex: sudo ./ssl-server 6000

Compiling Client.c :

  • Command Line : gcc -Wall -o ssl-client ssl-client.c -L/usr/lib -lssl -lcrypto

Executing Client :

  • Command Line : ./ssl-client || ./ssl-client 192.168.43.54 6000

Monitoring Traffic using ssldump :

  • ssldump -i wlan0 port 6000