2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
19 #include <crypto/hashers/hasher.h>
20 #include <bio/bio_writer.h>
21 #include <bio/bio_reader.h>
23 #include <trousers/tss.h>
24 #include <trousers/trousers.h>
27 #include <sys/utsname.h>
30 #define PTS_BUF_SIZE 4096
33 * Maximum number of PCR's of TPM, TPM Spec 1.2
35 #define PCR_MAX_NUM 24
38 * Number of bytes that can be saved in a PCR of TPM, TPM Spec 1.2
42 typedef struct private_pts_t private_pts_t
;
45 * Private data of a pts_t object.
48 struct private_pts_t
{
51 * Public pts_t interface.
56 * PTS Protocol Capabilities
58 pts_proto_caps_flag_t proto_caps
;
61 * PTS Measurement Algorithm
63 pts_meas_algorithms_t algorithm
;
68 pts_meas_algorithms_t dh_hash_algorithm
;
71 * PTS Diffie-Hellman Secret
76 * PTS Diffie-Hellman Initiator Nonce
78 chunk_t initiator_nonce
;
81 * PTS Diffie-Hellman Responder Nonce
83 chunk_t responder_nonce
;
86 * Secret assessment value to be used for TPM Quote as an external data
91 * Platform and OS Info
96 * TRUE if IMC-PTS, FALSE if IMV-PTS
101 * Do we have an activated TPM
106 * Contains a TPM_CAP_VERSION_INFO struct
108 chunk_t tpm_version_info
;
111 * Contains TSS Blob structure for AIK
116 * Contains a Attestation Identity Key or Certificate
121 * Table of extended PCRs with corresponding values
123 u_char
* pcrs
[PCR_MAX_NUM
];
126 * Length of PCR registers
131 * Number of extended PCR registers
136 * Highest extended PCR register
141 * Bitmap of extended PCR registers
143 u_int8_t pcr_select
[PCR_MAX_NUM
/ 8];
147 METHOD(pts_t
, get_proto_caps
, pts_proto_caps_flag_t
,
150 return this->proto_caps
;
153 METHOD(pts_t
, set_proto_caps
, void,
154 private_pts_t
*this, pts_proto_caps_flag_t flags
)
156 this->proto_caps
= flags
;
157 DBG2(DBG_PTS
, "supported PTS protocol capabilities: %s%s%s%s%s",
158 flags
& PTS_PROTO_CAPS_C ?
"C" : ".",
159 flags
& PTS_PROTO_CAPS_V ?
"V" : ".",
160 flags
& PTS_PROTO_CAPS_D ?
"D" : ".",
161 flags
& PTS_PROTO_CAPS_T ?
"T" : ".",
162 flags
& PTS_PROTO_CAPS_X ?
"X" : ".");
165 METHOD(pts_t
, get_meas_algorithm
, pts_meas_algorithms_t
,
168 return this->algorithm
;
171 METHOD(pts_t
, set_meas_algorithm
, void,
172 private_pts_t
*this, pts_meas_algorithms_t algorithm
)
174 hash_algorithm_t hash_alg
;
176 hash_alg
= pts_meas_algo_to_hash(algorithm
);
177 DBG2(DBG_PTS
, "selected PTS measurement algorithm is %N",
178 hash_algorithm_names
, hash_alg
);
179 if (hash_alg
!= HASH_UNKNOWN
)
181 this->algorithm
= algorithm
;
185 METHOD(pts_t
, get_dh_hash_algorithm
, pts_meas_algorithms_t
,
188 return this->dh_hash_algorithm
;
191 METHOD(pts_t
, set_dh_hash_algorithm
, void,
192 private_pts_t
*this, pts_meas_algorithms_t algorithm
)
194 hash_algorithm_t hash_alg
;
196 hash_alg
= pts_meas_algo_to_hash(algorithm
);
197 DBG2(DBG_PTS
, "selected DH hash algorithm is %N",
198 hash_algorithm_names
, hash_alg
);
199 if (hash_alg
!= HASH_UNKNOWN
)
201 this->dh_hash_algorithm
= algorithm
;
206 METHOD(pts_t
, create_dh_nonce
, bool,
207 private_pts_t
*this, pts_dh_group_t group
, int nonce_len
)
209 diffie_hellman_group_t dh_group
;
213 dh_group
= pts_dh_group_to_ike(group
);
214 DBG2(DBG_PTS
, "selected PTS DH group is %N",
215 diffie_hellman_group_names
, dh_group
);
216 DESTROY_IF(this->dh
);
217 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, dh_group
);
219 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_STRONG
);
222 DBG1(DBG_PTS
, "no rng available");
225 DBG2(DBG_PTS
, "nonce length is %d", nonce_len
);
226 nonce
= this->is_imc ?
&this->responder_nonce
: &this->initiator_nonce
;
228 rng
->allocate_bytes(rng
, nonce_len
, nonce
);
234 METHOD(pts_t
, get_my_public_value
, void,
235 private_pts_t
*this, chunk_t
*value
, chunk_t
*nonce
)
237 this->dh
->get_my_public_value(this->dh
, value
);
238 *nonce
= this->is_imc ?
this->responder_nonce
: this->initiator_nonce
;
241 METHOD(pts_t
, set_peer_public_value
, void,
242 private_pts_t
*this, chunk_t value
, chunk_t nonce
)
244 this->dh
->set_other_public_value(this->dh
, value
);
246 nonce
= chunk_clone(nonce
);
249 this->initiator_nonce
= nonce
;
253 this->responder_nonce
= nonce
;
257 METHOD(pts_t
, calculate_secret
, bool,
261 hash_algorithm_t hash_alg
;
262 chunk_t shared_secret
;
264 /* Check presence of nonces */
265 if (!this->initiator_nonce
.len
|| !this->responder_nonce
.len
)
267 DBG1(DBG_PTS
, "initiator and/or responder nonce is not available");
270 DBG3(DBG_PTS
, "initiator nonce: %B", &this->initiator_nonce
);
271 DBG3(DBG_PTS
, "responder nonce: %B", &this->responder_nonce
);
273 /* Calculate the DH secret */
274 if (this->dh
->get_shared_secret(this->dh
, &shared_secret
) != SUCCESS
)
276 DBG1(DBG_PTS
, "shared DH secret computation failed");
279 DBG3(DBG_PTS
, "shared DH secret: %B", &shared_secret
);
281 /* Calculate the secret assessment value */
282 hash_alg
= pts_meas_algo_to_hash(this->dh_hash_algorithm
);
283 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
285 hasher
->allocate_hash(hasher
, chunk_from_chars('1'), NULL
);
286 hasher
->allocate_hash(hasher
, this->initiator_nonce
, NULL
);
287 hasher
->allocate_hash(hasher
, this->responder_nonce
, NULL
);
288 hasher
->allocate_hash(hasher
, shared_secret
, &this->secret
);
289 hasher
->destroy(hasher
);
291 /* The DH secret must be destroyed */
292 chunk_clear(&shared_secret
);
295 * Truncate the hash to 20 bytes to fit the ExternalData
296 * argument of the TPM Quote command
298 this->secret
.len
= min(this->secret
.len
, 20);
299 DBG3(DBG_PTS
, "secret assessment value: %B", &this->secret
);
304 * Print TPM 1.2 Version Info
306 static void print_tpm_version_info(private_pts_t
*this)
308 TPM_CAP_VERSION_INFO versionInfo
;
312 result
= Trspi_UnloadBlob_CAP_VERSION_INFO(&offset
,
313 this->tpm_version_info
.ptr
, &versionInfo
);
314 if (result
!= TSS_SUCCESS
)
316 DBG1(DBG_PTS
, "could not parse tpm version info: tss error 0x%x",
321 DBG2(DBG_PTS
, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
322 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
323 versionInfo
.version
.major
, versionInfo
.version
.minor
,
324 versionInfo
.version
.revMajor
, versionInfo
.version
.revMinor
,
325 versionInfo
.specLevel
, versionInfo
.errataRev
,
326 versionInfo
.tpmVendorID
);
330 METHOD(pts_t
, get_platform_info
, char*,
333 return this->platform_info
;
336 METHOD(pts_t
, set_platform_info
, void,
337 private_pts_t
*this, char *info
)
339 free(this->platform_info
);
340 this->platform_info
= strdup(info
);
343 METHOD(pts_t
, get_tpm_version_info
, bool,
344 private_pts_t
*this, chunk_t
*info
)
350 *info
= this->tpm_version_info
;
351 print_tpm_version_info(this);
355 METHOD(pts_t
, set_tpm_version_info
, void,
356 private_pts_t
*this, chunk_t info
)
358 this->tpm_version_info
= chunk_clone(info
);
359 print_tpm_version_info(this);
362 METHOD(pts_t
, get_pcr_len
, size_t,
365 return this->pcr_len
;
369 * Load an AIK Blob (TSS_TSPATTRIB_KEYBLOB_BLOB attribute)
371 static void load_aik_blob(private_pts_t
*this)
375 u_int32_t aikBlobLen
;
377 blob_path
= lib
->settings
->get_str(lib
->settings
,
378 "libimcv.plugins.imc-attestation.aik_blob", NULL
);
382 /* Read aik key blob from a file */
383 if ((fp
= fopen(blob_path
, "r")) == NULL
)
385 DBG1(DBG_PTS
, "unable to open AIK Blob file: %s", blob_path
);
389 fseek(fp
, 0, SEEK_END
);
390 aikBlobLen
= ftell(fp
);
391 fseek(fp
, 0L, SEEK_SET
);
393 this->aik_blob
= chunk_alloc(aikBlobLen
);
394 if (fread(this->aik_blob
.ptr
, 1, aikBlobLen
, fp
))
396 DBG2(DBG_PTS
, "loaded AIK Blob from '%s'", blob_path
);
397 DBG3(DBG_PTS
, "AIK Blob: %B", &this->aik_blob
);
401 DBG1(DBG_PTS
, "unable to read AIK Blob file '%s'", blob_path
);
407 DBG1(DBG_PTS
, "AIK Blob is not available");
411 * Load an AIK certificate or public key
412 * the certificate having precedence over the public key if both are present
414 static void load_aik(private_pts_t
*this)
416 char *cert_path
, *key_path
;
418 cert_path
= lib
->settings
->get_str(lib
->settings
,
419 "libimcv.plugins.imc-attestation.aik_cert", NULL
);
420 key_path
= lib
->settings
->get_str(lib
->settings
,
421 "libimcv.plugins.imc-attestation.aik_key", NULL
);
425 this->aik
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
426 CERT_X509
, BUILD_FROM_FILE
,
427 cert_path
, BUILD_END
);
430 DBG2(DBG_PTS
, "loaded AIK certificate from '%s'", cert_path
);
436 this->aik
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
437 CERT_TRUSTED_PUBKEY
, BUILD_FROM_FILE
,
438 key_path
, BUILD_END
);
441 DBG2(DBG_PTS
, "loaded AIK public key from '%s'", key_path
);
446 DBG1(DBG_PTS
, "neither AIK certificate nor public key is available");
449 METHOD(pts_t
, get_aik
, certificate_t
*,
455 METHOD(pts_t
, set_aik
, void,
456 private_pts_t
*this, certificate_t
*aik
)
458 DESTROY_IF(this->aik
);
459 this->aik
= aik
->get_ref(aik
);
462 METHOD(pts_t
, get_aik_keyid
, bool,
463 private_pts_t
*this, chunk_t
*keyid
)
465 public_key_t
*public;
470 DBG1(DBG_PTS
, "no AIK certificate available");
473 public = this->aik
->get_public_key(this->aik
);
476 DBG1(DBG_PTS
, "no AIK public key available");
479 success
= public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1
, keyid
);
482 DBG1(DBG_PTS
, "no SHA-1 AIK public key info ID available");
484 public->destroy(public);
489 METHOD(pts_t
, hash_file
, bool,
490 private_pts_t
*this, hasher_t
*hasher
, char *pathname
, u_char
*hash
)
492 u_char buffer
[PTS_BUF_SIZE
];
496 file
= fopen(pathname
, "rb");
499 DBG1(DBG_PTS
," file '%s' can not be opened, %s", pathname
,
505 bytes_read
= fread(buffer
, 1, sizeof(buffer
), file
);
508 hasher
->get_hash(hasher
, chunk_create(buffer
, bytes_read
), NULL
);
512 hasher
->get_hash(hasher
, chunk_empty
, hash
);
522 * Get the relative filename of a fully qualified file pathname
524 static char* get_filename(char *pathname
)
526 char *pos
, *filename
;
528 pos
= filename
= pathname
;
529 while (pos
&& *(++pos
) != '\0')
532 pos
= strchr(filename
, '/');
537 METHOD(pts_t
, is_path_valid
, bool,
538 private_pts_t
*this, char *path
, pts_error_code_t
*error_code
)
544 if (!stat(path
, &st
))
548 else if (errno
== ENOENT
|| errno
== ENOTDIR
)
550 DBG1(DBG_PTS
, "file/directory does not exist %s", path
);
551 *error_code
= TCG_PTS_FILE_NOT_FOUND
;
553 else if (errno
== EFAULT
)
555 DBG1(DBG_PTS
, "bad address %s", path
);
556 *error_code
= TCG_PTS_INVALID_PATH
;
560 DBG1(DBG_PTS
, "error: %s occured while validating path: %s",
561 strerror(errno
), path
);
568 METHOD(pts_t
, do_measurements
, pts_file_meas_t
*,
569 private_pts_t
*this, u_int16_t request_id
, char *pathname
, bool is_directory
)
572 hash_algorithm_t hash_alg
;
573 u_char hash
[HASH_SIZE_SHA384
];
575 pts_file_meas_t
*measurements
;
577 /* Create a hasher */
578 hash_alg
= pts_meas_algo_to_hash(this->algorithm
);
579 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
582 DBG1(DBG_PTS
, "hasher %N not available", hash_algorithm_names
, hash_alg
);
586 /* Create a measurement object */
587 measurements
= pts_file_meas_create(request_id
);
589 /* Link the hash to the measurement and set the measurement length */
590 measurement
= chunk_create(hash
, hasher
->get_hash_size(hasher
));
594 enumerator_t
*enumerator
;
595 char *rel_name
, *abs_name
;
598 enumerator
= enumerator_create_directory(pathname
);
601 DBG1(DBG_PTS
," directory '%s' can not be opened, %s", pathname
,
603 hasher
->destroy(hasher
);
604 measurements
->destroy(measurements
);
607 while (enumerator
->enumerate(enumerator
, &rel_name
, &abs_name
, &st
))
609 /* measure regular files only */
610 if (S_ISREG(st
.st_mode
) && *rel_name
!= '.')
612 if (!hash_file(this, hasher
, abs_name
, hash
))
614 enumerator
->destroy(enumerator
);
615 hasher
->destroy(hasher
);
616 measurements
->destroy(measurements
);
619 DBG2(DBG_PTS
, " %#B for '%s'", &measurement
, rel_name
);
620 measurements
->add(measurements
, rel_name
, measurement
);
623 enumerator
->destroy(enumerator
);
629 if (!hash_file(this, hasher
, pathname
, hash
))
631 hasher
->destroy(hasher
);
632 measurements
->destroy(measurements
);
635 filename
= get_filename(pathname
);
636 DBG2(DBG_PTS
, " %#B for '%s'", &measurement
, filename
);
637 measurements
->add(measurements
, filename
, measurement
);
639 hasher
->destroy(hasher
);
645 * Obtain statistical information describing a file
647 static bool file_metadata(char *pathname
, pts_file_metadata_t
**entry
)
650 pts_file_metadata_t
*this;
652 this = malloc_thing(pts_file_metadata_t
);
654 if (stat(pathname
, &st
))
656 DBG1(DBG_PTS
, "unable to obtain statistics about '%s'", pathname
);
660 if (S_ISREG(st
.st_mode
))
662 this->type
= PTS_FILE_REGULAR
;
664 else if (S_ISDIR(st
.st_mode
))
666 this->type
= PTS_FILE_DIRECTORY
;
668 else if (S_ISCHR(st
.st_mode
))
670 this->type
= PTS_FILE_CHAR_SPEC
;
672 else if (S_ISBLK(st
.st_mode
))
674 this->type
= PTS_FILE_BLOCK_SPEC
;
676 else if (S_ISFIFO(st
.st_mode
))
678 this->type
= PTS_FILE_FIFO
;
680 else if (S_ISLNK(st
.st_mode
))
682 this->type
= PTS_FILE_SYM_LINK
;
684 else if (S_ISSOCK(st
.st_mode
))
686 this->type
= PTS_FILE_SOCKET
;
690 this->type
= PTS_FILE_OTHER
;
693 this->filesize
= st
.st_size
;
694 this->created
= st
.st_ctime
;
695 this->modified
= st
.st_mtime
;
696 this->accessed
= st
.st_atime
;
697 this->owner
= st
.st_uid
;
698 this->group
= st
.st_gid
;
704 METHOD(pts_t
, get_metadata
, pts_file_meta_t
*,
705 private_pts_t
*this, char *pathname
, bool is_directory
)
707 pts_file_meta_t
*metadata
;
708 pts_file_metadata_t
*entry
;
710 /* Create a metadata object */
711 metadata
= pts_file_meta_create();
715 enumerator_t
*enumerator
;
716 char *rel_name
, *abs_name
;
719 enumerator
= enumerator_create_directory(pathname
);
722 DBG1(DBG_PTS
," directory '%s' can not be opened, %s", pathname
,
724 metadata
->destroy(metadata
);
727 while (enumerator
->enumerate(enumerator
, &rel_name
, &abs_name
, &st
))
729 /* measure regular files only */
730 if (S_ISREG(st
.st_mode
) && *rel_name
!= '.')
732 if (!file_metadata(abs_name
, &entry
))
734 enumerator
->destroy(enumerator
);
735 metadata
->destroy(metadata
);
738 entry
->filename
= strdup(rel_name
);
739 metadata
->add(metadata
, entry
);
742 enumerator
->destroy(enumerator
);
746 if (!file_metadata(pathname
, &entry
))
748 metadata
->destroy(metadata
);
751 entry
->filename
= strdup(get_filename(pathname
));
752 metadata
->add(metadata
, entry
);
758 METHOD(pts_t
, read_pcr
, bool,
759 private_pts_t
*this, u_int32_t pcr_num
, chunk_t
*pcr_value
)
761 TSS_HCONTEXT hContext
;
766 bool success
= FALSE
;
768 result
= Tspi_Context_Create(&hContext
);
769 if (result
!= TSS_SUCCESS
)
771 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x", result
);
775 result
= Tspi_Context_Connect(hContext
, NULL
);
776 if (result
!= TSS_SUCCESS
)
780 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
781 if (result
!= TSS_SUCCESS
)
785 result
= Tspi_TPM_PcrRead(hTPM
, pcr_num
, (UINT32
*)&rgbPcrValue
.len
, &rgbPcrValue
.ptr
);
786 if (result
!= TSS_SUCCESS
)
790 *pcr_value
= chunk_clone(rgbPcrValue
);
791 DBG3(DBG_PTS
, "PCR %d value:%B", pcr_num
, pcr_value
);
797 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
799 Tspi_Context_FreeMemory(hContext
, NULL
);
800 Tspi_Context_Close(hContext
);
805 METHOD(pts_t
, extend_pcr
, bool,
806 private_pts_t
*this, u_int32_t pcr_num
, chunk_t input
, chunk_t
*output
)
808 TSS_HCONTEXT hContext
;
811 u_int32_t pcr_length
;
814 result
= Tspi_Context_Create(&hContext
);
815 if (result
!= TSS_SUCCESS
)
817 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
821 result
= Tspi_Context_Connect(hContext
, NULL
);
822 if (result
!= TSS_SUCCESS
)
826 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
827 if (result
!= TSS_SUCCESS
)
832 pcr_value
= chunk_alloc(PCR_LEN
);
833 result
= Tspi_TPM_PcrExtend(hTPM
, pcr_num
, PCR_LEN
, input
.ptr
,
834 NULL
, &pcr_length
, &pcr_value
.ptr
);
835 if (result
!= TSS_SUCCESS
)
841 *output
= chunk_clone(*output
);
843 DBG3(DBG_PTS
, "PCR %d extended with: %B", pcr_num
, &input
);
844 DBG3(DBG_PTS
, "PCR %d value after extend: %B", pcr_num
, output
);
846 chunk_clear(&pcr_value
);
847 Tspi_Context_FreeMemory(hContext
, NULL
);
848 Tspi_Context_Close(hContext
);
853 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
855 chunk_clear(&pcr_value
);
856 Tspi_Context_FreeMemory(hContext
, NULL
);
857 Tspi_Context_Close(hContext
);
863 static void clear_pcrs(private_pts_t
*this)
867 for (i
= 0; i
<= this->pcr_max
; i
++)
870 this->pcrs
[i
] = NULL
;
875 memset(this->pcr_select
, 0x00, sizeof(this->pcr_select
));
878 METHOD(pts_t
, quote_tpm
, bool,
879 private_pts_t
*this, bool use_quote2
, chunk_t
*pcr_comp
, chunk_t
*quote_sig
)
881 TSS_HCONTEXT hContext
;
885 TSS_HPOLICY srkUsagePolicy
;
886 TSS_UUID SRK_UUID
= TSS_UUID_SRK
;
887 BYTE secret
[] = TSS_WELL_KNOWN_SECRET
;
888 TSS_HPCRS hPcrComposite
;
889 TSS_VALIDATION valData
;
893 u_int32_t versionInfoSize
, pcr
, i
= 0, f
= 1;
894 bool success
= FALSE
;
896 result
= Tspi_Context_Create(&hContext
);
897 if (result
!= TSS_SUCCESS
)
899 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
903 result
= Tspi_Context_Connect(hContext
, NULL
);
904 if (result
!= TSS_SUCCESS
)
908 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
909 if (result
!= TSS_SUCCESS
)
914 /* Retrieve SRK from TPM and set the authentication to well known secret*/
915 result
= Tspi_Context_LoadKeyByUUID(hContext
, TSS_PS_TYPE_SYSTEM
,
917 if (result
!= TSS_SUCCESS
)
922 result
= Tspi_GetPolicyObject(hSRK
, TSS_POLICY_USAGE
, &srkUsagePolicy
);
923 if (result
!= TSS_SUCCESS
)
927 result
= Tspi_Policy_SetSecret(srkUsagePolicy
, TSS_SECRET_MODE_SHA1
,
929 if (result
!= TSS_SUCCESS
)
934 result
= Tspi_Context_LoadKeyByBlob (hContext
, hSRK
, this->aik_blob
.len
,
935 this->aik_blob
.ptr
, &hAIK
);
936 if (result
!= TSS_SUCCESS
)
941 /* Create PCR composite object */
942 result
= use_quote2 ?
943 Tspi_Context_CreateObject(hContext
, TSS_OBJECT_TYPE_PCRS
,
944 TSS_PCRS_STRUCT_INFO_SHORT
, &hPcrComposite
) :
945 Tspi_Context_CreateObject(hContext
, TSS_OBJECT_TYPE_PCRS
,
947 if (result
!= TSS_SUCCESS
)
953 for (pcr
= 0; pcr
<= this->pcr_max
; pcr
++)
960 if (this->pcr_select
[i
] & f
)
962 result
= use_quote2 ?
963 Tspi_PcrComposite_SelectPcrIndexEx(hPcrComposite
, pcr
,
964 TSS_PCRS_DIRECTION_RELEASE
) :
965 Tspi_PcrComposite_SelectPcrIndex(hPcrComposite
, pcr
);
966 if (result
!= TSS_SUCCESS
)
974 /* Set the Validation Data */
975 valData
.ulExternalDataLength
= this->secret
.len
;
976 valData
.rgbExternalData
= (BYTE
*)this->secret
.ptr
;
980 result
= use_quote2 ?
981 Tspi_TPM_Quote2(hTPM
, hAIK
, FALSE
, hPcrComposite
, &valData
,
982 &versionInfoSize
, &versionInfo
):
983 Tspi_TPM_Quote(hTPM
, hAIK
, hPcrComposite
, &valData
);
984 if (result
!= TSS_SUCCESS
)
989 /* Set output chunks */
990 *pcr_comp
= chunk_alloc(HASH_SIZE_SHA1
);
994 /* TPM_Composite_Hash is last 20 bytes of TPM_Quote_Info2 structure */
995 memcpy(pcr_comp
->ptr
, valData
.rgbData
+ valData
.ulDataLength
- HASH_SIZE_SHA1
,
1000 /* TPM_Composite_Hash is 8-28th bytes of TPM_Quote_Info structure */
1001 memcpy(pcr_comp
->ptr
, valData
.rgbData
+ 8, HASH_SIZE_SHA1
);
1003 DBG3(DBG_PTS
, "Hash of PCR Composite: %#B", pcr_comp
);
1005 quote_info
= chunk_create(valData
.rgbData
, valData
.ulDataLength
);
1006 DBG3(DBG_PTS
, "TPM Quote Info: %B","e_info
);
1008 *quote_sig
= chunk_clone(chunk_create(valData
.rgbValidationData
,
1009 valData
.ulValidationDataLength
));
1010 DBG3(DBG_PTS
, "TPM Quote Signature: %B",quote_sig
);
1016 Tspi_Context_FreeMemory(hContext
, NULL
);
1019 Tspi_Context_CloseObject(hContext
, hPcrComposite
);
1022 Tspi_Context_CloseObject(hContext
, hAIK
);
1025 Tspi_Context_Close(hContext
);
1029 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
1036 METHOD(pts_t
, select_pcr
, bool,
1037 private_pts_t
*this, u_int32_t pcr
)
1041 if (pcr
>= PCR_MAX_NUM
)
1043 DBG1(DBG_PTS
, "PCR %u: number is larger than maximum of %u",
1044 pcr
, PCR_MAX_NUM
-1);
1048 /* Determine PCR selection flag */
1050 f
= 1 << (pcr
- 8*i
);
1052 /* Has this PCR already been selected? */
1053 if (!(this->pcr_select
[i
] & f
))
1055 this->pcr_select
[i
] |= f
;
1056 this->pcr_max
= max(this->pcr_max
, pcr
);
1063 METHOD(pts_t
, add_pcr
, bool,
1064 private_pts_t
*this, u_int32_t pcr
, chunk_t pcr_before
, chunk_t pcr_after
)
1066 if (pcr
>= PCR_MAX_NUM
)
1068 DBG1(DBG_PTS
, "PCR %u: number is larger than maximum of %u",
1069 pcr
, PCR_MAX_NUM
-1);
1073 /* Is the length of the PCR registers already set? */
1076 if (pcr_after
.len
!= this->pcr_len
)
1078 DBG1(DBG_PTS
, "PCR %02u: length is %d bytes but should be %d bytes",
1079 pcr_after
.len
, this->pcr_len
);
1085 this->pcr_len
= pcr_after
.len
;
1088 /* Has the value of the PCR register already been assigned? */
1089 if (this->pcrs
[pcr
])
1091 if (!memeq(this->pcrs
[pcr
], pcr_before
.ptr
, this->pcr_len
))
1093 DBG1(DBG_PTS
, "PCR %02u: new pcr_before value does not equal "
1094 "old pcr_after value");
1096 /* remove the old PCR value */
1097 free(this->pcrs
[pcr
]);
1101 /* add extended PCR Register */
1102 this->pcr_select
[pcr
/ 8] |= 1 << (pcr
% 8);
1103 this->pcr_max
= max(this->pcr_max
, pcr
);
1107 /* Duplicate and store current PCR value */
1108 pcr_after
= chunk_clone(pcr_after
);
1109 this->pcrs
[pcr
] = pcr_after
.ptr
;
1115 * TPM_QUOTE_INFO structure:
1116 * 4 bytes of version
1117 * 4 bytes 'Q' 'U' 'O' 'T'
1118 * 20 byte SHA1 of TCPA_PCR_COMPOSITE
1121 * TPM_QUOTE_INFO2 structure:
1122 * 2 bytes Tag 0x0036 TPM_Tag_Quote_info2
1123 * 4 bytes 'Q' 'U' 'T' '2'
1125 * 26 bytes PCR_INFO_SHORT
1128 METHOD(pts_t
, get_quote_info
, bool,
1129 private_pts_t
*this, bool use_quote2
, bool use_ver_info
,
1130 pts_meas_algorithms_t comp_hash_algo
,
1131 chunk_t
*out_pcr_comp
, chunk_t
*out_quote_info
)
1133 u_int8_t size_of_select
;
1134 int pcr_comp_len
, i
;
1135 chunk_t pcr_comp
, hash_pcr_comp
;
1136 bio_writer_t
*writer
;
1139 if (this->pcr_count
== 0)
1141 DBG1(DBG_PTS
, "No extended PCR entries available, "
1142 "unable to construct TPM Quote Info");
1145 if (!this->secret
.ptr
)
1147 DBG1(DBG_PTS
, "Secret assessment value unavailable, ",
1148 "unable to construct TPM Quote Info");
1151 if (use_quote2
&& use_ver_info
&& !this->tpm_version_info
.ptr
)
1153 DBG1(DBG_PTS
, "TPM Version Information unavailable, ",
1154 "unable to construct TPM Quote Info2");
1159 * A TPM v1.2 has 24 PCR Registers
1160 * so the bitmask field length used by TrouSerS is at least 3 bytes
1162 size_of_select
= max(PCR_MAX_NUM
/ 8, 1 + this->pcr_max
/ 8);
1163 pcr_comp_len
= 2 + size_of_select
+ 4 + this->pcr_count
* this->pcr_len
;
1165 writer
= bio_writer_create(pcr_comp_len
);
1167 writer
->write_uint16(writer
, size_of_select
);
1168 for (i
= 0; i
< size_of_select
; i
++)
1170 writer
->write_uint8(writer
, this->pcr_select
[i
]);
1173 writer
->write_uint32(writer
, this->pcr_count
* this->pcr_len
);
1174 for (i
= 0; i
< 8 * size_of_select
; i
++)
1178 writer
->write_data(writer
, chunk_create(this->pcrs
[i
], this->pcr_len
));
1181 pcr_comp
= chunk_clone(writer
->get_buf(writer
));
1182 DBG3(DBG_PTS
, "constructed PCR Composite: %B", &pcr_comp
);
1184 writer
->destroy(writer
);
1186 /* Output the TPM_PCR_COMPOSITE expected from IMC */
1189 hash_algorithm_t algo
;
1191 algo
= pts_meas_algo_to_hash(comp_hash_algo
);
1192 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, algo
);
1194 /* Hash the PCR Composite Structure */
1195 hasher
->allocate_hash(hasher
, pcr_comp
, out_pcr_comp
);
1196 DBG3(DBG_PTS
, "constructed PCR Composite hash: %#B", out_pcr_comp
);
1197 hasher
->destroy(hasher
);
1201 *out_pcr_comp
= chunk_clone(pcr_comp
);
1204 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
1205 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1206 hasher
->allocate_hash(hasher
, pcr_comp
, &hash_pcr_comp
);
1207 hasher
->destroy(hasher
);
1209 /* Construct TPM_QUOTE_INFO/TPM_QUOTE_INFO2 structure */
1210 writer
= bio_writer_create(TPM_QUOTE_INFO_LEN
);
1214 /* TPM Structure Tag */
1215 writer
->write_uint16(writer
, TPM_TAG_QUOTE_INFO2
);
1217 /* Magic QUT2 value */
1218 writer
->write_data(writer
, chunk_create("QUT2", 4));
1220 /* Secret assessment value 20 bytes (nonce) */
1221 writer
->write_data(writer
, this->secret
);
1223 /* Length of the PCR selection field */
1224 writer
->write_uint16(writer
, size_of_select
);
1227 for (i
= 0; i
< size_of_select
; i
++)
1229 writer
->write_uint8(writer
, this->pcr_select
[i
]);
1232 /* TPM Locality Selection */
1233 writer
->write_uint8(writer
, TPM_LOC_ZERO
);
1235 /* PCR Composite Hash */
1236 writer
->write_data(writer
, hash_pcr_comp
);
1240 /* TPM version Info */
1241 writer
->write_data(writer
, this->tpm_version_info
);
1246 /* Version number */
1247 writer
->write_data(writer
, chunk_from_chars(1, 1, 0, 0));
1249 /* Magic QUOT value */
1250 writer
->write_data(writer
, chunk_create("QUOT", 4));
1252 /* PCR Composite Hash */
1253 writer
->write_data(writer
, hash_pcr_comp
);
1255 /* Secret assessment value 20 bytes (nonce) */
1256 writer
->write_data(writer
, this->secret
);
1259 /* TPM Quote Info */
1260 *out_quote_info
= chunk_clone(writer
->get_buf(writer
));
1261 DBG3(DBG_PTS
, "constructed TPM Quote Info: %B", out_quote_info
);
1263 writer
->destroy(writer
);
1265 free(hash_pcr_comp
.ptr
);
1271 METHOD(pts_t
, verify_quote_signature
, bool,
1272 private_pts_t
*this, chunk_t data
, chunk_t signature
)
1274 public_key_t
*aik_pub_key
;
1276 aik_pub_key
= this->aik
->get_public_key(this->aik
);
1279 DBG1(DBG_PTS
, "failed to get public key from AIK certificate");
1283 if (!aik_pub_key
->verify(aik_pub_key
, SIGN_RSA_EMSA_PKCS1_SHA1
,
1286 DBG1(DBG_PTS
, "signature verification failed for TPM Quote Info");
1287 DESTROY_IF(aik_pub_key
);
1291 aik_pub_key
->destroy(aik_pub_key
);
1295 METHOD(pts_t
, destroy
, void,
1296 private_pts_t
*this)
1299 DESTROY_IF(this->aik
);
1300 DESTROY_IF(this->dh
);
1301 free(this->initiator_nonce
.ptr
);
1302 free(this->responder_nonce
.ptr
);
1303 free(this->secret
.ptr
);
1304 free(this->platform_info
);
1305 free(this->aik_blob
.ptr
);
1306 free(this->tpm_version_info
.ptr
);
1310 #define RELEASE_LSB 0
1311 #define RELEASE_DEBIAN 1
1314 * Determine Linux distribution and hardware platform
1316 static char* extract_platform_info(void)
1319 char buf
[BUF_LEN
], *pos
= buf
, *value
= NULL
;
1320 int i
, len
= BUF_LEN
- 1;
1321 struct utsname uninfo
;
1323 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
1324 const char* releases
[] = {
1325 "/etc/lsb-release", "/etc/debian_version",
1326 "/etc/SuSE-release", "/etc/novell-release",
1327 "/etc/sles-release", "/etc/redhat-release",
1328 "/etc/fedora-release", "/etc/gentoo-release",
1329 "/etc/slackware-version", "/etc/annvix-release",
1330 "/etc/arch-release", "/etc/arklinux-release",
1331 "/etc/aurox-release", "/etc/blackcat-release",
1332 "/etc/cobalt-release", "/etc/conectiva-release",
1333 "/etc/debian_release", "/etc/immunix-release",
1334 "/etc/lfs-release", "/etc/linuxppc-release",
1335 "/etc/mandrake-release", "/etc/mandriva-release",
1336 "/etc/mandrakelinux-release", "/etc/mklinux-release",
1337 "/etc/pld-release", "/etc/redhat_version",
1338 "/etc/slackware-release", "/etc/e-smith-release",
1339 "/etc/release", "/etc/sun-release",
1340 "/etc/tinysofa-release", "/etc/turbolinux-release",
1341 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
1342 "/etc/va-release", "/etc/yellowdog-release"
1345 const char description
[] = "DISTRIB_DESCRIPTION=\"";
1346 const char str_debian
[] = "Debian ";
1348 for (i
= 0; i
< countof(releases
); i
++)
1350 file
= fopen(releases
[i
], "r");
1356 if (i
== RELEASE_DEBIAN
)
1358 strcpy(buf
, str_debian
);
1359 pos
+= strlen(str_debian
);
1360 len
-= strlen(str_debian
);
1363 fseek(file
, 0, SEEK_END
);
1364 len
= min(ftell(file
), len
);
1367 if (fread(pos
, 1, len
, file
) != len
)
1369 DBG1(DBG_PTS
, "failed to read file '%s'", releases
[i
]);
1375 if (i
== RELEASE_LSB
)
1377 pos
= strstr(buf
, description
);
1380 DBG1(DBG_PTS
, "failed to find begin of lsb-release "
1381 "DESCRIPTION field");
1384 value
= pos
+ strlen(description
);
1385 pos
= strchr(value
, '"');
1388 DBG1(DBG_PTS
, "failed to find end of lsb-release "
1389 "DESCRIPTION field");
1396 pos
= strchr(pos
, '\n');
1399 DBG1(DBG_PTS
, "failed to find end of release string");
1408 DBG1(DBG_PTS
, "no distribution release file found");
1412 if (uname(&uninfo
) < 0)
1414 DBG1(DBG_PTS
, "could not retrieve machine architecture");
1419 len
= sizeof(buf
)-1 + (pos
- buf
);
1420 strncpy(pos
, uninfo
.machine
, len
);
1422 DBG1(DBG_PTS
, "platform is '%s'", value
);
1423 return strdup(value
);
1427 * Check for a TPM by querying for TPM Version Info
1429 static bool has_tpm(private_pts_t
*this)
1431 TSS_HCONTEXT hContext
;
1434 u_int32_t version_info_len
;
1436 result
= Tspi_Context_Create(&hContext
);
1437 if (result
!= TSS_SUCCESS
)
1439 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
1443 result
= Tspi_Context_Connect(hContext
, NULL
);
1444 if (result
!= TSS_SUCCESS
)
1448 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
1449 if (result
!= TSS_SUCCESS
)
1453 result
= Tspi_TPM_GetCapability(hTPM
, TSS_TPMCAP_VERSION_VAL
, 0, NULL
,
1455 &this->tpm_version_info
.ptr
);
1456 this->tpm_version_info
.len
= version_info_len
;
1457 if (result
!= TSS_SUCCESS
)
1461 this->tpm_version_info
= chunk_clone(this->tpm_version_info
);
1463 Tspi_Context_FreeMemory(hContext
, NULL
);
1464 Tspi_Context_Close(hContext
);
1468 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
1469 Tspi_Context_FreeMemory(hContext
, NULL
);
1470 Tspi_Context_Close(hContext
);
1477 pts_t
*pts_create(bool is_imc
)
1479 private_pts_t
*this;
1483 .get_proto_caps
= _get_proto_caps
,
1484 .set_proto_caps
= _set_proto_caps
,
1485 .get_meas_algorithm
= _get_meas_algorithm
,
1486 .set_meas_algorithm
= _set_meas_algorithm
,
1487 .get_dh_hash_algorithm
= _get_dh_hash_algorithm
,
1488 .set_dh_hash_algorithm
= _set_dh_hash_algorithm
,
1489 .create_dh_nonce
= _create_dh_nonce
,
1490 .get_my_public_value
= _get_my_public_value
,
1491 .set_peer_public_value
= _set_peer_public_value
,
1492 .calculate_secret
= _calculate_secret
,
1493 .get_platform_info
= _get_platform_info
,
1494 .set_platform_info
= _set_platform_info
,
1495 .get_tpm_version_info
= _get_tpm_version_info
,
1496 .set_tpm_version_info
= _set_tpm_version_info
,
1497 .get_pcr_len
= _get_pcr_len
,
1498 .get_aik
= _get_aik
,
1499 .set_aik
= _set_aik
,
1500 .get_aik_keyid
= _get_aik_keyid
,
1501 .is_path_valid
= _is_path_valid
,
1502 .hash_file
= _hash_file
,
1503 .do_measurements
= _do_measurements
,
1504 .get_metadata
= _get_metadata
,
1505 .read_pcr
= _read_pcr
,
1506 .extend_pcr
= _extend_pcr
,
1507 .quote_tpm
= _quote_tpm
,
1508 .select_pcr
= _select_pcr
,
1509 .add_pcr
= _add_pcr
,
1510 .get_quote_info
= _get_quote_info
,
1511 .verify_quote_signature
= _verify_quote_signature
,
1512 .destroy
= _destroy
,
1515 .proto_caps
= PTS_PROTO_CAPS_V
,
1516 .algorithm
= PTS_MEAS_ALGO_SHA256
,
1517 .dh_hash_algorithm
= PTS_MEAS_ALGO_SHA256
,
1522 this->platform_info
= extract_platform_info();
1526 this->has_tpm
= TRUE
;
1527 this->pcr_len
= PCR_LEN
;
1528 this->proto_caps
|= PTS_PROTO_CAPS_T
| PTS_PROTO_CAPS_D
;
1530 load_aik_blob(this);
1535 this->proto_caps
|= PTS_PROTO_CAPS_T
| PTS_PROTO_CAPS_D
;
1538 return &this->public;