09ca21aa8bc2bbff0dca4c71da69ef27f920257a
[silc.git] / lib / silccore / silcmessage.h
1 /*
2
3   silcmessage.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2006 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; version 2 of the License.
12
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.
17
18 */
19
20 /****h* silccore/SILC Message Interface
21  *
22  * DESCRIPTION
23  *
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
28  * this interface.
29  *
30  ***/
31
32 #ifndef SILCMESSAGE_H
33 #define SILCMESSAGE_H
34
35 /****s* silccore/SilcMessageAPI/SilcMessagePayload
36  *
37  * NAME
38  *
39  *    typedef struct SilcMessagePayloadObject
40  *      *SilcMessagePayload, SilcMessagePayloadStruct;
41  *
42  *
43  * DESCRIPTION
44  *
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.
49  *
50  ***/
51 typedef struct SilcMessagePayloadObject
52   *SilcMessagePayload, SilcMessagePayloadStruct;
53
54 /****d* silccore/SilcMessageAPI/SilcMessageFlags
55  *
56  * NAME
57  *
58  *    typedef SilcUInt16 SilcMessageFlags;
59  *
60  * DESCRIPTION
61  *
62  *    The message flags type definition and the message flags.  The
63  *    message flags are used to indicate some status of the message.
64  *
65  * SOURCE
66  */
67 typedef SilcUInt16 SilcMessageFlags;
68
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 */
84 /***/
85
86 /****f* silccore/SilcMessageAPI/silc_message_payload_decrypt
87  *
88  * SYNOPSIS
89  *
90  *    SilcBool silc_message_payload_decrypt(unsigned char *data,
91  *                                          size_t data_len,
92  *                                          SilcBool private_message,
93  *                                          SilcBool static_key,
94  *                                          SilcCipher cipher,
95  *                                          SilcHmac hmac,
96  *                                          SilcBool check_mac);
97  *
98  * DESCRIPTION
99  *
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.
107  *
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.
113  *
114  ***/
115 SilcBool silc_message_payload_decrypt(unsigned char *data,
116                                       size_t data_len,
117                                       SilcBool private_message,
118                                       SilcBool static_key,
119                                       SilcCipher cipher,
120                                       SilcHmac hmac,
121                                       SilcBool check_mac);
122
123 /****f* silccore/SilcMessageAPI/silc_message_payload_parse
124  *
125  * SYNOPSIS
126  *
127  *    SilcMessagePayload
128  *    silc_message_payload_parse(unsigned char *payload,
129  *                               SilcUInt32 payload_len,
130  *                               SilcBool private_message,
131  *                               SilcBool static_key,
132  *                               SilcCipher cipher,
133  *                               SilcHmac hmac,
134  *                               SilcStack stack,
135  *                               SilcBool no_allocation,
136  *                               SilcMessagePayload message);
137  *
138  * DESCRIPTION
139  *
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.
148  *
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.
153  *
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.
161  *
162  ***/
163 SilcMessagePayload
164 silc_message_payload_parse(unsigned char *payload,
165                            SilcUInt32 payload_len,
166                            SilcBool private_message,
167                            SilcBool static_key,
168                            SilcCipher cipher,
169                            SilcHmac hmac,
170                            SilcStack stack,
171                            SilcBool no_allocation,
172                            SilcMessagePayload message);
173
174 /****f* silccore/SilcMessageAPI/silc_message_payload_encrypt
175  *
176  * SYNOPSIS
177  *
178  *    SilcBool silc_message_payload_encrypt(unsigned char *data,
179  *                                          SilcUInt32 data_len,
180  *                                          SilcUInt32 true_len,
181  *                                          unsigned char *iv,
182  *                                          SilcCipher cipher,
183  *                                          SilcHmac hmac);
184  *
185  * DESCRIPTION
186  *
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.
191  *
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.
197  *
198  ***/
199 SilcBool silc_message_payload_encrypt(unsigned char *data,
200                                       SilcUInt32 data_len,
201                                       SilcUInt32 true_len,
202                                       unsigned char *iv,
203                                       SilcCipher cipher,
204                                       SilcHmac hmac);
205
206 /****f* silccore/SilcMessageAPI/silc_message_payload_encode
207  *
208  * SYNOPSIS
209  *
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,
215  *                                           SilcCipher cipher,
216  *                                           SilcHmac hmac,
217  *                                           SilcRng rng,
218  *                                           SilcPublicKey public_key,
219  *                                           SilcPrivateKey private_key,
220  *                                           SilcHash hash,
221  *                                           SilcBuffer buffer);
222  *
223  * DESCRIPTION
224  *
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.
236  *
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).
244  *
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.
250  *
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.
255  *
256  ***/
257 SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
258                                        const unsigned char *data,
259                                        SilcUInt32 data_len,
260                                        SilcBool generate_iv,
261                                        SilcBool private_message,
262                                        SilcCipher cipher,
263                                        SilcHmac hmac,
264                                        SilcRng rng,
265                                        SilcPublicKey public_key,
266                                        SilcPrivateKey private_key,
267                                        SilcHash hash,
268                                        SilcBuffer buffer);
269
270 /****f* silccore/SilcMessageAPI/silc_message_payload_free
271  *
272  * SYNOPSIS
273  *
274  *    void silc_message_payload_free(SilcMessagePayload payload);
275  *
276  * DESCRIPTION
277  *
278  *    Free's Message Payload and all data in it.
279  *
280  ***/
281 void silc_message_payload_free(SilcMessagePayload payload);
282
283 /****f* silccore/SilcMessageAPI/silc_message_get_flags
284  *
285  * SYNOPSIS
286  *
287  *    SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
288  *
289  * DESCRIPTION
290  *
291  *    Returns the message flags from the payload.
292  *
293  ***/
294 SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
295
296 /****f* silccore/SilcMessageAPI/silc_message_get_data
297  *
298  * SYNOPSIS
299  *
300  *    unsigned char *
301  *    silc_message_get_data(SilcMessagePayload payload,
302  *                                  SilcUInt32 *data_len);
303  *
304  * DESCRIPTION
305  *
306  *    Return the data in the payload, that is, the actual message data.
307  *    The caller must not free it.
308  *
309  ***/
310 unsigned char *silc_message_get_data(SilcMessagePayload payload,
311                                      SilcUInt32 *data_len);
312
313 /****f* silccore/SilcMessageAPI/silc_message_get_mac
314  *
315  * SYNOPSIS
316  *
317  *    unsigned char *
318  *    silc_message_get_mac(SilcMessagePayload payload);
319  *
320  * DESCRIPTION
321  *
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.
324  *
325  ***/
326 unsigned char *silc_message_get_mac(SilcMessagePayload payload);
327
328 /****f* silccore/SilcMessageAPI/silc_message_signed_verify
329  *
330  * SYNOPSIS
331  *
332  *    int silc_message_signed_verify(SilcMessagePayload message,
333  *                                   SilcPublicKey remote_public_key,
334  *                                   SilcHash hash);
335  *
336  * DESCRIPTION
337  *
338  *    This routine can be used to verify the digital signature from the
339  *    message indicated by `message'.  The signature is present only if
340  *    the SILC_MESSAGE_FLAG_SIGNED is set in the message flags.  This
341  *    returns SILC_AUTH_OK if the signature verification was successful.
342  *
343  ***/
344 int silc_message_signed_verify(SilcMessagePayload message,
345                                SilcPublicKey remote_public_key,
346                                SilcHash hash);
347
348 /****f* silccore/SilcMessageAPI/silc_message_signed_get_public_key
349  *
350  * SYNOPSIS
351  *
352  *    SilcPublicKey
353  *    silc_message_signed_get_public_key(SilcMessagePayload payload,
354  *                                       const unsigned char **pk_data,
355  *                                       SilcUInt32 *pk_data_len);
356  *
357  * DESCRIPTION
358  *
359  *    Returns the decoded SilcPublicKey from the message payload or NULL
360  *    if it does not include public key.  The caller must free the returned
361  *    public key pointer.  This also returns the raw public key (before
362  *    decoding) into `pk_data' and `pk_data_len' if they are provided.  The
363  *    caller must not free these pointers.
364  *
365  ***/
366 SilcPublicKey
367 silc_message_signed_get_public_key(SilcMessagePayload payload,
368                                    const unsigned char **pk_data,
369                                    SilcUInt32 *pk_data_len);
370
371 #include "silcmessage_i.h"
372
373 #endif /* SILCMESSAGE_H */