017bf2c829bb7c2566e4aaaf7bf5406009c92c7f
[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, destroy, void,
152 private_tcg_seg_attr_max_size_t *this)
153 {
154 if (ref_put(&this->ref))
155 {
156 free(this->value.ptr);
157 free(this);
158 }
159 }
160
161 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
162 private_tcg_seg_attr_max_size_t *this)
163 {
164 ref_get(&this->ref);
165 return &this->public.pa_tnc_attribute;
166 }
167
168 METHOD(tcg_seg_attr_max_size_t, get_attr_size, void,
169 private_tcg_seg_attr_max_size_t *this, uint32_t *max_attr_size,
170 uint32_t *max_seg_size)
171 {
172 if (max_attr_size)
173 {
174 *max_attr_size = this->max_attr_size;
175 }
176 if (max_seg_size)
177 {
178 *max_seg_size = this->max_seg_size;
179 }
180 }
181
182 /**
183 * Described in header.
184 */
185 pa_tnc_attr_t* tcg_seg_attr_max_size_create(uint32_t max_attr_size,
186 uint32_t max_seg_size,
187 bool request)
188 {
189 private_tcg_seg_attr_max_size_t *this;
190
191 INIT(this,
192 .public = {
193 .pa_tnc_attribute = {
194 .get_type = _get_type,
195 .get_value = _get_value,
196 .get_noskip_flag = _get_noskip_flag,
197 .set_noskip_flag = _set_noskip_flag,
198 .build = _build,
199 .process = _process,
200 .get_ref = _get_ref,
201 .destroy = _destroy,
202 },
203 .get_attr_size = _get_attr_size,
204 },
205 .type = { PEN_TCG, request ? TCG_SEG_MAX_ATTR_SIZE_REQ :
206 TCG_SEG_MAX_ATTR_SIZE_RESP },
207 .max_attr_size = max_attr_size,
208 .max_seg_size = max_seg_size,
209 .ref = 1,
210 );
211
212 return &this->public.pa_tnc_attribute;
213 }
214
215 /**
216 * Described in header.
217 */
218 pa_tnc_attr_t *tcg_seg_attr_max_size_create_from_data(size_t length,
219 chunk_t data,
220 bool request)
221 {
222 private_tcg_seg_attr_max_size_t *this;
223
224 INIT(this,
225 .public = {
226 .pa_tnc_attribute = {
227 .get_type = _get_type,
228 .get_value = _get_value,
229 .get_noskip_flag = _get_noskip_flag,
230 .set_noskip_flag = _set_noskip_flag,
231 .build = _build,
232 .process = _process,
233 .get_ref = _get_ref,
234 .destroy = _destroy,
235 },
236 .get_attr_size = _get_attr_size,
237 },
238 .type = { PEN_TCG, request ? TCG_SEG_MAX_ATTR_SIZE_REQ :
239 TCG_SEG_MAX_ATTR_SIZE_RESP },
240 .length = length,
241 .value = chunk_clone(data),
242 .ref = 1,
243 );
244
245 return &this->public.pa_tnc_attribute;
246 }