5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 -2002 Pekka Riikonen
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.
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.
20 /****h* silccore/SILC Message Interface
24 * This interface includes the implementation of the Message Payload that
25 * is used to send private messages and channel messages.
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.
37 /****s* silccore/SilcMessageAPI/SilcMessagePayload
41 * typedef struct SilcMessagePayloadStruct *SilcMessagePayload;
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.
52 typedef struct SilcMessagePayloadStruct *SilcMessagePayload;
54 /****d* silccore/SilcMessageAPI/SilcMessageFlags
58 * typedef SilcUInt16 SilcMessageFlags;
62 * The message flags type definition and the message flags. The
63 * message flags are used to indicate some status of the message.
67 typedef SilcUInt16 SilcMessageFlags;
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_RESERVED 0x0200 /* to 0x0800 */
81 #define SILC_MESSAGE_FLAG_PRIVATE 0x1000 /* to 0x8000 */
84 /****f* silccore/SilcMessageAPI/silc_message_payload_decrypt
88 * bool silc_message_payload_decrypt(unsigned char *data,
90 * bool private_message,
98 * Decrypt Message Payload indicated by `data'. If the payload is
99 * channel message then `private_message' is FALSE, and if it is
100 * private message it is TRUE. If the private message key is static
101 * (pre-shared key) then protocol dictates that the IV is present
102 * and `static_key' must be set to TRUE. If the key is not static
103 * (Key Agreement was done for the key) then it MUST be FALSE. For
104 * channel messages the `static_key' is ignored.
106 * This is usually used by the Message Payload interface itself but can
107 * be called by the appliation if separate decryption process is required.
108 * For example server might need to call this directly in some
109 * circumstances. The `cipher' is used to decrypt the payload. If
110 * `check_mac' is FALSE then MAC is not verified.
113 bool silc_message_payload_decrypt(unsigned char *data,
115 bool private_message,
121 /****f* silccore/SilcMessageAPI/silc_message_payload_parse
126 * silc_message_payload_parse(unsigned char *payload,
127 * SilcUInt32 payload_len,
128 * bool private_message,
135 * Parses Message Payload returning new payload structure. This also
136 * decrypts the payload and checks the MAC. If the payload is
137 * channel message then `private_message' is FALSE, and if it is
138 * private message it is TRUE. If the private message key is static
139 * (pre-shared key) then protocol dictates that the IV is present
140 * and `static_key' must be set to TRUE. If the key is not static
141 * (Key Agreement was done for the key) then it MUST be FALSE. For
142 * channel messages the `static_key' is ignored.
144 * If the `hmac' is no provided then the MAC of the channel message is
145 * not verified. If the message is private message and `cipher' is NULL
146 * then this assumes that the packet was decrypted with session keys
147 * (no private message key) and this merely decodes the payload.
151 silc_message_payload_parse(unsigned char *payload,
152 SilcUInt32 payload_len,
153 bool private_message,
158 /****f* silccore/SilcMessageAPI/silc_message_payload_encrypt
162 * bool silc_message_payload_encrypt(unsigned char *data,
163 * SilcUInt32 data_len,
171 * This function is used to encrypt the Messsage Payload which is
172 * the `data' and `data_len'. The `data_len' is the data length which
173 * is used to create MAC out of. The `data' MUST have additional space
174 * after `data_len' bytes for the MAC which is appended to the data.
176 * This is usually used by the Message Payload interface itself but can
177 * be called by the appliation if separate encryption process is required.
178 * For example server might need to call this directly in some
179 * circumstances. The `cipher' is used to encrypt the payload and `hmac'
180 * to compute the MAC for the payload.
183 bool silc_message_payload_encrypt(unsigned char *data,
190 /****f* silccore/SilcMessageAPI/silc_message_payload_encode
194 * SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
195 * const unsigned char *data,
196 * SilcUInt32 data_len,
198 * bool private_message,
205 * Encodes a Message Payload into a buffer and returns it. This is
206 * used to encode channel messages and private messages into a packet.
207 * If `private_message' is FALSE then this encodes channel message, if
208 * it is TRUE this encodes private message. If `private_message' is
209 * TRUE then `generate_iv' MUST be FALSE if the private message key
210 * `cipher' is not static key (pre-shared key). If it is static key
211 * then protocol dictates that IV must be present in the Message Payload
212 * and `generate_iv' must be TRUE. The caller must know whether the key
213 * is static or not for private messages. If the key was generated with
214 * Key Agreement protocol then `generate_iv' is always FALSE. For
215 * channel messages `generate_iv' is always set to TRUE value.
217 * The `cipher' is the cipher used to encrypt the message and `hmac'
218 * is used to compute the MAC for the payload. If encoding private
219 * message that will be encrypted with session keys (no private message
220 * key) then `cipher' and `hmac' is NULL and this merely encodes the
221 * payload buffer, and the caller must encrypt the packet later.
223 * If `rng' is NULL then global RNG is used, if non-NULL then the
224 * `rng' is used (for IV and padding generation).
227 SilcBuffer silc_message_payload_encode(SilcMessageFlags flags,
228 const unsigned char *data,
231 bool private_message,
236 /****f* silccore/SilcMessageAPI/silc_message_payload_free
240 * void silc_message_payload_free(SilcMessagePayload payload);
244 * Free's Message Payload and all data in it.
247 void silc_message_payload_free(SilcMessagePayload payload);
249 /****f* silccore/SilcMessageAPI/silc_message_get_flags
253 * SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
257 * Returns the message flags from the payload.
260 SilcMessageFlags silc_message_get_flags(SilcMessagePayload payload);
262 /****f* silccore/SilcMessageAPI/silc_message_get_data
267 * silc_message_get_data(SilcMessagePayload payload,
268 * SilcUInt32 *data_len);
272 * Return the data in the payload, that is, the actual message data.
273 * The caller must not free it.
276 unsigned char *silc_message_get_data(SilcMessagePayload payload,
277 SilcUInt32 *data_len);
279 /****f* silccore/SilcMessageAPI/silc_message_get_mac
284 * silc_message_get_mac(SilcMessagePayload payload);
288 * Return the MAC of the payload. The caller must already know the
289 * length of the MAC. The caller must not free the MAC.
292 unsigned char *silc_message_get_mac(SilcMessagePayload payload);
294 /****f* silccore/SilcMessageAPI/silc_message_get_iv
299 * silc_message_get_iv(SilcMessagePayload payload);
303 * Return the IV of the payload. The caller must already know the
304 * length of the IV. The caller must not free the IV.
307 unsigned char *silc_message_get_iv(SilcMessagePayload payload);
309 /****s* silccore/SilcMessageAPI/SilcMessageSignedPayload
313 * typedef struct SilcMessageSignedPayloadStruct *SilcMessageSignedPayload;
318 * This context represents the SILC_MESSAGE_FLAG_SIGNED Payload which
319 * is used with channel messages and private messages to indicate that
320 * the message is digitally signed. This payload may include the
321 * message sender's public key and it includes the digital signature.
322 * This payload MUST NOT be used in any other context except with
323 * channel and private message sending and reception.
326 typedef struct SilcMessageSignedPayloadStruct *SilcMessageSignedPayload;
328 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_parse
332 * SilcMessageSignedPayload
333 * silc_message_signed_payload_parse(const unsigned char *data,
334 * SilcUInt32 data_len);
338 * Parses the SILC_MESSAGE_FLAG_SIGNED Payload from the `data' of
339 * length of `data_len' bytes. The `data' must be payload without
340 * the actual message payload. Returns the parsed payload or NULL
341 * on error. Caller must free the returned payload.
344 SilcMessageSignedPayload
345 silc_message_signed_payload_parse(const unsigned char *data,
346 SilcUInt32 data_len);
348 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_encode
353 * silc_message_signed_payload_encode(const unsigned char *message_payload,
354 * SilcUInt32 message_payload_len,
355 * SilcPublicKey public_key,
356 * SilcPrivateKey private_key,
357 * bool include_public_key);
361 * Encodes the SILC_MESSAGE_FLAG_SIGNED Payload and computes the
362 * digital signature. The `message_payload' is the message data that
363 * is used in the signature computation. The encoding of the buffer
364 * is specified in the SILC protocol. If `include_public_key' is
365 * TRUE then the public key included in the payload. The `private_key'
366 * is used to produce the signature. This function returns the encoded
367 * payload with the signature or NULL on error. Caller must free the
372 silc_message_signed_payload_encode(const unsigned char *message_payload,
373 SilcUInt32 message_payload_len,
374 SilcPublicKey public_key,
375 SilcPrivateKey private_key,
377 bool include_public_key);
379 /****f* silccore/SilcMessageAPI/silc_message_signed_payload_free
383 * void silc_message_signed_payload_free(SilcMessageSignedPayload sig);
387 * Frees the SILC_MESSAGE_FLAG_SIGNED Payload.
390 void silc_message_signed_payload_free(SilcMessageSignedPayload sig);
392 /****f* silccore/SilcMessageAPI/silc_message_signed_verify
396 * int silc_message_signed_verify(SilcMessageSignedPayload sig,
397 * SilcMessagePayload message,
398 * SilcPublicKey remote_public_key,
403 * This routine can be used to verify the signature found in
404 * SILC_MESSAGE_FLAG_SIGNED Payload. This returns SILC_AUTH_OK if the
405 * signature verification was successful.
408 int silc_message_signed_verify(SilcMessageSignedPayload sig,
409 SilcMessagePayload message,
410 SilcPublicKey remote_public_key,
413 /****f* silccore/SilcMessageAPI/silc_message_signed_get_public_key
418 * silc_message_signed_get_public_key(SilcMessageSignedPayload sig);
422 * Returns the public key from the SILC_MESSAGE_FLAG_SIGNED Payload
423 * or NULL if it does not include public key. The caller must free
424 * the returned public key.
428 silc_message_signed_get_public_key(SilcMessageSignedPayload sig);
430 #endif /* SILCMESSAGE_H */