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 * List of extended PCR's with corresponding values
116 METHOD(pts_t
, get_proto_caps
, pts_proto_caps_flag_t
,
119 return this->proto_caps
;
122 METHOD(pts_t
, set_proto_caps
, void,
123 private_pts_t
*this, pts_proto_caps_flag_t flags
)
125 this->proto_caps
= flags
;
126 DBG2(DBG_PTS
, "supported PTS protocol capabilities: %s%s%s%s%s",
127 flags
& PTS_PROTO_CAPS_C ?
"C" : ".",
128 flags
& PTS_PROTO_CAPS_V ?
"V" : ".",
129 flags
& PTS_PROTO_CAPS_D ?
"D" : ".",
130 flags
& PTS_PROTO_CAPS_T ?
"T" : ".",
131 flags
& PTS_PROTO_CAPS_X ?
"X" : ".");
134 METHOD(pts_t
, get_meas_algorithm
, pts_meas_algorithms_t
,
137 return this->algorithm
;
140 METHOD(pts_t
, set_meas_algorithm
, void,
141 private_pts_t
*this, pts_meas_algorithms_t algorithm
)
143 hash_algorithm_t hash_alg
;
145 hash_alg
= pts_meas_algo_to_hash(algorithm
);
146 DBG2(DBG_PTS
, "selected PTS measurement algorithm is %N",
147 hash_algorithm_names
, hash_alg
);
148 if (hash_alg
!= HASH_UNKNOWN
)
150 this->algorithm
= algorithm
;
154 METHOD(pts_t
, get_dh_hash_algorithm
, pts_meas_algorithms_t
,
157 return this->dh_hash_algorithm
;
160 METHOD(pts_t
, set_dh_hash_algorithm
, void,
161 private_pts_t
*this, pts_meas_algorithms_t algorithm
)
163 hash_algorithm_t hash_alg
;
165 hash_alg
= pts_meas_algo_to_hash(algorithm
);
166 DBG2(DBG_PTS
, "selected DH hash algorithm is %N",
167 hash_algorithm_names
, hash_alg
);
168 if (hash_alg
!= HASH_UNKNOWN
)
170 this->dh_hash_algorithm
= algorithm
;
175 METHOD(pts_t
, create_dh_nonce
, bool,
176 private_pts_t
*this, pts_dh_group_t group
, int nonce_len
)
178 diffie_hellman_group_t dh_group
;
182 dh_group
= pts_dh_group_to_ike(group
);
183 DBG2(DBG_PTS
, "selected PTS DH group is %N",
184 diffie_hellman_group_names
, dh_group
);
185 DESTROY_IF(this->dh
);
186 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, dh_group
);
188 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_STRONG
);
191 DBG1(DBG_PTS
, "no rng available");
194 DBG2(DBG_PTS
, "nonce length is %d", nonce_len
);
195 nonce
= this->is_imc ?
&this->responder_nonce
: &this->initiator_nonce
;
197 rng
->allocate_bytes(rng
, nonce_len
, nonce
);
203 METHOD(pts_t
, get_my_public_value
, void,
204 private_pts_t
*this, chunk_t
*value
, chunk_t
*nonce
)
206 this->dh
->get_my_public_value(this->dh
, value
);
207 *nonce
= this->is_imc ?
this->responder_nonce
: this->initiator_nonce
;
210 METHOD(pts_t
, set_peer_public_value
, void,
211 private_pts_t
*this, chunk_t value
, chunk_t nonce
)
213 this->dh
->set_other_public_value(this->dh
, value
);
215 nonce
= chunk_clone(nonce
);
218 this->initiator_nonce
= nonce
;
222 this->responder_nonce
= nonce
;
226 METHOD(pts_t
, calculate_secret
, bool,
230 hash_algorithm_t hash_alg
;
231 chunk_t shared_secret
;
233 /* Check presence of nonces */
234 if (!this->initiator_nonce
.len
|| !this->responder_nonce
.len
)
236 DBG1(DBG_PTS
, "initiator and/or responder nonce is not available");
239 DBG3(DBG_PTS
, "initiator nonce: %B", &this->initiator_nonce
);
240 DBG3(DBG_PTS
, "responder nonce: %B", &this->responder_nonce
);
242 /* Calculate the DH secret */
243 if (this->dh
->get_shared_secret(this->dh
, &shared_secret
) != SUCCESS
)
245 DBG1(DBG_PTS
, "shared DH secret computation failed");
248 DBG3(DBG_PTS
, "shared DH secret: %B", &shared_secret
);
250 /* Calculate the secret assessment value */
251 hash_alg
= pts_meas_algo_to_hash(this->dh_hash_algorithm
);
252 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
254 hasher
->allocate_hash(hasher
, chunk_from_chars('1'), NULL
);
255 hasher
->allocate_hash(hasher
, this->initiator_nonce
, NULL
);
256 hasher
->allocate_hash(hasher
, this->responder_nonce
, NULL
);
257 hasher
->allocate_hash(hasher
, shared_secret
, &this->secret
);
258 hasher
->destroy(hasher
);
260 /* The DH secret must be destroyed */
261 chunk_clear(&shared_secret
);
264 * Truncate the hash to 20 bytes to fit the ExternalData
265 * argument of the TPM Quote command
267 this->secret
.len
= min(this->secret
.len
, 20);
268 DBG3(DBG_PTS
, "secret assessment value: %B", &this->secret
);
273 * Print TPM 1.2 Version Info
275 static void print_tpm_version_info(private_pts_t
*this)
277 TPM_CAP_VERSION_INFO versionInfo
;
281 result
= Trspi_UnloadBlob_CAP_VERSION_INFO(&offset
,
282 this->tpm_version_info
.ptr
, &versionInfo
);
283 if (result
!= TSS_SUCCESS
)
285 DBG1(DBG_PTS
, "could not parse tpm version info: tss error 0x%x",
290 DBG2(DBG_PTS
, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
291 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
292 versionInfo
.version
.major
, versionInfo
.version
.minor
,
293 versionInfo
.version
.revMajor
, versionInfo
.version
.revMinor
,
294 versionInfo
.specLevel
, versionInfo
.errataRev
,
295 versionInfo
.tpmVendorID
);
299 METHOD(pts_t
, get_platform_info
, char*,
302 return this->platform_info
;
305 METHOD(pts_t
, set_platform_info
, void,
306 private_pts_t
*this, char *info
)
308 free(this->platform_info
);
309 this->platform_info
= strdup(info
);
312 METHOD(pts_t
, get_tpm_version_info
, bool,
313 private_pts_t
*this, chunk_t
*info
)
319 *info
= this->tpm_version_info
;
320 print_tpm_version_info(this);
324 METHOD(pts_t
, set_tpm_version_info
, void,
325 private_pts_t
*this, chunk_t info
)
327 this->tpm_version_info
= chunk_clone(info
);
328 print_tpm_version_info(this);
332 * Load an AIK Blob (TSS_TSPATTRIB_KEYBLOB_BLOB attribute)
334 static void load_aik_blob(private_pts_t
*this)
338 u_int32_t aikBlobLen
;
340 blob_path
= lib
->settings
->get_str(lib
->settings
,
341 "libimcv.plugins.imc-attestation.aik_blob", NULL
);
345 /* Read aik key blob from a file */
346 if ((fp
= fopen(blob_path
, "r")) == NULL
)
348 DBG1(DBG_PTS
, "unable to open AIK Blob file: %s", blob_path
);
352 fseek(fp
, 0, SEEK_END
);
353 aikBlobLen
= ftell(fp
);
354 fseek(fp
, 0L, SEEK_SET
);
356 this->aik_blob
= chunk_alloc(aikBlobLen
);
357 if (fread(this->aik_blob
.ptr
, 1, aikBlobLen
, fp
))
359 DBG2(DBG_PTS
, "loaded AIK Blob from '%s'", blob_path
);
360 DBG3(DBG_PTS
, "AIK Blob: %B", &this->aik_blob
);
364 DBG1(DBG_PTS
, "unable to read AIK Blob file '%s'", blob_path
);
370 DBG1(DBG_PTS
, "AIK Blob is not available");
374 * Load an AIK certificate or public key
375 * the certificate having precedence over the public key if both are present
377 static void load_aik(private_pts_t
*this)
379 char *cert_path
, *key_path
;
381 cert_path
= lib
->settings
->get_str(lib
->settings
,
382 "libimcv.plugins.imc-attestation.aik_cert", NULL
);
383 key_path
= lib
->settings
->get_str(lib
->settings
,
384 "libimcv.plugins.imc-attestation.aik_key", NULL
);
388 this->aik
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
389 CERT_X509
, BUILD_FROM_FILE
,
390 cert_path
, BUILD_END
);
393 DBG2(DBG_PTS
, "loaded AIK certificate from '%s'", cert_path
);
399 this->aik
= lib
->creds
->create(lib
->creds
, CRED_CERTIFICATE
,
400 CERT_TRUSTED_PUBKEY
, BUILD_FROM_FILE
,
401 key_path
, BUILD_END
);
404 DBG2(DBG_PTS
, "loaded AIK public key from '%s'", key_path
);
409 DBG1(DBG_PTS
, "neither AIK certificate nor public key is available");
412 METHOD(pts_t
, get_aik
, certificate_t
*,
418 METHOD(pts_t
, set_aik
, void,
419 private_pts_t
*this, certificate_t
*aik
)
421 DESTROY_IF(this->aik
);
422 this->aik
= aik
->get_ref(aik
);
425 METHOD(pts_t
, hash_file
, bool,
426 private_pts_t
*this, hasher_t
*hasher
, char *pathname
, u_char
*hash
)
428 u_char buffer
[PTS_BUF_SIZE
];
432 file
= fopen(pathname
, "rb");
435 DBG1(DBG_PTS
," file '%s' can not be opened, %s", pathname
,
441 bytes_read
= fread(buffer
, 1, sizeof(buffer
), file
);
444 hasher
->get_hash(hasher
, chunk_create(buffer
, bytes_read
), NULL
);
448 hasher
->get_hash(hasher
, chunk_empty
, hash
);
458 * Get the relative filename of a fully qualified file pathname
460 static char* get_filename(char *pathname
)
462 char *pos
, *filename
;
464 pos
= filename
= pathname
;
465 while (pos
&& *(++pos
) != '\0')
468 pos
= strchr(filename
, '/');
473 METHOD(pts_t
, is_path_valid
, bool,
474 private_pts_t
*this, char *path
, pts_error_code_t
*error_code
)
480 if (!stat(path
, &st
))
484 else if (errno
== ENOENT
|| errno
== ENOTDIR
)
486 DBG1(DBG_PTS
, "file/directory does not exist %s", path
);
487 *error_code
= TCG_PTS_FILE_NOT_FOUND
;
489 else if (errno
== EFAULT
)
491 DBG1(DBG_PTS
, "bad address %s", path
);
492 *error_code
= TCG_PTS_INVALID_PATH
;
496 DBG1(DBG_PTS
, "error: %s occured while validating path: %s",
497 strerror(errno
), path
);
504 METHOD(pts_t
, do_measurements
, pts_file_meas_t
*,
505 private_pts_t
*this, u_int16_t request_id
, char *pathname
, bool is_directory
)
508 hash_algorithm_t hash_alg
;
509 u_char hash
[HASH_SIZE_SHA384
];
511 pts_file_meas_t
*measurements
;
513 /* Create a hasher */
514 hash_alg
= pts_meas_algo_to_hash(this->algorithm
);
515 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, hash_alg
);
518 DBG1(DBG_PTS
, "hasher %N not available", hash_algorithm_names
, hash_alg
);
522 /* Create a measurement object */
523 measurements
= pts_file_meas_create(request_id
);
525 /* Link the hash to the measurement and set the measurement length */
526 measurement
= chunk_create(hash
, hasher
->get_hash_size(hasher
));
530 enumerator_t
*enumerator
;
531 char *rel_name
, *abs_name
;
534 enumerator
= enumerator_create_directory(pathname
);
537 DBG1(DBG_PTS
," directory '%s' can not be opened, %s", pathname
,
539 hasher
->destroy(hasher
);
540 measurements
->destroy(measurements
);
543 while (enumerator
->enumerate(enumerator
, &rel_name
, &abs_name
, &st
))
545 /* measure regular files only */
546 if (S_ISREG(st
.st_mode
) && *rel_name
!= '.')
548 if (!hash_file(this, hasher
, abs_name
, hash
))
550 enumerator
->destroy(enumerator
);
551 hasher
->destroy(hasher
);
552 measurements
->destroy(measurements
);
555 DBG2(DBG_PTS
, " %#B for '%s'", &measurement
, rel_name
);
556 measurements
->add(measurements
, rel_name
, measurement
);
559 enumerator
->destroy(enumerator
);
565 if (!hash_file(this, hasher
, pathname
, hash
))
567 hasher
->destroy(hasher
);
568 measurements
->destroy(measurements
);
571 filename
= get_filename(pathname
);
572 DBG2(DBG_PTS
, " %#B for '%s'", &measurement
, filename
);
573 measurements
->add(measurements
, filename
, measurement
);
575 hasher
->destroy(hasher
);
581 * Obtain statistical information describing a file
583 static bool file_metadata(char *pathname
, pts_file_metadata_t
**entry
)
586 pts_file_metadata_t
*this;
588 this = malloc_thing(pts_file_metadata_t
);
590 if (stat(pathname
, &st
))
592 DBG1(DBG_PTS
, "Unable to obtain statistics about '%s'", pathname
);
596 if (S_ISREG(st
.st_mode
))
598 this->type
= PTS_FILE_REGULAR
;
600 else if (S_ISDIR(st
.st_mode
))
602 this->type
= PTS_FILE_DIRECTORY
;
604 else if (S_ISCHR(st
.st_mode
))
606 this->type
= PTS_FILE_CHAR_SPEC
;
608 else if (S_ISBLK(st
.st_mode
))
610 this->type
= PTS_FILE_BLOCK_SPEC
;
612 else if (S_ISFIFO(st
.st_mode
))
614 this->type
= PTS_FILE_FIFO
;
616 else if (S_ISLNK(st
.st_mode
))
618 this->type
= PTS_FILE_SYM_LINK
;
620 else if (S_ISSOCK(st
.st_mode
))
622 this->type
= PTS_FILE_SOCKET
;
626 this->type
= PTS_FILE_OTHER
;
629 this->filesize
= st
.st_size
;
630 this->created
= st
.st_ctime
;
631 this->modified
= st
.st_mtime
;
632 this->accessed
= st
.st_atime
;
633 this->owner
= st
.st_uid
;
634 this->group
= st
.st_gid
;
640 METHOD(pts_t
, get_metadata
, pts_file_meta_t
*,
641 private_pts_t
*this, char *pathname
, bool is_directory
)
643 pts_file_meta_t
*metadata
;
644 pts_file_metadata_t
*entry
;
646 /* Create a metadata object */
647 metadata
= pts_file_meta_create();
651 enumerator_t
*enumerator
;
652 char *rel_name
, *abs_name
;
655 enumerator
= enumerator_create_directory(pathname
);
658 DBG1(DBG_PTS
," directory '%s' can not be opened, %s", pathname
,
660 metadata
->destroy(metadata
);
663 while (enumerator
->enumerate(enumerator
, &rel_name
, &abs_name
, &st
))
665 /* measure regular files only */
666 if (S_ISREG(st
.st_mode
) && *rel_name
!= '.')
668 if (!file_metadata(abs_name
, &entry
))
670 enumerator
->destroy(enumerator
);
671 metadata
->destroy(metadata
);
674 entry
->filename
= strdup(rel_name
);
675 metadata
->add(metadata
, entry
);
678 enumerator
->destroy(enumerator
);
682 if (!file_metadata(pathname
, &entry
))
684 metadata
->destroy(metadata
);
687 entry
->filename
= strdup(get_filename(pathname
));
688 metadata
->add(metadata
, entry
);
694 METHOD(pts_t
, read_pcr
, bool,
695 private_pts_t
*this, u_int32_t pcr_num
, chunk_t
*output
)
697 TSS_HCONTEXT hContext
;
700 u_int32_t pcr_length
;
703 result
= Tspi_Context_Create(&hContext
);
704 if (result
!= TSS_SUCCESS
)
706 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
711 result
= Tspi_Context_Connect(hContext
, NULL
);
712 if (result
!= TSS_SUCCESS
)
716 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
717 if (result
!= TSS_SUCCESS
)
721 pcr_value
= chunk_alloc(PCR_LEN
);
722 result
= Tspi_TPM_PcrRead(hTPM
, pcr_num
, &pcr_length
, &pcr_value
.ptr
);
723 if (result
!= TSS_SUCCESS
)
729 *output
= chunk_clone(*output
);
731 chunk_clear(&pcr_value
);
732 DBG3(DBG_PTS
, "PCR %d value:%B", pcr_num
, output
);
733 Tspi_Context_Close(hContext
);
737 chunk_clear(&pcr_value
);
738 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
739 Tspi_Context_Close(hContext
);
743 METHOD(pts_t
, extend_pcr
, bool,
744 private_pts_t
*this, u_int32_t pcr_num
, chunk_t input
, chunk_t
*output
)
746 TSS_HCONTEXT hContext
;
749 u_int32_t pcr_length
;
752 result
= Tspi_Context_Create(&hContext
);
753 if (result
!= TSS_SUCCESS
)
755 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
759 result
= Tspi_Context_Connect(hContext
, NULL
);
760 if (result
!= TSS_SUCCESS
)
764 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
765 if (result
!= TSS_SUCCESS
)
770 pcr_value
= chunk_alloc(PCR_LEN
);
771 result
= Tspi_TPM_PcrExtend(hTPM
, pcr_num
, PCR_LEN
, input
.ptr
,
772 NULL
, &pcr_length
, &pcr_value
.ptr
);
773 if (result
!= TSS_SUCCESS
)
779 *output
= chunk_clone(*output
);
781 chunk_clear(&pcr_value
);
782 Tspi_Context_Close(hContext
);
783 DBG3(DBG_PTS
, "PCR %d extended with: %B", pcr_num
, &input
);
784 DBG3(DBG_PTS
, "PCR %d value after extend: %B", pcr_num
, output
);
788 chunk_clear(&pcr_value
);
789 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
790 Tspi_Context_Close(hContext
);
794 METHOD(pts_t
, quote_tpm
, bool,
795 private_pts_t
*this, bool use_quote2
,
796 u_int32_t
*pcrs
, u_int32_t num_of_pcrs
,
797 chunk_t
*pcr_composite
, chunk_t
*quote_signature
)
799 TSS_HCONTEXT hContext
;
803 TSS_HPOLICY srkUsagePolicy
;
804 TSS_UUID SRK_UUID
= TSS_UUID_SRK
;
805 BYTE secret
[] = TSS_WELL_KNOWN_SECRET
;
806 TSS_HPCRS hPcrComposite
;
807 TSS_VALIDATION valData
;
808 u_int32_t i
, versionInfoSize
;
810 chunk_t pcr_comp
, quote_sign
;
813 result
= Tspi_Context_Create(&hContext
);
814 if (result
!= TSS_SUCCESS
)
816 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
820 result
= Tspi_Context_Connect(hContext
, NULL
);
821 if (result
!= TSS_SUCCESS
)
825 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
826 if (result
!= TSS_SUCCESS
)
831 /* Retrieve SRK from TPM and set the authentication to well known secret*/
832 result
= Tspi_Context_LoadKeyByUUID(hContext
, TSS_PS_TYPE_SYSTEM
,
834 if (result
!= TSS_SUCCESS
)
839 result
= Tspi_GetPolicyObject(hSRK
, TSS_POLICY_USAGE
, &srkUsagePolicy
);
840 if (result
!= TSS_SUCCESS
)
844 result
= Tspi_Policy_SetSecret(srkUsagePolicy
, TSS_SECRET_MODE_SHA1
,
846 if (result
!= TSS_SUCCESS
)
851 result
= Tspi_Context_LoadKeyByBlob (hContext
, hSRK
, this->aik_blob
.len
,
852 this->aik_blob
.ptr
, &hAIK
);
853 if (result
!= TSS_SUCCESS
)
858 /* Create PCR composite object */
859 result
= use_quote2 ?
860 Tspi_Context_CreateObject(hContext
, TSS_OBJECT_TYPE_PCRS
,
861 TSS_PCRS_STRUCT_INFO_SHORT
, &hPcrComposite
) :
862 Tspi_Context_CreateObject(hContext
, TSS_OBJECT_TYPE_PCRS
,
864 if (result
!= TSS_SUCCESS
)
870 for (i
= 0; i
< num_of_pcrs
; i
++)
872 if (pcrs
[i
] < 0 || pcrs
[i
] >= MAX_NUM_PCR
)
874 DBG1(DBG_PTS
, "Invalid PCR number: %d", pcrs
[i
]);
877 result
= use_quote2 ?
878 Tspi_PcrComposite_SelectPcrIndexEx(hPcrComposite
, pcrs
[i
],
879 TSS_PCRS_DIRECTION_RELEASE
):
880 Tspi_PcrComposite_SelectPcrIndex(hPcrComposite
, pcrs
[i
]);
881 if (result
!= TSS_SUCCESS
)
887 /* Set the Validation Data */
888 valData
.ulExternalDataLength
= this->secret
.len
;
889 valData
.rgbExternalData
= (BYTE
*)this->secret
.ptr
;
893 result
= use_quote2 ?
894 Tspi_TPM_Quote2(hTPM
, hAIK
, FALSE
, hPcrComposite
, &valData
,
895 &versionInfoSize
,&versionInfo
):
896 Tspi_TPM_Quote(hTPM
, hAIK
, hPcrComposite
, &valData
);
897 if (result
!= TSS_SUCCESS
)
902 /* Set output chunks */
903 pcr_comp
= chunk_alloc(HASH_SIZE_SHA1
);
906 /* TPM_Composite_Hash is last 20 bytes of TPM_Quote_Info2 structure */
907 memcpy(pcr_comp
.ptr
, valData
.rgbData
+ valData
.ulDataLength
- HASH_SIZE_SHA1
,
912 /* TPM_Composite_Hash is 8-28th bytes of TPM_Quote_Info structure */
913 memcpy(pcr_comp
.ptr
, valData
.rgbData
+ 8, HASH_SIZE_SHA1
);
916 *pcr_composite
= pcr_comp
;
917 *pcr_composite
= chunk_clone(*pcr_composite
);
918 DBG3(DBG_PTS
, "Hash of PCR Composite: %B",pcr_composite
);
920 chunk_t tmp
= chunk_create(valData
.rgbData
, valData
.ulDataLength
);
921 DBG3(DBG_PTS
, "TPM Quote Info: %B",&tmp
);
923 quote_sign
= chunk_alloc(valData
.ulValidationDataLength
);
924 memcpy(quote_sign
.ptr
, valData
.rgbValidationData
,
925 valData
.ulValidationDataLength
);
926 *quote_signature
= quote_sign
;
927 *quote_signature
= chunk_clone(*quote_signature
);
928 DBG3(DBG_PTS
, "TPM Quote Signature: %B",quote_signature
);
930 chunk_clear("e_sign
);
931 Tspi_Context_FreeMemory(hContext
, NULL
);
932 Tspi_Context_CloseObject(hContext
, hPcrComposite
);
933 Tspi_Context_CloseObject(hContext
, hAIK
);
934 Tspi_Context_Close(hContext
);
940 Tspi_Context_FreeMemory(hContext
, NULL
);
943 Tspi_Context_CloseObject(hContext
, hPcrComposite
);
946 Tspi_Context_CloseObject(hContext
, hAIK
);
949 Tspi_Context_Close(hContext
);
951 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
955 METHOD(pts_t
, add_pcr_entry
, void,
956 private_pts_t
*this, pcr_entry_t
*new)
963 this->pcrs
= linked_list_create();
966 e
= this->pcrs
->create_enumerator(this->pcrs
);
967 while (e
->enumerate(e
, &entry
))
969 if (entry
->pcr_number
== new->pcr_number
)
971 DBG3(DBG_PTS
, "updating already added PCR%d value",
973 this->pcrs
->remove_at(this->pcrs
, e
);
979 this->pcrs
->insert_last(this->pcrs
, new);
983 * Get the maximum PCR index received in pcr_after_value field
985 static u_int32_t
get_max_pcr_index(private_pts_t
*this)
988 pcr_entry_t
*pcr_entry
;
991 if (this->pcrs
->get_count(this->pcrs
) == 0)
996 e
= this->pcrs
->create_enumerator(this->pcrs
);
997 while (e
->enumerate(e
, &pcr_entry
))
999 if (pcr_entry
->pcr_number
> ret
)
1001 ret
= pcr_entry
->pcr_number
;
1009 METHOD(pts_t
, does_pcr_value_match
, bool,
1010 private_pts_t
*this, chunk_t pcr_after_value
)
1017 this->pcrs
= linked_list_create();
1020 e
= this->pcrs
->create_enumerator(this->pcrs
);
1021 while (e
->enumerate(e
, &entry
))
1023 if (entry
->pcr_number
== new->pcr_number
)
1025 DBG4(DBG_PTS
, "updating already added PCR%d value",
1027 this->pcrs
->remove_at(this->pcrs
, e
);
1033 this->pcrs
->insert_last(this->pcrs
, new);
1037 * TPM_QUOTE_INFO structure:
1038 * 4 bytes of version
1039 * 4 bytes 'Q' 'U' 'O' 'T'
1040 * 20 byte SHA1 of TCPA_PCR_COMPOSITE
1043 * TPM_QUOTE_INFO2 structure:
1044 * 2 bytes Tag 0x0036 TPM_Tag_Quote_info2
1045 * 4 bytes 'Q' 'U' 'T' '2'
1047 * 26 bytes PCR_INFO_SHORT
1050 METHOD(pts_t
, get_quote_info
, bool,
1051 private_pts_t
*this, bool use_quote2
, bool ver_info_included
,
1052 pts_meas_algorithms_t composite_algo
,
1053 chunk_t
*out_pcr_composite
, chunk_t
*out_quote_info
)
1056 pcr_entry_t
*pcr_entry
;
1057 chunk_t pcr_composite
, hash_pcr_composite
;
1058 u_int32_t pcr_composite_len
, i
, maximum_pcr_index
, bitmask_len
;
1059 bio_writer_t
*writer
;
1062 maximum_pcr_index
= get_max_pcr_index(this);
1063 if (maximum_pcr_index
== -1)
1065 DBG1(DBG_PTS
, "PCR entries unavailable, unable to construct "
1069 if (!this->secret
.ptr
)
1071 DBG1(DBG_PTS
, "Secret assessment value unavailable",
1072 "unable to construct TPM Quote Info");
1075 if (use_quote2
&& ver_info_included
&& !this->tpm_version_info
.ptr
)
1077 DBG1(DBG_PTS
, "TPM Version Information unavailable",
1078 "unable to construct TPM Quote Info2");
1082 bitmask_len
= maximum_pcr_index
/8 +1;
1083 u_int8_t mask_bytes
[MAX_NUM_PCR
/8] = {0};
1085 pcr_composite_len
= 2 + bitmask_len
+ 4 +
1086 this->pcrs
->get_count(this->pcrs
) * PCR_LEN
;
1088 writer
= bio_writer_create(pcr_composite_len
);
1089 /* Lenght of the bist mask field */
1090 writer
->write_uint16(writer
, bitmask_len
);
1091 /* Bit mask indicating selected PCRs */
1092 e
= this->pcrs
->create_enumerator(this->pcrs
);
1093 while (e
->enumerate(e
, &pcr_entry
))
1095 u_int32_t index
= pcr_entry
->pcr_number
;
1096 mask_bytes
[index
/ 8] |= (1 << (index
% 8));
1100 for (i
= 0; i
< bitmask_len
; i
++)
1102 writer
->write_uint8(writer
, mask_bytes
[i
]);
1105 /* Lenght of the pcr entries */
1106 writer
->write_uint32(writer
, this->pcrs
->get_count(this->pcrs
) * PCR_LEN
);
1107 /* Actual PCR values */
1108 e
= this->pcrs
->create_enumerator(this->pcrs
);
1109 while (e
->enumerate(e
, &pcr_entry
))
1111 writer
->write_data(writer
, chunk_create(pcr_entry
->pcr_value
, PCR_LEN
));
1116 /* PCR Composite structure */
1117 pcr_composite
= chunk_clone(writer
->get_buf(writer
));
1118 DBG3(DBG_PTS
, "PCR Composite: %B", &pcr_composite
);
1119 writer
->destroy(writer
);
1121 /* Output the TPM_PCR_COMPOSITE expected from IMC */
1124 hash_algorithm_t algo
;
1126 algo
= pts_meas_algo_to_hash(composite_algo
);
1127 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, algo
);
1129 /* Hash the PCR Composite Structure */
1130 hasher
->allocate_hash(hasher
, pcr_composite
, out_pcr_composite
);
1131 DBG3(DBG_PTS
, "Hash of calculated PCR Composite: %B", out_pcr_composite
);
1132 hasher
->destroy(hasher
);
1136 *out_pcr_composite
= chunk_clone(pcr_composite
);
1137 DBG3(DBG_PTS
, "calculated PCR Composite: %B", out_pcr_composite
);
1140 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
1141 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1142 hasher
->allocate_hash(hasher
, pcr_composite
, &hash_pcr_composite
);
1143 hasher
->destroy(hasher
);
1145 writer
->write_data(writer
, hash_pcr_composite
);
1146 chunk_clear(&pcr_composite
);
1147 chunk_clear(&hash_pcr_composite
);
1149 /* Hash the PCR Composite Structure */
1150 hasher
->allocate_hash(hasher
, pcr_composite
, out_pcr_composite
);
1151 DBG4(DBG_PTS
, "Hash of calculated PCR Composite: %B", out_pcr_composite
);
1152 hasher
->destroy(hasher
);
1156 *out_pcr_composite
= chunk_clone(pcr_composite
);
1157 DBG3(DBG_PTS
, "calculated PCR Composite: %B", out_pcr_composite
);
1160 /* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
1161 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
1162 hasher
->allocate_hash(hasher
, pcr_composite
, &hash_pcr_composite
);
1163 hasher
->destroy(hasher
);
1165 /* Construct TPM_QUOTE_INFO/TPM_QUOTE_INFO2 structure */
1166 writer
= bio_writer_create(TPM_QUOTE_INFO_LEN
);
1170 /* TPM Structure Tag */
1171 writer
->write_uint16(writer
, TPM_TAG_QUOTE_INFO2
);
1173 /* Magic QUT2 value */
1174 writer
->write_uint8(writer
, 'Q');
1175 writer
->write_uint8(writer
, 'U');
1176 writer
->write_uint8(writer
, 'T');
1177 writer
->write_uint8(writer
, '2');
1179 /* Secret assessment value 20 bytes (nonce) */
1180 writer
->write_data(writer
, this->secret
);
1182 /* Lenght of the bist mask field */
1183 writer
->write_uint16(writer
, bitmask_len
);
1185 /* PCR selection Bitmask */
1186 for (i
= 0; i
< bitmask_len
; i
++)
1188 writer
->write_uint8(writer
, mask_bytes
[i
]);
1191 /* TPM Locality Selection */
1192 writer
->write_uint8(writer
, TPM_LOC_ZERO
);
1194 /* PCR Composite Hash */
1195 writer
->write_data(writer
, hash_pcr_composite
);
1197 if (ver_info_included
)
1199 /* TPM version Info */
1200 writer
->write_data(writer
, this->tpm_version_info
);
1205 /* Version number */
1206 writer
->write_uint8(writer
, 1);
1207 writer
->write_uint8(writer
, 1);
1208 writer
->write_uint8(writer
, 0);
1209 writer
->write_uint8(writer
, 0);
1211 /* Magic QUOT value */
1212 writer
->write_uint8(writer
, 'Q');
1213 writer
->write_uint8(writer
, 'U');
1214 writer
->write_uint8(writer
, 'O');
1215 writer
->write_uint8(writer
, 'T');
1217 /* PCR Composite Hash */
1218 writer
->write_data(writer
, hash_pcr_composite
);
1220 /* Secret assessment value 20 bytes (nonce) */
1221 writer
->write_data(writer
, this->secret
);
1224 chunk_clear(&pcr_composite
);
1225 chunk_clear(&hash_pcr_composite
);
1227 /* TPM Quote Info */
1228 *out_quote_info
= chunk_clone(writer
->get_buf(writer
));
1229 DBG3(DBG_PTS
, "Calculated TPM Quote Info: %B", out_quote_info
);
1230 writer
->destroy(writer
);
1235 METHOD(pts_t
, verify_quote_signature
, bool,
1236 private_pts_t
*this, chunk_t data
, chunk_t signature
)
1238 public_key_t
*aik_pub_key
;
1240 aik_pub_key
= this->aik
->get_public_key(this->aik
);
1243 DBG1(DBG_PTS
, "failed to get public key from AIK certificate");
1247 if (!aik_pub_key
->verify(aik_pub_key
, SIGN_RSA_EMSA_PKCS1_SHA1
,
1250 DBG1(DBG_PTS
, "signature verification failed for TPM Quote Info");
1251 DESTROY_IF(aik_pub_key
);
1256 if (!aik_pub_key
->get_encoding(aik_pub_key
,
1257 PUBKEY_SPKI_ASN1_DER
, &key_encoding
))
1259 DBG1(DBG_PTS
, "failed to get encoding of AIK public key");
1263 aik_pub_key
->destroy(aik_pub_key
);
1267 METHOD(pts_t
, destroy
, void,
1268 private_pts_t
*this)
1270 DESTROY_IF(this->aik
);
1271 DESTROY_IF(this->dh
);
1272 DESTROY_IF(this->pcrs
);
1273 free(this->initiator_nonce
.ptr
);
1274 free(this->responder_nonce
.ptr
);
1275 free(this->secret
.ptr
);
1276 free(this->platform_info
);
1277 free(this->aik_blob
.ptr
);
1278 free(this->tpm_version_info
.ptr
);
1283 * Determine Linux distribution and hardware platform
1285 static char* extract_platform_info(void)
1288 char buf
[BUF_LEN
], *pos
, *value
= NULL
;
1290 struct utsname uninfo
;
1292 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
1293 const char* releases
[] = {
1294 "/etc/lsb-release", "/etc/debian_version",
1295 "/etc/SuSE-release", "/etc/novell-release",
1296 "/etc/sles-release", "/etc/redhat-release",
1297 "/etc/fedora-release", "/etc/gentoo-release",
1298 "/etc/slackware-version", "/etc/annvix-release",
1299 "/etc/arch-release", "/etc/arklinux-release",
1300 "/etc/aurox-release", "/etc/blackcat-release",
1301 "/etc/cobalt-release", "/etc/conectiva-release",
1302 "/etc/debian_release", "/etc/immunix-release",
1303 "/etc/lfs-release", "/etc/linuxppc-release",
1304 "/etc/mandrake-release", "/etc/mandriva-release",
1305 "/etc/mandrakelinux-release", "/etc/mklinux-release",
1306 "/etc/pld-release", "/etc/redhat_version",
1307 "/etc/slackware-release", "/etc/e-smith-release",
1308 "/etc/release", "/etc/sun-release",
1309 "/etc/tinysofa-release", "/etc/turbolinux-release",
1310 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
1311 "/etc/va-release", "/etc/yellowdog-release"
1314 const char description
[] = "DISTRIB_DESCRIPTION=\"";
1316 for (i
= 0; i
< countof(releases
); i
++)
1318 file
= fopen(releases
[i
], "r");
1323 fseek(file
, 0, SEEK_END
);
1324 len
= min(ftell(file
), sizeof(buf
)-1);
1327 if (fread(buf
, 1, len
, file
) != len
)
1329 DBG1(DBG_PTS
, "failed to read file '%s'", releases
[i
]);
1335 if (i
== 0) /* LSB release */
1337 pos
= strstr(buf
, description
);
1340 DBG1(DBG_PTS
, "failed to find begin of lsb-release "
1341 "DESCRIPTION field");
1344 value
= pos
+ strlen(description
);
1345 pos
= strchr(value
, '"');
1348 DBG1(DBG_PTS
, "failed to find end of lsb-release "
1349 "DESCRIPTION field");
1356 pos
= strchr(value
, '\n');
1359 DBG1(DBG_PTS
, "failed to find end of release string");
1368 DBG1(DBG_PTS
, "no distribution release file found");
1372 if (uname(&uninfo
) < 0)
1374 DBG1(DBG_PTS
, "could not retrieve machine architecture");
1379 len
= sizeof(buf
)-1 + (pos
- buf
);
1380 strncpy(pos
, uninfo
.machine
, len
);
1382 DBG1(DBG_PTS
, "platform is '%s'", value
);
1383 return strdup(value
);
1387 * Check for a TPM by querying for TPM Version Info
1389 static bool has_tpm(private_pts_t
*this)
1391 TSS_HCONTEXT hContext
;
1394 u_int32_t version_info_len
;
1396 result
= Tspi_Context_Create(&hContext
);
1397 if (result
!= TSS_SUCCESS
)
1399 DBG1(DBG_PTS
, "TPM context could not be created: tss error 0x%x",
1403 result
= Tspi_Context_Connect(hContext
, NULL
);
1404 if (result
!= TSS_SUCCESS
)
1408 result
= Tspi_Context_GetTpmObject (hContext
, &hTPM
);
1409 if (result
!= TSS_SUCCESS
)
1413 result
= Tspi_TPM_GetCapability(hTPM
, TSS_TPMCAP_VERSION_VAL
, 0, NULL
,
1415 &this->tpm_version_info
.ptr
);
1416 this->tpm_version_info
.len
= version_info_len
;
1417 if (result
!= TSS_SUCCESS
)
1421 this->tpm_version_info
= chunk_clone(this->tpm_version_info
);
1423 Tspi_Context_FreeMemory(hContext
, NULL
);
1424 Tspi_Context_Close(hContext
);
1428 DBG1(DBG_PTS
, "TPM not available: tss error 0x%x", result
);
1429 Tspi_Context_FreeMemory(hContext
, NULL
);
1430 Tspi_Context_Close(hContext
);
1437 pts_t
*pts_create(bool is_imc
)
1439 private_pts_t
*this;
1443 .get_proto_caps
= _get_proto_caps
,
1444 .set_proto_caps
= _set_proto_caps
,
1445 .get_meas_algorithm
= _get_meas_algorithm
,
1446 .set_meas_algorithm
= _set_meas_algorithm
,
1447 .get_dh_hash_algorithm
= _get_dh_hash_algorithm
,
1448 .set_dh_hash_algorithm
= _set_dh_hash_algorithm
,
1449 .create_dh_nonce
= _create_dh_nonce
,
1450 .get_my_public_value
= _get_my_public_value
,
1451 .set_peer_public_value
= _set_peer_public_value
,
1452 .calculate_secret
= _calculate_secret
,
1453 .get_platform_info
= _get_platform_info
,
1454 .set_platform_info
= _set_platform_info
,
1455 .get_tpm_version_info
= _get_tpm_version_info
,
1456 .set_tpm_version_info
= _set_tpm_version_info
,
1457 .get_aik
= _get_aik
,
1458 .set_aik
= _set_aik
,
1459 .is_path_valid
= _is_path_valid
,
1460 .hash_file
= _hash_file
,
1461 .do_measurements
= _do_measurements
,
1462 .get_metadata
= _get_metadata
,
1463 .read_pcr
= _read_pcr
,
1464 .extend_pcr
= _extend_pcr
,
1465 .quote_tpm
= _quote_tpm
,
1466 .add_pcr_entry
= _add_pcr_entry
,
1467 .get_quote_info
= _get_quote_info
,
1468 .verify_quote_signature
= _verify_quote_signature
,
1469 .destroy
= _destroy
,
1472 .proto_caps
= PTS_PROTO_CAPS_V
,
1473 .algorithm
= PTS_MEAS_ALGO_SHA256
,
1474 .dh_hash_algorithm
= PTS_MEAS_ALGO_SHA256
,
1479 this->platform_info
= extract_platform_info();
1483 this->has_tpm
= TRUE
;
1484 this->proto_caps
|= PTS_PROTO_CAPS_T
| PTS_PROTO_CAPS_D
;
1486 load_aik_blob(this);
1491 this->proto_caps
|= PTS_PROTO_CAPS_T
| PTS_PROTO_CAPS_D
;
1494 return &this->public;