static boolean SSL_recv_phr_file(SSL *peer, char *file_path)
{
	unlink(file_path);

	char         *buf = NULL;  // Include null-terminated character
	unsigned int data_len;
	char         data_len_str[LARGE_FILE_MAX_DATA_DIGIT_LENGTH + 1];
	int          ret_code;

	char         buffer[BUFFER_LENGTH + 1];
	char         token_name[TOKEN_NAME_LENGTH + 1];
	char         file_size_str_tmp[INT_TO_STR_DIGITS_LENGTH + 1];
	unsigned int file_size;
	unsigned int nreceived = 0;
	float        received_percent;

	// Allocate heap variable
	buf = (char *)malloc(sizeof(char)*LARGE_FILE_BUF_SIZE);
	if(!buf)
	{
		int_error("Allocating memory for \"buf\" failed");
	}

	// Receive the PHR file size
	if(SSL_recv_buffer(peer, buffer, NULL) == 0)
		goto ERROR;

	// Get the PHR file size token from buffer
	if(read_token_from_buffer(buffer, 1, token_name, file_size_str_tmp) != READ_TOKEN_SUCCESS || strcmp(token_name, "file_size") != 0)
	{
		int_error("Extracting the file_size failed");
	}

	file_size = atoi(file_size_str_tmp);

	for(;;)
    	{
		if(emergency_phr_downloading_cancellation_flag)
			goto OPERATION_HAS_BEEN_CANCELLED;

		// Read data from peer
		ret_code = SSL_recv(peer, buf, LARGE_FILE_BUF_SIZE);
		if(ret_code != SSL_ERROR_NONE)
		{
			// Transmission error occurred
			goto ERROR;
		}

		if(buf[0] == '1')    	// End of file
			break;

		memcpy(data_len_str, buf + 1, LARGE_FILE_MAX_DATA_DIGIT_LENGTH);
		data_len_str[LARGE_FILE_MAX_DATA_DIGIT_LENGTH] = 0;

		if(emergency_phr_downloading_cancellation_flag)
			goto OPERATION_HAS_BEEN_CANCELLED;
	
		// Write data to file
		data_len = atoi(data_len_str);
		if(!write_bin_file(file_path, "ab", buf + LARGE_FILE_PREFIX_SIZE, data_len))
		{
			// Writing file failed
			goto ERROR;
		}

		nreceived        += data_len;
		received_percent =  ((float)nreceived)/file_size*100.0F;
		update_emergency_phr_received_progression_handler_callback(received_percent);
    	}

	// Free heap variable
	if(buf)
	{
		free(buf);
		buf = NULL;
	}

	return true;

ERROR:
OPERATION_HAS_BEEN_CANCELLED:

	// Free heap variable
	if(buf)
	{
		free(buf);
		buf = NULL;
	}

	return false;
}
Пример #2
0
bool long_class::write_bin_file_class(char* filename, bool flag, bool flag_mul, bool flag_sum, bool flag_sub)
{
	return write_bin_file(filename, this->me, flag, flag_mul, flag_sum, flag_sub) == 0;
}
Пример #3
0
static boolean ssl_cert_response(BIO *bio_client, char *username, boolean is_admin_flag, char *key_exchange_passwd)
{
	char          *ssl_cert_data = NULL;
	unsigned long ssl_cert_data_length;
	char          ssl_cert_hash[SHA1_DIGEST_LENGTH + 1];

	MYSQL         *db_conn = NULL;
  	MYSQL_RES     *result  = NULL;
  	MYSQL_ROW     row;
	char          stat[SQL_STATEMENT_LENGTH + 1];
	char	      err_msg[ERR_MSG_LENGTH + 1];
	unsigned long *lengths = NULL;

	ssl_cert_data = (char *)malloc(sizeof(char)*1000*1024);
	if(!ssl_cert_data)
	{
		int_error("Allocating memory for \"ssl_cert_data\" failed");
	}

	// Connect the database
	connect_db(&db_conn, DB_IP, DB_USERNAME, DB_PASSWD, DB_NAME);

	// Query the user's SSL certificate and write it to buffer
	sprintf(stat, "SELECT enc_ssl_cert, enc_ssl_cert_hash FROM %s WHERE username LIKE '%s' "
		"COLLATE latin1_general_cs", (is_admin_flag) ? UA__ADMINS : UA__USERS, username);

	if(mysql_query(db_conn, stat))
  	{
      		sprintf(err_msg, "Error %u: %s\n", mysql_errno(db_conn), mysql_error(db_conn));
      		int_error(err_msg);
  	}

  	result = mysql_store_result(db_conn);
  	row = mysql_fetch_row(result);
	if(!row)
		int_error("Getting an SSL certificate from the database failed");

	lengths = mysql_fetch_lengths(result);
	ssl_cert_data_length = lengths[0];

	memcpy(ssl_cert_data, row[0], ssl_cert_data_length);
	strcpy(ssl_cert_hash, row[1]);

	if(result)
	{
		mysql_free_result(result);
		result = NULL;
	}
	disconnect_db(&db_conn);

	// Write the SSL ceertificate to file and encrypt it with random session password before sending it to the user
	if(!write_bin_file(SSL_CERT_PLAINTEXT_PATH, "wb", ssl_cert_data, ssl_cert_data_length))
	{
		fprintf(stderr, "Writing the SSL certificate hash failed");
		goto ERROR;
	}

	if(!des3_encrypt(SSL_CERT_PLAINTEXT_PATH, SSL_CERT_CIPHERTEXT_PATH, key_exchange_passwd, err_msg))
	{
		fprintf(stderr, "Decrypting the SSL certificate failed\n\"%s\"\n", err_msg);
		goto ERROR;
	}

	unlink(SSL_CERT_PLAINTEXT_PATH);

	// Send the user's SSL certificate
	if(!BIO_send_file(bio_client, SSL_CERT_CIPHERTEXT_PATH))
	{
		fprintf(stderr, "Sending the SSL certificate failed\n");
		goto ERROR;
	}

	unlink(SSL_CERT_CIPHERTEXT_PATH);

	// Write hash value to file and encrypt it with random session password before sending it to the user
	if(!write_bin_file(SSL_CERT_HASH_PLAINTEXT_PATH, "wb", ssl_cert_hash, SHA1_DIGEST_LENGTH))
	{
		fprintf(stderr, "Writing the SSL certificate hash failed");
		goto ERROR;
	}

	if(!des3_encrypt(SSL_CERT_HASH_PLAINTEXT_PATH, SSL_CERT_HASH_CIPHERTEXT_PATH, key_exchange_passwd, err_msg))
	{
		fprintf(stderr, "Decrypting the SSL certificate hash failed\n\"%s\"\n", err_msg);
		goto ERROR;
	}

	unlink(SSL_CERT_HASH_PLAINTEXT_PATH);

	// Send the user's SSL certificate hash
	if(!BIO_send_file(bio_client, SSL_CERT_HASH_CIPHERTEXT_PATH))
	{
		fprintf(stderr, "Sending the SSL certificate hash failed\n");
		goto ERROR;
	}

	unlink(SSL_CERT_HASH_CIPHERTEXT_PATH);

	if(ssl_cert_data)
	{
		free(ssl_cert_data);
		ssl_cert_data = NULL;
	}

	return true;

ERROR:
	unlink(SSL_CERT_PLAINTEXT_PATH);
	unlink(SSL_CERT_CIPHERTEXT_PATH);
	unlink(SSL_CERT_HASH_PLAINTEXT_PATH);
	unlink(SSL_CERT_HASH_CIPHERTEXT_PATH);

	if(ssl_cert_data)
	{
		free(ssl_cert_data);
		ssl_cert_data = NULL;
	}

	return false;
}