sub-component depth from pts database on component evidence request
[strongswan.git] / src / libpts / pts / components / ita / ita_comp_tboot.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_tboot.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_tboot_t pts_ita_comp_tboot_t;
26
27 /**
28 * Private data of a pts_ita_comp_tboot_t object.
29 *
30 */
31 struct pts_ita_comp_tboot_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 * Sub-component depth
45 */
46 u_int32_t depth;
47
48 /**
49 * Extended PCR last handled
50 */
51 u_int32_t extended_pcr;
52
53 /**
54 * Time of TBOOT measurement
55 */
56 time_t measurement_time;
57
58 };
59
60 METHOD(pts_component_t, get_comp_func_name, pts_comp_func_name_t*,
61 pts_ita_comp_tboot_t *this)
62 {
63 return this->name;
64 }
65
66 METHOD(pts_component_t, get_evidence_flags, u_int8_t,
67 pts_ita_comp_tboot_t *this)
68 {
69 return PTS_REQ_FUNC_COMP_EVID_PCR;
70 }
71
72 METHOD(pts_component_t, get_depth, u_int32_t,
73 pts_ita_comp_tboot_t *this)
74 {
75 return this->depth;
76 }
77
78 METHOD(pts_component_t, measure, status_t,
79 pts_ita_comp_tboot_t *this, pts_t *pts, pts_comp_evidence_t **evidence)
80 {
81 pts_comp_evidence_t *evid;
82 char *meas_hex, *pcr_before_hex, *pcr_after_hex;
83 chunk_t measurement, pcr_before, pcr_after;
84
85 switch (this->extended_pcr)
86 {
87 case 0:
88 /* dummy data since currently the TBOOT log is not retrieved */
89 time(&this->measurement_time);
90 meas_hex = lib->settings->get_str(lib->settings,
91 "libimcv.plugins.imc-attestation.pcr17_meas", NULL);
92 pcr_before_hex = lib->settings->get_str(lib->settings,
93 "libimcv.plugins.imc-attestation.pcr17_before", NULL);
94 pcr_after_hex = lib->settings->get_str(lib->settings,
95 "libimcv.plugins.imc-attestation.pcr17_after", NULL);
96 this->extended_pcr = PCR_TBOOT_POLICY;
97 break;
98 case PCR_TBOOT_POLICY:
99 /* dummy data since currently the TBOOT log is not retrieved */
100 meas_hex = lib->settings->get_str(lib->settings,
101 "libimcv.plugins.imc-attestation.pcr18_meas", NULL);
102 pcr_before_hex = lib->settings->get_str(lib->settings,
103 "libimcv.plugins.imc-attestation.pcr18_before", NULL);
104 pcr_after_hex = lib->settings->get_str(lib->settings,
105 "libimcv.plugins.imc-attestation.pcr18_after", NULL);
106 this->extended_pcr = PCR_TBOOT_MLE;
107 break;
108 default:
109 return FAILED;
110 }
111
112 measurement = chunk_from_hex(
113 chunk_create(meas_hex, strlen(meas_hex)), NULL);
114 pcr_before = chunk_from_hex(
115 chunk_create(pcr_before_hex, strlen(pcr_before_hex)), NULL);
116 pcr_after = chunk_from_hex(
117 chunk_create(pcr_after_hex, strlen(pcr_after_hex)), NULL);
118
119 evid = *evidence = pts_comp_evidence_create(this->name->clone(this->name),
120 0, this->extended_pcr,
121 PTS_MEAS_ALGO_SHA1, PTS_PCR_TRANSFORM_NO,
122 this->measurement_time, measurement);
123 evid->set_pcr_info(evid, pcr_before, pcr_after);
124
125 return (this->extended_pcr == PCR_TBOOT_MLE) ? SUCCESS : NEED_MORE;
126 }
127
128 METHOD(pts_component_t, verify, status_t,
129 pts_ita_comp_tboot_t *this, pts_t *pts, pts_database_t *pts_db,
130 pts_comp_evidence_t *evidence)
131 {
132 bool has_pcr_info;
133 u_int32_t extended_pcr;
134 pts_meas_algorithms_t algo;
135 pts_pcr_transform_t transform;
136 time_t measurement_time;
137 chunk_t measurement, pcr_before, pcr_after;
138 enumerator_t *enumerator;
139 char *file;
140 chunk_t hash;
141 char *platform_info;
142
143 platform_info = pts->get_platform_info(pts);
144 if (!pts_db || !platform_info)
145 {
146 DBG1(DBG_PTS, "%s%s%s not available",
147 (pts_db) ? "" : "pts database",
148 (!pts_db && !platform_info) ? "and" : "",
149 (platform_info) ? "" : "platform info");
150 return FAILED;
151 }
152
153 switch (this->extended_pcr)
154 {
155 case 0:
156 this->extended_pcr = PCR_TBOOT_POLICY;
157 file = "tboot_pcr17";
158 break;
159 case PCR_TBOOT_POLICY:
160 this->extended_pcr = PCR_TBOOT_MLE;
161 file = "tboot_pcr18";
162 break;
163 default:
164 return FAILED;
165 }
166
167 measurement = evidence->get_measurement(evidence, &extended_pcr,
168 &algo, &transform, &measurement_time);
169 if (extended_pcr != this->extended_pcr)
170 {
171 return FAILED;
172 }
173
174 /* check measurement in database */
175 enumerator = pts_db->create_comp_hash_enumerator(pts_db, file,
176 platform_info, this->name, algo);
177 while (enumerator->enumerate(enumerator, &hash))
178 {
179 if (!chunk_equals(hash, measurement))
180 {
181 DBG1(DBG_PTS, "Incorrect TBOOT component measurement for PCR %d. "
182 "Expected: %#B, Received: %#B",
183 this->extended_pcr, &hash, &measurement);
184 return FAILED;
185 }
186 else
187 {
188 DBG3(DBG_PTS, "Matching TBOOT component measurement for PCR %d",
189 this->extended_pcr);
190 break;
191 }
192 }
193 enumerator->destroy(enumerator);
194
195 has_pcr_info = evidence->get_pcr_info(evidence, &pcr_before, &pcr_after);
196 if (has_pcr_info)
197 {
198 if (!pts->add_pcr(pts, extended_pcr, pcr_before, pcr_after))
199 {
200 return FAILED;
201 }
202 }
203
204 return (this->extended_pcr == PCR_TBOOT_MLE) ? SUCCESS : NEED_MORE;
205 }
206
207 METHOD(pts_component_t, destroy, void,
208 pts_ita_comp_tboot_t *this)
209 {
210 this->name->destroy(this->name);
211 free(this);
212 }
213
214 /**
215 * See header
216 */
217 pts_component_t *pts_ita_comp_tboot_create(u_int8_t qualifier, u_int32_t depth)
218 {
219 pts_ita_comp_tboot_t *this;
220
221 INIT(this,
222 .public = {
223 .get_comp_func_name = _get_comp_func_name,
224 .get_evidence_flags = _get_evidence_flags,
225 .get_depth = _get_depth,
226 .measure = _measure,
227 .verify = _verify,
228 .destroy = _destroy,
229 },
230 .name = pts_comp_func_name_create(PEN_ITA, PTS_ITA_COMP_FUNC_NAME_TBOOT,
231 qualifier),
232 .depth = depth,
233 );
234
235 return &this->public;
236 }
237