use arch instead of uname -p
[strongswan.git] / src / libimcv / tcg / pts / pts.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
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.h"
17
18 #include <debug.h>
19 #include <crypto/hashers/hasher.h>
20 #include <utils/lexparser.h>
21
22 #include <trousers/tss.h>
23 #include <trousers/trousers.h>
24
25 #include <sys/stat.h>
26 #include <errno.h>
27
28 #define PTS_BUF_SIZE 4096
29
30 typedef struct private_pts_t private_pts_t;
31
32 /**
33 * Private data of a pts_t object.
34 *
35 */
36 struct private_pts_t {
37
38 /**
39 * Public pts_t interface.
40 */
41 pts_t public;
42
43 /**
44 * PTS Protocol Capabilities
45 */
46 pts_proto_caps_flag_t proto_caps;
47
48 /**
49 * PTS Measurement Algorithm
50 */
51 pts_meas_algorithms_t algorithm;
52
53 /**
54 * Platform and OS Info
55 */
56 char *platform_info;
57
58 /**
59 * Do we have an activated TPM
60 */
61 bool has_tpm;
62
63 /**
64 * Contains a TPM_CAP_VERSION_INFO struct
65 */
66 chunk_t tpm_version_info;
67
68 /**
69 * Contains a Attestation Identity Key or Certificate
70 */
71 certificate_t *aik;
72
73 };
74
75 METHOD(pts_t, get_proto_caps, pts_proto_caps_flag_t,
76 private_pts_t *this)
77 {
78 return this->proto_caps;
79 }
80
81 METHOD(pts_t, set_proto_caps, void,
82 private_pts_t *this, pts_proto_caps_flag_t flags)
83 {
84 this->proto_caps = flags;
85 DBG2(DBG_IMC, "supported PTS protocol capabilities: %s%s%s%s%s",
86 flags & PTS_PROTO_CAPS_C ? "C" : ".",
87 flags & PTS_PROTO_CAPS_V ? "V" : ".",
88 flags & PTS_PROTO_CAPS_D ? "D" : ".",
89 flags & PTS_PROTO_CAPS_T ? "T" : ".",
90 flags & PTS_PROTO_CAPS_X ? "X" : ".");
91 }
92
93 METHOD(pts_t, get_meas_algorithm, pts_meas_algorithms_t,
94 private_pts_t *this)
95 {
96 return this->algorithm;
97 }
98
99 METHOD(pts_t, set_meas_algorithm, void,
100 private_pts_t *this, pts_meas_algorithms_t algorithm)
101 {
102 hash_algorithm_t hash_alg;
103
104 hash_alg = pts_meas_to_hash_algorithm(algorithm);
105 DBG2(DBG_IMC, "selected PTS measurement algorithm is %N",
106 hash_algorithm_names, hash_alg);
107 if (hash_alg != HASH_UNKNOWN)
108 {
109 this->algorithm = algorithm;
110 }
111 }
112
113 /**
114 * Print TPM 1.2 Version Info
115 */
116 static void print_tpm_version_info(private_pts_t *this)
117 {
118 TPM_CAP_VERSION_INFO versionInfo;
119 UINT64 offset = 0;
120 TSS_RESULT result;
121
122 result = Trspi_UnloadBlob_CAP_VERSION_INFO(&offset,
123 this->tpm_version_info.ptr, &versionInfo);
124 if (result != TSS_SUCCESS)
125 {
126 DBG1(DBG_TNC, "could not parse tpm version info: tss error 0x%x",
127 result);
128 }
129 else
130 {
131 DBG2(DBG_TNC, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
132 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
133 versionInfo.version.major, versionInfo.version.minor,
134 versionInfo.version.revMajor, versionInfo.version.revMinor,
135 versionInfo.specLevel, versionInfo.errataRev,
136 versionInfo.tpmVendorID);
137 }
138 }
139
140 METHOD(pts_t, get_platform_info, char*,
141 private_pts_t *this)
142 {
143 return this->platform_info;
144 }
145
146 METHOD(pts_t, set_platform_info, void,
147 private_pts_t *this, char *info)
148 {
149 free(this->platform_info);
150 this->platform_info = strdup(info);
151 }
152
153 METHOD(pts_t, get_tpm_version_info, bool,
154 private_pts_t *this, chunk_t *info)
155 {
156 if (!this->has_tpm)
157 {
158 return FALSE;
159 }
160 *info = this->tpm_version_info;
161 print_tpm_version_info(this);
162 return TRUE;
163 }
164
165 METHOD(pts_t, set_tpm_version_info, void,
166 private_pts_t *this, chunk_t info)
167 {
168 this->tpm_version_info = chunk_clone(info);
169 print_tpm_version_info(this);
170 }
171
172 /**
173 * Load an AIK certificate or public key,
174 * the certificate having precedence over the public key if both are present
175 */
176 static void load_aik(private_pts_t *this)
177 {
178 char *cert_path, *key_path;
179
180 cert_path = lib->settings->get_str(lib->settings,
181 "libimcv.plugins.imc-attestation.aik_cert", NULL);
182 key_path = lib->settings->get_str(lib->settings,
183 "libimcv.plugins.imc-attestation.aik_key", NULL);
184
185 if (cert_path)
186 {
187 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
188 CERT_X509, BUILD_FROM_FILE,
189 cert_path, BUILD_END);
190 if (this->aik)
191 {
192 DBG2(DBG_IMC, "loaded AIK certificate from '%s'", cert_path);
193 return;
194 }
195 }
196 if (key_path)
197 {
198 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
199 CERT_TRUSTED_PUBKEY, BUILD_FROM_FILE,
200 key_path, BUILD_END);
201 if (this->aik)
202 {
203 DBG2(DBG_IMC, "loaded AIK public key from '%s'", key_path);
204 return;
205 }
206 }
207 DBG1(DBG_IMC, "neither AIK certificate nor public key is available");
208 }
209
210 METHOD(pts_t, get_aik, certificate_t*,
211 private_pts_t *this)
212 {
213 return this->aik;
214 }
215
216 METHOD(pts_t, set_aik, void,
217 private_pts_t *this, certificate_t *aik)
218 {
219 DESTROY_IF(this->aik);
220 this->aik = aik->get_ref(aik);
221 }
222
223 /**
224 * Compute a hash over a file
225 */
226 static bool hash_file(hasher_t *hasher, char *pathname, u_char *hash)
227 {
228 u_char buffer[PTS_BUF_SIZE];
229 FILE *file;
230 int bytes_read;
231
232 file = fopen(pathname, "rb");
233 if (!file)
234 {
235 DBG1(DBG_IMC," file '%s' can not be opened, %s", pathname,
236 strerror(errno));
237 return FALSE;
238 }
239 while (TRUE)
240 {
241 bytes_read = fread(buffer, 1, sizeof(buffer), file);
242 if (bytes_read > 0)
243 {
244 hasher->get_hash(hasher, chunk_create(buffer, bytes_read), NULL);
245 }
246 else
247 {
248 hasher->get_hash(hasher, chunk_empty, hash);
249 break;
250 }
251 }
252 fclose(file);
253
254 return TRUE;
255 }
256
257 /**
258 * Get the relative filename of a fully qualified file pathname
259 */
260 static char* get_filename(char *pathname)
261 {
262 char *pos, *filename;
263
264 pos = filename = pathname;
265 while (pos && *(++pos) != '\0')
266 {
267 filename = pos;
268 pos = strchr(filename, '/');
269 }
270 return filename;
271 }
272
273 METHOD(pts_t, do_measurements, pts_file_meas_t*,
274 private_pts_t *this, u_int16_t request_id, char *pathname, bool is_directory)
275 {
276 hasher_t *hasher;
277 hash_algorithm_t hash_alg;
278 u_char hash[HASH_SIZE_SHA384];
279 chunk_t measurement;
280 pts_file_meas_t *measurements;
281
282 /* Create a hasher */
283 hash_alg = pts_meas_to_hash_algorithm(this->algorithm);
284 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
285 if (!hasher)
286 {
287 DBG1(DBG_IMC, " hasher %N not available", hash_algorithm_names, hash_alg);
288 return NULL;
289 }
290
291 /* Create a measurement object */
292 measurements = pts_file_meas_create(request_id);
293
294 /* Link the hash to the measurement and set the measurement length */
295 measurement = chunk_create(hash, hasher->get_hash_size(hasher));
296
297 if (is_directory)
298 {
299 enumerator_t *enumerator;
300 char *rel_name, *abs_name;
301 struct stat st;
302
303 enumerator = enumerator_create_directory(pathname);
304 if (!enumerator)
305 {
306 DBG1(DBG_IMC," directory '%s' can not be opened, %s", pathname,
307 strerror(errno));
308 hasher->destroy(hasher);
309 measurements->destroy(measurements);
310 return NULL;
311 }
312 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
313 {
314 /* measure regular files only */
315 if (S_ISREG(st.st_mode) && *rel_name != '.')
316 {
317 if (!hash_file(hasher, abs_name, hash))
318 {
319 enumerator->destroy(enumerator);
320 hasher->destroy(hasher);
321 measurements->destroy(measurements);
322 return NULL;
323 }
324 DBG2(DBG_IMC, " %#B for '%s'", &measurement, rel_name);
325 measurements->add(measurements, rel_name, measurement);
326 }
327 }
328 enumerator->destroy(enumerator);
329 }
330 else
331 {
332 char *filename;
333
334 if (!hash_file(hasher, pathname, hash))
335 {
336 hasher->destroy(hasher);
337 measurements->destroy(measurements);
338 return NULL;
339 }
340 filename = get_filename(pathname);
341 DBG2(DBG_IMC, " %#B for '%s'", &measurement, filename);
342 measurements->add(measurements, filename, measurement);
343 }
344 hasher->destroy(hasher);
345
346 return measurements;
347 }
348
349 METHOD(pts_t, destroy, void,
350 private_pts_t *this)
351 {
352 DESTROY_IF(this->aik);
353 free(this->platform_info);
354 free(this->tpm_version_info.ptr);
355 free(this);
356 }
357
358 /**
359 * Determine Linux distribution and hardware platform
360 */
361 static char* extract_platform_info(void)
362 {
363 FILE *file;
364 const char description[] = "Description:";
365 char buf[BUF_LEN], *pos, *value;
366 int value_len;
367
368 /* open a pipe stream for reading the output of the lsb_release commmand */
369 file = popen("/usr/bin/lsb_release -d" , "r");
370 if (!file)
371 {
372 DBG2(DBG_IMC, "failed to run lsb_release command");
373 return NULL;
374 }
375
376 /* read the output the lsb_release command */
377 if (!fgets(buf, BUF_LEN-1, file))
378 {
379 DBG2(DBG_IMC, "failed to read output of lsb_release command");
380 pclose(file);
381 return NULL;
382 }
383 pclose(file);
384
385 pos = strstr(buf, description);
386 if (!pos)
387 {
388 DBG2(DBG_IMC, "failed to find lsb_release description field");
389 return NULL;
390 }
391 value = pos + strlen(description);
392
393 /* eat whitespace */
394 while (*value == ' ' || *value == '\t')
395 {
396 value++;
397 }
398
399 /* remove newline at the end and move value to the front of the buffer */
400 value_len = strlen(value) - 1;
401 memcpy(buf, value, value_len);
402 buf[value_len] = ' ';
403
404 /* open a pipe stream for reading the output of the arch commmand */
405 file = popen("/usr/bin/arch" , "r");
406 if (!file)
407 {
408 DBG2(DBG_IMC, "failed to run arch command");
409 return NULL;
410 }
411
412 /* Read the output the arch command */
413 if (!fgets(buf + value_len + 1, BUF_LEN - value_len - 2, file))
414 {
415 DBG2(DBG_IMC, "failed to read output of arch command");
416 pclose(file);
417 return NULL;
418 }
419 pclose(file);
420
421 /* remove newline at the end */
422 buf[strlen(buf)-1] = '\0';
423
424 DBG1(DBG_IMV, "platform is '%s'", buf);
425 return strdup(buf);
426 }
427
428 /**
429 * Check for a TPM by querying for TPM Version Info
430 */
431 static bool has_tpm(private_pts_t *this)
432 {
433 TSS_HCONTEXT hContext;
434 TSS_HTPM hTPM;
435 TSS_RESULT result;
436
437 result = Tspi_Context_Create(&hContext);
438 if (result != TSS_SUCCESS)
439 {
440 goto err;
441 }
442 result = Tspi_Context_Connect(hContext, NULL);
443 if (result != TSS_SUCCESS)
444 {
445 goto err;
446 }
447 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
448 if (result != TSS_SUCCESS)
449 {
450 goto err;
451 }
452 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
453 &this->tpm_version_info.len,
454 &this->tpm_version_info.ptr);
455 if (result != TSS_SUCCESS)
456 {
457 goto err;
458 }
459 this->tpm_version_info = chunk_clone(this->tpm_version_info);
460 return TRUE;
461
462 err:
463 DBG1(DBG_IMC, "TPM not available: tss error 0x%x", result);
464 return FALSE;
465 }
466
467 /**
468 * See header
469 */
470 pts_t *pts_create(bool is_imc)
471 {
472 private_pts_t *this;
473
474 INIT(this,
475 .public = {
476 .get_proto_caps = _get_proto_caps,
477 .set_proto_caps = _set_proto_caps,
478 .get_meas_algorithm = _get_meas_algorithm,
479 .set_meas_algorithm = _set_meas_algorithm,
480 .get_platform_info = _get_platform_info,
481 .set_platform_info = _set_platform_info,
482 .get_tpm_version_info = _get_tpm_version_info,
483 .set_tpm_version_info = _set_tpm_version_info,
484 .get_aik = _get_aik,
485 .set_aik = _set_aik,
486 .do_measurements = _do_measurements,
487 .destroy = _destroy,
488 },
489 .proto_caps = PTS_PROTO_CAPS_V,
490 .algorithm = PTS_MEAS_ALGO_SHA256,
491 );
492
493 if (is_imc)
494 {
495 this->platform_info = extract_platform_info();
496
497 if (has_tpm(this))
498 {
499 this->has_tpm = TRUE;
500 this->proto_caps |= PTS_PROTO_CAPS_T;
501 load_aik(this);
502 }
503 }
504 else
505 {
506 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_C;
507 }
508
509 return &this->public;
510 }
511