diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | receiver.cpp | 42 | ||||
-rw-r--r-- | sender.c | 39 |
3 files changed, 70 insertions, 14 deletions
@@ -9,5 +9,8 @@ sender: sender.c | |||
9 | receiver: receiver.cpp | 9 | receiver: receiver.cpp |
10 | $(CXX) -std=c++17 -o receiver receiver.cpp -lmbedtls -lmbedcrypto -I$(INCLUDES) -L$(LIBS) | 10 | $(CXX) -std=c++17 -o receiver receiver.cpp -lmbedtls -lmbedcrypto -I$(INCLUDES) -L$(LIBS) |
11 | 11 | ||
12 | test: receiver sender | ||
13 | ./test-all.sh | ||
14 | |||
12 | clean: | 15 | clean: |
13 | rm -f sender receiver | 16 | rm -f sender receiver |
diff --git a/receiver.cpp b/receiver.cpp index a86cf57..78e6aa7 100644 --- a/receiver.cpp +++ b/receiver.cpp | |||
@@ -262,26 +262,50 @@ static void import_sessions(const char *root_dir) { | |||
262 | closedir(dirp); | 262 | closedir(dirp); |
263 | } | 263 | } |
264 | 264 | ||
265 | int main(int args, char **argv) { | 265 | int main(int argc, char **argv) { |
266 | mbedtls_ctr_drbg_context ctr_drbg; | 266 | mbedtls_ctr_drbg_context ctr_drbg; |
267 | mbedtls_entropy_context entropy; | 267 | mbedtls_entropy_context entropy; |
268 | mbedtls_pk_context pk; | 268 | mbedtls_pk_context pk; |
269 | int ret = 0; | 269 | int ret = 0, clport = PORT; |
270 | unsigned char result[256]; | 270 | unsigned char result[256]; |
271 | unsigned char input[256]; | 271 | unsigned char input[256]; |
272 | size_t inputlen = 0; | 272 | size_t inputlen = 0; |
273 | char * privkey_file = 0; | ||
274 | char * me = argv[0]; | ||
275 | |||
276 | char ch; | ||
277 | while ((ch = getopt(argc, argv, "p:k:")) != -1) { | ||
278 | switch (ch) { | ||
279 | case 'k': | ||
280 | privkey_file = optarg; | ||
281 | break; | ||
282 | case 'p': | ||
283 | clport = std::stoi(optarg); | ||
284 | break; | ||
285 | default: | ||
286 | errx(-1, "Syntax: %s [sessionlogdir] xx", me); | ||
287 | break; | ||
288 | } | ||
289 | } | ||
290 | argc -= optind; | ||
291 | argv += optind; | ||
273 | 292 | ||
274 | if (args != 2) | 293 | if (argc != 1) |
275 | errx(-1, "Syntax: %s [sessionlogdir]", argv[0]); | 294 | errx(-1, "Syntax: %s [sessionlogdir]", me); |
276 | chdir(argv[1]); | 295 | chdir(argv[0]); |
277 | 296 | ||
278 | mbedtls_pk_init( &pk ); | 297 | mbedtls_pk_init( &pk ); |
279 | mbedtls_entropy_init( &entropy ); | 298 | mbedtls_entropy_init( &entropy ); |
280 | mbedtls_ctr_drbg_init( &ctr_drbg ); | 299 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
281 | mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp)); | 300 | mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pp, sizeof(pp)); |
282 | 301 | ||
283 | if ((ret = mbedtls_pk_parse_key(&pk, privkey, sizeof(privkey), NULL, 0) ) != 0 ) | 302 | if (privkey_file) { |
284 | errx(-1, "Fatal: mbedtls_pk_parse_key returned -0x%04x\n", -ret ); | 303 | if ((ret = mbedtls_pk_parse_keyfile(&pk, privkey_file, NULL) ) != 0 ) |
304 | errx(-1, "Fatal: mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret ); | ||
305 | } else { | ||
306 | if ((ret = mbedtls_pk_parse_key(&pk, privkey, sizeof(privkey), NULL, 0) ) != 0 ) | ||
307 | errx(-1, "Fatal: mbedtls_pk_parse_key returned -0x%04x\n", -ret ); | ||
308 | } | ||
285 | 309 | ||
286 | int sock = socket(AF_INET, SOCK_DGRAM, 0); | 310 | int sock = socket(AF_INET, SOCK_DGRAM, 0); |
287 | if (sock < 0) | 311 | if (sock < 0) |
@@ -290,9 +314,9 @@ int main(int args, char **argv) { | |||
290 | struct sockaddr_in servaddr, peer; | 314 | struct sockaddr_in servaddr, peer; |
291 | servaddr.sin_family = AF_INET; | 315 | servaddr.sin_family = AF_INET; |
292 | servaddr.sin_addr.s_addr = INADDR_ANY; | 316 | servaddr.sin_addr.s_addr = INADDR_ANY; |
293 | servaddr.sin_port = htons(PORT); | 317 | servaddr.sin_port = htons(clport); |
294 | if (bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) | 318 | if (bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) |
295 | errx(-1, "Fatal: Can't bind to port %d\n", PORT); | 319 | errx(-1, "Fatal: Can't bind to port %d\n", clport); |
296 | 320 | ||
297 | import_sessions("."); | 321 | import_sessions("."); |
298 | 322 | ||
@@ -23,6 +23,8 @@ static const unsigned char pubkey[] = | |||
23 | "SwIDAQAB \n" | 23 | "SwIDAQAB \n" |
24 | "-----END PUBLIC KEY----- \n"; | 24 | "-----END PUBLIC KEY----- \n"; |
25 | 25 | ||
26 | static char *pubkey_file = 0; | ||
27 | |||
26 | static const unsigned char pp[] = "9bf308b7ae027baa46091d980632e27b"; | 28 | static const unsigned char pp[] = "9bf308b7ae027baa46091d980632e27b"; |
27 | static const char *logging_host = "endpoint-de9XDJ0fH7.gsmk.de"; | 29 | static const char *logging_host = "endpoint-de9XDJ0fH7.gsmk.de"; |
28 | static const char *logging_port = "8238"; | 30 | static const char *logging_port = "8238"; |
@@ -66,9 +68,14 @@ void new_session(int sock, mbedtls_ctr_drbg_context *ctr_drbg) { | |||
66 | mbedtls_pk_context pk; | 68 | mbedtls_pk_context pk; |
67 | mbedtls_pk_init(&pk); | 69 | mbedtls_pk_init(&pk); |
68 | int ret = 0; | 70 | int ret = 0; |
69 | printf("%zd\n", sizeof(pubkey)); | 71 | |
70 | if ((ret = mbedtls_pk_parse_public_key(&pk, pubkey, sizeof(pubkey)) ) != 0 ) | 72 | if (pubkey_file) { |
71 | errx(-1, "mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); | 73 | if ((ret = mbedtls_pk_parse_public_keyfile(&pk, pubkey_file) ) != 0 ) |
74 | errx(-1, "mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); | ||
75 | } else { | ||
76 | if ((ret = mbedtls_pk_parse_public_key(&pk, pubkey, sizeof(pubkey)) ) != 0 ) | ||
77 | errx(-1, "mbedtls_pk_parse_public_key returned -0x%04x\n", -ret ); | ||
78 | } | ||
72 | 79 | ||
73 | size_t olen = 0; | 80 | size_t olen = 0; |
74 | if ((ret = mbedtls_pk_encrypt(&pk, aes_key, AES_KEY_LENGTH, output + 1 + SESSION_ID_LENGTH, &olen, | 81 | if ((ret = mbedtls_pk_encrypt(&pk, aes_key, AES_KEY_LENGTH, output + 1 + SESSION_ID_LENGTH, &olen, |
@@ -83,12 +90,34 @@ printf("%zd\n", sizeof(pubkey)); | |||
83 | mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH); | 90 | mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH); |
84 | } | 91 | } |
85 | 92 | ||
86 | int main() { | 93 | int main(int argc, char **argv) { |
94 | const char * host = logging_host, * port = logging_port; | ||
95 | char ch; | ||
96 | while ((ch = getopt(argc, argv, "h:p:c:")) != -1) { | ||
97 | switch (ch) { | ||
98 | case 'h': | ||
99 | host = optarg; | ||
100 | break; | ||
101 | case 'p': | ||
102 | port = optarg; | ||
103 | break; | ||
104 | case 'c': | ||
105 | pubkey_file = optarg; | ||
106 | break; | ||
107 | case '?': | ||
108 | default: | ||
109 | printf("Usage: %s [-h host] [-p port] [-c cert]", argv[0]); | ||
110 | exit(0); | ||
111 | } | ||
112 | } | ||
113 | argc -= optind; | ||
114 | argv += optind; | ||
115 | |||
87 | struct addrinfo hints, *result, *rp; | 116 | struct addrinfo hints, *result, *rp; |
88 | memset (&hints, 0, sizeof (hints)); | 117 | memset (&hints, 0, sizeof (hints)); |
89 | hints.ai_socktype = SOCK_DGRAM; | 118 | hints.ai_socktype = SOCK_DGRAM; |
90 | 119 | ||
91 | int sock = -1, res = getaddrinfo(logging_host, logging_port, &hints, &result); | 120 | int sock = -1, res = getaddrinfo(host, port, &hints, &result); |
92 | if (res != 0) | 121 | if (res != 0) |
93 | errx(EXIT_FAILURE, "getaddrinfo: %s\n", gai_strerror(res)); | 122 | errx(EXIT_FAILURE, "getaddrinfo: %s\n", gai_strerror(res)); |
94 | 123 | ||