strongswan/src/libpts/pts/pts.c

1552 lines
35 KiB
C
Raw Normal View History

2011-08-20 21:37:37 +00:00
/*
* Copyright (C) 2011 Sansar Choinyambuu
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "pts.h"
#include <debug.h>
#include <crypto/hashers/hasher.h>
#include <bio/bio_writer.h>
#include <bio/bio_reader.h>
2011-08-20 21:37:37 +00:00
#include <trousers/tss.h>
#include <trousers/trousers.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <errno.h>
#define PTS_BUF_SIZE 4096
/**
* Maximum number of PCR's of TPM, TPM Spec 1.2
*/
#define PCR_MAX_NUM 24
/**
* Number of bytes that can be saved in a PCR of TPM, TPM Spec 1.2
*/
#define PCR_LEN 20
2011-08-20 21:37:37 +00:00
typedef struct private_pts_t private_pts_t;
/**
* Private data of a pts_t object.
*
*/
struct private_pts_t {
/**
* Public pts_t interface.
*/
pts_t public;
/**
* PTS Protocol Capabilities
*/
pts_proto_caps_flag_t proto_caps;
/**
* PTS Measurement Algorithm
*/
pts_meas_algorithms_t algorithm;
/**
2011-10-08 20:17:50 +00:00
* DH Hash Algorithm
*/
pts_meas_algorithms_t dh_hash_algorithm;
/**
* PTS Diffie-Hellman Secret
*/
diffie_hellman_t *dh;
2011-10-08 20:17:50 +00:00
/**
* PTS Diffie-Hellman Initiator Nonce
*/
chunk_t initiator_nonce;
/**
* PTS Diffie-Hellman Responder Nonce
*/
chunk_t responder_nonce;
/**
* Secret assessment value to be used for TPM Quote as an external data
*/
chunk_t secret;
/**
* Platform and OS Info
*/
char *platform_info;
2011-10-08 20:17:50 +00:00
/**
* TRUE if IMC-PTS, FALSE if IMV-PTS
*/
bool is_imc;
2011-08-21 18:00:15 +00:00
/**
* Do we have an activated TPM
*/
bool has_tpm;
2011-08-20 21:37:37 +00:00
/**
* Contains a TPM_CAP_VERSION_INFO struct
*/
chunk_t tpm_version_info;
/**
* Contains TSS Blob structure for AIK
*/
chunk_t aik_blob;
2011-08-26 12:16:12 +00:00
/**
2011-09-05 09:15:34 +00:00
* Contains a Attestation Identity Key or Certificate
2011-08-26 12:16:12 +00:00
*/
2011-09-05 09:15:34 +00:00
certificate_t *aik;
/**
2011-11-22 16:00:38 +00:00
* Table of extended PCRs with corresponding values
*/
2011-11-22 16:00:38 +00:00
u_char* pcrs[PCR_MAX_NUM];
/**
* Length of PCR registers
*/
size_t pcr_len;
/**
* Number of extended PCR registers
*/
u_int32_t pcr_count;
/**
* Highest extended PCR register
*/
u_int32_t pcr_max;
/**
* Bitmap of extended PCR registers
*/
u_int8_t pcr_select[PCR_MAX_NUM / 8];
2011-08-20 21:37:37 +00:00
};
METHOD(pts_t, get_proto_caps, pts_proto_caps_flag_t,
private_pts_t *this)
{
return this->proto_caps;
}
METHOD(pts_t, set_proto_caps, void,
2011-10-08 20:17:50 +00:00
private_pts_t *this, pts_proto_caps_flag_t flags)
{
this->proto_caps = flags;
2011-09-10 22:11:04 +00:00
DBG2(DBG_PTS, "supported PTS protocol capabilities: %s%s%s%s%s",
flags & PTS_PROTO_CAPS_C ? "C" : ".",
flags & PTS_PROTO_CAPS_V ? "V" : ".",
flags & PTS_PROTO_CAPS_D ? "D" : ".",
flags & PTS_PROTO_CAPS_T ? "T" : ".",
flags & PTS_PROTO_CAPS_X ? "X" : ".");
}
METHOD(pts_t, get_meas_algorithm, pts_meas_algorithms_t,
2011-10-08 20:17:50 +00:00
private_pts_t *this)
{
return this->algorithm;
}
METHOD(pts_t, set_meas_algorithm, void,
2011-10-08 20:17:50 +00:00
private_pts_t *this, pts_meas_algorithms_t algorithm)
{
hash_algorithm_t hash_alg;
2011-10-08 20:17:50 +00:00
hash_alg = pts_meas_algo_to_hash(algorithm);
2011-09-10 22:11:04 +00:00
DBG2(DBG_PTS, "selected PTS measurement algorithm is %N",
2011-10-08 20:17:50 +00:00
hash_algorithm_names, hash_alg);
if (hash_alg != HASH_UNKNOWN)
{
this->algorithm = algorithm;
}
}
2011-10-08 20:17:50 +00:00
METHOD(pts_t, get_dh_hash_algorithm, pts_meas_algorithms_t,
private_pts_t *this)
{
return this->dh_hash_algorithm;
}
METHOD(pts_t, set_dh_hash_algorithm, void,
private_pts_t *this, pts_meas_algorithms_t algorithm)
{
hash_algorithm_t hash_alg;
hash_alg = pts_meas_algo_to_hash(algorithm);
DBG2(DBG_PTS, "selected DH hash algorithm is %N",
hash_algorithm_names, hash_alg);
if (hash_alg != HASH_UNKNOWN)
{
this->dh_hash_algorithm = algorithm;
}
}
METHOD(pts_t, create_dh_nonce, bool,
private_pts_t *this, pts_dh_group_t group, int nonce_len)
{
diffie_hellman_group_t dh_group;
2011-10-08 20:17:50 +00:00
chunk_t *nonce;
rng_t *rng;
2011-10-08 20:17:50 +00:00
dh_group = pts_dh_group_to_ike(group);
DBG2(DBG_PTS, "selected PTS DH group is %N",
diffie_hellman_group_names, dh_group);
DESTROY_IF(this->dh);
this->dh = lib->crypto->create_dh(lib->crypto, dh_group);
rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
if (!rng)
{
2011-10-08 20:17:50 +00:00
DBG1(DBG_PTS, "no rng available");
return FALSE;
}
2011-10-08 20:17:50 +00:00
DBG2(DBG_PTS, "nonce length is %d", nonce_len);
nonce = this->is_imc ? &this->responder_nonce : &this->initiator_nonce;
chunk_free(nonce);
rng->allocate_bytes(rng, nonce_len, nonce);
rng->destroy(rng);
2011-10-07 18:28:29 +00:00
2011-10-08 20:17:50 +00:00
return TRUE;
}
2011-10-07 18:28:29 +00:00
METHOD(pts_t, get_my_public_value, void,
2011-10-08 20:17:50 +00:00
private_pts_t *this, chunk_t *value, chunk_t *nonce)
{
2011-10-07 18:28:29 +00:00
this->dh->get_my_public_value(this->dh, value);
2011-10-08 20:17:50 +00:00
*nonce = this->is_imc ? this->responder_nonce : this->initiator_nonce;
}
2011-10-07 18:28:29 +00:00
METHOD(pts_t, set_peer_public_value, void,
2011-10-08 20:17:50 +00:00
private_pts_t *this, chunk_t value, chunk_t nonce)
{
this->dh->set_other_public_value(this->dh, value);
2011-10-08 20:17:50 +00:00
nonce = chunk_clone(nonce);
if (this->is_imc)
{
this->initiator_nonce = nonce;
}
else
{
this->responder_nonce = nonce;
}
}
METHOD(pts_t, calculate_secret, bool,
2011-10-08 20:17:50 +00:00
private_pts_t *this)
{
hasher_t *hasher;
hash_algorithm_t hash_alg;
chunk_t shared_secret;
2011-09-28 13:14:02 +00:00
2011-10-08 20:17:50 +00:00
/* Check presence of nonces */
if (!this->initiator_nonce.len || !this->responder_nonce.len)
{
2011-10-08 20:17:50 +00:00
DBG1(DBG_PTS, "initiator and/or responder nonce is not available");
return FALSE;
}
2011-10-08 20:17:50 +00:00
DBG3(DBG_PTS, "initiator nonce: %B", &this->initiator_nonce);
DBG3(DBG_PTS, "responder nonce: %B", &this->responder_nonce);
2011-09-28 13:14:02 +00:00
2011-10-08 20:17:50 +00:00
/* Calculate the DH secret */
if (this->dh->get_shared_secret(this->dh, &shared_secret) != SUCCESS)
{
2011-10-08 20:17:50 +00:00
DBG1(DBG_PTS, "shared DH secret computation failed");
return FALSE;
}
DBG3(DBG_PTS, "shared DH secret: %B", &shared_secret);
2011-10-08 20:17:50 +00:00
/* Calculate the secret assessment value */
hash_alg = pts_meas_algo_to_hash(this->dh_hash_algorithm);
hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
2011-09-28 13:14:02 +00:00
2011-10-08 20:17:50 +00:00
hasher->allocate_hash(hasher, chunk_from_chars('1'), NULL);
hasher->allocate_hash(hasher, this->initiator_nonce, NULL);
hasher->allocate_hash(hasher, this->responder_nonce, NULL);
hasher->allocate_hash(hasher, shared_secret, &this->secret);
hasher->destroy(hasher);
2011-10-08 20:17:50 +00:00
/* The DH secret must be destroyed */
chunk_clear(&shared_secret);
/*
* Truncate the hash to 20 bytes to fit the ExternalData
* argument of the TPM Quote command
*/
this->secret.len = min(this->secret.len, 20);
DBG3(DBG_PTS, "secret assessment value: %B", &this->secret);
2011-10-08 20:17:50 +00:00
return TRUE;
}
2011-08-20 21:37:37 +00:00
/**
* Print TPM 1.2 Version Info
*/
static void print_tpm_version_info(private_pts_t *this)
{
TPM_CAP_VERSION_INFO versionInfo;
UINT64 offset = 0;
TSS_RESULT result;
result = Trspi_UnloadBlob_CAP_VERSION_INFO(&offset,
2011-09-10 09:02:19 +00:00
this->tpm_version_info.ptr, &versionInfo);
2011-08-20 21:37:37 +00:00
if (result != TSS_SUCCESS)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "could not parse tpm version info: tss error 0x%x",
2011-08-20 21:37:37 +00:00
result);
}
else
{
2011-09-10 22:11:04 +00:00
DBG2(DBG_PTS, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
" Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
versionInfo.version.major, versionInfo.version.minor,
versionInfo.version.revMajor, versionInfo.version.revMinor,
versionInfo.specLevel, versionInfo.errataRev,
versionInfo.tpmVendorID);
2011-08-20 21:37:37 +00:00
}
}
METHOD(pts_t, get_platform_info, char*,
2011-10-08 20:17:50 +00:00
private_pts_t *this)
{
return this->platform_info;
}
METHOD(pts_t, set_platform_info, void,
2011-10-08 20:17:50 +00:00
private_pts_t *this, char *info)
{
free(this->platform_info);
this->platform_info = strdup(info);
}
2011-08-20 21:37:37 +00:00
METHOD(pts_t, get_tpm_version_info, bool,
2011-10-08 20:17:50 +00:00
private_pts_t *this, chunk_t *info)
2011-08-20 21:37:37 +00:00
{
2011-08-21 18:00:15 +00:00
if (!this->has_tpm)
2011-08-20 21:37:37 +00:00
{
2011-08-21 18:00:15 +00:00
return FALSE;
2011-08-20 21:37:37 +00:00
}
*info = this->tpm_version_info;
print_tpm_version_info(this);
return TRUE;
}
METHOD(pts_t, set_tpm_version_info, void,
2011-10-08 20:17:50 +00:00
private_pts_t *this, chunk_t info)
2011-08-20 21:37:37 +00:00
{
this->tpm_version_info = chunk_clone(info);
print_tpm_version_info(this);
}
METHOD(pts_t, get_pcr_len, size_t,
private_pts_t *this)
{
return this->pcr_len;
}
2011-08-31 13:04:05 +00:00
/**
* Load an AIK Blob (TSS_TSPATTRIB_KEYBLOB_BLOB attribute)
*/
static void load_aik_blob(private_pts_t *this)
{
char *blob_path;
FILE *fp;
u_int32_t aikBlobLen;
blob_path = lib->settings->get_str(lib->settings,
"libimcv.plugins.imc-attestation.aik_blob", NULL);
if (blob_path)
{
/* Read aik key blob from a file */
if ((fp = fopen(blob_path, "r")) == NULL)
{
DBG1(DBG_PTS, "unable to open AIK Blob file: %s", blob_path);
return;
}
fseek(fp, 0, SEEK_END);
aikBlobLen = ftell(fp);
fseek(fp, 0L, SEEK_SET);
this->aik_blob = chunk_alloc(aikBlobLen);
if (fread(this->aik_blob.ptr, 1, aikBlobLen, fp))
{
DBG2(DBG_PTS, "loaded AIK Blob from '%s'", blob_path);
DBG3(DBG_PTS, "AIK Blob: %B", &this->aik_blob);
}
else
{
DBG1(DBG_PTS, "unable to read AIK Blob file '%s'", blob_path);
}
fclose(fp);
return;
}
DBG1(DBG_PTS, "AIK Blob is not available");
}
/**
* Load an AIK certificate or public key
2011-09-05 09:15:34 +00:00
* the certificate having precedence over the public key if both are present
*/
2011-09-05 09:15:34 +00:00
static void load_aik(private_pts_t *this)
2011-08-26 12:16:12 +00:00
{
2011-09-05 09:15:34 +00:00
char *cert_path, *key_path;
2011-08-31 13:04:05 +00:00
2011-09-05 09:15:34 +00:00
cert_path = lib->settings->get_str(lib->settings,
"libimcv.plugins.imc-attestation.aik_cert", NULL);
key_path = lib->settings->get_str(lib->settings,
"libimcv.plugins.imc-attestation.aik_key", NULL);
2011-08-31 13:04:05 +00:00
2011-09-05 09:15:34 +00:00
if (cert_path)
{
2011-09-05 09:15:34 +00:00
this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
2011-09-10 09:02:19 +00:00
CERT_X509, BUILD_FROM_FILE,
cert_path, BUILD_END);
2011-09-05 09:15:34 +00:00
if (this->aik)
{
2011-09-10 22:11:04 +00:00
DBG2(DBG_PTS, "loaded AIK certificate from '%s'", cert_path);
2011-09-05 09:15:34 +00:00
return;
}
}
2011-09-05 09:15:34 +00:00
if (key_path)
2011-08-26 12:16:12 +00:00
{
2011-09-05 09:15:34 +00:00
this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
2011-09-10 09:02:19 +00:00
CERT_TRUSTED_PUBKEY, BUILD_FROM_FILE,
key_path, BUILD_END);
2011-09-05 09:15:34 +00:00
if (this->aik)
{
2011-09-10 22:11:04 +00:00
DBG2(DBG_PTS, "loaded AIK public key from '%s'", key_path);
2011-09-05 09:15:34 +00:00
return;
}
2011-08-26 12:16:12 +00:00
}
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "neither AIK certificate nor public key is available");
2011-08-26 12:16:12 +00:00
}
2011-09-05 09:15:34 +00:00
METHOD(pts_t, get_aik, certificate_t*,
2011-10-08 20:17:50 +00:00
private_pts_t *this)
{
return this->aik;
}
2011-09-05 09:15:34 +00:00
METHOD(pts_t, set_aik, void,
2011-10-08 20:17:50 +00:00
private_pts_t *this, certificate_t *aik)
2011-08-26 12:16:12 +00:00
{
2011-09-05 09:15:34 +00:00
DESTROY_IF(this->aik);
this->aik = aik->get_ref(aik);
2011-08-26 12:16:12 +00:00
}
METHOD(pts_t, hash_file, bool,
2011-10-08 20:17:50 +00:00
private_pts_t *this, hasher_t *hasher, char *pathname, u_char *hash)
{
u_char buffer[PTS_BUF_SIZE];
FILE *file;
int bytes_read;
2011-08-31 14:52:31 +00:00
file = fopen(pathname, "rb");
if (!file)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS," file '%s' can not be opened, %s", pathname,
strerror(errno));
2011-08-31 14:52:31 +00:00
return FALSE;
}
while (TRUE)
{
bytes_read = fread(buffer, 1, sizeof(buffer), file);
if (bytes_read > 0)
{
hasher->get_hash(hasher, chunk_create(buffer, bytes_read), NULL);
}
else
{
hasher->get_hash(hasher, chunk_empty, hash);
break;
}
}
fclose(file);
2011-08-31 14:52:31 +00:00
return TRUE;
}
/**
* Get the relative filename of a fully qualified file pathname
*/
static char* get_filename(char *pathname)
{
char *pos, *filename;
pos = filename = pathname;
while (pos && *(++pos) != '\0')
{
filename = pos;
pos = strchr(filename, '/');
}
return filename;
}
2011-10-08 20:17:50 +00:00
METHOD(pts_t, is_path_valid, bool,
private_pts_t *this, char *path, pts_error_code_t *error_code)
{
2011-09-14 13:41:57 +00:00
struct stat st;
2011-09-28 13:14:02 +00:00
2011-09-10 09:57:17 +00:00
*error_code = 0;
2011-09-28 13:14:02 +00:00
if (!stat(path, &st))
{
return TRUE;
}
else if (errno == ENOENT || errno == ENOTDIR)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "file/directory does not exist %s", path);
*error_code = TCG_PTS_FILE_NOT_FOUND;
}
else if (errno == EFAULT)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "bad address %s", path);
*error_code = TCG_PTS_INVALID_PATH;
}
else
{
DBG1(DBG_PTS, "error: %s occured while validating path: %s",
strerror(errno), path);
return FALSE;
}
return TRUE;
}
METHOD(pts_t, do_measurements, pts_file_meas_t*,
2011-10-08 20:17:50 +00:00
private_pts_t *this, u_int16_t request_id, char *pathname, bool is_directory)
{
hasher_t *hasher;
hash_algorithm_t hash_alg;
u_char hash[HASH_SIZE_SHA384];
chunk_t measurement;
pts_file_meas_t *measurements;
/* Create a hasher */
2011-10-08 20:17:50 +00:00
hash_alg = pts_meas_algo_to_hash(this->algorithm);
hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
if (!hasher)
{
2011-10-24 11:31:05 +00:00
DBG1(DBG_PTS, "hasher %N not available", hash_algorithm_names, hash_alg);
return NULL;
}
/* Create a measurement object */
measurements = pts_file_meas_create(request_id);
/* Link the hash to the measurement and set the measurement length */
measurement = chunk_create(hash, hasher->get_hash_size(hasher));
if (is_directory)
{
enumerator_t *enumerator;
char *rel_name, *abs_name;
struct stat st;
enumerator = enumerator_create_directory(pathname);
if (!enumerator)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
strerror(errno));
hasher->destroy(hasher);
measurements->destroy(measurements);
return NULL;
}
while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
{
/* measure regular files only */
if (S_ISREG(st.st_mode) && *rel_name != '.')
{
if (!hash_file(this, hasher, abs_name, hash))
{
enumerator->destroy(enumerator);
hasher->destroy(hasher);
measurements->destroy(measurements);
return NULL;
}
2011-09-10 22:11:04 +00:00
DBG2(DBG_PTS, " %#B for '%s'", &measurement, rel_name);
measurements->add(measurements, rel_name, measurement);
}
}
enumerator->destroy(enumerator);
}
else
{
char *filename;
if (!hash_file(this, hasher, pathname, hash))
{
hasher->destroy(hasher);
measurements->destroy(measurements);
return NULL;
}
filename = get_filename(pathname);
2011-09-10 22:11:04 +00:00
DBG2(DBG_PTS, " %#B for '%s'", &measurement, filename);
measurements->add(measurements, filename, measurement);
}
hasher->destroy(hasher);
return measurements;
}
2011-09-14 13:41:57 +00:00
/**
* Obtain statistical information describing a file
*/
static bool file_metadata(char *pathname, pts_file_metadata_t **entry)
{
struct stat st;
pts_file_metadata_t *this;
2011-09-14 13:41:57 +00:00
this = malloc_thing(pts_file_metadata_t);
2011-09-28 13:14:02 +00:00
2011-09-14 13:41:57 +00:00
if (stat(pathname, &st))
{
DBG1(DBG_PTS, "unable to obtain statistics about '%s'", pathname);
2011-09-14 13:41:57 +00:00
return FALSE;
}
2011-09-28 13:14:02 +00:00
2011-09-14 13:41:57 +00:00
if (S_ISREG(st.st_mode))
{
this->type = PTS_FILE_REGULAR;
2011-09-14 13:41:57 +00:00
}
else if (S_ISDIR(st.st_mode))
{
this->type = PTS_FILE_DIRECTORY;
2011-09-14 13:41:57 +00:00
}
else if (S_ISCHR(st.st_mode))
{
this->type = PTS_FILE_CHAR_SPEC;
2011-09-14 13:41:57 +00:00
}
else if (S_ISBLK(st.st_mode))
{
this->type = PTS_FILE_BLOCK_SPEC;
2011-09-14 13:41:57 +00:00
}
else if (S_ISFIFO(st.st_mode))
{
this->type = PTS_FILE_FIFO;
2011-09-14 13:41:57 +00:00
}
else if (S_ISLNK(st.st_mode))
{
this->type = PTS_FILE_SYM_LINK;
2011-09-14 13:41:57 +00:00
}
else if (S_ISSOCK(st.st_mode))
{
this->type = PTS_FILE_SOCKET;
2011-09-14 13:41:57 +00:00
}
else
{
this->type = PTS_FILE_OTHER;
2011-09-14 13:41:57 +00:00
}
2011-09-28 13:14:02 +00:00
this->filesize = st.st_size;
this->created = st.st_ctime;
this->modified = st.st_mtime;
this->accessed = st.st_atime;
this->owner = st.st_uid;
this->group = st.st_gid;
2011-09-14 13:41:57 +00:00
*entry = this;
2011-09-14 13:41:57 +00:00
return TRUE;
}
METHOD(pts_t, get_metadata, pts_file_meta_t*,
2011-10-08 20:17:50 +00:00
private_pts_t *this, char *pathname, bool is_directory)
2011-09-14 13:41:57 +00:00
{
pts_file_meta_t *metadata;
pts_file_metadata_t *entry;
2011-09-28 13:14:02 +00:00
2011-09-14 13:41:57 +00:00
/* Create a metadata object */
metadata = pts_file_meta_create();
if (is_directory)
{
enumerator_t *enumerator;
char *rel_name, *abs_name;
struct stat st;
enumerator = enumerator_create_directory(pathname);
if (!enumerator)
{
DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
strerror(errno));
metadata->destroy(metadata);
return NULL;
}
while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
{
/* measure regular files only */
if (S_ISREG(st.st_mode) && *rel_name != '.')
{
if (!file_metadata(abs_name, &entry))
{
enumerator->destroy(enumerator);
metadata->destroy(metadata);
return NULL;
}
entry->filename = strdup(rel_name);
2011-09-14 13:41:57 +00:00
metadata->add(metadata, entry);
}
}
enumerator->destroy(enumerator);
}
else
{
if (!file_metadata(pathname, &entry))
{
metadata->destroy(metadata);
return NULL;
}
entry->filename = strdup(get_filename(pathname));
2011-09-14 13:41:57 +00:00
metadata->add(metadata, entry);
}
return metadata;
}
METHOD(pts_t, read_pcr, bool,
2011-11-22 23:26:02 +00:00
private_pts_t *this, u_int32_t pcr_num, chunk_t *pcr_value)
{
TSS_HCONTEXT hContext;
TSS_HTPM hTPM;
TSS_RESULT result;
2011-11-22 23:26:02 +00:00
chunk_t rgbPcrValue;
bool success = FALSE;
result = Tspi_Context_Create(&hContext);
if (result != TSS_SUCCESS)
{
2011-11-22 23:26:02 +00:00
DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
return FALSE;
}
result = Tspi_Context_Connect(hContext, NULL);
if (result != TSS_SUCCESS)
{
goto err;
}
result = Tspi_Context_GetTpmObject (hContext, &hTPM);
if (result != TSS_SUCCESS)
{
goto err;
}
result = Tspi_TPM_PcrRead(hTPM, pcr_num, (UINT32*)&rgbPcrValue.len, &rgbPcrValue.ptr);
if (result != TSS_SUCCESS)
{
goto err;
}
2011-11-22 23:26:02 +00:00
*pcr_value = chunk_clone(rgbPcrValue);
DBG3(DBG_PTS, "PCR %d value:%B", pcr_num, pcr_value);
success = TRUE;
2011-11-22 23:26:02 +00:00
err:
if (!success)
{
DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
}
Tspi_Context_FreeMemory(hContext, NULL);
Tspi_Context_Close(hContext);
2011-11-22 23:26:02 +00:00
return success;
}
METHOD(pts_t, extend_pcr, bool,
2011-10-08 20:17:50 +00:00
private_pts_t *this, u_int32_t pcr_num, chunk_t input, chunk_t *output)
{
TSS_HCONTEXT hContext;
TSS_HTPM hTPM;
TSS_RESULT result;
u_int32_t pcr_length;
chunk_t pcr_value;
result = Tspi_Context_Create(&hContext);
if (result != TSS_SUCCESS)
{
2011-10-24 11:31:05 +00:00
DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
result);
return FALSE;
}
result = Tspi_Context_Connect(hContext, NULL);
if (result != TSS_SUCCESS)
{
goto err;
}
result = Tspi_Context_GetTpmObject (hContext, &hTPM);
if (result != TSS_SUCCESS)
{
goto err;
}
pcr_value = chunk_alloc(PCR_LEN);
result = Tspi_TPM_PcrExtend(hTPM, pcr_num, PCR_LEN, input.ptr,
NULL, &pcr_length, &pcr_value.ptr);
if (result != TSS_SUCCESS)
{
goto err;
}
*output = pcr_value;
*output = chunk_clone(*output);
DBG3(DBG_PTS, "PCR %d extended with: %B", pcr_num, &input);
DBG3(DBG_PTS, "PCR %d value after extend: %B", pcr_num, output);
chunk_clear(&pcr_value);
Tspi_Context_FreeMemory(hContext, NULL);
Tspi_Context_Close(hContext);
return TRUE;
2011-11-22 16:00:38 +00:00
err:
DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
chunk_clear(&pcr_value);
Tspi_Context_FreeMemory(hContext, NULL);
Tspi_Context_Close(hContext);
return FALSE;
}
2011-11-22 16:00:38 +00:00
static void clear_pcrs(private_pts_t *this)
{
int i;
for (i = 0; i <= this->pcr_max; i++)
{
free(this->pcrs[i]);
this->pcrs[i] = NULL;
}
this->pcr_count = 0;
this->pcr_max = 0;
memset(this->pcr_select, 0x00, sizeof(this->pcr_select));
}
METHOD(pts_t, quote_tpm, bool,
2011-11-22 21:29:40 +00:00
private_pts_t *this, bool use_quote2, chunk_t *pcr_comp, chunk_t *quote_sig)
{
TSS_HCONTEXT hContext;
TSS_HTPM hTPM;
TSS_HKEY hAIK;
TSS_HKEY hSRK;
TSS_HPOLICY srkUsagePolicy;
TSS_UUID SRK_UUID = TSS_UUID_SRK;
BYTE secret[] = TSS_WELL_KNOWN_SECRET;
TSS_HPCRS hPcrComposite;
TSS_VALIDATION valData;
TSS_RESULT result;
2011-11-22 21:29:40 +00:00
chunk_t quote_info;
BYTE* versionInfo;
2011-11-22 16:00:38 +00:00
u_int32_t versionInfoSize, pcr, i = 0, f = 1;
bool success = FALSE;
result = Tspi_Context_Create(&hContext);
if (result != TSS_SUCCESS)
{
2011-10-24 11:31:05 +00:00
DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
result);
return FALSE;
}
result = Tspi_Context_Connect(hContext, NULL);
if (result != TSS_SUCCESS)
{
goto err1;
}
result = Tspi_Context_GetTpmObject (hContext, &hTPM);
if (result != TSS_SUCCESS)
{
goto err1;
}
2011-10-24 11:31:05 +00:00
/* Retrieve SRK from TPM and set the authentication to well known secret*/
result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
SRK_UUID, &hSRK);
if (result != TSS_SUCCESS)
{
goto err1;
}
result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
if (result != TSS_SUCCESS)
{
goto err1;
}
result = Tspi_Policy_SetSecret(srkUsagePolicy, TSS_SECRET_MODE_SHA1,
20, secret);
if (result != TSS_SUCCESS)
{
goto err1;
}
result = Tspi_Context_LoadKeyByBlob (hContext, hSRK, this->aik_blob.len,
this->aik_blob.ptr, &hAIK);
if (result != TSS_SUCCESS)
{
goto err1;
}
/* Create PCR composite object */
result = use_quote2 ?
Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
TSS_PCRS_STRUCT_INFO_SHORT, &hPcrComposite) :
Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS,
0, &hPcrComposite);
if (result != TSS_SUCCESS)
{
goto err2;
}
2011-11-22 16:00:38 +00:00
/* Select PCRs */
for (pcr = 0; pcr <= this->pcr_max ; pcr++)
{
2011-11-22 16:00:38 +00:00
if (f == 256)
{
2011-11-22 16:00:38 +00:00
i++;
f = 1;
}
if (this->pcr_select[i] & f)
{
2011-11-22 16:00:38 +00:00
DBG2(DBG_TNC, "PCR %02d selected for TPM Quote", pcr);
result = use_quote2 ?
Tspi_PcrComposite_SelectPcrIndexEx(hPcrComposite, pcr,
TSS_PCRS_DIRECTION_RELEASE) :
Tspi_PcrComposite_SelectPcrIndex(hPcrComposite, pcr);
if (result != TSS_SUCCESS)
{
goto err3;
}
}
2011-11-22 16:00:38 +00:00
f <<= 1;
}
/* Set the Validation Data */
valData.ulExternalDataLength = this->secret.len;
valData.rgbExternalData = (BYTE *)this->secret.ptr;
/* TPM Quote */
result = use_quote2 ?
Tspi_TPM_Quote2(hTPM, hAIK, FALSE, hPcrComposite, &valData,
2011-11-22 23:26:02 +00:00
&versionInfoSize, &versionInfo):
Tspi_TPM_Quote(hTPM, hAIK, hPcrComposite, &valData);
if (result != TSS_SUCCESS)
{
goto err4;
}
/* Set output chunks */
2011-11-22 21:29:40 +00:00
*pcr_comp = chunk_alloc(HASH_SIZE_SHA1);
if (use_quote2)
{
/* TPM_Composite_Hash is last 20 bytes of TPM_Quote_Info2 structure */
2011-11-22 21:29:40 +00:00
memcpy(pcr_comp->ptr, valData.rgbData + valData.ulDataLength - HASH_SIZE_SHA1,
HASH_SIZE_SHA1);
}
else
{
/* TPM_Composite_Hash is 8-28th bytes of TPM_Quote_Info structure */
2011-11-22 21:29:40 +00:00
memcpy(pcr_comp->ptr, valData.rgbData + 8, HASH_SIZE_SHA1);
}
2011-11-22 21:29:40 +00:00
DBG3(DBG_PTS, "Hash of PCR Composite: %#B", pcr_comp);
2011-11-22 21:29:40 +00:00
quote_info = chunk_create(valData.rgbData, valData.ulDataLength);
DBG3(DBG_PTS, "TPM Quote Info: %B",&quote_info);
2011-11-22 21:29:40 +00:00
*quote_sig = chunk_clone(chunk_create(valData.rgbValidationData,
valData.ulValidationDataLength));
DBG3(DBG_PTS, "TPM Quote Signature: %B",quote_sig);
2011-11-22 16:00:38 +00:00
success = TRUE;
/* Cleanup */
2011-11-22 16:00:38 +00:00
err4:
Tspi_Context_FreeMemory(hContext, NULL);
2011-11-22 16:00:38 +00:00
err3:
Tspi_Context_CloseObject(hContext, hPcrComposite);
2011-11-22 16:00:38 +00:00
err2:
Tspi_Context_CloseObject(hContext, hAIK);
2011-11-22 16:00:38 +00:00
err1:
Tspi_Context_Close(hContext);
2011-11-22 16:00:38 +00:00
if (!success)
{
DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
}
clear_pcrs(this);
return success;
}
2011-09-14 13:41:57 +00:00
2011-11-22 16:00:38 +00:00
METHOD(pts_t, select_pcr, bool,
private_pts_t *this, u_int32_t pcr)
{
2011-11-22 16:00:38 +00:00
u_int32_t i, f;
2011-11-22 16:00:38 +00:00
if (pcr >= PCR_MAX_NUM)
{
2011-11-22 16:00:38 +00:00
DBG1(DBG_PTS, "PCR %u: number is larger than maximum of %u",
pcr, PCR_MAX_NUM-1);
return FALSE;
}
2011-11-22 16:00:38 +00:00
/* Determine PCR selection flag */
i = pcr / 8;
f = 1 << (pcr - 8*i);
/* Has this PCR already been selected? */
if (!(this->pcr_select[i] & f))
{
2011-11-22 16:00:38 +00:00
this->pcr_select[i] |= f;
this->pcr_max = max(this->pcr_max, pcr);
this->pcr_count++;
}
2011-11-22 16:00:38 +00:00
return TRUE;
}
2011-11-22 16:00:38 +00:00
METHOD(pts_t, add_pcr, bool,
private_pts_t *this, u_int32_t pcr, chunk_t pcr_before, chunk_t pcr_after)
{
2011-11-22 16:00:38 +00:00
if (pcr >= PCR_MAX_NUM)
{
DBG1(DBG_PTS, "PCR %u: number is larger than maximum of %u",
pcr, PCR_MAX_NUM-1);
return FALSE;
}
2011-11-22 16:00:38 +00:00
/* Is the length of the PCR registers already set? */
if (this->pcr_len)
{
2011-11-22 16:00:38 +00:00
if (pcr_after.len != this->pcr_len)
{
DBG1(DBG_PTS, "PCR %02u: length is %d bytes but should be %d bytes",
pcr_after.len, this->pcr_len);
return FALSE;
}
}
2011-11-22 16:00:38 +00:00
else
{
this->pcr_len = pcr_after.len;
}
/* Has the value of the PCR register already been assigned? */
if (this->pcrs[pcr])
{
2011-11-22 16:00:38 +00:00
if (!memeq(this->pcrs[pcr], pcr_before.ptr, this->pcr_len))
{
2011-11-22 16:00:38 +00:00
DBG1(DBG_PTS, "PCR %02u: new pcr_before value does not equal "
"old pcr_after value");
}
2011-11-22 16:00:38 +00:00
/* remove the old PCR value */
free(this->pcrs[pcr]);
}
2011-11-22 16:00:38 +00:00
else
{
/* add extended PCR Register */
this->pcr_select[pcr / 8] |= 1 << (pcr % 8);
this->pcr_max = max(this->pcr_max, pcr);
this->pcr_count++;
}
/* Duplicate and store current PCR value */
pcr_after = chunk_clone(pcr_after);
this->pcrs[pcr] = pcr_after.ptr;
2011-11-22 16:00:38 +00:00
return TRUE;
}
METHOD(pts_t, does_pcr_value_match, bool,
private_pts_t *this, chunk_t pcr_after_value)
{
enumerator_t *e;
pcr_entry_t *entry;
if (!this->pcrs)
{
this->pcrs = linked_list_create();
}
e = this->pcrs->create_enumerator(this->pcrs);
while (e->enumerate(e, &entry))
{
if (entry->pcr_number == new->pcr_number)
{
DBG4(DBG_PTS, "updating already added PCR%d value",
entry->pcr_number);
this->pcrs->remove_at(this->pcrs, e);
free(entry);
break;
}
}
DESTROY_IF(e);
this->pcrs->insert_last(this->pcrs, new);
}
/**
* TPM_QUOTE_INFO structure:
* 4 bytes of version
* 4 bytes 'Q' 'U' 'O' 'T'
* 20 byte SHA1 of TCPA_PCR_COMPOSITE
* 20 byte nonce
*
* TPM_QUOTE_INFO2 structure:
* 2 bytes Tag 0x0036 TPM_Tag_Quote_info2
* 4 bytes 'Q' 'U' 'T' '2'
* 20 bytes nonce
* 26 bytes PCR_INFO_SHORT
*/
METHOD(pts_t, get_quote_info, bool,
2011-11-22 21:29:40 +00:00
private_pts_t *this, bool use_quote2, bool use_ver_info,
pts_meas_algorithms_t comp_hash_algo,
chunk_t *out_pcr_comp, chunk_t *out_quote_info)
{
2011-11-22 16:00:38 +00:00
u_int8_t size_of_select;
2011-11-22 21:29:40 +00:00
int pcr_comp_len, i;
chunk_t pcr_comp, hash_pcr_comp;
bio_writer_t *writer;
hasher_t *hasher;
2011-11-22 16:00:38 +00:00
if (this->pcr_count == 0)
{
2011-11-22 16:00:38 +00:00
DBG1(DBG_PTS, "No extended PCR entries available, "
"unable to construct TPM Quote Info");
return FALSE;
}
if (!this->secret.ptr)
{
2011-11-22 16:00:38 +00:00
DBG1(DBG_PTS, "Secret assessment value unavailable, ",
"unable to construct TPM Quote Info");
return FALSE;
}
2011-11-22 21:29:40 +00:00
if (use_quote2 && use_ver_info && !this->tpm_version_info.ptr)
{
2011-11-22 16:00:38 +00:00
DBG1(DBG_PTS, "TPM Version Information unavailable, ",
"unable to construct TPM Quote Info2");
return FALSE;
}
2011-11-22 16:00:38 +00:00
size_of_select = 1 + this->pcr_max / 8;
2011-11-22 21:29:40 +00:00
pcr_comp_len = 2 + size_of_select + 4 + this->pcr_count * this->pcr_len;
2011-11-22 21:29:40 +00:00
writer = bio_writer_create(pcr_comp_len);
2011-11-22 16:00:38 +00:00
writer->write_uint16(writer, size_of_select);
for (i = 0; i < size_of_select; i++)
{
2011-11-22 16:00:38 +00:00
writer->write_uint8(writer, this->pcr_select[i]);
}
2011-11-22 16:00:38 +00:00
writer->write_uint32(writer, this->pcr_count * this->pcr_len);
for (i = 0; i < 8 * size_of_select; i++)
{
2011-11-22 16:00:38 +00:00
if (this->pcrs[i])
{
writer->write_data(writer, chunk_create(this->pcrs[i], this->pcr_len));
}
}
2011-11-22 21:29:40 +00:00
pcr_comp = chunk_clone(writer->get_buf(writer));
DBG3(DBG_PTS, "constructed PCR Composite: %B", &pcr_comp);
2011-11-22 16:00:38 +00:00
writer->destroy(writer);
/* Output the TPM_PCR_COMPOSITE expected from IMC */
2011-11-22 21:29:40 +00:00
if (comp_hash_algo)
{
hash_algorithm_t algo;
2011-11-22 21:29:40 +00:00
algo = pts_meas_algo_to_hash(comp_hash_algo);
hasher = lib->crypto->create_hasher(lib->crypto, algo);
/* Hash the PCR Composite Structure */
2011-11-22 21:29:40 +00:00
hasher->allocate_hash(hasher, pcr_comp, out_pcr_comp);
DBG3(DBG_PTS, "constructed PCR Composite hash: %#B", out_pcr_comp);
hasher->destroy(hasher);
}
else
{
2011-11-22 21:29:40 +00:00
*out_pcr_comp = chunk_clone(pcr_comp);
}
/* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
2011-11-22 21:29:40 +00:00
hasher->allocate_hash(hasher, pcr_comp, &hash_pcr_comp);
hasher->destroy(hasher);
writer->write_data(writer, hash_pcr_composite);
chunk_clear(&pcr_composite);
chunk_clear(&hash_pcr_composite);
/* Hash the PCR Composite Structure */
hasher->allocate_hash(hasher, pcr_composite, out_pcr_composite);
DBG4(DBG_PTS, "Hash of calculated PCR Composite: %B", out_pcr_composite);
hasher->destroy(hasher);
}
else
{
*out_pcr_composite = chunk_clone(pcr_composite);
DBG3(DBG_PTS, "calculated PCR Composite: %B", out_pcr_composite);
}
/* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
hasher->allocate_hash(hasher, pcr_composite, &hash_pcr_composite);
hasher->destroy(hasher);
/* Construct TPM_QUOTE_INFO/TPM_QUOTE_INFO2 structure */
writer = bio_writer_create(TPM_QUOTE_INFO_LEN);
if (use_quote2)
{
/* TPM Structure Tag */
writer->write_uint16(writer, TPM_TAG_QUOTE_INFO2);
/* Magic QUT2 value */
2011-11-22 16:00:38 +00:00
writer->write_data(writer, chunk_create("QUT2", 4));
/* Secret assessment value 20 bytes (nonce) */
writer->write_data(writer, this->secret);
2011-11-22 16:00:38 +00:00
/* Length of the PCR selection field */
writer->write_uint16(writer, size_of_select);
2011-11-22 16:00:38 +00:00
/* PCR selection */
for (i = 0; i < size_of_select ; i++)
{
2011-11-22 16:00:38 +00:00
writer->write_uint8(writer, this->pcr_select[i]);
}
/* TPM Locality Selection */
writer->write_uint8(writer, TPM_LOC_ZERO);
/* PCR Composite Hash */
2011-11-22 21:29:40 +00:00
writer->write_data(writer, hash_pcr_comp);
2011-11-22 21:29:40 +00:00
if (use_ver_info)
{
/* TPM version Info */
writer->write_data(writer, this->tpm_version_info);
}
}
else
{
/* Version number */
2011-11-22 16:00:38 +00:00
writer->write_data(writer, chunk_from_chars(1, 1, 0, 0));
/* Magic QUOT value */
2011-11-22 16:00:38 +00:00
writer->write_data(writer, chunk_create("QUOT", 4));
/* PCR Composite Hash */
2011-11-22 21:29:40 +00:00
writer->write_data(writer, hash_pcr_comp);
/* Secret assessment value 20 bytes (nonce) */
writer->write_data(writer, this->secret);
}
/* TPM Quote Info */
*out_quote_info = chunk_clone(writer->get_buf(writer));
2011-11-22 21:29:40 +00:00
DBG3(DBG_PTS, "constructed TPM Quote Info: %B", out_quote_info);
2011-11-22 16:00:38 +00:00
writer->destroy(writer);
2011-11-22 21:29:40 +00:00
free(pcr_comp.ptr);
free(hash_pcr_comp.ptr);
2011-11-22 16:00:38 +00:00
clear_pcrs(this);
return TRUE;
}
METHOD(pts_t, verify_quote_signature, bool,
private_pts_t *this, chunk_t data, chunk_t signature)
{
public_key_t *aik_pub_key;
aik_pub_key = this->aik->get_public_key(this->aik);
if (!aik_pub_key)
{
DBG1(DBG_PTS, "failed to get public key from AIK certificate");
return FALSE;
}
if (!aik_pub_key->verify(aik_pub_key, SIGN_RSA_EMSA_PKCS1_SHA1,
data, signature))
{
DBG1(DBG_PTS, "signature verification failed for TPM Quote Info");
2011-10-31 15:08:31 +00:00
DESTROY_IF(aik_pub_key);
return FALSE;
2011-10-21 11:50:28 +00:00
}
2011-10-21 11:50:28 +00:00
*/
2011-10-24 11:31:05 +00:00
if (!aik_pub_key->get_encoding(aik_pub_key,
PUBKEY_SPKI_ASN1_DER, &key_encoding))
2011-10-21 11:50:28 +00:00
{
DBG1(DBG_PTS, "failed to get encoding of AIK public key");
goto cleanup;
}
2011-10-21 11:50:28 +00:00
aik_pub_key->destroy(aik_pub_key);
return TRUE;
}
2011-08-20 21:37:37 +00:00
METHOD(pts_t, destroy, void,
2011-10-08 20:17:50 +00:00
private_pts_t *this)
2011-08-20 21:37:37 +00:00
{
2011-11-22 16:00:38 +00:00
clear_pcrs(this);
2011-09-05 09:15:34 +00:00
DESTROY_IF(this->aik);
DESTROY_IF(this->dh);
2011-10-08 20:17:50 +00:00
free(this->initiator_nonce.ptr);
free(this->responder_nonce.ptr);
free(this->secret.ptr);
free(this->platform_info);
free(this->aik_blob.ptr);
2011-08-20 21:37:37 +00:00
free(this->tpm_version_info.ptr);
free(this);
}
2011-09-06 22:48:25 +00:00
/**
* Determine Linux distribution and hardware platform
*/
2011-09-07 05:40:42 +00:00
static char* extract_platform_info(void)
2011-09-06 22:48:25 +00:00
{
FILE *file;
char buf[BUF_LEN], *pos, *value = NULL;
int i, len;
struct utsname uninfo;
/* Linux/Unix distribution release info (from http://linuxmafia.com) */
const char* releases[] = {
"/etc/lsb-release", "/etc/debian_version",
"/etc/SuSE-release", "/etc/novell-release",
"/etc/sles-release", "/etc/redhat-release",
2011-09-10 09:34:52 +00:00
"/etc/fedora-release", "/etc/gentoo-release",
"/etc/slackware-version", "/etc/annvix-release",
"/etc/arch-release", "/etc/arklinux-release",
"/etc/aurox-release", "/etc/blackcat-release",
"/etc/cobalt-release", "/etc/conectiva-release",
"/etc/debian_release", "/etc/immunix-release",
"/etc/lfs-release", "/etc/linuxppc-release",
"/etc/mandrake-release", "/etc/mandriva-release",
"/etc/mandrakelinux-release", "/etc/mklinux-release",
"/etc/pld-release", "/etc/redhat_version",
"/etc/slackware-release", "/etc/e-smith-release",
"/etc/release", "/etc/sun-release",
2011-09-10 09:34:52 +00:00
"/etc/tinysofa-release", "/etc/turbolinux-release",
"/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
"/etc/va-release", "/etc/yellowdog-release"
};
const char description[] = "DISTRIB_DESCRIPTION=\"";
for (i = 0; i < countof(releases); i++)
2011-09-06 22:48:25 +00:00
{
file = fopen(releases[i], "r");
if (!file)
{
continue;
}
fseek(file, 0, SEEK_END);
len = min(ftell(file), sizeof(buf)-1);
rewind(file);
buf[len] = '\0';
if (fread(buf, 1, len, file) != len)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "failed to read file '%s'", releases[i]);
fclose(file);
return NULL;
}
fclose(file);
2011-09-06 22:48:25 +00:00
if (i == 0) /* LSB release */
{
pos = strstr(buf, description);
if (!pos)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "failed to find begin of lsb-release "
"DESCRIPTION field");
return NULL;
}
value = pos + strlen(description);
pos = strchr(value, '"');
if (!pos)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "failed to find end of lsb-release "
"DESCRIPTION field");
return NULL;
}
}
else
{
value = buf;
pos = strchr(value, '\n');
if (!pos)
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "failed to find end of release string");
return NULL;
}
}
break;
2011-09-06 22:48:25 +00:00
}
if (!value)
2011-09-06 22:48:25 +00:00
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "no distribution release file found");
2011-09-06 22:48:25 +00:00
return NULL;
}
if (uname(&uninfo) < 0)
2011-09-06 22:48:25 +00:00
{
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "could not retrieve machine architecture");
2011-09-06 22:48:25 +00:00
return NULL;
}
*pos++ = ' ';
len = sizeof(buf)-1 + (pos - buf);
strncpy(pos, uninfo.machine, len);
2011-09-06 22:48:25 +00:00
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "platform is '%s'", value);
2011-09-10 09:34:52 +00:00
return strdup(value);
2011-09-06 22:48:25 +00:00
}
2011-08-21 18:00:15 +00:00
/**
* Check for a TPM by querying for TPM Version Info
*/
static bool has_tpm(private_pts_t *this)
{
TSS_HCONTEXT hContext;
TSS_HTPM hTPM;
TSS_RESULT result;
u_int32_t version_info_len;
2011-08-21 18:00:15 +00:00
result = Tspi_Context_Create(&hContext);
if (result != TSS_SUCCESS)
{
2011-10-24 11:31:05 +00:00
DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x",
result);
2011-09-10 08:55:21 +00:00
return FALSE;
2011-08-21 18:00:15 +00:00
}
result = Tspi_Context_Connect(hContext, NULL);
if (result != TSS_SUCCESS)
{
goto err;
}
result = Tspi_Context_GetTpmObject (hContext, &hTPM);
if (result != TSS_SUCCESS)
{
goto err;
}
result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
&version_info_len,
2011-08-21 18:00:15 +00:00
&this->tpm_version_info.ptr);
this->tpm_version_info.len = version_info_len;
2011-08-21 18:00:15 +00:00
if (result != TSS_SUCCESS)
{
goto err;
}
this->tpm_version_info = chunk_clone(this->tpm_version_info);
Tspi_Context_FreeMemory(hContext, NULL);
Tspi_Context_Close(hContext);
2011-08-21 18:00:15 +00:00
return TRUE;
err:
2011-09-10 22:11:04 +00:00
DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
Tspi_Context_FreeMemory(hContext, NULL);
2011-09-10 08:55:21 +00:00
Tspi_Context_Close(hContext);
2011-08-21 18:00:15 +00:00
return FALSE;
}
2011-08-20 21:37:37 +00:00
/**
* See header
*/
pts_t *pts_create(bool is_imc)
2011-08-20 21:37:37 +00:00
{
private_pts_t *this;
INIT(this,
2011-10-08 20:17:50 +00:00
.public = {
.get_proto_caps = _get_proto_caps,
.set_proto_caps = _set_proto_caps,
.get_meas_algorithm = _get_meas_algorithm,
.set_meas_algorithm = _set_meas_algorithm,
.get_dh_hash_algorithm = _get_dh_hash_algorithm,
.set_dh_hash_algorithm = _set_dh_hash_algorithm,
.create_dh_nonce = _create_dh_nonce,
.get_my_public_value = _get_my_public_value,
.set_peer_public_value = _set_peer_public_value,
.calculate_secret = _calculate_secret,
.get_platform_info = _get_platform_info,
.set_platform_info = _set_platform_info,
.get_tpm_version_info = _get_tpm_version_info,
.set_tpm_version_info = _set_tpm_version_info,
.get_pcr_len = _get_pcr_len,
2011-10-08 20:17:50 +00:00
.get_aik = _get_aik,
.set_aik = _set_aik,
.is_path_valid = _is_path_valid,
.hash_file = _hash_file,
.do_measurements = _do_measurements,
.get_metadata = _get_metadata,
.read_pcr = _read_pcr,
.extend_pcr = _extend_pcr,
.quote_tpm = _quote_tpm,
2011-11-22 16:00:38 +00:00
.select_pcr = _select_pcr,
.add_pcr = _add_pcr,
.get_quote_info = _get_quote_info,
.verify_quote_signature = _verify_quote_signature,
2011-10-08 20:17:50 +00:00
.destroy = _destroy,
},
.is_imc = is_imc,
.proto_caps = PTS_PROTO_CAPS_V,
.algorithm = PTS_MEAS_ALGO_SHA256,
.dh_hash_algorithm = PTS_MEAS_ALGO_SHA256,
2011-08-20 21:37:37 +00:00
);
2011-08-21 18:00:15 +00:00
if (is_imc)
{
2011-09-06 22:48:25 +00:00
this->platform_info = extract_platform_info();
2011-08-21 18:00:15 +00:00
if (has_tpm(this))
{
this->has_tpm = TRUE;
this->pcr_len = PCR_LEN;
this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_D;
2011-09-05 09:15:34 +00:00
load_aik(this);
load_aik_blob(this);
2011-08-21 18:00:15 +00:00
}
}
else
{
this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_D;
}
2011-08-20 21:37:37 +00:00
return &this->public;
}