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