Began implementing handline of Functional Component Measuring attributes
[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 u_int8_t flags = 0;
141
142 writer = bio_writer_create(PTS_SIMPLE_EVID_FINAL_SIZE);
143
144 /* Determine the flags to set*/
145 if (this->flags & PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO)
146 {
147 flags += 64;
148 }
149 else if (this->flags & PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO2)
150 {
151 flags += 128;
152 }
153 else if (this->flags & PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO2_CAP_VER)
154 {
155 flags += 192;
156 }
157 if (this->flags & PTS_SIMPLE_EVID_FINAL_FLAG_EVID)
158 {
159 flags += 32;
160 }
161
162 writer->write_uint8 (writer, flags);
163 writer->write_uint8 (writer, PTS_SIMPLE_EVID_FINAL_RESERVED);
164
165 /* Optional fields */
166 if (this->comp_hash_algorithm)
167 {
168 writer->write_uint16(writer, this->comp_hash_algorithm);
169 }
170 if (this->pcr_comp.ptr && this->pcr_comp.len > 0)
171 {
172 writer->write_uint32 (writer, this->pcr_comp.len);
173 writer->write_data (writer, this->pcr_comp);
174 }
175 if (this->tpm_quote_sign.ptr && this->tpm_quote_sign.len > 0)
176 {
177 writer->write_uint32 (writer, this->tpm_quote_sign.len);
178 writer->write_data (writer, this->tpm_quote_sign);
179 }
180 if (this->evid_sign.ptr && this->evid_sign.len > 0)
181 {
182 writer->write_data (writer, this->evid_sign);
183 }
184
185 this->value = chunk_clone(writer->get_buf(writer));
186 writer->destroy(writer);
187 }
188
189 METHOD(pa_tnc_attr_t, process, status_t,
190 private_tcg_pts_attr_simple_evid_final_t *this, u_int32_t *offset)
191 {
192 bio_reader_t *reader;
193 u_int8_t flags;
194 u_int8_t reserved;
195 u_int16_t algorithm;
196
197 if (this->value.len < PTS_SIMPLE_EVID_FINAL_SIZE)
198 {
199 DBG1(DBG_TNC, "insufficient data for Simple Evidence Final");
200 *offset = 0;
201 return FAILED;
202 }
203 reader = bio_reader_create(this->value);
204
205 reader->read_uint8(reader, &flags);
206 reader->read_uint8(reader, &reserved);
207
208 /* Determine the flags to set*/
209 if ((flags >> 5) & 1)
210 {
211 this->flags |= PTS_SIMPLE_EVID_FINAL_FLAG_EVID;
212 }
213
214 if (!((flags >> 6) & PTS_SIMPLE_EVID_FINAL_FLAG_NO))
215 {
216 this->flags |= PTS_SIMPLE_EVID_FINAL_FLAG_NO;
217 }
218 else if (!((flags >> 7) & 1) && ((flags >> 6) & 1))
219 {
220 this->flags |= PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO;
221 }
222 else if (((flags >> 7) & 1) && !((flags >> 6) & 1))
223 {
224 this->flags |= PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO2;
225 }
226 else if (((flags >> 7) & 1) && ((flags >> 6) & 1))
227 {
228 this->flags |= PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO2_CAP_VER;
229 }
230
231 /* Optional Composite Hash Algorithm and TPM PCR Composite field is included */
232 if ((flags >> 6) & PTS_SIMPLE_EVID_FINAL_FLAG_NO)
233 {
234 u_int32_t pcr_comp_len;
235 u_int32_t tpm_quote_sign_len;
236 reader->read_uint16(reader, &algorithm);
237 this->comp_hash_algorithm = algorithm;
238 reader->read_uint32(reader, &pcr_comp_len);
239 reader->read_data(reader, pcr_comp_len, &this->pcr_comp);
240 this->pcr_comp = chunk_clone(this->pcr_comp);
241 reader->read_uint32(reader, &tpm_quote_sign_len);
242 reader->read_data(reader, tpm_quote_sign_len, &this->tpm_quote_sign);
243 this->tpm_quote_sign = chunk_clone(this->tpm_quote_sign);
244 }
245
246 /* Optional Evidence Signature field is included */
247 if (this->flags & PTS_SIMPLE_EVID_FINAL_FLAG_EVID)
248 {
249 u_int32_t evid_sign_len = reader->remaining(reader);
250 reader->read_data(reader, evid_sign_len, &this->evid_sign);
251 this->evid_sign = chunk_clone(this->evid_sign);
252 }
253
254 reader->destroy(reader);
255 return SUCCESS;
256 }
257
258 METHOD(pa_tnc_attr_t, destroy, void,
259 private_tcg_pts_attr_simple_evid_final_t *this)
260 {
261 free(this->value.ptr);
262 free(this->pcr_comp.ptr);
263 free(this->tpm_quote_sign.ptr);
264 free(this->evid_sign.ptr);
265 free(this);
266 }
267
268 METHOD(tcg_pts_attr_simple_evid_final_t, get_flags, pts_simple_evid_final_flag_t,
269 private_tcg_pts_attr_simple_evid_final_t *this)
270 {
271 return this->flags;
272 }
273
274 METHOD(tcg_pts_attr_simple_evid_final_t, set_flags, void,
275 private_tcg_pts_attr_simple_evid_final_t *this, pts_simple_evid_final_flag_t flags)
276 {
277 this->flags = flags;
278 }
279
280 METHOD(tcg_pts_attr_simple_evid_final_t, get_comp_hash_algorithm, pts_meas_algorithms_t,
281 private_tcg_pts_attr_simple_evid_final_t *this)
282 {
283 return this->comp_hash_algorithm;
284 }
285
286 METHOD(tcg_pts_attr_simple_evid_final_t, set_comp_hash_algorithm, void,
287 private_tcg_pts_attr_simple_evid_final_t *this, pts_meas_algorithms_t comp_hash_algorithm)
288 {
289 this->comp_hash_algorithm = comp_hash_algorithm;
290 }
291
292 METHOD(tcg_pts_attr_simple_evid_final_t, get_comp_pcr_len, u_int32_t,
293 private_tcg_pts_attr_simple_evid_final_t *this)
294 {
295 if (this->pcr_comp.ptr && this->pcr_comp.len > 0)
296 {
297 return this->pcr_comp.len;
298 }
299 return 0;
300 }
301
302 METHOD(tcg_pts_attr_simple_evid_final_t, get_pcr_comp, chunk_t,
303 private_tcg_pts_attr_simple_evid_final_t *this)
304 {
305 return this->pcr_comp;
306 }
307
308 METHOD(tcg_pts_attr_simple_evid_final_t, set_pcr_comp, void,
309 private_tcg_pts_attr_simple_evid_final_t *this, chunk_t pcr_comp)
310 {
311 this->pcr_comp = pcr_comp;
312 }
313
314 METHOD(tcg_pts_attr_simple_evid_final_t, get_tpm_quote_sign_len, u_int32_t,
315 private_tcg_pts_attr_simple_evid_final_t *this)
316 {
317 if (this->tpm_quote_sign.ptr && this->tpm_quote_sign.len > 0)
318 {
319 return this->tpm_quote_sign.len;
320 }
321 return 0;
322 }
323
324 METHOD(tcg_pts_attr_simple_evid_final_t, get_tpm_quote_sign, chunk_t,
325 private_tcg_pts_attr_simple_evid_final_t *this)
326 {
327 return this->tpm_quote_sign;
328 }
329
330 METHOD(tcg_pts_attr_simple_evid_final_t, set_tpm_quote_sign, void,
331 private_tcg_pts_attr_simple_evid_final_t *this, chunk_t tpm_quote_sign)
332 {
333 this->tpm_quote_sign = tpm_quote_sign;
334 }
335
336 METHOD(tcg_pts_attr_simple_evid_final_t, get_evid_sign, chunk_t,
337 private_tcg_pts_attr_simple_evid_final_t *this)
338 {
339 return this->evid_sign;
340 }
341
342 METHOD(tcg_pts_attr_simple_evid_final_t, set_evid_sign, void,
343 private_tcg_pts_attr_simple_evid_final_t *this, chunk_t evid_sign)
344 {
345 this->evid_sign = evid_sign;
346 }
347
348 /**
349 * Described in header.
350 */
351 pa_tnc_attr_t *tcg_pts_attr_simple_evid_final_create(
352 pts_simple_evid_final_flag_t flags,
353 pts_meas_algorithms_t comp_hash_algorithm,
354 chunk_t pcr_comp,
355 chunk_t tpm_quote_sign,
356 chunk_t evid_sign)
357 {
358 private_tcg_pts_attr_simple_evid_final_t *this;
359
360 INIT(this,
361 .public = {
362 .pa_tnc_attribute = {
363 .get_vendor_id = _get_vendor_id,
364 .get_type = _get_type,
365 .get_value = _get_value,
366 .get_noskip_flag = _get_noskip_flag,
367 .set_noskip_flag = _set_noskip_flag,
368 .build = _build,
369 .process = _process,
370 .destroy = _destroy,
371 },
372 .get_flags= _get_flags,
373 .set_flags= _set_flags,
374 .get_comp_hash_algorithm = _get_comp_hash_algorithm,
375 .set_comp_hash_algorithm = _set_comp_hash_algorithm,
376 .get_comp_pcr_len = _get_comp_pcr_len,
377 .get_pcr_comp = _get_pcr_comp,
378 .set_pcr_comp = _set_pcr_comp,
379 .get_tpm_quote_sign_len = _get_tpm_quote_sign_len,
380 .get_tpm_quote_sign = _get_tpm_quote_sign,
381 .set_tpm_quote_sign = _set_tpm_quote_sign,
382 .get_evid_sign = _get_evid_sign,
383 .set_evid_sign = _set_evid_sign,
384 },
385 .vendor_id = PEN_TCG,
386 .type = TCG_PTS_SIMPLE_EVID_FINAL,
387 .flags = flags,
388 .comp_hash_algorithm = comp_hash_algorithm,
389 .pcr_comp = pcr_comp,
390 .tpm_quote_sign = tpm_quote_sign,
391 .evid_sign = evid_sign,
392 );
393
394 return &this->public.pa_tnc_attribute;
395 }
396
397
398 /**
399 * Described in header.
400 */
401 pa_tnc_attr_t *tcg_pts_attr_simple_evid_final_create_from_data(chunk_t data)
402 {
403 private_tcg_pts_attr_simple_evid_final_t *this;
404
405 INIT(this,
406 .public = {
407 .pa_tnc_attribute = {
408 .get_vendor_id = _get_vendor_id,
409 .get_type = _get_type,
410 .get_value = _get_value,
411 .get_noskip_flag = _get_noskip_flag,
412 .set_noskip_flag = _set_noskip_flag,
413 .build = _build,
414 .process = _process,
415 .destroy = _destroy,
416 },
417 .get_flags= _get_flags,
418 .set_flags= _set_flags,
419 .get_comp_hash_algorithm = _get_comp_hash_algorithm,
420 .set_comp_hash_algorithm = _set_comp_hash_algorithm,
421 .get_comp_pcr_len = _get_comp_pcr_len,
422 .get_pcr_comp = _get_pcr_comp,
423 .set_pcr_comp = _set_pcr_comp,
424 .get_tpm_quote_sign_len = _get_tpm_quote_sign_len,
425 .get_tpm_quote_sign = _get_tpm_quote_sign,
426 .set_tpm_quote_sign = _set_tpm_quote_sign,
427 .get_evid_sign = _get_evid_sign,
428 .set_evid_sign = _set_evid_sign,
429 },
430 .vendor_id = PEN_TCG,
431 .type = TCG_PTS_SIMPLE_EVID_FINAL,
432 .value = chunk_clone(data),
433 );
434
435 return &this->public.pa_tnc_attribute;
436 }