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