]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libimcv/tcg/seg/tcg_seg_attr_seg_env.c
7cd585a04ad63e2f63e1ce1e612b47914d76fdf4
[thirdparty/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, uint32_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/%N", pen_names, PEN_TCG,
131 tcg_attr_names, 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/%N", pen_names, PEN_TCG,
137 tcg_attr_names, 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, add_segment, void,
149 private_tcg_seg_attr_seg_env_t *this, chunk_t segment)
150 {
151 /* no segments are expected */
152 }
153
154 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
155 private_tcg_seg_attr_seg_env_t *this)
156 {
157 ref_get(&this->ref);
158 return &this->public.pa_tnc_attribute;
159 }
160
161 METHOD(pa_tnc_attr_t, destroy, void,
162 private_tcg_seg_attr_seg_env_t *this)
163 {
164 if (ref_put(&this->ref))
165 {
166 free(this->value.ptr);
167 free(this);
168 }
169 }
170
171 METHOD(tcg_seg_attr_seg_env_t, get_segment, chunk_t,
172 private_tcg_seg_attr_seg_env_t *this, uint8_t *flags)
173 {
174 if (flags)
175 {
176 *flags = this->flags;
177 }
178 return chunk_skip(this->value, TCG_SEG_ATTR_SEG_ENV_HEADER);
179 }
180
181 METHOD(tcg_seg_attr_seg_env_t, get_base_attr_id, uint32_t,
182 private_tcg_seg_attr_seg_env_t *this)
183 {
184 return this->base_attr_id;
185 }
186
187 /**
188 * Described in header.
189 */
190 pa_tnc_attr_t* tcg_seg_attr_seg_env_create(chunk_t segment, uint8_t flags,
191 uint32_t base_attr_id)
192 {
193 private_tcg_seg_attr_seg_env_t *this;
194
195 INIT(this,
196 .public = {
197 .pa_tnc_attribute = {
198 .get_type = _get_type,
199 .get_value = _get_value,
200 .get_noskip_flag = _get_noskip_flag,
201 .set_noskip_flag = _set_noskip_flag,
202 .build = _build,
203 .process = _process,
204 .add_segment = _add_segment,
205 .get_ref = _get_ref,
206 .destroy = _destroy,
207 },
208 .get_base_attr_id = _get_base_attr_id,
209 .get_segment = _get_segment,
210 },
211 .type = { PEN_TCG, TCG_SEG_ATTR_SEG_ENV },
212 .flags = flags,
213 .base_attr_id = base_attr_id,
214 .value = chunk_alloc(TCG_SEG_ATTR_SEG_ENV_HEADER + segment.len),
215 .ref = 1,
216 );
217
218 htoun32(this->value.ptr, base_attr_id);
219 *this->value.ptr = flags;
220 memcpy(this->value.ptr + TCG_SEG_ATTR_SEG_ENV_HEADER,
221 segment.ptr, segment.len);
222
223 return &this->public.pa_tnc_attribute;
224 }
225
226 /**
227 * Described in header.
228 */
229 pa_tnc_attr_t *tcg_seg_attr_seg_env_create_from_data(size_t length,
230 chunk_t data)
231 {
232 private_tcg_seg_attr_seg_env_t *this;
233
234 INIT(this,
235 .public = {
236 .pa_tnc_attribute = {
237 .get_type = _get_type,
238 .get_value = _get_value,
239 .get_noskip_flag = _get_noskip_flag,
240 .set_noskip_flag = _set_noskip_flag,
241 .build = _build,
242 .process = _process,
243 .add_segment = _add_segment,
244 .get_ref = _get_ref,
245 .destroy = _destroy,
246 },
247 .get_base_attr_id = _get_base_attr_id,
248 .get_segment = _get_segment,
249 },
250 .type = { PEN_TCG, TCG_SEG_ATTR_SEG_ENV },
251 .length = length,
252 .value = chunk_clone(data),
253 .ref = 1,
254 );
255
256 return &this->public.pa_tnc_attribute;
257 }