GNUnet  0.11.x
Macros | Functions
messenger_api_message.h File Reference

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

#include "platform.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_identity_service.h"
#include "gnunet_mq_lib.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 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)
 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_signature)
 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_signature)
 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_signature, 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 39 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PADDING_MIN

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

Definition at line 41 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PADDING_LEVEL0

#define GNUNET_MESSENGER_PADDING_LEVEL0   (512)

Definition at line 42 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PADDING_LEVEL1

#define GNUNET_MESSENGER_PADDING_LEVEL1   (4096)

Definition at line 43 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PADDING_LEVEL2

#define GNUNET_MESSENGER_PADDING_LEVEL2   (32768)

Definition at line 44 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PACK_MODE_ENVELOPE

#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE   0x1

Definition at line 209 of file messenger_api_message.h.

◆ GNUNET_MESSENGER_PACK_MODE_UNKNOWN

#define GNUNET_MESSENGER_PACK_MODE_UNKNOWN   0x0

Definition at line 210 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 41 of file messenger_api_message.c.

42 {
44 
45  message->header.kind = kind;
46 
47  switch (message->header.kind)
48  {
50  message->body.name.name = NULL;
51  break;
53  message->body.text.text = NULL;
54  break;
56  message->body.file.uri = NULL;
57  break;
59  message->body.privacy.length = 0;
60  message->body.privacy.data = NULL;
61  break;
62  default:
63  break;
64  }
65 
66  return message;
67 }
#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 70 of file messenger_api_message.c.

71 {
72  GNUNET_assert(message);
73 
75 
76  GNUNET_memcpy(copy, message, sizeof(struct GNUNET_MESSENGER_Message));
77 
78  switch (message->header.kind)
79  {
81  copy->body.name.name = GNUNET_strdup(message->body.name.name);
82  break;
84  copy->body.text.text = GNUNET_strdup(message->body.text.text);
85  break;
87  copy->body.file.uri = GNUNET_strdup(message->body.file.uri);
88  break;
90  copy->body.privacy.data = copy->body.privacy.length ? GNUNET_malloc(copy->body.privacy.length) : NULL;
91 
92  if (copy->body.privacy.data)
93  {
94  GNUNET_memcpy(copy->body.privacy.data, message->body.privacy.data, copy->body.privacy.length);
95  }
96 
97  break;
98  default:
99  break;
100  }
101 
102  return copy;
103 }
#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_handle_message(), and send_handle_message().

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]message Message

Definition at line 129 of file messenger_api_message.c.

130 {
131  GNUNET_assert(message);
132 
133  destroy_message_body (message->header.kind, &(message->body));
134 
135  GNUNET_free(message);
136 }
#define GNUNET_free(ptr)
Wrapper around free.
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_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().

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

140 {
141  GNUNET_assert(message);
142 
143  if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) ||
144  (GNUNET_MESSENGER_KIND_LEAVE == message->header.kind) ||
145  (GNUNET_MESSENGER_KIND_NAME == message->header.kind) ||
146  (GNUNET_MESSENGER_KIND_KEY == message->header.kind) ||
147  (GNUNET_MESSENGER_KIND_ID == message->header.kind))
148  return GNUNET_YES;
149  else
150  return GNUNET_NO;
151 }
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
@ 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_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)

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

225 {
226  uint16_t length = 0;
227 
228  length += member_size(struct GNUNET_MESSENGER_Message, header.timestamp);
229  length += member_size(struct GNUNET_MESSENGER_Message, header.sender_id);
230  length += member_size(struct GNUNET_MESSENGER_Message, header.previous);
231  length += sizeof(kind_t);
232 
233  return length + get_message_body_kind_size (kind);
234 }
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_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_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 273 of file messenger_api_message.c.

275 {
276  GNUNET_assert(message);
277 
278  uint16_t length = 0;
279 
280  if (GNUNET_YES == include_signature)
282 
283  length += get_message_kind_size (message->header.kind);
284  length += get_message_body_size (message->header.kind, &(message->body));
285 
286  return length;
287 }
ssize_t GNUNET_IDENTITY_signature_get_length(const struct GNUNET_IDENTITY_Signature *sig)
Get the compacted length of a GNUNET_IDENTITY_Signature.
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_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_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_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 456 of file messenger_api_message.c.

460 {
461  GNUNET_assert((message) && (buffer));
462 
463  uint16_t offset = 0;
464 
465  if (GNUNET_YES == include_signature)
466  encode_step_signature(buffer, offset, &(message->header.signature), length);
467 
468  const kind_t kind = GNUNET_htobe32((kind_t) message->header.kind);
469 
470  encode_step(buffer, offset, &(message->header.timestamp));
471  encode_step(buffer, offset, &(message->header.sender_id));
472  encode_step(buffer, offset, &(message->header.previous));
473  encode_step(buffer, offset, &kind);
474 
475  encode_message_body (message->header.kind, &(message->body), length, buffer, offset);
476 }
#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_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_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 611 of file messenger_api_message.c.

616 {
617  GNUNET_assert((message) && (buffer) && (length >= get_message_kind_size(GNUNET_MESSENGER_KIND_UNKNOWN)));
618 
619  uint16_t offset = 0;
620 
621  if (GNUNET_YES == include_signature)
622  {
624  &(message->header.signature), buffer, length - offset
625  );
626 
627  if (result < 0)
628  return GNUNET_NO;
629  else
630  offset += result;
631  }
632 
633  const uint16_t count = length - offset;
634 
636  return GNUNET_NO;
637 
638  kind_t kind;
639 
640  decode_step(buffer, offset, &(message->header.timestamp));
641  decode_step(buffer, offset, &(message->header.sender_id));
642  decode_step(buffer, offset, &(message->header.previous));
643  decode_step(buffer, offset, &kind);
644 
645  message->header.kind = (enum GNUNET_MESSENGER_MessageKind) GNUNET_be32toh(kind);
646 
647  if (count < get_message_kind_size (message->header.kind))
648  return GNUNET_NO;
649 
650  const uint16_t result = decode_message_body (&(message->header.kind), &(message->body), length, buffer, offset);
651 
652  if (padding)
653  *padding = result;
654 
655  return GNUNET_YES;
656 }
static int result
Global testing status.
#define GNUNET_be32toh(x)
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.
@ 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 698 of file messenger_api_message.c.

702 {
703  GNUNET_assert((message) && (buffer) && (hash));
704 
705  const ssize_t offset = GNUNET_IDENTITY_signature_get_length(
706  &(message->header.signature)
707  );
708 
709  GNUNET_CRYPTO_hash (buffer + offset, length - offset, hash);
710 }
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]message Message
[in]lengthLength of buffer
[out]bufferBuffer
[in]hashHash of message
[in]egoEGO

Definition at line 713 of file messenger_api_message.c.

718 {
719  GNUNET_assert((message) && (buffer) && (hash) && (ego));
720 
721  struct GNUNET_MESSENGER_MessageSignature signature;
722 
724  signature.purpose.size = htonl (sizeof(signature));
725 
726  GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
727  GNUNET_IDENTITY_sign(&(ego->priv), &signature, &(message->header.signature));
728 
729  uint16_t offset = 0;
730  encode_step_signature(buffer, offset, &(message->header.signature), length);
731 }
#define GNUNET_IDENTITY_sign(priv, ps, sig)
Sign a given block with GNUNET_IDENTITY_PrivateKey.
#define GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE
Signature of a chat message.
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 734 of file messenger_api_message.c.

737 {
738  GNUNET_assert((message) && (hash) && (key));
739 
740  if (ntohl (key->type) != ntohl (message->header.signature.type))
741  return GNUNET_SYSERR;
742 
743  struct GNUNET_MESSENGER_MessageSignature signature;
744 
746  signature.purpose.size = htonl (sizeof(signature));
747 
748  GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
749 
751  &(message->header.signature), key);
752 }
struct GNUNET_HashCode key
The key used in the DHT.
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#define GNUNET_IDENTITY_signature_verify(purp, ps, sig, pub)
Verify a given signature with GNUNET_IDENTITY_PublicKey.
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]message Message
[in]keyPublic key of EGO
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 755 of file messenger_api_message.c.

757 {
758  GNUNET_assert((message) && (key));
759 
760  struct GNUNET_MESSENGER_ShortMessage shortened;
761 
762  fold_short_message (message, &shortened);
763 
764  const uint16_t length = get_short_message_size (&shortened, GNUNET_YES);
765  const uint16_t padded_length = calc_padded_length(length);
766 
768  message->body.privacy.data = GNUNET_malloc(padded_length);
769  message->body.privacy.length = padded_length;
770 
771  encode_short_message (&shortened, padded_length, message->body.privacy.data);
772 
773  if (padded_length == GNUNET_IDENTITY_encrypt (message->body.privacy.data, padded_length, key,
774  &(message->body.privacy.key),
775  message->body.privacy.data))
776  {
777  destroy_message_body (shortened.kind, &(shortened.body));
778  return GNUNET_YES;
779  }
780  else
781  {
782  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Encrypting message failed!\n");
783 
784  unfold_short_message (&shortened, message);
785  return GNUNET_NO;
786  }
787 }
#define GNUNET_log(kind,...)
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...
@ 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)
struct GNUNET_CRYPTO_EcdhePublicKey key
The ECDH key to decrypt the 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_log, GNUNET_malloc, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_NO, GNUNET_YES, GNUNET_MESSENGER_Message::header, key, GNUNET_MESSENGER_MessagePrivate::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]message Message
[in]keyPrivate key of EGO
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 790 of file messenger_api_message.c.

792 {
793  GNUNET_assert((message) && (key));
794 
795  if (message->body.privacy.length != GNUNET_IDENTITY_decrypt (message->body.privacy.data, message->body.privacy.length,
796  key, &(message->body.privacy.key),
797  message->body.privacy.data))
798  {
799  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Decrypting message failed!\n");
800 
801  return GNUNET_NO;
802  }
803 
804  struct GNUNET_MESSENGER_ShortMessage shortened;
805 
806  if (GNUNET_YES != decode_short_message (&shortened, message->body.privacy.length, message->body.privacy.data))
807  {
808  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Decoding decrypted message failed!\n");
809 
810  return GNUNET_NO;
811  }
812 
813  unfold_short_message (&shortened, message);
814 
815  return GNUNET_YES;
816 }
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 ...
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_log, GNUNET_NO, GNUNET_YES, key, GNUNET_MESSENGER_MessagePrivate::key, GNUNET_MESSENGER_MessagePrivate::length, GNUNET_MESSENGER_MessageBody::privacy, and unfold_short_message().

Referenced by notify_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 819 of file messenger_api_message.c.

823 {
824  GNUNET_assert(message);
825 
826  if (ego)
827  message->header.signature.type = ego->priv.type;
828 
829  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packing message kind=%u and sender: %s\n",
830  message->header.kind, GNUNET_sh2s(&(message->header.sender_id)));
831 
832  struct GNUNET_MessageHeader *header;
833 
834  const uint16_t length = get_message_size (message, GNUNET_YES);
835  const uint16_t padded_length = calc_padded_length(length);
836 
837  struct GNUNET_MQ_Envelope *env;
838  char *buffer;
839 
841  {
842  env = GNUNET_MQ_msg_extra(header, padded_length, GNUNET_MESSAGE_TYPE_CADET_CLI);
843 
844  buffer = (char*) &(header[1]);
845  }
846  else
847  {
848  env = NULL;
849 
850  buffer = GNUNET_malloc(padded_length);
851  }
852 
853  encode_message (message, padded_length, buffer, GNUNET_YES);
854 
855  if (hash)
856  {
857  hash_message (message, length, buffer, hash);
858 
859  if (ego)
860  sign_message (message, length, buffer, hash, ego);
861  }
862 
864  GNUNET_free(buffer);
865 
866  return env;
867 }
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:52
#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.
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.
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_signature)
Encodes a given message into a buffer of a maximal length in bytes.
#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_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 870 of file messenger_api_message.c.

871 {
872  switch (message->header.kind)
873  {
875  return GNUNET_SYSERR; // Reserved for connection handling only!
877  return GNUNET_NO; // Use #GNUNET_MESSENGER_enter_room(...) instead!
879  return GNUNET_NO; // Use #GNUNET_MESSENGER_close_room(...) instead!
881  return GNUNET_YES;
883  return GNUNET_NO; // Use #GNUNET_MESSENGER_update(...) instead!
885  return GNUNET_NO; // Use #GNUNET_MESSENGER_open_room(...) instead!
887  return GNUNET_SYSERR; // Reserved for member id handling only!
889  return GNUNET_SYSERR; // Reserved for connection handling only!
891  return GNUNET_YES;
893  return GNUNET_YES;
895  return GNUNET_YES;
897  return GNUNET_YES;
899  return GNUNET_YES;
901  return GNUNET_NO; // Use #GNUNET_MESSENGER_send_message(...) with a contact instead!
903  return GNUNET_YES;
904  default:
905  return GNUNET_SYSERR;
906  }
907 }
@ 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: