Write/Read flags field directly to/from variable
[strongswan.git] / src / libpts / tcg / tcg_pts_attr_simple_evid_final.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_simple_evid_final.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_simple_evid_final_t private_tcg_pts_attr_simple_evid_final_t;
24
25 /**
26 * Simple Evidence Final
27 * see section 3.15.2 of PTS Protocol: Binding to TNC IF-M 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 * | Flags | Reserved | Optional Composite Hash Alg |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | Optional TPM PCR Composite Length |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * ~ Optional TPM PCR Composite (Variable Length) ~
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | Optional TPM Quote Signature Length |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * ~ Optional TPM Quote Signature (Variable Length) ~
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * ~ Optional Evidence Signature (Variable Length) ~
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 */
45
46 #define PTS_SIMPLE_EVID_FINAL_SIZE 2
47 #define PTS_SIMPLE_EVID_FINAL_RESERVED 0x00
48
49 /**
50 * Private data of an tcg_pts_attr_simple_evid_final_t object.
51 */
52 struct private_tcg_pts_attr_simple_evid_final_t {
53
54 /**
55 * Public members of tcg_pts_attr_simple_evid_final_t
56 */
57 tcg_pts_attr_simple_evid_final_t public;
58
59 /**
60 * Attribute vendor ID
61 */
62 pen_t vendor_id;
63
64 /**
65 * Attribute type
66 */
67 u_int32_t type;
68
69 /**
70 * Attribute value
71 */
72 chunk_t value;
73
74 /**
75 * Noskip flag
76 */
77 bool noskip_flag;
78
79 /**
80 * Set of flags for Simple Evidence Final
81 */
82 pts_simple_evid_final_flag_t flags;
83
84 /**
85 * Optional Composite Hash Algorithm
86 */
87 pts_meas_algorithms_t comp_hash_algorithm;
88
89 /**
90 * Optional TPM PCR Composite
91 */
92 chunk_t pcr_comp;
93
94 /**
95 * Optional TPM Quote Signature
96 */
97 chunk_t tpm_quote_sign;
98
99 /**
100 * Optional Evidence Signature
101 */
102 chunk_t evid_sign;
103
104 };
105
106 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
107 private_tcg_pts_attr_simple_evid_final_t *this)
108 {
109 return this->vendor_id;
110 }
111
112 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
113 private_tcg_pts_attr_simple_evid_final_t *this)
114 {
115 return this->type;
116 }
117
118 METHOD(pa_tnc_attr_t, get_value, chunk_t,
119 private_tcg_pts_attr_simple_evid_final_t *this)
120 {
121 return this->value;
122 }
123
124 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
125 private_tcg_pts_attr_simple_evid_final_t *this)
126 {
127 return this->noskip_flag;
128 }
129
130 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
131 private_tcg_pts_attr_simple_evid_final_t *this, bool noskip)
132 {
133 this->noskip_flag = noskip;
134 }
135
136 METHOD(pa_tnc_attr_t, build, void,
137 private_tcg_pts_attr_simple_evid_final_t *this)
138 {
139 bio_writer_t *writer;
140
141 writer = bio_writer_create(PTS_SIMPLE_EVID_FINAL_SIZE);
142 writer->write_uint8 (writer, this->flags);
143 writer->write_uint8 (writer, PTS_SIMPLE_EVID_FINAL_RESERVED);
144
145 /* Optional fields */
146 if (this->comp_hash_algorithm)
147 {
148 writer->write_uint16(writer, this->comp_hash_algorithm);
149 }
150 if (this->pcr_comp.ptr && this->pcr_comp.len > 0)
151 {
152 writer->write_uint32 (writer, this->pcr_comp.len);
153 writer->write_data (writer, this->pcr_comp);
154 }
155 if (this->tpm_quote_sign.ptr && this->tpm_quote_sign.len > 0)
156 {
157 writer->write_uint32 (writer, this->tpm_quote_sign.len);
158 writer->write_data (writer, this->tpm_quote_sign);
159 }
160 if (this->evid_sign.ptr && this->evid_sign.len > 0)
161 {
162 writer->write_data (writer, this->evid_sign);
163 }
164
165 this->value = chunk_clone(writer->get_buf(writer));
166 writer->destroy(writer);
167 }
168
169 METHOD(pa_tnc_attr_t, process, status_t,
170 private_tcg_pts_attr_simple_evid_final_t *this, u_int32_t *offset)
171 {
172 bio_reader_t *reader;
173 u_int8_t flags;
174 u_int8_t reserved;
175 u_int16_t algorithm;
176
177 if (this->value.len < PTS_SIMPLE_EVID_FINAL_SIZE)
178 {
179 DBG1(DBG_TNC, "insufficient data for Simple Evidence Final");
180 *offset = 0;
181 return FAILED;
182 }
183 reader = bio_reader_create(this->value);
184
185 reader->read_uint8(reader, &flags);
186 this->flags = flags;
187 reader->read_uint8(reader, &reserved);
188
189 /* Optional Composite Hash Algorithm and TPM PCR Composite field is included */
190 if ((flags >> 6) & PTS_SIMPLE_EVID_FINAL_FLAG_NO)
191 {
192 u_int32_t pcr_comp_len;
193 u_int32_t tpm_quote_sign_len;
194 reader->read_uint16(reader, &algorithm);
195 this->comp_hash_algorithm = algorithm;
196 reader->read_uint32(reader, &pcr_comp_len);
197 reader->read_data(reader, pcr_comp_len, &this->pcr_comp);
198 this->pcr_comp = chunk_clone(this->pcr_comp);
199 reader->read_uint32(reader, &tpm_quote_sign_len);
200 reader->read_data(reader, tpm_quote_sign_len, &this->tpm_quote_sign);
201 this->tpm_quote_sign = chunk_clone(this->tpm_quote_sign);
202 }
203
204 /* Optional Evidence Signature field is included */
205 if (this->flags & PTS_SIMPLE_EVID_FINAL_FLAG_EVID)
206 {
207 u_int32_t evid_sign_len = reader->remaining(reader);
208 reader->read_data(reader, evid_sign_len, &this->evid_sign);
209 this->evid_sign = chunk_clone(this->evid_sign);
210 }
211
212 reader->destroy(reader);
213 return SUCCESS;
214 }
215
216 METHOD(pa_tnc_attr_t, destroy, void,
217 private_tcg_pts_attr_simple_evid_final_t *this)
218 {
219 free(this->value.ptr);
220 free(this->pcr_comp.ptr);
221 free(this->tpm_quote_sign.ptr);
222 free(this->evid_sign.ptr);
223 free(this);
224 }
225
226 METHOD(tcg_pts_attr_simple_evid_final_t, get_flags, pts_simple_evid_final_flag_t,
227 private_tcg_pts_attr_simple_evid_final_t *this)
228 {
229 return this->flags;
230 }
231
232 METHOD(tcg_pts_attr_simple_evid_final_t, set_flags, void,
233 private_tcg_pts_attr_simple_evid_final_t *this, pts_simple_evid_final_flag_t flags)
234 {
235 this->flags = flags;
236 }
237
238 METHOD(tcg_pts_attr_simple_evid_final_t, get_comp_hash_algorithm, pts_meas_algorithms_t,
239 private_tcg_pts_attr_simple_evid_final_t *this)
240 {
241 return this->comp_hash_algorithm;
242 }
243
244 METHOD(tcg_pts_attr_simple_evid_final_t, set_comp_hash_algorithm, void,
245 private_tcg_pts_attr_simple_evid_final_t *this, pts_meas_algorithms_t comp_hash_algorithm)
246 {
247 this->comp_hash_algorithm = comp_hash_algorithm;
248 }
249
250 METHOD(tcg_pts_attr_simple_evid_final_t, get_comp_pcr_len, u_int32_t,
251 private_tcg_pts_attr_simple_evid_final_t *this)
252 {
253 if (this->pcr_comp.ptr && this->pcr_comp.len > 0)
254 {
255 return this->pcr_comp.len;
256 }
257 return 0;
258 }
259
260 METHOD(tcg_pts_attr_simple_evid_final_t, get_pcr_comp, chunk_t,
261 private_tcg_pts_attr_simple_evid_final_t *this)
262 {
263 return this->pcr_comp;
264 }
265
266 METHOD(tcg_pts_attr_simple_evid_final_t, set_pcr_comp, void,
267 private_tcg_pts_attr_simple_evid_final_t *this, chunk_t pcr_comp)
268 {
269 this->pcr_comp = pcr_comp;
270 }
271
272 METHOD(tcg_pts_attr_simple_evid_final_t, get_tpm_quote_sign_len, u_int32_t,
273 private_tcg_pts_attr_simple_evid_final_t *this)
274 {
275 if (this->tpm_quote_sign.ptr && this->tpm_quote_sign.len > 0)
276 {
277 return this->tpm_quote_sign.len;
278 }
279 return 0;
280 }
281
282 METHOD(tcg_pts_attr_simple_evid_final_t, get_tpm_quote_sign, chunk_t,
283 private_tcg_pts_attr_simple_evid_final_t *this)
284 {
285 return this->tpm_quote_sign;
286 }
287
288 METHOD(tcg_pts_attr_simple_evid_final_t, set_tpm_quote_sign, void,
289 private_tcg_pts_attr_simple_evid_final_t *this, chunk_t tpm_quote_sign)
290 {
291 this->tpm_quote_sign = tpm_quote_sign;
292 }
293
294 METHOD(tcg_pts_attr_simple_evid_final_t, get_evid_sign, chunk_t,
295 private_tcg_pts_attr_simple_evid_final_t *this)
296 {
297 return this->evid_sign;
298 }
299
300 METHOD(tcg_pts_attr_simple_evid_final_t, set_evid_sign, void,
301 private_tcg_pts_attr_simple_evid_final_t *this, chunk_t evid_sign)
302 {
303 this->evid_sign = evid_sign;
304 }
305
306 /**
307 * Described in header.
308 */
309 pa_tnc_attr_t *tcg_pts_attr_simple_evid_final_create(
310 pts_simple_evid_final_flag_t flags,
311 pts_meas_algorithms_t comp_hash_algorithm,
312 chunk_t pcr_comp,
313 chunk_t tpm_quote_sign,
314 chunk_t evid_sign)
315 {
316 private_tcg_pts_attr_simple_evid_final_t *this;
317
318 INIT(this,
319 .public = {
320 .pa_tnc_attribute = {
321 .get_vendor_id = _get_vendor_id,
322 .get_type = _get_type,
323 .get_value = _get_value,
324 .get_noskip_flag = _get_noskip_flag,
325 .set_noskip_flag = _set_noskip_flag,
326 .build = _build,
327 .process = _process,
328 .destroy = _destroy,
329 },
330 .get_flags= _get_flags,
331 .set_flags= _set_flags,
332 .get_comp_hash_algorithm = _get_comp_hash_algorithm,
333 .set_comp_hash_algorithm = _set_comp_hash_algorithm,
334 .get_comp_pcr_len = _get_comp_pcr_len,
335 .get_pcr_comp = _get_pcr_comp,
336 .set_pcr_comp = _set_pcr_comp,
337 .get_tpm_quote_sign_len = _get_tpm_quote_sign_len,
338 .get_tpm_quote_sign = _get_tpm_quote_sign,
339 .set_tpm_quote_sign = _set_tpm_quote_sign,
340 .get_evid_sign = _get_evid_sign,
341 .set_evid_sign = _set_evid_sign,
342 },
343 .vendor_id = PEN_TCG,
344 .type = TCG_PTS_SIMPLE_EVID_FINAL,
345 .flags = flags,
346 .comp_hash_algorithm = comp_hash_algorithm,
347 .pcr_comp = pcr_comp,
348 .tpm_quote_sign = tpm_quote_sign,
349 .evid_sign = evid_sign,
350 );
351
352 return &this->public.pa_tnc_attribute;
353 }
354
355
356 /**
357 * Described in header.
358 */
359 pa_tnc_attr_t *tcg_pts_attr_simple_evid_final_create_from_data(chunk_t data)
360 {
361 private_tcg_pts_attr_simple_evid_final_t *this;
362
363 INIT(this,
364 .public = {
365 .pa_tnc_attribute = {
366 .get_vendor_id = _get_vendor_id,
367 .get_type = _get_type,
368 .get_value = _get_value,
369 .get_noskip_flag = _get_noskip_flag,
370 .set_noskip_flag = _set_noskip_flag,
371 .build = _build,
372 .process = _process,
373 .destroy = _destroy,
374 },
375 .get_flags= _get_flags,
376 .set_flags= _set_flags,
377 .get_comp_hash_algorithm = _get_comp_hash_algorithm,
378 .set_comp_hash_algorithm = _set_comp_hash_algorithm,
379 .get_comp_pcr_len = _get_comp_pcr_len,
380 .get_pcr_comp = _get_pcr_comp,
381 .set_pcr_comp = _set_pcr_comp,
382 .get_tpm_quote_sign_len = _get_tpm_quote_sign_len,
383 .get_tpm_quote_sign = _get_tpm_quote_sign,
384 .set_tpm_quote_sign = _set_tpm_quote_sign,
385 .get_evid_sign = _get_evid_sign,
386 .set_evid_sign = _set_evid_sign,
387 },
388 .vendor_id = PEN_TCG,
389 .type = TCG_PTS_SIMPLE_EVID_FINAL,
390 .value = chunk_clone(data),
391 );
392
393 return &this->public.pa_tnc_attribute;
394 }