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
32 typedef struct private_pts_t private_pts_t
;
35 * Private data of a pts_t object.
38 struct private_pts_t
{
41 * Public pts_t interface.
46 * PTS Protocol Capabilities
48 pts_proto_caps_flag_t proto_caps
;
51 * PTS Measurement Algorithm
53 pts_meas_algorithms_t algorithm
;
58 pts_meas_algorithms_t dh_hash_algorithm
;
61 * PTS Diffie-Hellman Secret
66 * PTS Diffie-Hellman Initiator Nonce
68 chunk_t initiator_nonce
;
71 * PTS Diffie-Hellman Responder Nonce
73 chunk_t responder_nonce
;
76 * Secret assessment value to be used for TPM Quote as an external data
81 * Platform and OS Info
86 * TRUE if IMC-PTS, FALSE if IMV-PTS
91 * Do we have an activated TPM
96 * Contains a TPM_CAP_VERSION_INFO struct
98 chunk_t tpm_version_info
;
101 * Contains TSS Blob structure for AIK
106 * Contains a Attestation Identity Key or Certificate
111 * Table of extended PCRs with corresponding values
113 u_char
* pcrs
[PCR_MAX_NUM
];
116 * Length of PCR registers
121 * Number of extended PCR registers
126 * Highest extended PCR register
131 * Bitmap of extended PCR registers
133 u_int8_t pcr_select
[PCR_MAX_NUM
/ 8];
137 METHOD(pts_t
, get_proto_caps
, pts_proto_caps_flag_t
,
140 return this->proto_caps
;
143 METHOD(pts_t
, set_proto_caps
, void,
144 private_pts_t
*this, pts_proto_caps_flag_t flags
)
146 this->proto_caps
= flags
;
147 DBG2(DBG_PTS
, "supported PTS protocol capabilities: %s%s%s%s%s",
148 flags
& PTS_PROTO_CAPS_C ?
"C" : ".",
149 flags
& PTS_PROTO_CAPS_V ?
"V" : ".",
150 flags
& PTS_PROTO_CAPS_D ?
"D" : ".",
151 flags
& PTS_PROTO_CAPS_T ?
"T" : ".",
152 flags
& PTS_PROTO_CAPS_X ?
"X" : ".");
155 METHOD(pts_t
, get_meas_algorithm
, pts_meas_algorithms_t
,
158 return this->algorithm
;
161 METHOD(pts_t
, set_meas_algorithm
, void,
162 private_pts_t
*this, pts_meas_algorithms_t algorithm
)
164 hash_algorithm_t hash_alg
;
166 hash_alg
= pts_meas_algo_to_hash(algorithm
);
167 DBG2(DBG_PTS
, "selected PTS measurement algorithm is %N",
168 hash_algorithm_names
, hash_alg
);
169 if (hash_alg
!= HASH_UNKNOWN
)
171 this->algorithm
= algorithm
;
175 METHOD(pts_t
, get_dh_hash_algorithm
, pts_meas_algorithms_t
,
178 return this->dh_hash_algorithm
;
181 METHOD(pts_t
, set_dh_hash_algorithm
, void,
182 private_pts_t
*this, pts_meas_algorithms_t algorithm
)
184 hash_algorithm_t hash_alg
;
186 hash_alg
= pts_meas_algo_to_hash(algorithm
);
187 DBG2(DBG_PTS
, "selected DH hash algorithm is %N",
188 hash_algorithm_names
, hash_alg
);
189 if (hash_alg
!= HASH_UNKNOWN
)
191 this->dh_hash_algorithm
= algorithm
;
196 METHOD(pts_t
, create_dh_nonce
, bool,
197 private_pts_t
*this, pts_dh_group_t group
, int nonce_len
)
199 diffie_hellman_group_t dh_group
;
203 dh_group
= pts_dh_group_to_ike(group
);
204 DBG2(DBG_PTS
, "selected PTS DH group is %N",
205 diffie_hellman_group_names
, dh_group
);
206 DESTROY_IF(this->dh
);
207 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, dh_group
);
209 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_STRONG
);
212 DBG1(DBG_PTS
, "no rng available");
215 DBG2(DBG_PTS
, "nonce length is %d", nonce_len
);
216 nonce
= this->is_imc ?
&this->responder_nonce
: &this->initiator_nonce
;
218 rng
->allocate_bytes(rng
, nonce_len
, nonce
);
224 METHOD(pts_t
, get_my_public_value
, void,
225 private_pts_t
*this, chunk_t
*value
, chunk_t
*nonce
)
227 this->dh
->get_my_public_value(this->dh
, value
);
228 *nonce
= this->is_imc ?
this->responder_nonce
: this->initiator_nonce
;
231 METHOD(pts_t
, set_peer_public_value
, void,
232 private_pts_t
*this, chunk_t value
, chunk_t nonce
)
234 this->dh
->set_other_public_value(this->dh
, value
);
236 nonce
= chunk_clone(nonce
);
239 this->initiator_nonce
= nonce
;
243 this->responder_nonce
= nonce
;
247 METHOD(pts_t
, calculate_secret
, bool,
251 hash_algorithm_t hash_alg
;
252 chunk_t shared_secret
;
254 /* Check presence of nonces */
255 if (!this->initiator_nonce
.len
|| !this->responder_nonce
.len
)
257 DBG1(DBG_PTS
, "initiator and/or responder nonce is not available");
260 DBG3(DBG_PTS
, "initiator nonce: %B", &this->initiator_nonce
);
261 DBG3(DBG_PTS
, "responder nonce: %B", &this->responder_nonce
);
263 /* Calculate the DH secret */
264 if (this->dh
->get_shared_secret(this->dh
, &shared_secret
) != SUCCESS
)
266 DBG1(DBG_PTS
, "shared DH secret computation failed");
269 DBG3(DBG_PTS
, "shared DH secret: %B", &shared_secret
);
271 /* Calculate the secret assessment value */
272 hash_alg
= pts_meas_algo_to_hash(this->dh_hash_algorithm
);
273 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
275 hasher
->allocate_hash(hasher
, chunk_from_chars('1'), NULL
);
276 hasher
->allocate_hash(hasher
, this->initiator_nonce
, NULL
);
277 hasher
->allocate_hash(hasher
, this->responder_nonce
, NULL
);
278 hasher
->allocate_hash(hasher
, shared_secret
, &this->secret
);
279 hasher
->destroy(hasher
);
281 /* The DH secret must be destroyed */
282 chunk_clear(&shared_secret
);
285 * Truncate the hash to 20 bytes to fit the ExternalData
286 * argument of the TPM Quote command
288 this->secret
.len
= min(this->secret
.len
, 20);
289 DBG3(DBG_PTS
, "secret assessment value: %B", &this->secret
);
294 * Print TPM 1.2 Version Info
296 static void print_tpm_version_info(private_pts_t
*this)
298 TPM_CAP_VERSION_INFO versionInfo
;
302 result
= Trspi_UnloadBlob_CAP_VERSION_INFO(&offset
,
303 this->tpm_version_info
.ptr
, &versionInfo
);
304 if (result
!= TSS_SUCCESS
)
306 DBG1(DBG_PTS
, "could not parse tpm version info: tss error 0x%x",
311 DBG2(DBG_PTS
, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
312 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
313 versionInfo
.version
.major
, versionInfo
.version
.minor
,
314 versionInfo
.version
.revMajor
, versionInfo
.version
.revMinor
,
315 versionInfo
.specLevel
, versionInfo
.errataRev
,
316 versionInfo
.tpmVendorID
);
320 METHOD(pts_t
, get_platform_info
, char*,
323 return this->platform_info
;
326 METHOD(pts_t
, set_platform_info
, void,
327 private_pts_t
*this, char *info
)
329 free(this->platform_info
);
330 this->platform_info
= strdup(info
);
333 METHOD(pts_t
, get_tpm_version_info
, bool,
334 private_pts_t
*this, chunk_t
*info
)
340 *info
= this->tpm_version_info
;
341 print_tpm_version_info(this);
345 METHOD(pts_t
, set_tpm_version_info
, void,
346 private_pts_t
*this, chunk_t info
)
348 this->tpm_version_info
= chunk_clone(info
);
349 print_tpm_version_info(this);
353 * Load an AIK Blob (TSS_TSPATTRIB_KEYBLOB_BLOB attribute)
355 static void load_aik_blob(private_pts_t
*this)
359 u_int32_t aikBlobLen
;
361 blob_path
= lib
->settings
->get_str(lib
->settings
,
362 "libimcv.plugins.imc-attestation.aik_blob", NULL
);
366 /* Read aik key blob from a file */
367 if ((fp
= fopen(blob_path
, "r")) == NULL
)
369 DBG1(DBG_PTS
, "unable to open AIK Blob file: %s", blob_path
);
373 fseek(fp
, 0, SEEK_END
);
374 aikBlobLen
= ftell(fp
);
375 fseek(fp
, 0L, SEEK_SET
);
377 this->aik_blob
= chunk_alloc(aikBlobLen
);
378 if (fread(this->aik_blob
.ptr
, 1, aikBlobLen
, fp
))
380 DBG2(DBG_PTS
, "loaded AIK Blob from '%s'", blob_path
);
381 DBG3(DBG_PTS
, "AIK Blob: %B", &this->aik_blob
);
385 DBG1(DBG_PTS
, "unable to read AIK Blob file '%s'", blob_path
);
391 DBG1(DBG_PTS
, "AIK Blob is not available");
395 * Load an AIK certificate or public key
396 * the certificate having precedence over the public key if both are present
398 static void load_aik(private_pts_t
*this)
400 char *cert_path
, *key_path
;
402 cert_path
= lib
->settings
->get_str(lib
->settings
,
403 "libimcv.plugins.imc-attestation.aik_cert", NULL
);
404 key_path
= lib
->settings
->get_str(lib
->settings
,
405 "libimcv.plugins.imc-attestation.aik_key", NULL
);
409 this->aik
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
410 CERT_X509
, BUILD_FROM_FILE
,
411 cert_path
, BUILD_END
);
414 DBG2(DBG_PTS
, "loaded AIK certificate from '%s'", cert_path
);
420 this->aik
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
421 CERT_TRUSTED_PUBKEY
, BUILD_FROM_FILE
,
422 key_path
, BUILD_END
);
425 DBG2(DBG_PTS
, "loaded AIK public key from '%s'", key_path
);
430 DBG1(DBG_PTS
, "neither AIK certificate nor public key is available");
433 METHOD(pts_t
, get_aik
, certificate_t
*,
439 METHOD(pts_t
, set_aik
, void,
440 private_pts_t
*this, certificate_t
*aik
)
442 DESTROY_IF(this->aik
);
443 this->aik
= aik
->get_ref(aik
);
446 METHOD(pts_t
, hash_file
, bool,
447 private_pts_t
*this, hasher_t
*hasher
, char *pathname
, u_char
*hash
)
449 u_char buffer
[PTS_BUF_SIZE
];
453 file
= fopen(pathname
, "rb");
456 DBG1(DBG_PTS
," file '%s' can not be opened, %s", pathname
,
462 bytes_read
= fread(buffer
, 1, sizeof(buffer
), file
);
465 hasher
->get_hash(hasher
, chunk_create(buffer
, bytes_read
), NULL
);
469 hasher
->get_hash(hasher
, chunk_empty
, hash
);
479 * Get the relative filename of a fully qualified file pathname
481 static char* get_filename(char *pathname
)
483 char *pos
, *filename
;
485 pos
= filename
= pathname
;
486 while (pos
&& *(++pos
) != '\0')
489 pos
= strchr(filename
, '/');
494 METHOD(pts_t
, is_path_valid
, bool,
495 private_pts_t
*this, char *path
, pts_error_code_t
*error_code
)
501 if (!stat(path
, &st
))
505 else if (errno
== ENOENT
|| errno
== ENOTDIR
)
507 DBG1(DBG_PTS
, "file/directory does not exist %s", path
);
508 *error_code
= TCG_PTS_FILE_NOT_FOUND
;
510 else if (errno
== EFAULT
)
512 DBG1(DBG_PTS
, "bad address %s", path
);
513 *error_code
= TCG_PTS_INVALID_PATH
;
517 DBG1(DBG_PTS
, "error: %s occured while validating path: %s",
518 strerror(errno
), path
);
525 METHOD(pts_t
, do_measurements
, pts_file_meas_t
*,
526 private_pts_t
*this, u_int16_t request_id
, char *pathname
, bool is_directory
)
529 hash_algorithm_t hash_alg
;
530 u_char hash
[HASH_SIZE_SHA384
];
532 pts_file_meas_t
*measurements
;
534 /* Create a hasher */
535 hash_alg
= pts_meas_algo_to_hash(this->algorithm
);
536 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
539 DBG1(DBG_PTS
, "hasher %N not available", hash_algorithm_names
, hash_alg
);
543 /* Create a measurement object */
544 measurements
= pts_file_meas_create(request_id
);
546 /* Link the hash to the measurement and set the measurement length */
547 measurement
= chunk_create(hash
, hasher
->get_hash_size(hasher
));
551 enumerator_t
*enumerator
;
552 char *rel_name
, *abs_name
;
555 enumerator
= enumerator_create_directory(pathname
);
558 DBG1(DBG_PTS
," directory '%s' can not be opened, %s", pathname
,
560 hasher
->destroy(hasher
);
561 measurements
->destroy(measurements
);
564 while (enumerator
->enumerate(enumerator
, &rel_name
, &abs_name
, &st
))
566 /* measure regular files only */
567 if (S_ISREG(st
.st_mode
) && *rel_name
!= '.')
569 if (!hash_file(this, hasher
, abs_name
, hash
))
571 enumerator
->destroy(enumerator
);
572 hasher
->destroy(hasher
);
573 measurements
->destroy(measurements
);
576 DBG2(DBG_PTS
, " %#B for '%s'", &measurement
, rel_name
);
577 measurements
->add(measurements
, rel_name
, measurement
);
580 enumerator
->destroy(enumerator
);
586 if (!hash_file(this, hasher
, pathname
, hash
))
588 hasher
->destroy(hasher
);
589 measurements
->destroy(measurements
);
592 filename
= get_filename(pathname
);
593 DBG2(DBG_PTS
, " %#B for '%s'", &measurement
, filename
);
594 measurements
->add(measurements
, filename
, measurement
);
596 hasher
->destroy(hasher
);
602 * Obtain statistical information describing a file
604 static bool file_metadata(char *pathname
, pts_file_metadata_t
**entry
)
607 pts_file_metadata_t
*this;
609 this = malloc_thing(pts_file_metadata_t
);
611 if (stat(pathname
, &st
))
613 DBG1(DBG_PTS
, "Unable to obtain statistics about '%s'", pathname
);
617 if (S_ISREG(st
.st_mode
))
619 this->type
= PTS_FILE_REGULAR
;
621 else if (S_ISDIR(st
.st_mode
))
623 this->type
= PTS_FILE_DIRECTORY
;
625 else if (S_ISCHR(st
.st_mode
))
627 this->type
= PTS_FILE_CHAR_SPEC
;
629 else if (S_ISBLK(st
.st_mode
))
631 this->type
= PTS_FILE_BLOCK_SPEC
;
633 else if (S_ISFIFO(st
.st_mode
))
635 this->type
= PTS_FILE_FIFO
;
637 else if (S_ISLNK(st
.st_mode
))
639 this->type
= PTS_FILE_SYM_LINK
;
641 else if (S_ISSOCK(st
.st_mode
))
643 this->type
= PTS_FILE_SOCKET
;
647 this->type
= PTS_FILE_OTHER
;
650 this->filesize
= st
.st_size
;
651 this->created
= st
.st_ctime
;
652 this->modified
= st
.st_mtime
;
653 this->accessed
= st
.st_atime
;
654 this->owner
= st
.st_uid
;
655 this->group
= st
.st_gid
;
661 METHOD(pts_t
, get_metadata
, pts_file_meta_t
*,
662 private_pts_t
*this, char *pathname
, bool is_directory
)
664 pts_file_meta_t
*metadata
;
665 pts_file_metadata_t
*entry
;
667 /* Create a metadata object */
668 metadata
= pts_file_meta_create();
672 enumerator_t
*enumerator
;
673 char *rel_name
, *abs_name
;
676 enumerator
= enumerator_create_directory(pathname
);
679 DBG1(DBG_PTS
," directory '%s' can not be opened, %s", pathname
,
681 metadata
->destroy(metadata
);
684 while (enumerator
->enumerate(enumerator
, &rel_name
, &abs_name
, &st
))
686 /* measure regular files only */
687 if (S_ISREG(st
.st_mode
) && *rel_name
!= '.')
689 if (!file_metadata(abs_name
, &entry
))
691 enumerator
->destroy(enumerator
);
692 metadata
->destroy(metadata
);
695 entry
->filename
= strdup(rel_name
);
696 metadata
->add(metadata
, entry
);
699 enumerator
->destroy(enumerator
);
703 if (!file_metadata(pathname
, &entry
))
705 metadata
->destroy(metadata
);
708 entry
->filename
= strdup(get_filename(pathname
));
709 metadata
->add(metadata
, entry
);
715 METHOD(pts_t
, read_pcr
, bool,
716 private_pts_t
*this, u_int32_t pcr_num
, chunk_t
*output
)
718 TSS_HCONTEXT hContext
;
721 u_int32_t pcr_length
;
724 result
= Tspi_Context_Create(&hContext
);
725 if (result
!= TSS_SUCCESS
)
727 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
732 result
= Tspi_Context_Connect(hContext
, NULL
);
733 if (result
!= TSS_SUCCESS
)
737 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
738 if (result
!= TSS_SUCCESS
)
742 pcr_value
= chunk_alloc(PCR_LEN
);
743 result
= Tspi_TPM_PcrRead(hTPM
, pcr_num
, &pcr_length
, &pcr_value
.ptr
);
744 if (result
!= TSS_SUCCESS
)
750 *output
= chunk_clone(*output
);
752 chunk_clear(&pcr_value
);
753 DBG3(DBG_PTS
, "PCR %d value:%B", pcr_num
, output
);
754 Tspi_Context_Close(hContext
);
758 chunk_clear(&pcr_value
);
759 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
760 Tspi_Context_Close(hContext
);
764 METHOD(pts_t
, extend_pcr
, bool,
765 private_pts_t
*this, u_int32_t pcr_num
, chunk_t input
, chunk_t
*output
)
767 TSS_HCONTEXT hContext
;
770 u_int32_t pcr_length
;
773 result
= Tspi_Context_Create(&hContext
);
774 if (result
!= TSS_SUCCESS
)
776 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
780 result
= Tspi_Context_Connect(hContext
, NULL
);
781 if (result
!= TSS_SUCCESS
)
785 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
786 if (result
!= TSS_SUCCESS
)
791 pcr_value
= chunk_alloc(PCR_LEN
);
792 result
= Tspi_TPM_PcrExtend(hTPM
, pcr_num
, PCR_LEN
, input
.ptr
,
793 NULL
, &pcr_length
, &pcr_value
.ptr
);
794 if (result
!= TSS_SUCCESS
)
800 *output
= chunk_clone(*output
);
802 chunk_clear(&pcr_value
);
803 Tspi_Context_Close(hContext
);
804 DBG3(DBG_PTS
, "PCR %d extended with: %B", pcr_num
, &input
);
805 DBG3(DBG_PTS
, "PCR %d value after extend: %B", pcr_num
, output
);
809 chunk_clear(&pcr_value
);
810 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
811 Tspi_Context_Close(hContext
);
816 static void clear_pcrs(private_pts_t
*this)
820 for (i
= 0; i
<= this->pcr_max
; i
++)
823 this->pcrs
[i
] = NULL
;
828 memset(this->pcr_select
, 0x00, sizeof(this->pcr_select
));
831 METHOD(pts_t
, quote_tpm
, bool,
832 private_pts_t
*this, bool use_quote2
, chunk_t
*pcr_composite
,
833 chunk_t
*quote_signature
)
835 TSS_HCONTEXT hContext
;
839 TSS_HPOLICY srkUsagePolicy
;
840 TSS_UUID SRK_UUID
= TSS_UUID_SRK
;
841 BYTE secret
[] = TSS_WELL_KNOWN_SECRET
;
842 TSS_HPCRS hPcrComposite
;
843 TSS_VALIDATION valData
;
845 chunk_t pcr_comp
, quote_sign
;
847 u_int32_t versionInfoSize
, pcr
, i
= 0, f
= 1;
848 bool success
= FALSE
;
850 result
= Tspi_Context_Create(&hContext
);
851 if (result
!= TSS_SUCCESS
)
853 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
857 result
= Tspi_Context_Connect(hContext
, NULL
);
858 if (result
!= TSS_SUCCESS
)
862 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
863 if (result
!= TSS_SUCCESS
)
868 /* Retrieve SRK from TPM and set the authentication to well known secret*/
869 result
= Tspi_Context_LoadKeyByUUID(hContext
, TSS_PS_TYPE_SYSTEM
,
871 if (result
!= TSS_SUCCESS
)
876 result
= Tspi_GetPolicyObject(hSRK
, TSS_POLICY_USAGE
, &srkUsagePolicy
);
877 if (result
!= TSS_SUCCESS
)
881 result
= Tspi_Policy_SetSecret(srkUsagePolicy
, TSS_SECRET_MODE_SHA1
,
883 if (result
!= TSS_SUCCESS
)
888 result
= Tspi_Context_LoadKeyByBlob (hContext
, hSRK
, this->aik_blob
.len
,
889 this->aik_blob
.ptr
, &hAIK
);
890 if (result
!= TSS_SUCCESS
)
895 /* Create PCR composite object */
896 result
= use_quote2 ?
897 Tspi_Context_CreateObject(hContext
, TSS_OBJECT_TYPE_PCRS
,
898 TSS_PCRS_STRUCT_INFO_SHORT
, &hPcrComposite
) :
899 Tspi_Context_CreateObject(hContext
, TSS_OBJECT_TYPE_PCRS
,
901 if (result
!= TSS_SUCCESS
)
907 for (pcr
= 0; pcr
<= this->pcr_max
; pcr
++)
914 if (this->pcr_select
[i
] & f
)
916 DBG2(DBG_TNC
, "PCR %02d selected for TPM Quote", pcr
);
917 result
= use_quote2 ?
918 Tspi_PcrComposite_SelectPcrIndexEx(hPcrComposite
, pcr
,
919 TSS_PCRS_DIRECTION_RELEASE
) :
920 Tspi_PcrComposite_SelectPcrIndex(hPcrComposite
, pcr
);
921 if (result
!= TSS_SUCCESS
)
929 /* Set the Validation Data */
930 valData
.ulExternalDataLength
= this->secret
.len
;
931 valData
.rgbExternalData
= (BYTE
*)this->secret
.ptr
;
935 result
= use_quote2 ?
936 Tspi_TPM_Quote2(hTPM
, hAIK
, FALSE
, hPcrComposite
, &valData
,
937 &versionInfoSize
,&versionInfo
):
938 Tspi_TPM_Quote(hTPM
, hAIK
, hPcrComposite
, &valData
);
939 if (result
!= TSS_SUCCESS
)
944 /* Set output chunks */
945 pcr_comp
= chunk_alloc(HASH_SIZE_SHA1
);
948 /* TPM_Composite_Hash is last 20 bytes of TPM_Quote_Info2 structure */
949 memcpy(pcr_comp
.ptr
, valData
.rgbData
+ valData
.ulDataLength
- HASH_SIZE_SHA1
,
954 /* TPM_Composite_Hash is 8-28th bytes of TPM_Quote_Info structure */
955 memcpy(pcr_comp
.ptr
, valData
.rgbData
+ 8, HASH_SIZE_SHA1
);
958 *pcr_composite
= pcr_comp
;
959 *pcr_composite
= chunk_clone(*pcr_composite
);
960 DBG3(DBG_PTS
, "Hash of PCR Composite: %B",pcr_composite
);
962 chunk_t tmp
= chunk_create(valData
.rgbData
, valData
.ulDataLength
);
963 DBG3(DBG_PTS
, "TPM Quote Info: %B",&tmp
);
965 quote_sign
= chunk_alloc(valData
.ulValidationDataLength
);
966 memcpy(quote_sign
.ptr
, valData
.rgbValidationData
,
967 valData
.ulValidationDataLength
);
968 *quote_signature
= quote_sign
;
969 *quote_signature
= chunk_clone(*quote_signature
);
970 DBG3(DBG_PTS
, "TPM Quote Signature: %B",quote_signature
);
972 chunk_clear("e_sign
);
977 Tspi_Context_FreeMemory(hContext
, NULL
);
980 Tspi_Context_CloseObject(hContext
, hPcrComposite
);
983 Tspi_Context_CloseObject(hContext
, hAIK
);
986 Tspi_Context_Close(hContext
);
990 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
997 METHOD(pts_t
, select_pcr
, bool,
998 private_pts_t
*this, u_int32_t pcr
)
1002 if (pcr
>= PCR_MAX_NUM
)
1004 DBG1(DBG_PTS
, "PCR %u: number is larger than maximum of %u",
1005 pcr
, PCR_MAX_NUM
-1);
1009 /* Determine PCR selection flag */
1011 f
= 1 << (pcr
- 8*i
);
1013 /* Has this PCR already been selected? */
1014 if (!(this->pcr_select
[i
] & f
))
1016 this->pcr_select
[i
] |= f
;
1017 this->pcr_max
= max(this->pcr_max
, pcr
);
1024 METHOD(pts_t
, add_pcr
, bool,
1025 private_pts_t
*this, u_int32_t pcr
, chunk_t pcr_before
, chunk_t pcr_after
)
1027 if (pcr
>= PCR_MAX_NUM
)
1029 DBG1(DBG_PTS
, "PCR %u: number is larger than maximum of %u",
1030 pcr
, PCR_MAX_NUM
-1);
1034 /* Is the length of the PCR registers already set? */
1037 if (pcr_after
.len
!= this->pcr_len
)
1039 DBG1(DBG_PTS
, "PCR %02u: length is %d bytes but should be %d bytes",
1040 pcr_after
.len
, this->pcr_len
);
1046 this->pcr_len
= pcr_after
.len
;
1049 /* Has the value of the PCR register already been assigned? */
1050 if (this->pcrs
[pcr
])
1052 if (!memeq(this->pcrs
[pcr
], pcr_before
.ptr
, this->pcr_len
))
1054 DBG1(DBG_PTS
, "PCR %02u: new pcr_before value does not equal "
1055 "old pcr_after value");
1057 /* remove the old PCR value */
1058 free(this->pcrs
[pcr
]);
1062 /* add extended PCR Register */
1063 this->pcr_select
[pcr
/ 8] |= 1 << (pcr
% 8);
1064 this->pcr_max
= max(this->pcr_max
, pcr
);
1068 /* Duplicate and store current PCR value */
1069 pcr_after
= chunk_clone(pcr_after
);
1070 this->pcrs
[pcr
] = pcr_after
.ptr
;
1075 METHOD(pts_t
, does_pcr_value_match
, bool,
1076 private_pts_t
*this, chunk_t pcr_after_value
)
1083 this->pcrs
= linked_list_create();
1086 e
= this->pcrs
->create_enumerator(this->pcrs
);
1087 while (e
->enumerate(e
, &entry
))
1089 if (entry
->pcr_number
== new->pcr_number
)
1091 DBG4(DBG_PTS
, "updating already added PCR%d value",
1093 this->pcrs
->remove_at(this->pcrs
, e
);
1099 this->pcrs
->insert_last(this->pcrs
, new);
1103 * TPM_QUOTE_INFO structure:
1104 * 4 bytes of version
1105 * 4 bytes 'Q' 'U' 'O' 'T'
1106 * 20 byte SHA1 of TCPA_PCR_COMPOSITE
1109 * TPM_QUOTE_INFO2 structure:
1110 * 2 bytes Tag 0x0036 TPM_Tag_Quote_info2
1111 * 4 bytes 'Q' 'U' 'T' '2'
1113 * 26 bytes PCR_INFO_SHORT
1116 METHOD(pts_t
, get_quote_info
, bool,
1117 private_pts_t
*this, bool use_quote2
, bool ver_info_included
,
1118 pts_meas_algorithms_t composite_algo
,
1119 chunk_t
*out_pcr_composite
, chunk_t
*out_quote_info
)
1121 u_int8_t size_of_select
;
1122 int pcr_composite_len
, i
;
1123 chunk_t pcr_composite
, hash_pcr_composite
;
1124 bio_writer_t
*writer
;
1127 if (this->pcr_count
== 0)
1129 DBG1(DBG_PTS
, "No extended PCR entries available, "
1130 "unable to construct TPM Quote Info");
1133 if (!this->secret
.ptr
)
1135 DBG1(DBG_PTS
, "Secret assessment value unavailable, ",
1136 "unable to construct TPM Quote Info");
1139 if (use_quote2
&& ver_info_included
&& !this->tpm_version_info
.ptr
)
1141 DBG1(DBG_PTS
, "TPM Version Information unavailable, ",
1142 "unable to construct TPM Quote Info2");
1146 size_of_select
= 1 + this->pcr_max
/ 8;
1147 pcr_composite_len
= 2 + size_of_select
+
1148 4 + this->pcr_count
* this->pcr_len
;
1150 writer
= bio_writer_create(pcr_composite_len
);
1152 writer
->write_uint16(writer
, size_of_select
);
1153 for (i
= 0; i
< size_of_select
; i
++)
1155 writer
->write_uint8(writer
, this->pcr_select
[i
]);
1158 writer
->write_uint32(writer
, this->pcr_count
* this->pcr_len
);
1159 for (i
= 0; i
< 8 * size_of_select
; i
++)
1163 writer
->write_data(writer
, chunk_create(this->pcrs
[i
], this->pcr_len
));
1166 pcr_composite
= chunk_clone(writer
->get_buf(writer
));
1167 DBG3(DBG_PTS
, "PCR Composite: %B", &pcr_composite
);
1169 writer
->destroy(writer
);
1171 /* Output the TPM_PCR_COMPOSITE expected from IMC */
1174 hash_algorithm_t algo
;
1176 algo
= pts_meas_algo_to_hash(composite_algo
);
1177 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, algo
);
1179 /* Hash the PCR Composite Structure */
1180 hasher
->allocate_hash(hasher
, pcr_composite
, out_pcr_composite
);
1181 DBG3(DBG_PTS
, "Hash of calculated PCR Composite: %B", out_pcr_composite
);
1182 hasher
->destroy(hasher
);
1186 *out_pcr_composite
= chunk_clone(pcr_composite
);
1187 DBG3(DBG_PTS
, "calculated PCR Composite: %B", out_pcr_composite
);
1190 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
1191 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1192 hasher
->allocate_hash(hasher
, pcr_composite
, &hash_pcr_composite
);
1193 hasher
->destroy(hasher
);
1195 writer
->write_data(writer
, hash_pcr_composite
);
1196 chunk_clear(&pcr_composite
);
1197 chunk_clear(&hash_pcr_composite
);
1199 /* Hash the PCR Composite Structure */
1200 hasher
->allocate_hash(hasher
, pcr_composite
, out_pcr_composite
);
1201 DBG4(DBG_PTS
, "Hash of calculated PCR Composite: %B", out_pcr_composite
);
1202 hasher
->destroy(hasher
);
1206 *out_pcr_composite
= chunk_clone(pcr_composite
);
1207 DBG3(DBG_PTS
, "calculated PCR Composite: %B", out_pcr_composite
);
1210 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
1211 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1212 hasher
->allocate_hash(hasher
, pcr_composite
, &hash_pcr_composite
);
1213 hasher
->destroy(hasher
);
1215 /* Construct TPM_QUOTE_INFO/TPM_QUOTE_INFO2 structure */
1216 writer
= bio_writer_create(TPM_QUOTE_INFO_LEN
);
1220 /* TPM Structure Tag */
1221 writer
->write_uint16(writer
, TPM_TAG_QUOTE_INFO2
);
1223 /* Magic QUT2 value */
1224 writer
->write_data(writer
, chunk_create("QUT2", 4));
1226 /* Secret assessment value 20 bytes (nonce) */
1227 writer
->write_data(writer
, this->secret
);
1229 /* Length of the PCR selection field */
1230 writer
->write_uint16(writer
, size_of_select
);
1233 for (i
= 0; i
< size_of_select
; i
++)
1235 writer
->write_uint8(writer
, this->pcr_select
[i
]);
1238 /* TPM Locality Selection */
1239 writer
->write_uint8(writer
, TPM_LOC_ZERO
);
1241 /* PCR Composite Hash */
1242 writer
->write_data(writer
, hash_pcr_composite
);
1244 if (ver_info_included
)
1246 /* TPM version Info */
1247 writer
->write_data(writer
, this->tpm_version_info
);
1252 /* Version number */
1253 writer
->write_data(writer
, chunk_from_chars(1, 1, 0, 0));
1255 /* Magic QUOT value */
1256 writer
->write_data(writer
, chunk_create("QUOT", 4));
1258 /* PCR Composite Hash */
1259 writer
->write_data(writer
, hash_pcr_composite
);
1261 /* Secret assessment value 20 bytes (nonce) */
1262 writer
->write_data(writer
, this->secret
);
1265 chunk_clear(&pcr_composite
);
1266 chunk_clear(&hash_pcr_composite
);
1268 /* TPM Quote Info */
1269 *out_quote_info
= chunk_clone(writer
->get_buf(writer
));
1270 DBG3(DBG_PTS
, "Calculated TPM Quote Info: %B", out_quote_info
);
1272 writer
->destroy(writer
);
1278 METHOD(pts_t
, verify_quote_signature
, bool,
1279 private_pts_t
*this, chunk_t data
, chunk_t signature
)
1281 public_key_t
*aik_pub_key
;
1283 aik_pub_key
= this->aik
->get_public_key(this->aik
);
1286 DBG1(DBG_PTS
, "failed to get public key from AIK certificate");
1290 if (!aik_pub_key
->verify(aik_pub_key
, SIGN_RSA_EMSA_PKCS1_SHA1
,
1293 DBG1(DBG_PTS
, "signature verification failed for TPM Quote Info");
1294 DESTROY_IF(aik_pub_key
);
1299 if (!aik_pub_key
->get_encoding(aik_pub_key
,
1300 PUBKEY_SPKI_ASN1_DER
, &key_encoding
))
1302 DBG1(DBG_PTS
, "failed to get encoding of AIK public key");
1306 aik_pub_key
->destroy(aik_pub_key
);
1310 METHOD(pts_t
, destroy
, void,
1311 private_pts_t
*this)
1314 DESTROY_IF(this->aik
);
1315 DESTROY_IF(this->dh
);
1316 free(this->initiator_nonce
.ptr
);
1317 free(this->responder_nonce
.ptr
);
1318 free(this->secret
.ptr
);
1319 free(this->platform_info
);
1320 free(this->aik_blob
.ptr
);
1321 free(this->tpm_version_info
.ptr
);
1326 * Determine Linux distribution and hardware platform
1328 static char* extract_platform_info(void)
1331 char buf
[BUF_LEN
], *pos
, *value
= NULL
;
1333 struct utsname uninfo
;
1335 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
1336 const char* releases
[] = {
1337 "/etc/lsb-release", "/etc/debian_version",
1338 "/etc/SuSE-release", "/etc/novell-release",
1339 "/etc/sles-release", "/etc/redhat-release",
1340 "/etc/fedora-release", "/etc/gentoo-release",
1341 "/etc/slackware-version", "/etc/annvix-release",
1342 "/etc/arch-release", "/etc/arklinux-release",
1343 "/etc/aurox-release", "/etc/blackcat-release",
1344 "/etc/cobalt-release", "/etc/conectiva-release",
1345 "/etc/debian_release", "/etc/immunix-release",
1346 "/etc/lfs-release", "/etc/linuxppc-release",
1347 "/etc/mandrake-release", "/etc/mandriva-release",
1348 "/etc/mandrakelinux-release", "/etc/mklinux-release",
1349 "/etc/pld-release", "/etc/redhat_version",
1350 "/etc/slackware-release", "/etc/e-smith-release",
1351 "/etc/release", "/etc/sun-release",
1352 "/etc/tinysofa-release", "/etc/turbolinux-release",
1353 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
1354 "/etc/va-release", "/etc/yellowdog-release"
1357 const char description
[] = "DISTRIB_DESCRIPTION=\"";
1359 for (i
= 0; i
< countof(releases
); i
++)
1361 file
= fopen(releases
[i
], "r");
1366 fseek(file
, 0, SEEK_END
);
1367 len
= min(ftell(file
), sizeof(buf
)-1);
1370 if (fread(buf
, 1, len
, file
) != len
)
1372 DBG1(DBG_PTS
, "failed to read file '%s'", releases
[i
]);
1378 if (i
== 0) /* LSB release */
1380 pos
= strstr(buf
, description
);
1383 DBG1(DBG_PTS
, "failed to find begin of lsb-release "
1384 "DESCRIPTION field");
1387 value
= pos
+ strlen(description
);
1388 pos
= strchr(value
, '"');
1391 DBG1(DBG_PTS
, "failed to find end of lsb-release "
1392 "DESCRIPTION field");
1399 pos
= strchr(value
, '\n');
1402 DBG1(DBG_PTS
, "failed to find end of release string");
1411 DBG1(DBG_PTS
, "no distribution release file found");
1415 if (uname(&uninfo
) < 0)
1417 DBG1(DBG_PTS
, "could not retrieve machine architecture");
1422 len
= sizeof(buf
)-1 + (pos
- buf
);
1423 strncpy(pos
, uninfo
.machine
, len
);
1425 DBG1(DBG_PTS
, "platform is '%s'", value
);
1426 return strdup(value
);
1430 * Check for a TPM by querying for TPM Version Info
1432 static bool has_tpm(private_pts_t
*this)
1434 TSS_HCONTEXT hContext
;
1437 u_int32_t version_info_len
;
1439 result
= Tspi_Context_Create(&hContext
);
1440 if (result
!= TSS_SUCCESS
)
1442 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
1446 result
= Tspi_Context_Connect(hContext
, NULL
);
1447 if (result
!= TSS_SUCCESS
)
1451 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
1452 if (result
!= TSS_SUCCESS
)
1456 result
= Tspi_TPM_GetCapability(hTPM
, TSS_TPMCAP_VERSION_VAL
, 0, NULL
,
1458 &this->tpm_version_info
.ptr
);
1459 this->tpm_version_info
.len
= version_info_len
;
1460 if (result
!= TSS_SUCCESS
)
1464 this->tpm_version_info
= chunk_clone(this->tpm_version_info
);
1466 Tspi_Context_FreeMemory(hContext
, NULL
);
1467 Tspi_Context_Close(hContext
);
1471 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
1472 Tspi_Context_FreeMemory(hContext
, NULL
);
1473 Tspi_Context_Close(hContext
);
1480 pts_t
*pts_create(bool is_imc
)
1482 private_pts_t
*this;
1486 .get_proto_caps
= _get_proto_caps
,
1487 .set_proto_caps
= _set_proto_caps
,
1488 .get_meas_algorithm
= _get_meas_algorithm
,
1489 .set_meas_algorithm
= _set_meas_algorithm
,
1490 .get_dh_hash_algorithm
= _get_dh_hash_algorithm
,
1491 .set_dh_hash_algorithm
= _set_dh_hash_algorithm
,
1492 .create_dh_nonce
= _create_dh_nonce
,
1493 .get_my_public_value
= _get_my_public_value
,
1494 .set_peer_public_value
= _set_peer_public_value
,
1495 .calculate_secret
= _calculate_secret
,
1496 .get_platform_info
= _get_platform_info
,
1497 .set_platform_info
= _set_platform_info
,
1498 .get_tpm_version_info
= _get_tpm_version_info
,
1499 .set_tpm_version_info
= _set_tpm_version_info
,
1500 .get_aik
= _get_aik
,
1501 .set_aik
= _set_aik
,
1502 .is_path_valid
= _is_path_valid
,
1503 .hash_file
= _hash_file
,
1504 .do_measurements
= _do_measurements
,
1505 .get_metadata
= _get_metadata
,
1506 .read_pcr
= _read_pcr
,
1507 .extend_pcr
= _extend_pcr
,
1508 .quote_tpm
= _quote_tpm
,
1509 .select_pcr
= _select_pcr
,
1510 .add_pcr
= _add_pcr
,
1511 .get_quote_info
= _get_quote_info
,
1512 .verify_quote_signature
= _verify_quote_signature
,
1513 .destroy
= _destroy
,
1516 .proto_caps
= PTS_PROTO_CAPS_V
,
1517 .algorithm
= PTS_MEAS_ALGO_SHA256
,
1518 .dh_hash_algorithm
= PTS_MEAS_ALGO_SHA256
,
1523 this->platform_info
= extract_platform_info();
1527 this->has_tpm
= TRUE
;
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;