aboutsummaryrefslogtreecommitdiffstats
path: root/tools/vtpm_manager
diff options
context:
space:
mode:
authorkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>2005-09-20 09:08:26 +0000
committerkaf24@firebug.cl.cam.ac.uk <kaf24@firebug.cl.cam.ac.uk>2005-09-20 09:08:26 +0000
commit20e8ba075ba1a968006e4748ac4113ab84796549 (patch)
tree0edfd0bd04c4b2d7d23785db54f03dcc7fcc7259 /tools/vtpm_manager
parentbc6ef390faf4648d3cca12e68315512aebcf0db7 (diff)
downloadxen-20e8ba075ba1a968006e4748ac4113ab84796549.tar.gz
xen-20e8ba075ba1a968006e4748ac4113ab84796549.tar.bz2
xen-20e8ba075ba1a968006e4748ac4113ab84796549.zip
Add 64 bit support to the VTPM Tools plus do some minor cleanups.
The VTPM manager and VTPMs fully support both 32 and 64 bit OSes. The tpm_emulator (provided for debugging on TPM-less machines) does not support 64-bit kernels by default though. See the README for details on how to use it on 64-bit kernels. (Vinnie Scarlata, Intel Corporation) Signed-off-by: Joe Cihula <joe.cihula@intel.com>
Diffstat (limited to 'tools/vtpm_manager')
-rw-r--r--tools/vtpm_manager/README16
-rw-r--r--tools/vtpm_manager/Rules.mk3
-rw-r--r--tools/vtpm_manager/crypto/Makefile1
-rw-r--r--tools/vtpm_manager/manager/Makefile2
-rw-r--r--tools/vtpm_manager/manager/dmictl.c683
-rw-r--r--tools/vtpm_manager/manager/securestorage.c802
-rw-r--r--tools/vtpm_manager/manager/vtpm_manager.c1546
-rw-r--r--tools/vtpm_manager/manager/vtpmpriv.h4
-rw-r--r--tools/vtpm_manager/tcs/Makefile1
-rw-r--r--tools/vtpm_manager/tcs/contextmgr.c17
-rw-r--r--tools/vtpm_manager/tcs/contextmgr.h5
-rw-r--r--tools/vtpm_manager/tcs/tcs.c98
-rw-r--r--tools/vtpm_manager/tcs/tcs.h4
-rw-r--r--tools/vtpm_manager/tcs/transmit.c2
-rw-r--r--tools/vtpm_manager/util/Makefile1
-rw-r--r--tools/vtpm_manager/util/tcg.h6
16 files changed, 1672 insertions, 1519 deletions
diff --git a/tools/vtpm_manager/README b/tools/vtpm_manager/README
index 4c93d5a883..d01abf7e4b 100644
--- a/tools/vtpm_manager/README
+++ b/tools/vtpm_manager/README
@@ -51,14 +51,24 @@ VTPM_MULTI_VM -> Defined: VTPMs run in their own VMs
DUMMY_BACKEND -> vtpm_manager listens on /tmp/in.fifo and
/tmp/out.fifo rather than backend
-MANUAL_DM_LAUNCH -> User must manually launch & kill VTPMs
+MANUAL_DM_LAUNCH -> Must manually launch & kill VTPMs
-USE_FIXED_SRK_AUTH -> Do not randomly generate a random SRK & Owner auth
+WELL_KNOWN_SRK_AUTH -> Rather than randomly generating the password for the SRK,
+ use a well known value. This is necessary for sharing use
+ of the SRK across applications. Such as VTPM and Dom0
+ measurement software.
+
+WELL_KNOWN_OWNER_AUTH -> Rather than randomly generating the password for the owner,
+ use a well known value. This is useful for debugging and for
+ poor bios which do not support clearing TPM if OwnerAuth is
+ lost. However this has no protection from malicious app
+ issuing a TPM_OwnerClear to wipe the TPM
Requirements
============
- xen-unstable
-- IBM frontend/backend vtpm driver patch
+- vtpm frontend/backend driver patch
+- OpenSSL Library
Single-VM Flow
============================
diff --git a/tools/vtpm_manager/Rules.mk b/tools/vtpm_manager/Rules.mk
index 5a301e358c..26b44563c1 100644
--- a/tools/vtpm_manager/Rules.mk
+++ b/tools/vtpm_manager/Rules.mk
@@ -57,7 +57,8 @@ CFLAGS += -DLOGGING_MODULES="(BITMASK(VTPM_LOG_TCS)|BITMASK(VTPM_LOG_VTSP)|BITMA
#CFLAGS += -DMANUAL_DM_LAUNCH
# Fixed SRK
-CFLAGS += -DUSE_FIXED_SRK_AUTH
+CFLAGS += -DWELL_KNOWN_SRK_AUTH
+#CFLAGS += -DWELL_KNOWN_OWNER_AUTH
# TPM Hardware Device or TPM Simulator
#CFLAGS += -DTPM_HWDEV
diff --git a/tools/vtpm_manager/crypto/Makefile b/tools/vtpm_manager/crypto/Makefile
index a1926d7e76..cb5f65806a 100644
--- a/tools/vtpm_manager/crypto/Makefile
+++ b/tools/vtpm_manager/crypto/Makefile
@@ -13,6 +13,7 @@ clean:
rm -f *.a *.so *.o *.rpm $(DEP_FILES)
mrproper: clean
+ rm -f *~
$(BIN): $(OBJS)
$(AR) rcs $(BIN) $(OBJS)
diff --git a/tools/vtpm_manager/manager/Makefile b/tools/vtpm_manager/manager/Makefile
index 83fc5def92..2933627c84 100644
--- a/tools/vtpm_manager/manager/Makefile
+++ b/tools/vtpm_manager/manager/Makefile
@@ -17,7 +17,7 @@ clean:
rm -f *.a *.so *.o *.rpm $(DEP_FILES)
mrproper: clean
- rm -f $(BIN)
+ rm -f $(BIN) *~
$(BIN): $(OBJS)
$(CC) $(LDFLAGS) $^ $(LIBS) -o $@
diff --git a/tools/vtpm_manager/manager/dmictl.c b/tools/vtpm_manager/manager/dmictl.c
index d061a920ee..17625bc633 100644
--- a/tools/vtpm_manager/manager/dmictl.c
+++ b/tools/vtpm_manager/manager/dmictl.c
@@ -1,339 +1,344 @@
-// ===================================================================
-//
-// Copyright (c) 2005, Intel Corp.
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following
-// disclaimer in the documentation and/or other materials provided
-// with the distribution.
-// * Neither the name of Intel Corporation nor the names of its
-// contributors may be used to endorse or promote products derived
-// from this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
-// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
-// OF THE POSSIBILITY OF SUCH DAMAGE.
-// ===================================================================
-//
-// dmictl.c
-//
-// Functions for creating and destroying DMIs
-//
-// ==================================================================
-
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-
-#ifndef VTPM_MUTLI_VM
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <wait.h>
-#endif
-
-#include "vtpmpriv.h"
-#include "bsg.h"
-#include "buffer.h"
-#include "log.h"
-#include "hashtable.h"
-#include "hashtable_itr.h"
-
-#define TPM_EMULATOR_PATH "/usr/bin/vtpmd"
-
-TPM_RESULT close_dmi( VTPM_DMI_RESOURCE *dmi_res) {
- TPM_RESULT status = TPM_FAIL;
-
- if (dmi_res == NULL)
- return TPM_SUCCESS;
-
- status = TCS_CloseContext(dmi_res->TCSContext);
- free ( dmi_res->NVMLocation );
- dmi_res->connected = FALSE;
-
-#ifndef VTPM_MULTI_VM
- free(dmi_res->guest_tx_fname);
- free(dmi_res->vtpm_tx_fname);
-
- close(dmi_res->guest_tx_fh); dmi_res->guest_tx_fh = -1;
- close(dmi_res->vtpm_tx_fh); dmi_res->vtpm_tx_fh = -1;
-
-
- #ifndef MANUAL_DM_LAUNCH
- if (dmi_res->dmi_id != VTPM_CTL_DM) {
- if (dmi_res->dmi_pid != 0) {
- vtpmloginfo(VTPM_LOG_VTPM, "Killing dmi on pid %d.\n", dmi_res->dmi_pid);
- if ((kill(dmi_res->dmi_pid, SIGKILL) !=0) ||
- (waitpid(dmi_res->dmi_pid, NULL, 0) != dmi_res->dmi_pid)){
- vtpmlogerror(VTPM_LOG_VTPM, "Could not kill dmi on pid %d.\n", dmi_res->dmi_pid);
- status = TPM_FAIL;
- }
- } else
- vtpmlogerror(VTPM_LOG_VTPM, "Could not kill dmi because it's pid was 0.\n");
- }
- #endif
-#endif
-
- return status;
-}
-
-TPM_RESULT VTPM_Handle_New_DMI( const buffer_t *param_buf) {
-
- VTPM_DMI_RESOURCE *new_dmi=NULL;
- TPM_RESULT status=TPM_FAIL;
- BYTE type;
- UINT32 dmi_id, domain_id, *dmi_id_key;
- int fh;
-
-#ifndef VTPM_MUTLI_VM
- char dmi_id_str[11]; // UINT32s are up to 10 digits + NULL
- struct stat file_info;
-#endif
-
- if (param_buf == NULL) { // Assume creation of Dom 0 control
- type = 0;
- domain_id = VTPM_CTL_DM;
- dmi_id = VTPM_CTL_DM;
- } else if (buffer_len(param_buf) != sizeof(BYTE) + sizeof(UINT32) *2) {
- vtpmloginfo(VTPM_LOG_VTPM, "New DMI command wrong length: %d.\n", buffer_len(param_buf));
- status = TPM_BAD_PARAMETER;
- goto abort_egress;
- } else {
- BSG_UnpackList( param_buf->bytes, 3,
- BSG_TYPE_BYTE, &type,
- BSG_TYPE_UINT32, &domain_id,
- BSG_TYPE_UINT32, &dmi_id);
- }
-
- new_dmi = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi_id);
- if (new_dmi == NULL) {
- vtpmloginfo(VTPM_LOG_VTPM, "Creating new DMI instance %d attached on domain %d.\n", dmi_id, domain_id);
- // Brand New DMI. Initialize the persistent pieces
- if ((new_dmi = (VTPM_DMI_RESOURCE *) malloc (sizeof(VTPM_DMI_RESOURCE))) == NULL) {
- status = TPM_RESOURCES;
- goto abort_egress;
- }
- memset(new_dmi, 0, sizeof(VTPM_DMI_RESOURCE));
- new_dmi->dmi_id = dmi_id;
- new_dmi->connected = FALSE;
-
- if ((dmi_id_key = (UINT32 *) malloc (sizeof(UINT32))) == NULL) {
- status = TPM_RESOURCES;
- goto abort_egress;
- }
- *dmi_id_key = new_dmi->dmi_id;
-
- // install into map
- if (!hashtable_insert(vtpm_globals->dmi_map, dmi_id_key, new_dmi)){
- free(new_dmi);
- free(dmi_id_key);
- status = TPM_FAIL;
- goto egress;
- }
-
- } else
- vtpmloginfo(VTPM_LOG_VTPM, "Re-attaching DMI instance %d on domain %d .\n", dmi_id, domain_id);
-
- if (new_dmi->connected) {
- vtpmlogerror(VTPM_LOG_VTPM, "Attempt to re-attach, currently attached instance %d. Ignoring\n", dmi_id);
- status = TPM_BAD_PARAMETER;
- goto egress;
- }
-
- // Initialize the Non-persistent pieces
- new_dmi->dmi_domain_id = domain_id;
- new_dmi->NVMLocation = NULL;
-
- new_dmi->TCSContext = 0;
- TPMTRYRETURN( TCS_OpenContext(&new_dmi->TCSContext) );
-
- new_dmi->NVMLocation = (char *) malloc(11 + strlen(DMI_NVM_FILE));
- sprintf(new_dmi->NVMLocation, DMI_NVM_FILE, (uint32_t) new_dmi->dmi_id);
-
- // Measure DMI
- // FIXME: This will measure DMI. Until then use a fixed DMI_Measurement value
- /*
- fh = open(TPM_EMULATOR_PATH, O_RDONLY);
- stat_ret = fstat(fh, &file_stat);
- if (stat_ret == 0)
- dmi_size = file_stat.st_size;
- else {
- vtpmlogerror(VTPM_LOG_VTPM, "Could not open tpm_emulator!!\n");
- status = TPM_IOERROR;
- goto abort_egress;
- }
- dmi_buffer
- */
- memset(&new_dmi->DMI_measurement, 0xcc, sizeof(TPM_DIGEST));
-
-#ifndef VTPM_MULTI_VM
- if (dmi_id != VTPM_CTL_DM) {
- // Create a pair of fifo pipes
- if( (new_dmi->guest_tx_fname = (char *) malloc(11 + strlen(GUEST_TX_FIFO))) == NULL){
- status = TPM_RESOURCES;
- goto abort_egress;
- }
- sprintf(new_dmi->guest_tx_fname, GUEST_TX_FIFO, (uint32_t) dmi_id);
-
- if ((new_dmi->vtpm_tx_fname = (char *) malloc(11 + strlen(VTPM_TX_FIFO))) == NULL) {
- status = TPM_RESOURCES;
- goto abort_egress;
- }
- sprintf(new_dmi->vtpm_tx_fname, VTPM_TX_FIFO, (uint32_t) dmi_id);
-
- new_dmi->guest_tx_fh = -1;
- new_dmi->vtpm_tx_fh= -1;
-
- if ( stat(new_dmi->guest_tx_fname, &file_info) == -1) {
- if ( mkfifo(new_dmi->guest_tx_fname, S_IWUSR | S_IRUSR ) ){
- status = TPM_FAIL;
- goto abort_egress;
- }
- }
-
- if ( (fh = open(new_dmi->vtpm_tx_fname, O_RDWR)) == -1) {
- if ( mkfifo(new_dmi->vtpm_tx_fname, S_IWUSR | S_IRUSR ) ) {
- status = TPM_FAIL;
- goto abort_egress;
- }
- }
-
- // Launch DMI
- sprintf(dmi_id_str, "%d", (int) dmi_id);
-#ifdef MANUAL_DM_LAUNCH
- vtpmlogerror(VTPM_LOG_VTPM, "FAKING starting vtpm with dmi=%s\n", dmi_id_str);
- new_dmi->dmi_pid = 0;
-#else
- pid_t pid = fork();
-
- if (pid == -1) {
- vtpmlogerror(VTPM_LOG_VTPM, "Could not fork to launch vtpm\n");
- status = TPM_RESOURCES;
- goto abort_egress;
- } else if (pid == 0) {
- if ( stat(new_dmi->NVMLocation, &file_info) == -1)
- execl (TPM_EMULATOR_PATH, "vtmpd", "clear", dmi_id_str, NULL);
- else
- execl (TPM_EMULATOR_PATH, "vtpmd", "save", dmi_id_str, NULL);
-
- // Returning from these at all is an error.
- vtpmlogerror(VTPM_LOG_VTPM, "Could not exec to launch vtpm\n");
- } else {
- new_dmi->dmi_pid = pid;
- vtpmloginfo(VTPM_LOG_VTPM, "Launching DMI on PID = %d\n", pid);
- }
-#endif // MANUAL_DM_LAUNCH
- }
-#else // VTPM_MUTLI_VM
- // FIXME: Measure DMI through call to Measurement agent in platform.
-#endif
-
- vtpm_globals->DMI_table_dirty = TRUE;
- new_dmi->connected = TRUE;
- status=TPM_SUCCESS;
- goto egress;
-
- abort_egress:
- close_dmi( new_dmi );
-
- egress:
- return status;
-}
-
-TPM_RESULT VTPM_Handle_Close_DMI( const buffer_t *param_buf) {
-
- TPM_RESULT status=TPM_FAIL;
- VTPM_DMI_RESOURCE *dmi_res=NULL;
- UINT32 dmi_id;
-
- if ((param_buf == NULL) || (buffer_len(param_buf) != sizeof(UINT32)) ) {
- vtpmlogerror(VTPM_LOG_VTPM, "Closing DMI has bad size.");
- status = TPM_BAD_PARAMETER;
- goto abort_egress;
- }
-
- BSG_UnpackList( param_buf->bytes, 1,
- BSG_TYPE_UINT32, &dmi_id);
-
- vtpmloginfo(VTPM_LOG_VTPM, "Closing DMI %d.\n", dmi_id);
-
- dmi_res = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi_id);
- if (dmi_res == NULL ) {
- vtpmlogerror(VTPM_LOG_VTPM, "Trying to close nonexistent DMI.\n");
- status = TPM_BAD_PARAMETER;
- goto abort_egress;
- }
-
- if (!dmi_res->connected) {
- vtpmlogerror(VTPM_LOG_VTPM, "Closing non-connected DMI.\n");
- status = TPM_BAD_PARAMETER;
- goto abort_egress;
- }
-
- // Close Dmi
- TPMTRYRETURN(close_dmi( dmi_res ));
-
- status=TPM_SUCCESS;
- goto egress;
-
- abort_egress:
- egress:
-
- return status;
-}
-
-TPM_RESULT VTPM_Handle_Delete_DMI( const buffer_t *param_buf) {
-
- TPM_RESULT status=TPM_FAIL;
- VTPM_DMI_RESOURCE *dmi_res=NULL;
- UINT32 dmi_id;
-
- if ((param_buf == NULL) || (buffer_len(param_buf) != sizeof(UINT32)) ) {
- vtpmlogerror(VTPM_LOG_VTPM, "Closing DMI has bad size.\n");
- status = TPM_BAD_PARAMETER;
- goto abort_egress;
- }
-
- BSG_UnpackList( param_buf->bytes, 1,
- BSG_TYPE_UINT32, &dmi_id);
-
- vtpmloginfo(VTPM_LOG_VTPM, "Deleting DMI %d.\n", dmi_id);
-
- dmi_res = (VTPM_DMI_RESOURCE *) hashtable_remove(vtpm_globals->dmi_map, &dmi_id);
- if (dmi_res == NULL) {
- vtpmlogerror(VTPM_LOG_VTPM, "Closing non-existent DMI.\n");
- status = TPM_BAD_PARAMETER;
- goto abort_egress;
- }
-
- //TODO: Automatically delete file dmi_res->NVMLocation
-
- // Close DMI first
- TPMTRYRETURN(close_dmi( dmi_res ));
- free ( dmi_res );
-
- status=TPM_SUCCESS;
- goto egress;
-
- abort_egress:
- egress:
-
- return status;
-}
+// ===================================================================
+//
+// Copyright (c) 2005, Intel Corp.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Intel Corporation nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+// OF THE POSSIBILITY OF SUCH DAMAGE.
+// ===================================================================
+//
+// dmictl.c
+//
+// Functions for creating and destroying DMIs
+//
+// ==================================================================
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+#ifndef VTPM_MUTLI_VM
+ #include <sys/types.h>
+ #include <sys/stat.h>
+ #include <fcntl.h>
+ #include <signal.h>
+ #include <wait.h>
+#endif
+
+#include "vtpmpriv.h"
+#include "bsg.h"
+#include "buffer.h"
+#include "log.h"
+#include "hashtable.h"
+#include "hashtable_itr.h"
+
+#define TPM_EMULATOR_PATH "/usr/bin/vtpmd"
+
+TPM_RESULT close_dmi( VTPM_DMI_RESOURCE *dmi_res) {
+ TPM_RESULT status = TPM_FAIL;
+
+ if (dmi_res == NULL)
+ return TPM_SUCCESS;
+
+ status = TCS_CloseContext(dmi_res->TCSContext);
+ free ( dmi_res->NVMLocation );
+ dmi_res->connected = FALSE;
+
+#ifndef VTPM_MULTI_VM
+ free(dmi_res->guest_tx_fname);
+ free(dmi_res->vtpm_tx_fname);
+
+ close(dmi_res->guest_tx_fh); dmi_res->guest_tx_fh = -1;
+ close(dmi_res->vtpm_tx_fh); dmi_res->vtpm_tx_fh = -1;
+
+ #ifndef MANUAL_DM_LAUNCH
+ if (dmi_res->dmi_id != VTPM_CTL_DM) {
+ if (dmi_res->dmi_pid != 0) {
+ vtpmloginfo(VTPM_LOG_VTPM, "Killing dmi on pid %d.\n", dmi_res->dmi_pid);
+ if (kill(dmi_res->dmi_pid, SIGKILL) !=0) {
+ vtpmloginfo(VTPM_LOG_VTPM, "DMI on pid %d is already dead.\n", dmi_res->dmi_pid);
+ } else if (waitpid(dmi_res->dmi_pid, NULL, 0) != dmi_res->dmi_pid) {
+ vtpmlogerror(VTPM_LOG_VTPM, "DMI on pid %d failed to stop.\n", dmi_res->dmi_pid);
+ status = TPM_FAIL;
+ }
+ } else {
+ vtpmlogerror(VTPM_LOG_VTPM, "Could not kill dmi because it's pid was 0.\n");
+ status = TPM_FAIL;
+ }
+ }
+ #endif
+#endif
+
+ return status;
+}
+
+TPM_RESULT VTPM_Handle_New_DMI( const buffer_t *param_buf) {
+
+ VTPM_DMI_RESOURCE *new_dmi=NULL;
+ TPM_RESULT status=TPM_FAIL;
+ BYTE type;
+ UINT32 dmi_id, domain_id, *dmi_id_key;
+
+#ifndef VTPM_MULTI_VM
+ int fh;
+ char dmi_id_str[11]; // UINT32s are up to 10 digits + NULL
+ struct stat file_info;
+#endif
+
+ if (param_buf == NULL) { // Assume creation of Dom 0 control
+ type = 0;
+ domain_id = VTPM_CTL_DM;
+ dmi_id = VTPM_CTL_DM;
+ } else if (buffer_len(param_buf) != sizeof(BYTE) + sizeof(UINT32) *2) {
+ vtpmloginfo(VTPM_LOG_VTPM, "New DMI command wrong length: %d.\n", buffer_len(param_buf));
+ status = TPM_BAD_PARAMETER;
+ goto abort_egress;
+ } else {
+ BSG_UnpackList( param_buf->bytes, 3,
+ BSG_TYPE_BYTE, &type,
+ BSG_TYPE_UINT32, &domain_id,
+ BSG_TYPE_UINT32, &dmi_id);
+ }
+
+ new_dmi = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi_id);
+ if (new_dmi == NULL) {
+ vtpmloginfo(VTPM_LOG_VTPM, "Creating new DMI instance %d attached on domain %d.\n", dmi_id, domain_id);
+ // Brand New DMI. Initialize the persistent pieces
+ if ((new_dmi = (VTPM_DMI_RESOURCE *) malloc (sizeof(VTPM_DMI_RESOURCE))) == NULL) {
+ status = TPM_RESOURCES;
+ goto abort_egress;
+ }
+ memset(new_dmi, 0, sizeof(VTPM_DMI_RESOURCE));
+ new_dmi->dmi_id = dmi_id;
+ new_dmi->connected = FALSE;
+
+ if ((dmi_id_key = (UINT32 *) malloc (sizeof(UINT32))) == NULL) {
+ status = TPM_RESOURCES;
+ goto abort_egress;
+ }
+ *dmi_id_key = new_dmi->dmi_id;
+
+ // install into map
+ if (!hashtable_insert(vtpm_globals->dmi_map, dmi_id_key, new_dmi)){
+ free(new_dmi);
+ free(dmi_id_key);
+ status = TPM_FAIL;
+ goto egress;
+ }
+
+ } else
+ vtpmloginfo(VTPM_LOG_VTPM, "Re-attaching DMI instance %d on domain %d .\n", dmi_id, domain_id);
+
+ if (new_dmi->connected) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Attempt to re-attach, currently attached instance %d. Ignoring\n", dmi_id);
+ status = TPM_BAD_PARAMETER;
+ goto egress;
+ }
+
+ // Initialize the Non-persistent pieces
+ new_dmi->dmi_domain_id = domain_id;
+ new_dmi->NVMLocation = NULL;
+
+ new_dmi->TCSContext = 0;
+ TPMTRYRETURN( TCS_OpenContext(&new_dmi->TCSContext) );
+
+ new_dmi->NVMLocation = (char *) malloc(11 + strlen(DMI_NVM_FILE));
+ sprintf(new_dmi->NVMLocation, DMI_NVM_FILE, (uint32_t) new_dmi->dmi_id);
+
+ // Measure DMI
+ // FIXME: This will measure DMI. Until then use a fixed DMI_Measurement value
+ /*
+ fh = open(TPM_EMULATOR_PATH, O_RDONLY);
+ stat_ret = fstat(fh, &file_stat);
+ if (stat_ret == 0)
+ dmi_size = file_stat.st_size;
+ else {
+ vtpmlogerror(VTPM_LOG_VTPM, "Could not open tpm_emulator!!\n");
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+ dmi_buffer
+ */
+ memset(&new_dmi->DMI_measurement, 0xcc, sizeof(TPM_DIGEST));
+
+#ifndef VTPM_MULTI_VM
+ if (dmi_id != VTPM_CTL_DM) {
+ // Create a pair of fifo pipes
+ if( (new_dmi->guest_tx_fname = (char *) malloc(11 + strlen(GUEST_TX_FIFO))) == NULL){
+ status = TPM_RESOURCES;
+ goto abort_egress;
+ }
+ sprintf(new_dmi->guest_tx_fname, GUEST_TX_FIFO, (uint32_t) dmi_id);
+
+ if ((new_dmi->vtpm_tx_fname = (char *) malloc(11 + strlen(VTPM_TX_FIFO))) == NULL) {
+ status = TPM_RESOURCES;
+ goto abort_egress;
+ }
+ sprintf(new_dmi->vtpm_tx_fname, VTPM_TX_FIFO, (uint32_t) dmi_id);
+
+ new_dmi->guest_tx_fh = -1;
+ new_dmi->vtpm_tx_fh= -1;
+
+ if ( stat(new_dmi->guest_tx_fname, &file_info) == -1) {
+ if ( mkfifo(new_dmi->guest_tx_fname, S_IWUSR | S_IRUSR ) ){
+ vtpmlogerror(VTPM_LOG_VTPM, "Failed to create dmi fifo.\n");
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+ }
+
+ if ( (fh = open(new_dmi->vtpm_tx_fname, O_RDWR)) == -1) {
+ if ( mkfifo(new_dmi->vtpm_tx_fname, S_IWUSR | S_IRUSR ) ) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Failed to create dmi fifo.\n");
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+ }
+
+ // Launch DMI
+ sprintf(dmi_id_str, "%d", (int) dmi_id);
+#ifdef MANUAL_DM_LAUNCH
+ vtpmlogerror(VTPM_LOG_VTPM, "FAKING starting vtpm with dmi=%s\n", dmi_id_str);
+ new_dmi->dmi_pid = 0;
+#else
+ pid_t pid = fork();
+
+ if (pid == -1) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Could not fork to launch vtpm\n");
+ status = TPM_RESOURCES;
+ goto abort_egress;
+ } else if (pid == 0) {
+ if ( stat(new_dmi->NVMLocation, &file_info) == -1)
+ execl (TPM_EMULATOR_PATH, "vtmpd", "clear", dmi_id_str, NULL);
+ else
+ execl (TPM_EMULATOR_PATH, "vtpmd", "save", dmi_id_str, NULL);
+
+ // Returning from these at all is an error.
+ vtpmlogerror(VTPM_LOG_VTPM, "Could not exec to launch vtpm\n");
+ } else {
+ new_dmi->dmi_pid = pid;
+ vtpmloginfo(VTPM_LOG_VTPM, "Launching DMI on PID = %d\n", pid);
+ }
+#endif // MANUAL_DM_LAUNCH
+ }
+#else // VTPM_MUTLI_VM
+ // FIXME: Measure DMI through call to Measurement agent in platform.
+#endif
+
+ vtpm_globals->DMI_table_dirty = TRUE;
+ new_dmi->connected = TRUE;
+ status=TPM_SUCCESS;
+ goto egress;
+
+ abort_egress:
+ vtpmlogerror(VTPM_LOG_VTPM, "Failed to create DMI id=%d due to status=%s. Cleaning.\n", dmi_id, tpm_get_error_name(status));
+ close_dmi( new_dmi );
+
+ egress:
+ return status;
+}
+
+TPM_RESULT VTPM_Handle_Close_DMI( const buffer_t *param_buf) {
+
+ TPM_RESULT status=TPM_FAIL;
+ VTPM_DMI_RESOURCE *dmi_res=NULL;
+ UINT32 dmi_id;
+
+ if ((param_buf == NULL) || (buffer_len(param_buf) != sizeof(UINT32)) ) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Closing DMI has bad size.");
+ status = TPM_BAD_PARAMETER;
+ goto abort_egress;
+ }
+
+ BSG_UnpackList( param_buf->bytes, 1,
+ BSG_TYPE_UINT32, &dmi_id);
+
+ vtpmloginfo(VTPM_LOG_VTPM, "Closing DMI %d.\n", dmi_id);
+
+ dmi_res = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi_id);
+ if (dmi_res == NULL ) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Trying to close nonexistent DMI.\n");
+ status = TPM_BAD_PARAMETER;
+ goto abort_egress;
+ }
+
+ if (!dmi_res->connected) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Closing non-connected DMI.\n");
+ status = TPM_BAD_PARAMETER;
+ goto abort_egress;
+ }
+
+ // Close Dmi
+ TPMTRYRETURN(close_dmi( dmi_res ));
+
+ status=TPM_SUCCESS;
+ goto egress;
+
+ abort_egress:
+ egress:
+
+ return status;
+}
+
+TPM_RESULT VTPM_Handle_Delete_DMI( const buffer_t *param_buf) {
+
+ TPM_RESULT status=TPM_FAIL;
+ VTPM_DMI_RESOURCE *dmi_res=NULL;
+ UINT32 dmi_id;
+
+ if ((param_buf == NULL) || (buffer_len(param_buf) != sizeof(UINT32)) ) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Closing DMI has bad size.\n");
+ status = TPM_BAD_PARAMETER;
+ goto abort_egress;
+ }
+
+ BSG_UnpackList( param_buf->bytes, 1,
+ BSG_TYPE_UINT32, &dmi_id);
+
+ vtpmloginfo(VTPM_LOG_VTPM, "Deleting DMI %d.\n", dmi_id);
+
+ dmi_res = (VTPM_DMI_RESOURCE *) hashtable_remove(vtpm_globals->dmi_map, &dmi_id);
+ if (dmi_res == NULL) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Closing non-existent DMI.\n");
+ status = TPM_BAD_PARAMETER;
+ goto abort_egress;
+ }
+
+ //TODO: Automatically delete file dmi_res->NVMLocation
+
+ // Close DMI first
+ TPMTRYRETURN(close_dmi( dmi_res ));
+ free ( dmi_res );
+
+ status=TPM_SUCCESS;
+ goto egress;
+
+ abort_egress:
+ egress:
+
+ return status;
+}
diff --git a/tools/vtpm_manager/manager/securestorage.c b/tools/vtpm_manager/manager/securestorage.c
index 34a9fbed83..5cef086802 100644
--- a/tools/vtpm_manager/manager/securestorage.c
+++ b/tools/vtpm_manager/manager/securestorage.c
@@ -1,401 +1,401 @@
-// ===================================================================
-//
-// Copyright (c) 2005, Intel Corp.
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following
-// disclaimer in the documentation and/or other materials provided
-// with the distribution.
-// * Neither the name of Intel Corporation nor the names of its
-// contributors may be used to endorse or promote products derived
-// from this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
-// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
-// OF THE POSSIBILITY OF SUCH DAMAGE.
-// ===================================================================
-//
-// securestorage.c
-//
-// Functions regarding securely storing DMI secrets.
-//
-// ==================================================================
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <string.h>
-
-#include "tcg.h"
-#include "vtpm_manager.h"
-#include "vtpmpriv.h"
-#include "vtsp.h"
-#include "bsg.h"
-#include "crypto.h"
-#include "hashtable.h"
-#include "hashtable_itr.h"
-#include "buffer.h"
-#include "log.h"
-
-TPM_RESULT VTPM_Handle_Save_NVM(VTPM_DMI_RESOURCE *myDMI,
- const buffer_t *inbuf,
- buffer_t *outbuf) {
-
- TPM_RESULT status = TPM_SUCCESS;
- symkey_t symkey;
- buffer_t state_cipher = NULL_BUF,
- symkey_cipher = NULL_BUF;
- int fh;
- long bytes_written;
- BYTE *sealed_NVM=NULL;
- UINT32 sealed_NVM_size, i;
- struct pack_constbuf_t symkey_cipher32, state_cipher32;
-
- vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Save_NVMing[%d]: 0x", buffer_len(inbuf));
- for (i=0; i< buffer_len(inbuf); i++)
- vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", inbuf->bytes[i]);
- vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
-
- // Generate a sym key and encrypt state with it
- TPMTRY(TPM_ENCRYPT_ERROR, Crypto_symcrypto_genkey (&symkey) );
- TPMTRY(TPM_ENCRYPT_ERROR, Crypto_symcrypto_encrypt (&symkey, inbuf, &state_cipher) );
-
- // Encrypt symmetric key
- TPMTRYRETURN( VTSP_Bind( &vtpm_globals->storageKey,
- &symkey.key,
- &symkey_cipher) );
-
- // Create output blob: symkey_size + symkey_cipher + state_cipher_size + state_cipher
-
- symkey_cipher32.size = buffer_len(&symkey_cipher);
- symkey_cipher32.data = symkey_cipher.bytes;
-
- state_cipher32.size = buffer_len(&state_cipher);
- state_cipher32.data = state_cipher.bytes;
-
- sealed_NVM = (BYTE *) malloc( 2 * sizeof(UINT32) + symkey_cipher32.size + state_cipher32.size);
-
- sealed_NVM_size = BSG_PackList(sealed_NVM, 2,
- BSG_TPM_SIZE32_DATA, &symkey_cipher32,
- BSG_TPM_SIZE32_DATA, &state_cipher32);
-
- // Mark DMI Table so new save state info will get pushed to disk on return.
- vtpm_globals->DMI_table_dirty = TRUE;
-
- // Write sealed blob off disk from NVMLocation
- // TODO: How to properly return from these. Do we care if we return failure
- // after writing the file? We can't get the old one back.
- // TODO: Backup old file and try and recover that way.
- fh = open(myDMI->NVMLocation, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
- if ( (bytes_written = write(fh, sealed_NVM, sealed_NVM_size) ) != (long) sealed_NVM_size) {
- vtpmlogerror(VTPM_LOG_VTPM, "We just overwrote a DMI_NVM and failed to finish. %ld/%ld bytes.\n", bytes_written, (long)sealed_NVM_size);
- status = TPM_IOERROR;
- goto abort_egress;
- }
- close(fh);
-
- Crypto_SHA1Full (sealed_NVM, sealed_NVM_size, (BYTE *) &myDMI->NVM_measurement);
-
- vtpmloginfo(VTPM_LOG_VTPM, "Saved %d bytes of E(symkey) + %d bytes of E(NVM)\n", buffer_len(&symkey_cipher), buffer_len(&state_cipher));
- goto egress;
-
- abort_egress:
- vtpmlogerror(VTPM_LOG_VTPM, "Failed to load NVM\n.");
-
- egress:
-
- buffer_free ( &state_cipher);
- buffer_free ( &symkey_cipher);
- free(sealed_NVM);
- Crypto_symcrypto_freekey (&symkey);
-
- return status;
-}
-
-
-/* inbuf = null outbuf = sealed blob size, sealed blob.*/
-TPM_RESULT VTPM_Handle_Load_NVM(VTPM_DMI_RESOURCE *myDMI,
- const buffer_t *inbuf,
- buffer_t *outbuf) {
-
- TPM_RESULT status = TPM_SUCCESS;
- symkey_t symkey;
- buffer_t state_cipher = NULL_BUF,
- symkey_clear = NULL_BUF,
- symkey_cipher = NULL_BUF;
- struct pack_buf_t symkey_cipher32, state_cipher32;
-
- UINT32 sealed_NVM_size;
- BYTE *sealed_NVM = NULL;
- long fh_size;
- int fh, stat_ret, i;
- struct stat file_stat;
- TPM_DIGEST sealedNVMHash;
-
- memset(&symkey, 0, sizeof(symkey_t));
-
- if (myDMI->NVMLocation == NULL) {
- vtpmlogerror(VTPM_LOG_VTPM, "Unable to load NVM because the file name NULL.\n");
- status = TPM_AUTHFAIL;
- goto abort_egress;
- }
-
- //Read sealed blob off disk from NVMLocation
- fh = open(myDMI->NVMLocation, O_RDONLY);
- stat_ret = fstat(fh, &file_stat);
- if (stat_ret == 0)
- fh_size = file_stat.st_size;
- else {
- status = TPM_IOERROR;
- goto abort_egress;
- }
-
- sealed_NVM = (BYTE *) malloc(fh_size);
- if (read(fh, sealed_NVM, fh_size) != fh_size) {
- status = TPM_IOERROR;
- goto abort_egress;
- }
- close(fh);
-
- vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Load_NVMing[%ld]: 0x", fh_size);
- for (i=0; i< fh_size; i++)
- vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", sealed_NVM[i]);
- vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
-
- sealed_NVM_size = BSG_UnpackList(sealed_NVM, 2,
- BSG_TPM_SIZE32_DATA, &symkey_cipher32,
- BSG_TPM_SIZE32_DATA, &state_cipher32);
-
- TPMTRYRETURN( buffer_init_convert (&symkey_cipher,
- symkey_cipher32.size,
- symkey_cipher32.data) );
-
- TPMTRYRETURN( buffer_init_convert (&state_cipher,
- state_cipher32.size,
- state_cipher32.data) );
-
- Crypto_SHA1Full(sealed_NVM, sealed_NVM_size, (BYTE *) &sealedNVMHash);
-
- // Verify measurement of sealed blob.
- if (memcmp(&sealedNVMHash, &myDMI->NVM_measurement, sizeof(TPM_DIGEST)) ) {
- vtpmlogerror(VTPM_LOG_VTPM, "VTPM LoadNVM NVM measurement check failed.\n");
- vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Correct hash: ");
- for (i=0; i< sizeof(TPM_DIGEST); i++)
- vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", ((BYTE*)&myDMI->NVM_measurement)[i]);
- vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
-
- vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Measured hash: ");
- for (i=0; i< sizeof(TPM_DIGEST); i++)
- vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", ((BYTE*)&sealedNVMHash)[i]);
- vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
-
- status = TPM_AUTHFAIL;
- goto abort_egress;
- }
-
- // Decrypt Symmetric Key
- TPMTRYRETURN( VTSP_Unbind( myDMI->TCSContext,
- vtpm_globals->storageKeyHandle,
- &symkey_cipher,
- (const TPM_AUTHDATA*)&vtpm_globals->storage_key_usage_auth,
- &symkey_clear,
- &(vtpm_globals->keyAuth) ) );
-
- // create symmetric key using saved bits
- Crypto_symcrypto_initkey (&symkey, &symkey_clear);
-
- // Decrypt State
- TPMTRY(TPM_DECRYPT_ERROR, Crypto_symcrypto_decrypt (&symkey, &state_cipher, outbuf) );
-
- goto egress;
-
- abort_egress:
- vtpmlogerror(VTPM_LOG_VTPM, "Failed to load NVM\n.");
-
- egress:
-
- buffer_free ( &state_cipher);
- buffer_free ( &symkey_clear);
- buffer_free ( &symkey_cipher);
- free( sealed_NVM );
- Crypto_symcrypto_freekey (&symkey);
-
- return status;
-}
-
-TPM_RESULT VTPM_SaveService(void) {
- TPM_RESULT status=TPM_SUCCESS;
- int fh, dmis=-1;
-
- BYTE *flat_global;
- int flat_global_size, bytes_written;
- UINT32 storageKeySize = buffer_len(&vtpm_globals->storageKeyWrap);
- struct pack_buf_t storage_key_pack = {storageKeySize, vtpm_globals->storageKeyWrap.bytes};
-
- struct hashtable_itr *dmi_itr;
- VTPM_DMI_RESOURCE *dmi_res;
-
- UINT32 flat_global_full_size;
-
- // Global Values needing to be saved
- flat_global_full_size = 3*sizeof(TPM_DIGEST) + // Auths
- sizeof(UINT32) + // storagekeysize
- storageKeySize + // storage key
- hashtable_count(vtpm_globals->dmi_map) * // num DMIS
- (sizeof(UINT32) + 2*sizeof(TPM_DIGEST)); // Per DMI info
-
-
- flat_global = (BYTE *) malloc( flat_global_full_size);
-
- flat_global_size = BSG_PackList(flat_global, 4,
- BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth,
- BSG_TPM_AUTHDATA, &vtpm_globals->srk_usage_auth,
- BSG_TPM_SECRET, &vtpm_globals->storage_key_usage_auth,
- BSG_TPM_SIZE32_DATA, &storage_key_pack);
-
- // Per DMI values to be saved
- if (hashtable_count(vtpm_globals->dmi_map) > 0) {
-
- dmi_itr = hashtable_iterator(vtpm_globals->dmi_map);
- do {
- dmi_res = (VTPM_DMI_RESOURCE *) hashtable_iterator_value(dmi_itr);
- dmis++;
-
- // No need to save dmi0.
- if (dmi_res->dmi_id == 0)
- continue;
-
-
- flat_global_size += BSG_PackList( flat_global + flat_global_size, 3,
- BSG_TYPE_UINT32, &dmi_res->dmi_id,
- BSG_TPM_DIGEST, &dmi_res->NVM_measurement,
- BSG_TPM_DIGEST, &dmi_res->DMI_measurement);
-
- } while (hashtable_iterator_advance(dmi_itr));
- }
-
- //FIXME: Once we have a way to protect a TPM key, we should use it to
- // encrypt this blob. BUT, unless there is a way to ensure the key is
- // not used by other apps, this encryption is useless.
- fh = open(STATE_FILE, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
- if (fh == -1) {
- vtpmlogerror(VTPM_LOG_VTPM, "Unable to open %s file for write.\n", STATE_FILE);
- status = TPM_IOERROR;
- goto abort_egress;
- }
-
- if ( (bytes_written = write(fh, flat_global, flat_global_size)) != flat_global_size ) {
- vtpmlogerror(VTPM_LOG_VTPM, "Failed to save service data. %d/%d bytes written.\n", bytes_written, flat_global_size);
- status = TPM_IOERROR;
- goto abort_egress;
- }
- vtpm_globals->DMI_table_dirty = FALSE;
-
- goto egress;
-
- abort_egress:
- egress:
-
- free(flat_global);
- close(fh);
-
- vtpmloginfo(VTPM_LOG_VTPM, "Saved VTPM Service state (status = %d, dmis = %d)\n", (int) status, dmis);
- return status;
-}
-
-TPM_RESULT VTPM_LoadService(void) {
-
- TPM_RESULT status=TPM_SUCCESS;
- int fh, stat_ret, dmis=0;
- long fh_size = 0, step_size;
- BYTE *flat_global=NULL;
- struct pack_buf_t storage_key_pack;
- UINT32 *dmi_id_key;
-
- VTPM_DMI_RESOURCE *dmi_res;
- struct stat file_stat;
-
- fh = open(STATE_FILE, O_RDONLY );
- stat_ret = fstat(fh, &file_stat);
- if (stat_ret == 0)
- fh_size = file_stat.st_size;
- else {
- status = TPM_IOERROR;
- goto abort_egress;
- }
-
- flat_global = (BYTE *) malloc(fh_size);
-
- if ((long) read(fh, flat_global, fh_size) != fh_size ) {
- status = TPM_IOERROR;
- goto abort_egress;
- }
-
- // Global Values needing to be saved
- step_size = BSG_UnpackList( flat_global, 4,
- BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth,
- BSG_TPM_AUTHDATA, &vtpm_globals->srk_usage_auth,
- BSG_TPM_SECRET, &vtpm_globals->storage_key_usage_auth,
- BSG_TPM_SIZE32_DATA, &storage_key_pack);
-
- TPMTRYRETURN(buffer_init(&vtpm_globals->storageKeyWrap, 0, 0) );
- TPMTRYRETURN(buffer_append_raw(&vtpm_globals->storageKeyWrap, storage_key_pack.size, storage_key_pack.data) );
-
- // Per DMI values to be saved
- while ( step_size < fh_size ){
- if (fh_size - step_size < (long) (sizeof(UINT32) + 2*sizeof(TPM_DIGEST))) {
- vtpmlogerror(VTPM_LOG_VTPM, "Encountered %ld extra bytes at end of manager state.\n", fh_size-step_size);
- step_size = fh_size;
- } else {
- dmi_res = (VTPM_DMI_RESOURCE *) malloc(sizeof(VTPM_DMI_RESOURCE));
- dmis++;
-
- dmi_res->connected = FALSE;
-
- step_size += BSG_UnpackList(flat_global + step_size, 3,
- BSG_TYPE_UINT32, &dmi_res->dmi_id,
- BSG_TPM_DIGEST, &dmi_res->NVM_measurement,
- BSG_TPM_DIGEST, &dmi_res->DMI_measurement);
-
- // install into map
- dmi_id_key = (UINT32 *) malloc (sizeof(UINT32));
- *dmi_id_key = dmi_res->dmi_id;
- if (!hashtable_insert(vtpm_globals->dmi_map, dmi_id_key, dmi_res)) {
- status = TPM_FAIL;
- goto abort_egress;
- }
-
- }
-
- }
-
- goto egress;
-
- abort_egress:
- vtpmlogerror(VTPM_LOG_VTPM, "Failed to save service data\n");
- egress:
-
- if (flat_global)
- free(flat_global);
- close(fh);
-
- vtpmloginfo(VTPM_LOG_VTPM, "Previously saved state reloaded (status = %d, dmis = %d).\n", (int) status, dmis);
- return status;
-}
+// ===================================================================
+//
+// Copyright (c) 2005, Intel Corp.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Intel Corporation nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+// OF THE POSSIBILITY OF SUCH DAMAGE.
+// ===================================================================
+//
+// securestorage.c
+//
+// Functions regarding securely storing DMI secrets.
+//
+// ==================================================================
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "tcg.h"
+#include "vtpm_manager.h"
+#include "vtpmpriv.h"
+#include "vtsp.h"
+#include "bsg.h"
+#include "crypto.h"
+#include "hashtable.h"
+#include "hashtable_itr.h"
+#include "buffer.h"
+#include "log.h"
+
+TPM_RESULT VTPM_Handle_Save_NVM(VTPM_DMI_RESOURCE *myDMI,
+ const buffer_t *inbuf,
+ buffer_t *outbuf) {
+
+ TPM_RESULT status = TPM_SUCCESS;
+ symkey_t symkey;
+ buffer_t state_cipher = NULL_BUF,
+ symkey_cipher = NULL_BUF;
+ int fh;
+ long bytes_written;
+ BYTE *sealed_NVM=NULL;
+ UINT32 sealed_NVM_size, i;
+ struct pack_constbuf_t symkey_cipher32, state_cipher32;
+
+ vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Save_NVMing[%d]: 0x", buffer_len(inbuf));
+ for (i=0; i< buffer_len(inbuf); i++)
+ vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", inbuf->bytes[i]);
+ vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+
+ // Generate a sym key and encrypt state with it
+ TPMTRY(TPM_ENCRYPT_ERROR, Crypto_symcrypto_genkey (&symkey) );
+ TPMTRY(TPM_ENCRYPT_ERROR, Crypto_symcrypto_encrypt (&symkey, inbuf, &state_cipher) );
+
+ // Encrypt symmetric key
+ TPMTRYRETURN( VTSP_Bind( &vtpm_globals->storageKey,
+ &symkey.key,
+ &symkey_cipher) );
+
+ // Create output blob: symkey_size + symkey_cipher + state_cipher_size + state_cipher
+
+ symkey_cipher32.size = buffer_len(&symkey_cipher);
+ symkey_cipher32.data = symkey_cipher.bytes;
+
+ state_cipher32.size = buffer_len(&state_cipher);
+ state_cipher32.data = state_cipher.bytes;
+
+ sealed_NVM = (BYTE *) malloc( 2 * sizeof(UINT32) + symkey_cipher32.size + state_cipher32.size);
+
+ sealed_NVM_size = BSG_PackList(sealed_NVM, 2,
+ BSG_TPM_SIZE32_DATA, &symkey_cipher32,
+ BSG_TPM_SIZE32_DATA, &state_cipher32);
+
+ // Mark DMI Table so new save state info will get pushed to disk on return.
+ vtpm_globals->DMI_table_dirty = TRUE;
+
+ // Write sealed blob off disk from NVMLocation
+ // TODO: How to properly return from these. Do we care if we return failure
+ // after writing the file? We can't get the old one back.
+ // TODO: Backup old file and try and recover that way.
+ fh = open(myDMI->NVMLocation, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
+ if ( (bytes_written = write(fh, sealed_NVM, sealed_NVM_size) ) != (long) sealed_NVM_size) {
+ vtpmlogerror(VTPM_LOG_VTPM, "We just overwrote a DMI_NVM and failed to finish. %ld/%ld bytes.\n", bytes_written, (long)sealed_NVM_size);
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+ close(fh);
+
+ Crypto_SHA1Full (sealed_NVM, sealed_NVM_size, (BYTE *) &myDMI->NVM_measurement);
+
+ vtpmloginfo(VTPM_LOG_VTPM, "Saved %d bytes of E(symkey) + %d bytes of E(NVM)\n", buffer_len(&symkey_cipher), buffer_len(&state_cipher));
+ goto egress;
+
+ abort_egress:
+ vtpmlogerror(VTPM_LOG_VTPM, "Failed to load NVM\n.");
+
+ egress:
+
+ buffer_free ( &state_cipher);
+ buffer_free ( &symkey_cipher);
+ free(sealed_NVM);
+ Crypto_symcrypto_freekey (&symkey);
+
+ return status;
+}
+
+
+/* inbuf = null outbuf = sealed blob size, sealed blob.*/
+TPM_RESULT VTPM_Handle_Load_NVM(VTPM_DMI_RESOURCE *myDMI,
+ const buffer_t *inbuf,
+ buffer_t *outbuf) {
+
+ TPM_RESULT status = TPM_SUCCESS;
+ symkey_t symkey;
+ buffer_t state_cipher = NULL_BUF,
+ symkey_clear = NULL_BUF,
+ symkey_cipher = NULL_BUF;
+ struct pack_buf_t symkey_cipher32, state_cipher32;
+
+ UINT32 sealed_NVM_size;
+ BYTE *sealed_NVM = NULL;
+ long fh_size;
+ int fh, stat_ret, i;
+ struct stat file_stat;
+ TPM_DIGEST sealedNVMHash;
+
+ memset(&symkey, 0, sizeof(symkey_t));
+
+ if (myDMI->NVMLocation == NULL) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Unable to load NVM because the file name NULL.\n");
+ status = TPM_AUTHFAIL;
+ goto abort_egress;
+ }
+
+ //Read sealed blob off disk from NVMLocation
+ fh = open(myDMI->NVMLocation, O_RDONLY);
+ stat_ret = fstat(fh, &file_stat);
+ if (stat_ret == 0)
+ fh_size = file_stat.st_size;
+ else {
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+
+ sealed_NVM = (BYTE *) malloc(fh_size);
+ if (read(fh, sealed_NVM, fh_size) != fh_size) {
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+ close(fh);
+
+ vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Load_NVMing[%ld]: 0x", fh_size);
+ for (i=0; i< fh_size; i++)
+ vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", sealed_NVM[i]);
+ vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+
+ sealed_NVM_size = BSG_UnpackList(sealed_NVM, 2,
+ BSG_TPM_SIZE32_DATA, &symkey_cipher32,
+ BSG_TPM_SIZE32_DATA, &state_cipher32);
+
+ TPMTRYRETURN( buffer_init_convert (&symkey_cipher,
+ symkey_cipher32.size,
+ symkey_cipher32.data) );
+
+ TPMTRYRETURN( buffer_init_convert (&state_cipher,
+ state_cipher32.size,
+ state_cipher32.data) );
+
+ Crypto_SHA1Full(sealed_NVM, sealed_NVM_size, (BYTE *) &sealedNVMHash);
+
+ // Verify measurement of sealed blob.
+ if (memcmp(&sealedNVMHash, &myDMI->NVM_measurement, sizeof(TPM_DIGEST)) ) {
+ vtpmlogerror(VTPM_LOG_VTPM, "VTPM LoadNVM NVM measurement check failed.\n");
+ vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Correct hash: ");
+ for (i=0; i< sizeof(TPM_DIGEST); i++)
+ vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", ((BYTE*)&myDMI->NVM_measurement)[i]);
+ vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+
+ vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Measured hash: ");
+ for (i=0; i< sizeof(TPM_DIGEST); i++)
+ vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", ((BYTE*)&sealedNVMHash)[i]);
+ vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+
+ status = TPM_AUTHFAIL;
+ goto abort_egress;
+ }
+
+ // Decrypt Symmetric Key
+ TPMTRYRETURN( VTSP_Unbind( myDMI->TCSContext,
+ vtpm_globals->storageKeyHandle,
+ &symkey_cipher,
+ (const TPM_AUTHDATA*)&vtpm_globals->storage_key_usage_auth,
+ &symkey_clear,
+ &(vtpm_globals->keyAuth) ) );
+
+ // create symmetric key using saved bits
+ Crypto_symcrypto_initkey (&symkey, &symkey_clear);
+
+ // Decrypt State
+ TPMTRY(TPM_DECRYPT_ERROR, Crypto_symcrypto_decrypt (&symkey, &state_cipher, outbuf) );
+
+ goto egress;
+
+ abort_egress:
+ vtpmlogerror(VTPM_LOG_VTPM, "Failed to load NVM\n.");
+
+ egress:
+
+ buffer_free ( &state_cipher);
+ buffer_free ( &symkey_clear);
+ buffer_free ( &symkey_cipher);
+ free( sealed_NVM );
+ Crypto_symcrypto_freekey (&symkey);
+
+ return status;
+}
+
+TPM_RESULT VTPM_SaveService(void) {
+ TPM_RESULT status=TPM_SUCCESS;
+ int fh, dmis=-1;
+
+ BYTE *flat_global;
+ int flat_global_size, bytes_written;
+ UINT32 storageKeySize = buffer_len(&vtpm_globals->storageKeyWrap);
+ struct pack_buf_t storage_key_pack = {storageKeySize, vtpm_globals->storageKeyWrap.bytes};
+
+ struct hashtable_itr *dmi_itr;
+ VTPM_DMI_RESOURCE *dmi_res;
+
+ UINT32 flat_global_full_size;
+
+ // Global Values needing to be saved
+ flat_global_full_size = 3*sizeof(TPM_DIGEST) + // Auths
+ sizeof(UINT32) + // storagekeysize
+ storageKeySize + // storage key
+ hashtable_count(vtpm_globals->dmi_map) * // num DMIS
+ (sizeof(UINT32) + 2*sizeof(TPM_DIGEST)); // Per DMI info
+
+
+ flat_global = (BYTE *) malloc( flat_global_full_size);
+
+ flat_global_size = BSG_PackList(flat_global, 4,
+ BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth,
+ BSG_TPM_AUTHDATA, &vtpm_globals->srk_usage_auth,
+ BSG_TPM_SECRET, &vtpm_globals->storage_key_usage_auth,
+ BSG_TPM_SIZE32_DATA, &storage_key_pack);
+
+ // Per DMI values to be saved
+ if (hashtable_count(vtpm_globals->dmi_map) > 0) {
+
+ dmi_itr = hashtable_iterator(vtpm_globals->dmi_map);
+ do {
+ dmi_res = (VTPM_DMI_RESOURCE *) hashtable_iterator_value(dmi_itr);
+ dmis++;
+
+ // No need to save dmi0.
+ if (dmi_res->dmi_id == 0)
+ continue;
+
+
+ flat_global_size += BSG_PackList( flat_global + flat_global_size, 3,
+ BSG_TYPE_UINT32, &dmi_res->dmi_id,
+ BSG_TPM_DIGEST, &dmi_res->NVM_measurement,
+ BSG_TPM_DIGEST, &dmi_res->DMI_measurement);
+
+ } while (hashtable_iterator_advance(dmi_itr));
+ }
+
+ //FIXME: Once we have a way to protect a TPM key, we should use it to
+ // encrypt this blob. BUT, unless there is a way to ensure the key is
+ // not used by other apps, this encryption is useless.
+ fh = open(STATE_FILE, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
+ if (fh == -1) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Unable to open %s file for write.\n", STATE_FILE);
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+
+ if ( (bytes_written = write(fh, flat_global, flat_global_size)) != flat_global_size ) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Failed to save service data. %d/%d bytes written.\n", bytes_written, flat_global_size);
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+ vtpm_globals->DMI_table_dirty = FALSE;
+
+ goto egress;
+
+ abort_egress:
+ egress:
+
+ free(flat_global);
+ close(fh);
+
+ vtpmloginfo(VTPM_LOG_VTPM, "Saved VTPM Service state (status = %d, dmis = %d)\n", (int) status, dmis);
+ return status;
+}
+
+TPM_RESULT VTPM_LoadService(void) {
+
+ TPM_RESULT status=TPM_SUCCESS;
+ int fh, stat_ret, dmis=0;
+ long fh_size = 0, step_size;
+ BYTE *flat_global=NULL;
+ struct pack_buf_t storage_key_pack;
+ UINT32 *dmi_id_key;
+
+ VTPM_DMI_RESOURCE *dmi_res;
+ struct stat file_stat;
+
+ fh = open(STATE_FILE, O_RDONLY );
+ stat_ret = fstat(fh, &file_stat);
+ if (stat_ret == 0)
+ fh_size = file_stat.st_size;
+ else {
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+
+ flat_global = (BYTE *) malloc(fh_size);
+
+ if ((long) read(fh, flat_global, fh_size) != fh_size ) {
+ status = TPM_IOERROR;
+ goto abort_egress;
+ }
+
+ // Global Values needing to be saved
+ step_size = BSG_UnpackList( flat_global, 4,
+ BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth,
+ BSG_TPM_AUTHDATA, &vtpm_globals->srk_usage_auth,
+ BSG_TPM_SECRET, &vtpm_globals->storage_key_usage_auth,
+ BSG_TPM_SIZE32_DATA, &storage_key_pack);
+
+ TPMTRYRETURN(buffer_init(&vtpm_globals->storageKeyWrap, 0, 0) );
+ TPMTRYRETURN(buffer_append_raw(&vtpm_globals->storageKeyWrap, storage_key_pack.size, storage_key_pack.data) );
+
+ // Per DMI values to be saved
+ while ( step_size < fh_size ){
+ if (fh_size - step_size < (long) (sizeof(UINT32) + 2*sizeof(TPM_DIGEST))) {
+ vtpmlogerror(VTPM_LOG_VTPM, "Encountered %ld extra bytes at end of manager state.\n", fh_size-step_size);
+ step_size = fh_size;
+ } else {
+ dmi_res = (VTPM_DMI_RESOURCE *) malloc(sizeof(VTPM_DMI_RESOURCE));
+ dmis++;
+
+ dmi_res->connected = FALSE;
+
+ step_size += BSG_UnpackList(flat_global + step_size, 3,
+ BSG_TYPE_UINT32, &dmi_res->dmi_id,
+ BSG_TPM_DIGEST, &dmi_res->NVM_measurement,
+ BSG_TPM_DIGEST, &dmi_res->DMI_measurement);
+
+ // install into map
+ dmi_id_key = (UINT32 *) malloc (sizeof(UINT32));
+ *dmi_id_key = dmi_res->dmi_id;
+ if (!hashtable_insert(vtpm_globals->dmi_map, dmi_id_key, dmi_res)) {
+ status = TPM_FAIL;
+ goto abort_egress;
+ }
+
+ }
+
+ }
+
+ vtpmloginfo(VTPM_LOG_VTPM, "Loaded saved state (dmis = %d).\n", dmis);
+ goto egress;
+
+ abort_egress:
+ vtpmlogerror(VTPM_LOG_VTPM, "Failed to load service data with error = %s\n", tpm_get_error_name(status));
+ egress:
+
+ if (flat_global)
+ free(flat_global);
+ close(fh);
+
+ return status;
+}
diff --git a/tools/vtpm_manager/manager/vtpm_manager.c b/tools/vtpm_manager/manager/vtpm_manager.c
index a379b8c789..8d71ab0776 100644
--- a/tools/vtpm_manager/manager/vtpm_manager.c
+++ b/tools/vtpm_manager/manager/vtpm_manager.c
@@ -1,735 +1,811 @@
-// ===================================================================
-//
-// Copyright (c) 2005, Intel Corp.
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following
-// disclaimer in the documentation and/or other materials provided
-// with the distribution.
-// * Neither the name of Intel Corporation nor the names of its
-// contributors may be used to endorse or promote products derived
-// from this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
-// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
-// OF THE POSSIBILITY OF SUCH DAMAGE.
-// ===================================================================
-//
-// vtpm_manager.c
-//
-// This file will house the main logic of the VTPM Manager
-//
-// ==================================================================
-
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <string.h>
-
-#ifndef VTPM_MULTI_VM
-#include <pthread.h>
-#include <errno.h>
-#include <aio.h>
-#include <time.h>
-#endif
-
-#include "vtpm_manager.h"
-#include "vtpmpriv.h"
-#include "vtsp.h"
-#include "bsg.h"
-#include "hashtable.h"
-#include "hashtable_itr.h"
-
-#include "log.h"
-#include "buffer.h"
-
-VTPM_GLOBALS *vtpm_globals=NULL;
-
-#ifdef VTPM_MULTI_VM
- #define vtpmhandlerloginfo(module,fmt,args...) vtpmloginfo (module, fmt, ##args );
- #define vtpmhandlerloginfomore(module,fmt,args...) vtpmloginfomore (module, fmt, ##args );
- #define vtpmhandlerlogerror(module,fmt,args...) vtpmlogerror (module, fmt, ##args );
-#else
- #define vtpmhandlerloginfo(module,fmt,args...) vtpmloginfo (module, "[%d]: " fmt, threadType, ##args );
- #define vtpmhandlerloginfomore(module,fmt,args...) vtpmloginfomore (module, fmt, ##args );
- #define vtpmhandlerlogerror(module,fmt,args...) vtpmlogerror (module, "[%d]: " fmt, threadType, ##args );
-#endif
-
-// --------------------------- Static Auths --------------------------
-#ifdef USE_FIXED_SRK_AUTH
-
-static BYTE FIXED_SRK_AUTH[20] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
-
-static BYTE FIXED_EK_AUTH[20] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
-
-#endif
-
-// -------------------------- Hash table functions --------------------
-
-static unsigned int hashfunc32(void *ky) {
- return (* (UINT32 *) ky);
-}
-
-static int equals32(void *k1, void *k2) {
- return (*(UINT32 *) k1 == *(UINT32 *) k2);
-}
-
-// --------------------------- Functions ------------------------------
-
-TPM_RESULT VTPM_Create_Service(){
-
- TPM_RESULT status = TPM_SUCCESS;
-
- // Generate Auth's for SRK & Owner
-#ifdef USE_FIXED_SRK_AUTH
- memcpy(vtpm_globals->owner_usage_auth, FIXED_SRK_AUTH, sizeof(TPM_AUTHDATA));
- memcpy(vtpm_globals->srk_usage_auth, FIXED_EK_AUTH, sizeof(TPM_AUTHDATA));
-#else
- Crypto_GetRandom(vtpm_globals->owner_usage_auth, sizeof(TPM_AUTHDATA) );
- Crypto_GetRandom(vtpm_globals->srk_usage_auth, sizeof(TPM_AUTHDATA) );
-#endif
-
- // Take Owership of TPM
- CRYPTO_INFO ek_cryptoInfo;
-
- vtpmloginfo(VTPM_LOG_VTPM, "Attempting Pubek Read. NOTE: Failure is ok.\n");
- status = VTSP_ReadPubek(vtpm_globals->manager_tcs_handle, &ek_cryptoInfo);
-
- // If we can read PubEK then there is no owner and we should take it.
- if (status == TPM_SUCCESS) {
- TPMTRYRETURN(VTSP_TakeOwnership(vtpm_globals->manager_tcs_handle,
- (const TPM_AUTHDATA*)&vtpm_globals->owner_usage_auth,
- (const TPM_AUTHDATA*)&vtpm_globals->srk_usage_auth,
- &ek_cryptoInfo,
- &vtpm_globals->keyAuth));
-
- TPMTRYRETURN(VTSP_DisablePubekRead(vtpm_globals->manager_tcs_handle,
- (const TPM_AUTHDATA*)&vtpm_globals->owner_usage_auth,
- &vtpm_globals->keyAuth));
- }
-
- // Generate storage key's auth
- Crypto_GetRandom( &vtpm_globals->storage_key_usage_auth,
- sizeof(TPM_AUTHDATA) );
-
- TCS_AUTH osap;
- TPM_AUTHDATA sharedsecret;
-
- TPMTRYRETURN( VTSP_OSAP(vtpm_globals->manager_tcs_handle,
- TPM_ET_SRK,
- 0,
- (const TPM_AUTHDATA*)&vtpm_globals->srk_usage_auth,
- &sharedsecret,
- &osap) );
-
- TPMTRYRETURN( VTSP_CreateWrapKey( vtpm_globals->manager_tcs_handle,
- TPM_KEY_BIND,
- (const TPM_AUTHDATA*)&vtpm_globals->storage_key_usage_auth,
- TPM_SRK_KEYHANDLE,
- (const TPM_AUTHDATA*)&sharedsecret,
- &vtpm_globals->storageKeyWrap,
- &osap) );
-
- vtpm_globals->keyAuth.fContinueAuthSession = TRUE;
-
- goto egress;
-
- abort_egress:
- exit(1);
-
- egress:
- vtpmloginfo(VTPM_LOG_VTPM, "New VTPM Service initialized (Status = %d).\n", status);
- return status;
-
-}
-
-
-//////////////////////////////////////////////////////////////////////////////
-#ifdef VTPM_MULTI_VM
-int VTPM_Service_Handler(){
-#else
-void *VTPM_Service_Handler(void *threadTypePtr){
-#endif
- TPM_RESULT status = TPM_FAIL; // Should never return
- UINT32 dmi, in_param_size, cmd_size, out_param_size, out_message_size, out_message_size_full, dmi_cmd_size;
- BYTE *cmd_header, *in_param, *out_message, *dmi_cmd;
- buffer_t *command_buf=NULL, *result_buf=NULL;
- TPM_TAG tag;
- TPM_COMMAND_CODE ord;
- VTPM_DMI_RESOURCE *dmi_res;
- int size_read, size_write, i;
-
-#ifndef VTPM_MULTI_VM
- int threadType = *(int *) threadTypePtr;
-
- // async io structures
- struct aiocb dmi_aio;
- struct aiocb *dmi_aio_a[1];
- dmi_aio_a[0] = &dmi_aio;
-#endif
-
-#ifdef DUMMY_BACKEND
- int dummy_rx;
-#endif
-
- // TODO: Reinsert ifdefs to enable support for MULTI-VM
-
- cmd_header = (BYTE *) malloc(VTPM_COMMAND_HEADER_SIZE_SRV);
- command_buf = (buffer_t *) malloc(sizeof(buffer_t));
- result_buf = (buffer_t *) malloc(sizeof(buffer_t));
-
-#ifndef VTPM_MULTI_VM
- TPM_RESULT *ret_value = (TPM_RESULT *) malloc(sizeof(TPM_RESULT));
-#endif
-
- int *tx_fh, *rx_fh;
-
-#ifdef VTPM_MULTI_VM
- rx_fh = &vtpm_globals->be_fh;
-#else
- if (threadType == BE_LISTENER_THREAD) {
-#ifdef DUMMY_BACKEND
- dummy_rx = -1;
- rx_fh = &dummy_rx;
-#else
- rx_fh = &vtpm_globals->be_fh;
-#endif
- } else { // DMI_LISTENER_THREAD
- rx_fh = &vtpm_globals->vtpm_rx_fh;
- }
-#endif
-
-#ifndef VTPM_MULTI_VM
- int fh;
- if (threadType == BE_LISTENER_THREAD) {
- tx_fh = &vtpm_globals->be_fh;
- if ( (fh = open(GUEST_RX_FIFO, O_RDWR)) == -1) {
- if ( mkfifo(GUEST_RX_FIFO, S_IWUSR | S_IRUSR ) ){
- *ret_value = TPM_FAIL;
- pthread_exit(ret_value);
- }
- } else
- close(fh);
-
- } else { // else DMI_LISTENER_THREAD
- // tx_fh will be set once the DMI is identified
- // But we need to make sure the read pip is created.
- if ( (fh = open(VTPM_RX_FIFO, O_RDWR)) == -1) {
- if ( mkfifo(VTPM_RX_FIFO, S_IWUSR | S_IRUSR ) ){
- *ret_value = TPM_FAIL;
- pthread_exit(ret_value);
- }
- } else
- close(fh);
-
- }
-#endif
-
- while(1) {
-
- if (threadType == BE_LISTENER_THREAD) {
- vtpmhandlerloginfo(VTPM_LOG_VTPM, "Waiting for Guest requests & ctrl messages.\n");
- } else
- vtpmhandlerloginfo(VTPM_LOG_VTPM, "Waiting for DMI messages.\n");
-
-
- if (*rx_fh < 0) {
- if (threadType == BE_LISTENER_THREAD)
-#ifdef DUMMY_BACKEND
- *rx_fh = open("/tmp/in.fifo", O_RDWR);
-#else
- *rx_fh = open(VTPM_BE_DEV, O_RDWR);
-#endif
- else // DMI Listener
- *rx_fh = open(VTPM_RX_FIFO, O_RDWR);
-
- }
-
- if (*rx_fh < 0) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Can't open inbound fh.\n");
-#ifdef VTPM_MULTI_VM
- return TPM_IOERROR;
-#else
- *ret_value = TPM_IOERROR;
- pthread_exit(ret_value);
-#endif
- }
-
- size_read = read(*rx_fh, cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV);
- if (size_read > 0) {
- vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "RECV[%d}: 0x", size_read);
- for (i=0; i<size_read; i++)
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", cmd_header[i]);
- } else {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Can't read from BE. Aborting... \n");
- close(*rx_fh);
- *rx_fh = -1;
- goto abort_command;
- }
-
- if (size_read < (int) VTPM_COMMAND_HEADER_SIZE_SRV) {
- vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "\n");
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Command shorter than normal header (%d bytes). Aborting...\n", size_read);
- goto abort_command;
- }
-
- BSG_UnpackList(cmd_header, 4,
- BSG_TYPE_UINT32, &dmi,
- BSG_TPM_TAG, &tag,
- BSG_TYPE_UINT32, &in_param_size,
- BSG_TPM_COMMAND_CODE, &ord );
-
- // Note that in_param_size is in the client's context
- cmd_size = in_param_size - VTPM_COMMAND_HEADER_SIZE_CLT;
- if (cmd_size > 0) {
- in_param = (BYTE *) malloc(cmd_size);
- size_read = read( *rx_fh, in_param, cmd_size);
- if (size_read > 0) {
- for (i=0; i<size_read; i++)
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", in_param[i]);
-
- } else {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error reading from BE. Aborting... \n");
- close(*rx_fh);
- *rx_fh = -1;
- goto abort_command;
- }
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
-
- if (size_read < (int) cmd_size) {
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Command read(%d) is shorter than header indicates(%d). Aborting...\n", size_read, cmd_size);
- goto abort_command;
- }
- } else {
- in_param = NULL;
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
- }
-
- if ((threadType != BE_LISTENER_THREAD) && (dmi == 0)) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempt to access dom0 commands from DMI interface. Aborting...\n");
- goto abort_command;
- }
-
- dmi_res = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi);
- if (dmi_res == NULL) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempted access to non-existent DMI in domain: %d. Aborting...\n", dmi);
- goto abort_command;
- }
- if (!dmi_res->connected) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempted access to disconnected DMI in domain: %d. Aborting...\n", dmi);
- goto abort_command;
- }
-
- if (threadType != BE_LISTENER_THREAD)
- tx_fh = &dmi_res->vtpm_tx_fh;
- // else we set this before the while loop since it doesn't change.
-
- if ( (buffer_init_convert(command_buf, cmd_size, in_param) != TPM_SUCCESS) ||
- (buffer_init(result_buf, 0, 0) != TPM_SUCCESS) ) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Failed to setup buffers. Aborting...\n");
- goto abort_command;
- }
-
- // Dispatch it as either control or user request.
- if (tag == VTPM_TAG_REQ) {
- if (dmi_res->dmi_id == VTPM_CTL_DM){
- switch (ord) {
- case VTPM_ORD_OPEN:
- status = VTPM_Handle_New_DMI(command_buf);
- break;
-
- case VTPM_ORD_CLOSE:
- status = VTPM_Handle_Close_DMI(command_buf);
- break;
-
- case VTPM_ORD_DELETE:
- status = VTPM_Handle_Delete_DMI(command_buf);
- break;
- default:
- status = TPM_BAD_ORDINAL;
- } // switch
- } else {
-
- switch (ord) {
- case VTPM_ORD_SAVENVM:
- status= VTPM_Handle_Save_NVM(dmi_res,
- command_buf,
- result_buf);
- break;
- case VTPM_ORD_LOADNVM:
- status= VTPM_Handle_Load_NVM(dmi_res,
- command_buf,
- result_buf);
- break;
-
- case VTPM_ORD_TPMCOMMAND:
- status= VTPM_Handle_TPM_Command(dmi_res,
- command_buf,
- result_buf);
- break;
-
- default:
- status = TPM_BAD_ORDINAL;
- } // switch
- }
- } else { // This is not a VTPM Command at all
-
- if (threadType == BE_LISTENER_THREAD) {
- if (dmi == 0) {
- // This usually indicates a FE/BE driver.
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Illegal use of TPM command from dom0\n");
- status = TPM_FAIL;
- } else {
- vtpmhandlerloginfo(VTPM_LOG_VTPM, "Forwarding command to DMI.\n");
-
- if (dmi_res->guest_tx_fh < 0)
- dmi_res->guest_tx_fh = open(dmi_res->guest_tx_fname, O_WRONLY | O_NONBLOCK);
-
- if (dmi_res->guest_tx_fh < 0){
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "VTPM ERROR: Can't open outbound fh to dmi.\n");
- status = TPM_IOERROR;
- goto abort_with_error;
- }
-
- //Note: Send message + dmi_id
- if (cmd_size) {
- dmi_cmd = (BYTE *) malloc(VTPM_COMMAND_HEADER_SIZE_SRV + cmd_size);
- dmi_cmd_size = VTPM_COMMAND_HEADER_SIZE_SRV + cmd_size;
- memcpy(dmi_cmd, cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV);
- memcpy(dmi_cmd + VTPM_COMMAND_HEADER_SIZE_SRV, in_param, cmd_size);
- size_write = write(dmi_res->guest_tx_fh, dmi_cmd, dmi_cmd_size);
-
- if (size_write > 0) {
- vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "SENT (DMI): 0x");
- for (i=0; i<VTPM_COMMAND_HEADER_SIZE_SRV + cmd_size; i++) {
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", dmi_cmd[i]);
- }
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
- } else {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error writing to DMI. Aborting... \n");
- close(dmi_res->guest_tx_fh);
- dmi_res->guest_tx_fh = -1;
- status = TPM_IOERROR;
- goto abort_with_error;
- }
- free(dmi_cmd);
- } else {
- dmi_cmd_size = VTPM_COMMAND_HEADER_SIZE_SRV;
- size_write = write(dmi_res->guest_tx_fh, cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV );
- if (size_write > 0) {
- for (i=0; i<VTPM_COMMAND_HEADER_SIZE_SRV; i++)
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", cmd_header[i]);
-
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
- } else {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error writing to DMI. Aborting... \n");
- close(dmi_res->guest_tx_fh);
- dmi_res->guest_tx_fh = -1;
- status = TPM_IOERROR;
- goto abort_with_error;
- }
- }
-
- if (size_write != (int) dmi_cmd_size)
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Could not write entire command to DMI (%d/%d)\n", size_write, dmi_cmd_size);
- buffer_free(command_buf);
-
- if (vtpm_globals->guest_rx_fh < 0)
- vtpm_globals->guest_rx_fh = open(GUEST_RX_FIFO, O_RDONLY);
-
- if (vtpm_globals->guest_rx_fh < 0){
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Can't open inbound fh to dmi.\n");
- status = TPM_IOERROR;
- goto abort_with_error;
- }
-
- size_read = read( vtpm_globals->guest_rx_fh, cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV);
- if (size_read > 0) {
- vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "RECV (DMI): 0x");
- for (i=0; i<size_read; i++)
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", cmd_header[i]);
-
- } else {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error reading from DMI. Aborting... \n");
- close(vtpm_globals->guest_rx_fh);
- vtpm_globals->guest_rx_fh = -1;
- status = TPM_IOERROR;
- goto abort_with_error;
- }
-
- if (size_read < (int) VTPM_COMMAND_HEADER_SIZE_SRV) {
- //vtpmdeepsublog("\n");
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Command from DMI shorter than normal header. Aborting...\n");
- status = TPM_IOERROR;
- goto abort_with_error;
- }
-
- BSG_UnpackList(cmd_header, 4,
- BSG_TYPE_UINT32, &dmi,
- BSG_TPM_TAG, &tag,
- BSG_TYPE_UINT32, &in_param_size,
- BSG_TPM_COMMAND_CODE, &status );
-
- // Note that in_param_size is in the client's context
- cmd_size = in_param_size - VTPM_COMMAND_HEADER_SIZE_CLT;
- if (cmd_size > 0) {
- in_param = (BYTE *) malloc(cmd_size);
- size_read = read( vtpm_globals->guest_rx_fh, in_param, cmd_size);
- if (size_read > 0) {
- for (i=0; i<size_read; i++)
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", in_param[i]);
-
- } else {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error reading from BE. Aborting... \n");
- close(vtpm_globals->guest_rx_fh);
- vtpm_globals->guest_rx_fh = -1;
- status = TPM_IOERROR;
- goto abort_with_error;
- }
- vtpmhandlerloginfomore(VTPM_LOG_VTPM, "\n");
-
- if (size_read < (int)cmd_size) {
- vtpmhandlerloginfomore(VTPM_LOG_VTPM, "\n");
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Command read(%d) from DMI is shorter than header indicates(%d). Aborting...\n", size_read, cmd_size);
- status = TPM_IOERROR;
- goto abort_with_error;
- }
- } else {
- in_param = NULL;
- vtpmhandlerloginfomore(VTPM_LOG_VTPM, "\n");
- }
-
- if (buffer_init_convert(result_buf, cmd_size, in_param) != TPM_SUCCESS) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Failed to setup buffers. Aborting...\n");
- status = TPM_FAIL;
- goto abort_with_error;
- }
-
- vtpmhandlerloginfo(VTPM_LOG_VTPM, "Sending DMI's response to guest.\n");
- } // end else for if (dmi==0)
-
- } else { // This is a DMI lister thread. Thus this is from a DMI
-#ifdef VTPM_MULTI_VM
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempt to use unsupported direct access to TPM.\n");
- vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "Bad Command. dmi:%d, tag:%d, size:%d, ord:%d, Params: ", dmi, tag, in_param_size, ord);
- for (UINT32 q=0; q<cmd_size; q++)
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", in_param[q]);
-
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
-
- status = TPM_FAIL;
-#else
-
-#endif
- } // end else for if BE Listener
- } // end else for is VTPM Command
-
- // Send response to Backend
- if (*tx_fh < 0) {
- if (threadType == BE_LISTENER_THREAD)
-#ifdef DUMMY_BACKEND
- *tx_fh = open("/tmp/out.fifo", O_RDWR);
-#else
- *tx_fh = open(VTPM_BE_DEV, O_RDWR);
-#endif
- else // DMI Listener
- *tx_fh = open(dmi_res->vtpm_tx_fname, O_WRONLY);
- }
-
- if (*tx_fh < 0) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "VTPM ERROR: Can't open outbound fh.\n");
-#ifdef VTPM_MULTI_VM
- return TPM_IOERROR;
-#else
- *ret_value = TPM_IOERROR;
- pthread_exit(ret_value);
-#endif
- }
-
- abort_with_error:
- // Prepend VTPM header with destination DM stamped
- out_param_size = buffer_len(result_buf);
- out_message_size = VTPM_COMMAND_HEADER_SIZE_CLT + out_param_size;
- out_message_size_full = VTPM_COMMAND_HEADER_SIZE_SRV + out_param_size;
- out_message = (BYTE *) malloc (out_message_size_full);
-
- BSG_PackList(out_message, 4,
- BSG_TYPE_UINT32, (BYTE *) &dmi,
- BSG_TPM_TAG, (BYTE *) &tag,
- BSG_TYPE_UINT32, (BYTE *) &out_message_size,
- BSG_TPM_RESULT, (BYTE *) &status);
-
- if (buffer_len(result_buf) > 0)
- memcpy(out_message + VTPM_COMMAND_HEADER_SIZE_SRV, result_buf->bytes, out_param_size);
-
-
- //Note: Send message + dmi_id
- size_write = write(*tx_fh, out_message, out_message_size_full );
- if (size_write > 0) {
- vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "SENT: 0x");
- for (i=0; i < out_message_size_full; i++)
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", out_message[i]);
-
- vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
- } else {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error writing to BE. Aborting... \n");
- close(*tx_fh);
- *tx_fh = -1;
- goto abort_command;
- }
- free(out_message);
-
- if (size_write < (int)out_message_size_full) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "Unable to write full command to BE (%d/%d)\n", size_write, out_message_size_full);
- goto abort_command;
- }
-
- abort_command:
- //free buffers
- bzero(cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV);
- //free(in_param); // This was converted to command_buf. No need to free
- if (command_buf != result_buf)
- buffer_free(result_buf);
-
- buffer_free(command_buf);
-
-#ifndef VTPM_MULTI_VM
- if (threadType != BE_LISTENER_THREAD) {
-#endif
- if ( (vtpm_globals->DMI_table_dirty) &&
- (VTPM_SaveService() != TPM_SUCCESS) ) {
- vtpmhandlerlogerror(VTPM_LOG_VTPM, "ERROR: Unable to save manager data.\n");
- }
-#ifndef VTPM_MULTI_VM
- }
-#endif
-
- } // End while(1)
-
-}
-
-
-///////////////////////////////////////////////////////////////////////////////
-TPM_RESULT VTPM_Init_Service() {
- TPM_RESULT status = TPM_FAIL;
- BYTE *randomsead;
- UINT32 randomsize;
-
- if ((vtpm_globals = (VTPM_GLOBALS *) malloc(sizeof(VTPM_GLOBALS))) == NULL){
- status = TPM_FAIL;
- goto abort_egress;
- }
- memset(vtpm_globals, 0, sizeof(VTPM_GLOBALS));
- vtpm_globals->be_fh = -1;
-
-#ifndef VTPM_MULTI_VM
- vtpm_globals->vtpm_rx_fh = -1;
- vtpm_globals->guest_rx_fh = -1;
-#endif
- if ((vtpm_globals->dmi_map = create_hashtable(10, hashfunc32, equals32)) == NULL){
- status = TPM_FAIL;
- goto abort_egress;
- }
-
- vtpm_globals->DMI_table_dirty = FALSE;
-
- // Create new TCS Object
- vtpm_globals->manager_tcs_handle = 0;
-
- TPMTRYRETURN(TCS_create());
-
- // Create TCS Context for service
- TPMTRYRETURN( TCS_OpenContext(&vtpm_globals->manager_tcs_handle ) );
-
- TPMTRYRETURN( TCSP_GetRandom(vtpm_globals->manager_tcs_handle,
- &randomsize,
- &randomsead));
-
- Crypto_Init(randomsead, randomsize);
- TPMTRYRETURN( TCS_FreeMemory (vtpm_globals->manager_tcs_handle, randomsead));
-
- // Create OIAP session for service's authorized commands
- TPMTRYRETURN( VTSP_OIAP( vtpm_globals->manager_tcs_handle,
- &vtpm_globals->keyAuth) );
- vtpm_globals->keyAuth.fContinueAuthSession = TRUE;
-
- // If failed, create new Service.
- if (VTPM_LoadService() != TPM_SUCCESS)
- TPMTRYRETURN( VTPM_Create_Service() );
-
-
- //Load Storage Key
- TPMTRYRETURN( VTSP_LoadKey( vtpm_globals->manager_tcs_handle,
- TPM_SRK_KEYHANDLE,
- &vtpm_globals->storageKeyWrap,
- (const TPM_AUTHDATA*)&vtpm_globals->srk_usage_auth,
- &vtpm_globals->storageKeyHandle,
- &vtpm_globals->keyAuth,
- &vtpm_globals->storageKey) );
-
- // Create entry for Dom0 for control messages
- TPMTRYRETURN( VTPM_Handle_New_DMI(NULL) );
-
- // --------------------- Command handlers ---------------------------
-
- goto egress;
-
- abort_egress:
- egress:
-
- return(status);
-}
-
-void VTPM_Stop_Service() {
- VTPM_DMI_RESOURCE *dmi_res;
- struct hashtable_itr *dmi_itr;
-
- // Close all the TCS contexts. TCS should evict keys based on this
- if (hashtable_count(vtpm_globals->dmi_map) > 0) {
- dmi_itr = hashtable_iterator(vtpm_globals->dmi_map);
- do {
- dmi_res = (VTPM_DMI_RESOURCE *) hashtable_iterator_value(dmi_itr);
- if (dmi_res->connected)
- if (close_dmi( dmi_res ) != TPM_SUCCESS)
- vtpmlogerror(VTPM_LOG_VTPM, "Failed to close dmi %d properly.\n", dmi_res->dmi_id);
-
- } while (hashtable_iterator_advance(dmi_itr));
- free (dmi_itr);
- }
-
-
- TCS_CloseContext(vtpm_globals->manager_tcs_handle);
-
- if ( (vtpm_globals->DMI_table_dirty) &&
- (VTPM_SaveService() != TPM_SUCCESS) )
- vtpmlogerror(VTPM_LOG_VTPM, "Unable to save manager data.\n");
-
- hashtable_destroy(vtpm_globals->dmi_map, 1);
- free(vtpm_globals);
-
- close(vtpm_globals->be_fh);
- Crypto_Exit();
-
- vtpmloginfo(VTPM_LOG_VTPM, "VTPM Manager stopped.\n");
-}
+// ===================================================================
+//
+// Copyright (c) 2005, Intel Corp.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Intel Corporation nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+// OF THE POSSIBILITY OF SUCH DAMAGE.
+// ===================================================================
+//
+// vtpm_manager.c
+//
+// This file will house the main logic of the VTPM Manager
+//
+// ==================================================================
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+
+#ifndef VTPM_MULTI_VM
+#include <pthread.h>
+#include <errno.h>
+#include <aio.h>
+#include <time.h>
+#endif
+
+#include "vtpm_manager.h"
+#include "vtpmpriv.h"
+#include "vtsp.h"
+#include "bsg.h"
+#include "hashtable.h"
+#include "hashtable_itr.h"
+
+#include "log.h"
+#include "buffer.h"
+
+VTPM_GLOBALS *vtpm_globals=NULL;
+
+#ifdef VTPM_MULTI_VM
+ #define vtpmhandlerloginfo(module,fmt,args...) vtpmloginfo (module, fmt, ##args );
+ #define vtpmhandlerloginfomore(module,fmt,args...) vtpmloginfomore (module, fmt, ##args );
+ #define vtpmhandlerlogerror(module,fmt,args...) vtpmlogerror (module, fmt, ##args );
+#else
+ #define vtpmhandlerloginfo(module,fmt,args...) vtpmloginfo (module, "[%d]: " fmt, threadType, ##args );
+ #define vtpmhandlerloginfomore(module,fmt,args...) vtpmloginfomore (module, fmt, ##args );
+ #define vtpmhandlerlogerror(module,fmt,args...) vtpmlogerror (module, "[%d]: " fmt, threadType, ##args );
+#endif
+
+// --------------------------- Well Known Auths --------------------------
+#ifdef WELL_KNOWN_SRK_AUTH
+static BYTE FIXED_SRK_AUTH[20] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
+#endif
+
+#ifdef WELL_KNOWN_OWNER_AUTH
+static BYTE FIXED_OWNER_AUTH[20] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
+#endif
+
+// -------------------------- Hash table functions --------------------
+
+static unsigned int hashfunc32(void *ky) {
+ return (* (UINT32 *) ky);
+}
+
+static int equals32(void *k1, void *k2) {
+ return (*(UINT32 *) k1 == *(UINT32 *) k2);
+}
+
+// --------------------------- Functions ------------------------------
+
+TPM_RESULT VTPM_Create_Service(){
+
+ TPM_RESULT status = TPM_SUCCESS;
+
+ // Generate Auth's for SRK & Owner
+#ifdef WELL_KNOWN_SRK_AUTH
+ memcpy(vtpm_globals->srk_usage_auth, FIXED_SRK_AUTH, sizeof(TPM_AUTHDATA));
+#else
+ Crypto_GetRandom(vtpm_globals->srk_usage_auth, sizeof(TPM_AUTHDATA) );
+#endif
+
+#ifdef WELL_KNOWN_OWNER_AUTH
+ memcpy(vtpm_globals->owner_usage_auth, FIXED_OWNER_AUTH, sizeof(TPM_AUTHDATA));
+#else
+ Crypto_GetRandom(vtpm_globals->owner_usage_auth, sizeof(TPM_AUTHDATA) );
+#endif
+
+ // Take Owership of TPM
+ CRYPTO_INFO ek_cryptoInfo;
+
+ vtpmloginfo(VTPM_LOG_VTPM, "Attempting Pubek Read. NOTE: Failure is ok.\n");
+ status = VTSP_ReadPubek(vtpm_globals->manager_tcs_handle, &ek_cryptoInfo);
+
+ // If we can read PubEK then there is no owner and we should take it.
+ if (status == TPM_SUCCESS) {
+ TPMTRYRETURN(VTSP_TakeOwnership(vtpm_globals->manager_tcs_handle,
+ (const TPM_AUTHDATA*)&vtpm_globals->owner_usage_auth,
+ (const TPM_AUTHDATA*)&vtpm_globals->srk_usage_auth,
+ &ek_cryptoInfo,
+ &vtpm_globals->keyAuth));
+
+ TPMTRYRETURN(VTSP_DisablePubekRead(vtpm_globals->manager_tcs_handle,
+ (const TPM_AUTHDATA*)&vtpm_globals->owner_usage_auth,
+ &vtpm_globals->keyAuth));
+ }
+
+ // Generate storage key's auth
+ Crypto_GetRandom( &vtpm_globals->storage_key_usage_auth,
+ sizeof(TPM_AUTHDATA) );
+
+ TCS_AUTH osap;
+ TPM_AUTHDATA sharedsecret;
+
+ TPMTRYRETURN( VTSP_OSAP(vtpm_globals->manager_tcs_handle,
+ TPM_ET_SRK,
+ 0,
+ (const TPM_AUTHDATA*)&vtpm_globals->srk_usage_auth,
+ &sharedsecret,
+ &osap) );
+
+ TPMTRYRETURN( VTSP_CreateWrapKey( vtpm_globals->manager_tcs_handle,
+ TPM_KEY_BIND,
+ (const TPM_AUTHDATA*)&vtpm_globals->storage_key_usage_auth,
+ TPM_SRK_KEYHANDLE,
+ (const TPM_AUTHDATA*)&sharedsecret,
+ &vtpm_globals->storageKeyWrap,
+ &osap) );
+
+ vtpm_globals->keyAuth.fContinueAuthSession = TRUE;
+
+ goto egress;
+
+ abort_egress:
+ exit(1);
+
+ egress:
+ vtpmloginfo(VTPM_LOG_VTPM, "Finished initialized new VTPM service (Status = %d).\n", status);
+ return status;
+
+}
+
+
+//////////////////////////////////////////////////////////////////////////////
+#ifdef VTPM_MULTI_VM
+int VTPM_Service_Handler(){
+#else
+void *VTPM_Service_Handler(void *threadTypePtr){
+#endif
+ TPM_RESULT status = TPM_FAIL; // Should never return
+ UINT32 dmi, in_param_size, cmd_size, out_param_size, out_message_size, out_message_size_full;
+ BYTE *cmd_header, *in_param, *out_message;
+ buffer_t *command_buf=NULL, *result_buf=NULL;
+ TPM_TAG tag;
+ TPM_COMMAND_CODE ord;
+ VTPM_DMI_RESOURCE *dmi_res;
+ int size_read, size_write, i;
+
+#ifndef VTPM_MULTI_VM
+ UINT32 dmi_cmd_size;
+ BYTE *dmi_cmd;
+ int threadType = *(int *) threadTypePtr;
+
+ // async io structures
+ struct aiocb dmi_aio;
+ struct aiocb *dmi_aio_a[1];
+ dmi_aio_a[0] = &dmi_aio;
+#endif
+
+#ifdef DUMMY_BACKEND
+ int dummy_rx;
+#endif
+
+ cmd_header = (BYTE *) malloc(VTPM_COMMAND_HEADER_SIZE_SRV);
+ command_buf = (buffer_t *) malloc(sizeof(buffer_t));
+ result_buf = (buffer_t *) malloc(sizeof(buffer_t));
+
+#ifndef VTPM_MULTI_VM
+ TPM_RESULT *ret_value = (TPM_RESULT *) malloc(sizeof(TPM_RESULT));
+#endif
+
+ int *tx_fh, // Pointer to the filehandle this function will write to
+ *rx_fh; // Pointer to the filehandle this function will read from
+ // For a multi VM VTPM system, this function tx/rx with the BE
+ // via vtpm_globals->be_fh.
+ // For a single VM system, the BE_LISTENER_THREAD tx/rx with theBE
+ // via vtpm_globals->be_fh, and the DMI_LISTENER_THREAD rx from
+ // vtpm_globals->vtpm_rx_fh and tx to dmi_res->vtpm_tx_fh
+
+ // Set rx_fh to point to the correct fh based on this mode.
+#ifdef VTPM_MULTI_VM
+ rx_fh = &vtpm_globals->be_fh;
+#else
+ if (threadType == BE_LISTENER_THREAD) {
+ #ifdef DUMMY_BACKEND
+ dummy_rx = -1;
+ rx_fh = &dummy_rx;
+ #else
+ rx_fh = &vtpm_globals->be_fh;
+ #endif
+ } else { // DMI_LISTENER_THREAD
+ rx_fh = &vtpm_globals->vtpm_rx_fh;
+ }
+#endif
+
+ // Set tx_fh to point to the correct fh based on this mode (If static)
+ // Create any fifos that these fh will use.
+#ifndef VTPM_MULTI_VM
+ int fh;
+ if (threadType == BE_LISTENER_THREAD) {
+ tx_fh = &vtpm_globals->be_fh;
+ if ( (fh = open(GUEST_RX_FIFO, O_RDWR)) == -1) {
+ if ( mkfifo(GUEST_RX_FIFO, S_IWUSR | S_IRUSR ) ){
+ vtpmlogerror(VTPM_LOG_VTPM, "Unable to create FIFO: %s.\n", GUEST_RX_FIFO);
+ *ret_value = TPM_FAIL;
+ pthread_exit(ret_value);
+ }
+ } else
+ close(fh);
+
+ } else { // else DMI_LISTENER_THREAD
+ // tx_fh will be set once the DMI is identified
+ // But we need to make sure the read pip is created.
+ if ( (fh = open(VTPM_RX_FIFO, O_RDWR)) == -1) {
+ if ( mkfifo(VTPM_RX_FIFO, S_IWUSR | S_IRUSR ) ){
+ vtpmlogerror(VTPM_LOG_VTPM, "Unable to create FIFO: %s.\n", VTPM_RX_FIFO);
+ *ret_value = TPM_FAIL;
+ pthread_exit(ret_value);
+ }
+ } else
+ close(fh);
+
+ }
+#else
+ tx_fh = &vtpm_globals->be_fh;
+#endif
+
+ ////////////////////////// Main Loop //////////////////////////////////
+ while(1) {
+
+#ifdef VTPM_MULTI_VM
+ vtpmhandlerloginfo(VTPM_LOG_VTPM, "Waiting for DMI messages.\n");
+#else
+ if (threadType == BE_LISTENER_THREAD) {
+ vtpmhandlerloginfo(VTPM_LOG_VTPM, "Waiting for Guest requests & ctrl messages.\n");
+ } else
+ vtpmhandlerloginfo(VTPM_LOG_VTPM, "Waiting for DMI messages.\n");
+#endif
+
+ // Check status of rx_fh. If necessary attempt to re-open it.
+ if (*rx_fh < 0) {
+#ifdef VTPM_MULTI_VM
+ *rx_fh = open(VTPM_BE_DEV, O_RDWR);
+#else
+ if (threadType == BE_LISTENER_THREAD)
+ #ifdef DUMMY_BACKEND
+ *rx_fh = open("/tmp/in.fifo", O_RDWR);
+ #else
+ *rx_fh = open(VTPM_BE_DEV, O_RDWR);
+ #endif
+ else // DMI Listener
+ *rx_fh = open(VTPM_RX_FIFO, O_RDWR);
+#endif
+ }
+
+ // Respond to failures to open rx_fh
+ if (*rx_fh < 0) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Can't open inbound fh.\n");
+#ifdef VTPM_MULTI_VM
+ return TPM_IOERROR;
+#else
+ *ret_value = TPM_IOERROR;
+ pthread_exit(ret_value);
+#endif
+ }
+
+ // Read command header from rx_fh
+ size_read = read(*rx_fh, cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV);
+ if (size_read > 0) {
+ vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "RECV[%d}: 0x", size_read);
+ for (i=0; i<size_read; i++)
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", cmd_header[i]);
+ } else {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Can't read from BE. Aborting... \n");
+ close(*rx_fh);
+ *rx_fh = -1;
+ goto abort_command;
+ }
+
+ if (size_read < (int) VTPM_COMMAND_HEADER_SIZE_SRV) {
+ vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "\n");
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Command shorter than normal header (%d bytes). Aborting...\n", size_read);
+ goto abort_command;
+ }
+
+ // Unpack header
+ BSG_UnpackList(cmd_header, 4,
+ BSG_TYPE_UINT32, &dmi,
+ BSG_TPM_TAG, &tag,
+ BSG_TYPE_UINT32, &in_param_size,
+ BSG_TPM_COMMAND_CODE, &ord );
+
+ // Using the header info, read from rx_fh the parameters of the command
+ // Note that in_param_size is in the client's context
+ cmd_size = in_param_size - VTPM_COMMAND_HEADER_SIZE_CLT;
+ if (cmd_size > 0) {
+ in_param = (BYTE *) malloc(cmd_size);
+ size_read = read( *rx_fh, in_param, cmd_size);
+ if (size_read > 0) {
+ for (i=0; i<size_read; i++)
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", in_param[i]);
+
+ } else {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error reading from cmd. Aborting... \n");
+ close(*rx_fh);
+ *rx_fh = -1;
+ goto abort_command;
+ }
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+
+ if (size_read < (int) cmd_size) {
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Command read(%d) is shorter than header indicates(%d). Aborting...\n", size_read, cmd_size);
+ goto abort_command;
+ }
+ } else {
+ in_param = NULL;
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+ }
+
+#ifndef VTPM_MULTI_VM
+ // It's illegal to receive a Dom0 command from a DMI.
+ if ((threadType != BE_LISTENER_THREAD) && (dmi == 0)) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempt to access dom0 commands from DMI interface. Aborting...\n");
+ goto abort_command;
+ }
+#endif
+
+ // Fetch infomation about the DMI issuing the request.
+ dmi_res = (VTPM_DMI_RESOURCE *) hashtable_search(vtpm_globals->dmi_map, &dmi);
+ if (dmi_res == NULL) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempted access to non-existent DMI in domain: %d. Aborting...\n", dmi);
+ goto abort_command;
+ }
+ if (!dmi_res->connected) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempted access to disconnected DMI in domain: %d. Aborting...\n", dmi);
+ goto abort_command;
+ }
+
+#ifndef VTPM_MULTI_VM
+ // Now that we know which DMI this is, we can set the tx_fh handle.
+ if (threadType != BE_LISTENER_THREAD)
+ tx_fh = &dmi_res->vtpm_tx_fh;
+ // else we set this before the while loop since it doesn't change.
+#endif
+
+ // Init the buffers used to handle the command and the response
+ if ( (buffer_init_convert(command_buf, cmd_size, in_param) != TPM_SUCCESS) ||
+ (buffer_init(result_buf, 0, 0) != TPM_SUCCESS) ) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Failed to setup buffers. Aborting...\n");
+ goto abort_command;
+ }
+
+ // Dispatch it as either control or user request.
+ if (tag == VTPM_TAG_REQ) {
+ if (dmi_res->dmi_id == VTPM_CTL_DM){
+ switch (ord) {
+ case VTPM_ORD_OPEN:
+ status = VTPM_Handle_New_DMI(command_buf);
+ break;
+
+ case VTPM_ORD_CLOSE:
+ status = VTPM_Handle_Close_DMI(command_buf);
+ break;
+
+ case VTPM_ORD_DELETE:
+ status = VTPM_Handle_Delete_DMI(command_buf);
+ break;
+ default:
+ status = TPM_BAD_ORDINAL;
+ } // switch
+ } else {
+
+ switch (ord) {
+ case VTPM_ORD_SAVENVM:
+ status= VTPM_Handle_Save_NVM(dmi_res,
+ command_buf,
+ result_buf);
+ break;
+ case VTPM_ORD_LOADNVM:
+ status= VTPM_Handle_Load_NVM(dmi_res,
+ command_buf,
+ result_buf);
+ break;
+
+ case VTPM_ORD_TPMCOMMAND:
+ status= VTPM_Handle_TPM_Command(dmi_res,
+ command_buf,
+ result_buf);
+ break;
+
+ default:
+ status = TPM_BAD_ORDINAL;
+ } // switch
+ }
+ } else { // This is not a VTPM Command at all.
+ // This happens in two cases.
+ // MULTI_VM = A DMI illegally sent a raw TPM command to the manager
+ // Single VM:
+ // BE_LISTENER_THREAD: Guest issued a TPM command.
+ // Send this to DMI and wait for response
+ // DMI_LISTENER_THREAD: A DMI illegally sent a raw TPM command.
+
+#ifdef VTPM_MULTI_VM
+ // Raw TPM commands are not supported from the DMI
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempt to use unsupported direct access to TPM.\n");
+ vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "Bad Command. dmi:%d, tag:%d, size:%d, ord:%d, Params: ", dmi, tag, in_param_size, ord);
+ for (i=0; i<cmd_size; i++)
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", in_param[i]);
+
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+ status = TPM_FAIL;
+
+#else
+ // If BE_LISTENER_THREAD then this is a TPM command from a guest
+ if (threadType == BE_LISTENER_THREAD) {
+ // Dom0 can't talk to the BE, so this must be a broken FE/BE or badness
+ if (dmi == 0) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Illegal use of TPM command from dom0\n");
+ status = TPM_FAIL;
+ } else {
+ vtpmhandlerloginfo(VTPM_LOG_VTPM, "Forwarding command to DMI.\n");
+
+ // open the dmi_res->guest_tx_fh to send command to DMI
+ if (dmi_res->guest_tx_fh < 0)
+ dmi_res->guest_tx_fh = open(dmi_res->guest_tx_fname, O_WRONLY | O_NONBLOCK);
+
+ // handle failed opens dmi_res->guest_tx_fh
+ if (dmi_res->guest_tx_fh < 0){
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "VTPM ERROR: Can't open outbound fh to dmi.\n");
+ status = TPM_IOERROR;
+ goto abort_with_error;
+ }
+
+ //Forward TPM CMD stamped with dmi_id to DMI for handling
+ if (cmd_size) {
+ dmi_cmd = (BYTE *) malloc(VTPM_COMMAND_HEADER_SIZE_SRV + cmd_size);
+ dmi_cmd_size = VTPM_COMMAND_HEADER_SIZE_SRV + cmd_size;
+ memcpy(dmi_cmd, cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV);
+ memcpy(dmi_cmd + VTPM_COMMAND_HEADER_SIZE_SRV, in_param, cmd_size);
+ size_write = write(dmi_res->guest_tx_fh, dmi_cmd, dmi_cmd_size);
+
+ if (size_write > 0) {
+ vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "SENT (DMI): 0x");
+ for (i=0; i<VTPM_COMMAND_HEADER_SIZE_SRV + cmd_size; i++) {
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", dmi_cmd[i]);
+ }
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+ } else {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error writing to DMI. Aborting... \n");
+ close(dmi_res->guest_tx_fh);
+ dmi_res->guest_tx_fh = -1;
+ status = TPM_IOERROR;
+ goto abort_with_error;
+ }
+ free(dmi_cmd);
+ } else {
+ dmi_cmd_size = VTPM_COMMAND_HEADER_SIZE_SRV;
+ size_write = write(dmi_res->guest_tx_fh, cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV );
+ if (size_write > 0) {
+ for (i=0; i<VTPM_COMMAND_HEADER_SIZE_SRV; i++)
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", cmd_header[i]);
+
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+ } else {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error writing to DMI. Aborting... \n");
+ close(dmi_res->guest_tx_fh);
+ dmi_res->guest_tx_fh = -1;
+ status = TPM_IOERROR;
+ goto abort_with_error;
+ }
+ }
+
+ if (size_write != (int) dmi_cmd_size)
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Could not write entire command to DMI (%d/%d)\n", size_write, dmi_cmd_size);
+ buffer_free(command_buf);
+
+ // Open vtpm_globals->guest_rx_fh to receive DMI response
+ if (vtpm_globals->guest_rx_fh < 0)
+ vtpm_globals->guest_rx_fh = open(GUEST_RX_FIFO, O_RDONLY);
+
+ // Handle open failures
+ if (vtpm_globals->guest_rx_fh < 0){
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Can't open inbound fh to dmi.\n");
+ status = TPM_IOERROR;
+ goto abort_with_error;
+ }
+
+ // Read header for response to TPM command from DMI
+ size_read = read( vtpm_globals->guest_rx_fh, cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV);
+ if (size_read > 0) {
+ vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "RECV (DMI): 0x");
+ for (i=0; i<size_read; i++)
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", cmd_header[i]);
+
+ } else {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error reading from DMI. Aborting... \n");
+ close(vtpm_globals->guest_rx_fh);
+ vtpm_globals->guest_rx_fh = -1;
+ status = TPM_IOERROR;
+ goto abort_with_error;
+ }
+
+ if (size_read < (int) VTPM_COMMAND_HEADER_SIZE_SRV) {
+ //vtpmdeepsublog("\n");
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Command from DMI shorter than normal header. Aborting...\n");
+ status = TPM_IOERROR;
+ goto abort_with_error;
+ }
+
+ // Unpack response from DMI for TPM command
+ BSG_UnpackList(cmd_header, 4,
+ BSG_TYPE_UINT32, &dmi,
+ BSG_TPM_TAG, &tag,
+ BSG_TYPE_UINT32, &in_param_size,
+ BSG_TPM_COMMAND_CODE, &status );
+
+ // If response has parameters, read them.
+ // Note that in_param_size is in the client's context
+ cmd_size = in_param_size - VTPM_COMMAND_HEADER_SIZE_CLT;
+ if (cmd_size > 0) {
+ in_param = (BYTE *) malloc(cmd_size);
+ size_read = read( vtpm_globals->guest_rx_fh, in_param, cmd_size);
+ if (size_read > 0) {
+ for (i=0; i<size_read; i++)
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", in_param[i]);
+
+ } else {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error reading from BE. Aborting... \n");
+ close(vtpm_globals->guest_rx_fh);
+ vtpm_globals->guest_rx_fh = -1;
+ status = TPM_IOERROR;
+ goto abort_with_error;
+ }
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM, "\n");
+
+ if (size_read < (int)cmd_size) {
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM, "\n");
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Command read(%d) from DMI is shorter than header indicates(%d). Aborting...\n", size_read, cmd_size);
+ status = TPM_IOERROR;
+ goto abort_with_error;
+ }
+ } else {
+ in_param = NULL;
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM, "\n");
+ }
+
+ if (buffer_init_convert(result_buf, cmd_size, in_param) != TPM_SUCCESS) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Failed to setup buffers. Aborting...\n");
+ status = TPM_FAIL;
+ goto abort_with_error;
+ }
+
+ vtpmhandlerloginfo(VTPM_LOG_VTPM, "Sending DMI's response to guest.\n");
+ } // end else for if (dmi==0)
+
+ } else { // This is a DMI lister thread. Thus this is from a DMI
+ // Raw TPM commands are not supported from the DMI
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Attempt to use unsupported direct access to TPM.\n");
+ vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "Bad Command. dmi:%d, tag:%d, size:%d, ord:%d, Params: ", dmi, tag, in_param_size, ord);
+ for (i=0; i<cmd_size; i++)
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", in_param[i]);
+
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+
+ status = TPM_FAIL;
+ } // end else for if BE Listener
+#endif
+
+ } // end else for is VTPM Command
+
+ // This marks the beginning of preparing response to be sent out.
+ // Errors while handling responses jump here to reply with error messages
+ // NOTE: Currently there are no recoverable errors in multi-VM mode. If one
+ // is added to the code, this ifdef should be removed.
+ // Also note this is NOT referring to errors in commands, but rather
+ // this is about I/O errors and such.
+#ifndef VTPM_MULTI_VM
+ abort_with_error:
+#endif
+
+ // Open tx_fh in preperation to send reponse back
+ if (*tx_fh < 0) {
+#ifdef VTPM_MULTI_VM
+ *tx_fh = open(VTPM_BE_DEV, O_RDWR);
+#else
+ if (threadType == BE_LISTENER_THREAD)
+ #ifdef DUMMY_BACKEND
+ *tx_fh = open("/tmp/out.fifo", O_RDWR);
+ #else
+ *tx_fh = open(VTPM_BE_DEV, O_RDWR);
+ #endif
+ else // DMI Listener
+ *tx_fh = open(dmi_res->vtpm_tx_fname, O_WRONLY);
+#endif
+ }
+
+
+ // Handle failed open
+ if (*tx_fh < 0) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "VTPM ERROR: Can't open outbound fh.\n");
+#ifdef VTPM_MULTI_VM
+ return TPM_IOERROR;
+#else
+ *ret_value = TPM_IOERROR;
+ pthread_exit(ret_value);
+#endif
+ }
+
+ // Prepend VTPM header with destination DM stamped
+ out_param_size = buffer_len(result_buf);
+ out_message_size = VTPM_COMMAND_HEADER_SIZE_CLT + out_param_size;
+ out_message_size_full = VTPM_COMMAND_HEADER_SIZE_SRV + out_param_size;
+ out_message = (BYTE *) malloc (out_message_size_full);
+
+ BSG_PackList(out_message, 4,
+ BSG_TYPE_UINT32, (BYTE *) &dmi,
+ BSG_TPM_TAG, (BYTE *) &tag,
+ BSG_TYPE_UINT32, (BYTE *) &out_message_size,
+ BSG_TPM_RESULT, (BYTE *) &status);
+
+ if (buffer_len(result_buf) > 0)
+ memcpy(out_message + VTPM_COMMAND_HEADER_SIZE_SRV, result_buf->bytes, out_param_size);
+
+
+ //Note: Send message + dmi_id
+ size_write = write(*tx_fh, out_message, out_message_size_full );
+ if (size_write > 0) {
+ vtpmhandlerloginfo(VTPM_LOG_VTPM_DEEP, "SENT: 0x");
+ for (i=0; i < out_message_size_full; i++)
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", out_message[i]);
+
+ vtpmhandlerloginfomore(VTPM_LOG_VTPM_DEEP, "\n");
+ } else {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Error writing to BE. Aborting... \n");
+ close(*tx_fh);
+ *tx_fh = -1;
+ goto abort_command;
+ }
+ free(out_message);
+
+ if (size_write < (int)out_message_size_full) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "Unable to write full command to BE (%d/%d)\n", size_write, out_message_size_full);
+ goto abort_command;
+ }
+
+ // On certain failures an error message cannot be sent.
+ // This marks the beginning of cleanup in preperation for the next command.
+ abort_command:
+ //free buffers
+ bzero(cmd_header, VTPM_COMMAND_HEADER_SIZE_SRV);
+ //free(in_param); // This was converted to command_buf. No need to free
+ if (command_buf != result_buf)
+ buffer_free(result_buf);
+
+ buffer_free(command_buf);
+
+#ifndef VTPM_MULTI_VM
+ if (threadType != BE_LISTENER_THREAD) {
+#endif
+ if ( (vtpm_globals->DMI_table_dirty) &&
+ (VTPM_SaveService() != TPM_SUCCESS) ) {
+ vtpmhandlerlogerror(VTPM_LOG_VTPM, "ERROR: Unable to save manager data.\n");
+ }
+#ifndef VTPM_MULTI_VM
+ }
+#endif
+
+ } // End while(1)
+
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+TPM_RESULT VTPM_Init_Service() {
+ TPM_RESULT status = TPM_FAIL;
+ BYTE *randomsead;
+ UINT32 randomsize;
+
+ if ((vtpm_globals = (VTPM_GLOBALS *) malloc(sizeof(VTPM_GLOBALS))) == NULL){
+ status = TPM_FAIL;
+ goto abort_egress;
+ }
+ memset(vtpm_globals, 0, sizeof(VTPM_GLOBALS));
+ vtpm_globals->be_fh = -1;
+
+#ifndef VTPM_MULTI_VM
+ vtpm_globals->vtpm_rx_fh = -1;
+ vtpm_globals->guest_rx_fh = -1;
+#endif
+ if ((vtpm_globals->dmi_map = create_hashtable(10, hashfunc32, equals32)) == NULL){
+ status = TPM_FAIL;
+ goto abort_egress;
+ }
+
+ vtpm_globals->DMI_table_dirty = FALSE;
+
+ // Create new TCS Object
+ vtpm_globals->manager_tcs_handle = 0;
+
+ TPMTRYRETURN(TCS_create());
+
+ // Create TCS Context for service
+ TPMTRYRETURN( TCS_OpenContext(&vtpm_globals->manager_tcs_handle ) );
+
+ TPMTRYRETURN( TCSP_GetRandom(vtpm_globals->manager_tcs_handle,
+ &randomsize,
+ &randomsead));
+
+ Crypto_Init(randomsead, randomsize);
+ TPMTRYRETURN( TCS_FreeMemory (vtpm_globals->manager_tcs_handle, randomsead));
+
+ // Create OIAP session for service's authorized commands
+ TPMTRYRETURN( VTSP_OIAP( vtpm_globals->manager_tcs_handle,
+ &vtpm_globals->keyAuth) );
+ vtpm_globals->keyAuth.fContinueAuthSession = TRUE;
+
+ // If failed, create new Service.
+ if (VTPM_LoadService() != TPM_SUCCESS)
+ TPMTRYRETURN( VTPM_Create_Service() );
+
+ //Load Storage Key
+ TPMTRYRETURN( VTSP_LoadKey( vtpm_globals->manager_tcs_handle,
+ TPM_SRK_KEYHANDLE,
+ &vtpm_globals->storageKeyWrap,
+ (const TPM_AUTHDATA*)&vtpm_globals->srk_usage_auth,
+ &vtpm_globals->storageKeyHandle,
+ &vtpm_globals->keyAuth,
+ &vtpm_globals->storageKey) );
+
+ // Create entry for Dom0 for control messages
+ TPMTRYRETURN( VTPM_Handle_New_DMI(NULL) );
+
+ // --------------------- Command handlers ---------------------------
+
+ goto egress;
+
+ abort_egress:
+ egress:
+
+ return(status);
+}
+
+void VTPM_Stop_Service() {
+ VTPM_DMI_RESOURCE *dmi_res;
+ struct hashtable_itr *dmi_itr;
+
+ // Close all the TCS contexts. TCS should evict keys based on this
+ if (hashtable_count(vtpm_globals->dmi_map) > 0) {
+ dmi_itr = hashtable_iterator(vtpm_globals->dmi_map);
+ do {
+ dmi_res = (VTPM_DMI_RESOURCE *) hashtable_iterator_value(dmi_itr);
+ if (dmi_res->connected)
+ close_dmi( dmi_res ); // Not really interested in return code
+
+ } while (hashtable_iterator_advance(dmi_itr));
+ free (dmi_itr);
+ }
+
+
+ TCS_CloseContext(vtpm_globals->manager_tcs_handle);
+
+ if ( (vtpm_globals->DMI_table_dirty) &&
+ (VTPM_SaveService() != TPM_SUCCESS) )
+ vtpmlogerror(VTPM_LOG_VTPM, "Unable to save manager data.\n");
+
+ hashtable_destroy(vtpm_globals->dmi_map, 1);
+ free(vtpm_globals);
+
+ close(vtpm_globals->be_fh);
+ Crypto_Exit();
+
+ vtpmloginfo(VTPM_LOG_VTPM, "VTPM Manager stopped.\n");
+}
diff --git a/tools/vtpm_manager/manager/vtpmpriv.h b/tools/vtpm_manager/manager/vtpmpriv.h
index 81cf923bab..5777c1a3c2 100644
--- a/tools/vtpm_manager/manager/vtpmpriv.h
+++ b/tools/vtpm_manager/manager/vtpmpriv.h
@@ -47,8 +47,8 @@
#define STATE_FILE "/var/vtpm/VTPM"
#define DMI_NVM_FILE "/var/vtpm/vtpm_dm_%d.data"
-#define VTPM_BE_DEV "/dev/vtpm"
-#define VTPM_CTL_DM 0
+#define VTPM_BE_DEV "/dev/vtpm0"
+#define VTPM_CTL_DM 0
#ifndef VTPM_MUTLI_VM
#include <sys/types.h>
diff --git a/tools/vtpm_manager/tcs/Makefile b/tools/vtpm_manager/tcs/Makefile
index 2a4e7110f7..56f4166fdf 100644
--- a/tools/vtpm_manager/tcs/Makefile
+++ b/tools/vtpm_manager/tcs/Makefile
@@ -13,6 +13,7 @@ clean:
rm -f *.a *.so *.o *.rpm $(DEP_FILES)
mrproper: clean
+ rm -f *~
$(BIN): $(OBJS)
$(AR) rcs $(BIN) $(OBJS)
diff --git a/tools/vtpm_manager/tcs/contextmgr.c b/tools/vtpm_manager/tcs/contextmgr.c
index 31563f0e8e..cf3803c6ba 100644
--- a/tools/vtpm_manager/tcs/contextmgr.c
+++ b/tools/vtpm_manager/tcs/contextmgr.c
@@ -43,6 +43,7 @@
#include "tcs.h"
#include "contextmgr.h"
#include "log.h"
+#include "hashtable.h"
BYTE* AddMemBlock(CONTEXT_HANDLE* pContextHandle, // in
int BlockSize) { // in
@@ -131,12 +132,14 @@ BOOL DeleteMemBlock(CONTEXT_HANDLE* pContextHandle, // in
return bFound;
}
-BOOL AddHandleToList(CONTEXT_HANDLE* pContextHandle, // in
+BOOL AddHandleToList(TCS_CONTEXT_HANDLE hContext, // in
TPM_RESOURCE_TYPE type, // in
TPM_HANDLE handle) { // in
HANDLE_LIST* pNewHandle = NULL;
-
+
vtpmloginfo(VTPM_LOG_TCS_DEEP, "Adding Handle to list\n");
+ CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);
+
if (pContextHandle == NULL)
return 0;
@@ -154,11 +157,13 @@ BOOL AddHandleToList(CONTEXT_HANDLE* pContextHandle, // in
return 1;
}
-BOOL DeleteHandleFromList( CONTEXT_HANDLE* pContextHandle, // in
+BOOL DeleteHandleFromList( TCS_CONTEXT_HANDLE hContext, // in
TPM_HANDLE handle) { // in
+ CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);
+
HANDLE_LIST *pCurrentHandle = pContextHandle->pHandleList,
- *pLastHandle = pCurrentHandle;
+ *pLastHandle = pCurrentHandle;
vtpmloginfo(VTPM_LOG_TCS_DEEP, "Deleting Handle from list\n");
@@ -202,10 +207,10 @@ BOOL FreeHandleList( CONTEXT_HANDLE* pContextHandle) { // in
switch (pCurrentHandle->type) {
case TPM_RT_KEY:
- returncode = returncode && !TCSP_EvictKey((TCS_CONTEXT_HANDLE) pContextHandle, pCurrentHandle->handle);
+ returncode = returncode && !TCSP_EvictKey(pContextHandle->handle, pCurrentHandle->handle);
break;
case TPM_RT_AUTH:
- returncode = returncode && !TCSP_TerminateHandle((TCS_CONTEXT_HANDLE) pContextHandle, pCurrentHandle->handle);
+ returncode = returncode && !TCSP_TerminateHandle(pContextHandle->handle, pCurrentHandle->handle);
break;
default:
returncode = FALSE;
diff --git a/tools/vtpm_manager/tcs/contextmgr.h b/tools/vtpm_manager/tcs/contextmgr.h
index 9639df4df5..e3fdf0f322 100644
--- a/tools/vtpm_manager/tcs/contextmgr.h
+++ b/tools/vtpm_manager/tcs/contextmgr.h
@@ -57,6 +57,7 @@ typedef struct handle_List {
} HANDLE_LIST;
typedef struct context_handle {
+ TCS_CONTEXT_HANDLE handle;
int nBlockCount;
BLOCK* pTopBlock;
HANDLE_LIST* pHandleList;
@@ -69,11 +70,11 @@ BOOL DeleteMemBlock(CONTEXT_HANDLE* pContextHandle, // in
BYTE* pTCPA_BYTEs); // in
-BOOL AddHandleToList( CONTEXT_HANDLE* pContextHandle, // in
+BOOL AddHandleToList( TCS_CONTEXT_HANDLE hContext, // in
TPM_RESOURCE_TYPE type, // in
TPM_HANDLE handle); // in
-BOOL DeleteHandleFromList( CONTEXT_HANDLE* pContextHandle, // in
+BOOL DeleteHandleFromList( TCS_CONTEXT_HANDLE hContext, // in
TPM_HANDLE handle); // in
BOOL FreeHandleList( CONTEXT_HANDLE* pContextHandle); // in
diff --git a/tools/vtpm_manager/tcs/tcs.c b/tools/vtpm_manager/tcs/tcs.c
index 210a61bd34..ad31b0c26f 100644
--- a/tools/vtpm_manager/tcs/tcs.c
+++ b/tools/vtpm_manager/tcs/tcs.c
@@ -47,9 +47,10 @@
#include "contextmgr.h"
#include "tpmddl.h"
#include "log.h"
+#include "hashtable.h"
+#include "hashtable_itr.h"
// Static Global Vars for the TCS
-static BOOL TCS_m_bConnected;
static int TCS_m_nCount = 0;
#define TCPA_MAX_BUFFER_LENGTH 0x2000
@@ -57,6 +58,21 @@ static int TCS_m_nCount = 0;
static BYTE InBuf [TCPA_MAX_BUFFER_LENGTH];
static BYTE OutBuf[TCPA_MAX_BUFFER_LENGTH];
+struct hashtable *context_ht;
+
+// -------------------------- Hash table functions --------------------
+
+static unsigned int hashfunc32(void *ky) {
+ return (* (UINT32 *) ky);
+}
+
+static int equals32(void *k1, void *k2) {
+ return (*(UINT32 *) k1 == *(UINT32 *) k2);
+}
+
+CONTEXT_HANDLE *LookupContext( TCS_CONTEXT_HANDLE hContext) {
+ return( (CONTEXT_HANDLE *) hashtable_search(context_ht, &hContext) );
+}
// ---------------------------------------------------------------------------------
// Initialization/Uninitialization SubComponent API
@@ -64,34 +80,50 @@ static BYTE OutBuf[TCPA_MAX_BUFFER_LENGTH];
TPM_RESULT TCS_create() {
TDDL_RESULT hRes = TDDL_E_FAIL;
TPM_RESULT result = TPM_FAIL;
- TCS_m_bConnected = FALSE;
if (TCS_m_nCount == 0) {
vtpmloginfo(VTPM_LOG_TCS, "Constructing new TCS:\n");
hRes = TDDL_Open();
-
- if (hRes == TDDL_SUCCESS) {
- TCS_m_bConnected = TRUE;
+
+ context_ht = create_hashtable(10, hashfunc32, equals32);
+
+ if ((hRes == TDDL_SUCCESS) && (context_ht != NULL)) {
result = TPM_SUCCESS;
+ TCS_m_nCount++;
+ } else {
+ result = TPM_IOERROR;
+ hashtable_destroy(context_ht, 1);
}
} else
- TCS_m_bConnected = TRUE;
-
- TCS_m_nCount++;
-
+ TCS_m_nCount++;
+
return(result);
}
void TCS_destroy()
{
- // FIXME: Should iterate through all open contexts and close them.
TCS_m_nCount--;
- if (TCS_m_bConnected == TRUE && TCS_m_nCount == 0) {
+ if (TCS_m_nCount == 0) {
vtpmloginfo(VTPM_LOG_TCS, "Destructing TCS:\n");
TDDL_Close();
- TCS_m_bConnected = FALSE;
+
+ struct hashtable_itr *context_itr;
+ TCS_CONTEXT_HANDLE *hContext;
+
+ // Close all the TCS contexts. TCS should evict keys based on this
+ if (hashtable_count(context_ht) > 0) {
+ context_itr = hashtable_iterator(context_ht);
+ do {
+ hContext = (TCS_CONTEXT_HANDLE *) hashtable_iterator_key(context_itr);
+ if (TCS_CloseContext(*hContext) != TPM_SUCCESS)
+ vtpmlogerror(VTPM_LOG_TCS, "Failed to close context %d properly.\n", *hContext);
+
+ } while (hashtable_iterator_advance(context_itr));
+ free(context_itr);
+ }
+ hashtable_destroy(context_ht, 1);
}
}
@@ -101,7 +133,7 @@ TPM_RESULT TCS_Malloc( TCS_CONTEXT_HANDLE hContext, // in
BYTE** ppMemPtr) {// out
TPM_RESULT returnCode = TPM_FAIL;
- CONTEXT_HANDLE* pContextHandle = (CONTEXT_HANDLE*)hContext;
+ CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);
if (pContextHandle != NULL && ppMemPtr != NULL) {
*ppMemPtr = (BYTE *)AddMemBlock(pContextHandle, MemSize);
@@ -114,7 +146,7 @@ TPM_RESULT TCS_Malloc( TCS_CONTEXT_HANDLE hContext, // in
TPM_RESULT TCS_FreeMemory( TCS_CONTEXT_HANDLE hContext, // in
BYTE* pMemory) { // in
TPM_RESULT returnCode = TPM_FAIL;
- CONTEXT_HANDLE* pContextHandle = (CONTEXT_HANDLE*)hContext;
+ CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);
if ( (pContextHandle != NULL && pMemory != NULL) &&
(DeleteMemBlock(pContextHandle, pMemory) == TRUE) )
@@ -126,16 +158,16 @@ TPM_RESULT TCS_FreeMemory( TCS_CONTEXT_HANDLE hContext, // in
TPM_RESULT TCS_OpenContext(TCS_CONTEXT_HANDLE* hContext) { // out
TPM_RESULT returnCode = TPM_FAIL;
+ TCS_CONTEXT_HANDLE *newContext;
vtpmloginfo(VTPM_LOG_TCS, "Calling TCS_OpenContext:\n");
// hContext must point to a null memory context handle
if(*hContext == HANDLE_NULL) {
- CONTEXT_HANDLE* pContextHandle = (CONTEXT_HANDLE *)malloc(sizeof(CONTEXT_HANDLE));
+ CONTEXT_HANDLE* pContextHandle = (CONTEXT_HANDLE *) malloc(sizeof(CONTEXT_HANDLE));
if (pContextHandle == NULL)
return TPM_SIZE;
-
// initialize to 0
pContextHandle->nBlockCount = 0;
pContextHandle->pTopBlock = NULL;
@@ -144,19 +176,32 @@ TPM_RESULT TCS_OpenContext(TCS_CONTEXT_HANDLE* hContext) { // out
// Create New Block
AddMemBlock(pContextHandle, BLOCK_SIZE);
- *hContext = (TCS_CONTEXT_HANDLE)pContextHandle;
- returnCode = TPM_SUCCESS;
+ newContext = (TCS_CONTEXT_HANDLE *) malloc(sizeof(TCS_CONTEXT_HANDLE));
+ *newContext = (TCS_CONTEXT_HANDLE) (((uintptr_t) pContextHandle >> 2) & 0xffffffff);
+
+ if (hashtable_search(context_ht, &newContext) !=NULL)
+ *newContext += 1;
+
+ pContextHandle->handle = *newContext;
+ if (!hashtable_insert(context_ht, newContext, pContextHandle)) {
+ free(newContext);
+ free(pContextHandle);
+ returnCode = TPM_FAIL;
+ } else {
+ *hContext = *newContext;
+ returnCode = TPM_SUCCESS;
+ }
}
return(returnCode);
}
TPM_RESULT TCS_CloseContext(TCS_CONTEXT_HANDLE hContext) {// in
- //FIXME: TCS SHOULD Track track failed auths and make sure
+ //FIXME: TCS SHOULD Track failed auths and make sure
//we don't try and re-free them here.
TPM_RESULT returnCode = TPM_FAIL;
- CONTEXT_HANDLE* pContextHandle = (CONTEXT_HANDLE*)hContext;
+ CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);
if(pContextHandle != NULL) {
// Print test info
@@ -171,6 +216,9 @@ TPM_RESULT TCS_CloseContext(TCS_CONTEXT_HANDLE hContext) {// in
vtpmlogerror(VTPM_LOG_TCS, "Not all handles evicted from TPM.\n");
// Release the TPM's resources
+ if (hashtable_remove(context_ht, &hContext) == NULL)
+ vtpmlogerror(VTPM_LOG_TCS, "Not all handles evicted from TPM.\n");
+
free(pContextHandle);
returnCode = TPM_SUCCESS;
}
@@ -255,7 +303,7 @@ TPM_RESULT TCSP_OIAP(TCS_CONTEXT_HANDLE hContext, // in
BSG_TYPE_UINT32, authHandle,
BSG_TPM_NONCE, nonce0);
- if (!AddHandleToList((CONTEXT_HANDLE *)hContext, TPM_RT_AUTH, *authHandle))
+ if (!AddHandleToList(hContext, TPM_RT_AUTH, *authHandle))
vtpmlogerror(VTPM_LOG_TCS, "New AuthHandle not recorded\n");
vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
@@ -321,7 +369,7 @@ TPM_RESULT TCSP_OSAP(TCS_CONTEXT_HANDLE hContext, // in
BSG_TPM_NONCE, nonceEven,
BSG_TPM_NONCE, nonceEvenOSAP);
- if (!AddHandleToList((CONTEXT_HANDLE *)hContext, TPM_RT_AUTH, *authHandle)) {
+ if (!AddHandleToList(hContext, TPM_RT_AUTH, *authHandle)) {
vtpmlogerror(VTPM_LOG_TCS, "New AuthHandle not recorded\n");
}
@@ -498,7 +546,7 @@ TPM_RESULT TCSP_TerminateHandle(TCS_CONTEXT_HANDLE hContext, // in
BSG_TYPE_UINT32, &paramSize,
BSG_TPM_COMMAND_CODE, &returnCode);
- if (!DeleteHandleFromList((CONTEXT_HANDLE *)hContext, handle))
+ if (!DeleteHandleFromList(hContext, handle))
vtpmlogerror(VTPM_LOG_TCS, "KeyHandle not removed from list\n");
@@ -897,7 +945,7 @@ TPM_RESULT TCSP_LoadKeyByBlob(TCS_CONTEXT_HANDLE hContext, // in
phKeyTCSI);
unpackAuth(pAuth, OutBuf+i);
- if (!AddHandleToList((CONTEXT_HANDLE *)hContext, TPM_RT_KEY, *phKeyTCSI)) {
+ if (!AddHandleToList(hContext, TPM_RT_KEY, *phKeyTCSI)) {
vtpmlogerror(VTPM_LOG_TCS, "New KeyHandle not recorded\n");
}
@@ -942,7 +990,7 @@ TPM_RESULT TCSP_EvictKey(TCS_CONTEXT_HANDLE hContext, // in
BSG_TYPE_UINT32, &paramSize,
BSG_TPM_COMMAND_CODE, &returnCode);
- if (!DeleteHandleFromList((CONTEXT_HANDLE *)hContext, hKey)) {
+ if (!DeleteHandleFromList(hContext, hKey)) {
vtpmlogerror(VTPM_LOG_TCS, "KeyHandle not removed from list\n");
}
diff --git a/tools/vtpm_manager/tcs/tcs.h b/tools/vtpm_manager/tcs/tcs.h
index c50c948f3a..49e4b34543 100644
--- a/tools/vtpm_manager/tcs/tcs.h
+++ b/tools/vtpm_manager/tcs/tcs.h
@@ -41,6 +41,7 @@
#define __TCS_H__
#include "tcg.h"
+#include "contextmgr.h"
#include "buffer.h"
#define HANDLE_NULL 0
@@ -235,4 +236,7 @@ TPM_RESULT TCSP_RawTransmitData(UINT32 inDataSize, // in
UINT32 *outDataSize,// in/out
BYTE *outData); // out
+///////////// Private Functions ////////////////////
+CONTEXT_HANDLE* LookupContext( TCS_CONTEXT_HANDLE hContext);
+
#endif //TCS_H
diff --git a/tools/vtpm_manager/tcs/transmit.c b/tools/vtpm_manager/tcs/transmit.c
index 75ffce66de..9501f4cbe0 100644
--- a/tools/vtpm_manager/tcs/transmit.c
+++ b/tools/vtpm_manager/tcs/transmit.c
@@ -69,7 +69,7 @@ TDDL_TransmitData( TDDL_BYTE* in,
ERRORDIE (TPM_IOERROR);
}
else if ((TDDL_UINT32) size < insize) {
- vtpmlogerror(VTPM_LOG_TXDATA, "Wrote %d instead of %d bytes!\n", size, insize);
+ vtpmlogerror(VTPM_LOG_TXDATA, "Wrote %d instead of %d bytes!\n", (int) size, insize);
// ... ?
}
diff --git a/tools/vtpm_manager/util/Makefile b/tools/vtpm_manager/util/Makefile
index f81269e9f3..ea2e1988f7 100644
--- a/tools/vtpm_manager/util/Makefile
+++ b/tools/vtpm_manager/util/Makefile
@@ -13,6 +13,7 @@ clean:
rm -f *.a *.so *.o *.rpm $(DEP_FILES)
mrproper: clean
+ rm -f *~
$(BIN): $(OBJS)
$(AR) rcs $(BIN) $(OBJS)
diff --git a/tools/vtpm_manager/util/tcg.h b/tools/vtpm_manager/util/tcg.h
index 293aebd07c..741ffe21fd 100644
--- a/tools/vtpm_manager/util/tcg.h
+++ b/tools/vtpm_manager/util/tcg.h
@@ -453,14 +453,14 @@ typedef struct TCS_AUTH {
// DEPENDS: local var 'status' of type TPM_RESULT
// DEPENDS: label 'abort_egress' which cleans up and returns the status
#define ERRORDIE(s) do { status = s; \
- fprintf (stderr, "*** ERRORDIE in %s, line %i\n", __func__, __LINE__); \
+ fprintf (stderr, "*** ERRORDIE in %s at %s: %i\n", __func__, __FILE__, __LINE__); \
goto abort_egress; } \
while (0)
// ASSUME: the return value used after the abort_egress label has been set
// already (eg. the 'status' local var)
#define STATUSCHECK(s) if (s != TPM_SUCCESS) { \
- fprintf (stderr, "*** ERR in %s, line %i\n", __func__, __LINE__); \
+ fprintf (stderr, "*** ERR in %s at %s:%i\n", __func__, __FILE__, __LINE__); \
goto abort_egress; \
}
@@ -475,7 +475,7 @@ typedef struct TCS_AUTH {
// Try command c. If it fails, print error message, set status to actual return code. Goto shame
#define TPMTRYRETURN(c) do { status = c; \
if (status != TPM_SUCCESS) { \
- printf("ERROR in %s:%i code: %s.\n", __func__, __LINE__, tpm_get_error_name(status)); \
+ printf("ERROR in %s at %s:%i code: %s.\n", __func__, __FILE__, __LINE__, tpm_get_error_name(status)); \
goto abort_egress; \
} \
} while(0)