af8c1d05f0359ea722b20de8d71254fe26a1653d
[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 /**
339 * Compute a hash over a file
340 */
341 static bool hash_file(hasher_t *hasher, char *pathname, u_char *hash)
342 {
343 u_char buffer[PTS_BUF_SIZE];
344 FILE *file;
345 int bytes_read;
346
347 file = fopen(pathname, "rb");
348 if (!file)
349 {
350 DBG1(DBG_PTS," file '%s' can not be opened, %s", pathname,
351 strerror(errno));
352 return FALSE;
353 }
354 while (TRUE)
355 {
356 bytes_read = fread(buffer, 1, sizeof(buffer), file);
357 if (bytes_read > 0)
358 {
359 hasher->get_hash(hasher, chunk_create(buffer, bytes_read), NULL);
360 }
361 else
362 {
363 hasher->get_hash(hasher, chunk_empty, hash);
364 break;
365 }
366 }
367 fclose(file);
368
369 return TRUE;
370 }
371
372 /**
373 * Get the relative filename of a fully qualified file pathname
374 */
375 static char* get_filename(char *pathname)
376 {
377 char *pos, *filename;
378
379 pos = filename = pathname;
380 while (pos && *(++pos) != '\0')
381 {
382 filename = pos;
383 pos = strchr(filename, '/');
384 }
385 return filename;
386 }
387
388 METHOD(pts_t, is_path_valid, bool, private_pts_t *this, char *path,
389 pts_error_code_t *error_code)
390 {
391 struct stat st;
392
393 *error_code = 0;
394
395 if (!stat(path, &st))
396 {
397 return TRUE;
398 }
399 else if (errno == ENOENT || errno == ENOTDIR)
400 {
401 DBG1(DBG_PTS, "file/directory does not exist %s", path);
402 *error_code = TCG_PTS_FILE_NOT_FOUND;
403 }
404 else if (errno == EFAULT)
405 {
406 DBG1(DBG_PTS, "bad address %s", path);
407 *error_code = TCG_PTS_INVALID_PATH;
408 }
409 else
410 {
411 DBG1(DBG_PTS, "error: %s occured while validating path: %s",
412 strerror(errno), path);
413 return FALSE;
414 }
415
416 return TRUE;
417 }
418
419 METHOD(pts_t, do_measurements, pts_file_meas_t*,
420 private_pts_t *this, u_int16_t request_id, char *pathname, bool is_directory)
421 {
422 hasher_t *hasher;
423 hash_algorithm_t hash_alg;
424 u_char hash[HASH_SIZE_SHA384];
425 chunk_t measurement;
426 pts_file_meas_t *measurements;
427
428 /* Create a hasher */
429 hash_alg = pts_meas_to_hash_algorithm(this->algorithm);
430 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
431 if (!hasher)
432 {
433 DBG1(DBG_PTS, " hasher %N not available", hash_algorithm_names, hash_alg);
434 return NULL;
435 }
436
437 /* Create a measurement object */
438 measurements = pts_file_meas_create(request_id);
439
440 /* Link the hash to the measurement and set the measurement length */
441 measurement = chunk_create(hash, hasher->get_hash_size(hasher));
442
443 if (is_directory)
444 {
445 enumerator_t *enumerator;
446 char *rel_name, *abs_name;
447 struct stat st;
448
449 enumerator = enumerator_create_directory(pathname);
450 if (!enumerator)
451 {
452 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
453 strerror(errno));
454 hasher->destroy(hasher);
455 measurements->destroy(measurements);
456 return NULL;
457 }
458 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
459 {
460 /* measure regular files only */
461 if (S_ISREG(st.st_mode) && *rel_name != '.')
462 {
463 if (!hash_file(hasher, abs_name, hash))
464 {
465 enumerator->destroy(enumerator);
466 hasher->destroy(hasher);
467 measurements->destroy(measurements);
468 return NULL;
469 }
470 DBG2(DBG_PTS, " %#B for '%s'", &measurement, rel_name);
471 measurements->add(measurements, rel_name, measurement);
472 }
473 }
474 enumerator->destroy(enumerator);
475 }
476 else
477 {
478 char *filename;
479
480 if (!hash_file(hasher, pathname, hash))
481 {
482 hasher->destroy(hasher);
483 measurements->destroy(measurements);
484 return NULL;
485 }
486 filename = get_filename(pathname);
487 DBG2(DBG_PTS, " %#B for '%s'", &measurement, filename);
488 measurements->add(measurements, filename, measurement);
489 }
490 hasher->destroy(hasher);
491
492 return measurements;
493 }
494
495 /**
496 * Obtain statistical information describing a file
497 */
498 static bool file_metadata(char *pathname, pts_file_metadata_t **entry)
499 {
500 struct stat st;
501 pts_file_metadata_t *tmp;
502
503 tmp = malloc_thing(pts_file_metadata_t);
504
505 if (stat(pathname, &st))
506 {
507 DBG1(DBG_PTS, "Unable to obtain statistical information about %s", pathname);
508 return FALSE;
509 }
510
511 tmp->filename = strdup(pathname);
512 tmp->meta_length = PTS_FILE_METADATA_SIZE + strlen(tmp->filename);
513
514 if (S_ISREG(st.st_mode))
515 {
516 tmp->type = PTS_FILE_REGULAR;
517 }
518 else if (S_ISDIR(st.st_mode))
519 {
520 tmp->type = PTS_FILE_DIRECTORY;
521 }
522 else if (S_ISCHR(st.st_mode))
523 {
524 tmp->type = PTS_FILE_CHAR_SPEC;
525 }
526 else if (S_ISBLK(st.st_mode))
527 {
528 tmp->type = PTS_FILE_BLOCK_SPEC;
529 }
530 else if (S_ISFIFO(st.st_mode))
531 {
532 tmp->type = PTS_FILE_FIFO;
533 }
534 else if (S_ISLNK(st.st_mode))
535 {
536 tmp->type = PTS_FILE_SYM_LINK;
537 }
538 else if (S_ISSOCK(st.st_mode))
539 {
540 tmp->type = PTS_FILE_SOCKET;
541 }
542 else
543 {
544 tmp->type = PTS_FILE_OTHER;
545 }
546
547 tmp->filesize = (u_int64_t)st.st_size;
548 tmp->create_time = st.st_ctime;
549 tmp->last_modify_time = st.st_mtime;
550 tmp->last_access_time = st.st_atime;
551 tmp->owner_id = (u_int64_t)st.st_uid;
552 tmp->group_id = (u_int64_t)st.st_gid;
553
554 *entry = tmp;
555
556 return TRUE;
557 }
558
559 METHOD(pts_t, get_metadata, pts_file_meta_t*,
560 private_pts_t *this, char *pathname, bool is_directory)
561 {
562 pts_file_meta_t *metadata;
563 pts_file_metadata_t *entry;
564
565 /* Create a metadata object */
566 metadata = pts_file_meta_create();
567
568 if (is_directory)
569 {
570 enumerator_t *enumerator;
571 char *rel_name, *abs_name;
572 struct stat st;
573
574 enumerator = enumerator_create_directory(pathname);
575 if (!enumerator)
576 {
577 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
578 strerror(errno));
579 metadata->destroy(metadata);
580 return NULL;
581 }
582 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
583 {
584 /* measure regular files only */
585 if (S_ISREG(st.st_mode) && *rel_name != '.')
586 {
587 if (!file_metadata(abs_name, &entry))
588 {
589 enumerator->destroy(enumerator);
590 metadata->destroy(metadata);
591 return NULL;
592 }
593 DBG3(DBG_PTS, "File name: %s", entry->filename);
594 DBG3(DBG_PTS, " type: %d", entry->type);
595 DBG3(DBG_PTS, " size: %d", entry->filesize);
596 DBG3(DBG_PTS, " create time: %s", ctime(&entry->create_time));
597 DBG3(DBG_PTS, " last modified: %s", ctime(&entry->last_modify_time));
598 DBG3(DBG_PTS, " last accessed: %s", ctime(&entry->last_access_time));
599 DBG3(DBG_PTS, " owner id: %d", entry->owner_id);
600 DBG3(DBG_PTS, " group id: %d", entry->group_id);
601
602 metadata->add(metadata, entry);
603 }
604 }
605 enumerator->destroy(enumerator);
606 }
607 else
608 {
609 char *filename;
610
611 if (!file_metadata(pathname, &entry))
612 {
613 metadata->destroy(metadata);
614 return NULL;
615 }
616 filename = get_filename(pathname);
617 DBG3(DBG_PTS, "File name: %s", entry->filename);
618 DBG3(DBG_PTS, " type: %d", entry->type);
619 DBG3(DBG_PTS, " size: %d", entry->filesize);
620 DBG3(DBG_PTS, " create time: %s", ctime(&entry->create_time));
621 DBG3(DBG_PTS, " last modified: %s", ctime(&entry->last_modify_time));
622 DBG3(DBG_PTS, " last accessed: %s", ctime(&entry->last_access_time));
623 DBG3(DBG_PTS, " owner id: %d", entry->owner_id);
624 DBG3(DBG_PTS, " group id: %d", entry->group_id);
625 metadata->add(metadata, entry);
626 }
627
628 return metadata;
629 }
630
631
632 METHOD(pts_t, destroy, void,
633 private_pts_t *this)
634 {
635 DESTROY_IF(this->aik);
636 DESTROY_IF(this->dh);
637 free(this->platform_info);
638 free(this->tpm_version_info.ptr);
639 free(this);
640 }
641
642 /**
643 * Determine Linux distribution and hardware platform
644 */
645 static char* extract_platform_info(void)
646 {
647 FILE *file;
648 char buf[BUF_LEN], *pos, *value = NULL;
649 int i, len;
650 struct utsname uninfo;
651
652 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
653 const char* releases[] = {
654 "/etc/lsb-release", "/etc/debian_version",
655 "/etc/SuSE-release", "/etc/novell-release",
656 "/etc/sles-release", "/etc/redhat-release",
657 "/etc/fedora-release", "/etc/gentoo-release",
658 "/etc/slackware-version", "/etc/annvix-release",
659 "/etc/arch-release", "/etc/arklinux-release",
660 "/etc/aurox-release", "/etc/blackcat-release",
661 "/etc/cobalt-release", "/etc/conectiva-release",
662 "/etc/debian_release", "/etc/immunix-release",
663 "/etc/lfs-release", "/etc/linuxppc-release",
664 "/etc/mandrake-release", "/etc/mandriva-release",
665 "/etc/mandrakelinux-release", "/etc/mklinux-release",
666 "/etc/pld-release", "/etc/redhat_version",
667 "/etc/slackware-release", "/etc/e-smith-release",
668 "/etc/release", "/etc/sun-release",
669 "/etc/tinysofa-release", "/etc/turbolinux-release",
670 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
671 "/etc/va-release", "/etc/yellowdog-release"
672 };
673
674 const char description[] = "DISTRIB_DESCRIPTION=\"";
675
676 for (i = 0; i < countof(releases); i++)
677 {
678 file = fopen(releases[i], "r");
679 if (!file)
680 {
681 continue;
682 }
683 fseek(file, 0, SEEK_END);
684 len = min(ftell(file), sizeof(buf)-1);
685 rewind(file);
686 buf[len] = '\0';
687 if (fread(buf, 1, len, file) != len)
688 {
689 DBG1(DBG_PTS, "failed to read file '%s'", releases[i]);
690 fclose(file);
691 return NULL;
692 }
693 fclose(file);
694
695 if (i == 0) /* LSB release */
696 {
697 pos = strstr(buf, description);
698 if (!pos)
699 {
700 DBG1(DBG_PTS, "failed to find begin of lsb-release "
701 "DESCRIPTION field");
702 return NULL;
703 }
704 value = pos + strlen(description);
705 pos = strchr(value, '"');
706 if (!pos)
707 {
708 DBG1(DBG_PTS, "failed to find end of lsb-release "
709 "DESCRIPTION field");
710 return NULL;
711 }
712 }
713 else
714 {
715 value = buf;
716 pos = strchr(value, '\n');
717 if (!pos)
718 {
719 DBG1(DBG_PTS, "failed to find end of release string");
720 return NULL;
721 }
722 }
723 break;
724 }
725
726 if (!value)
727 {
728 DBG1(DBG_PTS, "no distribution release file found");
729 return NULL;
730 }
731
732 if (uname(&uninfo) < 0)
733 {
734 DBG1(DBG_PTS, "could not retrieve machine architecture");
735 return NULL;
736 }
737
738 *pos++ = ' ';
739 len = sizeof(buf)-1 + (pos - buf);
740 strncpy(pos, uninfo.machine, len);
741
742 DBG1(DBG_PTS, "platform is '%s'", value);
743 return strdup(value);
744 }
745
746 /**
747 * Check for a TPM by querying for TPM Version Info
748 */
749 static bool has_tpm(private_pts_t *this)
750 {
751 TSS_HCONTEXT hContext;
752 TSS_HTPM hTPM;
753 TSS_RESULT result;
754 u_int32_t version_info_len;
755
756 result = Tspi_Context_Create(&hContext);
757 if (result != TSS_SUCCESS)
758 {
759 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
760 return FALSE;
761 }
762 result = Tspi_Context_Connect(hContext, NULL);
763 if (result != TSS_SUCCESS)
764 {
765 goto err;
766 }
767 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
768 if (result != TSS_SUCCESS)
769 {
770 goto err;
771 }
772 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
773 &version_info_len,
774 &this->tpm_version_info.ptr);
775 this->tpm_version_info.len = version_info_len;
776 if (result != TSS_SUCCESS)
777 {
778 goto err;
779 }
780 this->tpm_version_info = chunk_clone(this->tpm_version_info);
781 return TRUE;
782
783 err:
784 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
785 Tspi_Context_Close(hContext);
786 return FALSE;
787 }
788
789 /**
790 * See header
791 */
792 pts_t *pts_create(bool is_imc)
793 {
794 private_pts_t *this;
795
796 INIT(this,
797 .public = {
798 .get_proto_caps = _get_proto_caps,
799 .set_proto_caps = _set_proto_caps,
800 .get_meas_algorithm = _get_meas_algorithm,
801 .set_meas_algorithm = _set_meas_algorithm,
802 .get_dh_group = _get_dh_group,
803 .set_dh_group = _set_dh_group,
804 .create_dh = _create_dh,
805 .get_my_pub_val = _get_my_pub_val,
806 .set_other_pub_val = _set_other_pub_val,
807 .calculate_secret = _calculate_secret,
808 .get_secret = _get_secret,
809 .get_platform_info = _get_platform_info,
810 .set_platform_info = _set_platform_info,
811 .get_tpm_version_info = _get_tpm_version_info,
812 .set_tpm_version_info = _set_tpm_version_info,
813 .get_aik = _get_aik,
814 .set_aik = _set_aik,
815 .is_path_valid = _is_path_valid,
816 .do_measurements = _do_measurements,
817 .get_metadata = _get_metadata,
818 .destroy = _destroy,
819 },
820 .proto_caps = PTS_PROTO_CAPS_V,
821 .algorithm = PTS_MEAS_ALGO_SHA256,
822 .dh_group = PTS_DH_GROUP_IKE19,
823 );
824
825 if (is_imc)
826 {
827 this->platform_info = extract_platform_info();
828
829 if (has_tpm(this))
830 {
831 this->has_tpm = TRUE;
832 this->proto_caps |= PTS_PROTO_CAPS_T;
833 load_aik(this);
834 }
835 }
836 else
837 {
838 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_C;
839 }
840
841 return &this->public;
842 }
843