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