3a8c464fe4a4ee7d79aae274418f54e0d02f1e67
[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 <sys/utsname.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_PTS, "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_PTS, "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_PTS, "could not parse tpm version info: tss error 0x%x",
127 result);
128 }
129 else
130 {
131 DBG2(DBG_PTS, "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_PTS, "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_PTS, "loaded AIK public key from '%s'", key_path);
204 return;
205 }
206 }
207 DBG1(DBG_PTS, "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_PTS," 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, is_path_valid, bool, private_pts_t *this, char *path,
274 pts_error_code_t *error_code)
275 {
276 int error;
277 struct stat st;
278
279 *error_code = 0;
280 error = stat(path, &st);
281 if (error == 0)
282 {
283 return TRUE;
284 }
285 else if (error == ENOENT || error == ENOTDIR)
286 {
287 DBG1(DBG_PTS, "file/directory does not exist %s", path);
288 *error_code = TCG_PTS_FILE_NOT_FOUND;
289 }
290 else if (error == EFAULT)
291 {
292 DBG1(DBG_PTS, "bad address %s", path);
293 *error_code = TCG_PTS_INVALID_PATH;
294 }
295 else
296 {
297 DBG1(DBG_PTS, "error: %s occurred while validating path: %s",
298 strerror(error), path);
299 return FALSE;
300 }
301
302 return TRUE;
303 }
304
305 METHOD(pts_t, do_measurements, pts_file_meas_t*,
306 private_pts_t *this, u_int16_t request_id, char *pathname, bool is_directory)
307 {
308 hasher_t *hasher;
309 hash_algorithm_t hash_alg;
310 u_char hash[HASH_SIZE_SHA384];
311 chunk_t measurement;
312 pts_file_meas_t *measurements;
313
314 /* Create a hasher */
315 hash_alg = pts_meas_to_hash_algorithm(this->algorithm);
316 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
317 if (!hasher)
318 {
319 DBG1(DBG_PTS, " hasher %N not available", hash_algorithm_names, hash_alg);
320 return NULL;
321 }
322
323 /* Create a measurement object */
324 measurements = pts_file_meas_create(request_id);
325
326 /* Link the hash to the measurement and set the measurement length */
327 measurement = chunk_create(hash, hasher->get_hash_size(hasher));
328
329 if (is_directory)
330 {
331 enumerator_t *enumerator;
332 char *rel_name, *abs_name;
333 struct stat st;
334
335 enumerator = enumerator_create_directory(pathname);
336 if (!enumerator)
337 {
338 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
339 strerror(errno));
340 hasher->destroy(hasher);
341 measurements->destroy(measurements);
342 return NULL;
343 }
344 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
345 {
346 /* measure regular files only */
347 if (S_ISREG(st.st_mode) && *rel_name != '.')
348 {
349 if (!hash_file(hasher, abs_name, hash))
350 {
351 enumerator->destroy(enumerator);
352 hasher->destroy(hasher);
353 measurements->destroy(measurements);
354 return NULL;
355 }
356 DBG2(DBG_PTS, " %#B for '%s'", &measurement, rel_name);
357 measurements->add(measurements, rel_name, measurement);
358 }
359 }
360 enumerator->destroy(enumerator);
361 }
362 else
363 {
364 char *filename;
365
366 if (!hash_file(hasher, pathname, hash))
367 {
368 hasher->destroy(hasher);
369 measurements->destroy(measurements);
370 return NULL;
371 }
372 filename = get_filename(pathname);
373 DBG2(DBG_PTS, " %#B for '%s'", &measurement, filename);
374 measurements->add(measurements, filename, measurement);
375 }
376 hasher->destroy(hasher);
377
378 return measurements;
379 }
380
381 /**
382 * Obtain statistical information describing a file
383 */
384 static bool file_metadata(char *pathname, pts_file_metadata_t **entry)
385 {
386 struct stat st;
387 pts_file_metadata_t *tmp;
388
389 tmp = malloc_thing(pts_file_metadata_t);
390
391 if (stat(pathname, &st))
392 {
393 DBG1(DBG_PTS, "Unable to obtain statistical information about %s", pathname);
394 return FALSE;
395 }
396
397 tmp->filename = strdup(pathname);
398 tmp->meta_length = PTS_FILE_METADATA_SIZE + strlen(tmp->filename);
399
400 if (S_ISREG(st.st_mode))
401 {
402 tmp->type = PTS_FILE_REGULAR;
403 }
404 else if (S_ISDIR(st.st_mode))
405 {
406 tmp->type = PTS_FILE_DIRECTORY;
407 }
408 else if (S_ISCHR(st.st_mode))
409 {
410 tmp->type = PTS_FILE_CHAR_SPEC;
411 }
412 else if (S_ISBLK(st.st_mode))
413 {
414 tmp->type = PTS_FILE_BLOCK_SPEC;
415 }
416 else if (S_ISFIFO(st.st_mode))
417 {
418 tmp->type = PTS_FILE_FIFO;
419 }
420 else if (S_ISLNK(st.st_mode))
421 {
422 tmp->type = PTS_FILE_SYM_LINK;
423 }
424 else if (S_ISSOCK(st.st_mode))
425 {
426 tmp->type = PTS_FILE_SOCKET;
427 }
428 else
429 {
430 tmp->type = PTS_FILE_OTHER;
431 }
432
433 tmp->filesize = (u_int64_t)st.st_size;
434 tmp->create_time = st.st_ctime;
435 tmp->last_modify_time = st.st_mtime;
436 tmp->last_access_time = st.st_atime;
437 tmp->owner_id = (u_int64_t)st.st_uid;
438 tmp->group_id = (u_int64_t)st.st_gid;
439
440 *entry = tmp;
441
442 return TRUE;
443 }
444
445 METHOD(pts_t, get_metadata, pts_file_meta_t*,
446 private_pts_t *this, char *pathname, bool is_directory)
447 {
448 pts_file_meta_t *metadata;
449 pts_file_metadata_t *entry;
450
451 /* Create a metadata object */
452 metadata = pts_file_meta_create();
453
454 if (is_directory)
455 {
456 enumerator_t *enumerator;
457 char *rel_name, *abs_name;
458 struct stat st;
459
460 enumerator = enumerator_create_directory(pathname);
461 if (!enumerator)
462 {
463 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
464 strerror(errno));
465 metadata->destroy(metadata);
466 return NULL;
467 }
468 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
469 {
470 /* measure regular files only */
471 if (S_ISREG(st.st_mode) && *rel_name != '.')
472 {
473 if (!file_metadata(abs_name, &entry))
474 {
475 enumerator->destroy(enumerator);
476 metadata->destroy(metadata);
477 return NULL;
478 }
479 DBG3(DBG_PTS, "File name: %s", entry->filename);
480 DBG3(DBG_PTS, " type: %d", entry->type);
481 DBG3(DBG_PTS, " size: %d", entry->filesize);
482 DBG3(DBG_PTS, " create time: %s", ctime(&entry->create_time));
483 DBG3(DBG_PTS, " last modified: %s", ctime(&entry->last_modify_time));
484 DBG3(DBG_PTS, " last accessed: %s", ctime(&entry->last_access_time));
485 DBG3(DBG_PTS, " owner id: %d", entry->owner_id);
486 DBG3(DBG_PTS, " group id: %d", entry->group_id);
487
488 metadata->add(metadata, entry);
489 }
490 }
491 enumerator->destroy(enumerator);
492 }
493 else
494 {
495 char *filename;
496
497 if (!file_metadata(pathname, &entry))
498 {
499 metadata->destroy(metadata);
500 return NULL;
501 }
502 filename = get_filename(pathname);
503 DBG3(DBG_PTS, "File name: %s", entry->filename);
504 DBG3(DBG_PTS, " type: %d", entry->type);
505 DBG3(DBG_PTS, " size: %d", entry->filesize);
506 DBG3(DBG_PTS, " create time: %s", ctime(&entry->create_time));
507 DBG3(DBG_PTS, " last modified: %s", ctime(&entry->last_modify_time));
508 DBG3(DBG_PTS, " last accessed: %s", ctime(&entry->last_access_time));
509 DBG3(DBG_PTS, " owner id: %d", entry->owner_id);
510 DBG3(DBG_PTS, " group id: %d", entry->group_id);
511 metadata->add(metadata, entry);
512 }
513
514 return metadata;
515 }
516
517
518 METHOD(pts_t, destroy, void,
519 private_pts_t *this)
520 {
521 DESTROY_IF(this->aik);
522 free(this->platform_info);
523 free(this->tpm_version_info.ptr);
524 free(this);
525 }
526
527 /**
528 * Determine Linux distribution and hardware platform
529 */
530 static char* extract_platform_info(void)
531 {
532 FILE *file;
533 char buf[BUF_LEN], *pos, *value = NULL;
534 int i, len;
535 struct utsname uninfo;
536
537 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
538 const char* releases[] = {
539 "/etc/lsb-release", "/etc/debian_version",
540 "/etc/SuSE-release", "/etc/novell-release",
541 "/etc/sles-release", "/etc/redhat-release",
542 "/etc/fedora-release", "/etc/gentoo-release",
543 "/etc/slackware-version", "/etc/annvix-release",
544 "/etc/arch-release", "/etc/arklinux-release",
545 "/etc/aurox-release", "/etc/blackcat-release",
546 "/etc/cobalt-release", "/etc/conectiva-release",
547 "/etc/debian_release", "/etc/immunix-release",
548 "/etc/lfs-release", "/etc/linuxppc-release",
549 "/etc/mandrake-release", "/etc/mandriva-release",
550 "/etc/mandrakelinux-release", "/etc/mklinux-release",
551 "/etc/pld-release", "/etc/redhat_version",
552 "/etc/slackware-release", "/etc/e-smith-release",
553 "/etc/release", "/etc/sun-release",
554 "/etc/tinysofa-release", "/etc/turbolinux-release",
555 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
556 "/etc/va-release", "/etc/yellowdog-release"
557 };
558
559 const char description[] = "DISTRIB_DESCRIPTION=\"";
560
561 for (i = 0; i < countof(releases); i++)
562 {
563 file = fopen(releases[i], "r");
564 if (!file)
565 {
566 continue;
567 }
568 fseek(file, 0, SEEK_END);
569 len = min(ftell(file), sizeof(buf)-1);
570 rewind(file);
571 buf[len] = '\0';
572 if (fread(buf, 1, len, file) != len)
573 {
574 DBG1(DBG_PTS, "failed to read file '%s'", releases[i]);
575 fclose(file);
576 return NULL;
577 }
578 fclose(file);
579
580 if (i == 0) /* LSB release */
581 {
582 pos = strstr(buf, description);
583 if (!pos)
584 {
585 DBG1(DBG_PTS, "failed to find begin of lsb-release "
586 "DESCRIPTION field");
587 return NULL;
588 }
589 value = pos + strlen(description);
590 pos = strchr(value, '"');
591 if (!pos)
592 {
593 DBG1(DBG_PTS, "failed to find end of lsb-release "
594 "DESCRIPTION field");
595 return NULL;
596 }
597 }
598 else
599 {
600 value = buf;
601 pos = strchr(value, '\n');
602 if (!pos)
603 {
604 DBG1(DBG_PTS, "failed to find end of release string");
605 return NULL;
606 }
607 }
608 break;
609 }
610
611 if (!value)
612 {
613 DBG1(DBG_PTS, "no distribution release file found");
614 return NULL;
615 }
616
617 if (uname(&uninfo) < 0)
618 {
619 DBG1(DBG_PTS, "could not retrieve machine architecture");
620 return NULL;
621 }
622
623 *pos++ = ' ';
624 len = sizeof(buf)-1 + (pos - buf);
625 strncpy(pos, uninfo.machine, len);
626
627 DBG1(DBG_PTS, "platform is '%s'", value);
628 return strdup(value);
629 }
630
631 /**
632 * Check for a TPM by querying for TPM Version Info
633 */
634 static bool has_tpm(private_pts_t *this)
635 {
636 TSS_HCONTEXT hContext;
637 TSS_HTPM hTPM;
638 TSS_RESULT result;
639 u_int32_t version_info_len;
640
641 result = Tspi_Context_Create(&hContext);
642 if (result != TSS_SUCCESS)
643 {
644 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
645 return FALSE;
646 }
647 result = Tspi_Context_Connect(hContext, NULL);
648 if (result != TSS_SUCCESS)
649 {
650 goto err;
651 }
652 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
653 if (result != TSS_SUCCESS)
654 {
655 goto err;
656 }
657 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
658 &version_info_len,
659 &this->tpm_version_info.ptr);
660 this->tpm_version_info.len = version_info_len;
661 if (result != TSS_SUCCESS)
662 {
663 goto err;
664 }
665 this->tpm_version_info = chunk_clone(this->tpm_version_info);
666 return TRUE;
667
668 err:
669 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
670 Tspi_Context_Close(hContext);
671 return FALSE;
672 }
673
674 /**
675 * See header
676 */
677 pts_t *pts_create(bool is_imc)
678 {
679 private_pts_t *this;
680
681 INIT(this,
682 .public = {
683 .get_proto_caps = _get_proto_caps,
684 .set_proto_caps = _set_proto_caps,
685 .get_meas_algorithm = _get_meas_algorithm,
686 .set_meas_algorithm = _set_meas_algorithm,
687 .get_platform_info = _get_platform_info,
688 .set_platform_info = _set_platform_info,
689 .get_tpm_version_info = _get_tpm_version_info,
690 .set_tpm_version_info = _set_tpm_version_info,
691 .get_aik = _get_aik,
692 .set_aik = _set_aik,
693 .is_path_valid = _is_path_valid,
694 .do_measurements = _do_measurements,
695 .get_metadata = _get_metadata,
696 .destroy = _destroy,
697 },
698 .proto_caps = PTS_PROTO_CAPS_V,
699 .algorithm = PTS_MEAS_ALGO_SHA256,
700 );
701
702 if (is_imc)
703 {
704 this->platform_info = extract_platform_info();
705
706 if (has_tpm(this))
707 {
708 this->has_tpm = TRUE;
709 this->proto_caps |= PTS_PROTO_CAPS_T;
710 load_aik(this);
711 }
712 }
713 else
714 {
715 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_C;
716 }
717
718 return &this->public;
719 }
720