GNUnet  0.11.x
Data Structures | Macros | Typedefs | Functions
messenger_api_message.c File Reference

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

#include "messenger_api_message.h"
Include dependency graph for messenger_api_message.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_MESSENGER_MessageSignature
 
struct  GNUNET_MESSENGER_ShortMessage
 

Macros

#define member_size(type, member)   sizeof(((type*) NULL)->member)
 
#define max(x, y)   (x > y? x : y)
 
#define min(x, y)   (x < y? x : y)
 
#define encode_step_ext(dst, offset, src, size)
 
#define encode_step(dst, offset, src)
 
#define encode_step_key(dst, offset, src, length)
 
#define encode_step_signature(dst, offset, src, length)
 
#define decode_step_ext(src, offset, dst, size)
 
#define decode_step(src, offset, dst)
 
#define decode_step_malloc(src, offset, dst, size, zero)
 
#define decode_step_key(src, offset, dst, length)
 

Typedefs

typedef uint32_t kind_t
 

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...
 
static void destroy_message_body (enum GNUNET_MESSENGER_MessageKind kind, struct GNUNET_MESSENGER_MessageBody *body)
 
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...
 
static void fold_short_message (const struct GNUNET_MESSENGER_Message *message, struct GNUNET_MESSENGER_ShortMessage *shortened)
 
static void unfold_short_message (struct GNUNET_MESSENGER_ShortMessage *shortened, struct GNUNET_MESSENGER_Message *message)
 
static uint16_t get_message_body_kind_size (enum GNUNET_MESSENGER_MessageKind kind)
 
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. More...
 
static uint16_t get_message_body_size (enum GNUNET_MESSENGER_MessageKind kind, const struct GNUNET_MESSENGER_MessageBody *body)
 
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. More...
 
static uint16_t get_short_message_size (const struct GNUNET_MESSENGER_ShortMessage *message, int include_body)
 
static uint16_t calc_usual_padding ()
 
static uint16_t calc_padded_length (uint16_t length)
 
static void encode_message_body (enum GNUNET_MESSENGER_MessageKind kind, const struct GNUNET_MESSENGER_MessageBody *body, uint16_t length, char *buffer, uint16_t offset)
 
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. More...
 
static void encode_short_message (const struct GNUNET_MESSENGER_ShortMessage *message, uint16_t length, char *buffer)
 
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)
 
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. More...
 
static int decode_short_message (struct GNUNET_MESSENGER_ShortMessage *message, uint16_t length, const char *buffer)
 
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.c.

Macro Definition Documentation

◆ member_size

#define member_size (   type,
  member 
)    sizeof(((type*) NULL)->member)

Definition at line 170 of file messenger_api_message.c.

Referenced by get_message_body_kind_size(), and get_message_kind_size().

◆ max

#define max (   x,
 
)    (x > y? x : y)

◆ min

#define min (   x,
 
)    (x < y? x : y)

◆ encode_step_ext

#define encode_step_ext (   dst,
  offset,
  src,
  size 
)
Value:
do { \
GNUNET_memcpy(dst + offset, src, size); \
offset += size; \
} while (0)
static unsigned int size
Size of the "table".
Definition: peer.c:67

Definition at line 343 of file messenger_api_message.c.

Referenced by encode_message_body().

◆ encode_step

#define encode_step (   dst,
  offset,
  src 
)
Value:
do { \
encode_step_ext(dst, offset, src, sizeof(*src)); \
} while (0)

Definition at line 348 of file messenger_api_message.c.

Referenced by encode_message(), encode_message_body(), and encode_short_message().

◆ encode_step_key

#define encode_step_key (   dst,
  offset,
  src,
  length 
)
Value:
do { \
src, dst + offset, length - offset \
); \
if (result < 0) \
GNUNET_break (0); \
else \
offset += result; \
} while (0)
ssize_t GNUNET_IDENTITY_write_key_to_buffer(const struct GNUNET_IDENTITY_PublicKey *key, void *buffer, size_t len)
Writes a GNUNET_IDENTITY_PublicKey to a compact buffer.
static int result
Global testing status.

Definition at line 352 of file messenger_api_message.c.

Referenced by encode_message_body().

◆ encode_step_signature

#define encode_step_signature (   dst,
  offset,
  src,
  length 
)
Value:
do { \
src, dst + offset, length - offset \
); \
if (result < 0) \
GNUNET_break (0); \
else \
offset += result; \
} while (0)
ssize_t GNUNET_IDENTITY_write_signature_to_buffer(const struct GNUNET_IDENTITY_Signature *sig, void *buffer, size_t len)
Writes a GNUNET_IDENTITY_Signature to a compact buffer.
static int result
Global testing status.

Definition at line 362 of file messenger_api_message.c.

Referenced by encode_message(), and sign_message().

◆ decode_step_ext

#define decode_step_ext (   src,
  offset,
  dst,
  size 
)
Value:
do { \
GNUNET_memcpy(dst, src + offset, size); \
offset += size; \
} while (0)
static unsigned int size
Size of the "table".
Definition: peer.c:67

Definition at line 486 of file messenger_api_message.c.

Referenced by decode_message_body().

◆ decode_step

#define decode_step (   src,
  offset,
  dst 
)
Value:
do { \
decode_step_ext(src, offset, dst, sizeof(*dst)); \
} while (0)

Definition at line 491 of file messenger_api_message.c.

Referenced by decode_message(), decode_message_body(), and decode_short_message().

◆ decode_step_malloc

#define decode_step_malloc (   src,
  offset,
  dst,
  size,
  zero 
)
Value:
do { \
dst = GNUNET_malloc(size + zero); \
if (zero) dst[size] = 0; \
decode_step_ext(src, offset, dst, size); \
} while (0)
static unsigned int size
Size of the "table".
Definition: peer.c:67
static const struct GNUNET_IDENTITY_PrivateKey zero
Public key of all zeros.
#define GNUNET_malloc(size)
Wrapper around malloc.

Definition at line 495 of file messenger_api_message.c.

Referenced by decode_message_body().

◆ decode_step_key

#define decode_step_key (   src,
  offset,
  dst,
  length 
)
Value:
do { \
dst, src + offset, length - offset \
); \
if (result < 0) \
GNUNET_break(0); \
else \
offset += result; \
} while (0)
ssize_t GNUNET_IDENTITY_read_key_from_buffer(struct GNUNET_IDENTITY_PublicKey *key, const void *buffer, size_t len)
Reads a GNUNET_IDENTITY_PublicKey from a compact buffer.
Definition: identity_api.c:994
static int result
Global testing status.

Definition at line 501 of file messenger_api_message.c.

Referenced by decode_message_body().

Typedef Documentation

◆ kind_t

typedef uint32_t kind_t

Definition at line 218 of file messenger_api_message.c.

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 41 of file messenger_api_message.c.

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::private, 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(), get_store_message(), and run().

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.private.length = 0;
60  message->body.private.data = NULL;
61  break;
62  default:
63  break;
64  }
65 
66  return message;
67 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
char * uri
The uri of the encrypted file.
struct GNUNET_MESSENGER_MessageText text
#define GNUNET_new(type)
Allocate a struct or union of the given type.
char * text
The containing text.
struct GNUNET_MESSENGER_MessageBody body
Body.
uint16_t length
The length of the encrypted message.
struct GNUNET_MESSENGER_MessageName name
char * data
The data of the encrypted message.
struct GNUNET_MESSENGER_MessagePrivate private
char * name
The new name which replaces the current senders name.
struct GNUNET_MESSENGER_MessageFile file
enum GNUNET_MESSENGER_MessageKind kind
The kind of the 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 70 of file messenger_api_message.c.

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::private, 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_handle_message(), and send_handle_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.private.data = copy->body.private.length ? GNUNET_malloc(copy->body.private.length) : NULL;
91 
92  if (copy->body.private.data)
93  {
94  GNUNET_memcpy(copy->body.private.data, message->body.private.data, copy->body.private.length);
95  }
96 
97  break;
98  default:
99  break;
100  }
101 
102  return copy;
103 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
char * uri
The uri of the encrypted file.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_MESSENGER_MessageText text
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
char * text
The containing text.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct GNUNET_MESSENGER_MessageBody body
Body.
uint16_t length
The length of the encrypted message.
struct GNUNET_MESSENGER_MessageName name
char * data
The data of the encrypted message.
struct GNUNET_MESSENGER_MessagePrivate private
char * name
The new name which replaces the current senders name.
struct GNUNET_MESSENGER_MessageFile file
#define GNUNET_malloc(size)
Wrapper around malloc.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the caller graph for this function:

◆ destroy_message_body()

static void destroy_message_body ( enum GNUNET_MESSENGER_MessageKind  kind,
struct GNUNET_MESSENGER_MessageBody body 
)
static

Definition at line 106 of file messenger_api_message.c.

References GNUNET_MESSENGER_MessagePrivate::data, GNUNET_MESSENGER_MessageBody::file, GNUNET_free, GNUNET_MESSENGER_KIND_FILE, GNUNET_MESSENGER_KIND_NAME, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_MESSENGER_KIND_TEXT, GNUNET_MESSENGER_MessageName::name, GNUNET_MESSENGER_MessageBody::name, GNUNET_MESSENGER_MessageBody::private, GNUNET_MESSENGER_MessageText::text, GNUNET_MESSENGER_MessageBody::text, and GNUNET_MESSENGER_MessageFile::uri.

Referenced by destroy_message(), encrypt_message(), and unfold_short_message().

107 {
108  switch (kind)
109  {
111  GNUNET_free(body->name.name);
112  break;
114  GNUNET_free(body->text.text);
115  break;
117  GNUNET_free(body->file.uri);
118  break;
120  GNUNET_free(body->private.data);
121  break;
122  default:
123  break;
124  }
125 }
char * uri
The uri of the encrypted file.
struct GNUNET_MESSENGER_MessageText text
char * text
The containing text.
struct GNUNET_MESSENGER_MessageName name
char * data
The data of the encrypted message.
struct GNUNET_MESSENGER_MessagePrivate private
char * name
The new name which replaces the current senders name.
struct GNUNET_MESSENGER_MessageFile file
#define GNUNET_free(ptr)
Wrapper around free.
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

Definition at line 128 of file messenger_api_message.c.

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_handle_message(), send_tunnel_message(), set_handle_ego(), and update_room_message().

129 {
130  GNUNET_assert(message);
131 
132  destroy_message_body (message->header.kind, &(message->body));
133 
134  GNUNET_free(message);
135 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_MESSENGER_MessageBody body
Body.
static void destroy_message_body(enum GNUNET_MESSENGER_MessageKind kind, struct GNUNET_MESSENGER_MessageBody *body)
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the 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 138 of file messenger_api_message.c.

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_room_message(), and update_member_session_history().

139 {
140  GNUNET_assert(message);
141 
142  if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) ||
143  (GNUNET_MESSENGER_KIND_LEAVE == message->header.kind) ||
144  (GNUNET_MESSENGER_KIND_NAME == message->header.kind) ||
145  (GNUNET_MESSENGER_KIND_KEY == message->header.kind) ||
146  (GNUNET_MESSENGER_KIND_ID == message->header.kind))
147  return GNUNET_YES;
148  else
149  return GNUNET_NO;
150 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the caller graph for this function:

◆ fold_short_message()

static void fold_short_message ( const struct GNUNET_MESSENGER_Message message,
struct GNUNET_MESSENGER_ShortMessage shortened 
)
static

Definition at line 153 of file messenger_api_message.c.

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_ShortMessage::body, GNUNET_memcpy, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, and GNUNET_MESSENGER_ShortMessage::kind.

Referenced by encrypt_message().

154 {
155  shortened->kind = message->header.kind;
156 
157  GNUNET_memcpy(&(shortened->body), &(message->body), sizeof(struct GNUNET_MESSENGER_MessageBody));
158 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
enum GNUNET_MESSENGER_MessageKind kind
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
The unified body of a GNUNET_MESSENGER_Message.
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_MESSENGER_MessageBody body
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the caller graph for this function:

◆ unfold_short_message()

static void unfold_short_message ( struct GNUNET_MESSENGER_ShortMessage shortened,
struct GNUNET_MESSENGER_Message message 
)
static

Definition at line 161 of file messenger_api_message.c.

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

Referenced by decrypt_message(), and encrypt_message().

162 {
163  destroy_message_body (message->header.kind, &(message->body));
164 
165  message->header.kind = shortened->kind;
166 
167  GNUNET_memcpy(&(message->body), &(shortened->body), sizeof(struct GNUNET_MESSENGER_MessageBody));
168 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
enum GNUNET_MESSENGER_MessageKind kind
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
The unified body of a GNUNET_MESSENGER_Message.
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_MESSENGER_MessageBody body
static void destroy_message_body(enum GNUNET_MESSENGER_MessageKind kind, struct GNUNET_MESSENGER_MessageBody *body)
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_message_body_kind_size()

static uint16_t get_message_body_kind_size ( enum GNUNET_MESSENGER_MessageKind  kind)
static

Definition at line 173 of file messenger_api_message.c.

References GNUNET_MESSENGER_MessageInvite::door, GNUNET_MESSENGER_MessageBody::file, GNUNET_MESSENGER_KIND_FILE, GNUNET_MESSENGER_KIND_ID, GNUNET_MESSENGER_KIND_INFO, GNUNET_MESSENGER_KIND_INVITE, GNUNET_MESSENGER_KIND_MERGE, GNUNET_MESSENGER_KIND_MISS, GNUNET_MESSENGER_KIND_PEER, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_MESSENGER_KIND_REQUEST, GNUNET_MESSENGER_KIND_TEXT, GNUNET_MESSENGER_MessageRequest::hash, GNUNET_MESSENGER_MessageFile::hash, GNUNET_MESSENGER_MessageId::id, GNUNET_MESSENGER_MessageBody::id, GNUNET_MESSENGER_MessageBody::info, GNUNET_MESSENGER_MessageBody::invite, GNUNET_MESSENGER_MessageInvite::key, GNUNET_MESSENGER_MessageFile::key, GNUNET_MESSENGER_MessagePrivate::key, member_size, GNUNET_MESSENGER_MessageBody::merge, GNUNET_MESSENGER_MessageInfo::messenger_version, GNUNET_MESSENGER_MessageBody::miss, GNUNET_MESSENGER_MessageFile::name, GNUNET_MESSENGER_MessagePeer::peer, GNUNET_MESSENGER_MessageMiss::peer, GNUNET_MESSENGER_MessageBody::peer, GNUNET_MESSENGER_MessageMerge::previous, GNUNET_MESSENGER_MessageBody::private, and GNUNET_MESSENGER_MessageBody::request.

Referenced by get_message_kind_size(), and get_short_message_size().

174 {
175  uint16_t length = 0;
176 
177  switch (kind)
178  {
181  break;
184  break;
186  length += member_size(struct GNUNET_MESSENGER_Message, body.id.id);
187  break;
190  break;
193  break;
196  break;
200  break;
202  break;
204  length += member_size(struct GNUNET_MESSENGER_Message, body.file.key);
207  break;
210  break;
211  default:
212  break;
213  }
214 
215  return length;
216 }
struct GNUNET_PeerIdentity peer
The peer identity of the sender opening a room.
struct GNUNET_PeerIdentity door
The peer identity of an open door to a room.
struct GNUNET_PeerIdentity peer
The peer identity of a disconnected door to a room.
struct GNUNET_ShortHashCode id
The new id which will replace the senders id in a room.
struct GNUNET_HashCode hash
The hash of the requested message.
uint32_t messenger_version
The version of GNUnet Messenger API.
char name[NAME_MAX]
The name of the original file.
struct GNUNET_MESSENGER_MessageRequest request
struct GNUNET_MESSENGER_MessageInvite invite
struct GNUNET_HashCode previous
The hash of a second previous message.
struct GNUNET_MESSENGER_MessageMiss miss
struct GNUNET_CRYPTO_SymmetricSessionKey key
The symmetric key to decrypt the file.
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_MESSENGER_MessageMerge merge
struct GNUNET_HashCode hash
The hash of the original file.
struct GNUNET_MESSENGER_MessageInfo info
struct GNUNET_CRYPTO_EcdhePublicKey key
The ECDH key to decrypt the message.
struct GNUNET_MESSENGER_MessagePeer peer
struct GNUNET_MESSENGER_MessagePrivate private
struct GNUNET_HashCode key
The hash identifying the port of the room.
#define member_size(type, member)
struct GNUNET_MESSENGER_MessageFile file
struct GNUNET_MESSENGER_MessageId id
Here is the caller graph for this function:

◆ get_message_kind_size()

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.

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

Definition at line 221 of file messenger_api_message.c.

References get_message_body_kind_size(), 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().

222 {
223  uint16_t length = 0;
224 
228  length += sizeof(kind_t);
229 
230  return length + get_message_body_kind_size (kind);
231 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the message.
static uint16_t get_message_body_kind_size(enum GNUNET_MESSENGER_MessageKind kind)
uint32_t kind_t
struct GNUNET_HashCode previous
The hash of the previous message from the senders perspective.
#define member_size(type, member)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_message_body_size()

static uint16_t get_message_body_size ( enum GNUNET_MESSENGER_MessageKind  kind,
const struct GNUNET_MESSENGER_MessageBody body 
)
static

Definition at line 234 of file messenger_api_message.c.

References GNUNET_MESSENGER_MessageBody::file, GNUNET_IDENTITY_key_get_length(), GNUNET_MESSENGER_KIND_FILE, GNUNET_MESSENGER_KIND_INFO, GNUNET_MESSENGER_KIND_JOIN, GNUNET_MESSENGER_KIND_KEY, GNUNET_MESSENGER_KIND_NAME, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_MESSENGER_KIND_TEXT, GNUNET_MESSENGER_MessageInfo::host_key, GNUNET_MESSENGER_MessageBody::info, GNUNET_MESSENGER_MessageBody::join, GNUNET_MESSENGER_MessageJoin::key, GNUNET_MESSENGER_MessageKey::key, GNUNET_MESSENGER_MessageBody::key, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageName::name, GNUNET_MESSENGER_MessageBody::name, GNUNET_MESSENGER_MessageBody::private, GNUNET_MESSENGER_MessageText::text, GNUNET_MESSENGER_MessageBody::text, and GNUNET_MESSENGER_MessageFile::uri.

Referenced by get_message_size(), and get_short_message_size().

235 {
236  uint16_t length = 0;
237 
238  switch (kind)
239  {
241  length += GNUNET_IDENTITY_key_get_length(&(body->info.host_key));
242  break;
244  length += GNUNET_IDENTITY_key_get_length(&(body->join.key));
245  break;
247  length += (body->name.name ? strlen (body->name.name) : 0);
248  break;
250  length += GNUNET_IDENTITY_key_get_length(&(body->key.key));
251  break;
253  length += strlen (body->text.text);
254  break;
256  length += strlen (body->file.uri);
257  break;
259  length += body->private.length;
260  break;
261  default:
262  break;
263  }
264 
265  return length;
266 }
char * uri
The uri of the encrypted file.
struct GNUNET_IDENTITY_PublicKey host_key
The senders key to verify its signatures.
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
struct GNUNET_MESSENGER_MessageText text
struct GNUNET_MESSENGER_MessageKey key
char * text
The containing text.
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.
uint16_t length
The length of the encrypted message.
struct GNUNET_MESSENGER_MessageName name
struct GNUNET_MESSENGER_MessageInfo info
struct GNUNET_MESSENGER_MessagePrivate private
char * name
The new name which replaces the current senders name.
struct GNUNET_MESSENGER_MessageJoin join
struct GNUNET_MESSENGER_MessageFile file
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_signature 
)

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

Parameters
[in]messageMessage
[in]encode_signatureFlag to include signature
Returns
Size to encode

Definition at line 269 of file messenger_api_message.c.

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_handle_message(), and pack_message().

271 {
272  GNUNET_assert(message);
273 
274  uint16_t length = 0;
275 
276  if (GNUNET_YES == include_signature)
278 
279  length += get_message_kind_size (message->header.kind);
280  length += get_message_body_size (message->header.kind, &(message->body));
281 
282  return length;
283 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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)
Returns the minimal size in bytes to encode a message of a specific kind.
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
ssize_t GNUNET_IDENTITY_signature_get_length(const struct GNUNET_IDENTITY_Signature *sig)
Get the compacted length of a GNUNET_IDENTITY_Signature.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_short_message_size()

static uint16_t get_short_message_size ( const struct GNUNET_MESSENGER_ShortMessage message,
int  include_body 
)
static

Definition at line 286 of file messenger_api_message.c.

References GNUNET_MESSENGER_ShortMessage::body, get_message_body_kind_size(), get_message_body_size(), GNUNET_YES, and GNUNET_MESSENGER_ShortMessage::kind.

Referenced by decode_short_message(), and encrypt_message().

287 {
288  const uint16_t minimum_size = sizeof(struct GNUNET_HashCode) + sizeof(kind_t);
289 
290  if (message)
291  return minimum_size + get_message_body_kind_size (message->kind)
292  + (include_body == GNUNET_YES? get_message_body_size (message->kind, &(message->body)) : 0);
293  else
294  return minimum_size;
295 }
enum GNUNET_MESSENGER_MessageKind kind
static uint16_t get_message_body_size(enum GNUNET_MESSENGER_MessageKind kind, const struct GNUNET_MESSENGER_MessageBody *body)
static uint16_t get_message_body_kind_size(enum GNUNET_MESSENGER_MessageKind kind)
A 512-bit hashcode.
uint32_t kind_t
struct GNUNET_MESSENGER_MessageBody body
Here is the call graph for this function:
Here is the caller graph for this function:

◆ calc_usual_padding()

static uint16_t calc_usual_padding ( )
static

Definition at line 298 of file messenger_api_message.c.

References get_message_kind_size(), GNUNET_MESSENGER_KIND_MAX, and GNUNET_MESSENGER_PADDING_MIN.

Referenced by calc_padded_length().

299 {
300  uint16_t padding = 0;
301  uint16_t kind_size;
302 
303  for (int i = 0; i <= GNUNET_MESSENGER_KIND_MAX; i++) {
305 
306  if (kind_size > padding)
307  padding = kind_size;
308  }
309 
310  return padding + GNUNET_MESSENGER_PADDING_MIN;
311 }
#define GNUNET_MESSENGER_PADDING_MIN
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.
#define GNUNET_MESSENGER_KIND_MAX
GNUNET_MESSENGER_MessageKind
Enum for the different supported kinds of messages.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ calc_padded_length()

static uint16_t calc_padded_length ( uint16_t  length)
static

Definition at line 316 of file messenger_api_message.c.

References calc_usual_padding(), GNUNET_MESSENGER_MAX_MESSAGE_SIZE, GNUNET_MESSENGER_PADDING_LEVEL0, GNUNET_MESSENGER_PADDING_LEVEL1, GNUNET_MESSENGER_PADDING_LEVEL2, GNUNET_MESSENGER_PADDING_MIN, and max.

Referenced by encrypt_message(), and pack_message().

317 {
318  static uint16_t usual_padding = 0;
319 
320  if (!usual_padding)
321  usual_padding = calc_usual_padding();
322 
323  const uint16_t padded_length = max(
325  usual_padding
326  );
327 
328  if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL0)
330 
331  if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL1)
333 
334  if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL2)
336 
338 
339 }
#define GNUNET_MESSENGER_PADDING_MIN
#define max(x, y)
static uint16_t calc_usual_padding()
#define GNUNET_MESSENGER_PADDING_LEVEL2
#define GNUNET_MESSENGER_PADDING_LEVEL1
#define GNUNET_MESSENGER_MAX_MESSAGE_SIZE
#define GNUNET_MESSENGER_PADDING_LEVEL0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_message_body()

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

Definition at line 373 of file messenger_api_message.c.

References GNUNET_MESSENGER_MessagePrivate::data, GNUNET_MESSENGER_MessageInvite::door, encode_step, encode_step_ext, encode_step_key, GNUNET_MESSENGER_MessageBody::file, GNUNET_assert, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_block(), GNUNET_memcpy, GNUNET_MESSENGER_KIND_FILE, GNUNET_MESSENGER_KIND_ID, GNUNET_MESSENGER_KIND_INFO, GNUNET_MESSENGER_KIND_INVITE, GNUNET_MESSENGER_KIND_JOIN, GNUNET_MESSENGER_KIND_KEY, GNUNET_MESSENGER_KIND_MERGE, GNUNET_MESSENGER_KIND_MISS, GNUNET_MESSENGER_KIND_NAME, GNUNET_MESSENGER_KIND_PEER, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_MESSENGER_KIND_REQUEST, GNUNET_MESSENGER_KIND_TEXT, GNUNET_MESSENGER_MessageRequest::hash, GNUNET_MESSENGER_MessageFile::hash, GNUNET_MESSENGER_MessageInfo::host_key, GNUNET_MESSENGER_MessageId::id, GNUNET_MESSENGER_MessageBody::id, GNUNET_MESSENGER_MessageBody::info, GNUNET_MESSENGER_MessageBody::invite, GNUNET_MESSENGER_MessageBody::join, GNUNET_MESSENGER_MessageJoin::key, GNUNET_MESSENGER_MessageKey::key, GNUNET_MESSENGER_MessageInvite::key, GNUNET_MESSENGER_MessageFile::key, GNUNET_MESSENGER_MessagePrivate::key, GNUNET_MESSENGER_MessageBody::key, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageBody::merge, GNUNET_MESSENGER_MessageInfo::messenger_version, min, GNUNET_MESSENGER_MessageBody::miss, GNUNET_MESSENGER_MessageName::name, GNUNET_MESSENGER_MessageFile::name, GNUNET_MESSENGER_MessageBody::name, GNUNET_MESSENGER_MessageMiss::peer, GNUNET_MESSENGER_MessagePeer::peer, GNUNET_MESSENGER_MessageBody::peer, GNUNET_MESSENGER_MessageMerge::previous, GNUNET_MESSENGER_MessageBody::private, GNUNET_MESSENGER_MessageBody::request, GNUNET_MESSENGER_MessageText::text, GNUNET_MESSENGER_MessageBody::text, and GNUNET_MESSENGER_MessageFile::uri.

Referenced by encode_message(), and encode_short_message().

375 {
376  switch (kind)
377  {
379  encode_step_key(buffer, offset, &(body->info.host_key), length);
380  encode_step(buffer, offset, &(body->info.messenger_version));
381  break;
383  encode_step_key(buffer, offset, &(body->join.key), length);
384  break;
386  if (body->name.name)
387  encode_step_ext(buffer, offset, body->name.name, min(length - offset, strlen(body->name.name)));
388  break;
390  encode_step_key(buffer, offset, &(body->key.key), length);
391  break;
393  encode_step(buffer, offset, &(body->peer.peer));
394  break;
396  encode_step(buffer, offset, &(body->id.id));
397  break;
399  encode_step(buffer, offset, &(body->miss.peer));
400  break;
402  encode_step(buffer, offset, &(body->merge.previous));
403  break;
405  encode_step(buffer, offset, &(body->request.hash));
406  break;
408  encode_step(buffer, offset, &(body->invite.door));
409  encode_step(buffer, offset, &(body->invite.key));
410  break;
412  encode_step_ext(buffer, offset, body->text.text, min(length - offset, strlen(body->text.text)));
413  break;
415  encode_step(buffer, offset, &(body->file.key));
416  encode_step(buffer, offset, &(body->file.hash));
417  encode_step_ext(buffer, offset, body->file.name, sizeof(body->file.name));
418  encode_step_ext(buffer, offset, body->file.uri, min(length - offset, strlen(body->file.uri)));
419  break;
421  encode_step(buffer, offset, &(body->private.key));
422  encode_step_ext(buffer, offset, body->private.data, min(length - offset, body->private.length));
423  break;
424  default:
425  break;
426  }
427 
428  if (offset >= length)
429  return;
430 
431  const uint16_t padding = length - offset;
432  const uint16_t used_padding = sizeof(padding) + sizeof(char);
433 
434  GNUNET_assert(padding >= used_padding);
435 
436  buffer[offset++] = '\0';
437 
438  if (padding > used_padding)
439  GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, buffer + offset, padding - used_padding);
440 
441  GNUNET_memcpy(buffer + length - sizeof(padding), &padding, sizeof(padding));
442 }
struct GNUNET_PeerIdentity peer
The peer identity of the sender opening a room.
struct GNUNET_PeerIdentity door
The peer identity of an open door to a room.
struct GNUNET_PeerIdentity peer
The peer identity of a disconnected door to a room.
char * uri
The uri of the encrypted file.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_IDENTITY_PublicKey host_key
The senders key to verify its signatures.
struct GNUNET_ShortHashCode id
The new id which will replace the senders id in a room.
struct GNUNET_MESSENGER_MessageText text
struct GNUNET_HashCode hash
The hash of the requested message.
struct GNUNET_MESSENGER_MessageKey key
uint32_t messenger_version
The version of GNUnet Messenger API.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define encode_step(dst, offset, src)
char name[NAME_MAX]
The name of the original file.
char * text
The containing text.
struct GNUNET_MESSENGER_MessageRequest request
struct GNUNET_IDENTITY_PublicKey key
The senders public key to verify its signatures.
struct GNUNET_MESSENGER_MessageInvite invite
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_MESSENGER_MessageMiss miss
struct GNUNET_CRYPTO_SymmetricSessionKey key
The symmetric key to decrypt the file.
#define encode_step_key(dst, offset, src, length)
uint16_t length
The length of the encrypted message.
struct GNUNET_MESSENGER_MessageMerge merge
#define encode_step_ext(dst, offset, src, size)
struct GNUNET_HashCode hash
The hash of the original file.
struct GNUNET_MESSENGER_MessageName name
struct GNUNET_MESSENGER_MessageInfo info
struct GNUNET_CRYPTO_EcdhePublicKey key
The ECDH key to decrypt the message.
struct GNUNET_MESSENGER_MessagePeer peer
#define min(x, y)
char * data
The data of the encrypted message.
void GNUNET_CRYPTO_random_block(enum GNUNET_CRYPTO_Quality mode, void *buffer, size_t length)
Fill block with a random values.
struct GNUNET_MESSENGER_MessagePrivate private
char * name
The new name which replaces the current senders name.
struct GNUNET_HashCode key
The hash identifying the port of the room.
struct GNUNET_MESSENGER_MessageJoin join
struct GNUNET_MESSENGER_MessageFile file
struct GNUNET_MESSENGER_MessageId id
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
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_signature 
)

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

Parameters
[in]messageMessage
[in]lengthMaximal length to encode
[out]bufferBuffer
[in]encode_signatureFlag to include signature

Definition at line 445 of file messenger_api_message.c.

References GNUNET_MESSENGER_Message::body, encode_message_body(), encode_step, encode_step_signature, GNUNET_assert, 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_handle_message(), and pack_message().

447 {
448  GNUNET_assert((message) && (buffer));
449 
450  uint16_t offset = 0;
451 
452  if (GNUNET_YES == include_signature)
453  encode_step_signature(buffer, offset, &(message->header.signature), length);
454 
455  const kind_t kind = (kind_t) message->header.kind;
456 
457  encode_step(buffer, offset, &(message->header.timestamp));
458  encode_step(buffer, offset, &(message->header.sender_id));
459  encode_step(buffer, offset, &(message->header.previous));
460  encode_step(buffer, offset, &kind);
461 
462  encode_message_body (message->header.kind, &(message->body), length, buffer, offset);
463 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the message.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define encode_step(dst, offset, src)
#define encode_step_signature(dst, offset, src, length)
static void encode_message_body(enum GNUNET_MESSENGER_MessageKind kind, const struct GNUNET_MESSENGER_MessageBody *body, uint16_t length, char *buffer, uint16_t offset)
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
uint32_t kind_t
struct GNUNET_HashCode previous
The hash of the previous message from the senders perspective.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_short_message()

static void encode_short_message ( const struct GNUNET_MESSENGER_ShortMessage message,
uint16_t  length,
char *  buffer 
)
static

Definition at line 466 of file messenger_api_message.c.

References GNUNET_MESSENGER_ShortMessage::body, encode_message_body(), encode_step, GNUNET_CRYPTO_hash(), GNUNET_memcpy, GNUNET_MESSENGER_MessageSignature::hash, and GNUNET_MESSENGER_ShortMessage::kind.

Referenced by encrypt_message().

467 {
468  struct GNUNET_HashCode hash;
469  uint16_t offset = sizeof(hash);
470 
471  const kind_t kind = (kind_t) message->kind;
472 
473  encode_step(buffer, offset, &kind);
474 
475  encode_message_body (message->kind, &(message->body), length, buffer, offset);
476 
478  buffer + sizeof(hash),
479  length - sizeof(hash),
480  &hash
481  );
482 
483  GNUNET_memcpy(buffer, &hash, sizeof(hash));
484 }
enum GNUNET_MESSENGER_MessageKind kind
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#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)
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
A 512-bit hashcode.
uint32_t kind_t
struct GNUNET_MESSENGER_MessageBody body
Here is the call graph for this function:
Here is the caller graph for this function:

◆ decode_message_body()

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 
)
static

Definition at line 512 of file messenger_api_message.c.

References GNUNET_MESSENGER_MessagePrivate::data, decode_step, decode_step_ext, decode_step_key, decode_step_malloc, GNUNET_MESSENGER_MessageInvite::door, GNUNET_MESSENGER_MessageBody::file, GNUNET_memcpy, GNUNET_MESSENGER_KIND_FILE, GNUNET_MESSENGER_KIND_ID, GNUNET_MESSENGER_KIND_INFO, GNUNET_MESSENGER_KIND_INVITE, GNUNET_MESSENGER_KIND_JOIN, GNUNET_MESSENGER_KIND_KEY, GNUNET_MESSENGER_KIND_MERGE, GNUNET_MESSENGER_KIND_MISS, GNUNET_MESSENGER_KIND_NAME, GNUNET_MESSENGER_KIND_PEER, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_MESSENGER_KIND_REQUEST, GNUNET_MESSENGER_KIND_TEXT, GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_MESSENGER_MessageRequest::hash, GNUNET_MESSENGER_MessageFile::hash, GNUNET_MESSENGER_MessageInfo::host_key, GNUNET_MESSENGER_MessageId::id, GNUNET_MESSENGER_MessageBody::id, GNUNET_MESSENGER_MessageBody::info, GNUNET_MESSENGER_MessageBody::invite, GNUNET_MESSENGER_MessageBody::join, GNUNET_MESSENGER_MessageJoin::key, GNUNET_MESSENGER_MessageInvite::key, GNUNET_MESSENGER_MessageKey::key, GNUNET_MESSENGER_MessageFile::key, GNUNET_MESSENGER_MessagePrivate::key, GNUNET_MESSENGER_MessageBody::key, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageBody::merge, GNUNET_MESSENGER_MessageInfo::messenger_version, GNUNET_MESSENGER_MessageBody::miss, GNUNET_MESSENGER_MessageName::name, GNUNET_MESSENGER_MessageFile::name, GNUNET_MESSENGER_MessageBody::name, GNUNET_MESSENGER_MessagePeer::peer, GNUNET_MESSENGER_MessageMiss::peer, GNUNET_MESSENGER_MessageBody::peer, GNUNET_MESSENGER_MessageMerge::previous, GNUNET_MESSENGER_MessageBody::private, GNUNET_MESSENGER_MessageBody::request, GNUNET_MESSENGER_MessageText::text, GNUNET_MESSENGER_MessageBody::text, and GNUNET_MESSENGER_MessageFile::uri.

Referenced by decode_message(), and decode_short_message().

514 {
515  uint16_t padding = 0;
516 
517  GNUNET_memcpy(&padding, buffer + length - sizeof(padding), sizeof(padding));
518 
519  if (padding > length - offset)
520  padding = 0;
521 
522  const uint16_t end_zero = length - padding;
523 
524  if ((padding) && (buffer[end_zero] != '\0'))
525  padding = 0;
526 
527  length -= padding;
528 
529  switch (*kind)
530  {
532  decode_step_key(buffer, offset, &(body->info.host_key), length);
533  decode_step(buffer, offset, &(body->info.messenger_version));
534  break;
536  decode_step_key(buffer, offset, &(body->join.key), length);
537  break;
539  if (length - offset > 0)
540  decode_step_malloc(buffer, offset, body->name.name, length - offset, 1);
541  else
542  body->name.name = NULL;
543  break;
545  decode_step_key(buffer, offset, &(body->key.key), length);
546  break;
548  decode_step(buffer, offset, &(body->peer.peer));
549  break;
551  decode_step(buffer, offset, &(body->id.id));
552  break;
554  decode_step(buffer, offset, &(body->miss.peer));
555  break;
557  decode_step(buffer, offset, &(body->merge.previous));
558  break;
560  decode_step(buffer, offset, &(body->request.hash));
561  break;
563  decode_step(buffer, offset, &(body->invite.door));
564  decode_step(buffer, offset, &(body->invite.key));
565  break;
567  decode_step_malloc(buffer, offset, body->text.text, length - offset, 1);
568  break;
570  decode_step(buffer, offset, &(body->file.key));
571  decode_step(buffer, offset, &(body->file.hash));
572  decode_step_ext(buffer, offset, body->file.name, sizeof(body->file.name));
573  decode_step_malloc(buffer, offset, body->file.uri, length - offset, 1);
574  break;
576  decode_step(buffer, offset, &(body->private.key));
577 
578  body->private.length = (length - offset);
579  decode_step_malloc(buffer, offset, body->private.data, length - offset, 0);
580  break;
581  default:
583  break;
584  }
585 
586  return padding;
587 }
struct GNUNET_PeerIdentity peer
The peer identity of the sender opening a room.
struct GNUNET_PeerIdentity door
The peer identity of an open door to a room.
struct GNUNET_PeerIdentity peer
The peer identity of a disconnected door to a room.
char * uri
The uri of the encrypted file.
#define decode_step_key(src, offset, dst, length)
struct GNUNET_IDENTITY_PublicKey host_key
The senders key to verify its signatures.
struct GNUNET_ShortHashCode id
The new id which will replace the senders id in a room.
struct GNUNET_MESSENGER_MessageText text
struct GNUNET_HashCode hash
The hash of the requested message.
struct GNUNET_MESSENGER_MessageKey key
uint32_t messenger_version
The version of GNUnet Messenger API.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
char name[NAME_MAX]
The name of the original file.
char * text
The containing text.
struct GNUNET_MESSENGER_MessageRequest request
#define decode_step(src, offset, dst)
struct GNUNET_IDENTITY_PublicKey key
The senders public key to verify its signatures.
struct GNUNET_MESSENGER_MessageInvite invite
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_MESSENGER_MessageMiss miss
struct GNUNET_CRYPTO_SymmetricSessionKey key
The symmetric key to decrypt the file.
uint16_t length
The length of the encrypted message.
struct GNUNET_MESSENGER_MessageMerge merge
struct GNUNET_HashCode hash
The hash of the original file.
struct GNUNET_MESSENGER_MessageName name
struct GNUNET_MESSENGER_MessageInfo info
struct GNUNET_CRYPTO_EcdhePublicKey key
The ECDH key to decrypt the message.
struct GNUNET_MESSENGER_MessagePeer peer
char * data
The data of the encrypted message.
#define decode_step_ext(src, offset, dst, size)
struct GNUNET_MESSENGER_MessagePrivate private
char * name
The new name which replaces the current senders name.
struct GNUNET_HashCode key
The hash identifying the port of the room.
struct GNUNET_MESSENGER_MessageJoin join
struct GNUNET_MESSENGER_MessageFile file
struct GNUNET_MESSENGER_MessageId id
#define decode_step_malloc(src, offset, dst, size, zero)
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_signature,
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
[out]paddingPadding
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 590 of file messenger_api_message.c.

References GNUNET_MESSENGER_Message::body, decode_message_body(), decode_step, get_message_kind_size(), GNUNET_assert, 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().

592 {
593  GNUNET_assert((message) && (buffer) && (length >= get_message_kind_size(GNUNET_MESSENGER_KIND_UNKNOWN)));
594 
595  uint16_t offset = 0;
596 
597  if (GNUNET_YES == include_signature)
598  {
600  &(message->header.signature), buffer, length - offset
601  );
602 
603  if (result < 0)
604  return GNUNET_NO;
605  else
606  offset += result;
607  }
608 
609  const uint16_t count = length - offset;
610 
612  return GNUNET_NO;
613 
614  kind_t kind;
615 
616  decode_step(buffer, offset, &(message->header.timestamp));
617  decode_step(buffer, offset, &(message->header.sender_id));
618  decode_step(buffer, offset, &(message->header.previous));
619  decode_step(buffer, offset, &kind);
620 
621  message->header.kind = (enum GNUNET_MESSENGER_MessageKind) kind;
622 
623  if (count < get_message_kind_size (message->header.kind))
624  return GNUNET_NO;
625 
626  const uint16_t result = decode_message_body (&(message->header.kind), &(message->body), length, buffer, offset);
627 
628  if (padding)
629  *padding = result;
630 
631  return GNUNET_YES;
632 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the message.
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)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define decode_step(src, offset, dst)
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.
struct GNUNET_MESSENGER_MessageBody body
Body.
static int result
Global testing status.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
uint32_t kind_t
struct GNUNET_HashCode previous
The hash of the previous message from the senders perspective.
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.
GNUNET_MESSENGER_MessageKind
Enum for the different supported kinds of messages.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ decode_short_message()

static int decode_short_message ( struct GNUNET_MESSENGER_ShortMessage message,
uint16_t  length,
const char *  buffer 
)
static

Definition at line 635 of file messenger_api_message.c.

References GNUNET_MESSENGER_ShortMessage::body, decode_message_body(), decode_step, get_short_message_size(), GNUNET_CRYPTO_hash(), GNUNET_CRYPTO_hash_cmp(), GNUNET_memcpy, GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_NO, GNUNET_YES, GNUNET_MESSENGER_MessageSignature::hash, and GNUNET_MESSENGER_ShortMessage::kind.

Referenced by decrypt_message().

636 {
637  struct GNUNET_HashCode expected, hash;
638  uint16_t offset = sizeof(hash);
639 
640  if (length < get_short_message_size (NULL, GNUNET_NO))
641  return GNUNET_NO;
642 
643  GNUNET_memcpy(&hash, buffer, sizeof(hash));
644 
646  buffer + sizeof(hash),
647  length - sizeof(hash),
648  &expected
649  );
650 
651  if (0 != GNUNET_CRYPTO_hash_cmp(&hash, &expected))
652  return GNUNET_NO;
653 
654  kind_t kind;
655 
656  decode_step(buffer, offset, &kind);
657 
658  message->kind = (enum GNUNET_MESSENGER_MessageKind) kind;
659 
660  if (length < get_short_message_size (message, GNUNET_NO))
661  return GNUNET_NO;
662 
663  decode_message_body (&(message->kind), &(message->body), length, buffer, offset);
664 
665  if (GNUNET_MESSENGER_KIND_UNKNOWN == message->kind)
666  return GNUNET_NO;
667 
668  return GNUNET_YES;
669 }
enum GNUNET_MESSENGER_MessageKind kind
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)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define decode_step(src, offset, dst)
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
A 512-bit hashcode.
uint32_t kind_t
static uint16_t get_short_message_size(const struct GNUNET_MESSENGER_ShortMessage *message, int include_body)
struct GNUNET_MESSENGER_MessageBody body
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
GNUNET_MESSENGER_MessageKind
Enum for the different supported kinds of messages.
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 672 of file messenger_api_message.c.

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().

674 {
675  GNUNET_assert((message) && (buffer) && (hash));
676 
677  const ssize_t offset = GNUNET_IDENTITY_signature_get_length(
678  &(message->header.signature)
679  );
680 
681  GNUNET_CRYPTO_hash (buffer + offset, length - offset, hash);
682 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
ssize_t GNUNET_IDENTITY_signature_get_length(const struct GNUNET_IDENTITY_Signature *sig)
Get the compacted length of a GNUNET_IDENTITY_Signature.
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

Definition at line 685 of file messenger_api_message.c.

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().

687 {
688  GNUNET_assert((message) && (buffer) && (hash) && (ego));
689 
690  struct GNUNET_MESSENGER_MessageSignature signature;
691 
693  signature.purpose.size = htonl (sizeof(signature));
694 
695  GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
696  GNUNET_IDENTITY_sign(&(ego->priv), &signature, &(message->header.signature));
697 
698  uint16_t offset = 0;
699  encode_step_signature(buffer, offset, &(message->header.signature), length);
700 }
#define GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE
Signature of a chat message.
struct GNUNET_MESSENGER_MessageHeader header
Header.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define encode_step_signature(dst, offset, src, length)
#define GNUNET_IDENTITY_sign(priv, ps, sig)
Sign a given block with GNUNET_IDENTITY_PrivateKey.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
A 512-bit hashcode.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
struct GNUNET_IDENTITY_PrivateKey priv
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 703 of file messenger_api_message.c.

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

Referenced by verify_member_session_as_sender().

705 {
706  GNUNET_assert((message) && (hash) && (key));
707 
708  if (ntohl (key->type) != ntohl (message->header.signature.type))
709  return GNUNET_SYSERR;
710 
711  struct GNUNET_MESSENGER_MessageSignature signature;
712 
714  signature.purpose.size = htonl (sizeof(signature));
715 
716  GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
717 
719  &(message->header.signature), key);
720 }
#define GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE
Signature of a chat message.
struct GNUNET_MESSENGER_MessageHeader header
Header.
uint32_t type
Type of public key.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_IDENTITY_signature_verify(purp, ps, sig, pub)
Verify a given signature with GNUNET_IDENTITY_PublicKey.
uint32_t type
Type of signature.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
A 512-bit hashcode.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
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

Definition at line 723 of file messenger_api_message.c.

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_log, GNUNET_malloc, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_NO, GNUNET_YES, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessagePrivate::key, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_ShortMessage::kind, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageBody::private, and unfold_short_message().

Referenced by handle_send_message().

724 {
725  GNUNET_assert((message) && (key));
726 
727  struct GNUNET_MESSENGER_ShortMessage shortened;
728 
729  fold_short_message (message, &shortened);
730 
731  const uint16_t length = get_short_message_size (&shortened, GNUNET_YES);
732  const uint16_t padded_length = calc_padded_length(length);
733 
735  message->body.private.data = GNUNET_malloc(padded_length);
736  message->body.private.length = padded_length;
737 
738  encode_short_message (&shortened, padded_length, message->body.private.data);
739 
740  if (padded_length == GNUNET_IDENTITY_encrypt (message->body.private.data, padded_length, key,
741  &(message->body.private.key),
742  message->body.private.data))
743  {
744  destroy_message_body (shortened.kind, &(shortened.body));
745  return GNUNET_YES;
746  }
747  else
748  {
749  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Encrypting message failed!\n");
750 
751  unfold_short_message (&shortened, message);
752  return GNUNET_NO;
753  }
754 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
static uint16_t calc_padded_length(uint16_t length)
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_assert(cond)
Use this for fatal errors that cannot be handled.
static void unfold_short_message(struct GNUNET_MESSENGER_ShortMessage *shortened, struct GNUNET_MESSENGER_Message *message)
struct GNUNET_MESSENGER_MessageBody body
Body.
uint16_t length
The length of the encrypted message.
struct GNUNET_CRYPTO_EcdhePublicKey key
The ECDH key to decrypt the message.
static uint16_t get_short_message_size(const struct GNUNET_MESSENGER_ShortMessage *message, int include_body)
char * data
The data of the encrypted message.
static void fold_short_message(const struct GNUNET_MESSENGER_Message *message, struct GNUNET_MESSENGER_ShortMessage *shortened)
static void encode_short_message(const struct GNUNET_MESSENGER_ShortMessage *message, uint16_t length, char *buffer)
#define GNUNET_log(kind,...)
struct GNUNET_MESSENGER_MessagePrivate private
static void destroy_message_body(enum GNUNET_MESSENGER_MessageKind kind, struct GNUNET_MESSENGER_MessageBody *body)
#define GNUNET_malloc(size)
Wrapper around malloc.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the 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

Definition at line 757 of file messenger_api_message.c.

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

Referenced by notify_handle_message().

758 {
759  GNUNET_assert((message) && (key));
760 
761  if (message->body.private.length != GNUNET_IDENTITY_decrypt (message->body.private.data, message->body.private.length,
762  key, &(message->body.private.key),
763  message->body.private.data))
764  {
765  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Decrypting message failed!\n");
766 
767  return GNUNET_NO;
768  }
769 
770  struct GNUNET_MESSENGER_ShortMessage shortened;
771 
772  if (GNUNET_YES != decode_short_message (&shortened, message->body.private.length, message->body.private.data))
773  {
774  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Decoding decrypted message failed!\n");
775 
776  return GNUNET_NO;
777  }
778 
779  unfold_short_message (&shortened, message);
780 
781  return GNUNET_YES;
782 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void unfold_short_message(struct GNUNET_MESSENGER_ShortMessage *shortened, struct GNUNET_MESSENGER_Message *message)
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 ...
struct GNUNET_MESSENGER_MessageBody body
Body.
uint16_t length
The length of the encrypted message.
static int decode_short_message(struct GNUNET_MESSENGER_ShortMessage *message, uint16_t length, const char *buffer)
struct GNUNET_CRYPTO_EcdhePublicKey key
The ECDH key to decrypt the message.
char * data
The data of the encrypted message.
#define GNUNET_log(kind,...)
struct GNUNET_MESSENGER_MessagePrivate private
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 785 of file messenger_api_message.c.

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, 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_room_message().

787 {
788  GNUNET_assert(message);
789 
790  if (ego)
791  message->header.signature.type = ego->priv.type;
792 
793  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packing message kind=%u and sender: %s\n",
794  message->header.kind, GNUNET_sh2s(&(message->header.sender_id)));
795 
796  struct GNUNET_MessageHeader *header;
797 
798  const uint16_t length = get_message_size (message, GNUNET_YES);
799  const uint16_t padded_length = calc_padded_length(length);
800 
801  struct GNUNET_MQ_Envelope *env;
802  char *buffer;
803 
805  {
806  env = GNUNET_MQ_msg_extra(header, padded_length, GNUNET_MESSAGE_TYPE_CADET_CLI);
807 
808  buffer = (char*) &(header[1]);
809  }
810  else
811  {
812  env = NULL;
813 
814  buffer = GNUNET_malloc(padded_length);
815  }
816 
817  encode_message (message, padded_length, buffer, GNUNET_YES);
818 
819  if (hash)
820  {
821  hash_message (message, length, buffer, hash);
822 
823  if (ego)
824  sign_message (message, length, buffer, hash, ego);
825  }
826 
828  GNUNET_free(buffer);
829 
830  return env;
831 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
const char * GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc)
Convert a short hash value to a string (for printing debug messages).
static uint16_t calc_padded_length(uint16_t length)
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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 GNUNET_MESSAGE_TYPE_CADET_CLI
Traffic (net-cat style) used by the Command Line Interface.
static enum @8 mode
Should we do a PUT (mode = 0) or GET (mode = 1);.
#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
uint32_t type
Type of signature.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
uint32_t type
Type of public key.
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.
#define GNUNET_log(kind,...)
struct GNUNET_IDENTITY_PrivateKey priv
Header for all communications.
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...
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
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.
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 834 of file messenger_api_message.c.

References GNUNET_MESSENGER_KIND_DELETE, GNUNET_MESSENGER_KIND_FILE, GNUNET_MESSENGER_KIND_ID, GNUNET_MESSENGER_KIND_INFO, GNUNET_MESSENGER_KIND_INVITE, GNUNET_MESSENGER_KIND_JOIN, GNUNET_MESSENGER_KIND_KEY, GNUNET_MESSENGER_KIND_LEAVE, GNUNET_MESSENGER_KIND_MERGE, GNUNET_MESSENGER_KIND_MISS, GNUNET_MESSENGER_KIND_NAME, GNUNET_MESSENGER_KIND_PEER, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_MESSENGER_KIND_REQUEST, GNUNET_MESSENGER_KIND_TEXT, GNUNET_NO, GNUNET_SYSERR, GNUNET_YES, GNUNET_MESSENGER_Message::header, and GNUNET_MESSENGER_MessageHeader::kind.

Referenced by check_send_message(), and GNUNET_MESSENGER_send_message().

835 {
836  switch (message->header.kind)
837  {
839  return GNUNET_SYSERR; // Reserved for connection handling only!
841  return GNUNET_NO; // Use #GNUNET_MESSENGER_enter_room(...) instead!
843  return GNUNET_NO; // Use #GNUNET_MESSENGER_close_room(...) instead!
845  return GNUNET_YES;
847  return GNUNET_NO; // Use #GNUNET_MESSENGER_update(...) instead!
849  return GNUNET_NO; // Use #GNUNET_MESSENGER_open_room(...) instead!
851  return GNUNET_SYSERR; // Reserved for member id handling only!
853  return GNUNET_SYSERR; // Reserved for connection handling only!
855  return GNUNET_YES;
857  return GNUNET_YES;
859  return GNUNET_YES;
861  return GNUNET_YES;
863  return GNUNET_YES;
865  return GNUNET_NO; // Use #GNUNET_MESSENGER_send_message(...) with a contact instead!
867  return GNUNET_YES;
868  default:
869  return GNUNET_SYSERR;
870  }
871 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
Here is the caller graph for this function: