Started implementing protocol version 1.1 and narrowing down
[silc.git] / lib / silccore / silcchannel.h
1 /*
2  
3   silcchannel.h
4  
5   Author: Pekka Riikonen <priikone@silcnet.org>
6  
7   Copyright (C) 1997 - 2001 Pekka Riikonen
8  
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; either version 2 of the License, or
12   (at your option) any later version.
13  
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 /****h* silccore/SILC Channel Interface
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 SilcUInt16 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 SilcUInt16 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_SIGNED      0x0020
114 #define SILC_MESSAGE_FLAG_REPLY       0x0040
115 #define SILC_MESSAGE_FLAG_DATA        0x0080
116 #define SILC_MESSAGE_FLAG_RESERVED    0x0100 /* to 0x0800 */
117 #define SILC_MESSAGE_FLAG_PRIVATE     0x1000 /* to 0x8000 */
118 /***/
119
120 /* Prototypes */
121
122 /****f* silccore/SilcChannelAPI/silc_channel_payload_parse
123  *
124  * SYNOPSIS
125  *
126  *    SilcChannelPayload 
127  *    silc_channel_payload_parse(const unsigned char *payload,
128  *                               SilcUInt32 payload_len);
129  *
130  * DESCRIPTION
131  *
132  *    Parses channel payload returning new channel payload structure. The
133  *    `buffer' is the raw payload buffer.
134  *
135  ***/
136 SilcChannelPayload silc_channel_payload_parse(const unsigned char *payload,
137                                               SilcUInt32 payload_len);
138
139 /****f* silccore/SilcChannelAPI/silc_channel_payload_parse_list
140  *
141  * SYNOPSIS
142  *
143  *    SilcDList
144  *    silc_channel_payload_parse_list(const unsigned char *payload,
145  *                                    SilcUInt32 payload_len);
146  *
147  * DESCRIPTION
148  *
149  *    Parses list of channel payloads returning list of payloads. This
150  *    is equivalent to the silc_channel_payload_parse except that the `buffer'
151  *    now includes multiple Channel Payloads one after the other.
152  *
153  ***/
154 SilcDList silc_channel_payload_parse_list(const unsigned char *payload,
155                                           SilcUInt32 payload_len);
156
157 /****f* silccore/SilcChannelAPI/silc_channel_payload_encode
158  *
159  * SYNOPSIS
160  *
161  *    SilcBuffer silc_channel_payload_encode(const unsigned char *channel_name,
162  *                                           SilcUInt16 channel_name_len,
163  *                                           const unsigned char *channel_id,
164  *                                           SilcUInt32 channel_id_len,
165  *                                           SilcUInt32 mode);
166  *
167  * DESCRIPTION
168  *
169  *    Encode new channel payload and returns it as buffer.
170  *
171  ***/
172 SilcBuffer silc_channel_payload_encode(const unsigned char *channel_name,
173                                        SilcUInt16 channel_name_len,
174                                        const unsigned char *channel_id,
175                                        SilcUInt32 channel_id_len,
176                                        SilcUInt32 mode);
177
178 /****f* silccore/SilcChannelAPI/silc_channel_payload_free
179  *
180  * SYNOPSIS
181  *
182  *    void silc_channel_payload_free(SilcChannelPayload payload);
183  *
184  * DESCRIPTION
185  *
186  *    Frees Channel Payload and all data in it.
187  *
188  ***/
189 void silc_channel_payload_free(SilcChannelPayload payload);
190
191 /****f* silccore/SilcChannelAPI/silc_channel_payload_list_free
192  *
193  * SYNOPSIS
194  *
195  *    void silc_channel_payload_list_free(SilcDList list);
196  *
197  * DESCRIPTION
198  *
199  *    Frees list of Channel Payloads and all data in them.
200  *
201  ***/
202 void silc_channel_payload_list_free(SilcDList list);
203
204 /****f* silccore/SilcChannelAPI/silc_channel_get_name
205  *
206  * SYNOPSIS
207  *
208  *    unsigned char *silc_channel_get_name(SilcChannelPayload payload,
209  *                                         SilcUInt32 *channel_name_len);
210  *
211  * DESCRIPTION
212  *
213  *    Return the channel name from the payload. The caller must not free it.
214  *
215  ***/
216 unsigned char *silc_channel_get_name(SilcChannelPayload payload,
217                                      SilcUInt32 *channel_name_len);
218
219 /****f* silccore/SilcChannelAPI/silc_channel_get_id
220  *
221  * SYNOPSIS
222  *
223  *    unsigned char *silc_channel_get_id(SilcChannelPayload payload,
224  *                                       SilcUInt32 *channel_id_len);
225  *
226  * DESCRIPTION
227  *
228  *    Return the Channel ID data from the payload. The caller must not free it.
229  *
230  ***/
231 unsigned char *silc_channel_get_id(SilcChannelPayload payload,
232                                    SilcUInt32 *channel_id_len);
233
234 /****f* silccore/SilcChannelAPI/silc_channel_get_id_parse
235  *
236  * SYNOPSIS
237  *
238  *    SilcChannelID *silc_channel_get_id_parse(SilcChannelPayload payload);
239  *
240  * DESCRIPTION
241  *
242  *    Return the Channel ID as parsed ID. This is equivalent to the
243  *    silc_channel_get_id execpt that the ID is already parsed. The caller
244  *    must free the parsed Channel ID.
245  *
246  ***/
247 SilcChannelID *silc_channel_get_id_parse(SilcChannelPayload payload);
248
249 /****f* silccore/SilcChannelAPI/silc_channel_get_mode
250  *
251  * SYNOPSIS
252  *
253  *    SilcUInt32 silc_channel_get_mode(SilcChannelPayload payload);
254  *
255  * DESCRIPTION
256  *
257  *    Return the mode. The mode is arbitrary. It can be the mode of the
258  *    channel or perhaps the mode of the client on the channel.  The protocol
259  *    dictates what the usage of the mode is in different circumstances.
260  *
261  ***/
262 SilcUInt32 silc_channel_get_mode(SilcChannelPayload payload);
263
264 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_decrypt
265  *
266  * SYNOPSIS
267  *
268  *    bool silc_channel_message_payload_decrypt(unsigned char *data,
269  *                                              size_t data_len,
270  *                                              SilcCipher cipher,
271  *                                              SilcHmac hmac);
272  *
273  * DESCRIPTION
274  *
275  *    Decrypt the channel message. First push the IV out of the packet `data'.
276  *    The IV is used in the decryption process. Then decrypt the message.
277  *    After decryption, take the MAC from the decrypted packet, compute MAC
278  *    and compare the MACs.  If they match, the decryption was successful
279  *    and we have the channel message ready to be displayed.
280  *
281  *    This is usually used by the Channel Message interface itself but can
282  *    be called by the appliation if separate decryption process is required.
283  *    For example server might need to call this directly in some 
284  *    circumstances. The `cipher' is used to decrypt the payload.
285  *
286  *    If the `hmac' is no provided then the MAC of the channel message is
287  *    not verified.
288  *
289  ***/
290 bool silc_channel_message_payload_decrypt(unsigned char *data,
291                                           size_t data_len,
292                                           SilcCipher cipher,
293                                           SilcHmac hmac);
294
295 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_parse
296  *
297  * SYNOPSIS
298  *
299  *    SilcChannelMessagePayload 
300  *    silc_channel_message_payload_parse(const unsigned char *payload,
301  *                                       SilcUInt32 payload_len,
302  *                                       SilcCipher cipher,
303  *                                       SilcHmac hmac);
304  *
305  * DESCRIPTION
306  *
307  *    Parses channel message payload returning new channel payload structure.
308  *    This also decrypts it and checks the MAC. The `cipher's is used to
309  *    decrypt the payload.
310  *
311  *    If the `hmac' is no provided then the MAC of the channel message is
312  *    not verified.
313  *
314  ***/
315 SilcChannelMessagePayload 
316 silc_channel_message_payload_parse(unsigned char *payload,
317                                    SilcUInt32 payload_len,
318                                    SilcCipher cipher,
319                                    SilcHmac hmac);
320
321 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_encrypt
322  *
323  * SYNOPSIS
324  *
325  *    bool silc_channel_message_payload_encrypt(unsigned char *data,
326  *                                              SilcUInt32 data_len,
327  *                                              SilcUInt32 true_len,
328  *                                              unsigned char *iv,
329  *                                              SilcUInt32 iv_len,
330  *                                              SilcCipher cipher,
331  *                                              SilcHmac hmac);
332  *
333  * DESCRIPTION
334  *
335  *    This function is used to encrypt the Channel Messsage Payload which is
336  *    the `data' and `data_len'.  The `data_len' is the data length which is
337  *    used to create MAC out of.  The `true_len' is the true length of `data'
338  *    message payload and is used assemble rest of the packet after MAC
339  *    creation. The `true_len' length packet will then be encrypted.
340  *
341  *    This is usually used by the Channel Message interface itself but can
342  *    be called by the appliation if separate encryption process is required.
343  *    For example server might need to call this directly in some 
344  *    circumstances. The `cipher' is used to encrypt the payload.
345  *
346  ***/
347 bool silc_channel_message_payload_encrypt(unsigned char *data,
348                                           SilcUInt32 data_len,
349                                           SilcUInt32 true_len,
350                                           unsigned char *iv,
351                                           SilcUInt32 iv_len,
352                                           SilcCipher cipher,
353                                           SilcHmac hmac);
354
355 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_encode
356  *
357  * SYNOPSIS
358  *
359  *    SilcBuffer silc_channel_message_payload_encode(SilcUInt16 flags,
360  *                                                   SilcUInt16 data_len,
361  *                                                   const unsigned char *data,
362  *                                                   SilcUInt16 iv_len,
363  *                                                   unsigned char *iv,
364  *                                                   SilcCipher cipher,
365  *                                                   SilcHmac hmac.
366  *                                                   SilcRng rng);
367  *
368  * DESCRIPTION
369  *
370  *    Encodes channel message payload into a buffer and returns it. This
371  *    is used to add channel message payload into a packet. As the channel
372  *    payload is encrypted separately from other parts of the packet padding
373  *    must be applied to the payload. The function generates the padding
374  *    automatically from random data.  The `cipher' is the cipher used
375  *    encrypt the payload and `hmac' is used to compute the MAC for the
376  *    payload.  If `rng' is NULL then global RNG is used, if non-NULL then
377  *    the `rng' is used.
378  *
379  ***/
380 SilcBuffer silc_channel_message_payload_encode(SilcUInt16 flags,
381                                                SilcUInt16 data_len,
382                                                const unsigned char *data,
383                                                SilcUInt16 iv_len,
384                                                unsigned char *iv,
385                                                SilcCipher cipher,
386                                                SilcHmac hmac,
387                                                SilcRng rng);
388
389 /****f* silccore/SilcChannelAPI/silc_channel_message_payload_free
390  *
391  * SYNOPSIS
392  *
393  *    void 
394  *    silc_channel_message_payload_free(SilcChannelMessagePayload payload);
395  *
396  * DESCRIPTION
397  *
398  *    Free's Channel Message Payload and all data in it.
399  *
400  ***/
401 void silc_channel_message_payload_free(SilcChannelMessagePayload payload);
402
403 /****f* silccore/SilcChannelAPI/silc_channel_message_get_flags
404  *
405  * SYNOPSIS
406  *
407  *    SilcMessageFlags
408  *    silc_channel_message_get_flags(SilcChannelMessagePayload payload);
409  *
410  * DESCRIPTION
411  *
412  *    Returns the message flags from the payload.
413  *
414  ***/
415 SilcMessageFlags
416 silc_channel_message_get_flags(SilcChannelMessagePayload payload);
417
418 /****f* silccore/SilcChannelAPI/silc_channel_message_get_data
419  *
420  * SYNOPSIS
421  *
422  *    unsigned char *
423  *    silc_channel_message_get_data(SilcChannelMessagePayload payload,
424  *                                  SilcUInt32 *data_len);
425  *
426  * DESCRIPTION
427  *
428  *    Return the data in the payload, that is, the actual channel message.
429  *    The caller must not free it.
430  *
431  ***/
432 unsigned char *silc_channel_message_get_data(SilcChannelMessagePayload payload,
433                                              SilcUInt32 *data_len);
434
435 /****f* silccore/SilcChannelAPI/silc_channel_message_get_mac
436  *
437  * SYNOPSIS
438  *
439  *    unsigned char *
440  *    silc_channel_message_get_mac(SilcChannelMessagePayload payload);
441  *
442  * DESCRIPTION
443  *
444  *    Return the MAC of the payload. The caller must already know the 
445  *    length of the MAC. The caller must not free the MAC.
446  *
447  ***/
448 unsigned char *silc_channel_message_get_mac(SilcChannelMessagePayload payload);
449
450 /****f* silccore/SilcChannelAPI/silc_channel_message_get_iv
451  *
452  * SYNOPSIS
453  *
454  *    unsigned char *
455  *    silc_channel_message_get_iv(SilcChannelMessagePayload payload);
456  *
457  * DESCRIPTION
458  *
459  *    Return the IV of the payload. The caller must already know the 
460  *    length of the IV. The caller must not free the IV.
461  *
462  ***/
463 unsigned char *silc_channel_message_get_iv(SilcChannelMessagePayload payload);
464
465 /****f* silccore/SilcChannelAPI/silc_channel_key_payload_parse
466  *
467  * SYNOPSIS
468  *
469  *    SilcChannelKeyPayload 
470  *    silc_channel_key_payload_parse(const unsigned char *payload,
471  *                                   uin32 payload_len);
472  *
473  * DESCRIPTION
474  *
475  *     Parses channel key payload returning new channel key payload 
476  *     structure.
477  *
478  ***/
479 SilcChannelKeyPayload 
480 silc_channel_key_payload_parse(const unsigned char *payload,
481                                SilcUInt32 payload_len);
482
483 /****f* silccore/SilcChannelAPI/silc_channel_key_payload_encode
484  *
485  * SYNOPSIS
486  *
487  *    SilcBuffer silc_channel_key_payload_encode(SilcUInt16 id_len,
488  *                                               const unsigned char *id,
489  *                                               SilcUInt16 cipher_len,
490  *                                               const unsigned char *cipher,
491  *                                               SilcUInt16 key_len,
492  *                                               const unsigned char *key);
493  *
494  * DESCRIPTION
495  *
496  *    Encodes channel key payload into a buffer and returns it. This is used 
497  *    to add channel key payload into a packet.
498  *
499  ***/
500 SilcBuffer silc_channel_key_payload_encode(SilcUInt16 id_len,
501                                            const unsigned char *id,
502                                            SilcUInt16 cipher_len,
503                                            const unsigned char *cipher,
504                                            SilcUInt16 key_len,
505                                            const unsigned char *key);
506
507 /****f* silccore/SilcChannelAPI/silc_channel_key_payload_free
508  *
509  * SYNOPSIS
510  *
511  *    void silc_channel_key_payload_free(SilcChannelKeyPayload payload);
512  *
513  * DESCRIPTION
514  *
515  *    Frees the Channel Key Payload and all data in it.
516  *
517  ***/
518 void silc_channel_key_payload_free(SilcChannelKeyPayload payload);
519
520 /****f* silccore/SilcChannelAPI/silc_channel_key_get_id
521  *
522  * SYNOPSIS
523  *
524  *    unsigned char *silc_channel_key_get_id(SilcChannelKeyPayload payload, 
525  *                                           SilcUInt32 *id_len);
526  *
527  * DESCRIPTION
528  *
529  *    Return the Channel ID data from the payload. The caller must not
530  *    free it.
531  *
532  ***/
533 unsigned char *silc_channel_key_get_id(SilcChannelKeyPayload payload, 
534                                        SilcUInt32 *id_len);
535
536 /****f* silccore/SilcChannelAPI/silc_channel_key_get_cipher
537  *
538  * SYNOPSIS
539  *
540  *    unsigned char *silc_channel_key_get_cipher(SilcChannelKeyPayload payload,
541  *                                               SilcUInt32 *cipher_len);
542  *
543  * DESCRIPTION
544  *
545  *    Return the name of the cipher from the payload. The caller must not
546  *    free it.
547  *
548  ***/
549 unsigned char *silc_channel_key_get_cipher(SilcChannelKeyPayload payload,
550                                            SilcUInt32 *cipher_len);
551
552 /****f* silccore/SilcChannelAPI/silc_channel_key_get_key
553  *
554  * SYNOPSIS
555  *
556  *    unsigned char *silc_channel_key_get_key(SilcChannelKeyPayload payload,
557  *                                            SilcUInt32 *key_len);
558  *
559  * DESCRIPTION
560  *
561  *    Return the raw key material from the payload. The caller must not
562  *    free it.
563  *
564  ***/
565 unsigned char *silc_channel_key_get_key(SilcChannelKeyPayload payload,
566                                         SilcUInt32 *key_len);
567
568 #endif