From 4638e0dfc3be720962990e5529587ee22d62f219 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Fri, 4 Dec 2020 06:11:14 +0100 Subject: Server and client implementation for encrypted udp logger --- Makefile | 10 ++ receiver.c | 298 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sender.c | 110 +++++++++++++++++++++ sessions/.empty | 0 4 files changed, 418 insertions(+) create mode 100644 Makefile create mode 100644 receiver.c create mode 100644 sender.c create mode 100644 sessions/.empty diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..631717d --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: sender receiver + +sender: sender.c + $(CC) -o sender sender.c -lmbedtls -lmbedcrypto + +receiver: receiver.c + $(CC) -o receiver receiver.c -lmbedtls -lmbedcrypto + +clean: + rm -f sender receiver diff --git a/receiver.c b/receiver.c new file mode 100644 index 0000000..b7171d9 --- /dev/null +++ b/receiver.c @@ -0,0 +1,298 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "mbedtls/ctr_drbg.h" +#include "mbedtls/gcm.h" + +const unsigned short PORT = 58132; + +static const unsigned char privkey[] = +"-----BEGIN RSA PRIVATE KEY-----\n" +"MIIEpAIBAAKCAQEAwWlNmLHOOzZpdrfp+EAANwqab0FhQwCyZ/u+ySBW5XxPf6mo\n" +"bySvtJrLdsWzdwnup/UfwZiEhJk/4wpD4Qf/2+syuJi3Rf7L+Jfh//Qf9uXAS80+\n" +"LYad7dW0c1r5nt+F9Can5fBn7futnd8n672T+y8QpHRwX9GtaILvYQe5GQac8cfq\n" +"2rGUd5iYj5KSdcaIZnJ4YgnjLHg2PMbtEJwqcV+2oAkcOPzTAJoNE7XjLZTwXmLl\n" +"FgL/2cN4uJZBDnwv3RZSAhpdYF4KOJmE2GFs2jdvRUrYO7WSl8fM16vRH4vz5MNN\n" +"caprg2MlXheVTPQa+WMdcz7dyQx8s9kRVPPUSwIDAQABAoIBAH1KD8A4flYRO2Ry\n" +"Yxgzr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lP0L4ndaNKbnPaixcoRo\n" +"kNpPhAECgYAg7jmNlw+7VVurzR36LKKE+d6veraF5ltpJiboDb3j38RGe3982LLq\n" +"WaBKm1gkHfXgBjkNzS4r2kyRijw0GQ9JgmWooR7BXFH30HkPNl4gFTSsrOG5zGLi\n" +"0aexkDpXQuKsgBzqU0Wn94GZMM+RhuOYec/56JFT+8U5Tcntb26wwA==\n" +"-----END RSA PRIVATE KEY-----\n"; + +static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH"; + +/* + 1 byte type: 0 allocate session, 1 log to session, rest: discard + 8 bytes session id + type 0: + 256 bytes rsa 2048 data yielding 32 byte AES session key + type 1: + 16 bytes iv + 16 bytes tag + rest cipher text + + File name format: 2020-10-13-23-42-05-SSSSSSSSSSSSSSSS-KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK.log + +*/ + +enum { SESSION_ID_LENGTH = 8, AES_KEY_LENGTH = 16, GCM_IV_LENGTH = 16, GCM_TAG_LENGTH = 16 }; +typedef struct { + uint8_t session_id[SESSION_ID_LENGTH]; + uint8_t key[AES_KEY_LENGTH]; + int fd; // or -1 if not existing + time_t last_access; + char file_name[80]; +} SESSION; +static SESSION *g_sessions = 0; +static size_t g_session_count = 0; + +int session_compare(const void *a, const void *b) { return memcmp(a, b, 8); } + +enum { SIDOFFS = 20, KEYOFFS = 37, }; + +static uint8_t hex2nyble(char c) +{ + return (c>='0'&&c<='9') ? (uint8_t)(c-'0') + : (c>='a'&&c<='f') ? (uint8_t)(c-'a'+10) + : (c>='A'&&c<='F') ? (uint8_t)(c-'A'+10) + : 0; +} + +static void import_sessions(const char *dirname) { + DIR * dirp = opendir(dirname); + if (!dirp) + errx(-1, "Fatal: Can't open dir %s\n", dirname); + + free(g_sessions); + g_session_count = 0; + + size_t allocated = 1024; + g_sessions = malloc(allocated * sizeof(SESSION)); + if (!g_sessions) + errx(-1, "Fatal: Out of memory"); + + regex_t regex; + if (regcomp(®ex, "^[[:digit:]]{4}-[[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]-" + "[[:digit:]][[:digit:]]-[[:xdigit:]]{16}-[[:xdigit:]]{32}.log$", REG_EXTENDED)) + errx(-1, "Fatal: Can't compile re"); + + struct dirent * entry; + while ((entry = readdir(dirp)) != NULL) { + // We expect a very specific format + if (entry->d_type != DT_REG || entry->d_namlen != 73) { + fprintf(stderr, "Skipping wrong length file: %*s\n", entry->d_namlen, entry->d_name); + continue; + } + + SESSION *ns = g_sessions + g_session_count; + + memcpy(ns->file_name, entry->d_name, entry->d_namlen); + ns->file_name[entry->d_namlen] = 0; + ns->fd = -1; + ns->last_access = 0; + + if (regexec(®ex, ns->file_name, (size_t) 0, NULL, 0)) { + fprintf(stderr, "Skipping non-re-matching file: %s\n", ns->file_name); + continue; + } + + if (sscanf(ns->file_name + SIDOFFS, "%" SCNx64, (uint64_t*)&ns->session_id) != 1) { + fprintf(stderr, "Skipping non-parsable file: %s\n", ns->file_name); + continue; + } + const char * hexkey = ns->file_name + KEYOFFS; + for (int i=0; i<16; ++i) + ns->key[i] = (hex2nyble(hexkey[2*i]) << 4 ) | hex2nyble(hexkey[2*i+1]); + + // Need more memory? + if (++g_session_count > allocated) { + allocated += 1024; + g_sessions = realloc(g_sessions, allocated * sizeof(SESSION)); + if (!g_sessions) + errx(-1, "Fatal: Out of memory"); + } + } + closedir(dirp); + regfree(®ex); + + qsort(g_sessions, g_session_count, sizeof(SESSION), session_compare); +} + +static void add_session(uint8_t *session_id, uint8_t aes_key[AES_KEY_LENGTH]) { + // We don't overwrite existing sessions + if (bsearch(session_id, g_sessions, g_session_count, sizeof(SESSION), session_compare)) + return; + + SESSION * sessions_realloc = realloc(g_sessions, sizeof(SESSION) * (1 + g_session_count)); + + if (!sessions_realloc) + errx(-1, "Fatal: Out of memory"); + + g_sessions = sessions_realloc; + SESSION * new_session = &g_sessions[g_session_count]; + g_session_count++; + + memcpy(new_session->session_id, session_id, SESSION_ID_LENGTH); + memcpy(new_session->key, aes_key, AES_KEY_LENGTH); + new_session->fd = -1; + new_session->last_access = 0; + + time_t t = time(NULL); + struct tm * jetzt = localtime(&t); + char tprefix[32], hexkey[2*AES_KEY_LENGTH + 1]; + size_t nlen = strftime(tprefix, sizeof(tprefix), "%F-%H-%M-%S", jetzt); + for (int i=0; ifile_name, sizeof(new_session->file_name), + "%s-%" PRIx64 "-%s.log", tprefix, *(uint64_t*)session_id, hexkey); + int fd = open("new_session->file_name", O_RDWR|O_CREAT, 0755); + close(fd); + + mergesort(g_sessions, g_session_count, sizeof(SESSION), session_compare); +} + +static time_t now() { + struct timespec tp; + clock_gettime(CLOCK_MONOTONIC, &tp); + return tp.tv_sec; +} + +static void log_to_session(const uint8_t *packet, size_t len) { + // First check if the packet holds enough space for session id, iv and at least one gcm block + if (len < SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH) { + fprintf(stderr, "Error: Short packet, size %zd\n", len); + return; + } + + const uint8_t *session_id = packet; + const uint8_t *iv = packet + SESSION_ID_LENGTH; + const uint8_t *tag = packet + SESSION_ID_LENGTH + GCM_IV_LENGTH; + packet += SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH; + len -= SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH; + + SESSION * s = bsearch(session_id, g_sessions, g_session_count, sizeof(SESSION), session_compare); + if (!s) { + fprintf(stderr, "Error: Can't log to unknown session 0x%" PRIx64 "\n", *(uint64_t*)session_id); + return; + } + + // Create output file if it doesn't exist + if (s->fd < 0) + s->fd = open(s->file_name, O_WRONLY | O_APPEND | O_CREAT, 0755); + if (s->fd < 0) { + fprintf(stderr, "Error: Can't create file %s for session 0x%" PRIx64 "\n", s->file_name, *(uint64_t*)packet); + return; + } + + // Now decrypt + mbedtls_gcm_context ctx; + mbedtls_gcm_init(&ctx); + mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, s->key, 8 * AES_KEY_LENGTH); + + // Prepare output scratch space + uint8_t *output = alloca(len); + + // This should fail on invalid input sizes + switch(mbedtls_gcm_auth_decrypt(&ctx, len, iv, GCM_IV_LENGTH, session_id, SESSION_ID_LENGTH, tag, GCM_TAG_LENGTH, packet, output)) + { + case 0: + write(s->fd, output, len); + s->last_access = now(); + break; + case MBEDTLS_ERR_GCM_BAD_INPUT: + fprintf(stderr, "Error: Invalid log data\n"); + break; + case MBEDTLS_ERR_GCM_AUTH_FAILED : + fprintf(stderr, "Error: Can't decrypt\n"); + break; + default: + fprintf(stderr, "Error: Unknown gcm error\n"); + } + + mbedtls_gcm_free(&ctx); +} + +int main() { + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_entropy_context entropy; + mbedtls_pk_context pk; + int ret = 0; + unsigned char result[256]; + unsigned char input[256]; + size_t inputlen = 0; + + chdir("sessions"); + + mbedtls_pk_init( &pk ); + mbedtls_entropy_init( &entropy ); + mbedtls_ctr_drbg_init( &ctr_drbg ); + mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp)); + + if ((ret = mbedtls_pk_parse_key(&pk, privkey, sizeof(privkey), NULL, 0) ) != 0 ) + errx(-1, "Fatal: mbedtls_pk_parse_key returned -0x%04x\n", -ret ); + + int sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) + errx(-1, "Fatal: Can not make socket\n"); + + struct sockaddr_in servaddr, peer; + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = INADDR_ANY; + servaddr.sin_port = htons(PORT); + if (bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) + errx(-1, "Fatal: Can't bind to port %d\n", PORT); + + import_sessions("."); + + while(1) { + uint8_t packet[16*1024]; + uint8_t rsa_plain_text[AES_KEY_LENGTH]; + size_t olen; + socklen_t peer_len = sizeof(peer); + + ssize_t len = recvfrom(sock, packet, sizeof(packet), MSG_WAITALL, (struct sockaddr *) &peer, &peer_len); + if (len <= 0) + errx(-1, "Fatal: recvfrom yields %zd\n", len); + switch(packet[0]) { + case 0: // Session setup + ret = mbedtls_pk_decrypt(&pk, packet + 1 + SESSION_ID_LENGTH, len - 1 - SESSION_ID_LENGTH, + rsa_plain_text, &olen, sizeof(rsa_plain_text), mbedtls_ctr_drbg_random, &ctr_drbg); + if (ret < 0) + fprintf(stderr, "Error: Failed to decrypt key (error %d) for session 0x%" PRIx64 "\n", -ret, *(uint64_t *)(packet + 1)); + else + add_session(packet + 1, rsa_plain_text); + break; + case 1: + log_to_session(packet + 1, len - 1); + break; + default: + break; + } + } +} diff --git a/sender.c b/sender.c new file mode 100644 index 0000000..3469735 --- /dev/null +++ b/sender.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include "mbedtls/ctr_drbg.h" +#include "mbedtls/gcm.h" + +const unsigned short PORT = 58132; + +static const unsigned char pubkey[] = +"-----BEGIN PUBLIC KEY-----\n" +"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwWlNmLHOOzZpdrfp+EAA\n" +"Nwqab0FhQwCyZ/u+ySBW5XxPf6mobySvtJrLdsWzdwnup/UfwZiEhJk/4wpD4Qf/\n" +"2+syuJi3Rf7L+Jfh//Qf9uXAS80+LYad7dW0c1r5nt+F9Can5fBn7futnd8n672T\n" +"+y8QpHRwX9GtaILvYQe5GQac8cfq2rGUd5iYj5KSdcaIZnJ4YgnjLHg2PMbtEJwq\n" +"cV+2oAkcOPzTAJoNE7XjLZTwXmLlFgL/2cN4uJZBDnwv3RZSAhpdYF4KOJmE2GFs\n" +"2jdvRUrYO7WSl8fM16vRH4vz5MNNcaprg2MlXheVTPQa+WMdcz7dyQx8s9kRVPPU\n" +"SwIDAQAB \n" +"-----END PUBLIC KEY----- \n"; + +static const unsigned char pp[] = "IJUHZGFDXTZKHJKHGFDHZLUÖDRTFGHHJGHH"; + +enum { SESSION_ID_LENGTH = 8, AES_KEY_LENGTH = 16, GCM_IV_LENGTH = 16, GCM_TAG_LENGTH = 16 }; +static uint8_t session_id[8] = { 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01 }; +static uint8_t aes_key[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; + +void send_udp(int sock, mbedtls_ctr_drbg_context *ctr_drbg, const uint8_t *text, size_t len) { + uint8_t iv[GCM_IV_LENGTH]; + mbedtls_ctr_drbg_random(ctr_drbg, iv, sizeof(GCM_IV_LENGTH)); + + const size_t total_length = 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH + len; + uint8_t *output = alloca(total_length); + output[0] = 1; + memcpy(output + 1, session_id, SESSION_ID_LENGTH); + memcpy(output + 1 + SESSION_ID_LENGTH, iv, GCM_IV_LENGTH); + + mbedtls_gcm_context ctx; + mbedtls_gcm_init(&ctx); + mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH); + + if (!mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, len, iv, GCM_IV_LENGTH, session_id, SESSION_ID_LENGTH, + text, output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH + GCM_TAG_LENGTH, GCM_TAG_LENGTH, + output + 1 + SESSION_ID_LENGTH + GCM_IV_LENGTH)) { + struct sockaddr_in to; + memset(&to, 0, sizeof(to)); + to.sin_family = AF_INET; + to.sin_addr.s_addr = inet_addr("127.0.0.1"); + to.sin_port = htons(PORT); + + sendto(sock, output, total_length, 0, (struct sockaddr*)&to, sizeof(to)); + } + + mbedtls_gcm_free(&ctx); +} + +void new_session(int sock, mbedtls_ctr_drbg_context *ctr_drbg) { + mbedtls_ctr_drbg_random(ctr_drbg, session_id, sizeof(session_id)); + mbedtls_ctr_drbg_random(ctr_drbg, aes_key, sizeof(aes_key)); + + unsigned char output[512]; + output[0] = 0; + memcpy(output + 1, session_id, SESSION_ID_LENGTH); + + mbedtls_pk_context pk; + mbedtls_pk_init(&pk); + int ret = 0; + if ((ret = mbedtls_pk_parse_public_key(&pk, pubkey, sizeof(pubkey)) ) != 0 ) + errx(-1, "mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); + + size_t olen = 0; + if ((ret = mbedtls_pk_encrypt(&pk, aes_key, AES_KEY_LENGTH, output + 1 + SESSION_ID_LENGTH, &olen, + sizeof(output) - 1 - SESSION_ID_LENGTH, mbedtls_ctr_drbg_random, ctr_drbg)) != 0) + errx(-1, "mbedtls_pk_encrypt returned -0x%04x\n", -ret ); + + mbedtls_pk_free(&pk); + + struct sockaddr_in to; + memset(&to, 0, sizeof(to)); + to.sin_family = AF_INET; + to.sin_addr.s_addr = inet_addr("127.0.0.1"); + to.sin_port = htons(PORT); + + sendto(sock, output, olen + 1 + SESSION_ID_LENGTH, 0, (struct sockaddr*)&to, sizeof(to)); +} + +int main() { + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_entropy_context entropy; + + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp)); + + int sock = socket(AF_INET, SOCK_DGRAM, 0); + + new_session(sock, &ctr_drbg); + + sleep(3); + + const unsigned char *logline = (const unsigned char*)"Juchuuu, es klappt!\n"; + send_udp(sock, &ctr_drbg, logline, strlen((char*)logline)); + + close(sock); +} diff --git a/sessions/.empty b/sessions/.empty new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3