fixed two memory leaks
[strongswan.git] / src / libpts / pts / components / pts_comp_evidence.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu, Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pts/components/pts_comp_evidence.h"
17
18 #include <debug.h>
19
20 typedef struct private_pts_comp_evidence_t private_pts_comp_evidence_t;
21
22 /**
23 * Private data of a pts_comp_evidence_t object.
24 */
25 struct private_pts_comp_evidence_t {
26
27 /**
28 * Public pts_comp_evidence_t interface.
29 */
30 pts_comp_evidence_t public;
31
32 /**
33 * Component Functional Name
34 */
35 pts_comp_func_name_t *name;
36
37 /**
38 * Sub-Component Depth
39 */
40 u_int32_t depth;
41
42 /**
43 * Measurement Time
44 */
45 time_t measurement_time;
46
47 /**
48 * Measurement Time
49 */
50 chunk_t measurement;
51
52 /**
53 * Measurement Hash Algorithm
54 */
55 pts_meas_algorithms_t hash_algorithm;
56
57 /**
58 * Is PCR Information included?
59 */
60 bool has_pcr_info;
61
62 /**
63 * PCR the measurement was extended into
64 */
65 u_int32_t extended_pcr;
66
67 /**
68 * PCR value before extension
69 */
70 chunk_t pcr_before;
71
72 /**
73 * PCR value after extension
74 */
75 chunk_t pcr_after;
76
77 /**
78 * Transformation used for extending measurement into PCR
79 */
80 pts_pcr_transform_t transform;
81
82 /**
83 * Component Validation Result
84 */
85 pts_comp_evid_validation_t validation;
86
87 /**
88 * Verification Policy URI
89 */
90 chunk_t policy_uri;
91
92 };
93
94 METHOD(pts_comp_evidence_t, get_comp_func_name, pts_comp_func_name_t*,
95 private_pts_comp_evidence_t *this, u_int32_t *depth)
96 {
97 if (depth)
98 {
99 *depth = this->depth;
100 }
101 return this->name;
102 }
103
104 METHOD(pts_comp_evidence_t, get_extended_pcr, u_int32_t,
105 private_pts_comp_evidence_t *this)
106 {
107 return this->extended_pcr;
108 }
109 METHOD(pts_comp_evidence_t, get_measurement, chunk_t,
110 private_pts_comp_evidence_t *this, u_int32_t *extended_pcr,
111 pts_meas_algorithms_t *algo, pts_pcr_transform_t *transform,
112 time_t *measurement_time)
113 {
114 if (extended_pcr)
115 {
116 *extended_pcr = this->extended_pcr;
117 }
118 if (algo)
119 {
120 *algo = this->hash_algorithm;
121 }
122 if (transform)
123 {
124 *transform = this->transform;
125 }
126 if (measurement_time)
127 {
128 *measurement_time = this->measurement_time;
129 }
130 return this->measurement;
131 }
132
133 METHOD(pts_comp_evidence_t, get_pcr_info, bool,
134 private_pts_comp_evidence_t *this, chunk_t *pcr_before, chunk_t *pcr_after)
135 {
136 if (pcr_before)
137 {
138 *pcr_before = this->pcr_before;
139 }
140 if (pcr_after)
141 {
142 *pcr_after = this->pcr_after;
143 }
144 return this->has_pcr_info;
145 }
146
147 METHOD(pts_comp_evidence_t, set_pcr_info, void,
148 private_pts_comp_evidence_t *this, chunk_t pcr_before, chunk_t pcr_after)
149 {
150 this->has_pcr_info = TRUE;
151 this->pcr_before = pcr_before;
152 this->pcr_after = pcr_after;
153
154 DBG2(DBG_PTS, "PCR %2d before value : %#B", this->extended_pcr, &pcr_before);
155 DBG2(DBG_PTS, "PCR %2d after value : %#B", this->extended_pcr, &pcr_after);
156 }
157
158 METHOD(pts_comp_evidence_t, get_validation, pts_comp_evid_validation_t,
159 private_pts_comp_evidence_t *this, chunk_t *uri)
160 {
161 if (uri)
162 {
163 *uri = this->policy_uri;
164 }
165 return this->validation;
166 }
167
168 METHOD(pts_comp_evidence_t, set_validation, void,
169 private_pts_comp_evidence_t *this, pts_comp_evid_validation_t validation,
170 chunk_t uri)
171 {
172 this->validation = validation;
173 this->policy_uri = chunk_clone(uri);
174 }
175
176 METHOD(pts_comp_evidence_t, destroy, void,
177 private_pts_comp_evidence_t *this)
178 {
179 this->name->destroy(this->name);
180 free(this->measurement.ptr);
181 free(this->pcr_before.ptr);
182 free(this->pcr_after.ptr);
183 free(this->policy_uri.ptr);
184 free(this);
185 }
186
187 /**
188 * See header
189 */
190 pts_comp_evidence_t *pts_comp_evidence_create(pts_comp_func_name_t *name,
191 u_int32_t depth,
192 u_int32_t extended_pcr,
193 pts_meas_algorithms_t algo,
194 pts_pcr_transform_t transform,
195 time_t measurement_time,
196 chunk_t measurement)
197 {
198 private_pts_comp_evidence_t *this;
199
200 INIT(this,
201 .public = {
202 .get_comp_func_name = _get_comp_func_name,
203 .get_extended_pcr = _get_extended_pcr,
204 .get_measurement = _get_measurement,
205 .get_pcr_info = _get_pcr_info,
206 .set_pcr_info = _set_pcr_info,
207 .get_validation = _get_validation,
208 .set_validation = _set_validation,
209 .destroy = _destroy,
210 },
211 .name = name,
212 .depth = depth,
213 .extended_pcr = extended_pcr,
214 .hash_algorithm = algo,
215 .transform = transform,
216 .measurement_time = measurement_time,
217 .measurement = measurement,
218 );
219
220 name->log(name, "");
221 DBG2(DBG_PTS, "measurement time: %T", &measurement_time, FALSE);
222 DBG2(DBG_PTS, "PCR %2d extended with: %#B", extended_pcr, &measurement);
223
224 return &this->public;
225 }
226