Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int sec_init(int socket, int verbose, int reliable, int secure)
- {
- frag_init();
- if(!secure)
- return 1;
- /* Client sends 'get public key' to the server, using frag_send_data() */
- char get_key[15];
- strcpy(get_key, "get public key");
- int length = 15;
- frag_send_data(socket, get_key, length, verbose, reliable);
- /* Receive the string that the server sends, which will contain its public key. */
- char *public_key = calloc(1, sizeof(PUB_KEY_SIZE));
- int public_len = PUB_KEY_SIZE;
- frag_receive_data(socket, &public_key, &public_len, verbose, reliable);
- /* Generate a string of 160 random bits for use as the secret key. */
- int i;
- time_t seconds;
- time(&seconds);
- srand((unsigned int) seconds);
- for (i = 0; i < SECRET_KEY_SIZE; i++)
- {
- secret_key[i] = (unsigned char) rand();
- }
- /* Encrypt the random string with the public key. */
- char out[ENCRYPTED_KEY_SIZE];
- sec_pk_encrypt((char*) &secret_key[SECRET_KEY_SIZE], &out[ENCRYPTED_KEY_SIZE], &public_key[public_len]);
- /* Send the encrypted string to the server. */
- frag_send_data(socket, out, ENCRYPTED_KEY_SIZE, verbose, reliable);
- /* Receive the server's response.
- If 'OK' then the random string is assigned as secret_key.
- */
- char *server_response = calloc(MAX_DATA, sizeof(char));
- int server_len;
- frag_receive_data(socket, &server_response, &server_len, verbose, reliable);
- fprintf(stderr, "Received server resp\n");
- int j = strcmp(server_response, "OK");
- fprintf(stderr,"got comparison\n");
- if (j == 0)
- {
- fprintf(stderr,"About to return 1\n");
- //free(public_key);
- //free(server_response);
- fprintf(stderr,"Freed\n");
- return 1;
- }
- fprintf(stderr, "Sup.\n%s, %d\n", server_response, server_len);
- return 0; // <--- TODO: Add your security code here (instead of this line).
- }
- // If security is turned on, decrypts the data received from the fragmentation layer.
- // Takes the socket ID, a pointer to the received data buffer and a pointer to its length.
- // returns 1 for success or 0 for failure
- int sec_receive_data(int socket, char** pBuf, int *pLen, int verbose, int reliable, int secure)
- {
- if(!secure)
- {
- return frag_receive_data(socket, pBuf, pLen, verbose, reliable);
- }
- return 0; // <--- TODO: Add your security code here (instead of this line).
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement