fb53212e965926b922c320a5df436cdf21303da8
[strongswan.git] / src / libimcv / tcg / seg / tcg_seg_attr_seg_env.c
1 /*
2 * Copyright (C) 2014 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 "tcg_seg_attr_seg_env.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <utils/debug.h>
22
23 typedef struct private_tcg_seg_attr_seg_env_t private_tcg_seg_attr_seg_env_t;
24
25 /**
26 * Attribute Segment Envelope
27 * see TCG IF-M Segmentation 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 * |M|S| Reserved | Base Attribute ID |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | Segment Value (Variable Length) |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 */
37
38 /**
39 * Private data of an tcg_seg_attr_seg_env_t object.
40 */
41 struct private_tcg_seg_attr_seg_env_t {
42
43 /**
44 * Public members of tcg_seg_attr_seg_env_t
45 */
46 tcg_seg_attr_seg_env_t public;
47
48 /**
49 * Vendor-specific attribute type
50 */
51 pen_type_t type;
52
53 /**
54 * Length of attribute value
55 */
56 size_t length;
57
58 /**
59 * Attribute value or segment
60 */
61 chunk_t value;
62
63 /**
64 * Noskip flag
65 */
66 bool noskip_flag;
67
68 /**
69 * PA-TNC segmentation flags
70 */
71 uint8_t flags;
72
73 /**
74 * Base Attribute ID
75 */
76 uint32_t base_attr_id;
77
78 /**
79 * Attribute value
80 */
81 chunk_t segment;
82
83 /**
84 * Reference count
85 */
86 refcount_t ref;
87 };
88
89 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
90 private_tcg_seg_attr_seg_env_t *this)
91 {
92 return this->type;
93 }
94
95 METHOD(pa_tnc_attr_t, get_value, chunk_t,
96 private_tcg_seg_attr_seg_env_t *this)
97 {
98 return this->value;
99 }
100
101 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
102 private_tcg_seg_attr_seg_env_t *this)
103 {
104 return this->noskip_flag;
105 }
106
107 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
108 private_tcg_seg_attr_seg_env_t *this, bool noskip)
109 {
110 this->noskip_flag = noskip;
111 }
112
113 METHOD(pa_tnc_attr_t, build, void,
114 private_tcg_seg_attr_seg_env_t *this)
115 {
116 /* constructor already allocated and built value */
117 this->length = this->value.len;
118 return;
119 }
120
121 METHOD(pa_tnc_attr_t, process, status_t,
122 private_tcg_seg_attr_seg_env_t *this, u_int32_t *offset)
123 {
124 bio_reader_t *reader;
125
126 *offset = 0;
127
128 if (this->value.len < this->length)
129 {
130 DBG1(DBG_TNC, "segmentation not allowed for %N", tcg_attr_names,
131 this->type.type);
132 return FAILED;
133 }
134 if (this->value.len < TCG_SEG_ATTR_SEG_ENV_HEADER)
135 {
136 DBG1(DBG_TNC, "insufficient data for %N", tcg_attr_names,
137 this->type.type);
138 return FAILED;
139 }
140 reader = bio_reader_create(this->value);
141 reader->read_uint8 (reader, &this->flags);
142 reader->read_uint24(reader, &this->base_attr_id);
143 reader->destroy(reader);
144
145 return SUCCESS;
146 }
147
148 METHOD(pa_tnc_attr_t, destroy, void,
149 private_tcg_seg_attr_seg_env_t *this)
150 {
151 if (ref_put(&this->ref))
152 {
153 free(this->value.ptr);
154 free(this);
155 }
156 }
157
158 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
159 private_tcg_seg_attr_seg_env_t *this)
160 {
161 ref_get(&this->ref);
162 return &this->public.pa_tnc_attribute;
163 }
164
165 METHOD(tcg_seg_attr_seg_env_t, get_segment, chunk_t,
166 private_tcg_seg_attr_seg_env_t *this, uint8_t *flags)
167 {
168 if (flags)
169 {
170 *flags = this->flags;
171 }
172 return chunk_skip(this->value, TCG_SEG_ATTR_SEG_ENV_HEADER);
173 }
174
175 METHOD(tcg_seg_attr_seg_env_t, get_base_attr_id, uint32_t,
176 private_tcg_seg_attr_seg_env_t *this)
177 {
178 return this->base_attr_id;
179 }
180
181 /**
182 * Described in header.
183 */
184 pa_tnc_attr_t* tcg_seg_attr_seg_env_create(chunk_t segment, uint8_t flags,
185 uint32_t base_attr_id)
186 {
187 private_tcg_seg_attr_seg_env_t *this;
188
189 INIT(this,
190 .public = {
191 .pa_tnc_attribute = {
192 .get_type = _get_type,
193 .get_value = _get_value,
194 .get_noskip_flag = _get_noskip_flag,
195 .set_noskip_flag = _set_noskip_flag,
196 .build = _build,
197 .process = _process,
198 .get_ref = _get_ref,
199 .destroy = _destroy,
200 },
201 .get_base_attr_id = _get_base_attr_id,
202 .get_segment = _get_segment,
203 },
204 .type = { PEN_TCG, TCG_SEG_ATTR_SEG_ENV },
205 .flags = flags,
206 .base_attr_id = base_attr_id,
207 .value = chunk_alloc(TCG_SEG_ATTR_SEG_ENV_HEADER + segment.len),
208 .ref = 1,
209 );
210
211 htoun32(this->value.ptr, base_attr_id);
212 *this->value.ptr = flags;
213 memcpy(this->value.ptr + TCG_SEG_ATTR_SEG_ENV_HEADER,
214 segment.ptr, segment.len);
215
216 return &this->public.pa_tnc_attribute;
217 }
218
219 /**
220 * Described in header.
221 */
222 pa_tnc_attr_t *tcg_seg_attr_seg_env_create_from_data(size_t length,
223 chunk_t data)
224 {
225 private_tcg_seg_attr_seg_env_t *this;
226
227 INIT(this,
228 .public = {
229 .pa_tnc_attribute = {
230 .get_type = _get_type,
231 .get_value = _get_value,
232 .get_noskip_flag = _get_noskip_flag,
233 .set_noskip_flag = _set_noskip_flag,
234 .build = _build,
235 .process = _process,
236 .get_ref = _get_ref,
237 .destroy = _destroy,
238 },
239 .get_base_attr_id = _get_base_attr_id,
240 .get_segment = _get_segment,
241 },
242 .type = { PEN_TCG, TCG_SEG_ATTR_SEG_ENV },
243 .length = length,
244 .value = chunk_clone(data),
245 .ref = 1,
246 );
247
248 return &this->public.pa_tnc_attribute;
249 }