2 * Copyright (C) 2011-2012 Sansar Choinyambuu, Andreas Steffen
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>
26 #include <sys/types.h>
28 #include <sys/utsname.h>
34 * Maximum number of PCR's of TPM, TPM Spec 1.2
36 #define PCR_MAX_NUM 24
39 * Number of bytes that can be saved in a PCR of TPM, TPM Spec 1.2
43 typedef struct private_pts_t private_pts_t
;
46 * Private data of a pts_t object.
49 struct private_pts_t
{
52 * Public pts_t interface.
57 * PTS Protocol Capabilities
59 pts_proto_caps_flag_t proto_caps
;
62 * PTS Measurement Algorithm
64 pts_meas_algorithms_t algorithm
;
69 pts_meas_algorithms_t dh_hash_algorithm
;
72 * PTS Diffie-Hellman Secret
77 * PTS Diffie-Hellman Initiator Nonce
79 chunk_t initiator_nonce
;
82 * PTS Diffie-Hellman Responder Nonce
84 chunk_t responder_nonce
;
87 * Secret assessment value to be used for TPM Quote as an external data
92 * Platform and OS Info
97 * TRUE if IMC-PTS, FALSE if IMV-PTS
102 * Do we have an activated TPM
107 * Contains a TPM_CAP_VERSION_INFO struct
109 chunk_t tpm_version_info
;
112 * Contains TSS Blob structure for AIK
117 * Contains a Attestation Identity Key or Certificate
122 * Table of extended PCRs with corresponding values
124 u_char
* pcrs
[PCR_MAX_NUM
];
127 * Length of PCR registers
132 * Number of extended PCR registers
137 * Highest extended PCR register
142 * Bitmap of extended PCR registers
144 u_int8_t pcr_select
[PCR_MAX_NUM
/ 8];
148 METHOD(pts_t
, get_proto_caps
, pts_proto_caps_flag_t
,
151 return this->proto_caps
;
154 METHOD(pts_t
, set_proto_caps
, void,
155 private_pts_t
*this, pts_proto_caps_flag_t flags
)
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" : ".");
166 METHOD(pts_t
, get_meas_algorithm
, pts_meas_algorithms_t
,
169 return this->algorithm
;
172 METHOD(pts_t
, set_meas_algorithm
, void,
173 private_pts_t
*this, pts_meas_algorithms_t algorithm
)
175 hash_algorithm_t hash_alg
;
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
)
182 this->algorithm
= algorithm
;
186 METHOD(pts_t
, get_dh_hash_algorithm
, pts_meas_algorithms_t
,
189 return this->dh_hash_algorithm
;
192 METHOD(pts_t
, set_dh_hash_algorithm
, void,
193 private_pts_t
*this, pts_meas_algorithms_t algorithm
)
195 hash_algorithm_t hash_alg
;
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
)
202 this->dh_hash_algorithm
= algorithm
;
207 METHOD(pts_t
, create_dh_nonce
, bool,
208 private_pts_t
*this, pts_dh_group_t group
, int nonce_len
)
210 diffie_hellman_group_t dh_group
;
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
);
220 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_STRONG
);
223 DBG1(DBG_PTS
, "no rng available");
226 DBG2(DBG_PTS
, "nonce length is %d", nonce_len
);
227 nonce
= this->is_imc
? &this->responder_nonce
: &this->initiator_nonce
;
229 if (!rng
->allocate_bytes(rng
, nonce_len
, nonce
))
231 DBG1(DBG_PTS
, "failed to allocate nonce");
239 METHOD(pts_t
, get_my_public_value
, void,
240 private_pts_t
*this, chunk_t
*value
, chunk_t
*nonce
)
242 this->dh
->get_my_public_value(this->dh
, value
);
243 *nonce
= this->is_imc
? this->responder_nonce
: this->initiator_nonce
;
246 METHOD(pts_t
, set_peer_public_value
, void,
247 private_pts_t
*this, chunk_t value
, chunk_t nonce
)
249 this->dh
->set_other_public_value(this->dh
, value
);
251 nonce
= chunk_clone(nonce
);
254 this->initiator_nonce
= nonce
;
258 this->responder_nonce
= nonce
;
262 METHOD(pts_t
, calculate_secret
, bool,
266 hash_algorithm_t hash_alg
;
267 chunk_t shared_secret
;
269 /* Check presence of nonces */
270 if (!this->initiator_nonce
.len
|| !this->responder_nonce
.len
)
272 DBG1(DBG_PTS
, "initiator and/or responder nonce is not available");
275 DBG3(DBG_PTS
, "initiator nonce: %B", &this->initiator_nonce
);
276 DBG3(DBG_PTS
, "responder nonce: %B", &this->responder_nonce
);
278 /* Calculate the DH secret */
279 if (this->dh
->get_shared_secret(this->dh
, &shared_secret
) != SUCCESS
)
281 DBG1(DBG_PTS
, "shared DH secret computation failed");
284 DBG3(DBG_PTS
, "shared DH secret: %B", &shared_secret
);
286 /* Calculate the secret assessment value */
287 hash_alg
= pts_meas_algo_to_hash(this->dh_hash_algorithm
);
288 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
291 !hasher
->get_hash(hasher
, chunk_from_chars('1'), NULL
) ||
292 !hasher
->get_hash(hasher
, this->initiator_nonce
, NULL
) ||
293 !hasher
->get_hash(hasher
, this->responder_nonce
, NULL
) ||
294 !hasher
->allocate_hash(hasher
, shared_secret
, &this->secret
))
299 hasher
->destroy(hasher
);
301 /* The DH secret must be destroyed */
302 chunk_clear(&shared_secret
);
305 * Truncate the hash to 20 bytes to fit the ExternalData
306 * argument of the TPM Quote command
308 this->secret
.len
= min(this->secret
.len
, 20);
309 DBG3(DBG_PTS
, "secret assessment value: %B", &this->secret
);
314 * Print TPM 1.2 Version Info
316 static void print_tpm_version_info(private_pts_t
*this)
318 TPM_CAP_VERSION_INFO versionInfo
;
322 result
= Trspi_UnloadBlob_CAP_VERSION_INFO(&offset
,
323 this->tpm_version_info
.ptr
, &versionInfo
);
324 if (result
!= TSS_SUCCESS
)
326 DBG1(DBG_PTS
, "could not parse tpm version info: tss error 0x%x",
331 DBG2(DBG_PTS
, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
332 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
333 versionInfo
.version
.major
, versionInfo
.version
.minor
,
334 versionInfo
.version
.revMajor
, versionInfo
.version
.revMinor
,
335 versionInfo
.specLevel
, versionInfo
.errataRev
,
336 versionInfo
.tpmVendorID
);
340 METHOD(pts_t
, get_platform_info
, char*,
343 return this->platform_info
;
346 METHOD(pts_t
, set_platform_info
, void,
347 private_pts_t
*this, char *info
)
349 free(this->platform_info
);
350 this->platform_info
= strdup(info
);
353 METHOD(pts_t
, get_tpm_version_info
, bool,
354 private_pts_t
*this, chunk_t
*info
)
360 *info
= this->tpm_version_info
;
361 print_tpm_version_info(this);
365 METHOD(pts_t
, set_tpm_version_info
, void,
366 private_pts_t
*this, chunk_t info
)
368 this->tpm_version_info
= chunk_clone(info
);
369 print_tpm_version_info(this);
372 METHOD(pts_t
, get_pcr_len
, size_t,
375 return this->pcr_len
;
379 * Load an AIK Blob (TSS_TSPATTRIB_KEYBLOB_BLOB attribute)
381 static void load_aik_blob(private_pts_t
*this)
385 u_int32_t aikBlobLen
;
387 blob_path
= lib
->settings
->get_str(lib
->settings
,
388 "libimcv.plugins.imc-attestation.aik_blob", NULL
);
392 /* Read aik key blob from a file */
393 if ((fp
= fopen(blob_path
, "r")) == NULL
)
395 DBG1(DBG_PTS
, "unable to open AIK Blob file: %s", blob_path
);
399 fseek(fp
, 0, SEEK_END
);
400 aikBlobLen
= ftell(fp
);
401 fseek(fp
, 0L, SEEK_SET
);
403 this->aik_blob
= chunk_alloc(aikBlobLen
);
404 if (fread(this->aik_blob
.ptr
, 1, aikBlobLen
, fp
))
406 DBG2(DBG_PTS
, "loaded AIK Blob from '%s'", blob_path
);
407 DBG3(DBG_PTS
, "AIK Blob: %B", &this->aik_blob
);
411 DBG1(DBG_PTS
, "unable to read AIK Blob file '%s'", blob_path
);
417 DBG1(DBG_PTS
, "AIK Blob is not available");
421 * Load an AIK certificate or public key
422 * the certificate having precedence over the public key if both are present
424 static void load_aik(private_pts_t
*this)
426 char *cert_path
, *key_path
;
428 cert_path
= lib
->settings
->get_str(lib
->settings
,
429 "libimcv.plugins.imc-attestation.aik_cert", NULL
);
430 key_path
= lib
->settings
->get_str(lib
->settings
,
431 "libimcv.plugins.imc-attestation.aik_key", NULL
);
435 this->aik
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
436 CERT_X509
, BUILD_FROM_FILE
,
437 cert_path
, BUILD_END
);
440 DBG2(DBG_PTS
, "loaded AIK certificate from '%s'", cert_path
);
446 this->aik
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
447 CERT_TRUSTED_PUBKEY
, BUILD_FROM_FILE
,
448 key_path
, BUILD_END
);
451 DBG2(DBG_PTS
, "loaded AIK public key from '%s'", key_path
);
456 DBG1(DBG_PTS
, "neither AIK certificate nor public key is available");
459 METHOD(pts_t
, get_aik
, certificate_t
*,
465 METHOD(pts_t
, set_aik
, void,
466 private_pts_t
*this, certificate_t
*aik
)
468 DESTROY_IF(this->aik
);
469 this->aik
= aik
->get_ref(aik
);
472 METHOD(pts_t
, get_aik_keyid
, bool,
473 private_pts_t
*this, chunk_t
*keyid
)
475 public_key_t
*public;
480 DBG1(DBG_PTS
, "no AIK certificate available");
483 public = this->aik
->get_public_key(this->aik
);
486 DBG1(DBG_PTS
, "no AIK public key available");
489 success
= public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1
, keyid
);
492 DBG1(DBG_PTS
, "no SHA-1 AIK public key info ID available");
494 public->destroy(public);
499 METHOD(pts_t
, is_path_valid
, bool,
500 private_pts_t
*this, char *path
, pts_error_code_t
*error_code
)
506 if (!stat(path
, &st
))
510 else if (errno
== ENOENT
|| errno
== ENOTDIR
)
512 DBG1(DBG_PTS
, "file/directory does not exist %s", path
);
513 *error_code
= TCG_PTS_FILE_NOT_FOUND
;
515 else if (errno
== EFAULT
)
517 DBG1(DBG_PTS
, "bad address %s", path
);
518 *error_code
= TCG_PTS_INVALID_PATH
;
522 DBG1(DBG_PTS
, "error: %s occurred while validating path: %s",
523 strerror(errno
), path
);
531 * Obtain statistical information describing a file
533 static bool file_metadata(char *pathname
, pts_file_metadata_t
**entry
)
536 pts_file_metadata_t
*this;
538 this = malloc_thing(pts_file_metadata_t
);
540 if (stat(pathname
, &st
))
542 DBG1(DBG_PTS
, "unable to obtain statistics about '%s'", pathname
);
546 if (S_ISREG(st
.st_mode
))
548 this->type
= PTS_FILE_REGULAR
;
550 else if (S_ISDIR(st
.st_mode
))
552 this->type
= PTS_FILE_DIRECTORY
;
554 else if (S_ISCHR(st
.st_mode
))
556 this->type
= PTS_FILE_CHAR_SPEC
;
558 else if (S_ISBLK(st
.st_mode
))
560 this->type
= PTS_FILE_BLOCK_SPEC
;
562 else if (S_ISFIFO(st
.st_mode
))
564 this->type
= PTS_FILE_FIFO
;
566 else if (S_ISLNK(st
.st_mode
))
568 this->type
= PTS_FILE_SYM_LINK
;
570 else if (S_ISSOCK(st
.st_mode
))
572 this->type
= PTS_FILE_SOCKET
;
576 this->type
= PTS_FILE_OTHER
;
579 this->filesize
= st
.st_size
;
580 this->created
= st
.st_ctime
;
581 this->modified
= st
.st_mtime
;
582 this->accessed
= st
.st_atime
;
583 this->owner
= st
.st_uid
;
584 this->group
= st
.st_gid
;
590 METHOD(pts_t
, get_metadata
, pts_file_meta_t
*,
591 private_pts_t
*this, char *pathname
, bool is_directory
)
593 pts_file_meta_t
*metadata
;
594 pts_file_metadata_t
*entry
;
596 /* Create a metadata object */
597 metadata
= pts_file_meta_create();
601 enumerator_t
*enumerator
;
602 char *rel_name
, *abs_name
;
605 enumerator
= enumerator_create_directory(pathname
);
608 DBG1(DBG_PTS
," directory '%s' can not be opened, %s", pathname
,
610 metadata
->destroy(metadata
);
613 while (enumerator
->enumerate(enumerator
, &rel_name
, &abs_name
, &st
))
615 /* measure regular files only */
616 if (S_ISREG(st
.st_mode
) && *rel_name
!= '.')
618 if (!file_metadata(abs_name
, &entry
))
620 enumerator
->destroy(enumerator
);
621 metadata
->destroy(metadata
);
624 entry
->filename
= strdup(rel_name
);
625 metadata
->add(metadata
, entry
);
628 enumerator
->destroy(enumerator
);
632 if (!file_metadata(pathname
, &entry
))
634 metadata
->destroy(metadata
);
637 entry
->filename
= strdup(basename(pathname
));
638 metadata
->add(metadata
, entry
);
644 METHOD(pts_t
, read_pcr
, bool,
645 private_pts_t
*this, u_int32_t pcr_num
, chunk_t
*pcr_value
)
647 TSS_HCONTEXT hContext
;
652 bool success
= FALSE
;
654 result
= Tspi_Context_Create(&hContext
);
655 if (result
!= TSS_SUCCESS
)
657 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x", result
);
661 result
= Tspi_Context_Connect(hContext
, NULL
);
662 if (result
!= TSS_SUCCESS
)
666 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
667 if (result
!= TSS_SUCCESS
)
671 result
= Tspi_TPM_PcrRead(hTPM
, pcr_num
, (UINT32
*)&rgbPcrValue
.len
, &rgbPcrValue
.ptr
);
672 if (result
!= TSS_SUCCESS
)
676 *pcr_value
= chunk_clone(rgbPcrValue
);
677 DBG3(DBG_PTS
, "PCR %d value:%B", pcr_num
, pcr_value
);
683 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
685 Tspi_Context_FreeMemory(hContext
, NULL
);
686 Tspi_Context_Close(hContext
);
691 METHOD(pts_t
, extend_pcr
, bool,
692 private_pts_t
*this, u_int32_t pcr_num
, chunk_t input
, chunk_t
*output
)
694 TSS_HCONTEXT hContext
;
697 u_int32_t pcr_length
;
700 result
= Tspi_Context_Create(&hContext
);
701 if (result
!= TSS_SUCCESS
)
703 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
707 result
= Tspi_Context_Connect(hContext
, NULL
);
708 if (result
!= TSS_SUCCESS
)
712 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
713 if (result
!= TSS_SUCCESS
)
718 pcr_value
= chunk_alloc(PCR_LEN
);
719 result
= Tspi_TPM_PcrExtend(hTPM
, pcr_num
, PCR_LEN
, input
.ptr
,
720 NULL
, &pcr_length
, &pcr_value
.ptr
);
721 if (result
!= TSS_SUCCESS
)
727 *output
= chunk_clone(*output
);
729 DBG3(DBG_PTS
, "PCR %d extended with: %B", pcr_num
, &input
);
730 DBG3(DBG_PTS
, "PCR %d value after extend: %B", pcr_num
, output
);
732 chunk_clear(&pcr_value
);
733 Tspi_Context_FreeMemory(hContext
, NULL
);
734 Tspi_Context_Close(hContext
);
739 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
741 chunk_clear(&pcr_value
);
742 Tspi_Context_FreeMemory(hContext
, NULL
);
743 Tspi_Context_Close(hContext
);
749 static void clear_pcrs(private_pts_t
*this)
753 for (i
= 0; i
<= this->pcr_max
; i
++)
756 this->pcrs
[i
] = NULL
;
761 memset(this->pcr_select
, 0x00, sizeof(this->pcr_select
));
764 METHOD(pts_t
, quote_tpm
, bool,
765 private_pts_t
*this, bool use_quote2
, chunk_t
*pcr_comp
, chunk_t
*quote_sig
)
767 TSS_HCONTEXT hContext
;
771 TSS_HPOLICY srkUsagePolicy
;
772 TSS_UUID SRK_UUID
= TSS_UUID_SRK
;
773 BYTE secret
[] = TSS_WELL_KNOWN_SECRET
;
774 TSS_HPCRS hPcrComposite
;
775 TSS_VALIDATION valData
;
779 u_int32_t versionInfoSize
, pcr
, i
= 0, f
= 1;
780 bool success
= FALSE
;
782 result
= Tspi_Context_Create(&hContext
);
783 if (result
!= TSS_SUCCESS
)
785 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
789 result
= Tspi_Context_Connect(hContext
, NULL
);
790 if (result
!= TSS_SUCCESS
)
794 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
795 if (result
!= TSS_SUCCESS
)
800 /* Retrieve SRK from TPM and set the authentication to well known secret*/
801 result
= Tspi_Context_LoadKeyByUUID(hContext
, TSS_PS_TYPE_SYSTEM
,
803 if (result
!= TSS_SUCCESS
)
808 result
= Tspi_GetPolicyObject(hSRK
, TSS_POLICY_USAGE
, &srkUsagePolicy
);
809 if (result
!= TSS_SUCCESS
)
813 result
= Tspi_Policy_SetSecret(srkUsagePolicy
, TSS_SECRET_MODE_SHA1
,
815 if (result
!= TSS_SUCCESS
)
820 result
= Tspi_Context_LoadKeyByBlob (hContext
, hSRK
, this->aik_blob
.len
,
821 this->aik_blob
.ptr
, &hAIK
);
822 if (result
!= TSS_SUCCESS
)
827 /* Create PCR composite object */
828 result
= use_quote2
?
829 Tspi_Context_CreateObject(hContext
, TSS_OBJECT_TYPE_PCRS
,
830 TSS_PCRS_STRUCT_INFO_SHORT
, &hPcrComposite
) :
831 Tspi_Context_CreateObject(hContext
, TSS_OBJECT_TYPE_PCRS
,
832 TSS_PCRS_STRUCT_DEFAULT
, &hPcrComposite
);
833 if (result
!= TSS_SUCCESS
)
839 for (pcr
= 0; pcr
<= this->pcr_max
; pcr
++)
846 if (this->pcr_select
[i
] & f
)
848 result
= use_quote2
?
849 Tspi_PcrComposite_SelectPcrIndexEx(hPcrComposite
, pcr
,
850 TSS_PCRS_DIRECTION_RELEASE
) :
851 Tspi_PcrComposite_SelectPcrIndex(hPcrComposite
, pcr
);
852 if (result
!= TSS_SUCCESS
)
860 /* Set the Validation Data */
861 valData
.ulExternalDataLength
= this->secret
.len
;
862 valData
.rgbExternalData
= (BYTE
*)this->secret
.ptr
;
866 result
= use_quote2
?
867 Tspi_TPM_Quote2(hTPM
, hAIK
, FALSE
, hPcrComposite
, &valData
,
868 &versionInfoSize
, &versionInfo
):
869 Tspi_TPM_Quote(hTPM
, hAIK
, hPcrComposite
, &valData
);
870 if (result
!= TSS_SUCCESS
)
875 /* Set output chunks */
876 *pcr_comp
= chunk_alloc(HASH_SIZE_SHA1
);
880 /* TPM_Composite_Hash is last 20 bytes of TPM_Quote_Info2 structure */
881 memcpy(pcr_comp
->ptr
, valData
.rgbData
+ valData
.ulDataLength
- HASH_SIZE_SHA1
,
886 /* TPM_Composite_Hash is 8-28th bytes of TPM_Quote_Info structure */
887 memcpy(pcr_comp
->ptr
, valData
.rgbData
+ 8, HASH_SIZE_SHA1
);
889 DBG3(DBG_PTS
, "Hash of PCR Composite: %#B", pcr_comp
);
891 quote_info
= chunk_create(valData
.rgbData
, valData
.ulDataLength
);
892 DBG3(DBG_PTS
, "TPM Quote Info: %B","e_info
);
894 *quote_sig
= chunk_clone(chunk_create(valData
.rgbValidationData
,
895 valData
.ulValidationDataLength
));
896 DBG3(DBG_PTS
, "TPM Quote Signature: %B",quote_sig
);
902 Tspi_Context_FreeMemory(hContext
, NULL
);
905 Tspi_Context_CloseObject(hContext
, hPcrComposite
);
908 Tspi_Context_CloseObject(hContext
, hAIK
);
911 Tspi_Context_Close(hContext
);
915 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
922 METHOD(pts_t
, select_pcr
, bool,
923 private_pts_t
*this, u_int32_t pcr
)
927 if (pcr
>= PCR_MAX_NUM
)
929 DBG1(DBG_PTS
, "PCR %u: number is larger than maximum of %u",
934 /* Determine PCR selection flag */
936 f
= 1 << (pcr
- 8*i
);
938 /* Has this PCR already been selected? */
939 if (!(this->pcr_select
[i
] & f
))
941 this->pcr_select
[i
] |= f
;
942 this->pcr_max
= max(this->pcr_max
, pcr
);
949 METHOD(pts_t
, add_pcr
, bool,
950 private_pts_t
*this, u_int32_t pcr
, chunk_t pcr_before
, chunk_t pcr_after
)
952 if (pcr
>= PCR_MAX_NUM
)
954 DBG1(DBG_PTS
, "PCR %u: number is larger than maximum of %u",
959 /* Is the length of the PCR registers already set? */
962 if (pcr_after
.len
!= this->pcr_len
)
964 DBG1(DBG_PTS
, "PCR %02u: length is %d bytes but should be %d bytes",
965 pcr_after
.len
, this->pcr_len
);
971 this->pcr_len
= pcr_after
.len
;
974 /* Has the value of the PCR register already been assigned? */
977 if (!memeq(this->pcrs
[pcr
], pcr_before
.ptr
, this->pcr_len
))
979 DBG1(DBG_PTS
, "PCR %02u: new pcr_before value does not equal "
980 "old pcr_after value");
982 /* remove the old PCR value */
983 free(this->pcrs
[pcr
]);
987 /* add extended PCR Register */
988 this->pcr_select
[pcr
/ 8] |= 1 << (pcr
% 8);
989 this->pcr_max
= max(this->pcr_max
, pcr
);
993 /* Duplicate and store current PCR value */
994 pcr_after
= chunk_clone(pcr_after
);
995 this->pcrs
[pcr
] = pcr_after
.ptr
;
1001 * TPM_QUOTE_INFO structure:
1002 * 4 bytes of version
1003 * 4 bytes 'Q' 'U' 'O' 'T'
1004 * 20 byte SHA1 of TCPA_PCR_COMPOSITE
1007 * TPM_QUOTE_INFO2 structure:
1008 * 2 bytes Tag 0x0036 TPM_Tag_Quote_info2
1009 * 4 bytes 'Q' 'U' 'T' '2'
1011 * 26 bytes PCR_INFO_SHORT
1014 METHOD(pts_t
, get_quote_info
, bool,
1015 private_pts_t
*this, bool use_quote2
, bool use_ver_info
,
1016 pts_meas_algorithms_t comp_hash_algo
,
1017 chunk_t
*out_pcr_comp
, chunk_t
*out_quote_info
)
1019 u_int8_t size_of_select
;
1020 int pcr_comp_len
, i
;
1021 chunk_t pcr_comp
, hash_pcr_comp
;
1022 bio_writer_t
*writer
;
1025 if (this->pcr_count
== 0)
1027 DBG1(DBG_PTS
, "No extended PCR entries available, "
1028 "unable to construct TPM Quote Info");
1031 if (!this->secret
.ptr
)
1033 DBG1(DBG_PTS
, "Secret assessment value unavailable, ",
1034 "unable to construct TPM Quote Info");
1037 if (use_quote2
&& use_ver_info
&& !this->tpm_version_info
.ptr
)
1039 DBG1(DBG_PTS
, "TPM Version Information unavailable, ",
1040 "unable to construct TPM Quote Info2");
1045 * A TPM v1.2 has 24 PCR Registers
1046 * so the bitmask field length used by TrouSerS is at least 3 bytes
1048 size_of_select
= max(PCR_MAX_NUM
/ 8, 1 + this->pcr_max
/ 8);
1049 pcr_comp_len
= 2 + size_of_select
+ 4 + this->pcr_count
* this->pcr_len
;
1051 writer
= bio_writer_create(pcr_comp_len
);
1053 writer
->write_uint16(writer
, size_of_select
);
1054 for (i
= 0; i
< size_of_select
; i
++)
1056 writer
->write_uint8(writer
, this->pcr_select
[i
]);
1059 writer
->write_uint32(writer
, this->pcr_count
* this->pcr_len
);
1060 for (i
= 0; i
< 8 * size_of_select
; i
++)
1064 writer
->write_data(writer
, chunk_create(this->pcrs
[i
], this->pcr_len
));
1067 pcr_comp
= chunk_clone(writer
->get_buf(writer
));
1068 DBG3(DBG_PTS
, "constructed PCR Composite: %B", &pcr_comp
);
1070 writer
->destroy(writer
);
1072 /* Output the TPM_PCR_COMPOSITE expected from IMC */
1075 hash_algorithm_t algo
;
1077 algo
= pts_meas_algo_to_hash(comp_hash_algo
);
1078 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, algo
);
1080 /* Hash the PCR Composite Structure */
1081 if (!hasher
|| !hasher
->allocate_hash(hasher
, pcr_comp
, out_pcr_comp
))
1087 DBG3(DBG_PTS
, "constructed PCR Composite hash: %#B", out_pcr_comp
);
1088 hasher
->destroy(hasher
);
1092 *out_pcr_comp
= chunk_clone(pcr_comp
);
1095 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
1096 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1097 if (!hasher
|| !hasher
->allocate_hash(hasher
, pcr_comp
, &hash_pcr_comp
))
1100 chunk_free(out_pcr_comp
);
1104 hasher
->destroy(hasher
);
1106 /* Construct TPM_QUOTE_INFO/TPM_QUOTE_INFO2 structure */
1107 writer
= bio_writer_create(TPM_QUOTE_INFO_LEN
);
1111 /* TPM Structure Tag */
1112 writer
->write_uint16(writer
, TPM_TAG_QUOTE_INFO2
);
1114 /* Magic QUT2 value */
1115 writer
->write_data(writer
, chunk_create("QUT2", 4));
1117 /* Secret assessment value 20 bytes (nonce) */
1118 writer
->write_data(writer
, this->secret
);
1120 /* Length of the PCR selection field */
1121 writer
->write_uint16(writer
, size_of_select
);
1124 for (i
= 0; i
< size_of_select
; i
++)
1126 writer
->write_uint8(writer
, this->pcr_select
[i
]);
1129 /* TPM Locality Selection */
1130 writer
->write_uint8(writer
, TPM_LOC_ZERO
);
1132 /* PCR Composite Hash */
1133 writer
->write_data(writer
, hash_pcr_comp
);
1137 /* TPM version Info */
1138 writer
->write_data(writer
, this->tpm_version_info
);
1143 /* Version number */
1144 writer
->write_data(writer
, chunk_from_chars(1, 1, 0, 0));
1146 /* Magic QUOT value */
1147 writer
->write_data(writer
, chunk_create("QUOT", 4));
1149 /* PCR Composite Hash */
1150 writer
->write_data(writer
, hash_pcr_comp
);
1152 /* Secret assessment value 20 bytes (nonce) */
1153 writer
->write_data(writer
, this->secret
);
1156 /* TPM Quote Info */
1157 *out_quote_info
= chunk_clone(writer
->get_buf(writer
));
1158 DBG3(DBG_PTS
, "constructed TPM Quote Info: %B", out_quote_info
);
1160 writer
->destroy(writer
);
1162 free(hash_pcr_comp
.ptr
);
1168 METHOD(pts_t
, verify_quote_signature
, bool,
1169 private_pts_t
*this, chunk_t data
, chunk_t signature
)
1171 public_key_t
*aik_pub_key
;
1173 aik_pub_key
= this->aik
->get_public_key(this->aik
);
1176 DBG1(DBG_PTS
, "failed to get public key from AIK certificate");
1180 if (!aik_pub_key
->verify(aik_pub_key
, SIGN_RSA_EMSA_PKCS1_SHA1
,
1183 DBG1(DBG_PTS
, "signature verification failed for TPM Quote Info");
1184 DESTROY_IF(aik_pub_key
);
1188 aik_pub_key
->destroy(aik_pub_key
);
1192 METHOD(pts_t
, destroy
, void,
1193 private_pts_t
*this)
1196 DESTROY_IF(this->aik
);
1197 DESTROY_IF(this->dh
);
1198 free(this->initiator_nonce
.ptr
);
1199 free(this->responder_nonce
.ptr
);
1200 free(this->secret
.ptr
);
1201 free(this->platform_info
);
1202 free(this->aik_blob
.ptr
);
1203 free(this->tpm_version_info
.ptr
);
1207 #define RELEASE_LSB 0
1208 #define RELEASE_DEBIAN 1
1211 * Determine Linux distribution and hardware platform
1213 static char* extract_platform_info(void)
1216 char buf
[BUF_LEN
], *pos
= buf
, *value
= NULL
;
1217 int i
, len
= BUF_LEN
- 1;
1218 struct utsname uninfo
;
1220 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
1221 const char* releases
[] = {
1222 "/etc/lsb-release", "/etc/debian_version",
1223 "/etc/SuSE-release", "/etc/novell-release",
1224 "/etc/sles-release", "/etc/redhat-release",
1225 "/etc/fedora-release", "/etc/gentoo-release",
1226 "/etc/slackware-version", "/etc/annvix-release",
1227 "/etc/arch-release", "/etc/arklinux-release",
1228 "/etc/aurox-release", "/etc/blackcat-release",
1229 "/etc/cobalt-release", "/etc/conectiva-release",
1230 "/etc/debian_release", "/etc/immunix-release",
1231 "/etc/lfs-release", "/etc/linuxppc-release",
1232 "/etc/mandrake-release", "/etc/mandriva-release",
1233 "/etc/mandrakelinux-release", "/etc/mklinux-release",
1234 "/etc/pld-release", "/etc/redhat_version",
1235 "/etc/slackware-release", "/etc/e-smith-release",
1236 "/etc/release", "/etc/sun-release",
1237 "/etc/tinysofa-release", "/etc/turbolinux-release",
1238 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
1239 "/etc/va-release", "/etc/yellowdog-release"
1242 const char description
[] = "DISTRIB_DESCRIPTION=\"";
1243 const char str_debian
[] = "Debian ";
1245 for (i
= 0; i
< countof(releases
); i
++)
1247 file
= fopen(releases
[i
], "r");
1253 if (i
== RELEASE_DEBIAN
)
1255 strcpy(buf
, str_debian
);
1256 pos
+= strlen(str_debian
);
1257 len
-= strlen(str_debian
);
1260 fseek(file
, 0, SEEK_END
);
1261 len
= min(ftell(file
), len
);
1264 if (fread(pos
, 1, len
, file
) != len
)
1266 DBG1(DBG_PTS
, "failed to read file '%s'", releases
[i
]);
1272 if (i
== RELEASE_LSB
)
1274 pos
= strstr(buf
, description
);
1277 DBG1(DBG_PTS
, "failed to find begin of lsb-release "
1278 "DESCRIPTION field");
1281 value
= pos
+ strlen(description
);
1282 pos
= strchr(value
, '"');
1285 DBG1(DBG_PTS
, "failed to find end of lsb-release "
1286 "DESCRIPTION field");
1293 pos
= strchr(pos
, '\n');
1296 DBG1(DBG_PTS
, "failed to find end of release string");
1305 DBG1(DBG_PTS
, "no distribution release file found");
1309 if (uname(&uninfo
) < 0)
1311 DBG1(DBG_PTS
, "could not retrieve machine architecture");
1316 len
= sizeof(buf
)-1 + (pos
- buf
);
1317 strncpy(pos
, uninfo
.machine
, len
);
1319 DBG1(DBG_PTS
, "platform is '%s'", value
);
1320 return strdup(value
);
1324 * Check for a TPM by querying for TPM Version Info
1326 static bool has_tpm(private_pts_t
*this)
1328 TSS_HCONTEXT hContext
;
1331 u_int32_t version_info_len
;
1333 result
= Tspi_Context_Create(&hContext
);
1334 if (result
!= TSS_SUCCESS
)
1336 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
1340 result
= Tspi_Context_Connect(hContext
, NULL
);
1341 if (result
!= TSS_SUCCESS
)
1345 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
1346 if (result
!= TSS_SUCCESS
)
1350 result
= Tspi_TPM_GetCapability(hTPM
, TSS_TPMCAP_VERSION_VAL
, 0, NULL
,
1352 &this->tpm_version_info
.ptr
);
1353 this->tpm_version_info
.len
= version_info_len
;
1354 if (result
!= TSS_SUCCESS
)
1358 this->tpm_version_info
= chunk_clone(this->tpm_version_info
);
1360 Tspi_Context_FreeMemory(hContext
, NULL
);
1361 Tspi_Context_Close(hContext
);
1365 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
1366 Tspi_Context_FreeMemory(hContext
, NULL
);
1367 Tspi_Context_Close(hContext
);
1374 pts_t
*pts_create(bool is_imc
)
1376 private_pts_t
*this;
1380 .get_proto_caps
= _get_proto_caps
,
1381 .set_proto_caps
= _set_proto_caps
,
1382 .get_meas_algorithm
= _get_meas_algorithm
,
1383 .set_meas_algorithm
= _set_meas_algorithm
,
1384 .get_dh_hash_algorithm
= _get_dh_hash_algorithm
,
1385 .set_dh_hash_algorithm
= _set_dh_hash_algorithm
,
1386 .create_dh_nonce
= _create_dh_nonce
,
1387 .get_my_public_value
= _get_my_public_value
,
1388 .set_peer_public_value
= _set_peer_public_value
,
1389 .calculate_secret
= _calculate_secret
,
1390 .get_platform_info
= _get_platform_info
,
1391 .set_platform_info
= _set_platform_info
,
1392 .get_tpm_version_info
= _get_tpm_version_info
,
1393 .set_tpm_version_info
= _set_tpm_version_info
,
1394 .get_pcr_len
= _get_pcr_len
,
1395 .get_aik
= _get_aik
,
1396 .set_aik
= _set_aik
,
1397 .get_aik_keyid
= _get_aik_keyid
,
1398 .is_path_valid
= _is_path_valid
,
1399 .get_metadata
= _get_metadata
,
1400 .read_pcr
= _read_pcr
,
1401 .extend_pcr
= _extend_pcr
,
1402 .quote_tpm
= _quote_tpm
,
1403 .select_pcr
= _select_pcr
,
1404 .add_pcr
= _add_pcr
,
1405 .get_quote_info
= _get_quote_info
,
1406 .verify_quote_signature
= _verify_quote_signature
,
1407 .destroy
= _destroy
,
1410 .proto_caps
= PTS_PROTO_CAPS_V
,
1411 .algorithm
= PTS_MEAS_ALGO_SHA256
,
1412 .dh_hash_algorithm
= PTS_MEAS_ALGO_SHA256
,
1417 this->platform_info
= extract_platform_info();
1421 this->has_tpm
= TRUE
;
1422 this->pcr_len
= PCR_LEN
;
1423 this->proto_caps
|= PTS_PROTO_CAPS_T
| PTS_PROTO_CAPS_D
;
1425 load_aik_blob(this);
1430 this->proto_caps
|= PTS_PROTO_CAPS_T
| PTS_PROTO_CAPS_D
;
1433 return &this->public;