Hi,

I am developing a client and server in C. Client encrypts some data and sends it to the server. The server receives the data and prints it out. I am using AES 128 bit encryption in CBC mode. But at the server side I am unable to receive any thing due to segmentation fault error.

Code is in the following:

Client Code:

#include "headerfiles.h"

#define MAXDATASIZE 256  


int main(int argc, char *argv[])
{
    
    int sockfd;
    int numbytes;
    int port;
    int in_len;
    int  out_len;
    char buf[MAXDATASIZE];
    char source[]="shahab";
    char *target;
    char mykey[EVP_MAX_KEY_LENGTH]="hardtobreak";
    char iv[EVP_MAX_IV_LENGTH] = "aniv";
    EVP_CIPHER_CTX ctx;

    target=malloc(sizeof(source));

    in_len=strlen(source);

    /*----------------------------------------------------------------------*/

     	EVP_CIPHER_CTX_init(&ctx);

	EVP_EncryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,mykey,iv);
	EVP_EncryptUpdate(&ctx,target,&out_len,source,in_len);
	EVP_EncryptFinal_ex(&ctx,target,&out_len);	


    /*----------------------------------------------------------------------*/

    struct hostent *he;
    struct sockaddr_in serverAddress; 

    
    if (argc != 3) {
         fprintf(stderr,"usage: client hostname\n");
         exit(1);
    }

    

    serverAddress.sin_addr.s_addr = inet_addr(argv[1]);
    he=gethostbyaddr((char *) &serverAddress.sin_addr.s_addr,sizeof(serverAddress.sin_addr.s_addr),AF_INET);
   
    
    port=atoi(argv[2]);

     

     if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
         perror("socket");
         exit(1);
     }
   
    
     serverAddress.sin_family = AF_INET;    
     serverAddress.sin_port = htons(port); 
     serverAddress.sin_addr = *((struct in_addr *)he->h_addr);

     
     
     memset(serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);

     

     if (connect(sockfd, (struct sockaddr *)&serverAddress,sizeof (serverAddress)) == -1) {
         perror("connect");
         exit(1);
     }
    
    
  /*  if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
         perror("recv");
         exit(1);
     }*/
    
    if (send(sockfd, target,  MAXDATASIZE, 0) == -1){

                 perror("send");
   }

   
  /* buf[numbytes] = '\0';
	
   
     printf("Received: %s",buf);*/
  
   
     close(sockfd);
     return 0;
 }

2. Server Code

#include "headerfiles.h"

#define SERVER_PORT 9990 
#define BACKLOG 10    
#define MAX_SIZE 500 

 int main(void)
 {
	
     int listenSocket;
     int activeSocket;
     int numberOfBytes; /*printf("Works");*/
     int socketOption;
     int bindCheck;
     int listenCheck;
     int sendCheck;
     int out_len,in_len;
     int yes=1;
     char buffer[MAX_SIZE];
     char mykey[EVP_MAX_KEY_LENGTH]="hardtobreak";
     char iv[EVP_MAX_IV_LENGTH] = "aniv"; 
     char *output;
     socklen_t client_size;
     EVP_CIPHER_CTX ctx;
	
     struct sockaddr_in serverAddress; 
     struct sockaddr_in clientAddress; 
     
     listenSocket = socket(AF_INET, SOCK_STREAM, 0);

	

     if(listenSocket==-1){

         perror("socket");
         exit(1);
     }
      
     socketOption=setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

     
     
     if(socketOption==-1){

         perror("setsockopt");
         exit(1);
     }

     bzero(&serverAddress,sizeof(serverAddress));    

     serverAddress.sin_family = AF_INET;         
     serverAddress.sin_port = htons(SERVER_PORT);     
     serverAddress.sin_addr.s_addr = INADDR_ANY; 
     
     bindCheck=bind(listenSocket, (struct sockaddr *)&serverAddress, sizeof (serverAddress));

    
    
     if(bindCheck==-1){
	
         perror("bind");
         exit(1);
     }

     
     listenCheck=listen(listenSocket, BACKLOG);

      
      if(listenCheck==-1){

         perror("listen");
         exit(1);
     }
     
     
     for(;;) { 

	
         client_size = sizeof (clientAddress);

	 
         activeSocket = accept(listenSocket, (struct sockaddr *)&clientAddress,  &client_size);

	
	if(activeSocket==-1){

             perror("accept");
             continue;
         }
	
       
       /* sendCheck=send(activeSocket, "Hello, world From Server!\n", MAX_SIZE, MSG_DONTWAIT);

	
	if(sendCheck==-1){

                 perror("send");
   	}*/

	
	numberOfBytes=recv(activeSocket, buffer,MAX_SIZE-1, 0);

	
	if(numberOfBytes==-1) 
	{
         perror("recv");
         exit(1);
     	}

	in_len=sizeof(buffer);	
	/*-----------------------------------------------------------*/
		EVP_CIPHER_CTX_init(&ctx);

		EVP_DecryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,mykey,iv);
		EVP_DecryptUpdate(&ctx,output,&out_len,buffer,in_len);
		EVP_DecryptFinal_ex(&ctx,output,&out_len);


       /*-----------------------------------------------------------*/
	
       output[out_len] = '\0';

       
       printf("Received: %s",output);

      
     }

  
  close(activeSocket);
	     
return 0;
 }

Thanks in Advance for your help.

You might try by finding out where the segmentation fault occurs either by using a symbolic debugger or if one is unavailable the more sedge hammer method of commenting out bits of code until you find the bit that causes the crash.

However I suspect that pass the buffer size rather than the amount of data received to the dycryption functions is a mistake as it will presumably be trying to decrypt the random data in the buffer following the actualy received data.

You are also making some false assumptions about you networking communications. You are using SOCK_STREAM so presumably a tcp socket. A tcp socket provides a coonection based byte stream. However you have assumed that it is packetised. That is you have assumed that for every transmit (send) called at the client your sever will receive precisesly i receive (recv) call.

That is just not true. Imagine your client calls send 3 times to send 3 bunches of encrypted data. Your server may receive the whole lot in 1 recv call or it may need to call recv more than 3 times to receive all the data.

I suggest you read Beej's Guide to Network Programming which IMO is quite a good primer.

This article has been dead for over six months. Start a new discussion instead.