GNUnet  0.20.0
messenger_api_message.h File Reference

messenger api: client and service implementation of GNUnet MESSENGER service More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_identity_service.h"
#include "gnunet_signatures.h"
#include "gnunet_messenger_service.h"
#include "messenger_api_ego.h"
Include dependency graph for messenger_api_message.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define GNUNET_MESSENGER_MAX_MESSAGE_SIZE   (GNUNET_MAX_MESSAGE_SIZE - GNUNET_MIN_MESSAGE_SIZE)
 
#define GNUNET_MESSENGER_PADDING_MIN   (sizeof(uint16_t) + sizeof(char))
 
#define GNUNET_MESSENGER_PADDING_LEVEL0   (512)
 
#define GNUNET_MESSENGER_PADDING_LEVEL1   (4096)
 
#define GNUNET_MESSENGER_PADDING_LEVEL2   (32768)
 
#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE   0x1
 
#define GNUNET_MESSENGER_PACK_MODE_UNKNOWN   0x0
 

Functions

struct GNUNET_MESSENGER_Messagecreate_message (enum GNUNET_MESSENGER_MessageKind kind)
 Creates and allocates a new message with a specific kind. More...
 
struct GNUNET_MESSENGER_Messagecopy_message (const struct GNUNET_MESSENGER_Message *message)
 Creates and allocates a copy of a given message. More...
 
void cleanup_message (struct GNUNET_MESSENGER_Message *message)
 Frees the messages body memory. More...
 
void destroy_message (struct GNUNET_MESSENGER_Message *message)
 Destroys a message and frees its memory fully. More...
 
int is_message_session_bound (const struct GNUNET_MESSENGER_Message *message)
 Returns if the message should be bound to a member session. More...
 
uint16_t get_message_kind_size (enum GNUNET_MESSENGER_MessageKind kind, int include_header)
 Returns the minimal size in bytes to encode a message of a specific kind. More...
 
uint16_t get_message_size (const struct GNUNET_MESSENGER_Message *message, int include_header)
 Returns the exact size in bytes to encode a given message. More...
 
void encode_message (const struct GNUNET_MESSENGER_Message *message, uint16_t length, char *buffer, int include_header)
 Encodes a given message into a buffer of a maximal length in bytes. More...
 
int decode_message (struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer, int include_header, uint16_t *padding)
 Decodes a message from a given buffer of a maximal length in bytes. More...
 
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. More...
 
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. More...
 
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. More...
 
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 encrypted message. More...
 
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 encrypted message. More...
 
struct GNUNET_MQ_Envelopepack_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_PACK_MODE_ENVELOPE. More...
 
int filter_message_sending (const struct GNUNET_MESSENGER_Message *message)
 Returns if a specific kind of message should be sent by a client. More...
 

Detailed Description

messenger api: client and service implementation of GNUnet MESSENGER service

Author
Tobias Frisch

Definition in file messenger_api_message.h.

Macro Definition Documentation

◆ GNUNET_MESSENGER_MAX_MESSAGE_SIZE

#define GNUNET_MESSENGER_MAX_MESSAGE_SIZE   (GNUNET_MAX_MESSAGE_SIZE - GNUNET_MIN_MESSAGE_SIZE)

Definition at line 38 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PADDING_MIN

#define GNUNET_MESSENGER_PADDING_MIN   (sizeof(uint16_t) + sizeof(char))

Definition at line 40 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PADDING_LEVEL0

#define GNUNET_MESSENGER_PADDING_LEVEL0   (512)

Definition at line 41 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PADDING_LEVEL1

#define GNUNET_MESSENGER_PADDING_LEVEL1   (4096)

Definition at line 42 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PADDING_LEVEL2

#define GNUNET_MESSENGER_PADDING_LEVEL2   (32768)

Definition at line 43 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PACK_MODE_ENVELOPE

#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE   0x1

Definition at line 219 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PACK_MODE_UNKNOWN

#define GNUNET_MESSENGER_PACK_MODE_UNKNOWN   0x0

Definition at line 220 of file messenger_api_message.h.

Function Documentation

◆ create_message()

struct GNUNET_MESSENGER_Message* create_message ( enum GNUNET_MESSENGER_MessageKind  kind)

Creates and allocates a new message with a specific kind.

Parameters
[in]kindKind of message
Returns
New message

Definition at line 42 of file messenger_api_message.c.

43 {
44  struct GNUNET_MESSENGER_Message *message = GNUNET_new (struct
46 
47  message->header.kind = kind;
48 
49  switch (message->header.kind)
50  {
52  message->body.name.name = NULL;
53  break;
55  message->body.text.text = NULL;
56  break;
58  message->body.file.uri = NULL;
59  break;
61  message->body.privacy.length = 0;
62  message->body.privacy.data = NULL;
63  break;
64  default:
65  break;
66  }
67 
68  return message;
69 }
#define GNUNET_new(type)
Allocate a struct or union of the given type.
@ GNUNET_MESSENGER_KIND_PRIVATE
The private kind.
@ GNUNET_MESSENGER_KIND_FILE
The file kind.
@ GNUNET_MESSENGER_KIND_NAME
The name kind.
@ GNUNET_MESSENGER_KIND_TEXT
The text kind.
struct GNUNET_MESSENGER_MessagePrivate privacy
struct GNUNET_MESSENGER_MessageText text
struct GNUNET_MESSENGER_MessageName name
struct GNUNET_MESSENGER_MessageFile file
char * uri
The uri of the encrypted file.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
char * name
The new name which replaces the current senders name.
uint16_t length
The length of the encrypted message.
char * data
The data of the encrypted message.
char * text
The containing text.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_MessageBody body
Body.

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_MessagePrivate::data, GNUNET_MESSENGER_MessageBody::file, GNUNET_MESSENGER_KIND_FILE, GNUNET_MESSENGER_KIND_NAME, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_MESSENGER_KIND_TEXT, GNUNET_new, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageName::name, GNUNET_MESSENGER_MessageBody::name, GNUNET_MESSENGER_MessageBody::privacy, GNUNET_MESSENGER_MessageText::text, GNUNET_MESSENGER_MessageBody::text, and GNUNET_MESSENGER_MessageFile::uri.

Referenced by create_message_delete(), create_message_id(), create_message_info(), create_message_invite(), create_message_join(), create_message_key(), create_message_leave(), create_message_merge(), create_message_miss(), create_message_name(), create_message_peer(), create_message_request(), create_message_text(), and get_store_message().

Here is the caller graph for this function:

◆ copy_message()

struct GNUNET_MESSENGER_Message* copy_message ( const struct GNUNET_MESSENGER_Message message)

Creates and allocates a copy of a given message.

Parameters
[in]messageMessage
Returns
New message

Definition at line 73 of file messenger_api_message.c.

74 {
75  GNUNET_assert (message);
76 
77  struct GNUNET_MESSENGER_Message *copy = GNUNET_new (struct
79 
80  GNUNET_memcpy (copy, message, sizeof(struct GNUNET_MESSENGER_Message));
81 
82  switch (message->header.kind)
83  {
85  copy->body.name.name = GNUNET_strdup (message->body.name.name);
86  break;
88  copy->body.text.text = GNUNET_strdup (message->body.text.text);
89  break;
91  copy->body.file.uri = GNUNET_strdup (message->body.file.uri);
92  break;
94  copy->body.privacy.data = copy->body.privacy.length ? GNUNET_malloc (
95  copy->body.privacy.length) : NULL;
96 
97  if (copy->body.privacy.data)
98  {
99  GNUNET_memcpy (copy->body.privacy.data, message->body.privacy.data,
100  copy->body.privacy.length);
101  }
102 
103  break;
104  default:
105  break;
106  }
107 
108  return copy;
109 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_malloc(size)
Wrapper around malloc.

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_MessagePrivate::data, GNUNET_MESSENGER_MessageBody::file, GNUNET_assert, GNUNET_malloc, GNUNET_memcpy, GNUNET_MESSENGER_KIND_FILE, GNUNET_MESSENGER_KIND_NAME, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_MESSENGER_KIND_TEXT, GNUNET_new, GNUNET_strdup, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageName::name, GNUNET_MESSENGER_MessageBody::name, GNUNET_MESSENGER_MessageBody::privacy, GNUNET_MESSENGER_MessageText::text, GNUNET_MESSENGER_MessageBody::text, and GNUNET_MESSENGER_MessageFile::uri.

Referenced by forward_tunnel_message(), handle_room_message(), handle_tunnel_message(), notify_srv_handle_message(), and send_srv_handle_message().

Here is the caller graph for this function:

◆ cleanup_message()

void cleanup_message ( struct GNUNET_MESSENGER_Message message)

Frees the messages body memory.

Parameters
[in,out]messageMessage

Definition at line 137 of file messenger_api_message.c.

138 {
139  GNUNET_assert (message);
140 
141  destroy_message_body (message->header.kind, &(message->body));
142 }
static void destroy_message_body(enum GNUNET_MESSENGER_MessageKind kind, struct GNUNET_MESSENGER_MessageBody *body)

References GNUNET_MESSENGER_Message::body, destroy_message_body(), GNUNET_assert, GNUNET_MESSENGER_Message::header, and GNUNET_MESSENGER_MessageHeader::kind.

Referenced by check_recv_message(), check_send_message(), handle_recv_message(), handle_send_message(), and handle_tunnel_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroy_message()

void destroy_message ( struct GNUNET_MESSENGER_Message message)

Destroys a message and frees its memory fully.

Parameters
[in,out]messageMessage

Definition at line 146 of file messenger_api_message.c.

147 {
148  GNUNET_assert (message);
149 
150  destroy_message_body (message->header.kind, &(message->body));
151 
152  GNUNET_free (message);
153 }
#define GNUNET_free(ptr)
Wrapper around free.

References GNUNET_MESSENGER_Message::body, destroy_message_body(), GNUNET_assert, GNUNET_free, GNUNET_MESSENGER_Message::header, and GNUNET_MESSENGER_MessageHeader::kind.

Referenced by callback_set_handle_name(), create_message_peer(), forward_tunnel_message(), get_store_message(), handle_delete_message(), handle_room_message(), iterate_destroy_message(), iterate_destroy_messages(), notify_srv_handle_message(), send_tunnel_message(), set_srv_handle_ego(), and update_room_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_message_session_bound()

int is_message_session_bound ( const struct GNUNET_MESSENGER_Message message)

Returns if the message should be bound to a member session.

Parameters
[in]messageMessage
Returns
GNUNET_YES or GNUNET_NO

Definition at line 157 of file messenger_api_message.c.

158 {
159  GNUNET_assert (message);
160 
161  if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) ||
162  (GNUNET_MESSENGER_KIND_LEAVE == message->header.kind) ||
163  (GNUNET_MESSENGER_KIND_NAME == message->header.kind) ||
164  (GNUNET_MESSENGER_KIND_KEY == message->header.kind) ||
165  (GNUNET_MESSENGER_KIND_ID == message->header.kind))
166  return GNUNET_YES;
167  else
168  return GNUNET_NO;
169 }
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_MESSENGER_KIND_LEAVE
The leave kind.
@ GNUNET_MESSENGER_KIND_KEY
The key kind.
@ GNUNET_MESSENGER_KIND_JOIN
The join kind.
@ GNUNET_MESSENGER_KIND_ID
The id kind.

References GNUNET_assert, GNUNET_MESSENGER_KIND_ID, GNUNET_MESSENGER_KIND_JOIN, GNUNET_MESSENGER_KIND_KEY, GNUNET_MESSENGER_KIND_LEAVE, GNUNET_MESSENGER_KIND_NAME, GNUNET_NO, GNUNET_YES, GNUNET_MESSENGER_Message::header, and GNUNET_MESSENGER_MessageHeader::kind.

Referenced by send_srv_room_message(), and update_member_session_history().

Here is the caller graph for this function:

◆ get_message_kind_size()

uint16_t get_message_kind_size ( enum GNUNET_MESSENGER_MessageKind  kind,
int  include_header 
)

Returns the minimal size in bytes to encode a message of a specific kind.

Parameters
[in]kindKind of message
[in]include_headerFlag to include header
Returns
Minimal size to encode

Definition at line 252 of file messenger_api_message.c.

254 {
255  uint16_t length = 0;
256 
257  if (GNUNET_YES == include_header)
258  {
259  length += member_size (struct GNUNET_MESSENGER_Message, header.timestamp);
260  length += member_size (struct GNUNET_MESSENGER_Message, header.sender_id);
261  length += member_size (struct GNUNET_MESSENGER_Message, header.previous);
262  }
263 
264  length += sizeof(kind_t);
265 
266  return length + get_message_body_kind_size (kind);
267 }
static uint16_t get_message_body_kind_size(enum GNUNET_MESSENGER_MessageKind kind)
uint32_t kind_t
#define member_size(type, member)

References get_message_body_kind_size(), GNUNET_YES, GNUNET_MESSENGER_Message::header, member_size, GNUNET_MESSENGER_MessageHeader::previous, GNUNET_MESSENGER_MessageHeader::sender_id, and GNUNET_MESSENGER_MessageHeader::timestamp.

Referenced by calc_usual_padding(), check_recv_message(), check_send_message(), check_tunnel_message(), decode_message(), get_message_size(), and get_store_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_message_size()

uint16_t get_message_size ( const struct GNUNET_MESSENGER_Message message,
int  include_header 
)

Returns the exact size in bytes to encode a given message.

Parameters
[in]messageMessage
[in]include_headerFlag to include header
Returns
Size to encode

Definition at line 308 of file messenger_api_message.c.

310 {
311  GNUNET_assert (message);
312 
313  uint16_t length = 0;
314 
315  if (GNUNET_YES == include_header)
317  &(message->header.signature));
318 
319  length += get_message_kind_size (message->header.kind, include_header);
320  length += get_message_body_size (message->header.kind, &(message->body));
321 
322  return length;
323 }
ssize_t GNUNET_IDENTITY_signature_get_length(const struct GNUNET_IDENTITY_Signature *sig)
Get the compacted length of a GNUNET_IDENTITY_Signature.
Definition: identity_api.c:951
static uint16_t get_message_body_size(enum GNUNET_MESSENGER_MessageKind kind, const struct GNUNET_MESSENGER_MessageBody *body)
uint16_t get_message_kind_size(enum GNUNET_MESSENGER_MessageKind kind, int include_header)
Returns the minimal size in bytes to encode a message of a specific kind.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.

References GNUNET_MESSENGER_Message::body, get_message_body_size(), get_message_kind_size(), GNUNET_assert, GNUNET_IDENTITY_signature_get_length(), GNUNET_YES, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, and GNUNET_MESSENGER_MessageHeader::signature.

Referenced by GNUNET_MESSENGER_send_message(), iterate_save_messages(), notify_srv_handle_message(), and pack_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_message()

void encode_message ( const struct GNUNET_MESSENGER_Message message,
uint16_t  length,
char *  buffer,
int  include_header 
)

Encodes a given message into a buffer of a maximal length in bytes.

Parameters
[in]messageMessage
[in]lengthMaximal length to encode
[out]bufferBuffer
[in]include_headerFlag to include header

Definition at line 513 of file messenger_api_message.c.

517 {
518  GNUNET_assert ((message) && (buffer));
519 
520  uint16_t offset = 0;
521 
522  if (GNUNET_YES == include_header)
523  encode_step_signature (buffer, offset, &(message->header.signature),
524  length);
525 
526  const kind_t kind = GNUNET_htobe32 ((kind_t) message->header.kind);
527 
528  if (GNUNET_YES == include_header)
529  {
530  encode_step (buffer, offset, &(message->header.timestamp));
531  encode_step (buffer, offset, &(message->header.sender_id));
532  encode_step (buffer, offset, &(message->header.previous));
533  }
534 
535  encode_step (buffer, offset, &kind);
536 
537  encode_message_body (message->header.kind, &(message->body), length, buffer,
538  offset);
539 }
#define GNUNET_htobe32(x)
#define encode_step(dst, offset, src)
static void encode_message_body(enum GNUNET_MESSENGER_MessageKind kind, const struct GNUNET_MESSENGER_MessageBody *body, uint16_t length, char *buffer, uint16_t offset)
#define encode_step_signature(dst, offset, src, length)
struct GNUNET_HashCode previous
The hash of the previous message from the senders perspective.
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.

References GNUNET_MESSENGER_Message::body, encode_message_body(), encode_step, encode_step_signature, GNUNET_assert, GNUNET_htobe32, GNUNET_YES, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_MessageHeader::previous, GNUNET_MESSENGER_MessageHeader::sender_id, GNUNET_MESSENGER_MessageHeader::signature, and GNUNET_MESSENGER_MessageHeader::timestamp.

Referenced by GNUNET_MESSENGER_send_message(), iterate_save_messages(), notify_srv_handle_message(), and pack_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decode_message()

int decode_message ( struct GNUNET_MESSENGER_Message message,
uint16_t  length,
const char *  buffer,
int  include_header,
uint16_t *  padding 
)

Decodes a message from a given buffer of a maximal length in bytes.

If the buffer is too small for a message of its decoded kind the function fails with resulting GNUNET_NO after decoding only the messages header.

On success the function returns GNUNET_YES.

Parameters
[out]messageMessage
[in]lengthMaximal length to decode
[in]bufferBuffer
[in]include_headerFlag to include header
[out]paddingPadding
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 681 of file messenger_api_message.c.

686 {
687  GNUNET_assert (
688  (message) &&
689  (buffer) &&
691  include_header))
692  );
693 
694  uint16_t offset = 0;
695 
696  if (GNUNET_YES == include_header)
697  {
699  &(message->header.signature), buffer, length - offset
700  );
701 
702  if (result < 0)
703  return GNUNET_NO;
704  else
705  offset += result;
706  }
707 
708  const uint16_t count = length - offset;
709 
711  include_header))
712  return GNUNET_NO;
713 
714  kind_t kind;
715 
716  if (GNUNET_YES == include_header)
717  {
718  decode_step (buffer, offset, &(message->header.timestamp));
719  decode_step (buffer, offset, &(message->header.sender_id));
720  decode_step (buffer, offset, &(message->header.previous));
721  }
722 
723  decode_step (buffer, offset, &kind);
724 
726  kind);
727 
728  if (count < get_message_kind_size (message->header.kind, include_header))
729  return GNUNET_NO;
730 
731  const uint16_t result = decode_message_body (&(message->header.kind),
732  &(message->body), length, buffer,
733  offset);
734 
735  if (padding)
736  *padding = result;
737 
738  return GNUNET_YES;
739 }
static int result
Global testing status.
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.
Definition: identity_api.c:988
#define GNUNET_be32toh(x)
GNUNET_MESSENGER_MessageKind
Enum for the different supported kinds of messages.
@ GNUNET_MESSENGER_KIND_UNKNOWN
The unknown kind.
#define decode_step(src, offset, dst)
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)

References GNUNET_MESSENGER_Message::body, decode_message_body(), decode_step, get_message_kind_size(), GNUNET_assert, GNUNET_be32toh, GNUNET_IDENTITY_read_signature_from_buffer(), GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_NO, GNUNET_YES, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_MessageHeader::previous, result, GNUNET_MESSENGER_MessageHeader::sender_id, GNUNET_MESSENGER_MessageHeader::signature, and GNUNET_MESSENGER_MessageHeader::timestamp.

Referenced by check_recv_message(), check_send_message(), check_tunnel_message(), get_store_message(), handle_recv_message(), handle_send_message(), and handle_tunnel_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_message()

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.

Parameters
[in]messageMessage
[in]lengthLength of buffer
[in]bufferBuffer
[out]hashHash

Definition at line 784 of file messenger_api_message.c.

788 {
789  GNUNET_assert ((message) && (buffer) && (hash));
790 
791  const ssize_t offset = GNUNET_IDENTITY_signature_get_length (
792  &(message->header.signature)
793  );
794 
795  GNUNET_CRYPTO_hash (buffer + offset, length - offset, hash);
796 }
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

References GNUNET_assert, GNUNET_CRYPTO_hash(), GNUNET_IDENTITY_signature_get_length(), GNUNET_MESSENGER_Message::header, and GNUNET_MESSENGER_MessageHeader::signature.

Referenced by check_tunnel_message(), get_store_message(), handle_tunnel_message(), and pack_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sign_message()

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.

Parameters
[in,out]messageMessage
[in]lengthLength of buffer
[out]bufferBuffer
[in]hashHash of message
[in]egoEGO

Definition at line 800 of file messenger_api_message.c.

805 {
806  GNUNET_assert ((message) && (buffer) && (hash) && (ego));
807 
808  struct GNUNET_MESSENGER_MessageSignature signature;
809 
811  signature.purpose.size = htonl (sizeof(signature));
812 
813  GNUNET_memcpy (&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
814  GNUNET_IDENTITY_sign (&(ego->priv), &signature, &(message->header.signature));
815 
816  uint16_t offset = 0;
817  encode_step_signature (buffer, offset, &(message->header.signature), length);
818 }
#define GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE
Signature of a chat message.
#define GNUNET_IDENTITY_sign(priv, ps, sig)
Sign a given block with GNUNET_IDENTITY_PrivateKey.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
A 512-bit hashcode.
struct GNUNET_IDENTITY_PrivateKey priv
struct GNUNET_CRYPTO_EccSignaturePurpose purpose

References encode_step_signature, GNUNET_assert, GNUNET_IDENTITY_sign, GNUNET_memcpy, GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE, GNUNET_MESSENGER_MessageSignature::hash, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_Ego::priv, GNUNET_CRYPTO_EccSignaturePurpose::purpose, GNUNET_MESSENGER_MessageSignature::purpose, GNUNET_MESSENGER_MessageHeader::signature, and GNUNET_CRYPTO_EccSignaturePurpose::size.

Referenced by pack_message().

Here is the caller graph for this function:

◆ verify_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.

The function returns GNUNET_OK if the signature was valid, otherwise GNUNET_SYSERR.

Parameters
[in]messageMessage
[in]hashHash of message
[in]keyPublic key of EGO
Returns
GNUNET_OK on success, otherwise GNUNET_SYSERR

Definition at line 822 of file messenger_api_message.c.

825 {
826  GNUNET_assert ((message) && (hash) && (key));
827 
828  if (ntohl (key->type) != ntohl (message->header.signature.type))
829  return GNUNET_SYSERR;
830 
831  struct GNUNET_MESSENGER_MessageSignature signature;
832 
834  signature.purpose.size = htonl (sizeof(signature));
835 
836  GNUNET_memcpy (&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
837 
840  &(message->header.signature), key);
841 }
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_IDENTITY_signature_verify(purp, ps, sig, pub)
Verify a given signature with GNUNET_IDENTITY_PublicKey.
@ GNUNET_SYSERR
uint32_t type
Type of signature.

References GNUNET_assert, GNUNET_IDENTITY_signature_verify, GNUNET_memcpy, GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE, GNUNET_SYSERR, GNUNET_MESSENGER_MessageSignature::hash, GNUNET_MESSENGER_Message::header, key, GNUNET_CRYPTO_EccSignaturePurpose::purpose, GNUNET_MESSENGER_MessageSignature::purpose, GNUNET_MESSENGER_MessageHeader::signature, GNUNET_CRYPTO_EccSignaturePurpose::size, and GNUNET_IDENTITY_Signature::type.

Referenced by verify_member_session_as_sender().

Here is the caller graph for this function:

◆ encrypt_message()

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 encrypted message.

The function returns GNUNET_YES if the operation succeeded, otherwise GNUNET_NO.

Parameters
[in,out]messageMessage
[in]keyPublic key of EGO
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 845 of file messenger_api_message.c.

847 {
848  GNUNET_assert ((message) && (key));
849 
850  struct GNUNET_MESSENGER_ShortMessage shortened;
851 
852  fold_short_message (message, &shortened);
853 
854  const uint16_t length = get_short_message_size (&shortened, GNUNET_YES);
855  const uint16_t padded_length = calc_padded_length (
857  );
858 
860  message->body.privacy.data = GNUNET_malloc (padded_length);
861  message->body.privacy.length = padded_length;
862 
863  const uint16_t encoded_length = (
865  );
866 
867  encode_short_message (&shortened, encoded_length, message->body.privacy.data);
868 
870  encoded_length,
871  key,
872  message->body.privacy.data,
873  padded_length))
874  {
875  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Encrypting message failed!\n");
876 
877  unfold_short_message (&shortened, message);
878  return GNUNET_NO;
879  }
880 
881  destroy_message_body (shortened.kind, &(shortened.body));
882  return GNUNET_YES;
883 }
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_encrypt(const void *pt, size_t pt_size, const struct GNUNET_IDENTITY_PublicKey *pub, void *ct_buf, size_t ct_size)
Encrypt a block with GNUNET_IDENTITY_PublicKey and derives a GNUNET_CRYPTO_EcdhePublicKey which is re...
#define GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_ERROR_TYPE_WARNING
static uint16_t get_short_message_size(const struct GNUNET_MESSENGER_ShortMessage *message, int include_body)
static void fold_short_message(const struct GNUNET_MESSENGER_Message *message, struct GNUNET_MESSENGER_ShortMessage *shortened)
static uint16_t calc_padded_length(uint16_t length)
static void encode_short_message(const struct GNUNET_MESSENGER_ShortMessage *message, uint16_t length, char *buffer)
static void unfold_short_message(struct GNUNET_MESSENGER_ShortMessage *shortened, struct GNUNET_MESSENGER_Message *message)

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_ShortMessage::body, calc_padded_length(), GNUNET_MESSENGER_MessagePrivate::data, destroy_message_body(), encode_short_message(), fold_short_message(), get_short_message_size(), GNUNET_assert, GNUNET_ERROR_TYPE_WARNING, GNUNET_IDENTITY_encrypt(), GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES, GNUNET_log, GNUNET_malloc, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_NO, GNUNET_OK, GNUNET_YES, GNUNET_MESSENGER_Message::header, key, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_ShortMessage::kind, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageBody::privacy, and unfold_short_message().

Referenced by handle_send_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decrypt_message()

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 encrypted message.

The function returns GNUNET_YES if the operation succeeded, otherwise GNUNET_NO.

Parameters
[in,out]messageMessage
[in]keyPrivate key of EGO
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 887 of file messenger_api_message.c.

889 {
890  GNUNET_assert ((message) && (key));
891 
892  const uint16_t padded_length = message->body.privacy.length;
893 
894  if (padded_length < GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES)
895  {
897  "Message length too short to decrypt!\n");
898 
899  return GNUNET_NO;
900  }
901 
902  const uint16_t encoded_length = (
904  );
905 
907  padded_length,
908  key,
909  message->body.privacy.data,
910  encoded_length))
911  {
912  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Decrypting message failed!\n");
913 
914  return GNUNET_NO;
915  }
916 
917  struct GNUNET_MESSENGER_ShortMessage shortened;
918 
919  if (GNUNET_YES != decode_short_message (&shortened,
920  encoded_length,
921  message->body.privacy.data))
922  {
924  "Decoding decrypted message failed!\n");
925 
926  return GNUNET_NO;
927  }
928 
929  unfold_short_message (&shortened, message);
930  return GNUNET_YES;
931 }
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_decrypt(const void *ct_buf, size_t ct_size, const struct GNUNET_IDENTITY_PrivateKey *priv, void *pt, size_t pt_size)
Decrypt a given block with GNUNET_IDENTITY_PrivateKey and a given GNUNET_CRYPTO_EcdhePublicKey using ...
static int decode_short_message(struct GNUNET_MESSENGER_ShortMessage *message, uint16_t length, const char *buffer)

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_MessagePrivate::data, decode_short_message(), GNUNET_assert, GNUNET_ERROR_TYPE_WARNING, GNUNET_IDENTITY_decrypt(), GNUNET_IDENTITY_ENCRYPT_OVERHEAD_BYTES, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_YES, key, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageBody::privacy, and unfold_short_message().

Referenced by notify_srv_handle_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pack_message()

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_PACK_MODE_ENVELOPE.

Independent of the mode the message will be hashed if hash is not NULL and it will be signed if the ego is not NULL.

Parameters
[out]messageMessage
[out]hashHash of message
[in]egoEGO to sign
[in]modeMode of packing
Returns
Envelope or NULL

Definition at line 935 of file messenger_api_message.c.

939 {
940  GNUNET_assert (message);
941 
942  if (ego)
943  message->header.signature.type = ego->priv.type;
944 
946  "Packing message kind=%u and sender: %s\n",
947  message->header.kind, GNUNET_sh2s (&(message->header.sender_id)));
948 
949  struct GNUNET_MessageHeader *header;
950 
951  const uint16_t length = get_message_size (message, GNUNET_YES);
952  const uint16_t padded_length = calc_padded_length (length);
953 
954  struct GNUNET_MQ_Envelope *env;
955  char *buffer;
956 
958  {
959  env = GNUNET_MQ_msg_extra (header, padded_length,
961 
962  buffer = (char*) &(header[1]);
963  }
964  else
965  {
966  env = NULL;
967 
968  buffer = GNUNET_malloc (padded_length);
969  }
970 
971  encode_message (message, padded_length, buffer, GNUNET_YES);
972 
973  if (hash)
974  {
975  hash_message (message, length, buffer, hash);
976 
977  if (ego)
978  sign_message (message, length, buffer, hash, ego);
979  }
980 
982  GNUNET_free (buffer);
983 
984  return env;
985 }
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
const char * GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc)
Convert a short hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
#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:63
#define GNUNET_MESSAGE_TYPE_CADET_CLI
Traffic (net-cat style) used by the Command Line Interface.
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.
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.
void encode_message(const struct GNUNET_MESSENGER_Message *message, uint16_t length, char *buffer, int include_header)
Encodes a given message into a buffer of a maximal length in bytes.
uint16_t get_message_size(const struct GNUNET_MESSENGER_Message *message, int include_header)
Returns the exact size in bytes to encode a given message.
#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE
uint32_t type
Type of public key.
Header for all communications.

References calc_padded_length(), encode_message(), env, get_message_size(), GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_MESSAGE_TYPE_CADET_CLI, GNUNET_MESSENGER_PACK_MODE_ENVELOPE, GNUNET_MQ_msg_extra, GNUNET_sh2s(), GNUNET_YES, hash_message(), GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, mode, GNUNET_MESSENGER_Ego::priv, GNUNET_MESSENGER_MessageHeader::sender_id, sign_message(), GNUNET_MESSENGER_MessageHeader::signature, GNUNET_IDENTITY_PrivateKey::type, and GNUNET_IDENTITY_Signature::type.

Referenced by forward_tunnel_message(), iterate_send_room_message(), and pack_srv_room_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ filter_message_sending()

int filter_message_sending ( const struct GNUNET_MESSENGER_Message message)

Returns if a specific kind of message should be sent by a client.

The function returns GNUNET_YES or GNUNET_NO for recommendations and GNUNET_SYSERR for specific kinds of messages which should not be sent manually at all.

Parameters
[in]messageMessage

Definition at line 989 of file messenger_api_message.c.

990 {
991  switch (message->header.kind)
992  {
994  return GNUNET_SYSERR; // Reserved for connection handling only!
996  return GNUNET_NO; // Use #GNUNET_MESSENGER_enter_room(...) instead!
998  return GNUNET_NO; // Use #GNUNET_MESSENGER_close_room(...) instead!
1000  return GNUNET_YES;
1002  return GNUNET_NO; // Use #GNUNET_MESSENGER_update(...) instead!
1004  return GNUNET_NO; // Use #GNUNET_MESSENGER_open_room(...) instead!
1006  return GNUNET_SYSERR; // Reserved for member id handling only!
1008  return GNUNET_SYSERR; // Reserved for connection handling only!
1010  return GNUNET_YES;
1012  return GNUNET_YES;
1014  return GNUNET_YES;
1016  return GNUNET_YES;
1018  return GNUNET_YES;
1020  return GNUNET_NO; // Use #GNUNET_MESSENGER_send_message(...) with a contact instead!
1022  return GNUNET_YES;
1023  default:
1024  return GNUNET_SYSERR;
1025  }
1026 }
@ GNUNET_MESSENGER_KIND_INFO
The info kind.
@ GNUNET_MESSENGER_KIND_MISS
The miss kind.
@ GNUNET_MESSENGER_KIND_INVITE
The invite kind.
@ GNUNET_MESSENGER_KIND_REQUEST
The request kind.
@ GNUNET_MESSENGER_KIND_PEER
The peer kind.
@ GNUNET_MESSENGER_KIND_DELETE
The delete kind.
@ GNUNET_MESSENGER_KIND_MERGE
The merge kind.

Referenced by check_send_message(), and GNUNET_MESSENGER_send_message().

Here is the caller graph for this function: