5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2006 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 * SilcBool check_mac);
100 * Decrypt Message Payload indicated by `data'. If the payload is
101 * channel message then `private_message' is FALSE, and if it is
102 * private message it is TRUE. If the private message key is static
103 * (pre-shared key) then protocol dictates that the IV is present
104 * and `static_key' must be set to TRUE. If the key is not static
105 * (Key Agreement was done for the key) then it MUST be FALSE. For
106 * channel messages the `static_key' is ignored.
108 * This is usually used by the Message Payload interface itself but can
109 * be called by the appliation if separate decryption process is required.
110 * For example server might need to call this directly in some
111 * circumstances. The `cipher' is used to decrypt the payload. If
112 * `check_mac' is FALSE then MAC is not verified.
115 SilcBool silc_message_payload_decrypt(unsigned char *data,
117 SilcBool private_message,
123 /****f* silccore/SilcMessageAPI/silc_message_payload_parse
128 * silc_message_payload_parse(unsigned char *payload,
129 * SilcUInt32 payload_len,
130 * SilcBool private_message,
131 * SilcBool static_key,
135 * SilcBool no_allocation,
136 * SilcMessagePayload message);
140 * Parses Message Payload returning new payload structure. This also
141 * decrypts the payload and checks the MAC. If the payload is
142 * channel message then `private_message' is FALSE, and if it is
143 * private message it is TRUE. If the private message key is static
144 * (pre-shared key) then protocol dictates that the IV is present
145 * and `static_key' must be set to TRUE. If the key is not static
146 * (Key Agreement was done for the key) then it MUST be FALSE. For
147 * channel messages the `static_key' is ignored.
149 * If the `hmac' is no provided then the MAC of the channel message is
150 * not verified. If the message is private message and `cipher' is NULL
151 * then this assumes that the packet was decrypted with session keys
152 * (no private message key) and this merely decodes the payload.
154 * If the `message' is non-NULL then that pre-allocated context is
155 * used in parsing. Same context is returned. Otherwise new context
156 * is allocated and returned. If the `stack' is non-NULL then memory
157 * is allocated from that stack. If `no_allocation' is TRUE then the
158 * `message' must be provided and data is merely parsed and referenced
159 * from `payload' and will become invalid when `payload' invalidates.
160 * If `no_allocation' is TRUE the routine does not do any allocations.
164 silc_message_payload_parse(unsigned char *payload,
165 SilcUInt32 payload_len,
166 SilcBool private_message,
171 SilcBool no_allocation,
172 SilcMessagePayload message);
174 /****f* silccore/SilcMessageAPI/silc_message_payload_encrypt
178 * SilcBool silc_message_payload_encrypt(unsigned char *data,
179 * SilcUInt32 data_len,
180 * SilcUInt32 true_len,
187 * This function is used to encrypt the Messsage Payload which is
188 * the `data' and `data_len'. The `data_len' is the data length which
189 * is used to create MAC out of. The `data' MUST have additional space
190 * after `true_len' bytes for the MAC which is appended to the data.
192 * This is usually used by the Message Payload interface itself but can
193 * be called by the appliation if separate encryption process is required.
194 * For example server might need to call this directly in some
195 * circumstances. The `cipher' is used to encrypt the payload and `hmac'
196 * to compute the MAC for the payload.
199 SilcBool silc_message_payload_encrypt(unsigned char *data,
206 /****f* silccore/SilcMessageAPI/silc_message_payload_encode
210 * SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
211 * const unsigned char *data,
212 * SilcUInt32 data_len,
213 * SilcBool generate_iv,
214 * SilcBool private_message,
218 * SilcPublicKey public_key,
219 * SilcPrivateKey private_key,
221 * SilcBuffer buffer);
225 * Encodes a Message Payload into a buffer and returns it. This is
226 * used to encode channel messages and private messages into a packet.
227 * If `private_message' is FALSE then this encodes channel message, if
228 * it is TRUE this encodes private message. If `private_message' is
229 * TRUE then `generate_iv' MUST be FALSE if the private message key
230 * `cipher' is not static key (pre-shared key). If it is static key
231 * then protocol dictates that IV must be present in the Message Payload
232 * and `generate_iv' must be TRUE. The caller must know whether the key
233 * is static or not for private messages. If the key was generated with
234 * Key Agreement protocol then `generate_iv' is always FALSE. For
235 * channel messages `generate_iv' is always set to TRUE value.
237 * The `cipher' is the cipher used to encrypt the message and `hmac'
238 * is used to compute the MAC for the payload. If encoding private
239 * message that will be encrypted with session keys (no private message
240 * key) then `cipher' and `hmac' is NULL and this merely encodes the
241 * payload buffer, and the caller must encrypt the packet later.
242 * If `rng' is NULL then global RNG is used, if non-NULL then the
243 * `rng' is used (for IV and padding generation).
245 * The `public_key', `private_key' and `hash' are provided only if the
246 * flags includes SILC_MESSAGE_FLAG_SIGNED, in which case the message
247 * will be digitally signed. If `public_key' is non-NULL then it will
248 * be included in the message. The `private_message' and `hash' MUST
249 * be provided. The `hash' SHOULD be SHA1.
251 * If the `buffer' is non-NULL then the payload will be encoded into
252 * that buffer. The same buffer is returned. Otherwise new buffer is
253 * allocated and returned. The `buffer' will be automatically enlarged
254 * if the payload does not fit to it.
257 SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
258 const unsigned char *data,
260 SilcBool generate_iv,
261 SilcBool private_message,
265 SilcPublicKey public_key,
266 SilcPrivateKey private_key,
270 /****f* silccore/SilcMessageAPI/silc_message_payload_free
274 * void silc_message_payload_free(SilcMessagePayload payload);
278 * Free's Message Payload and all data in it.
281 void silc_message_payload_free(SilcMessagePayload payload);
283 /****f* silccore/SilcMessageAPI/silc_message_get_flags
287 * SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
291 * Returns the message flags from the payload.
294 SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
296 /****f* silccore/SilcMessageAPI/silc_message_get_data
301 * silc_message_get_data(SilcMessagePayload payload,
302 * SilcUInt32 *data_len);
306 * Return the data in the payload, that is, the actual message data.
307 * The caller must not free it.
310 unsigned char *silc_message_get_data(SilcMessagePayload payload,
311 SilcUInt32 *data_len);
313 /****f* silccore/SilcMessageAPI/silc_message_get_mac
318 * silc_message_get_mac(SilcMessagePayload payload);
322 * Return the MAC of the payload. The caller must already know the
323 * length of the MAC. The caller must not free the MAC.
326 unsigned char *silc_message_get_mac(SilcMessagePayload payload);
328 /****f* silccore/SilcMessageAPI/silc_message_signed_verify
333 * silc_message_signed_verify(SilcMessagePayload message,
334 * SilcPublicKey remote_public_key,
339 * This routine can be used to verify the digital signature from the
340 * message indicated by `message'. The signature is present only if
341 * the SILC_MESSAGE_FLAG_SIGNED is set in the message flags. This
342 * returns SILC_AUTH_OK if the signature verification was successful.
345 SilcAuthResult silc_message_signed_verify(SilcMessagePayload message,
346 SilcPublicKey remote_public_key,
349 /****f* silccore/SilcMessageAPI/silc_message_signed_get_public_key
354 * silc_message_signed_get_public_key(SilcMessagePayload payload,
355 * const unsigned char **pk_data,
356 * SilcUInt32 *pk_data_len);
360 * Returns the decoded SilcPublicKey from the message payload or NULL
361 * if it does not include public key. The caller must free the returned
362 * public key pointer. This also returns the raw public key (before
363 * decoding) into `pk_data' and `pk_data_len' if they are provided. The
364 * caller must not free these pointers.
368 silc_message_signed_get_public_key(SilcMessagePayload payload,
369 const unsigned char **pk_data,
370 SilcUInt32 *pk_data_len);
372 #include "silcmessage_i.h"
374 #endif /* SILCMESSAGE_H */