5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 /* Includes the Private Message Payload implementation */
23 #include "silcincludes.h"
24 #include "silcprivate.h"
26 /******************************************************************************
28 Private Message Payload
30 ******************************************************************************/
32 /* Private Message Payload structure. Contents of this structure is parsed
34 struct SilcPrivateMessagePayloadStruct {
37 unsigned char *message;
40 /* Parses private message payload returning new private mesage payload
41 structure. This also decrypts the message if the `cipher' is provided. */
43 SilcPrivateMessagePayload
44 silc_private_message_payload_parse(SilcBuffer buffer, SilcCipher cipher)
46 SilcPrivateMessagePayload new;
49 SILC_LOG_DEBUG(("Parsing private message payload"));
51 /* Decrypt the payload */
53 silc_cipher_decrypt(cipher, buffer->data, buffer->data,
54 buffer->len, cipher->iv);
56 new = silc_calloc(1, sizeof(*new));
58 /* Parse the Private Message Payload. Ignore the padding. */
59 ret = silc_buffer_unformat(buffer,
60 SILC_STR_UI_SHORT(&new->flags),
61 SILC_STR_UI16_NSTRING_ALLOC(&new->message,
65 SILC_LOG_ERROR(("Incorrect private message payload"));
69 if ((new->message_len < 1 || new->message_len > buffer->len)) {
70 SILC_LOG_ERROR(("Incorrect private message payload in packet, "
78 silc_private_message_payload_free(new);
82 /* Encodes private message payload into a buffer and returns it. If
83 the cipher is provided the packet is also encrypted here. It is provided
84 if the private message private keys are used. */
86 SilcBuffer silc_private_message_payload_encode(uint16 flags,
93 uint32 len, pad_len = 0;
94 unsigned char pad[SILC_PACKET_MAX_PADLEN];
96 SILC_LOG_DEBUG(("Encoding private message payload"));
101 /* Calculate length of padding. */
102 pad_len = SILC_PACKET_PADLEN((len + 2));
105 /* Generate padding */
106 for (i = 0; i < pad_len; i++) pad[i] = silc_rng_global_get_byte();
109 /* Allocate private message payload buffer */
110 buffer = silc_buffer_alloc(len);
112 /* Encode the Channel Message Payload */
113 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
114 silc_buffer_format(buffer,
115 SILC_STR_UI_SHORT(flags),
116 SILC_STR_UI_SHORT(data_len),
117 SILC_STR_UI_XNSTRING(data, data_len),
118 SILC_STR_UI_XNSTRING(pad, pad_len),
122 /* Encrypt payload of the packet. */
123 silc_cipher_encrypt(cipher, buffer->data, buffer->data,
124 buffer->len, cipher->iv);
125 memset(pad, 0, sizeof(pad));
131 /* Frees Private Message Payload */
133 void silc_private_message_payload_free(SilcPrivateMessagePayload payload)
135 if (payload->message) {
136 memset(payload->message, 0, payload->message_len);
137 silc_free(payload->message);
145 silc_private_message_get_flags(SilcPrivateMessagePayload payload)
147 return payload->flags;
153 silc_private_message_get_message(SilcPrivateMessagePayload payload,
157 *message_len = payload->message_len;
159 return payload->message;