shortened some type names and enforced encoding rules
[strongswan.git] / src / libimcv / tcg / tcg_pts_attr_req_proto_caps.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
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 "tcg_pts_attr_req_proto_caps.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <debug.h>
22
23 typedef struct private_tcg_pts_attr_req_proto_caps_t private_tcg_pts_attr_req_proto_caps_t;
24
25 /**
26 * Request PTS Protocol Capabilities
27 * see section 3.6 of PTS Protocol: Binding to TNC IF-M Specification
28 *
29 * 1 2 3
30 * 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
31 *
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Reserved |C|V|D|T|X|
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 *
36 */
37
38 #define PTS_PROTO_CAPS_SIZE 4
39 #define PTS_PROTO_CAPS_RESERVED 0x00
40
41 /**
42 * Private data of an tcg_pts_attr_req_proto_caps_t object.
43 */
44 struct private_tcg_pts_attr_req_proto_caps_t {
45
46 /**
47 * Public members of tcg_pts_attr_req_proto_caps_t
48 */
49 tcg_pts_attr_req_proto_caps_t public;
50
51 /**
52 * Attribute vendor ID
53 */
54 pen_t vendor_id;
55
56 /**
57 * Attribute type
58 */
59 u_int32_t type;
60
61 /**
62 * Attribute value
63 */
64 chunk_t value;
65
66 /**
67 * Noskip flag
68 */
69 bool noskip_flag;
70
71 /**
72 * Set of flags
73 */
74 pts_proto_caps_flag_t flags;
75
76 };
77
78 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
79 private_tcg_pts_attr_req_proto_caps_t *this)
80 {
81 return this->vendor_id;
82 }
83
84 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
85 private_tcg_pts_attr_req_proto_caps_t *this)
86 {
87 return this->type;
88 }
89
90 METHOD(pa_tnc_attr_t, get_value, chunk_t,
91 private_tcg_pts_attr_req_proto_caps_t *this)
92 {
93 return this->value;
94 }
95
96 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
97 private_tcg_pts_attr_req_proto_caps_t *this)
98 {
99 return this->noskip_flag;
100 }
101
102 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
103 private_tcg_pts_attr_req_proto_caps_t *this, bool noskip)
104 {
105 this->noskip_flag = noskip;
106 }
107
108 METHOD(pa_tnc_attr_t, build, void,
109 private_tcg_pts_attr_req_proto_caps_t *this)
110 {
111 bio_writer_t *writer;
112 u_int8_t flags = 0;
113
114 writer = bio_writer_create(PTS_PROTO_CAPS_SIZE);
115 writer->write_uint24 (writer, PTS_PROTO_CAPS_RESERVED);
116
117 /* Determine the flags to set*/
118 if (this->flags & PTS_PROTO_CAPS_XML)
119 {
120 flags += 1;
121 }
122 if (this->flags & PTS_PROTO_CAPS_T)
123 {
124 flags += 2;
125 }
126 if (this->flags & PTS_PROTO_CAPS_DH)
127 {
128 flags += 4;
129 }
130 if (this->flags & PTS_PROTO_CAPS_VER)
131 {
132 flags += 8;
133 }
134 if (this->flags & PTS_PROTO_CAPS_CURRENT)
135 {
136 flags += 16;
137 }
138 writer->write_uint8(writer, flags);
139
140 this->value = chunk_clone(writer->get_buf(writer));
141 writer->destroy(writer);
142 }
143
144 METHOD(pa_tnc_attr_t, process, status_t,
145 private_tcg_pts_attr_req_proto_caps_t *this, u_int32_t *offset)
146 {
147 bio_reader_t *reader;
148 u_int32_t reserved;
149 u_int8_t flags;
150
151 if (this->value.len < PTS_PROTO_CAPS_SIZE)
152 {
153 DBG1(DBG_TNC, "insufficient data for Request PTS Protocol Capabilities");
154 *offset = 0;
155 return FAILED;
156 }
157 reader = bio_reader_create(this->value);
158 reader->read_uint24 (reader, &reserved);
159 reader->read_uint8(reader, &flags);
160
161 if ((flags >> 0) & 1)
162 {
163 this->flags |= PTS_PROTO_CAPS_XML;
164 }
165 if ((flags >> 1) & 1)
166 {
167 this->flags |= PTS_PROTO_CAPS_T;
168 }
169 if ((flags >> 2) & 1)
170 {
171 this->flags |= PTS_PROTO_CAPS_DH;
172 }
173 if ((flags >> 3) & 1)
174 {
175 this->flags |= PTS_PROTO_CAPS_VER;
176 }
177 if ((flags >> 4) & 1)
178 {
179 this->flags |= PTS_PROTO_CAPS_CURRENT;
180 }
181
182 reader->destroy(reader);
183
184 return SUCCESS;
185 }
186
187 METHOD(pa_tnc_attr_t, destroy, void,
188 private_tcg_pts_attr_req_proto_caps_t *this)
189 {
190 free(this->value.ptr);
191 free(this);
192 }
193
194 METHOD(tcg_pts_attr_req_proto_caps_t, get_flags, pts_proto_caps_flag_t,
195 private_tcg_pts_attr_req_proto_caps_t *this)
196 {
197 return this->flags;
198 }
199
200 METHOD(tcg_pts_attr_req_proto_caps_t, set_flags, void,
201 private_tcg_pts_attr_req_proto_caps_t *this, pts_proto_caps_flag_t flags)
202 {
203 this->flags = flags;
204 }
205
206 /**
207 * Described in header.
208 */
209 pa_tnc_attr_t *tcg_pts_attr_req_proto_caps_create(pts_proto_caps_flag_t flags)
210 {
211 private_tcg_pts_attr_req_proto_caps_t *this;
212
213 INIT(this,
214 .public = {
215 .pa_tnc_attribute = {
216 .get_vendor_id = _get_vendor_id,
217 .get_type = _get_type,
218 .get_value = _get_value,
219 .get_noskip_flag = _get_noskip_flag,
220 .set_noskip_flag = _set_noskip_flag,
221 .build = _build,
222 .process = _process,
223 .destroy = _destroy,
224 },
225 .get_flags = _get_flags,
226 .set_flags = _set_flags,
227 },
228 .vendor_id = PEN_TCG,
229 .type = TCG_PTS_REQ_PROTO_CAPS,
230 .flags = flags,
231 );
232
233 return &this->public.pa_tnc_attribute;
234 }
235
236
237 /**
238 * Described in header.
239 */
240 pa_tnc_attr_t *tcg_pts_attr_req_proto_caps_create_from_data(chunk_t data)
241 {
242 private_tcg_pts_attr_req_proto_caps_t *this;
243
244 INIT(this,
245 .public = {
246 .pa_tnc_attribute = {
247 .get_vendor_id = _get_vendor_id,
248 .get_type = _get_type,
249 .get_value = _get_value,
250 .get_noskip_flag = _get_noskip_flag,
251 .set_noskip_flag = _set_noskip_flag,
252 .build = _build,
253 .process = _process,
254 .destroy = _destroy,
255 },
256 .get_flags = _get_flags,
257 .set_flags = _set_flags,
258 },
259 .vendor_id = PEN_TCG,
260 .type = TCG_PTS_REQ_PROTO_CAPS,
261 .value = chunk_clone(data),
262 );
263
264 return &this->public.pa_tnc_attribute;
265 }