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