Merged from silc_1_0_branch.
[silc.git] / lib / silccore / silcmessage.h
1 /*
2
3   silcmessage.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2002 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.
26  *
27  * This interface defines also the SILC_MESSAGE_FLAG_SIGNED Payload,
28  * which defines how channel messages and private messages can be digitally
29  * signed.  This interface provides the payload parsing, encoding, 
30  * signature computing and signature verification routines.
31  *
32  ***/
33
34 #ifndef SILCMESSAGE_H
35 #define SILCMESSAGE_H
36
37 /****s* silccore/SilcMessageAPI/SilcMessagePayload
38  *
39  * NAME
40  * 
41  *    typedef struct SilcMessagePayloadStruct *SilcMessagePayload;
42  *
43  *
44  * DESCRIPTION
45  *
46  *    This context is the actual Message Payload and is allocated
47  *    by silc_message_payload_parse and given as argument usually
48  *    to all silc_message_* functions.  It is freed by the
49  *    silc_message_payload_free function.
50  *
51  ***/
52 typedef struct SilcMessagePayloadStruct *SilcMessagePayload;
53
54 /****s* silccore/SilcMessageAPI/SilcMessageSignedPayload
55  *
56  * NAME
57  * 
58  *    typedef struct SilcMessageSignedPayloadStruct *SilcMessageSignedPayload;
59  *
60  *
61  * DESCRIPTION
62  *
63  *    This context represents the SILC_MESSAGE_FLAG_SIGNED Payload which
64  *    is used with channel messages and private messages to indicate that
65  *    the message is digitally signed.  This payload may include the
66  *    message sender's public key and it includes the digital signature.
67  *    This payload MUST NOT be used in any other context except with
68  *    channel and private message sending and reception.
69  *
70  ***/
71 typedef struct SilcMessageSignedPayloadStruct *SilcMessageSignedPayload;
72
73 /****d* silccore/SilcMessageAPI/SilcMessageFlags
74  *
75  * NAME
76  * 
77  *    typedef SilcUInt16 SilcMessageFlags;
78  *
79  * DESCRIPTION
80  *
81  *    The message flags type definition and the message flags.  The 
82  *    message flags are used to indicate some status of the message.
83  *
84  * SOURCE
85  */
86 typedef SilcUInt16 SilcMessageFlags;
87
88 /* The message flags */
89 #define SILC_MESSAGE_FLAG_NONE        0x0000      /* No flags */
90 #define SILC_MESSAGE_FLAG_AUTOREPLY   0x0001      /* Automatically replied */
91 #define SILC_MESSAGE_FLAG_NOREPLY     0x0002      /* Send no reply to this */
92 #define SILC_MESSAGE_FLAG_ACTION      0x0004      /* Action message */
93 #define SILC_MESSAGE_FLAG_NOTICE      0x0008      /* Notice message */
94 #define SILC_MESSAGE_FLAG_REQUEST     0x0010      /* A request */
95 #define SILC_MESSAGE_FLAG_SIGNED      0x0020      /* Message is signed */
96 #define SILC_MESSAGE_FLAG_REPLY       0x0040      /* A reply */
97 #define SILC_MESSAGE_FLAG_DATA        0x0080      /* MIME object */
98 #define SILC_MESSAGE_FLAG_UTF8        0x0100      /* UTF-8 string */
99 #define SILC_MESSAGE_FLAG_RESERVED    0x0200      /* to 0x0800 */
100 #define SILC_MESSAGE_FLAG_PRIVATE     0x1000      /* to 0x8000 */
101 /***/
102
103 /****f* silccore/SilcMessageAPI/silc_message_payload_decrypt
104  *
105  * SYNOPSIS
106  *
107  *    bool silc_message_payload_decrypt(unsigned char *data,
108  *                                      size_t data_len,
109  *                                      bool private_message,
110  *                                      bool static_key,
111  *                                      SilcCipher cipher,
112  *                                      SilcHmac hmac,
113  *                                      bool check_mac);
114  *
115  * DESCRIPTION
116  *
117  *    Decrypt Message Payload indicated by `data'.  If the payload is
118  *    channel message then `private_message' is FALSE, and if it is
119  *    private message it is TRUE.  If the private message key is static
120  *    (pre-shared key) then protocol dictates that the IV is present
121  *    and `static_key' must be set to TRUE.  If the key is not static
122  *    (Key Agreement was done for the key) then it MUST be FALSE.  For
123  *    channel messages the `static_key' is ignored.
124  *
125  *    This is usually used by the Message Payload interface itself but can
126  *    be called by the appliation if separate decryption process is required.
127  *    For example server might need to call this directly in some 
128  *    circumstances. The `cipher' is used to decrypt the payload.  If
129  *    `check_mac' is FALSE then MAC is not verified.
130  *
131  ***/
132 bool silc_message_payload_decrypt(unsigned char *data,
133                                   size_t data_len,
134                                   bool private_message,
135                                   bool static_key,
136                                   SilcCipher cipher,
137                                   SilcHmac hmac,
138                                   bool check_mac);
139
140 /****f* silccore/SilcMessageAPI/silc_message_payload_parse
141  *
142  * SYNOPSIS
143  *
144  *    SilcMessagePayload 
145  *    silc_message_payload_parse(unsigned char *payload,
146  *                               SilcUInt32 payload_len,
147  *                               bool private_message,
148  *                               bool static_key,
149  *                               SilcCipher cipher,
150  *                               SilcHmac hmac);
151  *
152  * DESCRIPTION
153  *
154  *    Parses Message Payload returning new payload structure.  This also
155  *    decrypts the payload and checks the MAC.  If the payload is
156  *    channel message then `private_message' is FALSE, and if it is
157  *    private message it is TRUE.  If the private message key is static
158  *    (pre-shared key) then protocol dictates that the IV is present
159  *    and `static_key' must be set to TRUE.  If the key is not static
160  *    (Key Agreement was done for the key) then it MUST be FALSE.  For
161  *    channel messages the `static_key' is ignored.
162  *
163  *    If the `hmac' is no provided then the MAC of the channel message is
164  *    not verified.  If the message is private message and `cipher' is NULL
165  *    then this assumes that the packet was decrypted with session keys
166  *    (no private message key) and this merely decodes the payload.
167  *
168  ***/
169 SilcMessagePayload 
170 silc_message_payload_parse(unsigned char *payload,
171                            SilcUInt32 payload_len,
172                            bool private_message,
173                            bool static_key,
174                            SilcCipher cipher,
175                            SilcHmac hmac);
176
177 /****f* silccore/SilcMessageAPI/silc_message_payload_encrypt
178  *
179  * SYNOPSIS
180  *
181  *    bool silc_message_payload_encrypt(unsigned char *data,
182  *                                      SilcUInt32 data_len,
183  *                                      SilcUInt32 true_len,
184  *                                      unsigned char *iv,
185  *                                      SilcUInt32 iv_len,
186  *                                      SilcCipher cipher,
187  *                                      SilcHmac hmac);
188  *
189  * DESCRIPTION
190  *
191  *    This function is used to encrypt the Messsage Payload which is
192  *    the `data' and `data_len'.  The `data_len' is the data length which
193  *    is used to create MAC out of.  The `data' MUST have additional space
194  *    after `true_len' bytes for the MAC which is appended to the data.
195  *
196  *    This is usually used by the Message Payload interface itself but can
197  *    be called by the appliation if separate encryption process is required.
198  *    For example server might need to call this directly in some 
199  *    circumstances. The `cipher' is used to encrypt the payload and `hmac'
200  *    to compute the MAC for the payload.
201  *
202  ***/
203 bool silc_message_payload_encrypt(unsigned char *data,
204                                   SilcUInt32 data_len,
205                                   SilcUInt32 true_len,
206                                   unsigned char *iv,
207                                   SilcUInt32 iv_len,
208                                   SilcCipher cipher,
209                                   SilcHmac hmac);
210
211 /****f* silccore/SilcMessageAPI/silc_message_payload_encode
212  *
213  * SYNOPSIS
214  *
215  *    SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
216  *                                           const unsigned char *data,
217  *                                           SilcUInt32 data_len,
218  *                                           bool generate_iv,
219  *                                           bool private_message,
220  *                                           SilcCipher cipher,
221  *                                           SilcHmac hmac,
222  *                                           SilcRng rng,
223  *                                           SilcPublicKey public_key,
224  *                                           SilcPrivateKey private_key,
225  *                                           SilcHash hash);
226  *
227  * DESCRIPTION
228  *
229  *    Encodes a Message Payload into a buffer and returns it.  This is
230  *    used to encode channel messages and private messages into a packet.
231  *    If `private_message' is FALSE then this encodes channel message, if
232  *    it is TRUE this encodes private message.  If `private_message' is
233  *    TRUE then `generate_iv' MUST be FALSE if the private message key
234  *    `cipher' is not static key (pre-shared key).  If it is static key
235  *    then protocol dictates that IV must be present in the Message Payload
236  *    and `generate_iv' must be TRUE.  The caller must know whether the key
237  *    is static or not for private messages.  If the key was generated with
238  *    Key Agreement protocol then `generate_iv' is always FALSE.  For
239  *    channel messages `generate_iv' is always set to TRUE value.
240  *
241  *    The `cipher' is the cipher used to encrypt the message and `hmac'
242  *    is used to compute the MAC for the payload.  If encoding private
243  *    message that will be encrypted with session keys (no private message
244  *    key) then `cipher' and `hmac' is NULL and this merely encodes the
245  *    payload buffer, and the caller must encrypt the packet later.
246  *    If `rng' is NULL then global RNG is used, if non-NULL then the
247  *    `rng' is used (for IV and padding generation).
248  *
249  *    The `public_key', `private_key' and `hash' are provided only if the
250  *    flags includes SILC_MESSAGE_FLAG_SIGNED, in which case the message
251  *    will be digitally signed.  If `public_key' is non-NULL then it will
252  *    be included in the message.  The `private_message' and `hash' MUST
253  *    be provided.  The `hash' SHOULD be SHA1.
254  *
255  ***/
256 SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
257                                        const unsigned char *data,
258                                        SilcUInt32 data_len,
259                                        bool generate_iv,
260                                        bool private_message,
261                                        SilcCipher cipher,
262                                        SilcHmac hmac,
263                                        SilcRng rng,
264                                        SilcPublicKey public_key,
265                                        SilcPrivateKey private_key,
266                                        SilcHash hash);
267
268 /****f* silccore/SilcMessageAPI/silc_message_payload_free
269  *
270  * SYNOPSIS
271  *
272  *    void silc_message_payload_free(SilcMessagePayload payload);
273  *
274  * DESCRIPTION
275  *
276  *    Free's Message Payload and all data in it.
277  *
278  ***/
279 void silc_message_payload_free(SilcMessagePayload payload);
280
281 /****f* silccore/SilcMessageAPI/silc_message_get_flags
282  *
283  * SYNOPSIS
284  *
285  *    SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
286  *
287  * DESCRIPTION
288  *
289  *    Returns the message flags from the payload.
290  *
291  ***/
292 SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
293
294 /****f* silccore/SilcMessageAPI/silc_message_get_data
295  *
296  * SYNOPSIS
297  *
298  *    unsigned char *
299  *    silc_message_get_data(SilcMessagePayload payload,
300  *                                  SilcUInt32 *data_len);
301  *
302  * DESCRIPTION
303  *
304  *    Return the data in the payload, that is, the actual message data.
305  *    The caller must not free it.
306  *
307  ***/
308 unsigned char *silc_message_get_data(SilcMessagePayload payload,
309                                      SilcUInt32 *data_len);
310
311 /****f* silccore/SilcMessageAPI/silc_message_get_mac
312  *
313  * SYNOPSIS
314  *
315  *    unsigned char *
316  *    silc_message_get_mac(SilcMessagePayload payload);
317  *
318  * DESCRIPTION
319  *
320  *    Return the MAC of the payload. The caller must already know the 
321  *    length of the MAC. The caller must not free the MAC.
322  *
323  ***/
324 unsigned char *silc_message_get_mac(SilcMessagePayload payload);
325
326 /****f* silccore/SilcMessageAPI/silc_message_get_iv
327  *
328  * SYNOPSIS
329  *
330  *    unsigned char *
331  *    silc_message_get_iv(SilcMessagePayload payload);
332  *
333  * DESCRIPTION
334  *
335  *    Return the IV of the payload. The caller must already know the 
336  *    length of the IV. The caller must not free the IV.
337  *
338  ***/
339 unsigned char *silc_message_get_iv(SilcMessagePayload payload);
340
341 /****f* silccore/SilcMessageAPI/silc_message_get_signature
342  *
343  * SYNOPSIS
344  *
345  *    const SilcMessageSignedPayload
346  *    silc_message_get_signature(SilcMessagePayload payload);
347  *
348  * DESCRIPTION
349  *
350  *    Returns the pointer to the signature of the message if the
351  *    SILC_MESSAGE_FLAG_SIGNED was set.  If the flag is set and this
352  *    function returns NULL then error had occurred and the signature
353  *    could not be retrieved from the message.
354  *
355  *    The caller SHOULD verify the signature by calling the
356  *    silc_message_signed_verify function.
357  *
358  ***/
359 const SilcMessageSignedPayload
360 silc_message_get_signature(SilcMessagePayload payload);
361
362 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_parse
363  *
364  * SYNOPSIS
365  *
366  *    SilcMessageSignedPayload
367  *    silc_message_signed_payload_parse(const unsigned char *data,
368  *                                      SilcUInt32 data_len);
369  *
370  * DESCRIPTION
371  *
372  *    Parses the SilcMessageSignedPayload Payload from the `data' of
373  *    length of `data_len' bytes.  The `data' must be payload without
374  *    the actual message payload.  Returns the parsed payload or NULL
375  *    on error.  Caller must free the returned payload.  Application
376  *    usually does not need to call this since the function
377  *    silc_message_payload_parse calls this automatically for signed
378  *    messages.
379  *
380  ***/
381 SilcMessageSignedPayload
382 silc_message_signed_payload_parse(const unsigned char *data,
383                                   SilcUInt32 data_len);
384
385 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_encode
386  *
387  * SYNOPSIS
388  *
389  *    SilcBuffer
390  *    silc_message_signed_payload_encode(const unsigned char *message_payload,
391  *                                       SilcUInt32 message_payload_len,
392  *                                       SilcPublicKey public_key,
393  *                                       SilcPrivateKey private_key,
394  *                                       SilcHash hash);
395  *
396  * DESCRIPTION
397  *
398  *    Encodes the SilcMessageSignedPayload Payload and computes the
399  *    digital signature.  The `message_payload' is the message data that
400  *    is used in the signature computation.  The encoding of the buffer
401  *    is specified in the SILC protocol.  If `public_key' is provided
402  *    then the public key included in the payload.  The `private_key'
403  *    is used to produce the signature.  This function returns the encoded
404  *    payload with the signature or NULL on error.  Caller must free the
405  *    returned buffer.  The `hash' SHOULD be SHA-1 hash function.
406  *    
407  *    Application usually does not need to call this since the function
408  *    silc_message_payload_encode calls this automatically if the caller
409  *    wants to sign the message.
410  *
411  ***/
412 SilcBuffer
413 silc_message_signed_payload_encode(const unsigned char *message_payload,
414                                    SilcUInt32 message_payload_len,
415                                    SilcPublicKey public_key,
416                                    SilcPrivateKey private_key,
417                                    SilcHash hash);
418
419 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_free
420  *
421  * SYNOPSIS
422  *
423  *    void silc_message_signed_payload_free(SilcMessageSignedPayload sig);
424  *
425  * DESCRIPTION
426  *
427  *    Frees the SilcMessageSignedPayload Payload.
428  *
429  ***/
430 void silc_message_signed_payload_free(SilcMessageSignedPayload sig);
431
432 /****f* silccore/SilcMessageAPI/silc_message_signed_verify
433  *
434  * SYNOPSIS
435  *
436  *    int silc_message_signed_verify(SilcMessageSignedPayload sig,
437  *                                   SilcMessagePayload message,
438  *                                   SilcPublicKey remote_public_key,
439  *                                   SilcHash hash);
440  *
441  * DESCRIPTION
442  *
443  *    This routine can be used to verify the signature found in
444  *    SilcMessageSignedPayload Payload.  This returns SILC_AUTH_OK if the
445  *    signature verification was successful.
446  *
447  ***/
448 int silc_message_signed_verify(SilcMessageSignedPayload sig,
449                                SilcMessagePayload message,
450                                SilcPublicKey remote_public_key,
451                                SilcHash hash);
452
453 /****f* silccore/SilcMessageAPI/silc_message_signed_get_public_key
454  *
455  * SYNOPSIS
456  *
457  *    SilcPublicKey
458  *    silc_message_signed_get_public_key(SilcMessageSignedPayload sig);
459  *
460  * DESCRIPTION
461  *
462  *    Returns the decoded SilcPublicKey from the SilcMessageSignedPayload
463  *    Payload or NULL if it does not include public key.  The caller must
464  *    free the returned public key pointer.  This also returns the raw
465  *    public key (before decoding) into `pk_data' and `pk_data_len' if
466  *    they are provided.  The caller must not free these pointers.
467  *
468  ***/
469 SilcPublicKey
470 silc_message_signed_get_public_key(SilcMessageSignedPayload sig,
471                                    unsigned char **pk_data,
472                                    SilcUInt32 *pk_data_len);
473
474 #endif /* SILCMESSAGE_H */