From patchwork Wed Aug 31 14:36:05 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 112561 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5550AB6F75 for ; Thu, 1 Sep 2011 00:57:44 +1000 (EST) Received: from localhost ([::1]:42981 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qylvj-0006hb-UP for incoming@patchwork.ozlabs.org; Wed, 31 Aug 2011 10:38:15 -0400 Received: from eggs.gnu.org ([140.186.70.92]:43167) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QyluA-0003C4-Cb for qemu-devel@nongnu.org; Wed, 31 Aug 2011 10:36:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qylu7-0006T1-Jm for qemu-devel@nongnu.org; Wed, 31 Aug 2011 10:36:38 -0400 Received: from e9.ny.us.ibm.com ([32.97.182.139]:60142) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qylu7-0006Sv-F6 for qemu-devel@nongnu.org; Wed, 31 Aug 2011 10:36:35 -0400 Received: from d01relay04.pok.ibm.com (d01relay04.pok.ibm.com [9.56.227.236]) by e9.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id p7VE2Nqa028154 for ; Wed, 31 Aug 2011 10:02:23 -0400 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d01relay04.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p7VEaVQj219924 for ; Wed, 31 Aug 2011 10:36:34 -0400 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p7V8a0od000426 for ; Wed, 31 Aug 2011 02:36:01 -0600 Received: from localhost.localdomain (d941e-10.watson.ibm.com [9.59.241.154]) by d03av02.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p7V8ZxuX000303 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 31 Aug 2011 02:36:00 -0600 Received: from localhost.localdomain (d941e-10 [127.0.0.1]) by localhost.localdomain (8.14.4/8.14.3) with ESMTP id p7VEaP8h032764; Wed, 31 Aug 2011 10:36:25 -0400 Received: (from root@localhost) by localhost.localdomain (8.14.4/8.14.4/Submit) id p7VEaPew032763; Wed, 31 Aug 2011 10:36:25 -0400 Message-Id: <20110831143625.419705972@linux.vnet.ibm.com> User-Agent: quilt/0.48-1 Date: Wed, 31 Aug 2011 10:36:05 -0400 From: Stefan Berger To: stefanb@linux.vnet.ibm.com, qemu-devel@nongnu.org References: <20110831143551.127339744@linux.vnet.ibm.com> Content-Disposition: inline; filename=qemu_tpm_initstate.diff X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 32.97.182.139 Cc: chrisw@redhat.com, anbang.ruan@cs.ox.ac.uk, rrelyea@redhat.com, alevy@redhat.com, andreas.niederl@iaik.tugraz.at, serge@hallyn.com Subject: [Qemu-devel] [PATCH V8 14/14] Allow to provide inital TPM state X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch adds a -tpm ...,initstate=...,... command line option to the TPM's existing options and enables the TPM to be initialized with an existing state blob. This in turn allows us to simulate TPM manufacturing and equip the TPM with an endorsement key, certificates and initialize its NVRAM areas etc.. This step is typically done during manufacturng of the TPM and/or the (physical) machine. The initial state can be passed either as file or via a file descriptor. The encoding of the state can either be binary or in form of a base64-encoded blob surrounded by tags indicating the start and end. The intial state can be produced through a yet-to-be-published tpm-authoring tool. Signed-off-by: Stefan Berger --- hw/tpm_builtin.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- qemu-config.c | 12 +++++ qemu-options.hx | 40 ++++++++++++++++- 3 files changed, 172 insertions(+), 3 deletions(-) Index: qemu-git/hw/tpm_builtin.c =================================================================== --- qemu-git.orig/hw/tpm_builtin.c +++ qemu-git/hw/tpm_builtin.c @@ -170,9 +170,16 @@ static struct enckey { AES_KEY tpm_dec_key; } enckey; +static int tpm_initstatefd = -1; +static bool tpm_initstate_bin; + static int tpm_builtin_load_sized_data_from_bs(BlockDriverState *bs, enum BSEntryType be, TPMSizedBuffer *tsb); +static TPM_RESULT tpm_builtin_get_initial_state(unsigned char **data, + uint32_t *length, + size_t tpm_number, + const char *name); @@ -1269,7 +1276,7 @@ static TPM_RESULT tpm_builtin_nvram_load *length = permanent_state.size; if (*length == 0) { - rc = TPM_RETRY; + rc = tpm_builtin_get_initial_state(data, length, tpm_number, name); } else { /* keep a copy of the last permanent state */ rc = TPM_Malloc(data, *length); @@ -1452,6 +1459,94 @@ static TPM_RESULT tpm_builtin_io_getphys } +static TPM_RESULT tpm_builtin_get_initial_state(unsigned char **data, + uint32_t *length, + size_t tpm_number, + const char *name) +{ + TPM_RESULT rc = TPM_RETRY; + uint32_t allocated = 0; + int len, flags; + unsigned char buf[1024]; + unsigned char *result = NULL; + size_t result_len; + + if (tpm_initstatefd >= 0) { + *data = NULL; + *length = 0; + + flags = fcntl(tpm_initstatefd, F_GETFL); + if (flags < 0 || + fcntl(tpm_initstatefd, F_SETFL, flags & ~O_NONBLOCK) < 0) { + return TPM_FAIL; + } + + while (TRUE) { + len = read(tpm_initstatefd, buf, sizeof(buf)); + + if (len > 0) { + if (len > allocated - *length) { + allocated = *length + len + 1024; + if (TPM_Realloc(data, allocated) != TPM_SUCCESS) { + goto err_exit; + } + } + memcpy(&(*data)[*length], buf, len); + *length += len; + (*data)[*length] = 0; + } else if (len == 0) { + rc = TPM_SUCCESS; + break; + } else if (len < 0) { + if (errno == EINTR) { + continue; + } + goto err_exit; + } + } + + if (*data == NULL) { + /* nothing read */ + rc = TPM_FAIL; + goto err_exit; + } + + if (!tpm_initstate_bin) { + if (TPMLIB_DecodeBlob((char *)*data, TPMLIB_BLOB_TYPE_INITSTATE, + &result, &result_len) != TPM_SUCCESS) { + goto err_exit; + } + TPM_Free(*data); + *data = result; + *length = result_len; + result = NULL; + } + /* sanity check for the size of the blob */ + if (*length > tpmlib_get_prop(TPMPROP_TPM_MAX_NV_SPACE)) { + goto err_exit; + } + /* have it written into the BlockStorage */ + rc = tpm_builtin_nvram_storedata(*data, *length, tpm_number, name); + if (rc != TPM_SUCCESS) { + goto err_exit; + } + } + +norm_exit: + close(tpm_initstatefd); + tpm_initstatefd = -1; + + return rc; + +err_exit: + TPM_Free(*data); + *data = NULL; + *length = 0; + TPM_Free(result); + + goto norm_exit; +} + /*****************************************************************/ @@ -1748,6 +1843,7 @@ static TPMBackend *tpm_builtin_create(Qe const char *value; unsigned char keyvalue[256/8]; int keysize = sizeof(keyvalue); + unsigned int offset; driver = g_malloc(sizeof(TPMBackend)); if (!driver) { @@ -1801,6 +1897,28 @@ static TPMBackend *tpm_builtin_create(Qe enckey.enctype = BS_DIR_ENCTYPE_NONE; } + value = qemu_opt_get(opts, "initstate"); + if (value) { + offset = 0; + + if (!strncmp(value, "bin:", 4)) { + tpm_initstate_bin = true; + offset = 4; + } else if (!strncmp(value, "base64:", 7)) { + tpm_initstate_bin = false; + offset = 7; + } + + if (sscanf(&value[offset], "fd:%d", &tpm_initstatefd) != 1) { + tpm_initstatefd = open(&value[offset], O_RDONLY); + if (tpm_initstatefd < 0) { + fprintf(stderr, "tpm: could not open file '%s' for reading.\n", + value); + goto err_exit; + } + } + } + return driver; err_exit: @@ -1816,6 +1934,9 @@ static void tpm_builtin_destroy(TPMBacke g_free(driver->id); g_free(driver->model); g_free(driver); + + close(tpm_initstatefd); + tpm_initstatefd = -1; } Index: qemu-git/qemu-config.c =================================================================== --- qemu-git.orig/qemu-config.c +++ qemu-git/qemu-config.c @@ -527,6 +527,12 @@ static QemuOptsList qemu_tpmdev_opts = { .type = QEMU_OPT_STRING, .help = "Data encryption key", }, + { + .name = "initstate", + .type = QEMU_OPT_STRING, + .help = "File or file descriptor for reading initial TPM state " + "from", + }, { /* end of list */ } }, }; @@ -556,6 +562,12 @@ static QemuOptsList qemu_tpm_opts = { .type = QEMU_OPT_STRING, .help = "Data encryption key", }, + { + .name = "initstate", + .type = QEMU_OPT_STRING, + .help = "File or file descriptor for reading initial TPM state " + "from", + }, { /* end of list */ } }, }; Index: qemu-git/qemu-options.hx =================================================================== --- qemu-git.orig/qemu-options.hx +++ qemu-git/qemu-options.hx @@ -1767,8 +1767,10 @@ DEFHEADING(TPM device options:) DEF("tpm", HAS_ARG, QEMU_OPTION_tpm, \ "" \ "-tpm builtin,path=[,model=][,key=]\n" \ + " [,initstate=[bin:|base64:]fd:|]\n" \ " enable a builtin TPM with state in file in path\n" \ " and encrypt the TPM's state with the given AES key\n" \ + " initstate= path to initial state of TPM; default is base64\n" \ "-tpm null enable a TPM null driver that responds with a fault\n" \ " message to every TPM request\n" \ "-tpm model=? to list available TPM device models\n" \ @@ -1800,7 +1802,7 @@ Use ? to print all available TPM backend qemu -tpmdev ? @end example -@item -tpmdev builtin ,id=@var{id}, path=@var{path} [,key=@var{key}] +@item -tpmdev builtin ,id=@var{id}, path=@var{path} [,key=@var{key}] [,initstate=@var{path}] Creates an instance of the built-in TPM. @@ -1830,6 +1832,40 @@ using AES-CBC encryption scheme supply t -tpmdev builtin,id=tpm0,path=,key=aes-cbc:0x1234567890abcdef01234567890abcdef -device tpm-tis,tpmdev=tpm0 @end example +@option{initstate} specifies the path to a file containing the initial +state of the TPM. It can be used to provide the TPM with an EK and certificates +for the EK, TPM and Platform. Since the file contains binary data that +have to conform to the TPM's layout of data, it must have been created using +an approriate authoring tool. + +The initstate option allows to provide a binary state blob or one that is +encode in base 64. The base64-encode state blob must have the format + +@example +-----BEGIN INITSTATE----- + +-----END INITSTATE----- +@end example + +The initstate option is only effective when Qemu is started with blank +state. + +The initstate option supports several formats: + +@table @option + @item [base64:] + Provide the path to the TPM's initial state blob in base64 format. + @item bin: + Provide the path to the TPM's initial state blob in binary format. + @item [base64:]fd: + Provide the base64 formatted initial state via a file descriptor to read from. + @item bin:fd: + Provide the binary initial state via a file descriptor to read from. +@end table + +@option{initstate} is optional. + + @item -tpmdev null Creates an instance of a TPM null driver that responds to every command @@ -1840,7 +1876,7 @@ with a fault message. The short form of a TPM device option is: @table @option -@item -tpm @var{backend-type}, path=@var{path} [,model=@var{model}] [,key=@var{key}] +@item -tpm @var{backend-type}, path=@var{path} [,model=@var{model}] [,key=@var{key}] [,initstate=@var{path}] @findex -tpm @option{model} specifies the device model. The default device model is a