00b9ef3c5ce178e1cfb07b8431f172984fac6899
[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 * Sub-component depth
45 */
46 u_int32_t depth;
47
48 /**
49 * PTS measurement database
50 */
51 pts_database_t *pts_db;
52
53 };
54
55 METHOD(pts_component_t, get_comp_func_name, pts_comp_func_name_t*,
56 pts_ita_comp_tgrub_t *this)
57 {
58 return this->name;
59 }
60
61 METHOD(pts_component_t, get_evidence_flags, u_int8_t,
62 pts_ita_comp_tgrub_t *this)
63 {
64 return PTS_REQ_FUNC_COMP_EVID_PCR;
65 }
66
67 METHOD(pts_component_t, get_depth, u_int32_t,
68 pts_ita_comp_tgrub_t *this)
69 {
70 return this->depth;
71 }
72
73 METHOD(pts_component_t, measure, status_t,
74 pts_ita_comp_tgrub_t *this, pts_t *pts, pts_comp_evidence_t **evidence)
75 {
76 pts_comp_evidence_t *evid;
77 u_int32_t extended_pcr;
78 time_t measurement_time;
79 chunk_t measurement, pcr_before, pcr_after;
80 pts_pcr_transform_t pcr_transform;
81 pts_meas_algorithms_t hash_algo;
82 size_t hash_size, pcr_len;
83
84 /* Provisional implementation for TGRUB */
85 extended_pcr = PCR_DEBUG;
86 time(&measurement_time);
87
88 if (!pts->read_pcr(pts, extended_pcr, &pcr_after))
89 {
90 DBG1(DBG_PTS, "error occurred while reading PCR: %d", extended_pcr);
91 return FAILED;
92 }
93
94 hash_algo = pts->get_meas_algorithm(pts);
95 hash_size = pts_meas_algo_hash_size(hash_algo);
96 pcr_len = pts->get_pcr_len(pts);
97 pcr_transform = pts_meas_algo_to_pcr_transform(hash_algo, pcr_len);
98
99 measurement = chunk_alloc(hash_size);
100 memset(measurement.ptr, 0x00, measurement.len);
101
102 pcr_before = chunk_alloc(pcr_len);
103 memset(pcr_before.ptr, 0x00, pcr_before.len);
104
105 evid = *evidence = pts_comp_evidence_create(this->name->clone(this->name),
106 this->depth, extended_pcr,
107 hash_algo, pcr_transform,
108 measurement_time, measurement);
109 evid->set_pcr_info(evid, pcr_before, pcr_after);
110
111 return SUCCESS;
112 }
113
114 METHOD(pts_component_t, verify, status_t,
115 pts_ita_comp_tgrub_t *this, pts_t *pts, pts_comp_evidence_t *evidence)
116 {
117 bool has_pcr_info;
118 u_int32_t extended_pcr;
119 pts_meas_algorithms_t algo;
120 pts_pcr_transform_t transform;
121 time_t measurement_time;
122 chunk_t measurement, pcr_before, pcr_after;
123
124 measurement = evidence->get_measurement(evidence, &extended_pcr,
125 &algo, &transform, &measurement_time);
126 if (extended_pcr != PCR_DEBUG)
127 {
128 return FAILED;
129 }
130
131 /* TODO check measurement in database */
132
133 has_pcr_info = evidence->get_pcr_info(evidence, &pcr_before, &pcr_after);
134 if (has_pcr_info)
135 {
136 if (!pts->add_pcr(pts, extended_pcr, pcr_before, pcr_after))
137 {
138 return FAILED;
139 }
140 }
141
142 return SUCCESS;
143 }
144
145 METHOD(pts_component_t, finalize, bool,
146 pts_ita_comp_tgrub_t *this)
147 {
148 return FALSE;
149 }
150
151 METHOD(pts_component_t, destroy, void,
152 pts_ita_comp_tgrub_t *this)
153 {
154 this->name->destroy(this->name);
155 free(this);
156 }
157
158 /**
159 * See header
160 */
161 pts_component_t *pts_ita_comp_tgrub_create(u_int8_t qualifier, u_int32_t depth,
162 pts_database_t *pts_db)
163 {
164 pts_ita_comp_tgrub_t *this;
165
166 INIT(this,
167 .public = {
168 .get_comp_func_name = _get_comp_func_name,
169 .get_evidence_flags = _get_evidence_flags,
170 .get_depth = _get_depth,
171 .measure = _measure,
172 .verify = _verify,
173 .finalize = _finalize,
174 .destroy = _destroy,
175 },
176 .name = pts_comp_func_name_create(PEN_ITA, PTS_ITA_COMP_FUNC_NAME_TGRUB,
177 qualifier),
178 .depth = depth,
179 .pts_db = pts_db,
180 );
181
182 return &this->public;
183 }
184