summaryrefslogtreecommitdiff
path: root/receiver.c
blob: 36b649c61bc10d03c8e8198004552e4b323de811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <regex.h>
#include <string.h>
#include <dirent.h>
#include <err.h>
#include <stdio.h>
#include <inttypes.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <mbedtls/pk.h>
#include <mbedtls/entropy.h>
#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"
"LYad7dW0c1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dPLmCtuowfMO4LbGf\n"
"VcexpgLEJyAszvBteikeDwpcyCD19wxP9J4kIYCJiggQKpfLoWUfP/P6DydrPnSt\n"
"EUbAlaNqDpl9Mj7YonQVhCMCgYEAv0c9M5+hrDuX7d76zYaZvI4UymUO54E/yZ7e\n"
"UvdOXGPYed+SL/oKeD5aQAeyLzl79bHdgBs3g0QW9kvXzly0cC5eC0oZH5hhs7nI\n"
"TKII1i86bLtM3dD5vQYWnF0sNtWK/+8Bo6L5ZAiNxRE7lP0L4ndaNKbnPaixcoRo\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(&regex, "^[[: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(&regex, 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(&regex);

    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; i<AES_KEY_LENGTH; ++i)
        sprintf(hexkey + 2 * i, "%02x", aes_key[i]);
    int off = snprintf(new_session->file_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;
        }
    }
}