From 36051df5dc3defbbcecadd7e4b9dfb5f3ffbf6e8 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Mon, 18 Jan 2021 17:56:16 +0100 Subject: Add testing caps: host,port,cert from command line and a shell script to run test --- Makefile | 3 +++ receiver.cpp | 42 +++++++++++++++++++++++++++++++++--------- sender.c | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 6189df4..179ba7c 100644 --- a/Makefile +++ b/Makefile @@ -9,5 +9,8 @@ sender: sender.c receiver: receiver.cpp $(CXX) -std=c++17 -o receiver receiver.cpp -lmbedtls -lmbedcrypto -I$(INCLUDES) -L$(LIBS) +test: receiver sender + ./test-all.sh + clean: 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) { closedir(dirp); } -int main(int args, char **argv) { +int main(int argc, char **argv) { mbedtls_ctr_drbg_context ctr_drbg; mbedtls_entropy_context entropy; mbedtls_pk_context pk; - int ret = 0; + int ret = 0, clport = PORT; unsigned char result[256]; unsigned char input[256]; size_t inputlen = 0; + char * privkey_file = 0; + char * me = argv[0]; + + char ch; + while ((ch = getopt(argc, argv, "p:k:")) != -1) { + switch (ch) { + case 'k': + privkey_file = optarg; + break; + case 'p': + clport = std::stoi(optarg); + break; + default: + errx(-1, "Syntax: %s [sessionlogdir] xx", me); + break; + } + } + argc -= optind; + argv += optind; - if (args != 2) - errx(-1, "Syntax: %s [sessionlogdir]", argv[0]); - chdir(argv[1]); + if (argc != 1) + errx(-1, "Syntax: %s [sessionlogdir]", me); + chdir(argv[0]); 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 ); + if (privkey_file) { + if ((ret = mbedtls_pk_parse_keyfile(&pk, privkey_file, NULL) ) != 0 ) + errx(-1, "Fatal: mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret ); + } else { + 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) @@ -290,9 +314,9 @@ int main(int args, char **argv) { struct sockaddr_in servaddr, peer; servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = INADDR_ANY; - servaddr.sin_port = htons(PORT); + servaddr.sin_port = htons(clport); if (bind(sock, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) - errx(-1, "Fatal: Can't bind to port %d\n", PORT); + errx(-1, "Fatal: Can't bind to port %d\n", clport); import_sessions("."); diff --git a/sender.c b/sender.c index cda56f9..c0caf6a 100644 --- a/sender.c +++ b/sender.c @@ -23,6 +23,8 @@ static const unsigned char pubkey[] = "SwIDAQAB \n" "-----END PUBLIC KEY----- \n"; +static char *pubkey_file = 0; + static const unsigned char pp[] = "9bf308b7ae027baa46091d980632e27b"; static const char *logging_host = "endpoint-de9XDJ0fH7.gsmk.de"; static const char *logging_port = "8238"; @@ -66,9 +68,14 @@ void new_session(int sock, mbedtls_ctr_drbg_context *ctr_drbg) { mbedtls_pk_context pk; mbedtls_pk_init(&pk); int ret = 0; -printf("%zd\n", sizeof(pubkey)); - if ((ret = mbedtls_pk_parse_public_key(&pk, pubkey, sizeof(pubkey)) ) != 0 ) - errx(-1, "mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); + + if (pubkey_file) { + if ((ret = mbedtls_pk_parse_public_keyfile(&pk, pubkey_file) ) != 0 ) + errx(-1, "mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); + } else { + if ((ret = mbedtls_pk_parse_public_key(&pk, pubkey, sizeof(pubkey)) ) != 0 ) + errx(-1, "mbedtls_pk_parse_public_key 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, @@ -83,12 +90,34 @@ printf("%zd\n", sizeof(pubkey)); mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, aes_key, 8 * AES_KEY_LENGTH); } -int main() { +int main(int argc, char **argv) { + const char * host = logging_host, * port = logging_port; + char ch; + while ((ch = getopt(argc, argv, "h:p:c:")) != -1) { + switch (ch) { + case 'h': + host = optarg; + break; + case 'p': + port = optarg; + break; + case 'c': + pubkey_file = optarg; + break; + case '?': + default: + printf("Usage: %s [-h host] [-p port] [-c cert]", argv[0]); + exit(0); + } + } + argc -= optind; + argv += optind; + struct addrinfo hints, *result, *rp; memset (&hints, 0, sizeof (hints)); hints.ai_socktype = SOCK_DGRAM; - int sock = -1, res = getaddrinfo(logging_host, logging_port, &hints, &result); + int sock = -1, res = getaddrinfo(host, port, &hints, &result); if (res != 0) errx(EXIT_FAILURE, "getaddrinfo: %s\n", gai_strerror(res)); -- cgit v1.2.3