5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silccore/SILC Message Interface
24 * This interface includes the implementation of the Message Payload that
25 * is used to send private messages and channel messages. The interface
26 * is also able to automatically provide digital signature in the messages
27 * if it is requested. Message digital signatures may also be verified with
35 /****s* silccore/SilcMessageAPI/SilcMessagePayload
39 * typedef struct SilcMessagePayloadObject
40 * *SilcMessagePayload, SilcMessagePayloadStruct;
45 * This context is the actual Message Payload and is allocated
46 * by silc_message_payload_parse and given as argument usually
47 * to all silc_message_* functions. It is freed by the
48 * silc_message_payload_free function.
51 typedef struct SilcMessagePayloadObject
52 *SilcMessagePayload, SilcMessagePayloadStruct;
54 /****d* silccore/SilcMessageAPI/SilcMessageFlags
58 * typedef SilcUInt16 SilcMessageFlags;
62 * The message flags type definition and the message flags. The
63 * message flags are used to indicate some status of the message.
67 typedef SilcUInt16 SilcMessageFlags;
69 /* The message flags */
70 #define SILC_MESSAGE_FLAG_NONE 0x0000 /* No flags */
71 #define SILC_MESSAGE_FLAG_AUTOREPLY 0x0001 /* Automatically replied */
72 #define SILC_MESSAGE_FLAG_NOREPLY 0x0002 /* Send no reply to this */
73 #define SILC_MESSAGE_FLAG_ACTION 0x0004 /* Action message */
74 #define SILC_MESSAGE_FLAG_NOTICE 0x0008 /* Notice message */
75 #define SILC_MESSAGE_FLAG_REQUEST 0x0010 /* A request */
76 #define SILC_MESSAGE_FLAG_SIGNED 0x0020 /* Message is signed */
77 #define SILC_MESSAGE_FLAG_REPLY 0x0040 /* A reply */
78 #define SILC_MESSAGE_FLAG_DATA 0x0080 /* MIME object */
79 #define SILC_MESSAGE_FLAG_UTF8 0x0100 /* UTF-8 string */
80 #define SILC_MESSAGE_FLAG_ACK 0x0200 /* ACK messages */
81 #define SILC_MESSAGE_FLAG_STOP 0x0400 /* Stop indication */
82 #define SILC_MESSAGE_FLAG_RESERVED 0x0800 /* to 0x1000 */
83 #define SILC_MESSAGE_FLAG_PRIVATE 0x2000 /* to 0x8000 */
86 /****f* silccore/SilcMessageAPI/silc_message_payload_decrypt
90 * SilcBool silc_message_payload_decrypt(unsigned char *data,
92 * SilcBool private_message,
93 * SilcBool static_key,
96 * unsigned char *sender_id,
97 * SilcUInt32 sender_id_len,
98 * unsigned char *receiver_id,
99 * SilcUInt32 receiver_id_len,
100 * SilcBool check_mac);
104 * Decrypt Message Payload indicated by `data'. If the payload is
105 * channel message then `private_message' is FALSE, and if it is
106 * private message it is TRUE. If the private message key is static
107 * (pre-shared key) then protocol dictates that the IV is present
108 * and `static_key' must be set to TRUE. If the key is not static
109 * (Key Agreement was done for the key) then it MUST be FALSE. For
110 * channel messages the `static_key' is ignored.
112 * The `sender_id' and `receiver_id' are the IDs from the packet header
113 * of the packet where this message payload was received.
115 * This is usually used by the Message Payload interface itself but can
116 * be called by the appliation if separate decryption process is required.
117 * For example server might need to call this directly in some
118 * circumstances. The `cipher' is used to decrypt the payload. If
119 * `check_mac' is FALSE then MAC is not verified.
122 SilcBool silc_message_payload_decrypt(unsigned char *data,
124 SilcBool private_message,
128 unsigned char *sender_id,
129 SilcUInt32 sender_id_len,
130 unsigned char *receiver_id,
131 SilcUInt32 receiver_id_len,
134 /****f* silccore/SilcMessageAPI/silc_message_payload_parse
139 * silc_message_payload_parse(unsigned char *payload,
140 * SilcUInt32 payload_len,
141 * SilcBool private_message,
142 * SilcBool static_key,
145 * unsigned char *sender_id,
146 * SilcUInt32 sender_id_len,
147 * unsigned char *receiver_id,
148 * SilcUInt32 receiver_id_len,
150 * SilcBool no_allocation,
151 * SilcMessagePayload message);
155 * Parses Message Payload returning new payload structure. This also
156 * decrypts the payload and checks the MAC. If the payload is
157 * channel message then `private_message' is FALSE, and if it is
158 * private message it is TRUE. If the private message key is static
159 * (pre-shared key) then protocol dictates that the IV is present
160 * and `static_key' must be set to TRUE. If the key is not static
161 * (Key Agreement was done for the key) then it MUST be FALSE. For
162 * channel messages the `static_key' is ignored.
164 * If the `hmac' is no provided then the MAC of the channel message is
165 * not verified. If the message is private message and `cipher' is NULL
166 * then this assumes that the packet was decrypted with session keys
167 * (no private message key) and this merely decodes the payload.
169 * The `sender_id' and `receiver_id' are the IDs from the packet header
170 * of the packet where this message payload was received.
172 * If the `message' is non-NULL then that pre-allocated context is
173 * used in parsing. Same context is returned. Otherwise new context
174 * is allocated and returned. If the `stack' is non-NULL then memory
175 * is allocated from that stack. If `no_allocation' is TRUE then the
176 * `message' must be provided and data is merely parsed and referenced
177 * from `payload' and will become invalid when `payload' invalidates.
178 * If `no_allocation' is TRUE the routine does not do any allocations.
182 silc_message_payload_parse(unsigned char *payload,
183 SilcUInt32 payload_len,
184 SilcBool private_message,
188 unsigned char *sender_id,
189 SilcUInt32 sender_id_len,
190 unsigned char *receiver_id,
191 SilcUInt32 receiver_id_len,
193 SilcBool no_allocation,
194 SilcMessagePayload message);
196 /****f* silccore/SilcMessageAPI/silc_message_payload_encrypt
200 * SilcBool silc_message_payload_encrypt(unsigned char *data,
201 * SilcUInt32 data_len,
202 * SilcUInt32 true_len,
205 * SilcID *receiver_id,
211 * This function is used to encrypt the Messsage Payload which is
212 * the `data' and `data_len'. The `data_len' is the data length which
213 * is used to create MAC out of. The `data' MUST have additional space
214 * after `true_len' bytes for the MAC which is appended to the data.
215 * The `sender_id' is the ID message sender and `receiver_id' is ID of
218 * This is usually used by the Message Payload interface itself but can
219 * be called by the appliation if separate encryption process is required.
220 * For example server might need to call this directly in some
221 * circumstances. The `cipher' is used to encrypt the payload and `hmac'
222 * to compute the MAC for the payload.
225 SilcBool silc_message_payload_encrypt(unsigned char *data,
234 /****f* silccore/SilcMessageAPI/SilcMessagePayloadEncoded
238 * typedef void (*SilcMessagePayloadEncoded)(const SilcBuffer message,
243 * This callback is given as arugment to silc_message_payload_encode
244 * and will be called when the message payload has been encoded. If
245 * `message' is NULL, encoding failed.
248 typedef void (*SilcMessagePayloadEncoded)(SilcBuffer message,
251 /****f* silccore/SilcMessageAPI/silc_message_payload_encode
256 * silc_message_payload_encode(SilcMessageFlags flags,
257 * const unsigned char *data,
258 * SilcUInt32 data_len,
259 * SilcBool generate_iv,
260 * SilcBool private_message,
264 * SilcPublicKey public_key,
265 * SilcPrivateKey private_key,
268 * SilcID *receiver_id,
270 * SilcMessagePayloadEncoded encoded,
275 * Encodes a Message Payload into a buffer and returns it to the `encoded'
276 * callback. This is used to encode channel messages and private messages
277 * into a packet. If `private_message' is FALSE then this encodes channel
278 * message, if it is TRUE this encodes private message. If
279 * `private_message' is TRUE then `generate_iv' MUST be FALSE if the private
280 * message key `cipher' is not static key (pre-shared key). If it is static
281 * key then protocol dictates that IV must be present in the Message Payload
282 * and `generate_iv' must be TRUE. The caller must know whether the key
283 * is static or not for private messages. If the key was generated with
284 * Key Agreement protocol then `generate_iv' is always FALSE. For
285 * channel messages `generate_iv' is always set to TRUE value.
287 * The `cipher' is the cipher used to encrypt the message and `hmac'
288 * is used to compute the MAC for the payload. If encoding private
289 * message that will be encrypted with session keys (no private message
290 * key) then `cipher' and `hmac' is NULL and this merely encodes the
291 * payload buffer, and the caller must encrypt the packet later.
292 * If `rng' is NULL then global RNG is used, if non-NULL then the
293 * `rng' is used (for IV and padding generation).
295 * The `public_key', `private_key' and `hash' are provided only if the
296 * flags includes SILC_MESSAGE_FLAG_SIGNED, in which case the message
297 * will be digitally signed. If `public_key' is non-NULL then it will
298 * be included in the message. The `private_message' and `hash' MUST
299 * be provided. The `hash' SHOULD be SHA1.
301 * The `sender_id' is the ID message sender and `receiver_id' is ID of
304 * If `stack' is non-NULL the message payload is allocated from stack.
305 * The memory will be returned back to `stack' after the `encoded' has
310 silc_message_payload_encode(SilcMessageFlags flags,
311 const unsigned char *data,
313 SilcBool generate_iv,
314 SilcBool private_message,
318 SilcPublicKey public_key,
319 SilcPrivateKey private_key,
324 SilcMessagePayloadEncoded encoded,
327 /****f* silccore/SilcMessageAPI/silc_message_payload_free
331 * void silc_message_payload_free(SilcMessagePayload payload);
335 * Free's Message Payload and all data in it.
338 void silc_message_payload_free(SilcMessagePayload payload);
340 /****f* silccore/SilcMessageAPI/silc_message_get_flags
344 * SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
348 * Returns the message flags from the payload.
351 SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
353 /****f* silccore/SilcMessageAPI/silc_message_get_data
358 * silc_message_get_data(SilcMessagePayload payload,
359 * SilcUInt32 *data_len);
363 * Return the data in the payload, that is, the actual message data.
364 * The caller must not free it.
367 unsigned char *silc_message_get_data(SilcMessagePayload payload,
368 SilcUInt32 *data_len);
370 /****f* silccore/SilcMessageAPI/silc_message_get_mac
375 * silc_message_get_mac(SilcMessagePayload payload);
379 * Return the MAC of the payload. The caller must already know the
380 * length of the MAC. The caller must not free the MAC.
383 unsigned char *silc_message_get_mac(SilcMessagePayload payload);
385 /****f* silccore/SilcMessageAPI/silc_message_signed_verify
390 * silc_message_signed_verify(SilcMessagePayload message,
391 * SilcPublicKey remote_public_key,
393 * SilcAuthResultCb result,
399 * This routine can be used to verify the digital signature from the
400 * message indicated by `message'. The signature is present only if
401 * the SILC_MESSAGE_FLAG_SIGNED is set in the message flags. The
402 * result of the verification is returned to `result' callback.
405 SilcAsyncOperation silc_message_signed_verify(SilcMessagePayload message,
406 SilcPublicKey remote_public_key,
408 SilcAuthResultCb result,
411 /****f* silccore/SilcMessageAPI/silc_message_signed_get_public_key
416 * silc_message_signed_get_public_key(SilcMessagePayload payload,
417 * const unsigned char **pk_data,
418 * SilcUInt32 *pk_data_len);
422 * Returns the decoded SilcPublicKey from the message payload or NULL
423 * if it does not include public key. The caller must free the returned
424 * public key pointer. This also returns the raw public key (before
425 * decoding) into `pk_data' and `pk_data_len' if they are provided. The
426 * caller must not free these pointers.
430 silc_message_signed_get_public_key(SilcMessagePayload payload,
431 const unsigned char **pk_data,
432 SilcUInt32 *pk_data_len);
434 #include "silcmessage_i.h"
436 #endif /* SILCMESSAGE_H */