updates.
[silc.git] / lib / silccore / silcprivate.c
1 /*
2
3   silcprivate.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
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.
18
19 */
20 /* Includes the Private Message Payload implementation */
21 /* $Id$ */
22
23 #include "silcincludes.h"
24 #include "silcprivate.h"
25
26 /******************************************************************************
27
28                            Private Message Payload
29
30 ******************************************************************************/
31
32 #define SILC_PRIVATE_MESSAGE_PAD(__payloadlen) (16 - (__payloadlen) % 16)
33
34 /* Private Message Payload structure. Contents of this structure is parsed
35    from SILC packets. */
36 struct SilcPrivateMessagePayloadStruct {
37   uint16 flags;
38   uint16 message_len;
39   unsigned char *message;
40 };
41
42 /* Parses private message payload returning new private mesage payload 
43    structure. This also decrypts the message if the `cipher' is provided. */
44
45 SilcPrivateMessagePayload 
46 silc_private_message_payload_parse(SilcBuffer buffer, SilcCipher cipher)
47 {
48   SilcPrivateMessagePayload new;
49   int ret;
50
51   SILC_LOG_DEBUG(("Parsing private message payload"));
52
53   /* Decrypt the payload */
54   if (cipher)
55     silc_cipher_decrypt(cipher, buffer->data, buffer->data, 
56                         buffer->len, cipher->iv);
57
58   new = silc_calloc(1, sizeof(*new));
59
60   /* Parse the Private Message Payload. Ignore the padding. */
61   ret = silc_buffer_unformat(buffer,
62                              SILC_STR_UI_SHORT(&new->flags),
63                              SILC_STR_UI16_NSTRING_ALLOC(&new->message, 
64                                                          &new->message_len),
65                              SILC_STR_END);
66   if (ret == -1) {
67     SILC_LOG_DEBUG(("Incorrect private message payload"));
68     goto err;
69   }
70
71   if ((new->message_len < 1 || new->message_len > buffer->len)) {
72     SILC_LOG_DEBUG(("Incorrect private message payload in packet, "
73                     "packet dropped"));
74     goto err;
75   }
76
77   return new;
78
79  err:
80   silc_private_message_payload_free(new);
81   return NULL;
82 }
83
84 /* Encodes private message payload into a buffer and returns it.  If
85    the cipher is provided the packet is also encrypted here.  It is provided
86    if the private message private keys are used. */
87
88 SilcBuffer silc_private_message_payload_encode(uint16 flags,
89                                                uint16 data_len,
90                                                unsigned char *data,
91                                                SilcCipher cipher)
92 {
93   int i;
94   SilcBuffer buffer;
95   uint32 len, pad_len = 0;
96   unsigned char pad[16];
97
98   SILC_LOG_DEBUG(("Encoding private message payload"));
99
100   len = 4 + data_len;
101
102   if (cipher) {
103     /* Calculate length of padding. */
104     pad_len = SILC_PRIVATE_MESSAGE_PAD(len);
105     len += pad_len;
106
107     /* Generate padding */
108     for (i = 0; i < pad_len; i++) pad[i] = silc_rng_global_get_byte();
109   }
110
111   /* Allocate private message payload buffer */
112   buffer = silc_buffer_alloc(len);
113
114   /* Encode the Channel Message Payload */
115   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
116   silc_buffer_format(buffer, 
117                      SILC_STR_UI_SHORT(flags),
118                      SILC_STR_UI_SHORT(data_len),
119                      SILC_STR_UI_XNSTRING(data, data_len),
120                      SILC_STR_UI_XNSTRING(pad, pad_len),
121                      SILC_STR_END);
122
123   if (cipher) {
124     /* Encrypt payload of the packet. */
125     silc_cipher_encrypt(cipher, buffer->data, buffer->data, 
126                         buffer->len, cipher->iv);
127     memset(pad, 0, sizeof(pad));
128   }
129
130   return buffer;
131 }
132
133 /* Frees Private Message Payload */
134
135 void silc_private_message_payload_free(SilcPrivateMessagePayload payload)
136 {
137   if (payload->message) {
138     memset(payload->message, 0, payload->message_len);
139     silc_free(payload->message);
140   }
141   silc_free(payload);
142 }
143
144 /* Return flags */
145
146 uint16 
147 silc_private_message_get_flags(SilcPrivateMessagePayload payload)
148 {
149   return payload->flags;
150 }
151
152 /* Return message */
153
154 unsigned char *
155 silc_private_message_get_message(SilcPrivateMessagePayload payload,
156                                  uint32 *message_len)
157 {
158   if (message_len)
159     *message_len = payload->message_len;
160
161   return payload->message;
162 }