refactored simple component evidence
[strongswan.git] / src / libpts / pts / components / ita / ita_comp_tgrub.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen
3 *
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "ita_comp_tgrub.h"
18 #include "ita_comp_func_name.h"
19
20 #include "pts/components/pts_component.h"
21
22 #include <debug.h>
23 #include <pen/pen.h>
24
25 typedef struct pts_ita_comp_tgrub_t pts_ita_comp_tgrub_t;
26
27 /**
28 * Private data of a pts_ita_comp_tgrub_t object.
29 *
30 */
31 struct pts_ita_comp_tgrub_t {
32
33 /**
34 * Public pts_component_t interface.
35 */
36 pts_component_t public;
37
38 /**
39 * Component Functional Name
40 */
41 pts_comp_func_name_t *name;
42 };
43
44 METHOD(pts_component_t, get_comp_func_name, pts_comp_func_name_t*,
45 pts_ita_comp_tgrub_t *this)
46 {
47 return this->name;
48 }
49
50 METHOD(pts_component_t, get_evidence_flags, u_int8_t,
51 pts_ita_comp_tgrub_t *this)
52 {
53 return PTS_REQ_FUNC_COMP_FLAG_PCR;
54 }
55
56 METHOD(pts_component_t, measure, status_t,
57 pts_ita_comp_tgrub_t *this, pts_t *pts, pts_comp_evidence_t **evidence)
58 {
59 pts_comp_evidence_t *evid;
60 u_int32_t extended_pcr;
61 time_t measurement_time;
62 chunk_t measurement, pcr_before, pcr_after;
63
64 /* Provisional implementation for TGRUB */
65 extended_pcr = PCR_DEBUG;
66 time(&measurement_time);
67
68 if (!pts->read_pcr(pts, extended_pcr, &pcr_after))
69 {
70 DBG1(DBG_PTS, "error occured while reading PCR: %d", extended_pcr);
71 return FAILED;
72 }
73
74 measurement = chunk_alloc(HASH_SIZE_SHA1);
75 memset(measurement.ptr, 0x00, measurement.len);
76
77 pcr_before = chunk_alloc(PCR_LEN);
78 memset(pcr_before.ptr, 0x00, pcr_before.len);
79
80 evid = *evidence = pts_comp_evidence_create(this->name, 0, extended_pcr,
81 PTS_MEAS_ALGO_SHA1, PTS_PCR_TRANSFORM_NO,
82 measurement_time, measurement);
83 evid->set_pcr_info(evid, pcr_before, pcr_after);
84
85 return SUCCESS;
86 }
87
88 METHOD(pts_component_t, verify, status_t,
89 pts_ita_comp_tgrub_t *this, pts_t *pts, pts_database_t *pts_db,
90 pts_comp_evidence_t *evidence)
91 {
92 bool has_pcr_info;
93 u_int32_t extended_pcr;
94 pts_meas_algorithms_t algo;
95 pts_pcr_transform_t transform;
96 time_t measurement_time;
97 chunk_t measurement, pcr_before, pcr_after;
98 pcr_entry_t *entry;
99
100 measurement = evidence->get_measurement(evidence, &extended_pcr,
101 &algo, &transform, &measurement_time);
102 if (extended_pcr != PCR_DEBUG)
103 {
104 return FAILED;
105 }
106
107 /* TODO check measurement in database */
108
109 has_pcr_info = evidence->get_pcr_info(evidence, &pcr_before, &pcr_after);
110 if (has_pcr_info)
111 {
112 entry = malloc_thing(pcr_entry_t);
113 entry->pcr_number = extended_pcr;
114 memcpy(entry->pcr_value, pcr_after.ptr, PCR_LEN);
115 pts->add_pcr_entry(pts, entry);
116 }
117
118 return SUCCESS;
119 }
120
121 METHOD(pts_component_t, destroy, void,
122 pts_ita_comp_tgrub_t *this)
123 {
124 this->name->destroy(this->name);
125 free(this);
126 }
127
128 /**
129 * See header
130 */
131 pts_component_t *pts_ita_comp_tgrub_create(u_int8_t qualifier)
132 {
133 pts_ita_comp_tgrub_t *this;
134
135 INIT(this,
136 .public = {
137 .get_comp_func_name = _get_comp_func_name,
138 .get_evidence_flags = _get_evidence_flags,
139 .measure = _measure,
140 .verify = _verify,
141 .destroy = _destroy,
142 },
143 .name = pts_comp_func_name_create(PEN_ITA, PTS_ITA_COMP_FUNC_NAME_TBOOT,
144 qualifier),
145 );
146
147 return &this->public;
148 }
149