refactored PCR functionality
[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 * Extended PCR last handled
45 */
46 u_int32_t extended_pcr;
47
48 /**
49 * Time of TBOOT measurement
50 */
51 time_t measurement_time;
52
53 };
54
55 METHOD(pts_component_t, get_comp_func_name, pts_comp_func_name_t*,
56 pts_ita_comp_tboot_t *this)
57 {
58 return this->name;
59 }
60
61 METHOD(pts_component_t, get_evidence_flags, u_int8_t,
62 pts_ita_comp_tboot_t *this)
63 {
64 return PTS_REQ_FUNC_COMP_FLAG_PCR;
65 }
66
67 METHOD(pts_component_t, measure, status_t,
68 pts_ita_comp_tboot_t *this, pts_t *pts, pts_comp_evidence_t **evidence)
69 {
70 pts_comp_evidence_t *evid;
71 char *meas_hex, *pcr_before_hex, *pcr_after_hex;
72 chunk_t measurement, pcr_before, pcr_after;
73
74 switch (this->extended_pcr)
75 {
76 case 0:
77 /* dummy data since currently the TBOOT log is not retrieved */
78 time(&this->measurement_time);
79 meas_hex = lib->settings->get_str(lib->settings,
80 "libimcv.plugins.imc-attestation.pcr17_meas", NULL);
81 pcr_before_hex = lib->settings->get_str(lib->settings,
82 "libimcv.plugins.imc-attestation.pcr17_before", NULL);
83 pcr_after_hex = lib->settings->get_str(lib->settings,
84 "libimcv.plugins.imc-attestation.pcr17_after", NULL);
85 this->extended_pcr = PCR_TBOOT_POLICY;
86 break;
87 case PCR_TBOOT_POLICY:
88 /* dummy data since currently the TBOOT log is not retrieved */
89 meas_hex = lib->settings->get_str(lib->settings,
90 "libimcv.plugins.imc-attestation.pcr18_meas", NULL);
91 pcr_before_hex = lib->settings->get_str(lib->settings,
92 "libimcv.plugins.imc-attestation.pcr18_before", NULL);
93 pcr_after_hex = lib->settings->get_str(lib->settings,
94 "libimcv.plugins.imc-attestation.pcr18_after", NULL);
95 this->extended_pcr = PCR_TBOOT_MLE;
96 break;
97 default:
98 return FAILED;
99 }
100
101 measurement = chunk_from_hex(
102 chunk_create(meas_hex, strlen(meas_hex)), NULL);
103 pcr_before = chunk_from_hex(
104 chunk_create(pcr_before_hex, strlen(pcr_before_hex)), NULL);
105 pcr_after = chunk_from_hex(
106 chunk_create(pcr_after_hex, strlen(pcr_after_hex)), NULL);
107
108 evid = *evidence = pts_comp_evidence_create(this->name->clone(this->name),
109 0, this->extended_pcr,
110 PTS_MEAS_ALGO_SHA1, PTS_PCR_TRANSFORM_NO,
111 this->measurement_time, measurement);
112 evid->set_pcr_info(evid, pcr_before, pcr_after);
113
114 return (this->extended_pcr == PCR_TBOOT_MLE) ? SUCCESS : NEED_MORE;
115 }
116
117 METHOD(pts_component_t, verify, status_t,
118 pts_ita_comp_tboot_t *this, pts_t *pts, pts_database_t *pts_db,
119 pts_comp_evidence_t *evidence)
120 {
121 bool has_pcr_info;
122 u_int32_t extended_pcr;
123 pts_meas_algorithms_t algo;
124 pts_pcr_transform_t transform;
125 time_t measurement_time;
126 chunk_t measurement, pcr_before, pcr_after;
127
128 switch (this->extended_pcr)
129 {
130 case 0:
131 this->extended_pcr = PCR_TBOOT_POLICY;
132 break;
133 case PCR_TBOOT_POLICY:
134 this->extended_pcr = PCR_TBOOT_MLE;
135 break;
136 default:
137 return FAILED;
138 }
139
140 measurement = evidence->get_measurement(evidence, &extended_pcr,
141 &algo, &transform, &measurement_time);
142 if (extended_pcr != this->extended_pcr)
143 {
144 return FAILED;
145 }
146
147 /* TODO check measurement in database */
148
149 has_pcr_info = evidence->get_pcr_info(evidence, &pcr_before, &pcr_after);
150 if (has_pcr_info)
151 {
152 if (!pts->add_pcr(pts, extended_pcr, pcr_before, pcr_after))
153 {
154 return FAILED;
155 }
156 }
157
158 return (this->extended_pcr == PCR_TBOOT_MLE) ? SUCCESS : NEED_MORE;
159 }
160
161 METHOD(pts_component_t, destroy, void,
162 pts_ita_comp_tboot_t *this)
163 {
164 this->name->destroy(this->name);
165 free(this);
166 }
167
168 /**
169 * See header
170 */
171 pts_component_t *pts_ita_comp_tboot_create(u_int8_t qualifier)
172 {
173 pts_ita_comp_tboot_t *this;
174
175 INIT(this,
176 .public = {
177 .get_comp_func_name = _get_comp_func_name,
178 .get_evidence_flags = _get_evidence_flags,
179 .measure = _measure,
180 .verify = _verify,
181 .destroy = _destroy,
182 },
183 .name = pts_comp_func_name_create(PEN_ITA, PTS_ITA_COMP_FUNC_NAME_TBOOT,
184 qualifier),
185 );
186
187 return &this->public;
188 }
189