Added add_segment() method to TCG/SEG attributes
[strongswan.git] / src / libimcv / tcg / seg / tcg_seg_attr_max_size.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_max_size.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_max_size_t private_tcg_seg_attr_max_size_t;
24
25 /**
26 * Maximum Attribute Size Request/Response
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 * | Max Attribute Size |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | Max Segment Size |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 */
37
38 /**
39 * Private data of an tcg_seg_attr_max_size_t object.
40 */
41 struct private_tcg_seg_attr_max_size_t {
42
43 /**
44 * Public members of tcg_seg_attr_max_size_t
45 */
46 tcg_seg_attr_max_size_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 * Maximum IF-M attribute size in octets
70 */
71 uint32_t max_attr_size;
72
73 /**
74 * Maximum IF-M attribute segment size in octets
75 */
76 uint32_t max_seg_size;
77
78 /**
79 * Reference count
80 */
81 refcount_t ref;
82 };
83
84 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
85 private_tcg_seg_attr_max_size_t *this)
86 {
87 return this->type;
88 }
89
90 METHOD(pa_tnc_attr_t, get_value, chunk_t,
91 private_tcg_seg_attr_max_size_t *this)
92 {
93 return this->value;
94 }
95
96 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
97 private_tcg_seg_attr_max_size_t *this)
98 {
99 return this->noskip_flag;
100 }
101
102 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
103 private_tcg_seg_attr_max_size_t *this, bool noskip)
104 {
105 this->noskip_flag = noskip;
106 }
107
108 METHOD(pa_tnc_attr_t, build, void,
109 private_tcg_seg_attr_max_size_t *this)
110 {
111 bio_writer_t *writer;
112
113 if (this->value.ptr)
114 {
115 return;
116 }
117 writer = bio_writer_create(TCG_SEG_ATTR_MAX_SIZE_SIZE);
118 writer->write_uint32(writer, this->max_attr_size);
119 writer->write_uint32(writer, this->max_seg_size);
120
121 this->value = writer->extract_buf(writer);
122 this->length = this->value.len;
123 writer->destroy(writer);
124 }
125
126 METHOD(pa_tnc_attr_t, process, status_t,
127 private_tcg_seg_attr_max_size_t *this, u_int32_t *offset)
128 {
129 bio_reader_t *reader;
130
131 *offset = 0;
132
133 if (this->value.len < this->length)
134 {
135 return NEED_MORE;
136 }
137 if (this->value.len < TCG_SEG_ATTR_MAX_SIZE_SIZE)
138 {
139 DBG1(DBG_TNC, "insufficient data for %N", tcg_attr_names,
140 this->type.type);
141 return FAILED;
142 }
143 reader = bio_reader_create(this->value);
144 reader->read_uint32(reader, &this->max_attr_size);
145 reader->read_uint32(reader, &this->max_seg_size);
146 reader->destroy(reader);
147
148 return SUCCESS;
149 }
150
151 METHOD(pa_tnc_attr_t, add_segment, void,
152 private_tcg_seg_attr_max_size_t *this, chunk_t segment)
153 {
154 this->value = chunk_cat("mc", this->value, segment);
155 }
156
157 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
158 private_tcg_seg_attr_max_size_t *this)
159 {
160 ref_get(&this->ref);
161 return &this->public.pa_tnc_attribute;
162 }
163
164 METHOD(pa_tnc_attr_t, destroy, void,
165 private_tcg_seg_attr_max_size_t *this)
166 {
167 if (ref_put(&this->ref))
168 {
169 free(this->value.ptr);
170 free(this);
171 }
172 }
173
174 METHOD(tcg_seg_attr_max_size_t, get_attr_size, void,
175 private_tcg_seg_attr_max_size_t *this, uint32_t *max_attr_size,
176 uint32_t *max_seg_size)
177 {
178 if (max_attr_size)
179 {
180 *max_attr_size = this->max_attr_size;
181 }
182 if (max_seg_size)
183 {
184 *max_seg_size = this->max_seg_size;
185 }
186 }
187
188 /**
189 * Described in header.
190 */
191 pa_tnc_attr_t* tcg_seg_attr_max_size_create(uint32_t max_attr_size,
192 uint32_t max_seg_size,
193 bool request)
194 {
195 private_tcg_seg_attr_max_size_t *this;
196
197 INIT(this,
198 .public = {
199 .pa_tnc_attribute = {
200 .get_type = _get_type,
201 .get_value = _get_value,
202 .get_noskip_flag = _get_noskip_flag,
203 .set_noskip_flag = _set_noskip_flag,
204 .build = _build,
205 .process = _process,
206 .add_segment = _add_segment,
207 .get_ref = _get_ref,
208 .destroy = _destroy,
209 },
210 .get_attr_size = _get_attr_size,
211 },
212 .type = { PEN_TCG, request ? TCG_SEG_MAX_ATTR_SIZE_REQ :
213 TCG_SEG_MAX_ATTR_SIZE_RESP },
214 .max_attr_size = max_attr_size,
215 .max_seg_size = max_seg_size,
216 .ref = 1,
217 );
218
219 return &this->public.pa_tnc_attribute;
220 }
221
222 /**
223 * Described in header.
224 */
225 pa_tnc_attr_t *tcg_seg_attr_max_size_create_from_data(size_t length,
226 chunk_t data,
227 bool request)
228 {
229 private_tcg_seg_attr_max_size_t *this;
230
231 INIT(this,
232 .public = {
233 .pa_tnc_attribute = {
234 .get_type = _get_type,
235 .get_value = _get_value,
236 .get_noskip_flag = _get_noskip_flag,
237 .set_noskip_flag = _set_noskip_flag,
238 .build = _build,
239 .process = _process,
240 .add_segment = _add_segment,
241 .get_ref = _get_ref,
242 .destroy = _destroy,
243 },
244 .get_attr_size = _get_attr_size,
245 },
246 .type = { PEN_TCG, request ? TCG_SEG_MAX_ATTR_SIZE_REQ :
247 TCG_SEG_MAX_ATTR_SIZE_RESP },
248 .length = length,
249 .value = chunk_clone(data),
250 .ref = 1,
251 );
252
253 return &this->public.pa_tnc_attribute;
254 }