updates.
[silc.git] / lib / silccore / silcchannel.h
1 /****h* silccore/silcchannel.h
2  *
3  * NAME
4  *
5  * silcchannel.h
6  *
7  * COPYRIGHT
8  *
9  * Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
10  *
11  * Copyright (C) 1997 - 2001 Pekka Riikonen
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * DESCRIPTION
24  *
25  * Implementations of the Channel Payload, Channel Message Payload and
26  * Channel Key Payload.  The Channel Payload represents new channel and
27  * is used to distribute the information of the new channel.  The Channel
28  * Message Payload is used to deliver messages to the channel.  The routines
29  * for Channel Message Payload also handles the encryption and decryption
30  * of the payload.  Last, the Channel Key Payload is used to distribute
31  * a new key to the channel.  It is done for example every time someone
32  * joins a channel or the old key expires.
33  *
34  ***/
35
36 #ifndef SILCCHANNEL_H
37 #define SILCCHANNEL_H
38
39 #include "silcdlist.h"
40
41 /****s* silccore/SilcChannelAPI/SilcChannelPayload
42  *
43  * NAME
44  * 
45  *    typedef struct SilcChannelPayloadStruct *SilcChannelPayload;
46  *
47  * DESCRIPTION
48  *
49  *    This context is the actual Channel Payload and is allocated
50  *    by silc_channel_payload_parse and given as argument usually to
51  *    all silc_channel_payload_* functions.  It is freed by the
52  *    silc_channel_payload_free function.
53  *
54  ***/
55 typedef struct SilcChannelPayloadStruct *SilcChannelPayload;
56
57 /****s* silccore/SilcChannelAPI/SilcChannelMessagePayload
58  *
59  * NAME
60  * 
61  *    typedef struct 
62  *    SilcChannelMessagePayloadStruct *SilcChannelMessagePayload;
63  *
64  * DESCRIPTION
65  *
66  *    This context is the actual Channel Message Payload and is allocated
67  *    by silc_channel_message_payload_parse and given as argument usually to
68  *    all silc_channel_message_payload_* functions.  It is freed by the
69  *    silc_channel_message_payload_free function.
70  *
71  ***/
72 typedef struct SilcChannelMessagePayloadStruct *SilcChannelMessagePayload;
73
74 /****s* silccore/SilcChannelAPI/SilcChannelKeyPayload
75  *
76  * NAME
77  * 
78  *    typedef struct SilcChannelKeyPayloadStruct *SilcChannelKeyPayload;
79  *
80  * DESCRIPTION
81  *
82  *    This context is the actual Channel Key Payload and is allocated
83  *    by silc_channel_key_payload_parse and given as argument usually to
84  *    all silc_channel_key_payload_* functions.  It is freed by the
85  *    silc_channel_key_payload_free function.
86  *
87  ***/
88 typedef struct SilcChannelKeyPayloadStruct *SilcChannelKeyPayload;
89
90 /****d* silccore/SilcChannelAPI/SilcMessageFlags
91  *
92  * NAME
93  * 
94  *    typedef uint16 SilcMessageFlags;
95  *
96  * DESCRIPTION
97  *
98  *    The message flags type definition and the message flags.  The 
99  *    message flags are used to indicate some status of the message.
100  *    These flags are also used by the private message interfaces.
101  *
102  * SOURCE
103  */
104 typedef uint16 SilcMessageFlags;
105
106 /* The message flags (shared by both channel and private messages) */
107 #define SILC_MESSAGE_FLAG_NONE        0x0000
108 #define SILC_MESSAGE_FLAG_AUTOREPLY   0x0001
109 #define SILC_MESSAGE_FLAG_NOREPLY     0x0002
110 #define SILC_MESSAGE_FLAG_ACTION      0x0004
111 #define SILC_MESSAGE_FLAG_NOTICE      0x0008
112 #define SILC_MESSAGE_FLAG_REQUEST     0x0010
113 #define SILC_MESSAGE_FLAG_RESERVED    0x0020 /* to 0x0200 */
114 #define SILC_MESSAGE_FLAG_PRIVATE     0x0400 /* to 0x8000 */
115 /***/
116
117 /* Prototypes */
118
119 /****f* silccore/SilcChannelAPI/silc_channel_payload_parse
120  *
121  * SYNOPSIS
122  *
123  *    SilcChannelPayload silc_channel_payload_parse(SilcBuffer buffer);
124  *
125  * DESCRIPTION
126  *
127  *    Parses channel payload returning new channel payload structure. The
128  *    `buffer' is the raw payload buffer.
129  *
130  ***/
131 SilcChannelPayload silc_channel_payload_parse(SilcBuffer buffer);
132
133 /****f* silccore/SilcChannelAPI/silc_channel_payload_parse_list
134  *
135  * SYNOPSIS
136  *
137  *    SilcDList silc_channel_payload_parse_list(SilcBuffer buffer);
138  *
139  * DESCRIPTION
140  *
141  *    Parses list of channel payloads returning list of payloads. This
142  *    is equivalent to the silc_channel_payload_parse except that the `buffer'
143  *    now includes multiple Channel Payloads one after the other.
144  *
145  ***/
146 SilcDList silc_channel_payload_parse_list(SilcBuffer buffer);
147
148 /****f* silccore/SilcChannelAPI/silc_channel_payload_encode
149  *
150  * SYNOPSIS
151  *
152  *    SilcBuffer silc_channel_payload_encode(unsigned char *channel_name,
153  *                                           uint16 channel_name_len,
154  *                                           unsigned char *channel_id,
155  *                                           uint32 channel_id_len,
156  *                                           uint32 mode);
157  *
158  * DESCRIPTION
159  *
160  *    Encode new channel payload and returns it as buffer.
161  *
162  ***/
163 SilcBuffer silc_channel_payload_encode(unsigned char *channel_name,
164                                        uint16 channel_name_len,
165                                        unsigned char *channel_id,
166                                        uint32 channel_id_len,
167                                        uint32 mode);
168
169 /****f* silccore/SilcChannelAPI/silc_channel_payload_free
170  *
171  * SYNOPSIS
172  *
173  *    void silc_channel_payload_free(SilcChannelPayload payload);
174  *
175  * DESCRIPTION
176  *
177  *    Frees Channel Payload and all data in it.
178  *
179  ***/
180 void silc_channel_payload_free(SilcChannelPayload payload);
181
182 /****f* silccore/SilcChannelAPI/silc_channel_payload_list_free
183  *
184  * SYNOPSIS
185  *
186  *    void silc_channel_payload_list_free(SilcDList list);
187  *
188  * DESCRIPTION
189  *
190  *    Frees list of Channel Payloads and all data in them.
191  *
192  ***/
193 void silc_channel_payload_list_free(SilcDList list);
194
195 /****f* silccore/SilcChannelAPI/silc_channel_get_name
196  *
197  * SYNOPSIS
198  *
199  *    unsigned char *silc_channel_get_name(SilcChannelPayload payload,
200  *                                         uint32 *channel_name_len);
201  *
202  * DESCRIPTION
203  *
204  *    Return the channel name from the payload. The caller must not free it.
205  *
206  ***/
207 unsigned char *silc_channel_get_name(SilcChannelPayload payload,
208                                      uint32 *channel_name_len);
209
210 /****f* silccore/SilcChannelAPI/silc_channel_get_id
211  *
212  * SYNOPSIS
213  *
214  *    unsigned char *silc_channel_get_id(SilcChannelPayload payload,
215  *                                       uint32 *channel_id_len);
216  *
217  * DESCRIPTION
218  *
219  *    Return the Channel ID data from the payload. The caller must not free it.
220  *
221  ***/
222 unsigned char *silc_channel_get_id(SilcChannelPayload payload,
223                                    uint32 *channel_id_len);
224
225 /****f* silccore/SilcChannelAPI/silc_channel_get_id_parse
226  *
227  * SYNOPSIS
228  *
229  *    SilcChannelID *silc_channel_get_id_parse(SilcChannelPayload payload);
230  *
231  * DESCRIPTION
232  *
233  *    Return the Channel ID as parsed ID. This is equivalent to the
234  *    silc_channel_get_id execpt that the ID is already parsed. The caller
235  *    must free the parsed Channel ID.
236  *
237  ***/
238 SilcChannelID *silc_channel_get_id_parse(SilcChannelPayload payload);
239
240 /****f* silccore/SilcChannelAPI/silc_channel_get_mode
241  *
242  * SYNOPSIS
243  *
244  *    uint32 silc_channel_get_mode(SilcChannelPayload payload);
245  *
246  * DESCRIPTION
247  *
248  *    Return the mode. The mode is arbitrary. It can be the mode of the
249  *    channel or perhaps the mode of the client on the channel.  The protocol
250  *    dictates what the usage of the mode is in different circumstances.
251  *
252  ***/
253 uint32 silc_channel_get_mode(SilcChannelPayload payload);
254
255 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_decrypt
256  *
257  * SYNOPSIS
258  *
259  *    int silc_channel_message_payload_decrypt(unsigned char *data,
260  *                                             size_t data_len,
261  *                                             SilcCipher cipher,
262  *                                             SilcHmac hmac);
263  *
264  * DESCRIPTION
265  *
266  *    Decrypt the channel message. First push the IV out of the packet `data'.
267  *    The IV is used in the decryption process. Then decrypt the message.
268  *    After decryption, take the MAC from the decrypted packet, compute MAC
269  *    and compare the MACs.  If they match, the decryption was successful
270  *    and we have the channel message ready to be displayed.
271  *
272  *    This is usually used by the Channel Message interface itself but can
273  *    be called by the appliation if separate decryption process is required.
274  *    For example server might need to call this directly in some 
275  *    circumstances. The `cipher' is used to decrypt the payload.
276  *
277  *    If the `hmac' is no provided then the MAC of the channel message is
278  *    not verified.
279  *
280  ***/
281 int silc_channel_message_payload_decrypt(unsigned char *data,
282                                          size_t data_len,
283                                          SilcCipher cipher,
284                                          SilcHmac hmac);
285
286 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_parse
287  *
288  * SYNOPSIS
289  *
290  *    SilcChannelMessagePayload 
291  *    silc_channel_message_payload_parse(SilcBuffer buffer,
292  *                                       SilcCipher cipher,
293  *                                       SilcHmac hmac);
294  *
295  * DESCRIPTION
296  *
297  *    Parses channel message payload returning new channel payload structure.
298  *    This also decrypts it and checks the MAC. The `cipher's is used to
299  *    decrypt the payload.
300  *
301  *    If the `hmac' is no provided then the MAC of the channel message is
302  *    not verified.
303  *
304  ***/
305 SilcChannelMessagePayload 
306 silc_channel_message_payload_parse(SilcBuffer buffer,
307                                    SilcCipher cipher,
308                                    SilcHmac hmac);
309
310 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_encode
311  *
312  * SYNOPSIS
313  *
314  *    SilcBuffer silc_channel_message_payload_encode(uint16 flags,
315  *                                                   uint16 data_len,
316  *                                                   unsigned char *data,
317  *                                                   uint16 iv_len,
318  *                                                   unsigned char *iv,
319  *                                                   SilcCipher cipher,
320  *                                                   SilcHmac hmac);
321  *
322  * DESCRIPTION
323  *
324  *    Encodes channel message payload into a buffer and returns it. This
325  *    is used to add channel message payload into a packet. As the channel
326  *    payload is encrypted separately from other parts of the packet padding
327  *    must be applied to the payload. The function generates the padding
328  *    automatically from random data.  The `cipher' is the cipher used
329  *    encrypt the payload and `hmac' is used to compute the MAC for the
330  *    payload.
331  *
332  ***/
333 SilcBuffer silc_channel_message_payload_encode(uint16 flags,
334                                                uint16 data_len,
335                                                unsigned char *data,
336                                                uint16 iv_len,
337                                                unsigned char *iv,
338                                                SilcCipher cipher,
339                                                SilcHmac hmac);
340
341 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_free
342  *
343  * SYNOPSIS
344  *
345  *    void 
346  *    silc_channel_message_payload_free(SilcChannelMessagePayload payload);
347  *
348  * DESCRIPTION
349  *
350  *    Free's Channel Message Payload and all data in it.
351  *
352  ***/
353 void silc_channel_message_payload_free(SilcChannelMessagePayload payload);
354
355 /****f* silccore/SilcChannelAPI/silc_channel_message_get_flags
356  *
357  * SYNOPSIS
358  *
359  *    SilcMessageFlags
360  *    silc_channel_message_get_flags(SilcChannelMessagePayload payload);
361  *
362  * DESCRIPTION
363  *
364  *    Returns the message flags from the payload.
365  *
366  ***/
367 SilcMessageFlags
368 silc_channel_message_get_flags(SilcChannelMessagePayload payload);
369
370 /****f* silccore/SilcChannelAPI/silc_channel_message_get_data
371  *
372  * SYNOPSIS
373  *
374  *    unsigned char *
375  *    silc_channel_message_get_data(SilcChannelMessagePayload payload,
376  *                                  uint32 *data_len);
377  *
378  * DESCRIPTION
379  *
380  *    Return the data in the payload, that is, the actual channel message.
381  *    The caller must not free it.
382  *
383  ***/
384 unsigned char *silc_channel_message_get_data(SilcChannelMessagePayload payload,
385                                      uint32 *data_len);
386
387 /****f* silccore/SilcChannelAPI/silc_channel_message_get_mac
388  *
389  * SYNOPSIS
390  *
391  *    unsigned char *
392  *    silc_channel_message_get_mac(SilcChannelMessagePayload payload);
393  *
394  * DESCRIPTION
395  *
396  *    Return the MAC of the payload. The caller must already know the 
397  *    length of the MAC. The caller must not free the MAC.
398  *
399  ***/
400 unsigned char *silc_channel_message_get_mac(SilcChannelMessagePayload payload);
401
402 /****f* silccore/SilcChannelAPI/silc_channel_message_get_iv
403  *
404  * SYNOPSIS
405  *
406  *    unsigned char *
407  *    silc_channel_message_get_iv(SilcChannelMessagePayload payload);
408  *
409  * DESCRIPTION
410  *
411  *    Return the IV of the payload. The caller must already know the 
412  *    length of the IV. The caller must not free the IV.
413  *
414  ***/
415 unsigned char *silc_channel_message_get_iv(SilcChannelMessagePayload payload);
416
417 /****f* silccore/SilcChannelAPI/silc_channel_key_payload_parse
418  *
419  * SYNOPSIS
420  *
421  *    SilcChannelKeyPayload silc_channel_key_payload_parse(SilcBuffer buffer);
422  *
423  * DESCRIPTION
424  *
425  *     Parses channel key payload returning new channel key payload 
426  *     structure.
427  *
428  ***/
429 SilcChannelKeyPayload silc_channel_key_payload_parse(SilcBuffer buffer);
430
431 /****f* silccore/SilcChannelAPI/silc_channel_key_payload_encode
432  *
433  * SYNOPSIS
434  *
435  *    SilcBuffer silc_channel_key_payload_encode(uint16 id_len,
436  *                                               unsigned char *id,
437  *                                               uint16 cipher_len,
438  *                                               unsigned char *cipher,
439  *                                               uint16 key_len,
440  *                                               unsigned char *key);
441  *
442  * DESCRIPTION
443  *
444  *    Encodes channel key payload into a buffer and returns it. This is used 
445  *    to add channel key payload into a packet.
446  *
447  ***/
448 SilcBuffer silc_channel_key_payload_encode(uint16 id_len,
449                                            unsigned char *id,
450                                            uint16 cipher_len,
451                                            unsigned char *cipher,
452                                            uint16 key_len,
453                                            unsigned char *key);
454
455 /****f* silccore/SilcChannelAPI/silc_channel_key_payload_free
456  *
457  * SYNOPSIS
458  *
459  *    void silc_channel_key_payload_free(SilcChannelKeyPayload payload);
460  *
461  * DESCRIPTION
462  *
463  *    Frees the Channel Key Payload and all data in it.
464  *
465  ***/
466 void silc_channel_key_payload_free(SilcChannelKeyPayload payload);
467
468 /****f* silccore/SilcChannelAPI/silc_channel_key_get_id
469  *
470  * SYNOPSIS
471  *
472  *    unsigned char *silc_channel_key_get_id(SilcChannelKeyPayload payload, 
473  *                                           uint32 *id_len);
474  *
475  * DESCRIPTION
476  *
477  *    Return the Channel ID data from the payload. The caller must not
478  *    free it.
479  *
480  ***/
481 unsigned char *silc_channel_key_get_id(SilcChannelKeyPayload payload, 
482                                        uint32 *id_len);
483
484 /****f* silccore/SilcChannelAPI/silc_channel_key_get_cipher
485  *
486  * SYNOPSIS
487  *
488  *    unsigned char *silc_channel_key_get_cipher(SilcChannelKeyPayload payload,
489  *                                               uint32 *cipher_len);
490  *
491  * DESCRIPTION
492  *
493  *    Return the name of the cipher from the payload. The caller must not
494  *    free it.
495  *
496  ***/
497 unsigned char *silc_channel_key_get_cipher(SilcChannelKeyPayload payload,
498                                            uint32 *cipher_len);
499
500 /****f* silccore/SilcChannelAPI/silc_channel_key_get_key
501  *
502  * SYNOPSIS
503  *
504  *    unsigned char *silc_channel_key_get_key(SilcChannelKeyPayload payload,
505  *                                            uint32 *key_len);
506  *
507  * DESCRIPTION
508  *
509  *    Return the raw key material from the payload. The caller must not
510  *    free it.
511  *
512  ***/
513 unsigned char *silc_channel_key_get_key(SilcChannelKeyPayload payload,
514                                         uint32 *key_len);
515
516 #endif