Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. int sec_init(int socket, int verbose, int reliable, int secure)
  2. {
  3.     frag_init();
  4.  
  5.     if(!secure)
  6.         return 1;
  7.  
  8.     /* Client sends 'get public key' to the server, using frag_send_data() */
  9.     char get_key[15];
  10.     strcpy(get_key, "get public key");
  11.     int length = 15;
  12.     frag_send_data(socket, get_key, length, verbose, reliable);
  13.     /* Receive the string that the server sends, which will contain its public key. */
  14.     char *public_key = calloc(1, sizeof(PUB_KEY_SIZE));
  15.     int public_len = PUB_KEY_SIZE;
  16.     frag_receive_data(socket, &public_key, &public_len, verbose, reliable);
  17.     /* Generate a string of 160 random bits for use as the secret key.  */
  18.     int i;
  19.     time_t seconds;
  20.     time(&seconds);
  21.     srand((unsigned int) seconds);
  22.     for (i = 0; i < SECRET_KEY_SIZE; i++)
  23.     {
  24.         secret_key[i] = (unsigned char) rand();
  25.     }
  26.     /* Encrypt the random string with the public key. */
  27.     char out[ENCRYPTED_KEY_SIZE];
  28.     sec_pk_encrypt((char*) &secret_key[SECRET_KEY_SIZE], &out[ENCRYPTED_KEY_SIZE], &public_key[public_len]);
  29.     /* Send the encrypted string to the server. */
  30.     frag_send_data(socket, out, ENCRYPTED_KEY_SIZE, verbose, reliable);
  31.     /* Receive the server's response.
  32.     If 'OK' then the random string is assigned as secret_key.
  33.     */
  34.     char *server_response = calloc(MAX_DATA, sizeof(char));
  35.     int server_len;
  36.     frag_receive_data(socket, &server_response, &server_len, verbose, reliable);
  37.     fprintf(stderr, "Received server resp\n");
  38.     int j = strcmp(server_response, "OK");
  39.     fprintf(stderr,"got comparison\n");
  40.     if (j == 0)
  41.     {
  42.         fprintf(stderr,"About to return 1\n");
  43.         //free(public_key);
  44.         //free(server_response);
  45.         fprintf(stderr,"Freed\n");
  46.         return 1;
  47.     }
  48.  
  49.     fprintf(stderr, "Sup.\n%s, %d\n", server_response, server_len);
  50.     return 0;  //  <--- TODO: Add your security code here (instead of this line).
  51. }
  52.  
  53. // If security is turned on, decrypts the data received from the fragmentation layer.
  54. // Takes the socket ID, a pointer to the received data buffer and a pointer to its length.
  55. // returns 1 for success or 0 for failure
  56. int sec_receive_data(int socket, char** pBuf, int *pLen, int verbose, int reliable, int secure)
  57. {
  58.     if(!secure)
  59.     {
  60.         return frag_receive_data(socket, pBuf, pLen, verbose, reliable);
  61.     }
  62.  
  63.     return 0; //  <--- TODO: Add your security code here (instead of this line).
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement