Merge commit 'origin/silc.1.1.branch'
[silc.git] / lib / silccore / silcmessage.h
index 09ca21aa8bc2bbff0dca4c71da69ef27f920257a..ef6ed3a8af6778147fe470cef49fd99c15c74ce0 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2006 Pekka Riikonen
+  Copyright (C) 1997 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -93,6 +93,10 @@ typedef SilcUInt16 SilcMessageFlags;
  *                                          SilcBool static_key,
  *                                          SilcCipher cipher,
  *                                          SilcHmac hmac,
+ *                                          unsigned char *sender_id,
+ *                                          SilcUInt32 sender_id_len,
+ *                                          unsigned char *receiver_id,
+ *                                          SilcUInt32 receiver_id_len,
  *                                          SilcBool check_mac);
  *
  * DESCRIPTION
@@ -105,6 +109,9 @@ typedef SilcUInt16 SilcMessageFlags;
  *    (Key Agreement was done for the key) then it MUST be FALSE.  For
  *    channel messages the `static_key' is ignored.
  *
+ *    The `sender_id' and `receiver_id' are the IDs from the packet header
+ *    of the packet where this message payload was received.
+ *
  *    This is usually used by the Message Payload interface itself but can
  *    be called by the appliation if separate decryption process is required.
  *    For example server might need to call this directly in some
@@ -118,6 +125,10 @@ SilcBool silc_message_payload_decrypt(unsigned char *data,
                                      SilcBool static_key,
                                      SilcCipher cipher,
                                      SilcHmac hmac,
+                                     unsigned char *sender_id,
+                                     SilcUInt32 sender_id_len,
+                                     unsigned char *receiver_id,
+                                     SilcUInt32 receiver_id_len,
                                      SilcBool check_mac);
 
 /****f* silccore/SilcMessageAPI/silc_message_payload_parse
@@ -131,6 +142,10 @@ SilcBool silc_message_payload_decrypt(unsigned char *data,
  *                               SilcBool static_key,
  *                               SilcCipher cipher,
  *                               SilcHmac hmac,
+ *                               unsigned char *sender_id,
+ *                               SilcUInt32 sender_id_len,
+ *                               unsigned char *receiver_id,
+ *                               SilcUInt32 receiver_id_len,
  *                               SilcStack stack,
  *                               SilcBool no_allocation,
  *                               SilcMessagePayload message);
@@ -151,6 +166,9 @@ SilcBool silc_message_payload_decrypt(unsigned char *data,
  *    then this assumes that the packet was decrypted with session keys
  *    (no private message key) and this merely decodes the payload.
  *
+ *    The `sender_id' and `receiver_id' are the IDs from the packet header
+ *    of the packet where this message payload was received.
+ *
  *    If the `message' is non-NULL then that pre-allocated context is
  *    used in parsing.  Same context is returned.  Otherwise new context
  *    is allocated and returned.  If the `stack' is non-NULL then memory
@@ -167,6 +185,10 @@ silc_message_payload_parse(unsigned char *payload,
                           SilcBool static_key,
                           SilcCipher cipher,
                           SilcHmac hmac,
+                          unsigned char *sender_id,
+                          SilcUInt32 sender_id_len,
+                          unsigned char *receiver_id,
+                          SilcUInt32 receiver_id_len,
                           SilcStack stack,
                           SilcBool no_allocation,
                           SilcMessagePayload message);
@@ -179,6 +201,8 @@ silc_message_payload_parse(unsigned char *payload,
  *                                          SilcUInt32 data_len,
  *                                          SilcUInt32 true_len,
  *                                          unsigned char *iv,
+ *                                          SilcID *sender_id,
+ *                                          SilcID *receiver_id,
  *                                          SilcCipher cipher,
  *                                          SilcHmac hmac);
  *
@@ -188,6 +212,8 @@ silc_message_payload_parse(unsigned char *payload,
  *    the `data' and `data_len'.  The `data_len' is the data length which
  *    is used to create MAC out of.  The `data' MUST have additional space
  *    after `true_len' bytes for the MAC which is appended to the data.
+ *    The `sender_id' is the ID message sender and `receiver_id' is ID of
+ *    message receiver.
  *
  *    This is usually used by the Message Payload interface itself but can
  *    be called by the appliation if separate encryption process is required.
@@ -200,35 +226,59 @@ SilcBool silc_message_payload_encrypt(unsigned char *data,
                                      SilcUInt32 data_len,
                                      SilcUInt32 true_len,
                                      unsigned char *iv,
+                                     SilcID *sender_id,
+                                     SilcID *receiver_id,
                                      SilcCipher cipher,
                                      SilcHmac hmac);
 
+/****f* silccore/SilcMessageAPI/SilcMessagePayloadEncoded
+ *
+ * SYNOPSIS
+ *
+ *    typedef void (*SilcMessagePayloadEncoded)(const SilcBuffer message,
+ *                                              void *context);
+ *
+ * DESCRIPTION
+ *
+ *    This callback is given as arugment to silc_message_payload_encode
+ *    and will be called when the message payload has been encoded.  If
+ *    `message' is NULL, encoding failed.
+ *
+ ***/
+typedef void (*SilcMessagePayloadEncoded)(SilcBuffer message,
+                                         void *context);
+
 /****f* silccore/SilcMessageAPI/silc_message_payload_encode
  *
  * SYNOPSIS
  *
- *    SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
- *                                           const unsigned char *data,
- *                                           SilcUInt32 data_len,
- *                                           SilcBool generate_iv,
- *                                           SilcBool private_message,
- *                                           SilcCipher cipher,
- *                                           SilcHmac hmac,
- *                                           SilcRng rng,
- *                                           SilcPublicKey public_key,
- *                                           SilcPrivateKey private_key,
- *                                           SilcHash hash,
- *                                           SilcBuffer buffer);
+ *    SilcAsyncOperation
+ *    silc_message_payload_encode(SilcMessageFlags flags,
+ *                                const unsigned char *data,
+ *                                SilcUInt32 data_len,
+ *                                SilcBool generate_iv,
+ *                                SilcBool private_message,
+ *                                SilcCipher cipher,
+ *                                SilcHmac hmac,
+ *                                SilcRng rng,
+ *                                SilcPublicKey public_key,
+ *                                SilcPrivateKey private_key,
+ *                                SilcHash hash,
+ *                                SilcID *sender_id,
+ *                                SilcID *receiver_id,
+ *                                SilcStack stack,
+ *                                SilcMessagePayloadEncoded encoded,
+ *                                void *context);
  *
  * DESCRIPTION
  *
- *    Encodes a Message Payload into a buffer and returns it.  This is
- *    used to encode channel messages and private messages into a packet.
- *    If `private_message' is FALSE then this encodes channel message, if
- *    it is TRUE this encodes private message.  If `private_message' is
- *    TRUE then `generate_iv' MUST be FALSE if the private message key
- *    `cipher' is not static key (pre-shared key).  If it is static key
- *    then protocol dictates that IV must be present in the Message Payload
+ *    Encodes a Message Payload into a buffer and returns it to the `encoded'
+ *    callback.  This is used to encode channel messages and private messages
+ *    into a packet.  If `private_message' is FALSE then this encodes channel
+ *    message, if it is TRUE this encodes private message.  If
+ *    `private_message' is TRUE then `generate_iv' MUST be FALSE if the private
+ *    message key `cipher' is not static key (pre-shared key).  If it is static
+ *    key then protocol dictates that IV must be present in the Message Payload
  *    and `generate_iv' must be TRUE.  The caller must know whether the key
  *    is static or not for private messages.  If the key was generated with
  *    Key Agreement protocol then `generate_iv' is always FALSE.  For
@@ -248,24 +298,31 @@ SilcBool silc_message_payload_encrypt(unsigned char *data,
  *    be included in the message.  The `private_message' and `hash' MUST
  *    be provided.  The `hash' SHOULD be SHA1.
  *
- *    If the `buffer' is non-NULL then the payload will be encoded into
- *    that buffer.  The same buffer is returned.  Otherwise new buffer is
- *    allocated and returned.  The `buffer' will be automatically enlarged
- *    if the payload does not fit to it.
+ *    The `sender_id' is the ID message sender and `receiver_id' is ID of
+ *    message receiver.
+ *
+ *    If `stack' is non-NULL the message payload is allocated from stack.
+ *    The memory will be returned back to `stack' after the `encoded' has
+ *    been called.
  *
  ***/
-SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
-                                      const unsigned char *data,
-                                      SilcUInt32 data_len,
-                                      SilcBool generate_iv,
-                                      SilcBool private_message,
-                                      SilcCipher cipher,
-                                      SilcHmac hmac,
-                                      SilcRng rng,
-                                      SilcPublicKey public_key,
-                                      SilcPrivateKey private_key,
-                                      SilcHash hash,
-                                      SilcBuffer buffer);
+SilcAsyncOperation
+silc_message_payload_encode(SilcMessageFlags flags,
+                           const unsigned char *data,
+                           SilcUInt32 data_len,
+                           SilcBool generate_iv,
+                           SilcBool private_message,
+                           SilcCipher cipher,
+                           SilcHmac hmac,
+                           SilcRng rng,
+                           SilcPublicKey public_key,
+                           SilcPrivateKey private_key,
+                           SilcHash hash,
+                           SilcID *sender_id,
+                           SilcID *receiver_id,
+                           SilcStack stack,
+                           SilcMessagePayloadEncoded encoded,
+                           void *context);
 
 /****f* silccore/SilcMessageAPI/silc_message_payload_free
  *
@@ -329,21 +386,27 @@ unsigned char *silc_message_get_mac(SilcMessagePayload payload);
  *
  * SYNOPSIS
  *
- *    int silc_message_signed_verify(SilcMessagePayload message,
- *                                   SilcPublicKey remote_public_key,
- *                                   SilcHash hash);
+ *    SilcAsyncOperation
+ *    silc_message_signed_verify(SilcMessagePayload message,
+ *                               SilcPublicKey remote_public_key,
+ *                               SilcHash hash,
+ *                               SilcAuthResultCb result,
+ *                               void *context);
+ *
  *
  * DESCRIPTION
  *
  *    This routine can be used to verify the digital signature from the
  *    message indicated by `message'.  The signature is present only if
- *    the SILC_MESSAGE_FLAG_SIGNED is set in the message flags.  This
- *    returns SILC_AUTH_OK if the signature verification was successful.
+ *    the SILC_MESSAGE_FLAG_SIGNED is set in the message flags.  The
+ *    result of the verification is returned to `result' callback.
  *
  ***/
-int silc_message_signed_verify(SilcMessagePayload message,
-                              SilcPublicKey remote_public_key,
-                              SilcHash hash);
+SilcAsyncOperation silc_message_signed_verify(SilcMessagePayload message,
+                                             SilcPublicKey remote_public_key,
+                                             SilcHash hash,
+                                             SilcAuthResultCb result,
+                                             void *context);
 
 /****f* silccore/SilcMessageAPI/silc_message_signed_get_public_key
  *