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