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_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...
 
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)
 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)
 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)
 Decodes a message from a given buffer of a maximal length in bytes. More...
 
void hash_message (uint16_t length, const char *buffer, struct GNUNET_HashCode *hash)
 Calculates a hash of a given buffer of a length in bytes. 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...
 

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_PACK_MODE_ENVELOPE

#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE   0x1

◆ GNUNET_MESSENGER_PACK_MODE_UNKNOWN

#define GNUNET_MESSENGER_PACK_MODE_UNKNOWN   0x0

Definition at line 172 of file messenger_api_message.h.

Referenced by send_room_message(), and send_room_message_ext().

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
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_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
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_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_room_message(), forward_tunnel_message(), handle_room_message(), handle_tunnel_message(), iterate_send_room_message(), send_room_message(), send_room_message_ext(), and send_tunnel_message().

71 {
73 
74  GNUNET_memcpy(copy, message, sizeof(struct GNUNET_MESSENGER_Message));
75 
76  switch (message->header.kind)
77  {
79  copy->body.name.name = GNUNET_strdup(message->body.name.name);
80  break;
82  copy->body.text.text = GNUNET_strdup(message->body.text.text);
83  break;
85  copy->body.file.uri = GNUNET_strdup(message->body.file.uri);
86  break;
88  copy->body.private.data = copy->body.private.length ? GNUNET_malloc(copy->body.private.length) : NULL;
89 
90  if (copy->body.private.data)
91  {
92  GNUNET_memcpy(copy->body.private.data, message->body.private.data, copy->body.private.length);
93  }
94 
95  break;
96  default:
97  break;
98  }
99 
100  return copy;
101 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
char * uri
The uri of the encrypted file.
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()

void destroy_message ( struct GNUNET_MESSENGER_Message message)

Destroys a message and frees its memory fully.

Parameters
messageMessage

Definition at line 126 of file messenger_api_message.c.

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

Referenced by callback_ego_create(), callback_room_disconnect(), close_service_room(), create_message_info(), create_message_peer(), get_room_message(), get_store_message(), handle_room_message(), iterate_destroy_message(), iterate_destroy_messages(), join_room(), merge_room_last_messages(), open_room(), recv_message_info(), send_room_info(), and set_handle_name().

127 {
128  destroy_message_body (message->header.kind, &(message->body));
129 
130  GNUNET_free(message);
131 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
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:

◆ 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
kindKind of message
Returns
Minimal size to encode

Definition at line 211 of file messenger_api_message.c.

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

Referenced by decode_message(), and get_message_size().

212 {
213  uint16_t length = 0;
214 
219  length += member_size(struct GNUNET_MESSENGER_Message, header.kind);
220 
221  return length + get_message_body_kind_size (kind);
222 }
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)
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
struct GNUNET_HashCode previous
The hash of the previous message from the senders perspective.
#define member_size(type, member)
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_size()

uint16_t get_message_size ( const struct GNUNET_MESSENGER_Message message)

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

Parameters
messageMessage
Returns
Size to encode

Definition at line 251 of file messenger_api_message.c.

References GNUNET_MESSENGER_Message::body, get_message_body_size(), get_message_kind_size(), GNUNET_MESSENGER_Message::header, and GNUNET_MESSENGER_MessageHeader::kind.

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

252 {
253  return get_message_kind_size (message->header.kind) + get_message_body_size (message->header.kind, &(message->body));
254 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
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.
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_message()

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

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

Parameters
messageMessage
lengthMaximal length to encode
[out]bufferBuffer

Definition at line 337 of file messenger_api_message.c.

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

338 {
339  uint16_t offset = 0;
340 
341  encode_step(buffer, offset, &(message->header.signature));
342  encode_step(buffer, offset, &(message->header.timestamp));
343  encode_step(buffer, offset, &(message->header.sender_id));
344  encode_step(buffer, offset, &(message->header.previous));
345  encode_step(buffer, offset, &(message->header.kind));
346 
347  encode_message_body (message->header.kind, &(message->body), length, buffer, offset);
348 }
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 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)
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
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:

◆ decode_message()

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

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
lengthMaximal length to decode
bufferBuffer
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 440 of file messenger_api_message.c.

References GNUNET_MESSENGER_Message::body, decode_message_body(), decode_step, get_message_kind_size(), GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_NO, 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 check_recv_message(), check_send_message(), check_tunnel_message(), get_store_message(), handle_recv_message(), handle_send_message(), and handle_tunnel_message().

441 {
442  uint16_t offset = 0;
443 
445  return GNUNET_NO;
446 
447  decode_step(buffer, offset, &(message->header.signature));
448  decode_step(buffer, offset, &(message->header.timestamp));
449  decode_step(buffer, offset, &(message->header.sender_id));
450  decode_step(buffer, offset, &(message->header.previous));
451  decode_step(buffer, offset, &(message->header.kind));
452 
453  if (length < get_message_kind_size (message->header.kind))
454  return GNUNET_NO;
455 
456  decode_message_body (&(message->header.kind), &(message->body), length, buffer, offset);
457 
458  return GNUNET_YES;
459 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
static void decode_message_body(enum GNUNET_MESSENGER_MessageKind *kind, struct GNUNET_MESSENGER_MessageBody *body, uint16_t length, const char *buffer, uint16_t offset)
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the message.
#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.
struct GNUNET_IDENTITY_Signature signature
The signature of the senders private key.
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:

◆ hash_message()

void hash_message ( uint16_t  length,
const char *  buffer,
struct GNUNET_HashCode hash 
)

Calculates a hash of a given buffer of a length in bytes.

Parameters
lengthLength of buffer
bufferBuffer
[out]hashHash

Definition at line 480 of file messenger_api_message.c.

References GNUNET_CRYPTO_hash().

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

481 {
482  GNUNET_CRYPTO_hash (buffer + sizeof(struct GNUNET_CRYPTO_EcdsaSignature),
483  length - sizeof(struct GNUNET_CRYPTO_EcdsaSignature), hash);
484 }
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
an ECC signature using ECDSA
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
[out]messageMessage
lengthLength of buffer
[out]bufferBuffer
hashHash of message
egoEGO

Definition at line 487 of file messenger_api_message.c.

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

489 {
490  struct GNUNET_MESSENGER_MessageSignature signature;
491 
493  signature.purpose.size = htonl (sizeof(signature));
494 
495  GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
496 
497  GNUNET_IDENTITY_sign(&(ego->priv), &signature, &(message->header.signature));
498  GNUNET_memcpy(buffer, &(message->header.signature), sizeof(struct GNUNET_CRYPTO_EcdsaSignature));
499 }
#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_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_IDENTITY_sign(priv, ps, sig)
Sign a given block with GNUNET_IDENTITY_PrivateKey.
an ECC signature using ECDSA
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
messageMessage
hashHash of message
keyPublic key of EGO
Returns
GNUNET_OK on success, otherwise GNUNET_SYSERR

Definition at line 502 of file messenger_api_message.c.

References GNUNET_IDENTITY_signature_verify, GNUNET_memcpy, GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE, GNUNET_MESSENGER_MessageSignature::hash, GNUNET_MESSENGER_Message::header, GNUNET_CRYPTO_EccSignaturePurpose::purpose, GNUNET_MESSENGER_MessageSignature::purpose, GNUNET_MESSENGER_MessageHeader::signature, and GNUNET_CRYPTO_EccSignaturePurpose::size.

Referenced by callback_verify_room_message().

504 {
505  struct GNUNET_MESSENGER_MessageSignature signature;
506 
508  signature.purpose.size = htonl (sizeof(signature));
509 
510  GNUNET_memcpy(&(signature.hash), hash, sizeof(struct GNUNET_HashCode));
511 
513  &(message->header.signature), key);
514 }
#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_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.
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
messageMessage
keyPublic key of EGO
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 517 of file messenger_api_message.c.

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_ShortMessage::body, GNUNET_MESSENGER_MessagePrivate::data, destroy_message_body(), encode_short_message(), fold_short_message(), get_short_message_size(), GNUNET_IDENTITY_encrypt(), GNUNET_malloc, GNUNET_MESSENGER_KIND_PRIVATE, GNUNET_NO, GNUNET_YES, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessagePrivate::key, GNUNET_MESSENGER_ShortMessage::kind, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_MessageBody::private, and unfold_short_message().

518 {
519  struct GNUNET_MESSENGER_ShortMessage shortened;
520 
521  fold_short_message (message, &shortened);
522 
523  const uint16_t length = get_short_message_size (&shortened);
524 
526  message->body.private.data = GNUNET_malloc(length);
527 
528  encode_short_message (&shortened, length, message->body.private.data);
529 
530  if (GNUNET_IDENTITY_encrypt (message->body.private.data, length, key, &(message->body.private.key),
531  message->body.private.data)
532  == length)
533  {
534  destroy_message_body (shortened.kind, &(shortened.body));
535  return GNUNET_YES;
536  }
537  else
538  {
539  unfold_short_message (&shortened, message);
540  return GNUNET_NO;
541  }
542 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
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...
static uint16_t get_short_message_size(const struct GNUNET_MESSENGER_ShortMessage *message)
static void unfold_short_message(struct GNUNET_MESSENGER_ShortMessage *shortened, struct GNUNET_MESSENGER_Message *message)
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_CRYPTO_EcdhePublicKey key
The ECDH key to decrypt the message.
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)
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:

◆ 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
messageMessage
keyPrivate key of EGO
Returns
GNUNET_YES on success, otherwise GNUNET_NO

Definition at line 545 of file messenger_api_message.c.

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

546 {
547  if (message->body.private.length != GNUNET_IDENTITY_decrypt (message->body.private.data,
548  message->body.private.length, key,
549  &(message->body.private.key),
550  message->body.private.data))
551  return GNUNET_NO;
552 
553  struct GNUNET_MESSENGER_ShortMessage shortened;
554 
555  if (GNUNET_YES != decode_short_message (&shortened, message->body.private.length, message->body.private.data))
556  return GNUNET_NO;
557 
558  unfold_short_message (&shortened, message);
559  return GNUNET_YES;
560 }
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.
struct GNUNET_MESSENGER_MessagePrivate private
Here is the call 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
egoEGO to sign
modeMode of packing
Returns
Envelope or NULL

Definition at line 563 of file messenger_api_message.c.

References encode_message(), env, get_message_size(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log, GNUNET_malloc, GNUNET_MESSAGE_TYPE_CADET_CLI, GNUNET_MESSENGER_PACK_MODE_ENVELOPE, GNUNET_MQ_msg_extra, hash_message(), GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, and sign_message().

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

565 {
566  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Packing message: %u\n", message->header.kind);
567 
568  struct GNUNET_MessageHeader *header;
569 
570  uint16_t length = get_message_size (message);
571 
572  struct GNUNET_MQ_Envelope *env;
573  char *buffer;
574 
576  {
577  env = GNUNET_MQ_msg_extra(header, length, GNUNET_MESSAGE_TYPE_CADET_CLI);
578 
579  buffer = (char*) &(header[1]);
580  }
581  else
582  {
583  env = NULL;
584 
585  buffer = GNUNET_malloc(length);
586  }
587 
588  encode_message (message, length, buffer);
589 
590  if (hash)
591  {
592  hash_message (length, buffer, hash);
593 
594  if (ego)
595  sign_message (message, length, buffer, hash, ego);
596  }
597 
599  GNUNET_free(buffer);
600 
601  return env;
602 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
void hash_message(uint16_t length, const char *buffer, struct GNUNET_HashCode *hash)
Calculates a hash of a given buffer of a length in bytes.
uint16_t get_message_size(const struct GNUNET_MESSENGER_Message *message)
Returns the exact size in bytes to encode a given message.
void encode_message(const struct GNUNET_MESSENGER_Message *message, uint16_t length, char *buffer)
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
#define GNUNET_MESSENGER_PACK_MODE_ENVELOPE
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_log(kind,...)
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.
Here is the call graph for this function:
Here is the caller graph for this function: