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/silc_message_payload_encode
238 * SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
239 * const unsigned char *data,
240 * SilcUInt32 data_len,
241 * SilcBool generate_iv,
242 * SilcBool private_message,
246 * SilcPublicKey public_key,
247 * SilcPrivateKey private_key,
250 * SilcID *receiver_id,
251 * SilcBuffer buffer);
255 * Encodes a Message Payload into a buffer and returns it. This is
256 * used to encode channel messages and private messages into a packet.
257 * If `private_message' is FALSE then this encodes channel message, if
258 * it is TRUE this encodes private message. If `private_message' is
259 * TRUE then `generate_iv' MUST be FALSE if the private message key
260 * `cipher' is not static key (pre-shared key). If it is static key
261 * then protocol dictates that IV must be present in the Message Payload
262 * and `generate_iv' must be TRUE. The caller must know whether the key
263 * is static or not for private messages. If the key was generated with
264 * Key Agreement protocol then `generate_iv' is always FALSE. For
265 * channel messages `generate_iv' is always set to TRUE value.
267 * The `cipher' is the cipher used to encrypt the message and `hmac'
268 * is used to compute the MAC for the payload. If encoding private
269 * message that will be encrypted with session keys (no private message
270 * key) then `cipher' and `hmac' is NULL and this merely encodes the
271 * payload buffer, and the caller must encrypt the packet later.
272 * If `rng' is NULL then global RNG is used, if non-NULL then the
273 * `rng' is used (for IV and padding generation).
275 * The `public_key', `private_key' and `hash' are provided only if the
276 * flags includes SILC_MESSAGE_FLAG_SIGNED, in which case the message
277 * will be digitally signed. If `public_key' is non-NULL then it will
278 * be included in the message. The `private_message' and `hash' MUST
279 * be provided. The `hash' SHOULD be SHA1.
281 * The `sender_id' is the ID message sender and `receiver_id' is ID of
284 * If the `buffer' is non-NULL then the payload will be encoded into
285 * that buffer. The same buffer is returned. Otherwise new buffer is
286 * allocated and returned. The `buffer' will be automatically enlarged
287 * if the payload does not fit to it.
290 SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
291 const unsigned char *data,
293 SilcBool generate_iv,
294 SilcBool private_message,
298 SilcPublicKey public_key,
299 SilcPrivateKey private_key,
305 /****f* silccore/SilcMessageAPI/silc_message_payload_free
309 * void silc_message_payload_free(SilcMessagePayload payload);
313 * Free's Message Payload and all data in it.
316 void silc_message_payload_free(SilcMessagePayload payload);
318 /****f* silccore/SilcMessageAPI/silc_message_get_flags
322 * SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
326 * Returns the message flags from the payload.
329 SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
331 /****f* silccore/SilcMessageAPI/silc_message_get_data
336 * silc_message_get_data(SilcMessagePayload payload,
337 * SilcUInt32 *data_len);
341 * Return the data in the payload, that is, the actual message data.
342 * The caller must not free it.
345 unsigned char *silc_message_get_data(SilcMessagePayload payload,
346 SilcUInt32 *data_len);
348 /****f* silccore/SilcMessageAPI/silc_message_get_mac
353 * silc_message_get_mac(SilcMessagePayload payload);
357 * Return the MAC of the payload. The caller must already know the
358 * length of the MAC. The caller must not free the MAC.
361 unsigned char *silc_message_get_mac(SilcMessagePayload payload);
363 /****f* silccore/SilcMessageAPI/silc_message_signed_verify
368 * silc_message_signed_verify(SilcMessagePayload message,
369 * SilcPublicKey remote_public_key,
374 * This routine can be used to verify the digital signature from the
375 * message indicated by `message'. The signature is present only if
376 * the SILC_MESSAGE_FLAG_SIGNED is set in the message flags. This
377 * returns SILC_AUTH_OK if the signature verification was successful.
380 SilcAuthResult silc_message_signed_verify(SilcMessagePayload message,
381 SilcPublicKey remote_public_key,
384 /****f* silccore/SilcMessageAPI/silc_message_signed_get_public_key
389 * silc_message_signed_get_public_key(SilcMessagePayload payload,
390 * const unsigned char **pk_data,
391 * SilcUInt32 *pk_data_len);
395 * Returns the decoded SilcPublicKey from the message payload or NULL
396 * if it does not include public key. The caller must free the returned
397 * public key pointer. This also returns the raw public key (before
398 * decoding) into `pk_data' and `pk_data_len' if they are provided. The
399 * caller must not free these pointers.
403 silc_message_signed_get_public_key(SilcMessagePayload payload,
404 const unsigned char **pk_data,
405 SilcUInt32 *pk_data_len);
407 #include "silcmessage_i.h"
409 #endif /* SILCMESSAGE_H */