GNUnet  0.11.x
messenger_api_message.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2020--2021 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "messenger_api_message.h"
27 
29 {
31  struct GNUNET_HashCode hash;
32 };
33 
35 {
38 };
39 
42 {
44 
45  message->header.kind = kind;
46 
47  switch (message->header.kind)
48  {
50  message->body.name.name = NULL;
51  break;
53  message->body.text.text = NULL;
54  break;
56  message->body.file.uri = NULL;
57  break;
59  message->body.privacy.length = 0;
60  message->body.privacy.data = NULL;
61  break;
62  default:
63  break;
64  }
65 
66  return message;
67 }
68 
70 copy_message (const struct GNUNET_MESSENGER_Message *message)
71 {
72  GNUNET_assert(message);
73 
75 
76  GNUNET_memcpy(copy, message, sizeof(struct GNUNET_MESSENGER_Message));
77 
78  switch (message->header.kind)
79  {
81  copy->body.name.name = GNUNET_strdup(message->body.name.name);
82  break;
84  copy->body.text.text = GNUNET_strdup(message->body.text.text);
85  break;
87  copy->body.file.uri = GNUNET_strdup(message->body.file.uri);
88  break;
90  copy->body.privacy.data = copy->body.privacy.length ? GNUNET_malloc(copy->body.privacy.length) : NULL;
91 
92  if (copy->body.privacy.data)
93  {
94  GNUNET_memcpy(copy->body.privacy.data, message->body.privacy.data, copy->body.privacy.length);
95  }
96 
97  break;
98  default:
99  break;
100  }
101 
102  return copy;
103 }
104 
105 static void
108 {
109  switch (kind)
110  {
113  break;
116  break;
119  break;
122  break;
123  default:
124  break;
125  }
126 }
127 
128 void
130 {
131  GNUNET_assert(message);
132 
133  destroy_message_body (message->header.kind, &(message->body));
134 
135  GNUNET_free(message);
136 }
137 
138 int
140 {
141  GNUNET_assert(message);
142 
143  if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) ||
144  (GNUNET_MESSENGER_KIND_LEAVE == message->header.kind) ||
145  (GNUNET_MESSENGER_KIND_NAME == message->header.kind) ||
146  (GNUNET_MESSENGER_KIND_KEY == message->header.kind) ||
147  (GNUNET_MESSENGER_KIND_ID == message->header.kind))
148  return GNUNET_YES;
149  else
150  return GNUNET_NO;
151 }
152 
153 static void
155  struct GNUNET_MESSENGER_ShortMessage *shortened)
156 {
157  shortened->kind = message->header.kind;
158 
159  GNUNET_memcpy(&(shortened->body), &(message->body), sizeof(struct GNUNET_MESSENGER_MessageBody));
160 }
161 
162 static void
164  struct GNUNET_MESSENGER_Message *message)
165 {
166  destroy_message_body (message->header.kind, &(message->body));
167 
168  message->header.kind = shortened->kind;
169 
170  GNUNET_memcpy(&(message->body), &(shortened->body), sizeof(struct GNUNET_MESSENGER_MessageBody));
171 }
172 
173 #define member_size(type, member) sizeof(((type*) NULL)->member)
174 
175 static uint16_t
177 {
178  uint16_t length = 0;
179 
180  switch (kind)
181  {
184  break;
187  break;
189  length += member_size(struct GNUNET_MESSENGER_Message, body.id.id);
190  break;
193  break;
196  break;
199  break;
203  break;
205  break;
207  length += member_size(struct GNUNET_MESSENGER_Message, body.file.key);
210  break;
213  break;
214  default:
215  break;
216  }
217 
218  return length;
219 }
220 
221 typedef uint32_t kind_t;
222 
223 uint16_t
225 {
226  uint16_t length = 0;
227 
231  length += sizeof(kind_t);
232 
233  return length + get_message_body_kind_size (kind);
234 }
235 
236 static uint16_t
238  const struct GNUNET_MESSENGER_MessageBody *body)
239 {
240  uint16_t length = 0;
241 
242  switch (kind)
243  {
246  break;
249  break;
251  length += (body->name.name ? strlen (body->name.name) : 0);
252  break;
255  break;
257  length += strlen (body->text.text);
258  break;
260  length += strlen (body->file.uri);
261  break;
263  length += body->privacy.length;
264  break;
265  default:
266  break;
267  }
268 
269  return length;
270 }
271 
272 uint16_t
274  int include_signature)
275 {
276  GNUNET_assert(message);
277 
278  uint16_t length = 0;
279 
280  if (GNUNET_YES == include_signature)
282 
283  length += get_message_kind_size (message->header.kind);
284  length += get_message_body_size (message->header.kind, &(message->body));
285 
286  return length;
287 }
288 
289 static uint16_t
291  int include_body)
292 {
293  const uint16_t minimum_size = sizeof(struct GNUNET_HashCode) + sizeof(kind_t);
294 
295  if (message)
296  return minimum_size + get_message_body_kind_size (message->kind)
297  + (include_body == GNUNET_YES? get_message_body_size (message->kind, &(message->body)) : 0);
298  else
299  return minimum_size;
300 }
301 
302 static uint16_t
304 {
305  uint16_t padding = 0;
306  uint16_t kind_size;
307 
308  for (int i = 0; i <= GNUNET_MESSENGER_KIND_MAX; i++) {
310 
311  if (kind_size > padding)
312  padding = kind_size;
313  }
314 
315  return padding + GNUNET_MESSENGER_PADDING_MIN;
316 }
317 
318 #define max(x, y) (x > y? x : y)
319 
320 static uint16_t
321 calc_padded_length (uint16_t length)
322 {
323  static uint16_t usual_padding = 0;
324 
325  if (!usual_padding)
326  usual_padding = calc_usual_padding();
327 
328  const uint16_t padded_length = max(
330  usual_padding
331  );
332 
333  if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL0)
335 
336  if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL1)
338 
339  if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL2)
341 
343 
344 }
345 
346 #define min(x, y) (x < y? x : y)
347 
348 #define encode_step_ext(dst, offset, src, size) do { \
349  GNUNET_memcpy(dst + offset, src, size); \
350  offset += size; \
351 } while (0)
352 
353 #define encode_step(dst, offset, src) do { \
354  encode_step_ext(dst, offset, src, sizeof(*src)); \
355 } while (0)
356 
357 #define encode_step_key(dst, offset, src, length) do { \
358  ssize_t result = GNUNET_IDENTITY_write_key_to_buffer( \
359  src, dst + offset, length - offset \
360  ); \
361  if (result < 0) \
362  GNUNET_break (0); \
363  else \
364  offset += result; \
365 } while (0)
366 
367 #define encode_step_signature(dst, offset, src, length) do { \
368  ssize_t result = GNUNET_IDENTITY_write_signature_to_buffer( \
369  src, dst + offset, length - offset \
370  ); \
371  if (result < 0) \
372  GNUNET_break (0); \
373  else \
374  offset += result; \
375 } while (0)
376 
377 static void
379  const struct GNUNET_MESSENGER_MessageBody *body,
380  uint16_t length,
381  char *buffer,
382  uint16_t offset)
383 {
384  uint32_t version;
385  switch (kind)
386  {
388  version = GNUNET_htobe32(body->info.messenger_version);
389 
390  encode_step_key(buffer, offset, &(body->info.host_key), length);
391  encode_step(buffer, offset, &version);
392  break;
394  encode_step_key(buffer, offset, &(body->join.key), length);
395  break;
397  if (body->name.name)
398  encode_step_ext(buffer, offset, body->name.name, min(length - offset, strlen(body->name.name)));
399  break;
401  encode_step_key(buffer, offset, &(body->key.key), length);
402  break;
404  encode_step(buffer, offset, &(body->peer.peer));
405  break;
407  encode_step(buffer, offset, &(body->id.id));
408  break;
410  encode_step(buffer, offset, &(body->miss.peer));
411  break;
413  encode_step(buffer, offset, &(body->merge.previous));
414  break;
416  encode_step(buffer, offset, &(body->request.hash));
417  break;
419  encode_step(buffer, offset, &(body->invite.door));
420  encode_step(buffer, offset, &(body->invite.key));
421  break;
423  encode_step_ext(buffer, offset, body->text.text, min(length - offset, strlen(body->text.text)));
424  break;
426  encode_step(buffer, offset, &(body->file.key));
427  encode_step(buffer, offset, &(body->file.hash));
428  encode_step_ext(buffer, offset, body->file.name, sizeof(body->file.name));
429  encode_step_ext(buffer, offset, body->file.uri, min(length - offset, strlen(body->file.uri)));
430  break;
432  encode_step(buffer, offset, &(body->privacy.key));
433  encode_step_ext(buffer, offset, body->privacy.data, min(length - offset, body->privacy.length));
434  break;
435  default:
436  break;
437  }
438 
439  if (offset >= length)
440  return;
441 
442  const uint16_t padding = length - offset;
443  const uint16_t used_padding = sizeof(padding) + sizeof(char);
444 
445  GNUNET_assert(padding >= used_padding);
446 
447  buffer[offset++] = '\0';
448 
449  if (padding > used_padding)
450  GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, buffer + offset, padding - used_padding);
451 
452  GNUNET_memcpy(buffer + length - sizeof(padding), &padding, sizeof(padding));
453 }
454 
455 void
457  uint16_t length,
458  char *buffer,
459  int include_signature)
460 {
461  GNUNET_assert((message) && (buffer));
462 
463  uint16_t offset = 0;
464 
465  if (GNUNET_YES == include_signature)
466  encode_step_signature(buffer, offset, &(message->header.signature), length);
467 
468  const kind_t kind = GNUNET_htobe32((kind_t) message->header.kind);
469 
470  encode_step(buffer, offset, &(message->header.timestamp));
471  encode_step(buffer, offset, &(message->header.sender_id));
472  encode_step(buffer, offset, &(message->header.previous));
473  encode_step(buffer, offset, &kind);
474 
475  encode_message_body (message->header.kind, &(message->body), length, buffer, offset);
476 }
477 
478 static void
480  uint16_t length,
481  char *buffer)
482 {
483  struct GNUNET_HashCode hash;
484  uint16_t offset = sizeof(hash);
485 
486  const kind_t kind = GNUNET_htobe32((kind_t) message->kind);
487 
488  encode_step(buffer, offset, &kind);
489 
490  encode_message_body (message->kind, &(message->body), length, buffer, offset);
491 
493  buffer + sizeof(hash),
494  length - sizeof(hash),
495  &hash
496  );
497 
498  GNUNET_memcpy(buffer, &hash, sizeof(hash));
499 }
500 
501 #define decode_step_ext(src, offset, dst, size) do { \
502  GNUNET_memcpy(dst, src + offset, size); \
503  offset += size; \
504 } while (0)
505 
506 #define decode_step(src, offset, dst) do { \
507  decode_step_ext(src, offset, dst, sizeof(*dst)); \
508 } while (0)
509 
510 #define decode_step_malloc(src, offset, dst, size, zero) do { \
511  dst = GNUNET_malloc(size + zero); \
512  if (zero) dst[size] = 0; \
513  decode_step_ext(src, offset, dst, size); \
514 } while (0)
515 
516 #define decode_step_key(src, offset, dst, length) do { \
517  ssize_t result = GNUNET_IDENTITY_read_key_from_buffer( \
518  dst, src + offset, length - offset \
519  ); \
520  if (result < 0) \
521  GNUNET_break(0); \
522  else \
523  offset += result; \
524 } while (0)
525 
526 static uint16_t
528  struct GNUNET_MESSENGER_MessageBody *body,
529  uint16_t length,
530  const char *buffer,
531  uint16_t offset)
532 {
533  uint16_t padding = 0;
534 
535  GNUNET_memcpy(&padding, buffer + length - sizeof(padding), sizeof(padding));
536 
537  if (padding > length - offset)
538  padding = 0;
539 
540  const uint16_t end_zero = length - padding;
541 
542  if ((padding) && (buffer[end_zero] != '\0'))
543  padding = 0;
544 
545  length -= padding;
546 
547  uint32_t version;
548  switch (*kind)
549  {
551  decode_step_key(buffer, offset, &(body->info.host_key), length);
552  decode_step(buffer, offset, &version);
553 
554  body->info.messenger_version = GNUNET_be32toh(version);
555  break;
557  decode_step_key(buffer, offset, &(body->join.key), length);
558  break;
560  if (length - offset > 0)
561  decode_step_malloc(buffer, offset, body->name.name, length - offset, 1);
562  else
563  body->name.name = NULL;
564  break;
566  decode_step_key(buffer, offset, &(body->key.key), length);
567  break;
569  decode_step(buffer, offset, &(body->peer.peer));
570  break;
572  decode_step(buffer, offset, &(body->id.id));
573  break;
575  decode_step(buffer, offset, &(body->miss.peer));
576  break;
578  decode_step(buffer, offset, &(body->merge.previous));
579  break;
581  decode_step(buffer, offset, &(body->request.hash));
582  break;
584  decode_step(buffer, offset, &(body->invite.door));
585  decode_step(buffer, offset, &(body->invite.key));
586  break;
588  decode_step_malloc(buffer, offset, body->text.text, length - offset, 1);
589  break;
591  decode_step(buffer, offset, &(body->file.key));
592  decode_step(buffer, offset, &(body->file.hash));
593  decode_step_ext(buffer, offset, body->file.name, sizeof(body->file.name));
594  decode_step_malloc(buffer, offset, body->file.uri, length - offset, 1);
595  break;
597  decode_step(buffer, offset, &(body->privacy.key));
598 
599  body->privacy.length = (length - offset);
600  decode_step_malloc(buffer, offset, body->privacy.data, length - offset, 0);
601  break;
602  default:
604  break;
605  }
606 
607  return padding;
608 }
609 
610 int
612  uint16_t length,
613  const char *buffer,
614  int include_signature,
615  uint16_t *padding)
616 {
617  GNUNET_assert((message) && (buffer) && (length >= get_message_kind_size(GNUNET_MESSENGER_KIND_UNKNOWN)));
618 
619  uint16_t offset = 0;
620 
621  if (GNUNET_YES == include_signature)
622  {
624  &(message->header.signature), buffer, length - offset
625  );
626 
627  if (result < 0)
628  return GNUNET_NO;
629  else
630  offset += result;
631  }
632 
633  const uint16_t count = length - offset;
634 
636  return GNUNET_NO;
637 
638  kind_t kind;
639 
640  decode_step(buffer, offset, &(message->header.timestamp));
641  decode_step(buffer, offset, &(message->header.sender_id));
642  decode_step(buffer, offset, &(message->header.previous));
643  decode_step(buffer, offset, &kind);
644 
645  message->header.kind = (enum GNUNET_MESSENGER_MessageKind) GNUNET_be32toh(kind);
646 
647  if (count < get_message_kind_size (message->header.kind))
648  return GNUNET_NO;
649 
650  const uint16_t result = decode_message_body (&(message->header.kind), &(message->body), length, buffer, offset);
651 
652  if (padding)
653  *padding = result;
654 
655  return GNUNET_YES;
656 }
657 
658 static int
660  uint16_t length,
661  const char *buffer)
662 {
663  struct GNUNET_HashCode expected, hash;
664  uint16_t offset = sizeof(hash);
665 
666  if (length < get_short_message_size (NULL, GNUNET_NO))
667  return GNUNET_NO;
668 
669  GNUNET_memcpy(&hash, buffer, sizeof(hash));
670 
672  buffer + sizeof(hash),
673  length - sizeof(hash),
674  &expected
675  );
676 
677  if (0 != GNUNET_CRYPTO_hash_cmp(&hash, &expected))
678  return GNUNET_NO;
679 
680  kind_t kind;
681 
682  decode_step(buffer, offset, &kind);
683 
684  message->kind = (enum GNUNET_MESSENGER_MessageKind) GNUNET_be32toh(kind);
685 
686  if (length < get_short_message_size (message, GNUNET_NO))
687  return GNUNET_NO;
688 
689  decode_message_body (&(message->kind), &(message->body), length, buffer, offset);
690 
691  if (GNUNET_MESSENGER_KIND_UNKNOWN == message->kind)
692  return GNUNET_NO;
693 
694  return GNUNET_YES;
695 }
696 
697 void
698 hash_message (const struct GNUNET_MESSENGER_Message *message,
699  uint16_t length,
700  const char *buffer,
701  struct GNUNET_HashCode *hash)
702 {
703  GNUNET_assert((message) && (buffer) && (hash));
704 
705  const ssize_t offset = GNUNET_IDENTITY_signature_get_length(
706  &(message->header.signature)
707  );
708 
709  GNUNET_CRYPTO_hash (buffer + offset, length - offset, hash);
710 }
711 
712 void
714  uint16_t length,
715  char *buffer,
716  const struct GNUNET_HashCode *hash,
717  const struct GNUNET_MESSENGER_Ego *ego)
718 {
719  GNUNET_assert((message) && (buffer) && (hash) && (ego));
720 
721  struct GNUNET_MESSENGER_MessageSignature signature;
722 
724  signature.purpose.size = htonl (sizeof(signature));
725 
726  GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
727  GNUNET_IDENTITY_sign(&(ego->priv), &signature, &(message->header.signature));
728 
729  uint16_t offset = 0;
730  encode_step_signature(buffer, offset, &(message->header.signature), length);
731 }
732 
733 int
735  const struct GNUNET_HashCode *hash,
736  const struct GNUNET_IDENTITY_PublicKey *key)
737 {
738  GNUNET_assert((message) && (hash) && (key));
739 
740  if (ntohl (key->type) != ntohl (message->header.signature.type))
741  return GNUNET_SYSERR;
742 
743  struct GNUNET_MESSENGER_MessageSignature signature;
744 
746  signature.purpose.size = htonl (sizeof(signature));
747 
748  GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
749 
751  &(message->header.signature), key);
752 }
753 
754 int
756  const struct GNUNET_IDENTITY_PublicKey *key)
757 {
758  GNUNET_assert((message) && (key));
759 
760  struct GNUNET_MESSENGER_ShortMessage shortened;
761 
762  fold_short_message (message, &shortened);
763 
764  const uint16_t length = get_short_message_size (&shortened, GNUNET_YES);
765  const uint16_t padded_length = calc_padded_length(length);
766 
768  message->body.privacy.data = GNUNET_malloc(padded_length);
769  message->body.privacy.length = padded_length;
770 
771  encode_short_message (&shortened, padded_length, message->body.privacy.data);
772 
773  if (padded_length == GNUNET_IDENTITY_encrypt (message->body.privacy.data, padded_length, key,
774  &(message->body.privacy.key),
775  message->body.privacy.data))
776  {
777  destroy_message_body (shortened.kind, &(shortened.body));
778  return GNUNET_YES;
779  }
780  else
781  {
782  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Encrypting message failed!\n");
783 
784  unfold_short_message (&shortened, message);
785  return GNUNET_NO;
786  }
787 }
788 
789 int
791  const struct GNUNET_IDENTITY_PrivateKey *key)
792 {
793  GNUNET_assert((message) && (key));
794 
795  if (message->body.privacy.length != GNUNET_IDENTITY_decrypt (message->body.privacy.data, message->body.privacy.length,
796  key, &(message->body.privacy.key),
797  message->body.privacy.data))
798  {
799  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Decrypting message failed!\n");
800 
801  return GNUNET_NO;
802  }
803 
804  struct GNUNET_MESSENGER_ShortMessage shortened;
805 
806  if (GNUNET_YES != decode_short_message (&shortened, message->body.privacy.length, message->body.privacy.data))
807  {
808  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Decoding decrypted message failed!\n");
809 
810  return GNUNET_NO;
811  }
812 
813  unfold_short_message (&shortened, message);
814 
815  return GNUNET_YES;
816 }
817 
818 struct GNUNET_MQ_Envelope*
820  struct GNUNET_HashCode *hash,
821  const struct GNUNET_MESSENGER_Ego *ego,
822  int mode)
823 {
824  GNUNET_assert(message);
825 
826  if (ego)
827  message->header.signature.type = ego->priv.type;
828 
829  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packing message kind=%u and sender: %s\n",
830  message->header.kind, GNUNET_sh2s(&(message->header.sender_id)));
831 
832  struct GNUNET_MessageHeader *header;
833 
834  const uint16_t length = get_message_size (message, GNUNET_YES);
835  const uint16_t padded_length = calc_padded_length(length);
836 
837  struct GNUNET_MQ_Envelope *env;
838  char *buffer;
839 
841  {
842  env = GNUNET_MQ_msg_extra(header, padded_length, GNUNET_MESSAGE_TYPE_CADET_CLI);
843 
844  buffer = (char*) &(header[1]);
845  }
846  else
847  {
848  env = NULL;
849 
850  buffer = GNUNET_malloc(padded_length);
851  }
852 
853  encode_message (message, padded_length, buffer, GNUNET_YES);
854 
855  if (hash)
856  {
857  hash_message (message, length, buffer, hash);
858 
859  if (ego)
860  sign_message (message, length, buffer, hash, ego);
861  }
862 
864  GNUNET_free(buffer);
865 
866  return env;
867 }
868 
869 int
871 {
872  switch (message->header.kind)
873  {
875  return GNUNET_SYSERR; // Reserved for connection handling only!
877  return GNUNET_NO; // Use #GNUNET_MESSENGER_enter_room(...) instead!
879  return GNUNET_NO; // Use #GNUNET_MESSENGER_close_room(...) instead!
881  return GNUNET_YES;
883  return GNUNET_NO; // Use #GNUNET_MESSENGER_update(...) instead!
885  return GNUNET_NO; // Use #GNUNET_MESSENGER_open_room(...) instead!
887  return GNUNET_SYSERR; // Reserved for member id handling only!
889  return GNUNET_SYSERR; // Reserved for connection handling only!
891  return GNUNET_YES;
893  return GNUNET_YES;
895  return GNUNET_YES;
897  return GNUNET_YES;
899  return GNUNET_YES;
901  return GNUNET_NO; // Use #GNUNET_MESSENGER_send_message(...) with a contact instead!
903  return GNUNET_YES;
904  default:
905  return GNUNET_SYSERR;
906  }
907 }
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct GNUNET_HashCode key
The key used in the DHT.
static int result
Global testing status.
#define GNUNET_log(kind,...)
#define GNUNET_be32toh(x)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_htobe32(x)
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
Definition: crypto_hash.c:201
ssize_t GNUNET_IDENTITY_decrypt(const void *block, size_t size, const struct GNUNET_IDENTITY_PrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *ecc, void *result)
Decrypt a given block with GNUNET_IDENTITY_PrivateKey and a given GNUNET_CRYPTO_EcdhePublicKey using ...
ssize_t GNUNET_IDENTITY_encrypt(const void *block, size_t size, const struct GNUNET_IDENTITY_PublicKey *pub, struct GNUNET_CRYPTO_EcdhePublicKey *ecc, void *result)
Encrypt a block with GNUNET_IDENTITY_PublicKey and derives a GNUNET_CRYPTO_EcdhePublicKey which is re...
#define GNUNET_IDENTITY_signature_verify(purp, ps, sig, pub)
Verify a given signature with GNUNET_IDENTITY_PublicKey.
ssize_t GNUNET_IDENTITY_read_signature_from_buffer(struct GNUNET_IDENTITY_Signature *sig, const void *buffer, size_t len)
Reads a GNUNET_IDENTITY_Signature from a compact buffer.
#define GNUNET_IDENTITY_sign(priv, ps, sig)
Sign a given block with GNUNET_IDENTITY_PrivateKey.
ssize_t GNUNET_IDENTITY_key_get_length(const struct GNUNET_IDENTITY_PublicKey *key)
Get the compacted length of a GNUNET_IDENTITY_PublicKey.
Definition: identity_api.c:976
ssize_t GNUNET_IDENTITY_signature_get_length(const struct GNUNET_IDENTITY_Signature *sig)
Get the compacted length of a GNUNET_IDENTITY_Signature.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc)
Convert a short hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MESSENGER_MessageKind
Enum for the different supported kinds of messages.
#define GNUNET_MESSENGER_KIND_MAX
@ GNUNET_MESSENGER_KIND_INFO
The info kind.
@ GNUNET_MESSENGER_KIND_MISS
The miss kind.
@ GNUNET_MESSENGER_KIND_INVITE
The invite kind.
@ GNUNET_MESSENGER_KIND_PRIVATE
The private kind.
@ GNUNET_MESSENGER_KIND_FILE
The file kind.
@ GNUNET_MESSENGER_KIND_REQUEST
The request kind.
@ GNUNET_MESSENGER_KIND_NAME
The name kind.
@ GNUNET_MESSENGER_KIND_LEAVE
The leave kind.
@ GNUNET_MESSENGER_KIND_PEER
The peer kind.
@ GNUNET_MESSENGER_KIND_UNKNOWN
The unknown kind.
@ GNUNET_MESSENGER_KIND_KEY
The key kind.
@ GNUNET_MESSENGER_KIND_TEXT
The text kind.
@ GNUNET_MESSENGER_KIND_JOIN
The join kind.
@ GNUNET_MESSENGER_KIND_DELETE
The delete kind.
@ GNUNET_MESSENGER_KIND_MERGE
The merge kind.
@ GNUNET_MESSENGER_KIND_ID
The id kind.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:52
#define GNUNET_MESSAGE_TYPE_CADET_CLI
Traffic (net-cat style) used by the Command Line Interface.
#define GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE
Signature of a chat message.
static void destroy_message_body(enum GNUNET_MESSENGER_MessageKind kind, struct GNUNET_MESSENGER_MessageBody *body)
#define decode_step(src, offset, dst)
static uint16_t get_short_message_size(const struct GNUNET_MESSENGER_ShortMessage *message, int include_body)
#define encode_step_ext(dst, offset, src, size)
static uint16_t get_message_body_kind_size(enum GNUNET_MESSENGER_MessageKind kind)
int decrypt_message(struct GNUNET_MESSENGER_Message *message, const struct GNUNET_IDENTITY_PrivateKey *key)
Decrypts a private message using a given private key and replaces its body and kind with the inner en...
static uint16_t get_message_body_size(enum GNUNET_MESSENGER_MessageKind kind, const struct GNUNET_MESSENGER_MessageBody *body)
void sign_message(struct GNUNET_MESSENGER_Message *message, uint16_t length, char *buffer, const struct GNUNET_HashCode *hash, const struct GNUNET_MESSENGER_Ego *ego)
Signs the hash of a message with a given ego and writes the signature into the buffer as well.
struct GNUNET_MQ_Envelope * pack_message(struct GNUNET_MESSENGER_Message *message, struct GNUNET_HashCode *hash, const struct GNUNET_MESSENGER_Ego *ego, int mode)
Encodes the message to pack it into a newly allocated envelope if mode is equal to GNUNET_MESSENGER_P...
static void fold_short_message(const struct GNUNET_MESSENGER_Message *message, struct GNUNET_MESSENGER_ShortMessage *shortened)
int decode_message(struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer, int include_signature, uint16_t *padding)
Decodes a message from a given buffer of a maximal length in bytes.
#define decode_step_key(src, offset, dst, length)
static uint16_t calc_padded_length(uint16_t length)
int filter_message_sending(const struct GNUNET_MESSENGER_Message *message)
Returns if a specific kind of message should be sent by a client.
#define decode_step_malloc(src, offset, dst, size, zero)
#define encode_step(dst, offset, src)
int encrypt_message(struct GNUNET_MESSENGER_Message *message, const struct GNUNET_IDENTITY_PublicKey *key)
Encrypts a message using a given public key and replaces its body and kind with the now private encry...
uint16_t get_message_size(const struct GNUNET_MESSENGER_Message *message, int include_signature)
Returns the exact size in bytes to encode a given message.
static void encode_message_body(enum GNUNET_MESSENGER_MessageKind kind, const struct GNUNET_MESSENGER_MessageBody *body, uint16_t length, char *buffer, uint16_t offset)
static void encode_short_message(const struct GNUNET_MESSENGER_ShortMessage *message, uint16_t length, char *buffer)
struct GNUNET_MESSENGER_Message * copy_message(const struct GNUNET_MESSENGER_Message *message)
Creates and allocates a copy of a given message.
int is_message_session_bound(const struct GNUNET_MESSENGER_Message *message)
Returns if the message should be bound to a member session.
void hash_message(const struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer, struct GNUNET_HashCode *hash)
Calculates a hash of a given buffer with a length in bytes from a message.
int verify_message(const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash, const struct GNUNET_IDENTITY_PublicKey *key)
Verifies the signature of a given message and its hash with a specific public key.
#define decode_step_ext(src, offset, dst, size)
static void unfold_short_message(struct GNUNET_MESSENGER_ShortMessage *shortened, struct GNUNET_MESSENGER_Message *message)
#define encode_step_signature(dst, offset, src, length)
static uint16_t calc_usual_padding()
static uint16_t decode_message_body(enum GNUNET_MESSENGER_MessageKind *kind, struct GNUNET_MESSENGER_MessageBody *body, uint16_t length, const char *buffer, uint16_t offset)
uint32_t kind_t
#define min(x, y)
#define max(x, y)
uint16_t get_message_kind_size(enum GNUNET_MESSENGER_MessageKind kind)
Returns the minimal size in bytes to encode a message of a specific kind.
void destroy_message(struct GNUNET_MESSENGER_Message *message)
Destroys a message and frees its memory fully.
void encode_message(const struct GNUNET_MESSENGER_Message *message, uint16_t length, char *buffer, int include_signature)
Encodes a given message into a buffer of a maximal length in bytes.
#define encode_step_key(dst, offset, src, length)
struct GNUNET_MESSENGER_Message * create_message(enum GNUNET_MESSENGER_MessageKind kind)
Creates and allocates a new message with a specific kind.
static int decode_short_message(struct GNUNET_MESSENGER_ShortMessage *message, uint16_t length, const char *buffer)
#define member_size(type, member)
messenger api: client and service implementation of GNUnet MESSENGER service
#define GNUNET_MESSENGER_MAX_MESSAGE_SIZE
#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE
#define GNUNET_MESSENGER_PADDING_LEVEL0
#define GNUNET_MESSENGER_PADDING_MIN
#define GNUNET_MESSENGER_PADDING_LEVEL2
#define GNUNET_MESSENGER_PADDING_LEVEL1
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
A 512-bit hashcode.
A private key for an identity as per LSD0001.
uint32_t type
Type of public key.
An identity key as per LSD0001.
uint32_t type
Type of signature.
struct GNUNET_IDENTITY_PrivateKey priv
The unified body of a GNUNET_MESSENGER_Message.
struct GNUNET_MESSENGER_MessagePrivate privacy
struct GNUNET_MESSENGER_MessageText text
struct GNUNET_MESSENGER_MessageRequest request
struct GNUNET_MESSENGER_MessageMerge merge
struct GNUNET_MESSENGER_MessageId id
struct GNUNET_MESSENGER_MessageName name
struct GNUNET_MESSENGER_MessageFile file
struct GNUNET_MESSENGER_MessageKey key
struct GNUNET_MESSENGER_MessageInvite invite
struct GNUNET_MESSENGER_MessageMiss miss
struct GNUNET_MESSENGER_MessagePeer peer
struct GNUNET_MESSENGER_MessageJoin join
struct GNUNET_MESSENGER_MessageInfo info
char * uri
The uri of the encrypted file.
struct GNUNET_HashCode hash
The hash of the original file.
struct GNUNET_CRYPTO_SymmetricSessionKey key
The symmetric key to decrypt the file.
char name[NAME_MAX]
The name of the original file.
struct GNUNET_HashCode previous
The hash of the previous message from the senders perspective.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the message.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
struct GNUNET_ShortHashCode id
The new id which will replace the senders id in a room.
uint32_t messenger_version
The version of GNUnet Messenger API.
struct GNUNET_IDENTITY_PublicKey host_key
The senders key to verify its signatures.
struct GNUNET_HashCode key
The hash identifying the port of the room.
struct GNUNET_PeerIdentity door
The peer identity of an open door to a room.
struct GNUNET_IDENTITY_PublicKey key
The senders public key to verify its signatures.
struct GNUNET_IDENTITY_PublicKey key
The new public key which replaces the current senders public key.
struct GNUNET_HashCode previous
The hash of a second previous message.
struct GNUNET_PeerIdentity peer
The peer identity of a disconnected door to a room.
char * name
The new name which replaces the current senders name.
struct GNUNET_PeerIdentity peer
The peer identity of the sender opening a room.
uint16_t length
The length of the encrypted message.
struct GNUNET_CRYPTO_EcdhePublicKey key
The ECDH key to decrypt the message.
char * data
The data of the encrypted message.
struct GNUNET_HashCode hash
The hash of the requested message.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
char * text
The containing text.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_MESSENGER_MessageBody body
enum GNUNET_MESSENGER_MessageKind kind
Header for all communications.