mirror of
https://github.com/berlin-open-wireless-lab/DAWN.git
synced 2025-03-09 15:40:12 +00:00
Add base64 encoding (own lib)
This commit is contained in:
parent
7ba1f28f6b
commit
b45b120773
8 changed files with 196 additions and 60 deletions
|
@ -30,7 +30,12 @@ SET(SOURCES
|
|||
utils/runopts.c
|
||||
include/runopts.h
|
||||
|
||||
utils/dawn_uci.c include/dawn_uci.h crypto/crypto.c include/crypto.h)
|
||||
utils/dawn_uci.c
|
||||
include/dawn_uci.h
|
||||
|
||||
crypto/crypto.c
|
||||
include/crypto.h
|
||||
utils/utils.c)
|
||||
|
||||
SET(LIBS
|
||||
ubox ubus json-c blobmsg_json config uci gcrypt)
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#include "crypto.h"
|
||||
|
||||
#include <gcrypt.h>
|
||||
#include <stdio.h>
|
||||
#include <gcrypt.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define GCRY_CIPHER GCRY_CIPHER_AES128 // Pick the cipher here
|
||||
#define GCRY_C_MODE GCRY_CIPHER_MODE_ECB // Pick the cipher mode here
|
||||
|
@ -104,4 +105,101 @@ char *gcrypt_decrypt_msg(char *msg, size_t msg_length) {
|
|||
return out;
|
||||
}
|
||||
|
||||
/* Base Encoding
|
||||
* Source: https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c
|
||||
*/
|
||||
|
||||
|
||||
|
||||
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
||||
'4', '5', '6', '7', '8', '9', '+', '/'};
|
||||
static char *decoding_table = NULL;
|
||||
static int mod_table[] = {0, 2, 1};
|
||||
|
||||
|
||||
char *base64_encode(const unsigned char *data,
|
||||
size_t input_length,
|
||||
size_t *output_length) {
|
||||
|
||||
*output_length = 4 * ((input_length + 2) / 3);
|
||||
|
||||
char *encoded_data = malloc(*output_length);
|
||||
if (encoded_data == NULL) return NULL;
|
||||
|
||||
for (int i = 0, j = 0; i < input_length;) {
|
||||
|
||||
uint32_t octet_a = i < input_length ? (unsigned char) data[i++] : 0;
|
||||
uint32_t octet_b = i < input_length ? (unsigned char) data[i++] : 0;
|
||||
uint32_t octet_c = i < input_length ? (unsigned char) data[i++] : 0;
|
||||
|
||||
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
|
||||
|
||||
encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
|
||||
encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
|
||||
encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
|
||||
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
|
||||
}
|
||||
|
||||
for (int i = 0; i < mod_table[input_length % 3]; i++)
|
||||
encoded_data[*output_length - 1 - i] = '=';
|
||||
|
||||
return encoded_data;
|
||||
}
|
||||
|
||||
|
||||
unsigned char *base64_decode(const char *data,
|
||||
size_t input_length,
|
||||
size_t *output_length) {
|
||||
|
||||
if (decoding_table == NULL) build_decoding_table();
|
||||
|
||||
if (input_length % 4 != 0) return NULL;
|
||||
|
||||
*output_length = input_length / 4 * 3;
|
||||
if (data[input_length - 1] == '=') (*output_length)--;
|
||||
if (data[input_length - 2] == '=') (*output_length)--;
|
||||
|
||||
unsigned char *decoded_data = malloc(*output_length);
|
||||
if (decoded_data == NULL) return NULL;
|
||||
|
||||
for (int i = 0, j = 0; i < input_length;) {
|
||||
|
||||
uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char) data[i++]];
|
||||
uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char) data[i++]];
|
||||
uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char) data[i++]];
|
||||
uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[(unsigned char) data[i++]];
|
||||
|
||||
uint32_t triple = (sextet_a << 3 * 6)
|
||||
+ (sextet_b << 2 * 6)
|
||||
+ (sextet_c << 1 * 6)
|
||||
+ (sextet_d << 0 * 6);
|
||||
|
||||
if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
|
||||
if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
|
||||
if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
|
||||
}
|
||||
|
||||
return decoded_data;
|
||||
}
|
||||
|
||||
|
||||
void build_decoding_table() {
|
||||
|
||||
decoding_table = malloc(256);
|
||||
|
||||
for (int i = 0; i < 64; i++)
|
||||
decoding_table[(unsigned char) encoding_table[i]] = i;
|
||||
}
|
||||
|
||||
|
||||
void base64_cleanup() {
|
||||
free(decoding_table);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -11,4 +11,18 @@ char *gcrypt_encrypt_msg(char *msg, size_t msg_length);
|
|||
|
||||
char *gcrypt_decrypt_msg(char *msg, size_t msg_length);
|
||||
|
||||
|
||||
char *base64_encode(const unsigned char *data,
|
||||
size_t input_length,
|
||||
size_t *output_length);
|
||||
|
||||
unsigned char *base64_decode(const char *data,
|
||||
size_t input_length,
|
||||
size_t *output_length);
|
||||
|
||||
void build_decoding_table();
|
||||
|
||||
void base64_cleanup();
|
||||
|
||||
|
||||
#endif //DAWN_CRYPTO_H
|
||||
|
|
|
@ -1,32 +1,13 @@
|
|||
#ifndef __DAWN_UTILS_H
|
||||
#define __DAWN_UTILS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
|
||||
#define STR2MAC(a) &(a)[0], &(a)[1], &(a)[2], &(a)[3], &(a)[4], &(a)[5]
|
||||
|
||||
|
||||
static int hex_to_bin(char ch) {
|
||||
if ((ch >= '0') && (ch <= '9')) return ch - '0';
|
||||
ch = tolower(ch);
|
||||
if ((ch >= 'a') && (ch <= 'f')) return ch - 'a' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int hwaddr_aton(const char *txt, uint8_t *addr) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ETH_ALEN; i++) {
|
||||
int a, b;
|
||||
|
||||
a = hex_to_bin(*txt++);
|
||||
if (a < 0) return -1;
|
||||
b = hex_to_bin(*txt++);
|
||||
if (b < 0) return -1;
|
||||
*addr++ = (a << 4) | b;
|
||||
if (i < 5 && *txt++ != ':') return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
int hex_to_bin(char ch);
|
||||
int hwaddr_aton(const char *txt, uint8_t *addr);
|
||||
|
||||
#endif
|
|
@ -99,6 +99,9 @@ int main(int argc, char **argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
build_decoding_table();
|
||||
|
||||
|
||||
init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1);
|
||||
|
||||
pthread_t tid_probe;
|
||||
|
@ -122,7 +125,8 @@ int main(int argc, char **argv) {
|
|||
pthread_mutex_destroy(&probe_array_mutex);
|
||||
pthread_mutex_destroy(&client_array_mutex);
|
||||
|
||||
free_list(probe_list_head);
|
||||
//free_list(probe_list_head);
|
||||
base64_cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -15,6 +15,7 @@
|
|||
#include "multicastsocket.h"
|
||||
#include "ubus.h"
|
||||
#include "crypto.h"
|
||||
#include "utils.h"
|
||||
|
||||
/* Network Defines */
|
||||
#define MAX_RECV_STRING 5000
|
||||
|
@ -139,7 +140,11 @@ void *receive_msg_enc(void *args) {
|
|||
}
|
||||
//recv_string[recv_string_len] = '\0';
|
||||
|
||||
char *dec = gcrypt_decrypt_msg(recv_string, recv_string_len);
|
||||
// first decode base 64
|
||||
|
||||
size_t base64_msg_len;
|
||||
unsigned char* base64_msg_dec = base64_decode(recv_string, recv_string_len, &base64_msg_len);
|
||||
char *dec = gcrypt_decrypt_msg((char*)base64_msg_dec, base64_msg_len);
|
||||
|
||||
printf("[WC] Network-Received: %s\n", dec);
|
||||
|
||||
|
@ -175,6 +180,7 @@ void *receive_msg_enc(void *args) {
|
|||
int send_string(char *msg) {
|
||||
pthread_mutex_lock(&send_mutex);
|
||||
size_t msglen = strlen(msg);
|
||||
|
||||
//printf("Sending string! %s\n", msg);
|
||||
if (sendto(sock,
|
||||
msg,
|
||||
|
@ -200,11 +206,15 @@ int send_string(char *msg) {
|
|||
int send_string_enc(char *msg) {
|
||||
pthread_mutex_lock(&send_mutex);
|
||||
size_t msglen = strlen(msg);
|
||||
|
||||
char *enc = gcrypt_encrypt_msg(msg, msglen + 1);
|
||||
|
||||
size_t base64_msg_len;
|
||||
char* base64_msg_dec = base64_encode((unsigned char*)enc, msglen, &base64_msg_len);
|
||||
|
||||
if (sendto(sock,
|
||||
enc,
|
||||
msglen + 1, // very important to use actual length of string because of '\0' in encrypted msg
|
||||
base64_msg_dec,
|
||||
base64_msg_len, // very important to use actual length of string because of '\0' in encrypted msg
|
||||
0,
|
||||
(struct sockaddr *) &addr,
|
||||
sizeof(addr)) < 0) {
|
||||
|
|
|
@ -31,13 +31,13 @@ enum {
|
|||
};
|
||||
|
||||
static const struct blobmsg_policy prob_policy[__PROB_MAX] = {
|
||||
[PROB_BSSID_ADDR] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
|
||||
[PROB_CLIENT_ADDR] = {.name = "address", .type = BLOBMSG_TYPE_STRING},
|
||||
[PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING},
|
||||
[PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32},
|
||||
[PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
|
||||
//[PROB_HT_SUPPORT] = {.name = "ht_support", .type = BLOBMSG_TYPE_INT8},
|
||||
//[PROB_VHT_SUPPORT] = {.name = "vht_support", .type = BLOBMSG_TYPE_INT8},
|
||||
[PROB_BSSID_ADDR] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
|
||||
[PROB_CLIENT_ADDR] = {.name = "address", .type = BLOBMSG_TYPE_STRING},
|
||||
[PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING},
|
||||
[PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32},
|
||||
[PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
|
||||
//[PROB_HT_SUPPORT] = {.name = "ht_support", .type = BLOBMSG_TYPE_INT8},
|
||||
//[PROB_VHT_SUPPORT] = {.name = "vht_support", .type = BLOBMSG_TYPE_INT8},
|
||||
};
|
||||
|
||||
enum {
|
||||
|
@ -50,11 +50,11 @@ enum {
|
|||
};
|
||||
|
||||
static const struct blobmsg_policy client_table_policy[__CLIENT_TABLE_MAX] = {
|
||||
[CLIENT_TABLE] = {.name = "clients", .type = BLOBMSG_TYPE_TABLE},
|
||||
[CLIENT_TABLE_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
|
||||
[CLIENT_TABLE_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
|
||||
[CLIENT_TABLE_HT] = {.name = "ht_supported", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_TABLE_VHT] = {.name = "vht_supported", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_TABLE] = {.name = "clients", .type = BLOBMSG_TYPE_TABLE},
|
||||
[CLIENT_TABLE_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
|
||||
[CLIENT_TABLE_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
|
||||
[CLIENT_TABLE_HT] = {.name = "ht_supported", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_TABLE_VHT] = {.name = "vht_supported", .type = BLOBMSG_TYPE_INT8},
|
||||
};
|
||||
|
||||
enum {
|
||||
|
@ -73,17 +73,17 @@ enum {
|
|||
};
|
||||
|
||||
static const struct blobmsg_policy client_policy[__CLIENT_MAX] = {
|
||||
[CLIENT_AUTH] = {.name = "auth", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_ASSOC] = {.name = "assoc", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_AUTHORIZED] = {.name = "authorized", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_PREAUTH] = {.name = "preauth", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_WDS] = {.name = "wds", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_WMM] = {.name = "wmm", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_HT] = {.name = "ht", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_VHT] = {.name = "vht", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_WPS] = {.name = "wps", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_MFP] = {.name = "mfp", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_AID] = {.name = "aid", .type = BLOBMSG_TYPE_INT32},
|
||||
[CLIENT_AUTH] = {.name = "auth", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_ASSOC] = {.name = "assoc", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_AUTHORIZED] = {.name = "authorized", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_PREAUTH] = {.name = "preauth", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_WDS] = {.name = "wds", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_WMM] = {.name = "wmm", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_HT] = {.name = "ht", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_VHT] = {.name = "vht", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_WPS] = {.name = "wps", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_MFP] = {.name = "mfp", .type = BLOBMSG_TYPE_INT8},
|
||||
[CLIENT_AID] = {.name = "aid", .type = BLOBMSG_TYPE_INT32},
|
||||
};
|
||||
|
||||
/* Function Definitions */
|
||||
|
@ -228,7 +228,7 @@ static int add_subscriber(char *name) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int subscribe_to_hostapd_interfaces(char *hostapd_dir) {
|
||||
static int subscribe_to_hostapd_interfaces(char *hostapd_dir) {
|
||||
DIR *dirp;
|
||||
struct dirent *entry;
|
||||
|
||||
|
@ -239,8 +239,7 @@ static int subscribe_to_hostapd_interfaces(char *hostapd_dir) {
|
|||
}
|
||||
|
||||
dirp = opendir(hostapd_dir); // error handling?
|
||||
if(!dirp)
|
||||
{
|
||||
if (!dirp) {
|
||||
fprintf(stderr, "No hostapd sockets!\n");
|
||||
return -1;
|
||||
}
|
||||
|
@ -387,7 +386,7 @@ int parse_to_clients(struct blob_attr *msg, int do_kick, uint32_t id) {
|
|||
uint8_t bssid[ETH_ALEN];
|
||||
hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), bssid);
|
||||
|
||||
if(do_kick){
|
||||
if (do_kick) {
|
||||
printf("[CLIENTS] : Kick Clients\n");
|
||||
kick_clients(bssid, id);
|
||||
printf("[CLIENTS] : KickED Clients\n");
|
||||
|
@ -416,7 +415,7 @@ static int ubus_get_clients() {
|
|||
struct dirent *entry;
|
||||
|
||||
dirp = opendir(hostapd_dir_glob); // error handling?
|
||||
if(!dirp) {
|
||||
if (!dirp) {
|
||||
fprintf(stderr, "No hostapd sockets!\n");
|
||||
return -1;
|
||||
}
|
||||
|
@ -490,8 +489,7 @@ void del_client_all_interfaces(const uint8_t *client_addr, uint32_t reason, uint
|
|||
DIR *dirp;
|
||||
struct dirent *entry;
|
||||
dirp = opendir(hostapd_dir_glob); // error handling?
|
||||
if(!dirp)
|
||||
{
|
||||
if (!dirp) {
|
||||
fprintf(stderr, "No hostapd sockets!\n");
|
||||
return;
|
||||
}
|
||||
|
|
26
src/utils/utils.c
Normal file
26
src/utils/utils.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "utils.h"
|
||||
#include "ubus.h"
|
||||
|
||||
int hex_to_bin(char ch) {
|
||||
if ((ch >= '0') && (ch <= '9')) return ch - '0';
|
||||
ch = tolower(ch);
|
||||
if ((ch >= 'a') && (ch <= 'f')) return ch - 'a' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int hwaddr_aton(const char *txt, uint8_t *addr) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ETH_ALEN; i++) {
|
||||
int a, b;
|
||||
|
||||
a = hex_to_bin(*txt++);
|
||||
if (a < 0) return -1;
|
||||
b = hex_to_bin(*txt++);
|
||||
if (b < 0) return -1;
|
||||
*addr++ = (a << 4) | b;
|
||||
if (i < 5 && *txt++ != ':') return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue