1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Copyright (C) 2006 Mike McCauley (mikem@open.com.au)
* Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "imc_imv_msg.h"
#include <tnc/tnccs/tnccs.h>
#include <utils/lexparser.h>
#include <utils/debug.h>
typedef struct private_imc_imv_msg_t private_imc_imv_msg_t;
#define BYTES_PER_LINE 57
/**
* Private data of a imc_imv_msg_t object.
*
*/
struct private_imc_imv_msg_t {
/**
* Public imc_imv_msg_t interface.
*/
imc_imv_msg_t public;
/**
* TNCCS message type
*/
tnccs_msg_type_t type;
/**
* XML-encoded message node
*/
xmlNodePtr node;
/**
* IMC-IMV message type
*/
TNC_MessageType msg_type;
/**
* IMC-IMV message body
*/
chunk_t msg_body;
};
/**
* Encodes message data into multiple base64-encoded lines
*/
static chunk_t encode_base64(chunk_t data)
{
chunk_t encoding;
u_char *pos;
size_t b64_chars, b64_lines;
/* handle empty message data object */
if (data.len == 0)
{
encoding = chunk_alloc(1);
*encoding.ptr = '\0';
return encoding;
}
/* compute and allocate maximum size of base64 object */
b64_chars = 4 * ((data.len + 2) / 3);
b64_lines = (data.len + BYTES_PER_LINE - 1) / BYTES_PER_LINE;
encoding = chunk_alloc(b64_chars + b64_lines);
pos = encoding.ptr;
/* encode lines */
while (b64_lines--)
{
chunk_t data_line, b64_line;
data_line = chunk_create(data.ptr, min(data.len, BYTES_PER_LINE));
data.ptr += data_line.len;
data.len -= data_line.len;
b64_line = chunk_to_base64(data_line, pos);
pos += b64_line.len;
*pos = '\n';
pos++;
}
/* terminate last line with NULL character instead of newline */
*(pos-1) = '\0';
return encoding;
}
/**
* Decodes message data from multiple base64-encoded lines
*/
static chunk_t decode_base64(chunk_t data)
{
chunk_t decoding, data_line, b64_line;
u_char *pos;
/* compute and allocate maximum size of decoded message data */
decoding = chunk_alloc(3 * ((data.len + 3) / 4));
pos = decoding.ptr;
decoding.len = 0;
while (fetchline(&data, &b64_line))
{
data_line = chunk_from_base64(b64_line, pos);
pos += data_line.len;
decoding.len += data_line.len;
}
return decoding;
}
METHOD(tnccs_msg_t, get_type, tnccs_msg_type_t,
private_imc_imv_msg_t *this)
{
return this->type;
}
METHOD(tnccs_msg_t, get_node, xmlNodePtr,
private_imc_imv_msg_t *this)
{
return this->node;
}
METHOD(tnccs_msg_t, destroy, void,
private_imc_imv_msg_t *this)
{
free(this->msg_body.ptr);
free(this);
}
METHOD(imc_imv_msg_t, get_msg_type, TNC_MessageType,
private_imc_imv_msg_t *this)
{
return this->msg_type;
}
METHOD(imc_imv_msg_t, get_msg_body, chunk_t,
private_imc_imv_msg_t *this)
{
return this->msg_body;
}
/**
* See header
*/
tnccs_msg_t *imc_imv_msg_create_from_node(xmlNodePtr node, linked_list_t *errors)
{
private_imc_imv_msg_t *this;
xmlNsPtr ns;
xmlNodePtr cur;
xmlChar *content;
chunk_t b64_body;
INIT(this,
.public = {
.tnccs_msg_interface = {
.get_type = _get_type,
.get_node = _get_node,
.destroy = _destroy,
},
.get_msg_type = _get_msg_type,
.get_msg_body = _get_msg_body,
},
.type = IMC_IMV_MSG,
.node = node,
);
ns = node->ns;
cur = node->xmlChildrenNode;
while (cur)
{
if (streq((char*)cur->name, "Type") && cur->ns == ns)
{
content = xmlNodeGetContent(cur);
this->msg_type = strtoul((char*)content, NULL, 16);
xmlFree(content);
}
else if (streq((char*)cur->name, "Base64") && cur->ns == ns)
{
content = xmlNodeGetContent(cur);
b64_body = chunk_create((char*)content, strlen((char*)content));
this->msg_body = decode_base64(b64_body);
xmlFree(content);
}
cur = cur->next;
}
return &this->public.tnccs_msg_interface;
}
/**
* See header
*/
tnccs_msg_t *imc_imv_msg_create(TNC_MessageType msg_type, chunk_t msg_body)
{
private_imc_imv_msg_t *this;
chunk_t b64_body;
char buf[10]; /* big enough for hex-encoded message type */
xmlNodePtr n;
INIT(this,
.public = {
.tnccs_msg_interface = {
.get_type = _get_type,
.get_node = _get_node,
.destroy = _destroy,
},
.get_msg_type = _get_msg_type,
.get_msg_body = _get_msg_body,
},
.type = IMC_IMV_MSG,
.node = xmlNewNode(NULL, BAD_CAST "IMC-IMV-Message"),
.msg_type = msg_type,
.msg_body = chunk_clone(msg_body),
);
/* add the message type number in hex */
n = xmlNewNode(NULL, BAD_CAST "Type");
snprintf(buf, 10, "%08x", this->msg_type);
xmlNodeSetContent(n, BAD_CAST buf);
xmlAddChild(this->node, n);
/* encode the message as a Base64 node */
n = xmlNewNode(NULL, BAD_CAST "Base64");
b64_body = encode_base64(this->msg_body);
xmlNodeSetContent(n, BAD_CAST b64_body.ptr);
xmlAddChild(this->node, n);
free(b64_body.ptr);
return &this->public.tnccs_msg_interface;
}
|