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 --- sender.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'sender.c') 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