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