defined a pb_tnc_message_t interface
[strongswan.git] / src / libcharon / plugins / tnccs_20 / messages / pb_pa_message.c
1 /*
2 * Copyright (C) 2010 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pb_pa_message.h"
17
18 #include <tls_writer.h>
19 #include <tls_reader.h>
20 #include <debug.h>
21
22 typedef struct private_pb_pa_message_t private_pb_pa_message_t;
23
24 /**
25 * PB-PA message
26 *
27 * 0 1 2 3
28 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
29 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 * | Flags | PA Message Vendor ID |
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 * | PA Subtype |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | Posture Collector Identifier | Posture Validator Identifier |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | PA Message Body (Variable Length) |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 */
39
40 #define PA_FLAG_NONE 0x00
41 #define PA_FLAG_EXCL (1<<7)
42
43 #define PB_PA_HEADER_SIZE 12
44
45 /**
46 * Private data of a pb_pa_message_t object.
47 *
48 */
49 struct private_pb_pa_message_t {
50 /**
51 * Public pb_pa_message_t interface.
52 */
53 pb_pa_message_t public;
54
55 /**
56 * PB-TNC message type
57 */
58 pb_tnc_msg_type_t type;
59
60 /**
61 * Exclusive flag
62 */
63 bool excl;
64
65 /**
66 * PA Message Vendor ID
67 */
68 u_int32_t vendor_id;
69
70 /**
71 * PA Subtype
72 */
73 u_int32_t subtype;
74
75 /**
76 * Posture Validator Identifier
77 */
78 u_int16_t collector_id;
79
80 /**
81 * Posture Validator Identifier
82 */
83 u_int16_t validator_id;
84
85 /**
86 * PA Message Body
87 */
88 chunk_t msg_body;
89
90 /**
91 * Encoded message
92 */
93 chunk_t encoding;
94 };
95
96 METHOD(pb_tnc_message_t, get_type, pb_tnc_msg_type_t,
97 private_pb_pa_message_t *this)
98 {
99 return this->type;
100 }
101
102 METHOD(pb_tnc_message_t, get_encoding, chunk_t,
103 private_pb_pa_message_t *this)
104 {
105 return this->encoding;
106 }
107
108 METHOD(pb_tnc_message_t, build, void,
109 private_pb_pa_message_t *this)
110 {
111 chunk_t msg_header;
112 tls_writer_t *writer;
113
114 /* build message header */
115 writer = tls_writer_create(PB_PA_HEADER_SIZE);
116 writer->write_uint8 (writer, this->excl ? PA_FLAG_EXCL : PA_FLAG_NONE);
117 writer->write_uint24(writer, this->vendor_id);
118 writer->write_uint32(writer, this->subtype);
119 writer->write_uint16(writer, this->collector_id);
120 writer->write_uint16(writer, this->validator_id);
121 msg_header = writer->get_buf(writer);
122
123 /* create encoding by concatenating message header and message body */
124 free(this->encoding.ptr);
125 this->encoding = chunk_cat("cc", msg_header, this->msg_body);
126 writer->destroy(writer);
127 }
128
129 METHOD(pb_tnc_message_t, process, status_t,
130 private_pb_pa_message_t *this)
131 {
132 u_int8_t flags;
133 size_t msg_body_len;
134 tls_reader_t *reader;
135
136 if (this->encoding.len < PB_PA_HEADER_SIZE)
137 {
138 DBG1(DBG_TNC,"%N message is shorter than header size of %u bytes",
139 pb_tnc_msg_type_names, PB_MSG_PA, PB_PA_HEADER_SIZE);
140 return FAILED;
141 }
142
143 /* process message header */
144 reader = tls_reader_create(this->encoding);
145 reader->read_uint8 (reader, &flags);
146 reader->read_uint24(reader, &this->vendor_id);
147 reader->read_uint32(reader, &this->subtype);
148 reader->read_uint16(reader, &this->collector_id);
149 reader->read_uint16(reader, &this->validator_id);
150 this->excl = ((flags & PA_FLAG_EXCL) != PA_FLAG_NONE);
151
152 /* process message body */
153 msg_body_len = reader->remaining(reader);
154 if (msg_body_len)
155 {
156 reader->read_data(reader, msg_body_len, &this->msg_body);
157 this->msg_body = chunk_clone(this->msg_body);
158 }
159 reader->destroy(reader);
160 return SUCCESS;
161 }
162
163 METHOD(pb_tnc_message_t, destroy, void,
164 private_pb_pa_message_t *this)
165 {
166 free(this->encoding.ptr);
167 free(this->msg_body.ptr);
168 free(this);
169 }
170
171 METHOD(pb_pa_message_t, get_vendor_id, u_int32_t,
172 private_pb_pa_message_t *this, u_int32_t *subtype)
173 {
174 *subtype = this->subtype;
175 return this->vendor_id;
176 }
177
178 METHOD(pb_pa_message_t, get_collector_id, u_int16_t,
179 private_pb_pa_message_t *this)
180 {
181 return this->collector_id;
182 }
183
184 METHOD(pb_pa_message_t, get_validator_id, u_int16_t,
185 private_pb_pa_message_t *this)
186 {
187 return this->validator_id;
188 }
189
190 METHOD(pb_pa_message_t, get_body, chunk_t,
191 private_pb_pa_message_t *this)
192 {
193 return this->msg_body;
194 }
195
196 METHOD(pb_pa_message_t, get_exclusive_flag, bool,
197 private_pb_pa_message_t *this)
198 {
199 return this->excl;
200 }
201
202 METHOD(pb_pa_message_t, set_exclusive_flag, void,
203 private_pb_pa_message_t *this, bool excl)
204 {
205 this->excl = excl;
206 }
207
208 /**
209 * See header
210 */
211 pb_tnc_message_t *pb_pa_message_create_from_data(chunk_t data)
212 {
213 private_pb_pa_message_t *this;
214
215 INIT(this,
216 .public = {
217 .pb_interface = {
218 .get_type = _get_type,
219 .get_encoding = _get_encoding,
220 .process = _process,
221 .destroy = _destroy,
222 },
223 .get_vendor_id = _get_vendor_id,
224 .get_collector_id = _get_collector_id,
225 .get_validator_id = _get_validator_id,
226 .get_body = _get_body,
227 .get_exclusive_flag = _get_exclusive_flag,
228 .set_exclusive_flag = _set_exclusive_flag,
229 },
230 .encoding = chunk_clone(data),
231 );
232
233 return &this->public.pb_interface;
234 }
235
236 /**
237 * See header
238 */
239 pb_tnc_message_t *pb_pa_message_create(u_int32_t vendor_id, u_int32_t subtype,
240 u_int16_t collector_id,
241 u_int16_t validator_id,
242 chunk_t msg_body)
243 {
244 private_pb_pa_message_t *this;
245
246 INIT(this,
247 .public = {
248 .pb_interface = {
249 .get_type = _get_type,
250 .get_encoding = _get_encoding,
251 .build = _build,
252 .process = _process,
253 .destroy = _destroy,
254 },
255 .get_vendor_id = _get_vendor_id,
256 .get_collector_id = _get_collector_id,
257 .get_validator_id = _get_validator_id,
258 .get_body = _get_body,
259 .get_exclusive_flag = _get_exclusive_flag,
260 .set_exclusive_flag = _set_exclusive_flag,
261 },
262 .vendor_id = vendor_id,
263 .subtype = subtype,
264 .collector_id = collector_id,
265 .validator_id = validator_id,
266 .msg_body = chunk_clone(msg_body),
267 );
268
269 return &this->public.pb_interface;
270 }