diff options
Diffstat (limited to 'src/identity/gnunet-identity.c')
-rw-r--r-- | src/identity/gnunet-identity.c | 103 |
1 files changed, 29 insertions, 74 deletions
diff --git a/src/identity/gnunet-identity.c b/src/identity/gnunet-identity.c index 06e6fb613..c4cae78ca 100644 --- a/src/identity/gnunet-identity.c +++ b/src/identity/gnunet-identity.c | |||
@@ -259,29 +259,23 @@ static void | |||
259 | write_encrypted_message (void) | 259 | write_encrypted_message (void) |
260 | { | 260 | { |
261 | struct GNUNET_IDENTITY_PublicKey recipient; | 261 | struct GNUNET_IDENTITY_PublicKey recipient; |
262 | unsigned char ct[strlen (write_msg) + 1024]; | ||
262 | if (GNUNET_IDENTITY_public_key_from_string (pubkey_msg, &recipient) != | 263 | if (GNUNET_IDENTITY_public_key_from_string (pubkey_msg, &recipient) != |
263 | GNUNET_SYSERR) | 264 | GNUNET_SYSERR) |
264 | { | 265 | { |
265 | struct GNUNET_CRYPTO_EcdhePublicKey message_key; | ||
266 | size_t msg_len = strlen (write_msg) + 1; | 266 | size_t msg_len = strlen (write_msg) + 1; |
267 | ssize_t res = GNUNET_IDENTITY_encrypt (write_msg, | 267 | ssize_t res = GNUNET_IDENTITY_encrypt2 (write_msg, |
268 | msg_len, | 268 | msg_len, |
269 | &recipient, | 269 | &recipient, |
270 | &message_key, | 270 | ct, strlen (write_msg) + 1024); |
271 | write_msg); | ||
272 | if (-1 != res) | 271 | if (-1 != res) |
273 | { | 272 | { |
274 | char *keystr; | ||
275 | char *serialized_msg; | 273 | char *serialized_msg; |
276 | keystr = GNUNET_STRINGS_data_to_string_alloc (&message_key, | 274 | serialized_msg = GNUNET_STRINGS_data_to_string_alloc (ct, |
277 | sizeof(struct | 275 | res); |
278 | GNUNET_CRYPTO_EcdhePublicKey)); | ||
279 | serialized_msg = GNUNET_STRINGS_data_to_string_alloc (write_msg, | ||
280 | msg_len); | ||
281 | fprintf (stdout, | 276 | fprintf (stdout, |
282 | "%s.%s\n", | 277 | "%s\n", |
283 | keystr, serialized_msg); | 278 | serialized_msg); |
284 | GNUNET_free (keystr); | ||
285 | GNUNET_free (serialized_msg); | 279 | GNUNET_free (serialized_msg); |
286 | } | 280 | } |
287 | else | 281 | else |
@@ -307,75 +301,36 @@ write_encrypted_message (void) | |||
307 | static void | 301 | static void |
308 | read_encrypted_message (struct GNUNET_IDENTITY_Ego *ego) | 302 | read_encrypted_message (struct GNUNET_IDENTITY_Ego *ego) |
309 | { | 303 | { |
310 | // message contains ECDHE key and ciphertext divided by ".", so split up first | 304 | char *deserialized_msg; |
311 | char delim[2] = "."; | 305 | size_t msg_len; |
312 | char *key_msg = strtok (read_msg, delim); | 306 | if (GNUNET_OK == GNUNET_STRINGS_string_to_data_alloc (read_msg, strlen ( |
313 | char *cipher; | 307 | read_msg), |
314 | if (NULL == key_msg) | 308 | (void **) & |
309 | deserialized_msg, | ||
310 | &msg_len)) | ||
315 | { | 311 | { |
316 | fprintf (stderr, "Invalid message format.\n"); | 312 | ssize_t res = GNUNET_IDENTITY_decrypt2 (deserialized_msg, |
317 | global_ret = 1; | 313 | msg_len, |
318 | return; | 314 | GNUNET_IDENTITY_ego_get_private_key ( |
319 | } | 315 | ego), |
320 | cipher = strtok (NULL, delim); | 316 | deserialized_msg, msg_len); |
321 | if (NULL == cipher) | 317 | if (-1 != res) |
322 | { | ||
323 | fprintf (stderr, "Invalid message format, text missing.\n"); | ||
324 | global_ret = 1; | ||
325 | return; | ||
326 | } | ||
327 | |||
328 | if (NULL != strtok (NULL, delim)) | ||
329 | { | ||
330 | fprintf (stderr, | ||
331 | "Invalid message format, expecting only key and cipher components.\n"); | ||
332 | global_ret = 1; | ||
333 | return; | ||
334 | } | ||
335 | |||
336 | struct GNUNET_CRYPTO_EcdhePublicKey message_key; | ||
337 | if (GNUNET_OK == GNUNET_STRINGS_string_to_data (key_msg, strlen ( | ||
338 | key_msg), | ||
339 | &message_key, | ||
340 | sizeof(message_key))) | ||
341 | { | ||
342 | char *deserialized_msg; | ||
343 | size_t msg_len; | ||
344 | if (GNUNET_OK == GNUNET_STRINGS_string_to_data_alloc (cipher, strlen ( | ||
345 | cipher), | ||
346 | (void **) & | ||
347 | deserialized_msg, | ||
348 | &msg_len)) | ||
349 | { | 318 | { |
350 | ssize_t res = GNUNET_IDENTITY_decrypt (deserialized_msg, | 319 | deserialized_msg[res - 1] = '\0'; |
351 | msg_len, | 320 | fprintf (stdout, |
352 | GNUNET_IDENTITY_ego_get_private_key ( | 321 | "%s\n", |
353 | ego), | 322 | deserialized_msg); |
354 | &message_key, | ||
355 | deserialized_msg); | ||
356 | if (-1 != res) | ||
357 | { | ||
358 | deserialized_msg[res - 1] = '\0'; | ||
359 | fprintf (stdout, | ||
360 | "%s\n", | ||
361 | deserialized_msg); | ||
362 | } | ||
363 | else | ||
364 | { | ||
365 | fprintf (stderr, "Failed to decrypt message.\n"); | ||
366 | global_ret = 1; | ||
367 | } | ||
368 | GNUNET_free (deserialized_msg); | ||
369 | } | 323 | } |
370 | else | 324 | else |
371 | { | 325 | { |
372 | fprintf (stderr, "Invalid message format.\n"); | 326 | fprintf (stderr, "Failed to decrypt message.\n"); |
373 | global_ret = 1; | 327 | global_ret = 1; |
374 | } | 328 | } |
329 | GNUNET_free (deserialized_msg); | ||
375 | } | 330 | } |
376 | else | 331 | else |
377 | { | 332 | { |
378 | fprintf (stderr, "Invalid message ephemeral key.\n"); | 333 | fprintf (stderr, "Invalid message format.\n"); |
379 | global_ret = 1; | 334 | global_ret = 1; |
380 | } | 335 | } |
381 | } | 336 | } |