diff options
Diffstat (limited to 'sender.c')
-rw-r--r-- | sender.c | 39 |
1 files changed, 34 insertions, 5 deletions
@@ -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 | ||