make encryption optional

This commit is contained in:
PolynomialDivision 2018-01-22 23:27:57 +01:00
parent cc2fc17ecd
commit f9d7ac0adf
4 changed files with 46 additions and 22 deletions

View file

@ -5,6 +5,7 @@ config network
option network_option '2' # 0 udp broadcast, 1 mutlicast, 2 tcp
option shared_key 'Niiiiiiiiiiiiiik'
option iv 'Niiiiiiiiiiiiiik'
option use_symm_enc '1'
config ordering
option sort_order 'cbfs'

View file

@ -80,6 +80,7 @@ struct network_config_s {
const char *shared_key;
const char *iv;
int bool_multicast;
int use_symm_enc;
};
struct network_config_s network_config;

View file

@ -69,6 +69,8 @@ static void client_read_cb(struct ustream *s, int bytes) {
//printf("RECEIVED String: %s\n", str);
if(network_config.use_symm_enc)
{
char *base64_dec_str = malloc(Base64decode_len(str));
int base64_dec_length = Base64decode(base64_dec_str, str);
char *dec = gcrypt_decrypt_msg(base64_dec_str, base64_dec_length);
@ -76,6 +78,9 @@ static void client_read_cb(struct ustream *s, int bytes) {
free(base64_dec_str);
handle_network_msg(dec);
free(dec);
} else {
handle_network_msg(str);
}
ustream_consume(s, len);
@ -177,9 +182,9 @@ void send_tcp(char *msg) {
printf("SENDING TCP!\n");
pthread_mutex_lock(&tcp_array_mutex);
size_t msglen = strlen(msg);
if(network_config.use_symm_enc) {
int length_enc;
size_t msglen = strlen(msg);
char *enc = gcrypt_encrypt_msg(msg, msglen + 1, &length_enc);
char *base64_enc_str = malloc(Base64encode_len(length_enc));
@ -200,6 +205,22 @@ void send_tcp(char *msg) {
}
free(base64_enc_str);
free(enc);
} else {
for (int i = 0; i <= tcp_entry_last; i++) {
if (send(network_array[i].sockfd, msg, strlen(msg), 0) < 0) {
close(network_array->sockfd);
printf("Removing bad TCP connection!\n");
for (int j = i; j < tcp_entry_last; j++) {
network_array[j] = network_array[j + 1];
}
if (tcp_entry_last > -1) {
tcp_entry_last--;
}
}
}
}
pthread_mutex_unlock(&tcp_array_mutex);
}

View file

@ -99,6 +99,7 @@ struct network_config_s uci_get_dawn_network() {
ret.iv = uci_lookup_option_string(uci_ctx, s, "iv");
ret.network_option = uci_lookup_option_int(uci_ctx, s, "network_option");
ret.tcp_port = uci_lookup_option_int(uci_ctx, s, "tcp_port");
ret.use_symm_enc = uci_lookup_option_int(uci_ctx, s, "use_symm_enc");
return ret;
}
}