873678043402bcac34ab59cbc71c1c12357f87b8
[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 #include <bio/bio_writer.h>
21 #include <bio/bio_reader.h>
22
23 #include <trousers/tss.h>
24 #include <trousers/trousers.h>
25
26 #include <sys/stat.h>
27 #include <sys/utsname.h>
28 #include <errno.h>
29
30 #define PTS_BUF_SIZE 4096
31
32 /**
33 * Maximum number of PCR's of TPM, TPM Spec 1.2
34 */
35 #define PCR_MAX_NUM 24
36
37 /**
38 * Number of bytes that can be saved in a PCR of TPM, TPM Spec 1.2
39 */
40 #define PCR_LEN 20
41
42 typedef struct private_pts_t private_pts_t;
43
44 /**
45 * Private data of a pts_t object.
46 *
47 */
48 struct private_pts_t {
49
50 /**
51 * Public pts_t interface.
52 */
53 pts_t public;
54
55 /**
56 * PTS Protocol Capabilities
57 */
58 pts_proto_caps_flag_t proto_caps;
59
60 /**
61 * PTS Measurement Algorithm
62 */
63 pts_meas_algorithms_t algorithm;
64
65 /**
66 * DH Hash Algorithm
67 */
68 pts_meas_algorithms_t dh_hash_algorithm;
69
70 /**
71 * PTS Diffie-Hellman Secret
72 */
73 diffie_hellman_t *dh;
74
75 /**
76 * PTS Diffie-Hellman Initiator Nonce
77 */
78 chunk_t initiator_nonce;
79
80 /**
81 * PTS Diffie-Hellman Responder Nonce
82 */
83 chunk_t responder_nonce;
84
85 /**
86 * Secret assessment value to be used for TPM Quote as an external data
87 */
88 chunk_t secret;
89
90 /**
91 * Platform and OS Info
92 */
93 char *platform_info;
94
95 /**
96 * TRUE if IMC-PTS, FALSE if IMV-PTS
97 */
98 bool is_imc;
99
100 /**
101 * Do we have an activated TPM
102 */
103 bool has_tpm;
104
105 /**
106 * Contains a TPM_CAP_VERSION_INFO struct
107 */
108 chunk_t tpm_version_info;
109
110 /**
111 * Contains TSS Blob structure for AIK
112 */
113 chunk_t aik_blob;
114
115 /**
116 * Contains a Attestation Identity Key or Certificate
117 */
118 certificate_t *aik;
119
120 /**
121 * Table of extended PCRs with corresponding values
122 */
123 u_char* pcrs[PCR_MAX_NUM];
124
125 /**
126 * Length of PCR registers
127 */
128 size_t pcr_len;
129
130 /**
131 * Number of extended PCR registers
132 */
133 u_int32_t pcr_count;
134
135 /**
136 * Highest extended PCR register
137 */
138 u_int32_t pcr_max;
139
140 /**
141 * Bitmap of extended PCR registers
142 */
143 u_int8_t pcr_select[PCR_MAX_NUM / 8];
144
145 };
146
147 METHOD(pts_t, get_proto_caps, pts_proto_caps_flag_t,
148 private_pts_t *this)
149 {
150 return this->proto_caps;
151 }
152
153 METHOD(pts_t, set_proto_caps, void,
154 private_pts_t *this, pts_proto_caps_flag_t flags)
155 {
156 this->proto_caps = flags;
157 DBG2(DBG_PTS, "supported PTS protocol capabilities: %s%s%s%s%s",
158 flags & PTS_PROTO_CAPS_C ? "C" : ".",
159 flags & PTS_PROTO_CAPS_V ? "V" : ".",
160 flags & PTS_PROTO_CAPS_D ? "D" : ".",
161 flags & PTS_PROTO_CAPS_T ? "T" : ".",
162 flags & PTS_PROTO_CAPS_X ? "X" : ".");
163 }
164
165 METHOD(pts_t, get_meas_algorithm, pts_meas_algorithms_t,
166 private_pts_t *this)
167 {
168 return this->algorithm;
169 }
170
171 METHOD(pts_t, set_meas_algorithm, void,
172 private_pts_t *this, pts_meas_algorithms_t algorithm)
173 {
174 hash_algorithm_t hash_alg;
175
176 hash_alg = pts_meas_algo_to_hash(algorithm);
177 DBG2(DBG_PTS, "selected PTS measurement algorithm is %N",
178 hash_algorithm_names, hash_alg);
179 if (hash_alg != HASH_UNKNOWN)
180 {
181 this->algorithm = algorithm;
182 }
183 }
184
185 METHOD(pts_t, get_dh_hash_algorithm, pts_meas_algorithms_t,
186 private_pts_t *this)
187 {
188 return this->dh_hash_algorithm;
189 }
190
191 METHOD(pts_t, set_dh_hash_algorithm, void,
192 private_pts_t *this, pts_meas_algorithms_t algorithm)
193 {
194 hash_algorithm_t hash_alg;
195
196 hash_alg = pts_meas_algo_to_hash(algorithm);
197 DBG2(DBG_PTS, "selected DH hash algorithm is %N",
198 hash_algorithm_names, hash_alg);
199 if (hash_alg != HASH_UNKNOWN)
200 {
201 this->dh_hash_algorithm = algorithm;
202 }
203 }
204
205
206 METHOD(pts_t, create_dh_nonce, bool,
207 private_pts_t *this, pts_dh_group_t group, int nonce_len)
208 {
209 diffie_hellman_group_t dh_group;
210 chunk_t *nonce;
211 rng_t *rng;
212
213 dh_group = pts_dh_group_to_ike(group);
214 DBG2(DBG_PTS, "selected PTS DH group is %N",
215 diffie_hellman_group_names, dh_group);
216 DESTROY_IF(this->dh);
217 this->dh = lib->crypto->create_dh(lib->crypto, dh_group);
218
219 rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
220 if (!rng)
221 {
222 DBG1(DBG_PTS, "no rng available");
223 return FALSE;
224 }
225 DBG2(DBG_PTS, "nonce length is %d", nonce_len);
226 nonce = this->is_imc ? &this->responder_nonce : &this->initiator_nonce;
227 chunk_free(nonce);
228 rng->allocate_bytes(rng, nonce_len, nonce);
229 rng->destroy(rng);
230
231 return TRUE;
232 }
233
234 METHOD(pts_t, get_my_public_value, void,
235 private_pts_t *this, chunk_t *value, chunk_t *nonce)
236 {
237 this->dh->get_my_public_value(this->dh, value);
238 *nonce = this->is_imc ? this->responder_nonce : this->initiator_nonce;
239 }
240
241 METHOD(pts_t, set_peer_public_value, void,
242 private_pts_t *this, chunk_t value, chunk_t nonce)
243 {
244 this->dh->set_other_public_value(this->dh, value);
245
246 nonce = chunk_clone(nonce);
247 if (this->is_imc)
248 {
249 this->initiator_nonce = nonce;
250 }
251 else
252 {
253 this->responder_nonce = nonce;
254 }
255 }
256
257 METHOD(pts_t, calculate_secret, bool,
258 private_pts_t *this)
259 {
260 hasher_t *hasher;
261 hash_algorithm_t hash_alg;
262 chunk_t shared_secret;
263
264 /* Check presence of nonces */
265 if (!this->initiator_nonce.len || !this->responder_nonce.len)
266 {
267 DBG1(DBG_PTS, "initiator and/or responder nonce is not available");
268 return FALSE;
269 }
270 DBG3(DBG_PTS, "initiator nonce: %B", &this->initiator_nonce);
271 DBG3(DBG_PTS, "responder nonce: %B", &this->responder_nonce);
272
273 /* Calculate the DH secret */
274 if (this->dh->get_shared_secret(this->dh, &shared_secret) != SUCCESS)
275 {
276 DBG1(DBG_PTS, "shared DH secret computation failed");
277 return FALSE;
278 }
279 DBG3(DBG_PTS, "shared DH secret: %B", &shared_secret);
280
281 /* Calculate the secret assessment value */
282 hash_alg = pts_meas_algo_to_hash(this->dh_hash_algorithm);
283 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
284
285 hasher->allocate_hash(hasher, chunk_from_chars('1'), NULL);
286 hasher->allocate_hash(hasher, this->initiator_nonce, NULL);
287 hasher->allocate_hash(hasher, this->responder_nonce, NULL);
288 hasher->allocate_hash(hasher, shared_secret, &this->secret);
289 hasher->destroy(hasher);
290
291 /* The DH secret must be destroyed */
292 chunk_clear(&shared_secret);
293
294 /*
295 * Truncate the hash to 20 bytes to fit the ExternalData
296 * argument of the TPM Quote command
297 */
298 this->secret.len = min(this->secret.len, 20);
299 DBG3(DBG_PTS, "secret assessment value: %B", &this->secret);
300 return TRUE;
301 }
302
303 /**
304 * Print TPM 1.2 Version Info
305 */
306 static void print_tpm_version_info(private_pts_t *this)
307 {
308 TPM_CAP_VERSION_INFO versionInfo;
309 UINT64 offset = 0;
310 TSS_RESULT result;
311
312 result = Trspi_UnloadBlob_CAP_VERSION_INFO(&offset,
313 this->tpm_version_info.ptr, &versionInfo);
314 if (result != TSS_SUCCESS)
315 {
316 DBG1(DBG_PTS, "could not parse tpm version info: tss error 0x%x",
317 result);
318 }
319 else
320 {
321 DBG2(DBG_PTS, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
322 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
323 versionInfo.version.major, versionInfo.version.minor,
324 versionInfo.version.revMajor, versionInfo.version.revMinor,
325 versionInfo.specLevel, versionInfo.errataRev,
326 versionInfo.tpmVendorID);
327 }
328 }
329
330 METHOD(pts_t, get_platform_info, char*,
331 private_pts_t *this)
332 {
333 return this->platform_info;
334 }
335
336 METHOD(pts_t, set_platform_info, void,
337 private_pts_t *this, char *info)
338 {
339 free(this->platform_info);
340 this->platform_info = strdup(info);
341 }
342
343 METHOD(pts_t, get_tpm_version_info, bool,
344 private_pts_t *this, chunk_t *info)
345 {
346 if (!this->has_tpm)
347 {
348 return FALSE;
349 }
350 *info = this->tpm_version_info;
351 print_tpm_version_info(this);
352 return TRUE;
353 }
354
355 METHOD(pts_t, set_tpm_version_info, void,
356 private_pts_t *this, chunk_t info)
357 {
358 this->tpm_version_info = chunk_clone(info);
359 print_tpm_version_info(this);
360 }
361
362 METHOD(pts_t, get_pcr_len, size_t,
363 private_pts_t *this)
364 {
365 return this->pcr_len;
366 }
367
368 /**
369 * Load an AIK Blob (TSS_TSPATTRIB_KEYBLOB_BLOB attribute)
370 */
371 static void load_aik_blob(private_pts_t *this)
372 {
373 char *blob_path;
374 FILE *fp;
375 u_int32_t aikBlobLen;
376
377 blob_path = lib->settings->get_str(lib->settings,
378 "libimcv.plugins.imc-attestation.aik_blob", NULL);
379
380 if (blob_path)
381 {
382 /* Read aik key blob from a file */
383 if ((fp = fopen(blob_path, "r")) == NULL)
384 {
385 DBG1(DBG_PTS, "unable to open AIK Blob file: %s", blob_path);
386 return;
387 }
388
389 fseek(fp, 0, SEEK_END);
390 aikBlobLen = ftell(fp);
391 fseek(fp, 0L, SEEK_SET);
392
393 this->aik_blob = chunk_alloc(aikBlobLen);
394 if (fread(this->aik_blob.ptr, 1, aikBlobLen, fp))
395 {
396 DBG2(DBG_PTS, "loaded AIK Blob from '%s'", blob_path);
397 DBG3(DBG_PTS, "AIK Blob: %B", &this->aik_blob);
398 }
399 else
400 {
401 DBG1(DBG_PTS, "unable to read AIK Blob file '%s'", blob_path);
402 }
403 fclose(fp);
404 return;
405 }
406
407 DBG1(DBG_PTS, "AIK Blob is not available");
408 }
409
410 /**
411 * Load an AIK certificate or public key
412 * the certificate having precedence over the public key if both are present
413 */
414 static void load_aik(private_pts_t *this)
415 {
416 char *cert_path, *key_path;
417
418 cert_path = lib->settings->get_str(lib->settings,
419 "libimcv.plugins.imc-attestation.aik_cert", NULL);
420 key_path = lib->settings->get_str(lib->settings,
421 "libimcv.plugins.imc-attestation.aik_key", NULL);
422
423 if (cert_path)
424 {
425 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
426 CERT_X509, BUILD_FROM_FILE,
427 cert_path, BUILD_END);
428 if (this->aik)
429 {
430 DBG2(DBG_PTS, "loaded AIK certificate from '%s'", cert_path);
431 return;
432 }
433 }
434 if (key_path)
435 {
436 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
437 CERT_TRUSTED_PUBKEY, BUILD_FROM_FILE,
438 key_path, BUILD_END);
439 if (this->aik)
440 {
441 DBG2(DBG_PTS, "loaded AIK public key from '%s'", key_path);
442 return;
443 }
444 }
445
446 DBG1(DBG_PTS, "neither AIK certificate nor public key is available");
447 }
448
449 METHOD(pts_t, get_aik, certificate_t*,
450 private_pts_t *this)
451 {
452 return this->aik;
453 }
454
455 METHOD(pts_t, set_aik, void,
456 private_pts_t *this, certificate_t *aik)
457 {
458 DESTROY_IF(this->aik);
459 this->aik = aik->get_ref(aik);
460 }
461
462 METHOD(pts_t, hash_file, bool,
463 private_pts_t *this, hasher_t *hasher, char *pathname, u_char *hash)
464 {
465 u_char buffer[PTS_BUF_SIZE];
466 FILE *file;
467 int bytes_read;
468
469 file = fopen(pathname, "rb");
470 if (!file)
471 {
472 DBG1(DBG_PTS," file '%s' can not be opened, %s", pathname,
473 strerror(errno));
474 return FALSE;
475 }
476 while (TRUE)
477 {
478 bytes_read = fread(buffer, 1, sizeof(buffer), file);
479 if (bytes_read > 0)
480 {
481 hasher->get_hash(hasher, chunk_create(buffer, bytes_read), NULL);
482 }
483 else
484 {
485 hasher->get_hash(hasher, chunk_empty, hash);
486 break;
487 }
488 }
489 fclose(file);
490
491 return TRUE;
492 }
493
494 /**
495 * Get the relative filename of a fully qualified file pathname
496 */
497 static char* get_filename(char *pathname)
498 {
499 char *pos, *filename;
500
501 pos = filename = pathname;
502 while (pos && *(++pos) != '\0')
503 {
504 filename = pos;
505 pos = strchr(filename, '/');
506 }
507 return filename;
508 }
509
510 METHOD(pts_t, is_path_valid, bool,
511 private_pts_t *this, char *path, pts_error_code_t *error_code)
512 {
513 struct stat st;
514
515 *error_code = 0;
516
517 if (!stat(path, &st))
518 {
519 return TRUE;
520 }
521 else if (errno == ENOENT || errno == ENOTDIR)
522 {
523 DBG1(DBG_PTS, "file/directory does not exist %s", path);
524 *error_code = TCG_PTS_FILE_NOT_FOUND;
525 }
526 else if (errno == EFAULT)
527 {
528 DBG1(DBG_PTS, "bad address %s", path);
529 *error_code = TCG_PTS_INVALID_PATH;
530 }
531 else
532 {
533 DBG1(DBG_PTS, "error: %s occured while validating path: %s",
534 strerror(errno), path);
535 return FALSE;
536 }
537
538 return TRUE;
539 }
540
541 METHOD(pts_t, do_measurements, pts_file_meas_t*,
542 private_pts_t *this, u_int16_t request_id, char *pathname, bool is_directory)
543 {
544 hasher_t *hasher;
545 hash_algorithm_t hash_alg;
546 u_char hash[HASH_SIZE_SHA384];
547 chunk_t measurement;
548 pts_file_meas_t *measurements;
549
550 /* Create a hasher */
551 hash_alg = pts_meas_algo_to_hash(this->algorithm);
552 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
553 if (!hasher)
554 {
555 DBG1(DBG_PTS, "hasher %N not available", hash_algorithm_names, hash_alg);
556 return NULL;
557 }
558
559 /* Create a measurement object */
560 measurements = pts_file_meas_create(request_id);
561
562 /* Link the hash to the measurement and set the measurement length */
563 measurement = chunk_create(hash, hasher->get_hash_size(hasher));
564
565 if (is_directory)
566 {
567 enumerator_t *enumerator;
568 char *rel_name, *abs_name;
569 struct stat st;
570
571 enumerator = enumerator_create_directory(pathname);
572 if (!enumerator)
573 {
574 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
575 strerror(errno));
576 hasher->destroy(hasher);
577 measurements->destroy(measurements);
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 (!hash_file(this, hasher, abs_name, hash))
586 {
587 enumerator->destroy(enumerator);
588 hasher->destroy(hasher);
589 measurements->destroy(measurements);
590 return NULL;
591 }
592 DBG2(DBG_PTS, " %#B for '%s'", &measurement, rel_name);
593 measurements->add(measurements, rel_name, measurement);
594 }
595 }
596 enumerator->destroy(enumerator);
597 }
598 else
599 {
600 char *filename;
601
602 if (!hash_file(this, hasher, pathname, hash))
603 {
604 hasher->destroy(hasher);
605 measurements->destroy(measurements);
606 return NULL;
607 }
608 filename = get_filename(pathname);
609 DBG2(DBG_PTS, " %#B for '%s'", &measurement, filename);
610 measurements->add(measurements, filename, measurement);
611 }
612 hasher->destroy(hasher);
613
614 return measurements;
615 }
616
617 /**
618 * Obtain statistical information describing a file
619 */
620 static bool file_metadata(char *pathname, pts_file_metadata_t **entry)
621 {
622 struct stat st;
623 pts_file_metadata_t *this;
624
625 this = malloc_thing(pts_file_metadata_t);
626
627 if (stat(pathname, &st))
628 {
629 DBG1(DBG_PTS, "Unable to obtain statistics about '%s'", pathname);
630 return FALSE;
631 }
632
633 if (S_ISREG(st.st_mode))
634 {
635 this->type = PTS_FILE_REGULAR;
636 }
637 else if (S_ISDIR(st.st_mode))
638 {
639 this->type = PTS_FILE_DIRECTORY;
640 }
641 else if (S_ISCHR(st.st_mode))
642 {
643 this->type = PTS_FILE_CHAR_SPEC;
644 }
645 else if (S_ISBLK(st.st_mode))
646 {
647 this->type = PTS_FILE_BLOCK_SPEC;
648 }
649 else if (S_ISFIFO(st.st_mode))
650 {
651 this->type = PTS_FILE_FIFO;
652 }
653 else if (S_ISLNK(st.st_mode))
654 {
655 this->type = PTS_FILE_SYM_LINK;
656 }
657 else if (S_ISSOCK(st.st_mode))
658 {
659 this->type = PTS_FILE_SOCKET;
660 }
661 else
662 {
663 this->type = PTS_FILE_OTHER;
664 }
665
666 this->filesize = st.st_size;
667 this->created = st.st_ctime;
668 this->modified = st.st_mtime;
669 this->accessed = st.st_atime;
670 this->owner = st.st_uid;
671 this->group = st.st_gid;
672
673 *entry = this;
674 return TRUE;
675 }
676
677 METHOD(pts_t, get_metadata, pts_file_meta_t*,
678 private_pts_t *this, char *pathname, bool is_directory)
679 {
680 pts_file_meta_t *metadata;
681 pts_file_metadata_t *entry;
682
683 /* Create a metadata object */
684 metadata = pts_file_meta_create();
685
686 if (is_directory)
687 {
688 enumerator_t *enumerator;
689 char *rel_name, *abs_name;
690 struct stat st;
691
692 enumerator = enumerator_create_directory(pathname);
693 if (!enumerator)
694 {
695 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
696 strerror(errno));
697 metadata->destroy(metadata);
698 return NULL;
699 }
700 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
701 {
702 /* measure regular files only */
703 if (S_ISREG(st.st_mode) && *rel_name != '.')
704 {
705 if (!file_metadata(abs_name, &entry))
706 {
707 enumerator->destroy(enumerator);
708 metadata->destroy(metadata);
709 return NULL;
710 }
711 entry->filename = strdup(rel_name);
712 metadata->add(metadata, entry);
713 }
714 }
715 enumerator->destroy(enumerator);
716 }
717 else
718 {
719 if (!file_metadata(pathname, &entry))
720 {
721 metadata->destroy(metadata);
722 return NULL;
723 }
724 entry->filename = strdup(get_filename(pathname));
725 metadata->add(metadata, entry);
726 }
727
728 return metadata;
729 }
730
731 METHOD(pts_t, read_pcr, bool,
732 private_pts_t *this, u_int32_t pcr_num, chunk_t *pcr_value)
733 {
734 TSS_HCONTEXT hContext;
735 TSS_HTPM hTPM;
736 TSS_RESULT result;
737 chunk_t rgbPcrValue;
738
739 bool success = FALSE;
740
741 result = Tspi_Context_Create(&hContext);
742 if (result != TSS_SUCCESS)
743 {
744 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
745 return FALSE;
746 }
747
748 result = Tspi_Context_Connect(hContext, NULL);
749 if (result != TSS_SUCCESS)
750 {
751 goto err;
752 }
753 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
754 if (result != TSS_SUCCESS)
755 {
756 goto err;
757 }
758 result = Tspi_TPM_PcrRead(hTPM, pcr_num, (UINT32*)&rgbPcrValue.len, &rgbPcrValue.ptr);
759 if (result != TSS_SUCCESS)
760 {
761 goto err;
762 }
763 *pcr_value = chunk_clone(rgbPcrValue);
764 DBG3(DBG_PTS, "PCR %d value:%B", pcr_num, pcr_value);
765 success = TRUE;
766
767 err:
768 if (!success)
769 {
770 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
771 }
772 Tspi_Context_FreeMemory(hContext, NULL);
773 Tspi_Context_Close(hContext);
774
775 return success;
776 }
777
778 METHOD(pts_t, extend_pcr, bool,
779 private_pts_t *this, u_int32_t pcr_num, chunk_t input, chunk_t *output)
780 {
781 TSS_HCONTEXT hContext;
782 TSS_HTPM hTPM;
783 TSS_RESULT result;
784 u_int32_t pcr_length;
785 chunk_t pcr_value;
786
787 result = Tspi_Context_Create(&hContext);
788 if (result != TSS_SUCCESS)
789 {
790 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
791 result);
792 return FALSE;
793 }
794 result = Tspi_Context_Connect(hContext, NULL);
795 if (result != TSS_SUCCESS)
796 {
797 goto err;
798 }
799 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
800 if (result != TSS_SUCCESS)
801 {
802 goto err;
803 }
804
805 pcr_value = chunk_alloc(PCR_LEN);
806 result = Tspi_TPM_PcrExtend(hTPM, pcr_num, PCR_LEN, input.ptr,
807 NULL, &pcr_length, &pcr_value.ptr);
808 if (result != TSS_SUCCESS)
809 {
810 goto err;
811 }
812
813 *output = pcr_value;
814 *output = chunk_clone(*output);
815
816 DBG3(DBG_PTS, "PCR %d extended with: %B", pcr_num, &input);
817 DBG3(DBG_PTS, "PCR %d value after extend: %B", pcr_num, output);
818
819 chunk_clear(&pcr_value);
820 Tspi_Context_FreeMemory(hContext, NULL);
821 Tspi_Context_Close(hContext);
822
823 return TRUE;
824
825 err:
826 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
827
828 chunk_clear(&pcr_value);
829 Tspi_Context_FreeMemory(hContext, NULL);
830 Tspi_Context_Close(hContext);
831
832 return FALSE;
833 }
834
835
836 static void clear_pcrs(private_pts_t *this)
837 {
838 int i;
839
840 for (i = 0; i <= this->pcr_max; i++)
841 {
842 free(this->pcrs[i]);
843 this->pcrs[i] = NULL;
844 }
845 this->pcr_count = 0;
846 this->pcr_max = 0;
847
848 memset(this->pcr_select, 0x00, sizeof(this->pcr_select));
849 }
850
851 METHOD(pts_t, quote_tpm, bool,
852 private_pts_t *this, bool use_quote2, chunk_t *pcr_comp, chunk_t *quote_sig)
853 {
854 TSS_HCONTEXT hContext;
855 TSS_HTPM hTPM;
856 TSS_HKEY hAIK;
857 TSS_HKEY hSRK;
858 TSS_HPOLICY srkUsagePolicy;
859 TSS_UUID SRK_UUID = TSS_UUID_SRK;
860 BYTE secret[] = TSS_WELL_KNOWN_SECRET;
861 TSS_HPCRS hPcrComposite;
862 TSS_VALIDATION valData;
863 TSS_RESULT result;
864 chunk_t quote_info;
865 BYTE* versionInfo;
866 u_int32_t versionInfoSize, pcr, i = 0, f = 1;
867 bool success = FALSE;
868
869 result = Tspi_Context_Create(&hContext);
870 if (result != TSS_SUCCESS)
871 {
872 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
873 result);
874 return FALSE;
875 }
876 result = Tspi_Context_Connect(hContext, NULL);
877 if (result != TSS_SUCCESS)
878 {
879 goto err1;
880 }
881 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
882 if (result != TSS_SUCCESS)
883 {
884 goto err1;
885 }
886
887 /* Retrieve SRK from TPM and set the authentication to well known secret*/
888 result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
889 SRK_UUID, &hSRK);
890 if (result != TSS_SUCCESS)
891 {
892 goto err1;
893 }
894
895 result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
896 if (result != TSS_SUCCESS)
897 {
898 goto err1;
899 }
900 result = Tspi_Policy_SetSecret(srkUsagePolicy, TSS_SECRET_MODE_SHA1,
901 20, secret);
902 if (result != TSS_SUCCESS)
903 {
904 goto err1;
905 }
906
907 result = Tspi_Context_LoadKeyByBlob (hContext, hSRK, this->aik_blob.len,
908 this->aik_blob.ptr, &hAIK);
909 if (result != TSS_SUCCESS)
910 {
911 goto err1;
912 }
913
914 /* Create PCR composite object */
915 result = use_quote2 ?
916 Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
917 TSS_PCRS_STRUCT_INFO_SHORT, &hPcrComposite) :
918 Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
919 0, &hPcrComposite);
920 if (result != TSS_SUCCESS)
921 {
922 goto err2;
923 }
924
925 /* Select PCRs */
926 for (pcr = 0; pcr <= this->pcr_max ; pcr++)
927 {
928 if (f == 256)
929 {
930 i++;
931 f = 1;
932 }
933 if (this->pcr_select[i] & f)
934 {
935 DBG2(DBG_TNC, "PCR %02d selected for TPM Quote", pcr);
936 result = use_quote2 ?
937 Tspi_PcrComposite_SelectPcrIndexEx(hPcrComposite, pcr,
938 TSS_PCRS_DIRECTION_RELEASE) :
939 Tspi_PcrComposite_SelectPcrIndex(hPcrComposite, pcr);
940 if (result != TSS_SUCCESS)
941 {
942 goto err3;
943 }
944 }
945 f <<= 1;
946 }
947
948 /* Set the Validation Data */
949 valData.ulExternalDataLength = this->secret.len;
950 valData.rgbExternalData = (BYTE *)this->secret.ptr;
951
952
953 /* TPM Quote */
954 result = use_quote2 ?
955 Tspi_TPM_Quote2(hTPM, hAIK, FALSE, hPcrComposite, &valData,
956 &versionInfoSize, &versionInfo):
957 Tspi_TPM_Quote(hTPM, hAIK, hPcrComposite, &valData);
958 if (result != TSS_SUCCESS)
959 {
960 goto err4;
961 }
962
963 /* Set output chunks */
964 *pcr_comp = chunk_alloc(HASH_SIZE_SHA1);
965
966 if (use_quote2)
967 {
968 /* TPM_Composite_Hash is last 20 bytes of TPM_Quote_Info2 structure */
969 memcpy(pcr_comp->ptr, valData.rgbData + valData.ulDataLength - HASH_SIZE_SHA1,
970 HASH_SIZE_SHA1);
971 }
972 else
973 {
974 /* TPM_Composite_Hash is 8-28th bytes of TPM_Quote_Info structure */
975 memcpy(pcr_comp->ptr, valData.rgbData + 8, HASH_SIZE_SHA1);
976 }
977 DBG3(DBG_PTS, "Hash of PCR Composite: %#B", pcr_comp);
978
979 quote_info = chunk_create(valData.rgbData, valData.ulDataLength);
980 DBG3(DBG_PTS, "TPM Quote Info: %B",&quote_info);
981
982 *quote_sig = chunk_clone(chunk_create(valData.rgbValidationData,
983 valData.ulValidationDataLength));
984 DBG3(DBG_PTS, "TPM Quote Signature: %B",quote_sig);
985
986 success = TRUE;
987
988 /* Cleanup */
989 err4:
990 Tspi_Context_FreeMemory(hContext, NULL);
991
992 err3:
993 Tspi_Context_CloseObject(hContext, hPcrComposite);
994
995 err2:
996 Tspi_Context_CloseObject(hContext, hAIK);
997
998 err1:
999 Tspi_Context_Close(hContext);
1000
1001 if (!success)
1002 {
1003 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
1004 }
1005 clear_pcrs(this);
1006
1007 return success;
1008 }
1009
1010 METHOD(pts_t, select_pcr, bool,
1011 private_pts_t *this, u_int32_t pcr)
1012 {
1013 u_int32_t i, f;
1014
1015 if (pcr >= PCR_MAX_NUM)
1016 {
1017 DBG1(DBG_PTS, "PCR %u: number is larger than maximum of %u",
1018 pcr, PCR_MAX_NUM-1);
1019 return FALSE;
1020 }
1021
1022 /* Determine PCR selection flag */
1023 i = pcr / 8;
1024 f = 1 << (pcr - 8*i);
1025
1026 /* Has this PCR already been selected? */
1027 if (!(this->pcr_select[i] & f))
1028 {
1029 this->pcr_select[i] |= f;
1030 this->pcr_max = max(this->pcr_max, pcr);
1031 this->pcr_count++;
1032 }
1033
1034 return TRUE;
1035 }
1036
1037 METHOD(pts_t, add_pcr, bool,
1038 private_pts_t *this, u_int32_t pcr, chunk_t pcr_before, chunk_t pcr_after)
1039 {
1040 if (pcr >= PCR_MAX_NUM)
1041 {
1042 DBG1(DBG_PTS, "PCR %u: number is larger than maximum of %u",
1043 pcr, PCR_MAX_NUM-1);
1044 return FALSE;
1045 }
1046
1047 /* Is the length of the PCR registers already set? */
1048 if (this->pcr_len)
1049 {
1050 if (pcr_after.len != this->pcr_len)
1051 {
1052 DBG1(DBG_PTS, "PCR %02u: length is %d bytes but should be %d bytes",
1053 pcr_after.len, this->pcr_len);
1054 return FALSE;
1055 }
1056 }
1057 else
1058 {
1059 this->pcr_len = pcr_after.len;
1060 }
1061
1062 /* Has the value of the PCR register already been assigned? */
1063 if (this->pcrs[pcr])
1064 {
1065 if (!memeq(this->pcrs[pcr], pcr_before.ptr, this->pcr_len))
1066 {
1067 DBG1(DBG_PTS, "PCR %02u: new pcr_before value does not equal "
1068 "old pcr_after value");
1069 }
1070 /* remove the old PCR value */
1071 free(this->pcrs[pcr]);
1072 }
1073 else
1074 {
1075 /* add extended PCR Register */
1076 this->pcr_select[pcr / 8] |= 1 << (pcr % 8);
1077 this->pcr_max = max(this->pcr_max, pcr);
1078 this->pcr_count++;
1079 }
1080
1081 /* Duplicate and store current PCR value */
1082 pcr_after = chunk_clone(pcr_after);
1083 this->pcrs[pcr] = pcr_after.ptr;
1084
1085 return TRUE;
1086 }
1087
1088 METHOD(pts_t, does_pcr_value_match, bool,
1089 private_pts_t *this, chunk_t pcr_after_value)
1090 {
1091 enumerator_t *e;
1092 pcr_entry_t *entry;
1093
1094 if (!this->pcrs)
1095 {
1096 this->pcrs = linked_list_create();
1097 }
1098
1099 e = this->pcrs->create_enumerator(this->pcrs);
1100 while (e->enumerate(e, &entry))
1101 {
1102 if (entry->pcr_number == new->pcr_number)
1103 {
1104 DBG4(DBG_PTS, "updating already added PCR%d value",
1105 entry->pcr_number);
1106 this->pcrs->remove_at(this->pcrs, e);
1107 free(entry);
1108 break;
1109 }
1110 }
1111 DESTROY_IF(e);
1112 this->pcrs->insert_last(this->pcrs, new);
1113 }
1114
1115 /**
1116 * TPM_QUOTE_INFO structure:
1117 * 4 bytes of version
1118 * 4 bytes 'Q' 'U' 'O' 'T'
1119 * 20 byte SHA1 of TCPA_PCR_COMPOSITE
1120 * 20 byte nonce
1121 *
1122 * TPM_QUOTE_INFO2 structure:
1123 * 2 bytes Tag 0x0036 TPM_Tag_Quote_info2
1124 * 4 bytes 'Q' 'U' 'T' '2'
1125 * 20 bytes nonce
1126 * 26 bytes PCR_INFO_SHORT
1127 */
1128
1129 METHOD(pts_t, get_quote_info, bool,
1130 private_pts_t *this, bool use_quote2, bool use_ver_info,
1131 pts_meas_algorithms_t comp_hash_algo,
1132 chunk_t *out_pcr_comp, chunk_t *out_quote_info)
1133 {
1134 u_int8_t size_of_select;
1135 int pcr_comp_len, i;
1136 chunk_t pcr_comp, hash_pcr_comp;
1137 bio_writer_t *writer;
1138 hasher_t *hasher;
1139
1140 if (this->pcr_count == 0)
1141 {
1142 DBG1(DBG_PTS, "No extended PCR entries available, "
1143 "unable to construct TPM Quote Info");
1144 return FALSE;
1145 }
1146 if (!this->secret.ptr)
1147 {
1148 DBG1(DBG_PTS, "Secret assessment value unavailable, ",
1149 "unable to construct TPM Quote Info");
1150 return FALSE;
1151 }
1152 if (use_quote2 && use_ver_info && !this->tpm_version_info.ptr)
1153 {
1154 DBG1(DBG_PTS, "TPM Version Information unavailable, ",
1155 "unable to construct TPM Quote Info2");
1156 return FALSE;
1157 }
1158
1159 size_of_select = 1 + this->pcr_max / 8;
1160 pcr_comp_len = 2 + size_of_select + 4 + this->pcr_count * this->pcr_len;
1161
1162 writer = bio_writer_create(pcr_comp_len);
1163
1164 writer->write_uint16(writer, size_of_select);
1165 for (i = 0; i < size_of_select; i++)
1166 {
1167 writer->write_uint8(writer, this->pcr_select[i]);
1168 }
1169
1170 writer->write_uint32(writer, this->pcr_count * this->pcr_len);
1171 for (i = 0; i < 8 * size_of_select; i++)
1172 {
1173 if (this->pcrs[i])
1174 {
1175 writer->write_data(writer, chunk_create(this->pcrs[i], this->pcr_len));
1176 }
1177 }
1178 pcr_comp = chunk_clone(writer->get_buf(writer));
1179 DBG3(DBG_PTS, "constructed PCR Composite: %B", &pcr_comp);
1180
1181 writer->destroy(writer);
1182
1183 /* Output the TPM_PCR_COMPOSITE expected from IMC */
1184 if (comp_hash_algo)
1185 {
1186 hash_algorithm_t algo;
1187
1188 algo = pts_meas_algo_to_hash(comp_hash_algo);
1189 hasher = lib->crypto->create_hasher(lib->crypto, algo);
1190
1191 /* Hash the PCR Composite Structure */
1192 hasher->allocate_hash(hasher, pcr_comp, out_pcr_comp);
1193 DBG3(DBG_PTS, "constructed PCR Composite hash: %#B", out_pcr_comp);
1194 hasher->destroy(hasher);
1195 }
1196 else
1197 {
1198 *out_pcr_comp = chunk_clone(pcr_comp);
1199 }
1200
1201 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
1202 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1203 hasher->allocate_hash(hasher, pcr_comp, &hash_pcr_comp);
1204 hasher->destroy(hasher);
1205
1206 writer->write_data(writer, hash_pcr_composite);
1207 chunk_clear(&pcr_composite);
1208 chunk_clear(&hash_pcr_composite);
1209
1210 /* Hash the PCR Composite Structure */
1211 hasher->allocate_hash(hasher, pcr_composite, out_pcr_composite);
1212 DBG4(DBG_PTS, "Hash of calculated PCR Composite: %B", out_pcr_composite);
1213 hasher->destroy(hasher);
1214 }
1215 else
1216 {
1217 *out_pcr_composite = chunk_clone(pcr_composite);
1218 DBG3(DBG_PTS, "calculated PCR Composite: %B", out_pcr_composite);
1219 }
1220
1221 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
1222 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
1223 hasher->allocate_hash(hasher, pcr_composite, &hash_pcr_composite);
1224 hasher->destroy(hasher);
1225
1226 /* Construct TPM_QUOTE_INFO/TPM_QUOTE_INFO2 structure */
1227 writer = bio_writer_create(TPM_QUOTE_INFO_LEN);
1228
1229 if (use_quote2)
1230 {
1231 /* TPM Structure Tag */
1232 writer->write_uint16(writer, TPM_TAG_QUOTE_INFO2);
1233
1234 /* Magic QUT2 value */
1235 writer->write_data(writer, chunk_create("QUT2", 4));
1236
1237 /* Secret assessment value 20 bytes (nonce) */
1238 writer->write_data(writer, this->secret);
1239
1240 /* Length of the PCR selection field */
1241 writer->write_uint16(writer, size_of_select);
1242
1243 /* PCR selection */
1244 for (i = 0; i < size_of_select ; i++)
1245 {
1246 writer->write_uint8(writer, this->pcr_select[i]);
1247 }
1248
1249 /* TPM Locality Selection */
1250 writer->write_uint8(writer, TPM_LOC_ZERO);
1251
1252 /* PCR Composite Hash */
1253 writer->write_data(writer, hash_pcr_comp);
1254
1255 if (use_ver_info)
1256 {
1257 /* TPM version Info */
1258 writer->write_data(writer, this->tpm_version_info);
1259 }
1260 }
1261 else
1262 {
1263 /* Version number */
1264 writer->write_data(writer, chunk_from_chars(1, 1, 0, 0));
1265
1266 /* Magic QUOT value */
1267 writer->write_data(writer, chunk_create("QUOT", 4));
1268
1269 /* PCR Composite Hash */
1270 writer->write_data(writer, hash_pcr_comp);
1271
1272 /* Secret assessment value 20 bytes (nonce) */
1273 writer->write_data(writer, this->secret);
1274 }
1275
1276 /* TPM Quote Info */
1277 *out_quote_info = chunk_clone(writer->get_buf(writer));
1278 DBG3(DBG_PTS, "constructed TPM Quote Info: %B", out_quote_info);
1279
1280 writer->destroy(writer);
1281 free(pcr_comp.ptr);
1282 free(hash_pcr_comp.ptr);
1283 clear_pcrs(this);
1284
1285 return TRUE;
1286 }
1287
1288 METHOD(pts_t, verify_quote_signature, bool,
1289 private_pts_t *this, chunk_t data, chunk_t signature)
1290 {
1291 public_key_t *aik_pub_key;
1292
1293 aik_pub_key = this->aik->get_public_key(this->aik);
1294 if (!aik_pub_key)
1295 {
1296 DBG1(DBG_PTS, "failed to get public key from AIK certificate");
1297 return FALSE;
1298 }
1299
1300 if (!aik_pub_key->verify(aik_pub_key, SIGN_RSA_EMSA_PKCS1_SHA1,
1301 data, signature))
1302 {
1303 DBG1(DBG_PTS, "signature verification failed for TPM Quote Info");
1304 DESTROY_IF(aik_pub_key);
1305 return FALSE;
1306 }
1307 */
1308
1309 if (!aik_pub_key->get_encoding(aik_pub_key,
1310 PUBKEY_SPKI_ASN1_DER, &key_encoding))
1311 {
1312 DBG1(DBG_PTS, "failed to get encoding of AIK public key");
1313 goto cleanup;
1314 }
1315
1316 aik_pub_key->destroy(aik_pub_key);
1317 return TRUE;
1318 }
1319
1320 METHOD(pts_t, destroy, void,
1321 private_pts_t *this)
1322 {
1323 clear_pcrs(this);
1324 DESTROY_IF(this->aik);
1325 DESTROY_IF(this->dh);
1326 free(this->initiator_nonce.ptr);
1327 free(this->responder_nonce.ptr);
1328 free(this->secret.ptr);
1329 free(this->platform_info);
1330 free(this->aik_blob.ptr);
1331 free(this->tpm_version_info.ptr);
1332 free(this);
1333 }
1334
1335 /**
1336 * Determine Linux distribution and hardware platform
1337 */
1338 static char* extract_platform_info(void)
1339 {
1340 FILE *file;
1341 char buf[BUF_LEN], *pos, *value = NULL;
1342 int i, len;
1343 struct utsname uninfo;
1344
1345 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
1346 const char* releases[] = {
1347 "/etc/lsb-release", "/etc/debian_version",
1348 "/etc/SuSE-release", "/etc/novell-release",
1349 "/etc/sles-release", "/etc/redhat-release",
1350 "/etc/fedora-release", "/etc/gentoo-release",
1351 "/etc/slackware-version", "/etc/annvix-release",
1352 "/etc/arch-release", "/etc/arklinux-release",
1353 "/etc/aurox-release", "/etc/blackcat-release",
1354 "/etc/cobalt-release", "/etc/conectiva-release",
1355 "/etc/debian_release", "/etc/immunix-release",
1356 "/etc/lfs-release", "/etc/linuxppc-release",
1357 "/etc/mandrake-release", "/etc/mandriva-release",
1358 "/etc/mandrakelinux-release", "/etc/mklinux-release",
1359 "/etc/pld-release", "/etc/redhat_version",
1360 "/etc/slackware-release", "/etc/e-smith-release",
1361 "/etc/release", "/etc/sun-release",
1362 "/etc/tinysofa-release", "/etc/turbolinux-release",
1363 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
1364 "/etc/va-release", "/etc/yellowdog-release"
1365 };
1366
1367 const char description[] = "DISTRIB_DESCRIPTION=\"";
1368
1369 for (i = 0; i < countof(releases); i++)
1370 {
1371 file = fopen(releases[i], "r");
1372 if (!file)
1373 {
1374 continue;
1375 }
1376 fseek(file, 0, SEEK_END);
1377 len = min(ftell(file), sizeof(buf)-1);
1378 rewind(file);
1379 buf[len] = '\0';
1380 if (fread(buf, 1, len, file) != len)
1381 {
1382 DBG1(DBG_PTS, "failed to read file '%s'", releases[i]);
1383 fclose(file);
1384 return NULL;
1385 }
1386 fclose(file);
1387
1388 if (i == 0) /* LSB release */
1389 {
1390 pos = strstr(buf, description);
1391 if (!pos)
1392 {
1393 DBG1(DBG_PTS, "failed to find begin of lsb-release "
1394 "DESCRIPTION field");
1395 return NULL;
1396 }
1397 value = pos + strlen(description);
1398 pos = strchr(value, '"');
1399 if (!pos)
1400 {
1401 DBG1(DBG_PTS, "failed to find end of lsb-release "
1402 "DESCRIPTION field");
1403 return NULL;
1404 }
1405 }
1406 else
1407 {
1408 value = buf;
1409 pos = strchr(value, '\n');
1410 if (!pos)
1411 {
1412 DBG1(DBG_PTS, "failed to find end of release string");
1413 return NULL;
1414 }
1415 }
1416 break;
1417 }
1418
1419 if (!value)
1420 {
1421 DBG1(DBG_PTS, "no distribution release file found");
1422 return NULL;
1423 }
1424
1425 if (uname(&uninfo) < 0)
1426 {
1427 DBG1(DBG_PTS, "could not retrieve machine architecture");
1428 return NULL;
1429 }
1430
1431 *pos++ = ' ';
1432 len = sizeof(buf)-1 + (pos - buf);
1433 strncpy(pos, uninfo.machine, len);
1434
1435 DBG1(DBG_PTS, "platform is '%s'", value);
1436 return strdup(value);
1437 }
1438
1439 /**
1440 * Check for a TPM by querying for TPM Version Info
1441 */
1442 static bool has_tpm(private_pts_t *this)
1443 {
1444 TSS_HCONTEXT hContext;
1445 TSS_HTPM hTPM;
1446 TSS_RESULT result;
1447 u_int32_t version_info_len;
1448
1449 result = Tspi_Context_Create(&hContext);
1450 if (result != TSS_SUCCESS)
1451 {
1452 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
1453 result);
1454 return FALSE;
1455 }
1456 result = Tspi_Context_Connect(hContext, NULL);
1457 if (result != TSS_SUCCESS)
1458 {
1459 goto err;
1460 }
1461 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
1462 if (result != TSS_SUCCESS)
1463 {
1464 goto err;
1465 }
1466 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
1467 &version_info_len,
1468 &this->tpm_version_info.ptr);
1469 this->tpm_version_info.len = version_info_len;
1470 if (result != TSS_SUCCESS)
1471 {
1472 goto err;
1473 }
1474 this->tpm_version_info = chunk_clone(this->tpm_version_info);
1475
1476 Tspi_Context_FreeMemory(hContext, NULL);
1477 Tspi_Context_Close(hContext);
1478 return TRUE;
1479
1480 err:
1481 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
1482 Tspi_Context_FreeMemory(hContext, NULL);
1483 Tspi_Context_Close(hContext);
1484 return FALSE;
1485 }
1486
1487 /**
1488 * See header
1489 */
1490 pts_t *pts_create(bool is_imc)
1491 {
1492 private_pts_t *this;
1493
1494 INIT(this,
1495 .public = {
1496 .get_proto_caps = _get_proto_caps,
1497 .set_proto_caps = _set_proto_caps,
1498 .get_meas_algorithm = _get_meas_algorithm,
1499 .set_meas_algorithm = _set_meas_algorithm,
1500 .get_dh_hash_algorithm = _get_dh_hash_algorithm,
1501 .set_dh_hash_algorithm = _set_dh_hash_algorithm,
1502 .create_dh_nonce = _create_dh_nonce,
1503 .get_my_public_value = _get_my_public_value,
1504 .set_peer_public_value = _set_peer_public_value,
1505 .calculate_secret = _calculate_secret,
1506 .get_platform_info = _get_platform_info,
1507 .set_platform_info = _set_platform_info,
1508 .get_tpm_version_info = _get_tpm_version_info,
1509 .set_tpm_version_info = _set_tpm_version_info,
1510 .get_pcr_len = _get_pcr_len,
1511 .get_aik = _get_aik,
1512 .set_aik = _set_aik,
1513 .is_path_valid = _is_path_valid,
1514 .hash_file = _hash_file,
1515 .do_measurements = _do_measurements,
1516 .get_metadata = _get_metadata,
1517 .read_pcr = _read_pcr,
1518 .extend_pcr = _extend_pcr,
1519 .quote_tpm = _quote_tpm,
1520 .select_pcr = _select_pcr,
1521 .add_pcr = _add_pcr,
1522 .get_quote_info = _get_quote_info,
1523 .verify_quote_signature = _verify_quote_signature,
1524 .destroy = _destroy,
1525 },
1526 .is_imc = is_imc,
1527 .proto_caps = PTS_PROTO_CAPS_V,
1528 .algorithm = PTS_MEAS_ALGO_SHA256,
1529 .dh_hash_algorithm = PTS_MEAS_ALGO_SHA256,
1530 );
1531
1532 if (is_imc)
1533 {
1534 this->platform_info = extract_platform_info();
1535
1536 if (has_tpm(this))
1537 {
1538 this->has_tpm = TRUE;
1539 this->pcr_len = PCR_LEN;
1540 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_D;
1541 load_aik(this);
1542 load_aik_blob(this);
1543 }
1544 }
1545 else
1546 {
1547 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_D;
1548 }
1549
1550 return &this->public;
1551 }