Integer type name change.
[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   SilcUInt16 flags;
38   SilcUInt16 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(unsigned char *payload,
47                                    SilcUInt32 payload_len,
48                                    SilcCipher cipher)
49 {
50   SilcBufferStruct buffer;
51   SilcPrivateMessagePayload new;
52   int ret;
53
54   SILC_LOG_DEBUG(("Parsing private message payload"));
55
56   silc_buffer_set(&buffer, payload, payload_len);
57
58   /* Decrypt the payload */
59   if (cipher)
60     silc_cipher_decrypt(cipher, buffer.data, buffer.data, 
61                         buffer.len, cipher->iv);
62
63   new = silc_calloc(1, sizeof(*new));
64
65   /* Parse the Private Message Payload. Ignore the padding. */
66   ret = silc_buffer_unformat(&buffer,
67                              SILC_STR_UI_SHORT(&new->flags),
68                              SILC_STR_UI16_NSTRING_ALLOC(&new->message, 
69                                                          &new->message_len),
70                              SILC_STR_END);
71   if (ret == -1) {
72     SILC_LOG_DEBUG(("Incorrect private message payload"));
73     goto err;
74   }
75
76   if ((new->message_len < 1 || new->message_len > buffer.len)) {
77     SILC_LOG_DEBUG(("Incorrect private message payload in packet, "
78                     "packet dropped"));
79     goto err;
80   }
81
82   return new;
83
84  err:
85   silc_private_message_payload_free(new);
86   return NULL;
87 }
88
89 /* Encodes private message payload into a buffer and returns it.  If
90    the cipher is provided the packet is also encrypted here.  It is provided
91    if the private message private keys are used. */
92
93 SilcBuffer silc_private_message_payload_encode(SilcUInt16 flags,
94                                                SilcUInt16 data_len,
95                                                const unsigned char *data,
96                                                SilcCipher cipher)
97 {
98   int i;
99   SilcBuffer buffer;
100   SilcUInt32 len, pad_len = 0;
101   unsigned char pad[16];
102
103   SILC_LOG_DEBUG(("Encoding private message payload"));
104
105   len = 4 + data_len;
106
107   if (cipher) {
108     /* Calculate length of padding. */
109     pad_len = SILC_PRIVATE_MESSAGE_PAD(len);
110     len += pad_len;
111
112     /* Generate padding */
113     for (i = 0; i < pad_len; i++) pad[i] = silc_rng_global_get_byte();
114   }
115
116   /* Allocate private message payload buffer */
117   buffer = silc_buffer_alloc(len);
118
119   /* Encode the Channel Message Payload */
120   silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
121   silc_buffer_format(buffer, 
122                      SILC_STR_UI_SHORT(flags),
123                      SILC_STR_UI_SHORT(data_len),
124                      SILC_STR_UI_XNSTRING(data, data_len),
125                      SILC_STR_UI_XNSTRING(pad, pad_len),
126                      SILC_STR_END);
127
128   if (cipher) {
129     /* Encrypt payload of the packet. */
130     silc_cipher_encrypt(cipher, buffer->data, buffer->data, 
131                         buffer->len, cipher->iv);
132     memset(pad, 0, sizeof(pad));
133   }
134
135   return buffer;
136 }
137
138 /* Frees Private Message Payload */
139
140 void silc_private_message_payload_free(SilcPrivateMessagePayload payload)
141 {
142   if (payload->message) {
143     memset(payload->message, 0, payload->message_len);
144     silc_free(payload->message);
145   }
146   silc_free(payload);
147 }
148
149 /* Return flags */
150
151 SilcUInt16 
152 silc_private_message_get_flags(SilcPrivateMessagePayload payload)
153 {
154   return payload->flags;
155 }
156
157 /* Return message */
158
159 unsigned char *
160 silc_private_message_get_message(SilcPrivateMessagePayload payload,
161                                  SilcUInt32 *message_len)
162 {
163   if (message_len)
164     *message_len = payload->message_len;
165
166   return payload->message;
167 }