509fc41d94fdc3b42c1316dfc17e003b53c12db4
[strongswan.git] / src / libpts / pts / pts.c
1 /*
2 * Copyright (C) 2011-2012 Sansar Choinyambuu, Andreas Steffen
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 <utils/debug.h>
19 #include <crypto/hashers/hasher.h>
20 #include <bio/bio_writer.h>
21 #include <bio/bio_reader.h>
22
23 #ifdef TSS_TROUSERS
24 #include <trousers/tss.h>
25 #include <trousers/trousers.h>
26 #else
27 #ifndef TPM_TAG_QUOTE_INFO2
28 #define TPM_TAG_QUOTE_INFO2 0x0036
29 #endif
30 #ifndef TPM_LOC_ZERO
31 #define TPM_LOC_ZERO 0x01
32 #endif
33 #endif
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/utsname.h>
38 #include <libgen.h>
39 #include <unistd.h>
40 #include <errno.h>
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 * Shadow PCR set
122 */
123 pts_pcr_t *pcrs;
124
125 };
126
127 METHOD(pts_t, get_proto_caps, pts_proto_caps_flag_t,
128 private_pts_t *this)
129 {
130 return this->proto_caps;
131 }
132
133 METHOD(pts_t, set_proto_caps, void,
134 private_pts_t *this, pts_proto_caps_flag_t flags)
135 {
136 this->proto_caps = flags;
137 DBG2(DBG_PTS, "supported PTS protocol capabilities: %s%s%s%s%s",
138 flags & PTS_PROTO_CAPS_C ? "C" : ".",
139 flags & PTS_PROTO_CAPS_V ? "V" : ".",
140 flags & PTS_PROTO_CAPS_D ? "D" : ".",
141 flags & PTS_PROTO_CAPS_T ? "T" : ".",
142 flags & PTS_PROTO_CAPS_X ? "X" : ".");
143 }
144
145 METHOD(pts_t, get_meas_algorithm, pts_meas_algorithms_t,
146 private_pts_t *this)
147 {
148 return this->algorithm;
149 }
150
151 METHOD(pts_t, set_meas_algorithm, void,
152 private_pts_t *this, pts_meas_algorithms_t algorithm)
153 {
154 hash_algorithm_t hash_alg;
155
156 hash_alg = pts_meas_algo_to_hash(algorithm);
157 DBG2(DBG_PTS, "selected PTS measurement algorithm is %N",
158 hash_algorithm_names, hash_alg);
159 if (hash_alg != HASH_UNKNOWN)
160 {
161 this->algorithm = algorithm;
162 }
163 }
164
165 METHOD(pts_t, get_dh_hash_algorithm, pts_meas_algorithms_t,
166 private_pts_t *this)
167 {
168 return this->dh_hash_algorithm;
169 }
170
171 METHOD(pts_t, set_dh_hash_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 DH hash algorithm is %N",
178 hash_algorithm_names, hash_alg);
179 if (hash_alg != HASH_UNKNOWN)
180 {
181 this->dh_hash_algorithm = algorithm;
182 }
183 }
184
185
186 METHOD(pts_t, create_dh_nonce, bool,
187 private_pts_t *this, pts_dh_group_t group, int nonce_len)
188 {
189 diffie_hellman_group_t dh_group;
190 chunk_t *nonce;
191 rng_t *rng;
192
193 dh_group = pts_dh_group_to_ike(group);
194 DBG2(DBG_PTS, "selected PTS DH group is %N",
195 diffie_hellman_group_names, dh_group);
196 DESTROY_IF(this->dh);
197 this->dh = lib->crypto->create_dh(lib->crypto, dh_group);
198
199 rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
200 if (!rng)
201 {
202 DBG1(DBG_PTS, "no rng available");
203 return FALSE;
204 }
205 DBG2(DBG_PTS, "nonce length is %d", nonce_len);
206 nonce = this->is_imc ? &this->responder_nonce : &this->initiator_nonce;
207 chunk_free(nonce);
208 if (!rng->allocate_bytes(rng, nonce_len, nonce))
209 {
210 DBG1(DBG_PTS, "failed to allocate nonce");
211 rng->destroy(rng);
212 return FALSE;
213 }
214 rng->destroy(rng);
215 return TRUE;
216 }
217
218 METHOD(pts_t, get_my_public_value, void,
219 private_pts_t *this, chunk_t *value, chunk_t *nonce)
220 {
221 this->dh->get_my_public_value(this->dh, value);
222 *nonce = this->is_imc ? this->responder_nonce : this->initiator_nonce;
223 }
224
225 METHOD(pts_t, set_peer_public_value, void,
226 private_pts_t *this, chunk_t value, chunk_t nonce)
227 {
228 this->dh->set_other_public_value(this->dh, value);
229
230 nonce = chunk_clone(nonce);
231 if (this->is_imc)
232 {
233 this->initiator_nonce = nonce;
234 }
235 else
236 {
237 this->responder_nonce = nonce;
238 }
239 }
240
241 METHOD(pts_t, calculate_secret, bool,
242 private_pts_t *this)
243 {
244 hasher_t *hasher;
245 hash_algorithm_t hash_alg;
246 chunk_t shared_secret;
247
248 /* Check presence of nonces */
249 if (!this->initiator_nonce.len || !this->responder_nonce.len)
250 {
251 DBG1(DBG_PTS, "initiator and/or responder nonce is not available");
252 return FALSE;
253 }
254 DBG3(DBG_PTS, "initiator nonce: %B", &this->initiator_nonce);
255 DBG3(DBG_PTS, "responder nonce: %B", &this->responder_nonce);
256
257 /* Calculate the DH secret */
258 if (this->dh->get_shared_secret(this->dh, &shared_secret) != SUCCESS)
259 {
260 DBG1(DBG_PTS, "shared DH secret computation failed");
261 return FALSE;
262 }
263 DBG3(DBG_PTS, "shared DH secret: %B", &shared_secret);
264
265 /* Calculate the secret assessment value */
266 hash_alg = pts_meas_algo_to_hash(this->dh_hash_algorithm);
267 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
268
269 if (!hasher ||
270 !hasher->get_hash(hasher, chunk_from_chars('1'), NULL) ||
271 !hasher->get_hash(hasher, this->initiator_nonce, NULL) ||
272 !hasher->get_hash(hasher, this->responder_nonce, NULL) ||
273 !hasher->allocate_hash(hasher, shared_secret, &this->secret))
274 {
275 DESTROY_IF(hasher);
276 return FALSE;
277 }
278 hasher->destroy(hasher);
279
280 /* The DH secret must be destroyed */
281 chunk_clear(&shared_secret);
282
283 /*
284 * Truncate the hash to 20 bytes to fit the ExternalData
285 * argument of the TPM Quote command
286 */
287 this->secret.len = min(this->secret.len, 20);
288 DBG3(DBG_PTS, "secret assessment value: %B", &this->secret);
289 return TRUE;
290 }
291
292 #ifdef TSS_TROUSERS
293
294 /**
295 * Print TPM 1.2 Version Info
296 */
297 static void print_tpm_version_info(private_pts_t *this)
298 {
299 TPM_CAP_VERSION_INFO versionInfo;
300 UINT64 offset = 0;
301 TSS_RESULT result;
302
303 result = Trspi_UnloadBlob_CAP_VERSION_INFO(&offset,
304 this->tpm_version_info.ptr, &versionInfo);
305 if (result != TSS_SUCCESS)
306 {
307 DBG1(DBG_PTS, "could not parse tpm version info: tss error 0x%x",
308 result);
309 }
310 else
311 {
312 DBG2(DBG_PTS, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
313 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
314 versionInfo.version.major, versionInfo.version.minor,
315 versionInfo.version.revMajor, versionInfo.version.revMinor,
316 versionInfo.specLevel, versionInfo.errataRev,
317 versionInfo.tpmVendorID);
318 }
319 }
320
321 #else
322
323 static void print_tpm_version_info(private_pts_t *this)
324 {
325 DBG1(DBG_PTS, "unknown TPM version: no TSS implementation available");
326 }
327
328 #endif /* TSS_TROUSERS */
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, chunk_t name, chunk_t version)
338 {
339 int len = name.len + 1 + version.len + 1;
340
341 /* platform info is a concatenation of OS name and OS version */
342 free(this->platform_info);
343 this->platform_info = malloc(len);
344 snprintf(this->platform_info, len, "%.*s %.*s", name.len, name.ptr,
345 version.len, version.ptr);
346 }
347
348 METHOD(pts_t, get_tpm_version_info, bool,
349 private_pts_t *this, chunk_t *info)
350 {
351 if (!this->has_tpm)
352 {
353 return FALSE;
354 }
355 *info = this->tpm_version_info;
356 print_tpm_version_info(this);
357 return TRUE;
358 }
359
360 METHOD(pts_t, set_tpm_version_info, void,
361 private_pts_t *this, chunk_t info)
362 {
363 this->tpm_version_info = chunk_clone(info);
364 print_tpm_version_info(this);
365 }
366
367 /**
368 * Load an AIK Blob (TSS_TSPATTRIB_KEYBLOB_BLOB attribute)
369 */
370 static void load_aik_blob(private_pts_t *this)
371 {
372 char *blob_path;
373 FILE *fp;
374 u_int32_t aikBlobLen;
375
376 blob_path = lib->settings->get_str(lib->settings,
377 "libimcv.plugins.imc-attestation.aik_blob", NULL);
378
379 if (blob_path)
380 {
381 /* Read aik key blob from a file */
382 if ((fp = fopen(blob_path, "r")) == NULL)
383 {
384 DBG1(DBG_PTS, "unable to open AIK Blob file: %s", blob_path);
385 return;
386 }
387
388 fseek(fp, 0, SEEK_END);
389 aikBlobLen = ftell(fp);
390 fseek(fp, 0L, SEEK_SET);
391
392 this->aik_blob = chunk_alloc(aikBlobLen);
393 if (fread(this->aik_blob.ptr, 1, aikBlobLen, fp))
394 {
395 DBG2(DBG_PTS, "loaded AIK Blob from '%s'", blob_path);
396 DBG3(DBG_PTS, "AIK Blob: %B", &this->aik_blob);
397 }
398 else
399 {
400 DBG1(DBG_PTS, "unable to read AIK Blob file '%s'", blob_path);
401 }
402 fclose(fp);
403 return;
404 }
405
406 DBG1(DBG_PTS, "AIK Blob is not available");
407 }
408
409 /**
410 * Load an AIK certificate or public key
411 * the certificate having precedence over the public key if both are present
412 */
413 static void load_aik(private_pts_t *this)
414 {
415 char *cert_path, *key_path;
416
417 cert_path = lib->settings->get_str(lib->settings,
418 "libimcv.plugins.imc-attestation.aik_cert", NULL);
419 key_path = lib->settings->get_str(lib->settings,
420 "libimcv.plugins.imc-attestation.aik_key", NULL);
421
422 if (cert_path)
423 {
424 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
425 CERT_X509, BUILD_FROM_FILE,
426 cert_path, BUILD_END);
427 if (this->aik)
428 {
429 DBG2(DBG_PTS, "loaded AIK certificate from '%s'", cert_path);
430 return;
431 }
432 }
433 if (key_path)
434 {
435 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
436 CERT_TRUSTED_PUBKEY, BUILD_FROM_FILE,
437 key_path, BUILD_END);
438 if (this->aik)
439 {
440 DBG2(DBG_PTS, "loaded AIK public key from '%s'", key_path);
441 return;
442 }
443 }
444
445 DBG1(DBG_PTS, "neither AIK certificate nor public key is available");
446 }
447
448 METHOD(pts_t, get_aik, certificate_t*,
449 private_pts_t *this)
450 {
451 return this->aik;
452 }
453
454 METHOD(pts_t, set_aik, void,
455 private_pts_t *this, certificate_t *aik)
456 {
457 DESTROY_IF(this->aik);
458 this->aik = aik->get_ref(aik);
459 }
460
461 METHOD(pts_t, get_aik_keyid, bool,
462 private_pts_t *this, chunk_t *keyid)
463 {
464 public_key_t *public;
465 bool success;
466
467 if (!this->aik)
468 {
469 DBG1(DBG_PTS, "no AIK certificate available");
470 return FALSE;
471 }
472 public = this->aik->get_public_key(this->aik);
473 if (!public)
474 {
475 DBG1(DBG_PTS, "no AIK public key available");
476 return FALSE;
477 }
478 success = public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, keyid);
479 if (!success)
480 {
481 DBG1(DBG_PTS, "no SHA-1 AIK public key info ID available");
482 }
483 public->destroy(public);
484
485 return success;
486 }
487
488 METHOD(pts_t, is_path_valid, bool,
489 private_pts_t *this, char *path, pts_error_code_t *error_code)
490 {
491 struct stat st;
492
493 *error_code = 0;
494
495 if (!stat(path, &st))
496 {
497 return TRUE;
498 }
499 else if (errno == ENOENT || errno == ENOTDIR)
500 {
501 DBG1(DBG_PTS, "file/directory does not exist %s", path);
502 *error_code = TCG_PTS_FILE_NOT_FOUND;
503 }
504 else if (errno == EFAULT)
505 {
506 DBG1(DBG_PTS, "bad address %s", path);
507 *error_code = TCG_PTS_INVALID_PATH;
508 }
509 else
510 {
511 DBG1(DBG_PTS, "error: %s occurred while validating path: %s",
512 strerror(errno), path);
513 return FALSE;
514 }
515
516 return TRUE;
517 }
518
519 /**
520 * Obtain statistical information describing a file
521 */
522 static bool file_metadata(char *pathname, pts_file_metadata_t **entry)
523 {
524 struct stat st;
525 pts_file_metadata_t *this;
526
527 this = malloc_thing(pts_file_metadata_t);
528
529 if (stat(pathname, &st))
530 {
531 DBG1(DBG_PTS, "unable to obtain statistics about '%s'", pathname);
532 free(this);
533 return FALSE;
534 }
535
536 if (S_ISREG(st.st_mode))
537 {
538 this->type = PTS_FILE_REGULAR;
539 }
540 else if (S_ISDIR(st.st_mode))
541 {
542 this->type = PTS_FILE_DIRECTORY;
543 }
544 else if (S_ISCHR(st.st_mode))
545 {
546 this->type = PTS_FILE_CHAR_SPEC;
547 }
548 else if (S_ISBLK(st.st_mode))
549 {
550 this->type = PTS_FILE_BLOCK_SPEC;
551 }
552 else if (S_ISFIFO(st.st_mode))
553 {
554 this->type = PTS_FILE_FIFO;
555 }
556 else if (S_ISLNK(st.st_mode))
557 {
558 this->type = PTS_FILE_SYM_LINK;
559 }
560 else if (S_ISSOCK(st.st_mode))
561 {
562 this->type = PTS_FILE_SOCKET;
563 }
564 else
565 {
566 this->type = PTS_FILE_OTHER;
567 }
568
569 this->filesize = st.st_size;
570 this->created = st.st_ctime;
571 this->modified = st.st_mtime;
572 this->accessed = st.st_atime;
573 this->owner = st.st_uid;
574 this->group = st.st_gid;
575
576 *entry = this;
577 return TRUE;
578 }
579
580 METHOD(pts_t, get_metadata, pts_file_meta_t*,
581 private_pts_t *this, char *pathname, bool is_directory)
582 {
583 pts_file_meta_t *metadata;
584 pts_file_metadata_t *entry;
585
586 /* Create a metadata object */
587 metadata = pts_file_meta_create();
588
589 if (is_directory)
590 {
591 enumerator_t *enumerator;
592 char *rel_name, *abs_name;
593 struct stat st;
594
595 enumerator = enumerator_create_directory(pathname);
596 if (!enumerator)
597 {
598 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
599 strerror(errno));
600 metadata->destroy(metadata);
601 return NULL;
602 }
603 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
604 {
605 /* measure regular files only */
606 if (S_ISREG(st.st_mode) && *rel_name != '.')
607 {
608 if (!file_metadata(abs_name, &entry))
609 {
610 enumerator->destroy(enumerator);
611 metadata->destroy(metadata);
612 return NULL;
613 }
614 entry->filename = strdup(rel_name);
615 metadata->add(metadata, entry);
616 }
617 }
618 enumerator->destroy(enumerator);
619 }
620 else
621 {
622 if (!file_metadata(pathname, &entry))
623 {
624 metadata->destroy(metadata);
625 return NULL;
626 }
627 entry->filename = strdup(basename(pathname));
628 metadata->add(metadata, entry);
629 }
630
631 return metadata;
632 }
633
634
635 #ifdef TSS_TROUSERS
636
637 METHOD(pts_t, read_pcr, bool,
638 private_pts_t *this, u_int32_t pcr_num, chunk_t *pcr_value)
639 {
640 TSS_HCONTEXT hContext;
641 TSS_HTPM hTPM;
642 TSS_RESULT result;
643 chunk_t rgbPcrValue;
644
645 bool success = FALSE;
646
647 result = Tspi_Context_Create(&hContext);
648 if (result != TSS_SUCCESS)
649 {
650 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
651 return FALSE;
652 }
653
654 result = Tspi_Context_Connect(hContext, NULL);
655 if (result != TSS_SUCCESS)
656 {
657 goto err;
658 }
659 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
660 if (result != TSS_SUCCESS)
661 {
662 goto err;
663 }
664 result = Tspi_TPM_PcrRead(hTPM, pcr_num, (UINT32*)&rgbPcrValue.len, &rgbPcrValue.ptr);
665 if (result != TSS_SUCCESS)
666 {
667 goto err;
668 }
669 *pcr_value = chunk_clone(rgbPcrValue);
670 DBG3(DBG_PTS, "PCR %d value:%B", pcr_num, pcr_value);
671 success = TRUE;
672
673 err:
674 if (!success)
675 {
676 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
677 }
678 Tspi_Context_FreeMemory(hContext, NULL);
679 Tspi_Context_Close(hContext);
680
681 return success;
682 }
683
684 METHOD(pts_t, extend_pcr, bool,
685 private_pts_t *this, u_int32_t pcr_num, chunk_t input, chunk_t *output)
686 {
687 TSS_HCONTEXT hContext;
688 TSS_HTPM hTPM;
689 TSS_RESULT result;
690 u_int32_t pcr_length;
691 chunk_t pcr_value = chunk_empty;
692
693 result = Tspi_Context_Create(&hContext);
694 if (result != TSS_SUCCESS)
695 {
696 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
697 result);
698 return FALSE;
699 }
700 result = Tspi_Context_Connect(hContext, NULL);
701 if (result != TSS_SUCCESS)
702 {
703 goto err;
704 }
705 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
706 if (result != TSS_SUCCESS)
707 {
708 goto err;
709 }
710
711 pcr_value = chunk_alloc(PTS_PCR_LEN);
712 result = Tspi_TPM_PcrExtend(hTPM, pcr_num, PTS_PCR_LEN, input.ptr,
713 NULL, &pcr_length, &pcr_value.ptr);
714 if (result != TSS_SUCCESS)
715 {
716 goto err;
717 }
718
719 *output = pcr_value;
720 *output = chunk_clone(*output);
721
722 DBG3(DBG_PTS, "PCR %d extended with: %B", pcr_num, &input);
723 DBG3(DBG_PTS, "PCR %d value after extend: %B", pcr_num, output);
724
725 chunk_clear(&pcr_value);
726 Tspi_Context_FreeMemory(hContext, NULL);
727 Tspi_Context_Close(hContext);
728
729 return TRUE;
730
731 err:
732 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
733
734 chunk_clear(&pcr_value);
735 Tspi_Context_FreeMemory(hContext, NULL);
736 Tspi_Context_Close(hContext);
737
738 return FALSE;
739 }
740
741 METHOD(pts_t, quote_tpm, bool,
742 private_pts_t *this, bool use_quote2, chunk_t *pcr_comp, chunk_t *quote_sig)
743 {
744 TSS_HCONTEXT hContext;
745 TSS_HTPM hTPM;
746 TSS_HKEY hAIK;
747 TSS_HKEY hSRK;
748 TSS_HPOLICY srkUsagePolicy;
749 TSS_UUID SRK_UUID = TSS_UUID_SRK;
750 BYTE secret[] = TSS_WELL_KNOWN_SECRET;
751 TSS_HPCRS hPcrComposite;
752 TSS_VALIDATION valData;
753 TSS_RESULT result;
754 chunk_t quote_info;
755 BYTE* versionInfo;
756 u_int32_t versionInfoSize, pcr;
757 enumerator_t *enumerator;
758 bool success = FALSE;
759
760 result = Tspi_Context_Create(&hContext);
761 if (result != TSS_SUCCESS)
762 {
763 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
764 result);
765 return FALSE;
766 }
767 result = Tspi_Context_Connect(hContext, NULL);
768 if (result != TSS_SUCCESS)
769 {
770 goto err1;
771 }
772 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
773 if (result != TSS_SUCCESS)
774 {
775 goto err1;
776 }
777
778 /* Retrieve SRK from TPM and set the authentication to well known secret*/
779 result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
780 SRK_UUID, &hSRK);
781 if (result != TSS_SUCCESS)
782 {
783 goto err1;
784 }
785
786 result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
787 if (result != TSS_SUCCESS)
788 {
789 goto err1;
790 }
791 result = Tspi_Policy_SetSecret(srkUsagePolicy, TSS_SECRET_MODE_SHA1,
792 20, secret);
793 if (result != TSS_SUCCESS)
794 {
795 goto err1;
796 }
797
798 result = Tspi_Context_LoadKeyByBlob (hContext, hSRK, this->aik_blob.len,
799 this->aik_blob.ptr, &hAIK);
800 if (result != TSS_SUCCESS)
801 {
802 goto err1;
803 }
804
805 /* Create PCR composite object */
806 result = use_quote2 ?
807 Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
808 TSS_PCRS_STRUCT_INFO_SHORT, &hPcrComposite) :
809 Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
810 TSS_PCRS_STRUCT_DEFAULT, &hPcrComposite);
811 if (result != TSS_SUCCESS)
812 {
813 goto err2;
814 }
815
816 /* Select PCRs */
817 enumerator = this->pcrs->create_enumerator(this->pcrs);
818 while (enumerator->enumerate(enumerator, &pcr))
819 {
820 result = use_quote2 ?
821 Tspi_PcrComposite_SelectPcrIndexEx(hPcrComposite, pcr,
822 TSS_PCRS_DIRECTION_RELEASE) :
823 Tspi_PcrComposite_SelectPcrIndex(hPcrComposite, pcr);
824 if (result != TSS_SUCCESS)
825 {
826 break;
827 }
828 }
829 enumerator->destroy(enumerator);
830
831 if (result != TSS_SUCCESS)
832 {
833 goto err3;
834 }
835
836 /* Set the Validation Data */
837 valData.ulExternalDataLength = this->secret.len;
838 valData.rgbExternalData = (BYTE *)this->secret.ptr;
839
840
841 /* TPM Quote */
842 result = use_quote2 ?
843 Tspi_TPM_Quote2(hTPM, hAIK, FALSE, hPcrComposite, &valData,
844 &versionInfoSize, &versionInfo):
845 Tspi_TPM_Quote(hTPM, hAIK, hPcrComposite, &valData);
846 if (result != TSS_SUCCESS)
847 {
848 goto err4;
849 }
850
851 /* Set output chunks */
852 *pcr_comp = chunk_alloc(HASH_SIZE_SHA1);
853
854 if (use_quote2)
855 {
856 /* TPM_Composite_Hash is last 20 bytes of TPM_Quote_Info2 structure */
857 memcpy(pcr_comp->ptr, valData.rgbData + valData.ulDataLength - HASH_SIZE_SHA1,
858 HASH_SIZE_SHA1);
859 }
860 else
861 {
862 /* TPM_Composite_Hash is 8-28th bytes of TPM_Quote_Info structure */
863 memcpy(pcr_comp->ptr, valData.rgbData + 8, HASH_SIZE_SHA1);
864 }
865 DBG3(DBG_PTS, "Hash of PCR Composite: %#B", pcr_comp);
866
867 quote_info = chunk_create(valData.rgbData, valData.ulDataLength);
868 DBG3(DBG_PTS, "TPM Quote Info: %B",&quote_info);
869
870 *quote_sig = chunk_clone(chunk_create(valData.rgbValidationData,
871 valData.ulValidationDataLength));
872 DBG3(DBG_PTS, "TPM Quote Signature: %B",quote_sig);
873
874 success = TRUE;
875
876 /* Cleanup */
877 err4:
878 Tspi_Context_FreeMemory(hContext, NULL);
879
880 err3:
881 Tspi_Context_CloseObject(hContext, hPcrComposite);
882
883 err2:
884 Tspi_Context_CloseObject(hContext, hAIK);
885
886 err1:
887 Tspi_Context_Close(hContext);
888 if (!success)
889 {
890 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
891 }
892 return success;
893 }
894
895 #else /* TSS_TROUSERS */
896
897 METHOD(pts_t, read_pcr, bool,
898 private_pts_t *this, u_int32_t pcr_num, chunk_t *pcr_value)
899 {
900 return FALSE;
901 }
902
903 METHOD(pts_t, extend_pcr, bool,
904 private_pts_t *this, u_int32_t pcr_num, chunk_t input, chunk_t *output)
905 {
906 return FALSE;
907 }
908
909 METHOD(pts_t, quote_tpm, bool,
910 private_pts_t *this, bool use_quote2, chunk_t *pcr_comp, chunk_t *quote_sig)
911 {
912 return FALSE;
913 }
914
915 #endif /* TSS_TROUSERS */
916
917 /**
918 * TPM_QUOTE_INFO structure:
919 * 4 bytes of version
920 * 4 bytes 'Q' 'U' 'O' 'T'
921 * 20 byte SHA1 of TCPA_PCR_COMPOSITE
922 * 20 byte nonce
923 *
924 * TPM_QUOTE_INFO2 structure:
925 * 2 bytes Tag 0x0036 TPM_Tag_Quote_info2
926 * 4 bytes 'Q' 'U' 'T' '2'
927 * 20 bytes nonce
928 * 26 bytes PCR_INFO_SHORT
929 */
930
931 METHOD(pts_t, get_quote_info, bool,
932 private_pts_t *this, bool use_quote2, bool use_ver_info,
933 pts_meas_algorithms_t comp_hash_algo,
934 chunk_t *out_pcr_comp, chunk_t *out_quote_info)
935 {
936 chunk_t selection, pcr_comp, hash_pcr_comp;
937 bio_writer_t *writer;
938 hasher_t *hasher;
939
940 if (!this->pcrs->get_count(this->pcrs))
941 {
942 DBG1(DBG_PTS, "No extended PCR entries available, "
943 "unable to construct TPM Quote Info");
944 return FALSE;
945 }
946 if (!this->secret.ptr)
947 {
948 DBG1(DBG_PTS, "Secret assessment value unavailable, ",
949 "unable to construct TPM Quote Info");
950 return FALSE;
951 }
952 if (use_quote2 && use_ver_info && !this->tpm_version_info.ptr)
953 {
954 DBG1(DBG_PTS, "TPM Version Information unavailable, ",
955 "unable to construct TPM Quote Info2");
956 return FALSE;
957 }
958
959 pcr_comp = this->pcrs->get_composite(this->pcrs);
960
961
962 /* Output the TPM_PCR_COMPOSITE expected from IMC */
963 if (comp_hash_algo)
964 {
965 hash_algorithm_t algo;
966
967 algo = pts_meas_algo_to_hash(comp_hash_algo);
968 hasher = lib->crypto->create_hasher(lib->crypto, algo);
969
970 /* Hash the PCR Composite Structure */
971 if (!hasher || !hasher->allocate_hash(hasher, pcr_comp, out_pcr_comp))
972 {
973 DESTROY_IF(hasher);
974 free(pcr_comp.ptr);
975 return FALSE;
976 }
977 DBG3(DBG_PTS, "constructed PCR Composite hash: %#B", out_pcr_comp);
978 hasher->destroy(hasher);
979 }
980 else
981 {
982 *out_pcr_comp = chunk_clone(pcr_comp);
983 }
984
985 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
986 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
987 if (!hasher || !hasher->allocate_hash(hasher, pcr_comp, &hash_pcr_comp))
988 {
989 DESTROY_IF(hasher);
990 chunk_free(out_pcr_comp);
991 free(pcr_comp.ptr);
992 return FALSE;
993 }
994 hasher->destroy(hasher);
995
996 /* Construct TPM_QUOTE_INFO/TPM_QUOTE_INFO2 structure */
997 writer = bio_writer_create(TPM_QUOTE_INFO_LEN);
998
999 if (use_quote2)
1000 {
1001 /* TPM Structure Tag */
1002 writer->write_uint16(writer, TPM_TAG_QUOTE_INFO2);
1003
1004 /* Magic QUT2 value */
1005 writer->write_data(writer, chunk_create("QUT2", 4));
1006
1007 /* Secret assessment value 20 bytes (nonce) */
1008 writer->write_data(writer, this->secret);
1009
1010 /* PCR selection */
1011 selection.ptr = pcr_comp.ptr;
1012 selection.len = 2 + this->pcrs->get_selection_size(this->pcrs);
1013 writer->write_data(writer, selection);
1014
1015 /* TPM Locality Selection */
1016 writer->write_uint8(writer, TPM_LOC_ZERO);
1017
1018 /* PCR Composite Hash */
1019 writer->write_data(writer, hash_pcr_comp);
1020
1021 if (use_ver_info)
1022 {
1023 /* TPM version Info */
1024 writer->write_data(writer, this->tpm_version_info);
1025 }
1026 }
1027 else
1028 {
1029 /* Version number */
1030 writer->write_data(writer, chunk_from_chars(1, 1, 0, 0));
1031
1032 /* Magic QUOT value */
1033 writer->write_data(writer, chunk_create("QUOT", 4));
1034
1035 /* PCR Composite Hash */
1036 writer->write_data(writer, hash_pcr_comp);
1037
1038 /* Secret assessment value 20 bytes (nonce) */
1039 writer->write_data(writer, this->secret);
1040 }
1041
1042 /* TPM Quote Info */
1043 *out_quote_info = chunk_clone(writer->get_buf(writer));
1044 DBG3(DBG_PTS, "constructed TPM Quote Info: %B", out_quote_info);
1045
1046 writer->destroy(writer);
1047 free(pcr_comp.ptr);
1048 free(hash_pcr_comp.ptr);
1049
1050 return TRUE;
1051 }
1052
1053 METHOD(pts_t, verify_quote_signature, bool,
1054 private_pts_t *this, chunk_t data, chunk_t signature)
1055 {
1056 public_key_t *aik_pub_key;
1057
1058 aik_pub_key = this->aik->get_public_key(this->aik);
1059 if (!aik_pub_key)
1060 {
1061 DBG1(DBG_PTS, "failed to get public key from AIK certificate");
1062 return FALSE;
1063 }
1064
1065 if (!aik_pub_key->verify(aik_pub_key, SIGN_RSA_EMSA_PKCS1_SHA1,
1066 data, signature))
1067 {
1068 DBG1(DBG_PTS, "signature verification failed for TPM Quote Info");
1069 DESTROY_IF(aik_pub_key);
1070 return FALSE;
1071 }
1072
1073 aik_pub_key->destroy(aik_pub_key);
1074 return TRUE;
1075 }
1076
1077 METHOD(pts_t, get_pcrs, pts_pcr_t*,
1078 private_pts_t *this)
1079 {
1080 return this->pcrs;
1081 }
1082
1083 METHOD(pts_t, destroy, void,
1084 private_pts_t *this)
1085 {
1086 DESTROY_IF(this->pcrs);
1087 DESTROY_IF(this->aik);
1088 DESTROY_IF(this->dh);
1089 free(this->initiator_nonce.ptr);
1090 free(this->responder_nonce.ptr);
1091 free(this->secret.ptr);
1092 free(this->platform_info);
1093 free(this->aik_blob.ptr);
1094 free(this->tpm_version_info.ptr);
1095 free(this);
1096 }
1097
1098
1099 #ifdef TSS_TROUSERS
1100
1101 /**
1102 * Check for a TPM by querying for TPM Version Info
1103 */
1104 static bool has_tpm(private_pts_t *this)
1105 {
1106 TSS_HCONTEXT hContext;
1107 TSS_HTPM hTPM;
1108 TSS_RESULT result;
1109 u_int32_t version_info_len;
1110
1111 result = Tspi_Context_Create(&hContext);
1112 if (result != TSS_SUCCESS)
1113 {
1114 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
1115 result);
1116 return FALSE;
1117 }
1118 result = Tspi_Context_Connect(hContext, NULL);
1119 if (result != TSS_SUCCESS)
1120 {
1121 goto err;
1122 }
1123 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
1124 if (result != TSS_SUCCESS)
1125 {
1126 goto err;
1127 }
1128 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
1129 &version_info_len,
1130 &this->tpm_version_info.ptr);
1131 this->tpm_version_info.len = version_info_len;
1132 if (result != TSS_SUCCESS)
1133 {
1134 goto err;
1135 }
1136 this->tpm_version_info = chunk_clone(this->tpm_version_info);
1137
1138 Tspi_Context_FreeMemory(hContext, NULL);
1139 Tspi_Context_Close(hContext);
1140 return TRUE;
1141
1142 err:
1143 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
1144 Tspi_Context_FreeMemory(hContext, NULL);
1145 Tspi_Context_Close(hContext);
1146 return FALSE;
1147 }
1148
1149 #else /* TSS_TROUSERS */
1150
1151 static bool has_tpm(private_pts_t *this)
1152 {
1153 return FALSE;
1154 }
1155
1156 #endif /* TSS_TROUSERS */
1157
1158
1159 /**
1160 * See header
1161 */
1162 pts_t *pts_create(bool is_imc)
1163 {
1164 private_pts_t *this;
1165 pts_pcr_t *pcrs;
1166
1167 pcrs = pts_pcr_create();
1168 if (!pcrs)
1169 {
1170 DBG1(DBG_PTS, "shadow PCR set could not be created");
1171 return NULL;
1172 }
1173
1174 INIT(this,
1175 .public = {
1176 .get_proto_caps = _get_proto_caps,
1177 .set_proto_caps = _set_proto_caps,
1178 .get_meas_algorithm = _get_meas_algorithm,
1179 .set_meas_algorithm = _set_meas_algorithm,
1180 .get_dh_hash_algorithm = _get_dh_hash_algorithm,
1181 .set_dh_hash_algorithm = _set_dh_hash_algorithm,
1182 .create_dh_nonce = _create_dh_nonce,
1183 .get_my_public_value = _get_my_public_value,
1184 .set_peer_public_value = _set_peer_public_value,
1185 .calculate_secret = _calculate_secret,
1186 .get_platform_info = _get_platform_info,
1187 .set_platform_info = _set_platform_info,
1188 .get_tpm_version_info = _get_tpm_version_info,
1189 .set_tpm_version_info = _set_tpm_version_info,
1190 .get_aik = _get_aik,
1191 .set_aik = _set_aik,
1192 .get_aik_keyid = _get_aik_keyid,
1193 .is_path_valid = _is_path_valid,
1194 .get_metadata = _get_metadata,
1195 .read_pcr = _read_pcr,
1196 .extend_pcr = _extend_pcr,
1197 .quote_tpm = _quote_tpm,
1198 .get_pcrs = _get_pcrs,
1199 .get_quote_info = _get_quote_info,
1200 .verify_quote_signature = _verify_quote_signature,
1201 .destroy = _destroy,
1202 },
1203 .is_imc = is_imc,
1204 .proto_caps = PTS_PROTO_CAPS_V,
1205 .algorithm = PTS_MEAS_ALGO_SHA256,
1206 .dh_hash_algorithm = PTS_MEAS_ALGO_SHA256,
1207 .pcrs = pcrs,
1208 );
1209
1210 if (is_imc)
1211 {
1212 if (has_tpm(this))
1213 {
1214 this->has_tpm = TRUE;
1215 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_D;
1216 load_aik(this);
1217 load_aik_blob(this);
1218 }
1219 }
1220 else
1221 {
1222 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_D;
1223 }
1224
1225 return &this->public;
1226 }