Added SILC Server library.
[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_ACK         0x0200      /* ACK messages */
100 #define SILC_MESSAGE_FLAG_RESERVED    0x0400      /* to 0x1000 */
101 #define SILC_MESSAGE_FLAG_PRIVATE     0x2000      /* to 0x8000 */
102 /***/
103
104 /****f* silccore/SilcMessageAPI/silc_message_payload_decrypt
105  *
106  * SYNOPSIS
107  *
108  *    SilcBool silc_message_payload_decrypt(unsigned char *data,
109  *                                      size_t data_len,
110  *                                      SilcBool private_message,
111  *                                      SilcBool static_key,
112  *                                      SilcCipher cipher,
113  *                                      SilcHmac hmac,
114  *                                      SilcBool check_mac);
115  *
116  * DESCRIPTION
117  *
118  *    Decrypt Message Payload indicated by `data'.  If the payload is
119  *    channel message then `private_message' is FALSE, and if it is
120  *    private message it is TRUE.  If the private message key is static
121  *    (pre-shared key) then protocol dictates that the IV is present
122  *    and `static_key' must be set to TRUE.  If the key is not static
123  *    (Key Agreement was done for the key) then it MUST be FALSE.  For
124  *    channel messages the `static_key' is ignored.
125  *
126  *    This is usually used by the Message Payload interface itself but can
127  *    be called by the appliation if separate decryption process is required.
128  *    For example server might need to call this directly in some 
129  *    circumstances. The `cipher' is used to decrypt the payload.  If
130  *    `check_mac' is FALSE then MAC is not verified.
131  *
132  ***/
133 SilcBool silc_message_payload_decrypt(unsigned char *data,
134                                   size_t data_len,
135                                   SilcBool private_message,
136                                   SilcBool static_key,
137                                   SilcCipher cipher,
138                                   SilcHmac hmac,
139                                   SilcBool check_mac);
140
141 /****f* silccore/SilcMessageAPI/silc_message_payload_parse
142  *
143  * SYNOPSIS
144  *
145  *    SilcMessagePayload 
146  *    silc_message_payload_parse(unsigned char *payload,
147  *                               SilcUInt32 payload_len,
148  *                               SilcBool private_message,
149  *                               SilcBool static_key,
150  *                               SilcCipher cipher,
151  *                               SilcHmac hmac);
152  *
153  * DESCRIPTION
154  *
155  *    Parses Message Payload returning new payload structure.  This also
156  *    decrypts the payload and checks the MAC.  If the payload is
157  *    channel message then `private_message' is FALSE, and if it is
158  *    private message it is TRUE.  If the private message key is static
159  *    (pre-shared key) then protocol dictates that the IV is present
160  *    and `static_key' must be set to TRUE.  If the key is not static
161  *    (Key Agreement was done for the key) then it MUST be FALSE.  For
162  *    channel messages the `static_key' is ignored.
163  *
164  *    If the `hmac' is no provided then the MAC of the channel message is
165  *    not verified.  If the message is private message and `cipher' is NULL
166  *    then this assumes that the packet was decrypted with session keys
167  *    (no private message key) and this merely decodes the payload.
168  *
169  ***/
170 SilcMessagePayload 
171 silc_message_payload_parse(unsigned char *payload,
172                            SilcUInt32 payload_len,
173                            SilcBool private_message,
174                            SilcBool static_key,
175                            SilcCipher cipher,
176                            SilcHmac hmac);
177
178 /****f* silccore/SilcMessageAPI/silc_message_payload_encrypt
179  *
180  * SYNOPSIS
181  *
182  *    SilcBool silc_message_payload_encrypt(unsigned char *data,
183  *                                      SilcUInt32 data_len,
184  *                                      SilcUInt32 true_len,
185  *                                      unsigned char *iv,
186  *                                      SilcUInt32 iv_len,
187  *                                      SilcCipher cipher,
188  *                                      SilcHmac hmac);
189  *
190  * DESCRIPTION
191  *
192  *    This function is used to encrypt the Messsage Payload which is
193  *    the `data' and `data_len'.  The `data_len' is the data length which
194  *    is used to create MAC out of.  The `data' MUST have additional space
195  *    after `true_len' bytes for the MAC which is appended to the data.
196  *
197  *    This is usually used by the Message Payload interface itself but can
198  *    be called by the appliation if separate encryption process is required.
199  *    For example server might need to call this directly in some 
200  *    circumstances. The `cipher' is used to encrypt the payload and `hmac'
201  *    to compute the MAC for the payload.
202  *
203  ***/
204 SilcBool silc_message_payload_encrypt(unsigned char *data,
205                                   SilcUInt32 data_len,
206                                   SilcUInt32 true_len,
207                                   unsigned char *iv,
208                                   SilcUInt32 iv_len,
209                                   SilcCipher cipher,
210                                   SilcHmac hmac);
211
212 /****f* silccore/SilcMessageAPI/silc_message_payload_encode
213  *
214  * SYNOPSIS
215  *
216  *    SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
217  *                                           const unsigned char *data,
218  *                                           SilcUInt32 data_len,
219  *                                           SilcBool generate_iv,
220  *                                           SilcBool private_message,
221  *                                           SilcCipher cipher,
222  *                                           SilcHmac hmac,
223  *                                           SilcRng rng,
224  *                                           SilcPublicKey public_key,
225  *                                           SilcPrivateKey private_key,
226  *                                           SilcHash hash);
227  *
228  * DESCRIPTION
229  *
230  *    Encodes a Message Payload into a buffer and returns it.  This is
231  *    used to encode channel messages and private messages into a packet.
232  *    If `private_message' is FALSE then this encodes channel message, if
233  *    it is TRUE this encodes private message.  If `private_message' is
234  *    TRUE then `generate_iv' MUST be FALSE if the private message key
235  *    `cipher' is not static key (pre-shared key).  If it is static key
236  *    then protocol dictates that IV must be present in the Message Payload
237  *    and `generate_iv' must be TRUE.  The caller must know whether the key
238  *    is static or not for private messages.  If the key was generated with
239  *    Key Agreement protocol then `generate_iv' is always FALSE.  For
240  *    channel messages `generate_iv' is always set to TRUE value.
241  *
242  *    The `cipher' is the cipher used to encrypt the message and `hmac'
243  *    is used to compute the MAC for the payload.  If encoding private
244  *    message that will be encrypted with session keys (no private message
245  *    key) then `cipher' and `hmac' is NULL and this merely encodes the
246  *    payload buffer, and the caller must encrypt the packet later.
247  *    If `rng' is NULL then global RNG is used, if non-NULL then the
248  *    `rng' is used (for IV and padding generation).
249  *
250  *    The `public_key', `private_key' and `hash' are provided only if the
251  *    flags includes SILC_MESSAGE_FLAG_SIGNED, in which case the message
252  *    will be digitally signed.  If `public_key' is non-NULL then it will
253  *    be included in the message.  The `private_message' and `hash' MUST
254  *    be provided.  The `hash' SHOULD be SHA1.
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
269 /****f* silccore/SilcMessageAPI/silc_message_payload_free
270  *
271  * SYNOPSIS
272  *
273  *    void silc_message_payload_free(SilcMessagePayload payload);
274  *
275  * DESCRIPTION
276  *
277  *    Free's Message Payload and all data in it.
278  *
279  ***/
280 void silc_message_payload_free(SilcMessagePayload payload);
281
282 /****f* silccore/SilcMessageAPI/silc_message_get_flags
283  *
284  * SYNOPSIS
285  *
286  *    SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
287  *
288  * DESCRIPTION
289  *
290  *    Returns the message flags from the payload.
291  *
292  ***/
293 SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
294
295 /****f* silccore/SilcMessageAPI/silc_message_get_data
296  *
297  * SYNOPSIS
298  *
299  *    unsigned char *
300  *    silc_message_get_data(SilcMessagePayload payload,
301  *                                  SilcUInt32 *data_len);
302  *
303  * DESCRIPTION
304  *
305  *    Return the data in the payload, that is, the actual message data.
306  *    The caller must not free it.
307  *
308  ***/
309 unsigned char *silc_message_get_data(SilcMessagePayload payload,
310                                      SilcUInt32 *data_len);
311
312 /****f* silccore/SilcMessageAPI/silc_message_get_mac
313  *
314  * SYNOPSIS
315  *
316  *    unsigned char *
317  *    silc_message_get_mac(SilcMessagePayload payload);
318  *
319  * DESCRIPTION
320  *
321  *    Return the MAC of the payload. The caller must already know the 
322  *    length of the MAC. The caller must not free the MAC.
323  *
324  ***/
325 unsigned char *silc_message_get_mac(SilcMessagePayload payload);
326
327 /****f* silccore/SilcMessageAPI/silc_message_get_iv
328  *
329  * SYNOPSIS
330  *
331  *    unsigned char *
332  *    silc_message_get_iv(SilcMessagePayload payload);
333  *
334  * DESCRIPTION
335  *
336  *    Return the IV of the payload. The caller must already know the 
337  *    length of the IV. The caller must not free the IV.
338  *
339  ***/
340 unsigned char *silc_message_get_iv(SilcMessagePayload payload);
341
342 /****f* silccore/SilcMessageAPI/silc_message_get_signature
343  *
344  * SYNOPSIS
345  *
346  *    SilcMessageSignedPayload
347  *    silc_message_get_signature(SilcMessagePayload payload);
348  *
349  * DESCRIPTION
350  *
351  *    Returns the pointer to the signature of the message if the
352  *    SILC_MESSAGE_FLAG_SIGNED was set.  If the flag is set and this
353  *    function returns NULL then error had occurred and the signature
354  *    could not be retrieved from the message.
355  *
356  *    The caller SHOULD verify the signature by calling the
357  *    silc_message_signed_verify function.  Caller must not free the
358  *    returned payload pointer.
359  *
360  ***/
361 SilcMessageSignedPayload
362 silc_message_get_signature(SilcMessagePayload payload);
363
364 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_parse
365  *
366  * SYNOPSIS
367  *
368  *    SilcMessageSignedPayload
369  *    silc_message_signed_payload_parse(const unsigned char *data,
370  *                                      SilcUInt32 data_len);
371  *
372  * DESCRIPTION
373  *
374  *    Parses the SilcMessageSignedPayload Payload from the `data' of
375  *    length of `data_len' bytes.  The `data' must be payload without
376  *    the actual message payload.  Returns the parsed payload or NULL
377  *    on error.  Caller must free the returned payload.  Application
378  *    usually does not need to call this since the function
379  *    silc_message_payload_parse calls this automatically for signed
380  *    messages.
381  *
382  ***/
383 SilcMessageSignedPayload
384 silc_message_signed_payload_parse(const unsigned char *data,
385                                   SilcUInt32 data_len);
386
387 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_encode
388  *
389  * SYNOPSIS
390  *
391  *    SilcBuffer
392  *    silc_message_signed_payload_encode(const unsigned char *message_payload,
393  *                                       SilcUInt32 message_payload_len,
394  *                                       SilcPublicKey public_key,
395  *                                       SilcPrivateKey private_key,
396  *                                       SilcHash hash);
397  *
398  * DESCRIPTION
399  *
400  *    Encodes the SilcMessageSignedPayload Payload and computes the
401  *    digital signature.  The `message_payload' is the message data that
402  *    is used in the signature computation.  The encoding of the buffer
403  *    is specified in the SILC protocol.  If `public_key' is provided
404  *    then the public key included in the payload.  The `private_key'
405  *    is used to produce the signature.  This function returns the encoded
406  *    payload with the signature or NULL on error.  Caller must free the
407  *    returned buffer.  The `hash' SHOULD be SHA-1 hash function.
408  *    
409  *    Application usually does not need to call this since the function
410  *    silc_message_payload_encode calls this automatically if the caller
411  *    wants to sign the message.
412  *
413  ***/
414 SilcBuffer
415 silc_message_signed_payload_encode(const unsigned char *message_payload,
416                                    SilcUInt32 message_payload_len,
417                                    SilcPublicKey public_key,
418                                    SilcPrivateKey private_key,
419                                    SilcHash hash);
420
421 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_free
422  *
423  * SYNOPSIS
424  *
425  *    void silc_message_signed_payload_free(SilcMessageSignedPayload sig);
426  *
427  * DESCRIPTION
428  *
429  *    Frees the SilcMessageSignedPayload Payload.
430  *
431  ***/
432 void silc_message_signed_payload_free(SilcMessageSignedPayload sig);
433
434 /****f* silccore/SilcMessageAPI/silc_message_signed_verify
435  *
436  * SYNOPSIS
437  *
438  *    int silc_message_signed_verify(SilcMessageSignedPayload sig,
439  *                                   SilcMessagePayload message,
440  *                                   SilcPublicKey remote_public_key,
441  *                                   SilcHash hash);
442  *
443  * DESCRIPTION
444  *
445  *    This routine can be used to verify the signature found in
446  *    SilcMessageSignedPayload Payload.  This returns SILC_AUTH_OK if the
447  *    signature verification was successful.
448  *
449  ***/
450 int silc_message_signed_verify(SilcMessageSignedPayload sig,
451                                SilcMessagePayload message,
452                                SilcPublicKey remote_public_key,
453                                SilcHash hash);
454
455 /****f* silccore/SilcMessageAPI/silc_message_signed_get_public_key
456  *
457  * SYNOPSIS
458  *
459  *    SilcPublicKey
460  *    silc_message_signed_get_public_key(SilcMessageSignedPayload sig,
461  *                                       unsigned char **pk_data,
462  *                                       SilcUInt32 *pk_data_len);
463  *
464  * DESCRIPTION
465  *
466  *    Returns the decoded SilcPublicKey from the SilcMessageSignedPayload
467  *    Payload or NULL if it does not include public key.  The caller must
468  *    free the returned public key pointer.  This also returns the raw
469  *    public key (before decoding) into `pk_data' and `pk_data_len' if
470  *    they are provided.  The caller must not free these pointers.
471  *
472  ***/
473 SilcPublicKey
474 silc_message_signed_get_public_key(SilcMessageSignedPayload sig,
475                                    unsigned char **pk_data,
476                                    SilcUInt32 *pk_data_len);
477
478 #endif /* SILCMESSAGE_H */