Implemented functions for reading and extending TPM PCR
[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 * PTS Diffie Hellman Group
55 */
56 pts_dh_group_t dh_group;
57
58 /**
59 * Contains a Diffie Hellman object
60 */
61 diffie_hellman_t *dh;
62
63 /**
64 * Secret assessment value to be used for TPM Quote as an external data
65 */
66 chunk_t secret;
67
68 /**
69 * Platform and OS Info
70 */
71 char *platform_info;
72
73 /**
74 * Do we have an activated TPM
75 */
76 bool has_tpm;
77
78 /**
79 * Contains a TPM_CAP_VERSION_INFO struct
80 */
81 chunk_t tpm_version_info;
82
83 /**
84 * Contains a Attestation Identity Key or Certificate
85 */
86 certificate_t *aik;
87
88 };
89
90 METHOD(pts_t, get_proto_caps, pts_proto_caps_flag_t,
91 private_pts_t *this)
92 {
93 return this->proto_caps;
94 }
95
96 METHOD(pts_t, set_proto_caps, void,
97 private_pts_t *this, pts_proto_caps_flag_t flags)
98 {
99 this->proto_caps = flags;
100 DBG2(DBG_PTS, "supported PTS protocol capabilities: %s%s%s%s%s",
101 flags & PTS_PROTO_CAPS_C ? "C" : ".",
102 flags & PTS_PROTO_CAPS_V ? "V" : ".",
103 flags & PTS_PROTO_CAPS_D ? "D" : ".",
104 flags & PTS_PROTO_CAPS_T ? "T" : ".",
105 flags & PTS_PROTO_CAPS_X ? "X" : ".");
106 }
107
108 METHOD(pts_t, get_meas_algorithm, pts_meas_algorithms_t,
109 private_pts_t *this)
110 {
111 return this->algorithm;
112 }
113
114 METHOD(pts_t, set_meas_algorithm, void,
115 private_pts_t *this, pts_meas_algorithms_t algorithm)
116 {
117 hash_algorithm_t hash_alg;
118
119 hash_alg = pts_meas_to_hash_algorithm(algorithm);
120 DBG2(DBG_PTS, "selected PTS measurement algorithm is %N",
121 hash_algorithm_names, hash_alg);
122 if (hash_alg != HASH_UNKNOWN)
123 {
124 this->algorithm = algorithm;
125 }
126 }
127
128 METHOD(pts_t, get_dh_group, pts_dh_group_t,
129 private_pts_t *this)
130 {
131 return this->dh_group;
132 }
133
134 METHOD(pts_t, set_dh_group, void,
135 private_pts_t *this, pts_dh_group_t group)
136 {
137 diffie_hellman_group_t dh_group;
138
139 dh_group = pts_dh_group_to_strongswan_dh_group(group);
140 DBG2(DBG_PTS, "selected PTS Diffie Hellman Group is %N",
141 diffie_hellman_group_names, dh_group);
142 if (dh_group != MODP_NONE)
143 {
144 this->dh_group = group;
145 }
146 }
147
148 METHOD(pts_t, create_dh, bool,
149 private_pts_t *this, pts_dh_group_t group)
150 {
151 diffie_hellman_group_t dh_group;
152
153 dh_group = pts_dh_group_to_strongswan_dh_group(group);
154 if (dh_group != MODP_NONE)
155 {
156 this->dh = lib->crypto->create_dh(lib->crypto, dh_group);
157 return TRUE;
158 }
159 DBG1(DBG_PTS, "Unable to create Diffie Hellman object with group %N",
160 diffie_hellman_group_names, dh_group);
161 return FALSE;
162 }
163
164 METHOD(pts_t, get_my_pub_val, chunk_t,
165 private_pts_t *this)
166 {
167 chunk_t public_value;
168
169 this->dh->get_my_public_value(this->dh, &public_value);
170 DBG3(DBG_PTS, "My Public value:%B", &public_value);
171 return public_value;
172 }
173
174 METHOD(pts_t, set_other_pub_val, void,
175 private_pts_t *this, chunk_t value)
176 {
177 DBG3(DBG_PTS, "Partner's Public value:%B", &value);
178 this->dh->set_other_public_value(this->dh, value);
179 }
180
181 METHOD(pts_t, calculate_secret, bool,
182 private_pts_t *this, chunk_t initiator_nonce, chunk_t responder_nonce,
183 pts_meas_algorithms_t algorithm)
184 {
185 hasher_t *hasher;
186 hash_algorithm_t hash_alg;
187 u_char output[HASH_SIZE_SHA384];
188 chunk_t shared_secret;
189
190 /* Create a hasher */
191 hash_alg = pts_meas_to_hash_algorithm(algorithm);
192 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
193 if (!hasher)
194 {
195 DBG1(DBG_PTS, " hasher %N not available", hash_algorithm_names, hash_alg);
196 return FALSE;
197 }
198
199 if (this->dh->get_shared_secret(this->dh, &shared_secret) != SUCCESS)
200 {
201 DBG1(DBG_PTS, "Shared secret couldn't be calculated");
202 hasher->destroy(hasher);
203 return FALSE;
204 }
205
206 hasher->get_hash(hasher, chunk_create("1", sizeof("1")), NULL);
207 hasher->get_hash(hasher, initiator_nonce, NULL);
208 hasher->get_hash(hasher, responder_nonce, NULL);
209 hasher->get_hash(hasher, shared_secret, output);
210
211 /**
212 * Link the hash output to the secret and set the length
213 * Truncate the output to 20 bytes to fit ExternalDate argument of TPM Quote
214 */
215 this->secret = chunk_create(output, HASH_SIZE_SHA1);
216 DBG3(DBG_PTS, "Secret assessment value: %B", &this->secret);
217
218 hasher->destroy(hasher);
219 return TRUE;
220 }
221
222 METHOD(pts_t, get_secret, chunk_t,
223 private_pts_t *this)
224 {
225 return this->secret;
226 }
227
228 /**
229 * Print TPM 1.2 Version Info
230 */
231 static void print_tpm_version_info(private_pts_t *this)
232 {
233 TPM_CAP_VERSION_INFO versionInfo;
234 UINT64 offset = 0;
235 TSS_RESULT result;
236
237 result = Trspi_UnloadBlob_CAP_VERSION_INFO(&offset,
238 this->tpm_version_info.ptr, &versionInfo);
239 if (result != TSS_SUCCESS)
240 {
241 DBG1(DBG_PTS, "could not parse tpm version info: tss error 0x%x",
242 result);
243 }
244 else
245 {
246 DBG2(DBG_PTS, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
247 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
248 versionInfo.version.major, versionInfo.version.minor,
249 versionInfo.version.revMajor, versionInfo.version.revMinor,
250 versionInfo.specLevel, versionInfo.errataRev,
251 versionInfo.tpmVendorID);
252 }
253 }
254
255 METHOD(pts_t, get_platform_info, char*,
256 private_pts_t *this)
257 {
258 return this->platform_info;
259 }
260
261 METHOD(pts_t, set_platform_info, void,
262 private_pts_t *this, char *info)
263 {
264 free(this->platform_info);
265 this->platform_info = strdup(info);
266 }
267
268 METHOD(pts_t, get_tpm_version_info, bool,
269 private_pts_t *this, chunk_t *info)
270 {
271 if (!this->has_tpm)
272 {
273 return FALSE;
274 }
275 *info = this->tpm_version_info;
276 print_tpm_version_info(this);
277 return TRUE;
278 }
279
280 METHOD(pts_t, set_tpm_version_info, void,
281 private_pts_t *this, chunk_t info)
282 {
283 this->tpm_version_info = chunk_clone(info);
284 print_tpm_version_info(this);
285 }
286
287 /**
288 * Load an AIK certificate or public key,
289 * the certificate having precedence over the public key if both are present
290 */
291 static void load_aik(private_pts_t *this)
292 {
293 char *cert_path, *key_path;
294
295 cert_path = lib->settings->get_str(lib->settings,
296 "libimcv.plugins.imc-attestation.aik_cert", NULL);
297 key_path = lib->settings->get_str(lib->settings,
298 "libimcv.plugins.imc-attestation.aik_key", NULL);
299
300 if (cert_path)
301 {
302 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
303 CERT_X509, BUILD_FROM_FILE,
304 cert_path, BUILD_END);
305 if (this->aik)
306 {
307 DBG2(DBG_PTS, "loaded AIK certificate from '%s'", cert_path);
308 return;
309 }
310 }
311 if (key_path)
312 {
313 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
314 CERT_TRUSTED_PUBKEY, BUILD_FROM_FILE,
315 key_path, BUILD_END);
316 if (this->aik)
317 {
318 DBG2(DBG_PTS, "loaded AIK public key from '%s'", key_path);
319 return;
320 }
321 }
322 DBG1(DBG_PTS, "neither AIK certificate nor public key is available");
323 }
324
325 METHOD(pts_t, get_aik, certificate_t*,
326 private_pts_t *this)
327 {
328 return this->aik;
329 }
330
331 METHOD(pts_t, set_aik, void,
332 private_pts_t *this, certificate_t *aik)
333 {
334 DESTROY_IF(this->aik);
335 this->aik = aik->get_ref(aik);
336 }
337
338 METHOD(pts_t, hash_file, bool,
339 private_pts_t *this, hasher_t *hasher, char *pathname, u_char *hash)
340 {
341 u_char buffer[PTS_BUF_SIZE];
342 FILE *file;
343 int bytes_read;
344
345 file = fopen(pathname, "rb");
346 if (!file)
347 {
348 DBG1(DBG_PTS," file '%s' can not be opened, %s", pathname,
349 strerror(errno));
350 return FALSE;
351 }
352 while (TRUE)
353 {
354 bytes_read = fread(buffer, 1, sizeof(buffer), file);
355 if (bytes_read > 0)
356 {
357 hasher->get_hash(hasher, chunk_create(buffer, bytes_read), NULL);
358 }
359 else
360 {
361 hasher->get_hash(hasher, chunk_empty, hash);
362 break;
363 }
364 }
365 fclose(file);
366
367 return TRUE;
368 }
369
370 /**
371 * Get the relative filename of a fully qualified file pathname
372 */
373 static char* get_filename(char *pathname)
374 {
375 char *pos, *filename;
376
377 pos = filename = pathname;
378 while (pos && *(++pos) != '\0')
379 {
380 filename = pos;
381 pos = strchr(filename, '/');
382 }
383 return filename;
384 }
385
386 METHOD(pts_t, is_path_valid, bool, private_pts_t *this, char *path,
387 pts_error_code_t *error_code)
388 {
389 struct stat st;
390
391 *error_code = 0;
392
393 if (!stat(path, &st))
394 {
395 return TRUE;
396 }
397 else if (errno == ENOENT || errno == ENOTDIR)
398 {
399 DBG1(DBG_PTS, "file/directory does not exist %s", path);
400 *error_code = TCG_PTS_FILE_NOT_FOUND;
401 }
402 else if (errno == EFAULT)
403 {
404 DBG1(DBG_PTS, "bad address %s", path);
405 *error_code = TCG_PTS_INVALID_PATH;
406 }
407 else
408 {
409 DBG1(DBG_PTS, "error: %s occured while validating path: %s",
410 strerror(errno), path);
411 return FALSE;
412 }
413
414 return TRUE;
415 }
416
417 METHOD(pts_t, do_measurements, pts_file_meas_t*,
418 private_pts_t *this, u_int16_t request_id, char *pathname, bool is_directory)
419 {
420 hasher_t *hasher;
421 hash_algorithm_t hash_alg;
422 u_char hash[HASH_SIZE_SHA384];
423 chunk_t measurement;
424 pts_file_meas_t *measurements;
425
426 /* Create a hasher */
427 hash_alg = pts_meas_to_hash_algorithm(this->algorithm);
428 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
429 if (!hasher)
430 {
431 DBG1(DBG_PTS, " hasher %N not available", hash_algorithm_names, hash_alg);
432 return NULL;
433 }
434
435 /* Create a measurement object */
436 measurements = pts_file_meas_create(request_id);
437
438 /* Link the hash to the measurement and set the measurement length */
439 measurement = chunk_create(hash, hasher->get_hash_size(hasher));
440
441 if (is_directory)
442 {
443 enumerator_t *enumerator;
444 char *rel_name, *abs_name;
445 struct stat st;
446
447 enumerator = enumerator_create_directory(pathname);
448 if (!enumerator)
449 {
450 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
451 strerror(errno));
452 hasher->destroy(hasher);
453 measurements->destroy(measurements);
454 return NULL;
455 }
456 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
457 {
458 /* measure regular files only */
459 if (S_ISREG(st.st_mode) && *rel_name != '.')
460 {
461 if (!hash_file(this, hasher, abs_name, hash))
462 {
463 enumerator->destroy(enumerator);
464 hasher->destroy(hasher);
465 measurements->destroy(measurements);
466 return NULL;
467 }
468 DBG2(DBG_PTS, " %#B for '%s'", &measurement, rel_name);
469 measurements->add(measurements, rel_name, measurement);
470 }
471 }
472 enumerator->destroy(enumerator);
473 }
474 else
475 {
476 char *filename;
477
478 if (!hash_file(this, hasher, pathname, hash))
479 {
480 hasher->destroy(hasher);
481 measurements->destroy(measurements);
482 return NULL;
483 }
484 filename = get_filename(pathname);
485 DBG2(DBG_PTS, " %#B for '%s'", &measurement, filename);
486 measurements->add(measurements, filename, measurement);
487 }
488 hasher->destroy(hasher);
489
490 return measurements;
491 }
492
493 /**
494 * Obtain statistical information describing a file
495 */
496 static bool file_metadata(char *pathname, pts_file_metadata_t **entry)
497 {
498 struct stat st;
499 pts_file_metadata_t *tmp;
500
501 tmp = malloc_thing(pts_file_metadata_t);
502
503 if (stat(pathname, &st))
504 {
505 DBG1(DBG_PTS, "Unable to obtain statistical information about %s", pathname);
506 return FALSE;
507 }
508
509 tmp->filename = strdup(pathname);
510 tmp->meta_length = PTS_FILE_METADATA_SIZE + strlen(tmp->filename);
511
512 if (S_ISREG(st.st_mode))
513 {
514 tmp->type = PTS_FILE_REGULAR;
515 }
516 else if (S_ISDIR(st.st_mode))
517 {
518 tmp->type = PTS_FILE_DIRECTORY;
519 }
520 else if (S_ISCHR(st.st_mode))
521 {
522 tmp->type = PTS_FILE_CHAR_SPEC;
523 }
524 else if (S_ISBLK(st.st_mode))
525 {
526 tmp->type = PTS_FILE_BLOCK_SPEC;
527 }
528 else if (S_ISFIFO(st.st_mode))
529 {
530 tmp->type = PTS_FILE_FIFO;
531 }
532 else if (S_ISLNK(st.st_mode))
533 {
534 tmp->type = PTS_FILE_SYM_LINK;
535 }
536 else if (S_ISSOCK(st.st_mode))
537 {
538 tmp->type = PTS_FILE_SOCKET;
539 }
540 else
541 {
542 tmp->type = PTS_FILE_OTHER;
543 }
544
545 tmp->filesize = (u_int64_t)st.st_size;
546 tmp->create_time = st.st_ctime;
547 tmp->last_modify_time = st.st_mtime;
548 tmp->last_access_time = st.st_atime;
549 tmp->owner_id = (u_int64_t)st.st_uid;
550 tmp->group_id = (u_int64_t)st.st_gid;
551
552 *entry = tmp;
553
554 return TRUE;
555 }
556
557 METHOD(pts_t, get_metadata, pts_file_meta_t*,
558 private_pts_t *this, char *pathname, bool is_directory)
559 {
560 pts_file_meta_t *metadata;
561 pts_file_metadata_t *entry;
562
563 /* Create a metadata object */
564 metadata = pts_file_meta_create();
565
566 if (is_directory)
567 {
568 enumerator_t *enumerator;
569 char *rel_name, *abs_name;
570 struct stat st;
571
572 enumerator = enumerator_create_directory(pathname);
573 if (!enumerator)
574 {
575 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
576 strerror(errno));
577 metadata->destroy(metadata);
578 return NULL;
579 }
580 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
581 {
582 /* measure regular files only */
583 if (S_ISREG(st.st_mode) && *rel_name != '.')
584 {
585 if (!file_metadata(abs_name, &entry))
586 {
587 enumerator->destroy(enumerator);
588 metadata->destroy(metadata);
589 return NULL;
590 }
591 DBG3(DBG_PTS, "File name: %s", entry->filename);
592 DBG3(DBG_PTS, " type: %d", entry->type);
593 DBG3(DBG_PTS, " size: %d", entry->filesize);
594 DBG3(DBG_PTS, " create time: %s", ctime(&entry->create_time));
595 DBG3(DBG_PTS, " last modified: %s", ctime(&entry->last_modify_time));
596 DBG3(DBG_PTS, " last accessed: %s", ctime(&entry->last_access_time));
597 DBG3(DBG_PTS, " owner id: %d", entry->owner_id);
598 DBG3(DBG_PTS, " group id: %d", entry->group_id);
599
600 metadata->add(metadata, entry);
601 }
602 }
603 enumerator->destroy(enumerator);
604 }
605 else
606 {
607 char *filename;
608
609 if (!file_metadata(pathname, &entry))
610 {
611 metadata->destroy(metadata);
612 return NULL;
613 }
614 filename = get_filename(pathname);
615 DBG3(DBG_PTS, "File name: %s", entry->filename);
616 DBG3(DBG_PTS, " type: %d", entry->type);
617 DBG3(DBG_PTS, " size: %d", entry->filesize);
618 DBG3(DBG_PTS, " create time: %s", ctime(&entry->create_time));
619 DBG3(DBG_PTS, " last modified: %s", ctime(&entry->last_modify_time));
620 DBG3(DBG_PTS, " last accessed: %s", ctime(&entry->last_access_time));
621 DBG3(DBG_PTS, " owner id: %d", entry->owner_id);
622 DBG3(DBG_PTS, " group id: %d", entry->group_id);
623 metadata->add(metadata, entry);
624 }
625
626 return metadata;
627 }
628
629 METHOD(pts_t, read_pcr, bool,
630 private_pts_t *this, u_int32_t pcr_num, chunk_t *output)
631 {
632 TSS_HCONTEXT hContext;
633 TSS_HTPM hTPM;
634 TSS_RESULT result;
635 u_int32_t pcr_length;
636 chunk_t pcr_value;
637
638 result = Tspi_Context_Create(&hContext);
639 if (result != TSS_SUCCESS)
640 {
641 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
642 return FALSE;
643 }
644
645 result = Tspi_Context_Connect(hContext, NULL);
646 if (result != TSS_SUCCESS)
647 {
648 goto err;
649 }
650 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
651 if (result != TSS_SUCCESS)
652 {
653 goto err;
654 }
655 result = Tspi_TPM_PcrRead(hTPM, pcr_num, &pcr_length, &pcr_value.ptr);
656 if (result != TSS_SUCCESS)
657 {
658 goto err;
659 }
660
661 *output = pcr_value;
662 *output = chunk_clone(*output);
663
664 Tspi_Context_Close(hContext);
665 DBG3(DBG_PTS, "PCR %d value:%B", pcr_num, output);
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 METHOD(pts_t, extend_pcr, bool,
675 private_pts_t *this, u_int32_t pcr_num, chunk_t input, chunk_t *output)
676 {
677 TSS_HCONTEXT hContext;
678 TSS_HTPM hTPM;
679 TSS_RESULT result;
680 u_int32_t pcr_length;
681 chunk_t pcr_value;
682
683 result = Tspi_Context_Create(&hContext);
684 if (result != TSS_SUCCESS)
685 {
686 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
687 return FALSE;
688 }
689 result = Tspi_Context_Connect(hContext, NULL);
690 if (result != TSS_SUCCESS)
691 {
692 goto err;
693 }
694 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
695 if (result != TSS_SUCCESS)
696 {
697 goto err;
698 }
699 result = Tspi_TPM_PcrExtend(hTPM, pcr_num, 20, input.ptr, NULL, &pcr_length, &pcr_value.ptr);
700 if (result != TSS_SUCCESS)
701 {
702 goto err;
703 }
704
705 *output = pcr_value;
706 *output = chunk_clone(*output);
707
708 Tspi_Context_Close(hContext);
709 DBG3(DBG_PTS, "PCR %d extended with: %B", pcr_num, &input);
710 DBG3(DBG_PTS, "PCR %d value after extend: %B", pcr_num, output);
711
712 return TRUE;
713
714 err:
715 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
716 Tspi_Context_Close(hContext);
717 return FALSE;
718 }
719
720 METHOD(pts_t, quote_tpm, bool,
721 private_pts_t *this, u_int32_t pcr_num, chunk_t *output)
722 {
723 TSS_HCONTEXT hContext;
724 TSS_HTPM hTPM;
725 TSS_HKEY hIdentKey;
726 TSS_HPCRS hPcrComposite;
727 TSS_RESULT result;
728
729 result = Tspi_Context_Create(&hContext);
730 if (result != TSS_SUCCESS)
731 {
732 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
733 return FALSE;
734 }
735 result = Tspi_Context_Connect(hContext, NULL);
736 if (result != TSS_SUCCESS)
737 {
738 goto err;
739 }
740 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
741 if (result != TSS_SUCCESS)
742 {
743 goto err;
744 }
745
746 Tspi_Context_Close(hContext);
747 return TRUE;
748
749 err:
750 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
751 Tspi_Context_Close(hContext);
752 return FALSE;
753 }
754
755 METHOD(pts_t, destroy, void,
756 private_pts_t *this)
757 {
758 DESTROY_IF(this->aik);
759 DESTROY_IF(this->dh);
760 free(this->platform_info);
761 free(this->tpm_version_info.ptr);
762 free(this);
763 }
764
765 /**
766 * Determine Linux distribution and hardware platform
767 */
768 static char* extract_platform_info(void)
769 {
770 FILE *file;
771 char buf[BUF_LEN], *pos, *value = NULL;
772 int i, len;
773 struct utsname uninfo;
774
775 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
776 const char* releases[] = {
777 "/etc/lsb-release", "/etc/debian_version",
778 "/etc/SuSE-release", "/etc/novell-release",
779 "/etc/sles-release", "/etc/redhat-release",
780 "/etc/fedora-release", "/etc/gentoo-release",
781 "/etc/slackware-version", "/etc/annvix-release",
782 "/etc/arch-release", "/etc/arklinux-release",
783 "/etc/aurox-release", "/etc/blackcat-release",
784 "/etc/cobalt-release", "/etc/conectiva-release",
785 "/etc/debian_release", "/etc/immunix-release",
786 "/etc/lfs-release", "/etc/linuxppc-release",
787 "/etc/mandrake-release", "/etc/mandriva-release",
788 "/etc/mandrakelinux-release", "/etc/mklinux-release",
789 "/etc/pld-release", "/etc/redhat_version",
790 "/etc/slackware-release", "/etc/e-smith-release",
791 "/etc/release", "/etc/sun-release",
792 "/etc/tinysofa-release", "/etc/turbolinux-release",
793 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
794 "/etc/va-release", "/etc/yellowdog-release"
795 };
796
797 const char description[] = "DISTRIB_DESCRIPTION=\"";
798
799 for (i = 0; i < countof(releases); i++)
800 {
801 file = fopen(releases[i], "r");
802 if (!file)
803 {
804 continue;
805 }
806 fseek(file, 0, SEEK_END);
807 len = min(ftell(file), sizeof(buf)-1);
808 rewind(file);
809 buf[len] = '\0';
810 if (fread(buf, 1, len, file) != len)
811 {
812 DBG1(DBG_PTS, "failed to read file '%s'", releases[i]);
813 fclose(file);
814 return NULL;
815 }
816 fclose(file);
817
818 if (i == 0) /* LSB release */
819 {
820 pos = strstr(buf, description);
821 if (!pos)
822 {
823 DBG1(DBG_PTS, "failed to find begin of lsb-release "
824 "DESCRIPTION field");
825 return NULL;
826 }
827 value = pos + strlen(description);
828 pos = strchr(value, '"');
829 if (!pos)
830 {
831 DBG1(DBG_PTS, "failed to find end of lsb-release "
832 "DESCRIPTION field");
833 return NULL;
834 }
835 }
836 else
837 {
838 value = buf;
839 pos = strchr(value, '\n');
840 if (!pos)
841 {
842 DBG1(DBG_PTS, "failed to find end of release string");
843 return NULL;
844 }
845 }
846 break;
847 }
848
849 if (!value)
850 {
851 DBG1(DBG_PTS, "no distribution release file found");
852 return NULL;
853 }
854
855 if (uname(&uninfo) < 0)
856 {
857 DBG1(DBG_PTS, "could not retrieve machine architecture");
858 return NULL;
859 }
860
861 *pos++ = ' ';
862 len = sizeof(buf)-1 + (pos - buf);
863 strncpy(pos, uninfo.machine, len);
864
865 DBG1(DBG_PTS, "platform is '%s'", value);
866 return strdup(value);
867 }
868
869 /**
870 * Check for a TPM by querying for TPM Version Info
871 */
872 static bool has_tpm(private_pts_t *this)
873 {
874 TSS_HCONTEXT hContext;
875 TSS_HTPM hTPM;
876 TSS_RESULT result;
877 u_int32_t version_info_len;
878
879 result = Tspi_Context_Create(&hContext);
880 if (result != TSS_SUCCESS)
881 {
882 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
883 return FALSE;
884 }
885 result = Tspi_Context_Connect(hContext, NULL);
886 if (result != TSS_SUCCESS)
887 {
888 goto err;
889 }
890 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
891 if (result != TSS_SUCCESS)
892 {
893 goto err;
894 }
895 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
896 &version_info_len,
897 &this->tpm_version_info.ptr);
898 this->tpm_version_info.len = version_info_len;
899 if (result != TSS_SUCCESS)
900 {
901 goto err;
902 }
903 this->tpm_version_info = chunk_clone(this->tpm_version_info);
904 Tspi_Context_Close(hContext);
905 return TRUE;
906
907 err:
908 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
909 Tspi_Context_Close(hContext);
910 return FALSE;
911 }
912
913 /**
914 * See header
915 */
916 pts_t *pts_create(bool is_imc)
917 {
918 private_pts_t *this;
919
920 INIT(this,
921 .public = {
922 .get_proto_caps = _get_proto_caps,
923 .set_proto_caps = _set_proto_caps,
924 .get_meas_algorithm = _get_meas_algorithm,
925 .set_meas_algorithm = _set_meas_algorithm,
926 .get_dh_group = _get_dh_group,
927 .set_dh_group = _set_dh_group,
928 .create_dh = _create_dh,
929 .get_my_pub_val = _get_my_pub_val,
930 .set_other_pub_val = _set_other_pub_val,
931 .calculate_secret = _calculate_secret,
932 .get_secret = _get_secret,
933 .get_platform_info = _get_platform_info,
934 .set_platform_info = _set_platform_info,
935 .get_tpm_version_info = _get_tpm_version_info,
936 .set_tpm_version_info = _set_tpm_version_info,
937 .get_aik = _get_aik,
938 .set_aik = _set_aik,
939 .is_path_valid = _is_path_valid,
940 .hash_file = _hash_file,
941 .do_measurements = _do_measurements,
942 .get_metadata = _get_metadata,
943 .read_pcr = _read_pcr,
944 .extend_pcr = _extend_pcr,
945 .quote_tpm = _quote_tpm,
946 .destroy = _destroy,
947 },
948 .proto_caps = PTS_PROTO_CAPS_V,
949 .algorithm = PTS_MEAS_ALGO_SHA256,
950 .dh_group = PTS_DH_GROUP_IKE19,
951 );
952
953 if (is_imc)
954 {
955 this->platform_info = extract_platform_info();
956
957 if (has_tpm(this))
958 {
959 this->has_tpm = TRUE;
960 this->proto_caps |= PTS_PROTO_CAPS_T;
961 load_aik(this);
962 }
963 }
964 else
965 {
966 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_C;
967 }
968
969 return &this->public;
970 }
971