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