From: Matthew Fioravante Subject: [PATCH 11/12] VTPM mini-os: vtpmmgrdom Date: Fri, 11 Mar 2011 18:26:12 -0500 Message-ID: <4D7AAF94.1080806@jhuapl.edu> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1670206541==" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org This is a cryptographically signed message in MIME format. --===============1670206541== Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha1; boundary="------------ms010204010507000308040803" This is a cryptographically signed message in MIME format. --------------ms010204010507000308040803 Content-Type: multipart/mixed; boundary="------------000003060604080703030204" This is a multi-part message in MIME format. --------------000003060604080703030204 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable This patch provides a mini-os domain for the vtpm manager. It links in=20 parts of the original vtpm_managerd code, openssl ported to mini-os, and = implements the rest of the functionality itself. All data and keys are stored in a disk image with a custom format which=20 must be created and provided through the domain config file. This disk=20 image is encrypted using TPM keys. The vtpm manager requires access to the hardware tpm. This can be=20 facilitated in 2 ways: 1. Direct access (default). To use this method, provide an io memory=20 region to the domain using the vtpm managers domain config file. mmio =3D ['fed40',5] 2. Passthrough dom0: To use this method, use the paravirtualized tpm=20 driver and set its backend to dom0. vtpm =3D ['backend=3D0'] Since hardware access is the default, you will also have tell the=20 manager to use the paravirtualized driver extra =3D 'tpmdriver=3Dtpmfront' Install the vtpm connection daemon in dom0 $ cd /tools/vtpm_manager/vtpmconnd # make install Finally run the connection daemon before booting the domain. Make sure=20 tpmbk and tpm_tis are loaded in dom0. # vtpmconnd Further and more complete documentation is forthcoming Signed off by: Matthew Fioravante --------------000003060604080703030204 Content-Type: text/x-patch; name="11-vtpmmgrdom.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="11-vtpmmgrdom.patch" diff -Naur xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/Makefile xen-uns= table-trp-sdp/stubdom/vtpmmgr/Makefile --- xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/Makefile 1969-12-31 19:= 00:00.000000000 -0500 +++ xen-unstable-trp-sdp/stubdom/vtpmmgr/Makefile 2011-02-23 19:40:48.000= 000000 -0500 @@ -0,0 +1,51 @@ +# LICENSE AND DISCLAIMER +# +# Copyright =C2=A9 2011 The Johns Hopkins University/Applied Physics=20 +# Laboratory + +# This software was developed at The Johns Hopkins University/Applied=20 +# Physics Laboratory (=E2=80=9CJHU/APL=E2=80=9D) that is the author ther= eof under the=20 +# =E2=80=9Cwork made for hire=E2=80=9D provisions of the copyright law. = Permission is=20 +# hereby granted, free of charge, to any person obtaining a copy of this= =20 +# software and associated documentation (the =E2=80=9CSoftware=E2=80=9D)= , to use the=20 +# Software without restriction, including without limitation the rights = to=20 +# copy, modify, merge, publish, distribute, sublicense, and/or sell copi= es=20 +# of the Software, and to permit others to do so, subject to the followi= ng=20 +# conditions: + +# 1. This LICENSE AND DISCLAIMER, including the copyright notice, shall= =20 +# be included in all copies of the Software, including copies of=20 +# substantial portions of the Software;=20 + +# 2. JHU/APL assumes no obligation to provide support of any kind with = +# regard to the Software. This includes no obligation to provide assist= ance=20 +# in using the Software nor to provide updated versions of the Software;= and + +# 3. THE SOFTWARE AND ITS DOCUMENTATION ARE PROVIDED AS IS AND WITHOUT = ANY=20 +# EXPRESS OR IMPLIED WARRANTIES WHATSOEVER. ALL WARRANTIES INCLUDING, B= UT=20 +# NOT LIMITED TO, PERFORMANCE, MERCHANTABILITY, FITNESS FOR A PARTICULAR= =20 +# PURPOSE, AND NONINFRINGEMENT ARE HEREBY DISCLAIMED. USERS ASSUME THE = +# ENTIRE RISK AND LIABILITY OF USING THE SOFTWARE. USERS ARE ADVISED TO= =20 +# TEST THE SOFTWARE THOROUGHLY BEFORE RELYING ON IT. IN NO EVENT SHALL = THE=20 +# JOHNS HOPKINS UNIVERSITY BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDI= NG,=20 +# WITHOUT LIMITATION, ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTAL= OR=20 +# CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY TO USE THE = +# SOFTWARE. + +XEN_ROOT=3D../.. + +TARGET=3Dvtpmmgr.a +OBJS=3Dvtpmmgr.o vtpm_cmd_handler.o vtpm_storage.o + +CFLAGS+=3D-Werror + +build: $(TARGET) +$(TARGET): $(OBJS) + ar -rcs $@ $^ + +clean:=20 + rm -f $(TARGET) $(OBJS) + +distclean: clean + +.PHONY: compat clean-compat clean distclean diff -Naur xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpm_cmd_handler= =2Ec xen-unstable-trp-sdp/stubdom/vtpmmgr/vtpm_cmd_handler.c --- xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpm_cmd_handler.c 1969= -12-31 19:00:00.000000000 -0500 +++ xen-unstable-trp-sdp/stubdom/vtpmmgr/vtpm_cmd_handler.c 2011-02-23 19= :40:48.000000000 -0500 @@ -0,0 +1,174 @@ +/* LICENSE AND DISCLAIMER + * + * Copyright =C2=A9 2011 The Johns Hopkins University/Applied Physics=20 + * Laboratory + + * This software was developed at The Johns Hopkins University/Applied=20 + * Physics Laboratory (=E2=80=9CJHU/APL=E2=80=9D) that is the author the= reof under the=20 + * =E2=80=9Cwork made for hire=E2=80=9D provisions of the copyright law.= Permission is=20 + * hereby granted, free of charge, to any person obtaining a copy of thi= s=20 + * software and associated documentation (the =E2=80=9CSoftware=E2=80=9D= ), to use the=20 + * Software without restriction, including without limitation the rights= to=20 + * copy, modify, merge, publish, distribute, sublicense, and/or sell cop= ies=20 + * of the Software, and to permit others to do so, subject to the follow= ing=20 + * conditions: + + * 1. This LICENSE AND DISCLAIMER, including the copyright notice, shal= l=20 + * be included in all copies of the Software, including copies of=20 + * substantial portions of the Software;=20 + + * 2. JHU/APL assumes no obligation to provide support of any kind with= =20 + * regard to the Software. This includes no obligation to provide assis= tance=20 + * in using the Software nor to provide updated versions of the Software= ; and + + * 3. THE SOFTWARE AND ITS DOCUMENTATION ARE PROVIDED AS IS AND WITHOUT= ANY=20 + * EXPRESS OR IMPLIED WARRANTIES WHATSOEVER. ALL WARRANTIES INCLUDING, = BUT=20 + * NOT LIMITED TO, PERFORMANCE, MERCHANTABILITY, FITNESS FOR A PARTICULA= R=20 + * PURPOSE, AND NONINFRINGEMENT ARE HEREBY DISCLAIMED. USERS ASSUME THE= =20 + * ENTIRE RISK AND LIABILITY OF USING THE SOFTWARE. USERS ARE ADVISED T= O=20 + * TEST THE SOFTWARE THOROUGHLY BEFORE RELYING ON IT. IN NO EVENT SHALL= THE=20 + * JOHNS HOPKINS UNIVERSITY BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUD= ING,=20 + * WITHOUT LIMITATION, ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTA= L OR=20 + * CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY TO USE THE= =20 + * SOFTWARE. + */ +#include "vtpm_cmd_handler.h" + +#include +#include +#include +#include +#include +#include + +#include "vtpm_storage.h" +#include "vtpmmgr.h" + +static TPM_RESULT make_vtpm_resp_buf(buffer_t* resbuf, TPM_TAG tag, TPM_= RESULT status, int size) { + UINT32 sz =3D VTPM_COMMAND_HEADER_SIZE_CLT + size; + TPM_TAG restag; + + switch(tag) { + case TPM_TAG_RQU_COMMAND: + restag =3D TPM_TAG_RSP_COMMAND; + break; + case TPM_TAG_RQU_AUTH1_COMMAND: + restag =3D TPM_TAG_RSP_AUTH1_COMMAND; + break; + case TPM_TAG_RQU_AUTH2_COMMAND: + restag =3D TPM_TAG_RSP_AUTH2_COMMAND; + break; + case VTPM_TAG_REQ: + restag =3D VTPM_TAG_RSP; + break; + default: + restag =3D tag; + } + + buffer_init(resbuf, sz, NULL); + BSG_PackList(resbuf->bytes, 3, + BSG_TPM_TAG, (BYTE*) &restag, + BSG_TYPE_UINT32, (BYTE*) &sz, + BSG_TPM_RESULT, (BYTE*) &status); + + return status; +} + +TPM_RESULT vtpmmgr_tpmpass(char* uuid, buffer_t* cmdbuf, buffer_t* resbu= f) { + TPM_RESULT status =3D TPM_SUCCESS; + + TPM_TAG tag; + UINT32 insz; + TPM_COMMAND_CODE ord; + + BSG_UnpackList(cmdbuf->bytes, 3, + BSG_TPM_TAG, &tag, + BSG_TYPE_UINT32, &insz, + BSG_TPM_COMMAND_CODE, &ord); + + switch(ord) { + /*This should be a list of allowed ordinals, right now only 1 is a= llowed */ + case TPM_ORD_GetRandom: + vtpmloginfo(VTPM_LOG_VTPM, "TPM Command Passthrough ord=3D%" PRIu32 "\= n", ord); + TPMTRY(TPM_IOERROR, VTSP_RawTransmit(vtpm_globals->manager_tcs_handle,= cmdbuf, resbuf)); + + break; + default: + status =3D TPM_DISABLED_CMD; + vtpmlogerror(VTPM_LOG_VTPM, "TPM Command passthrough failed: command n= ot allowed! ord =3D %" PRIu32 "\n", ord); + } + goto egress; +abort_egress: +egress: + return status; +} + +TPM_RESULT vtpmmgr_handle_cmd(char* uuid, buffer_t* cmdbuf, buffer_t* re= sbuf)=20 +{ + TPM_RESULT status =3D TPM_SUCCESS; + TPM_TAG tag; + UINT32 insz; + TPM_COMMAND_CODE ord; + + /* Unpack the header fields */ + BSG_UnpackList(cmdbuf->bytes, 3, + BSG_TPM_TAG, &tag, + BSG_TYPE_UINT32, &insz, + BSG_TPM_COMMAND_CODE, &ord); + + /* Handle the command now */ + if(tag =3D=3D VTPM_TAG_REQ) { + //This is a vTPM command + buffer_t inbuf; + buffer_init_alias_convert(&inbuf, buffer_len(cmdbuf) - VTPM_COMMAN= D_HEADER_SIZE_CLT, cmdbuf->bytes + VTPM_COMMAND_HEADER_SIZE_CLT); + switch(ord) { + case VTPM_ORD_TPMCOMMAND: + /* Forward raw tpm command embedded inside */ + TPMTRYRETURN(vtpmmgr_tpmpass(uuid, &inbuf, resbuf)); + break; + case VTPM_ORD_GET_MIG_KEY: + /* Get migration key */ + vtpmlogerror(VTPM_LOG_VTPM,"VTPM_ORD_GET_MIG_KEY: Not Implemented!\= n"); + status =3D TPM_FAIL; + goto abort_egress; + break; + case VTPM_ORD_LOAD_MIG_KEY: + /* Load Migration key */ + vtpmlogerror(VTPM_LOG_VTPM, "VTPM_ORD_LOAD_MIG_KEY: Not Implemented= !\n"); + status =3D TPM_FAIL; + goto abort_egress; + break; + case VTPM_ORD_SAVEHASHKEY: + /* Save Storage Key and hash */ + TPMTRYRETURN(vtpm_storage_SaveHashKey(uuid, &inbuf)); + make_vtpm_resp_buf(resbuf, tag, status, 0); + break; + case VTPM_ORD_LOADHASHKEY: + /* Load storage key and hash */ + { + /* Try to load key from storage */ + buffer_t keybuf =3D NULL_BUF; + TPMTRYRETURN(vtpm_storage_LoadHashKey(uuid, &keybuf)); + + /* pack the key data into a tpm response */ + make_vtpm_resp_buf(resbuf, tag, status, buffer_len(&keybuf)); + memcpy(resbuf->bytes + VTPM_COMMAND_HEADER_SIZE_CLT, keybuf.byte= s, buffer_len(&keybuf)); + buffer_free(&keybuf); + } + break; + default: + vtpmlogerror(VTPM_LOG_VTPM, "Invalid vTPM Ordinal %" PRIu32 "\n", o= rd); + status =3D TPM_BAD_ORDINAL; + goto abort_egress; + } + } else { + //This is not a vTPM command, forward to tpm + TPMTRYRETURN(vtpmmgr_tpmpass(uuid, cmdbuf, resbuf)); + } + goto egress; +abort_egress: + make_vtpm_resp_buf(resbuf, tag, status, 0); +egress: + return status; +} + diff -Naur xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpm_cmd_handler= =2Eh xen-unstable-trp-sdp/stubdom/vtpmmgr/vtpm_cmd_handler.h --- xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpm_cmd_handler.h 1969= -12-31 19:00:00.000000000 -0500 +++ xen-unstable-trp-sdp/stubdom/vtpmmgr/vtpm_cmd_handler.h 2011-02-23 19= :40:48.000000000 -0500 @@ -0,0 +1,42 @@ +/* LICENSE AND DISCLAIMER + * + * Copyright =C2=A9 2011 The Johns Hopkins University/Applied Physics=20 + * Laboratory + + * This software was developed at The Johns Hopkins University/Applied=20 + * Physics Laboratory (=E2=80=9CJHU/APL=E2=80=9D) that is the author the= reof under the=20 + * =E2=80=9Cwork made for hire=E2=80=9D provisions of the copyright law.= Permission is=20 + * hereby granted, free of charge, to any person obtaining a copy of thi= s=20 + * software and associated documentation (the =E2=80=9CSoftware=E2=80=9D= ), to use the=20 + * Software without restriction, including without limitation the rights= to=20 + * copy, modify, merge, publish, distribute, sublicense, and/or sell cop= ies=20 + * of the Software, and to permit others to do so, subject to the follow= ing=20 + * conditions: + + * 1. This LICENSE AND DISCLAIMER, including the copyright notice, shal= l=20 + * be included in all copies of the Software, including copies of=20 + * substantial portions of the Software;=20 + + * 2. JHU/APL assumes no obligation to provide support of any kind with= =20 + * regard to the Software. This includes no obligation to provide assis= tance=20 + * in using the Software nor to provide updated versions of the Software= ; and + + * 3. THE SOFTWARE AND ITS DOCUMENTATION ARE PROVIDED AS IS AND WITHOUT= ANY=20 + * EXPRESS OR IMPLIED WARRANTIES WHATSOEVER. ALL WARRANTIES INCLUDING, = BUT=20 + * NOT LIMITED TO, PERFORMANCE, MERCHANTABILITY, FITNESS FOR A PARTICULA= R=20 + * PURPOSE, AND NONINFRINGEMENT ARE HEREBY DISCLAIMED. USERS ASSUME THE= =20 + * ENTIRE RISK AND LIABILITY OF USING THE SOFTWARE. USERS ARE ADVISED T= O=20 + * TEST THE SOFTWARE THOROUGHLY BEFORE RELYING ON IT. IN NO EVENT SHALL= THE=20 + * JOHNS HOPKINS UNIVERSITY BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUD= ING,=20 + * WITHOUT LIMITATION, ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTA= L OR=20 + * CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY TO USE THE= =20 + * SOFTWARE. + */ +#ifndef VTPMMGR_CMD_HANDLER_H +#define VTPMMGR_CMD_HANDLER_H + +#include + +TPM_RESULT vtpmmgr_handle_cmd(char* uuid, buffer_t* cmdbuf, buffer_t* re= sbuf); + +#endif diff -Naur xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpmmgr.c xen-un= stable-trp-sdp/stubdom/vtpmmgr/vtpmmgr.c --- xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpmmgr.c 1969-12-31 19= :00:00.000000000 -0500 +++ xen-unstable-trp-sdp/stubdom/vtpmmgr/vtpmmgr.c 2011-02-23 19:40:48.00= 0000000 -0500 @@ -0,0 +1,466 @@ +/* LICENSE AND DISCLAIMER + * + * Copyright =C2=A9 2011 The Johns Hopkins University/Applied Physics=20 + * Laboratory + + * This software was developed at The Johns Hopkins University/Applied=20 + * Physics Laboratory (=E2=80=9CJHU/APL=E2=80=9D) that is the author the= reof under the=20 + * =E2=80=9Cwork made for hire=E2=80=9D provisions of the copyright law.= Permission is=20 + * hereby granted, free of charge, to any person obtaining a copy of thi= s=20 + * software and associated documentation (the =E2=80=9CSoftware=E2=80=9D= ), to use the=20 + * Software without restriction, including without limitation the rights= to=20 + * copy, modify, merge, publish, distribute, sublicense, and/or sell cop= ies=20 + * of the Software, and to permit others to do so, subject to the follow= ing=20 + * conditions: + + * 1. This LICENSE AND DISCLAIMER, including the copyright notice, shal= l=20 + * be included in all copies of the Software, including copies of=20 + * substantial portions of the Software;=20 + + * 2. JHU/APL assumes no obligation to provide support of any kind with= =20 + * regard to the Software. This includes no obligation to provide assis= tance=20 + * in using the Software nor to provide updated versions of the Software= ; and + + * 3. THE SOFTWARE AND ITS DOCUMENTATION ARE PROVIDED AS IS AND WITHOUT= ANY=20 + * EXPRESS OR IMPLIED WARRANTIES WHATSOEVER. ALL WARRANTIES INCLUDING, = BUT=20 + * NOT LIMITED TO, PERFORMANCE, MERCHANTABILITY, FITNESS FOR A PARTICULA= R=20 + * PURPOSE, AND NONINFRINGEMENT ARE HEREBY DISCLAIMED. USERS ASSUME THE= =20 + * ENTIRE RISK AND LIABILITY OF USING THE SOFTWARE. USERS ARE ADVISED T= O=20 + * TEST THE SOFTWARE THOROUGHLY BEFORE RELYING ON IT. IN NO EVENT SHALL= THE=20 + * JOHNS HOPKINS UNIVERSITY BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUD= ING,=20 + * WITHOUT LIMITATION, ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTA= L OR=20 + * CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY TO USE THE= =20 + * SOFTWARE. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vtpm_cmd_handler.h" +#include "vtpm_storage.h" +#include "vtpmmgr.h" + +#include + +VTPM_GLOBALS *vtpm_globals=3DNULL; + +// --------------------------- Well Known Auths ------------------------= -- +const TPM_AUTHDATA WELLKNOWN_SRK_AUTH =3D {0x00, 0x00, 0x00, 0x00, 0x00,= 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +const TPM_AUTHDATA WELLKNOWN_OWNER_AUTH =3D {0xff, 0xff, 0xff, 0xff, 0xf= f, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + +int hwtpm_fd =3D -1; + +/* Default commandline options */ +struct Opts opts =3D +{ + .seal =3D 1, + .tpmdriver =3D TPMDRV_TPM_TIS, + .tpmiomem =3D TPM_BASEADDR, + .tpmirq =3D 0, + .tpmlocality =3D 0, +}; + +TPM_RESULT vtpmmgr_create(void) { + TPM_RESULT status =3D TPM_SUCCESS; + CRYPTO_INFO ek_cryptoInfo; + TCS_AUTH osap; + TPM_AUTHDATA sharedsecret; + TPM_AUTHDATA bootKeyWrapAuth; + =20 + + // Generate Auth for Owner + memcpy(vtpm_globals->owner_usage_auth, opts.ownerauth, sizeof(TPM_AUT= HDATA)); + + // Take Owership of TPM + + // If we can read PubEK then there is no owner and we should take it.= + // We use the abilty to read the pubEK to flag that the TPM is owned.= + // FIXME: Change to just trying to take ownership and react to the st= atus + if(VTSP_ReadPubek(vtpm_globals->manager_tcs_handle, &ek_cryptoInfo) =3D= =3D TPM_SUCCESS) { + TPMTRYRETURN(VTSP_TakeOwnership(vtpm_globals->manager_tcs_handle, + (const TPM_AUTHDATA*)&vtpm_globals->owner_usage_auth,=20 + (const TPM_AUTHDATA*)&opts.srkauth, + &ek_cryptoInfo, + &vtpm_globals->keyAuth));=20 + + TPMTRYRETURN(VTSP_DisablePubekRead(vtpm_globals->manager_tcs_handl= e, + (const TPM_AUTHDATA*)&vtpm_globals->owner_usage_auth, =20 + &vtpm_globals->keyAuth)); =20 + Crypto_RSACryptoInfoFree(&ek_cryptoInfo); + } else { + vtpmloginfo(VTPM_LOG_VTPM, "Failed to readEK meaning TPM has an ow= ner. Creating Keys off existing SRK.\n"); + } + + // Generate storage key's auth + Crypto_GetRandom( &vtpm_globals->storage_key_usage_auth,=20 + sizeof(TPM_AUTHDATA) ); + + + TPMTRYRETURN( VTSP_OSAP(vtpm_globals->manager_tcs_handle, + TPM_ET_KEYHANDLE, + TPM_SRK_KEYHANDLE,=20 + (const TPM_AUTHDATA*)&opts.srkauth, + &sharedsecret,=20 + &osap) );=20 + + osap.fContinueAuthSession =3D FALSE; + + TPMTRYRETURN( VTSP_CreateWrapKey( vtpm_globals->manager_tcs_handle, + TPM_KEY_BIND, + (const TPM_AUTHDATA*)&vtpm_globals->storage_key_usage_auth, + TPM_SRK_KEYHANDLE,=20 + (const TPM_AUTHDATA*)&sharedsecret, + &vtpm_globals->storageKeyWrap, + &osap) ); + + // Generate boot key's auth + memset(&bootKeyWrapAuth, 0, sizeof(bootKeyWrapAuth)); + + TPMTRYRETURN( VTSP_OSAP(vtpm_globals->manager_tcs_handle, + TPM_ET_KEYHANDLE, + TPM_SRK_KEYHANDLE,=20 + (const TPM_AUTHDATA*)&opts.srkauth, + &sharedsecret,=20 + &osap) );=20 + + osap.fContinueAuthSession =3D FALSE; + + // FIXME: This key protects the global secrets on disk. It should use= TPM + // PCR bindings to limit its use to legit configurations. + // Current binds are open, implying a Trusted VM contains this= code. + // If this VM is not Trusted, use measurement and PCR bindings= =2E + TPMTRYRETURN( VTSP_CreateWrapKey( vtpm_globals->manager_tcs_handle, + TPM_KEY_BIND, + (const TPM_AUTHDATA*)&bootKeyWrapAuth, + TPM_SRK_KEYHANDLE,=20 + (const TPM_AUTHDATA*)&sharedsecret, + &vtpm_globals->bootKeyWrap, + &osap) ); + + // Populate CRYPTO_INFO vtpm_globals->bootKey. This does not load it = into the TPM + TPMTRYRETURN( VTSP_LoadKey( vtpm_globals->manager_tcs_handle, + TPM_SRK_KEYHANDLE, + &vtpm_globals->bootKeyWrap, + NULL, + NULL, + NULL, + &vtpm_globals->bootKey, + TRUE ) ); + + TPMTRYRETURN( VTSP_SaveState(vtpm_globals->manager_tcs_handle) ); + + goto egress; +abort_egress: +egress: + vtpmloginfo(VTPM_LOG_VTPM, "Finished initialized new VTPM manager\n")= ; + + return status; +} + +TPM_RESULT vtpmmgr_init(void) { + TPM_RESULT status =3D TPM_SUCCESS; + BYTE *randomseed =3D NULL; + UINT32 randomsize=3D256; + + if ((vtpm_globals =3D (VTPM_GLOBALS *) malloc(sizeof(VTPM_GLOBALS))) = =3D=3D NULL){ + status =3D TPM_FAIL; + goto abort_egress; + } + memset(vtpm_globals, 0, sizeof(VTPM_GLOBALS)); + + // Create new TCS Object + vtpm_globals->manager_tcs_handle =3D 0; + TPMTRYRETURN(TDDL_Open_use_fd(hwtpm_fd)); + TPMTRYRETURN(TCS_create()); + + // Blow away all stale handles left in the tpm + if(TDDL_FlushAllResources() !=3D TPM_SUCCESS) { + vtpmlogerror(VTPM_LOG_VTPM, "VTPM_FlushResources failed, continuin= g anyway..\n"); + } + + // Create TCS Context for service + TPMTRYRETURN( TCS_OpenContext(&vtpm_globals->manager_tcs_handle ) ); + TPMTRYRETURN( TCSP_GetRandom(vtpm_globals->manager_tcs_handle,=20 + &randomsize,=20 + &randomseed)); + + RAND_seed(randomseed, randomsize); + /* Initialize the crypto system */ + Crypto_Init(randomseed, randomsize); + + // Create OIAP session for service's authorized commands + TPMTRYRETURN( VTSP_OIAP( vtpm_globals->manager_tcs_handle,=20 + &vtpm_globals->keyAuth) ); + vtpm_globals->keyAuth.fContinueAuthSession =3D TRUE; + + /* Load the Manager data, if it fails create a new manager */ + if (vtpm_storage_LoadManagerData() !=3D TPM_SUCCESS) { + vtpmloginfo(VTPM_LOG_VTPM, "Failed to read manager file. Assuming = first time initialization.\n"); + TPMTRYRETURN( vtpmmgr_create() ); =20 + TPMTRYRETURN( vtpm_storage_SaveManagerData() ); + }=20 + + //Load Storage Key=20 + TPMTRYRETURN( VTSP_LoadKey( vtpm_globals->manager_tcs_handle, + TPM_SRK_KEYHANDLE, + &vtpm_globals->storageKeyWrap, + (const TPM_AUTHDATA*)&opts.srkauth, + &vtpm_globals->storageKeyHandle, + &vtpm_globals->keyAuth, + &vtpm_globals->storageKey, + FALSE ) ); + + /* Load the uuid and key data header */ + TPMTRYRETURN(vtpm_storage_LoadKeyMeta()); + + goto egress; +abort_egress: +egress: + TCS_FreeMemory (vtpm_globals->manager_tcs_handle, randomseed); + return status; +} + +void vtpmmgr_shutdown(void)=20 +{ + /*Save Data to disk */ + if ( vtpm_storage_SaveManagerData() !=3D TPM_SUCCESS )=20 + vtpmlogerror(VTPM_LOG_VTPM, "Unable to save manager data.\n"); + + /* Shutdown TCS context */ + TCS_CloseContext(vtpm_globals->manager_tcs_handle); + TCS_destroy(); + + /* Cleanup resources */ + Crypto_RSACryptoInfoFree(&vtpm_globals->bootKey); + Crypto_RSACryptoInfoFree(&vtpm_globals->storageKey); + buffer_free(&vtpm_globals->bootKeyWrap); + buffer_free(&vtpm_globals->storageKeyWrap); + free(vtpm_globals); + + /* Close crypto system */ + Crypto_Exit(); + + vtpmloginfo(VTPM_LOG_VTPM, "VTPM Manager stopped.\n"); +} + +void main_loop(void) { + tpmcmd_t* tpmcmd; + buffer_t cmdbuf; + buffer_t resbuf; + + while(1) { + /* Wait for requests from a vtpm */ + vtpmloginfo(VTPM_LOG_VTPM, "Waiting for commands from vTPM's:\n");= + if((tpmcmd =3D tpmback_req_any()) =3D=3D NULL) { + vtpmlogerror(VTPM_LOG_VTPM, "NULL tpmcmd\n"); + continue; + } + + /* setup buffers */ + buffer_init_alias_convert(&cmdbuf, tpmcmd->req_len, tpmcmd->req); + buffer_init(&resbuf, 0, NULL); + + /* Now process the command */ + vtpmmgr_handle_cmd(tpmcmd->uuid, &cmdbuf, &resbuf); + + /* Send response */ + tpmcmd->resp =3D resbuf.bytes; + tpmcmd->resp_len =3D buffer_len(&resbuf); + tpmback_resp(tpmcmd); + + /* Cleanup */ + buffer_free(&cmdbuf); + buffer_free(&resbuf); + } +} + +int parse_auth_string(char* authstr, BYTE* target, const TPM_AUTHDATA we= llknown, int allowrandom) { + int rc; + /* well known owner auth */ + if(!strcmp(authstr, "well-known")) { + memcpy(target, wellknown, sizeof(TPM_AUTHDATA)); + }=20 + /* Create a randomly generated owner auth */ + else if(allowrandom && !strcmp(authstr, "random")) { + Crypto_GetRandom(target, sizeof(TPM_AUTHDATA)); + } + /* owner auth is a raw hash */ + else if(!strncmp(authstr, "hash:", 5)) { + authstr +=3D 5; + if((rc =3D strlen(authstr)) !=3D 40) { + vtpmlogerror(VTPM_LOG_VTPM, "Supplied owner auth hex string `%s' must = be exactly 40 characters (20 bytes) long, length=3D%d\n", authstr, rc); + return -1; + } + for(int j =3D 0; j < 20; ++j) { + if(sscanf(authstr, "%hhX", target + j) !=3D 1) { + vtpmlogerror(VTPM_LOG_VTPM, "Supplied owner auth string `%s' is not= a valid hex string\n", authstr); + return -1; + } + authstr +=3D 2; + } + }=20 + /* owner auth is a string that will be hashed */ + else if(!strncmp(authstr, "text:", 5)) { + authstr +=3D 5; + Crypto_SHA1Full((BYTE*) authstr, strlen(authstr), target); + } + else { + vtpmlogerror(VTPM_LOG_VTPM, "Invalid auth string %s\n", authstr); + return -1; + } + + return 0; +} + +int parse_cmdline_opts(int argc, char** argv) +{ + int rc; + int i; + + //Set defaults + memcpy(opts.ownerauth, WELLKNOWN_OWNER_AUTH, sizeof(TPM_AUTHDATA)); + memcpy(opts.srkauth, WELLKNOWN_SRK_AUTH, sizeof(TPM_AUTHDATA)); + + for(i =3D 1; i < argc; ++i) { + if(!strcmp(argv[i], "seal")) { + opts.seal =3D 1; + } else if(!strcmp(argv[i], "noseal")) { + opts.seal =3D 0; + }=20 + else if(!strncmp(argv[i], "ownerauth:", 10)) { + if((rc =3D parse_auth_string(argv[i] + 10, opts.ownerauth, WELLKNOWN_O= WNER_AUTH, 1)) !=3D 0) { + goto err_invalid; + } + } + else if(!strncmp(argv[i], "srkauth:", 8)) { + if((rc =3D parse_auth_string(argv[i] + 8, opts.srkauth, WELLKNOWN_SRK_= AUTH, 0)) !=3D 0) { + goto err_invalid; + } + } + else if(!strncmp(argv[i], "tpmdriver=3D", 10)) { + if(!strcmp(argv[i] + 10, "tpm_tis")) { + opts.tpmdriver =3D TPMDRV_TPM_TIS; + } else if(!strcmp(argv[i] + 10, "tpmfront")) { + opts.tpmdriver =3D TPMDRV_TPMFRONT; + } else { + goto err_invalid; + } + } + else if(!strncmp(argv[i], "tpmiomem=3D",9)) { + if(sscanf(argv[i] + 9, "0x%lX", &opts.tpmiomem) !=3D 1) { + goto err_invalid; + } + } + else if(!strncmp(argv[i], "tpmirq=3D",7)) { + if(!strcmp(argv[i] + 7, "probe")) { + opts.tpmirq =3D TPM_PROBE_IRQ; + } else if( sscanf(argv[i] + 7, "%u", &opts.tpmirq) !=3D 1) { + goto err_invalid; + } + } + else if(!strncmp(argv[i], "tpmlocality=3D",12)) { + if(sscanf(argv[i] + 12, "%u", &opts.tpmlocality) !=3D 1 || opts.tpmloc= ality > 4) { + goto err_invalid; + } + } + } + + if(opts.seal) { + vtpmloginfo(VTPM_LOG_VTPM, "Option: Sealed Storage Enabled\n"); + } else { + vtpmloginfo(VTPM_LOG_VTPM, "Option: Sealed Storage Disabled\n"); + } + switch(opts.tpmdriver) { + case TPMDRV_TPM_TIS: + vtpmloginfo(VTPM_LOG_VTPM, "Option: Using tpm_tis driver\n"); + break; + case TPMDRV_TPMFRONT: + vtpmloginfo(VTPM_LOG_VTPM, "Option: Using tpmfront driver\n"); + break; + } + + return 0; +err_invalid: + vtpmlogerror(VTPM_LOG_VTPM, "Invalid Option %s\n", argv[i]); + return -1; +} + +int main(int argc, char** argv)=20 +{ + sleep(2); + vtpmloginfo(VTPM_LOG_VTPM, "Starting vTPM manager domain\n"); + + if(parse_cmdline_opts(argc, argv) !=3D 0) { + vtpmlogerror(VTPM_LOG_VTPM, "Command line parsing failed! exiting.= =2E\n"); + goto exit_postcmd; + } + + //Setup storage system + if(vtpm_storage_init() !=3D 0) { + vtpmlogerror(VTPM_LOG_VTPM, "Unable to initialize storage subsyste= m!\n"); + goto exit_poststorage; + } + + //Setup tpmback device + init_tpmback(NULL); + + //Setup tpm access + switch(opts.tpmdriver) { + case TPMDRV_TPM_TIS: + { + struct tpm_chip* tpm; + if((tpm =3D init_tpm_tis(opts.tpmiomem, TPM_TIS_LOCL_INT_TO_FLAG(op= ts.tpmlocality), opts.tpmirq)) =3D=3D NULL) { + vtpmlogerror(VTPM_LOG_VTPM, "Unable to initialize tpmfront devic= e\n"); + goto exit_posttpmfront; + } + hwtpm_fd =3D tpm_tis_open(tpm); + tpm_tis_request_locality(tpm, opts.tpmlocality); + } + break; + case TPMDRV_TPMFRONT: + { + struct tpmfront_dev* tpmfront_dev; + if((tpmfront_dev =3D init_tpmfront(NULL)) =3D=3D NULL) { + vtpmlogerror(VTPM_LOG_VTPM, "Unable to initialize tpmfront devic= e\n"); + goto exit_posttpmfront; + } + hwtpm_fd =3D tpmfront_open(tpmfront_dev); + } + break; + } + + /* Initialize the vtpm manager */ + if(vtpmmgr_init() !=3D TPM_SUCCESS) { + vtpmlogerror(VTPM_LOG_VTPM, "Unable to initialize vtpmmgr domain!\= n"); + goto exit_postmgrinit; + } + + main_loop(); + + vtpmloginfo(VTPM_LOG_VTPM, "vTPM Manager shutting down...\n"); + +exit_postmgrinit: + vtpmmgr_shutdown(); + /* tpmfront will be shutdown by manager destroying TCS Context */ +exit_posttpmfront: + shutdown_tpmback(); + vtpm_storage_shutdown(); +exit_poststorage: +exit_postcmd: + + return 0; + +} diff -Naur xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpmmgr.h xen-un= stable-trp-sdp/stubdom/vtpmmgr/vtpmmgr.h --- xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpmmgr.h 1969-12-31 19= :00:00.000000000 -0500 +++ xen-unstable-trp-sdp/stubdom/vtpmmgr/vtpmmgr.h 2011-02-23 19:40:48.00= 0000000 -0500 @@ -0,0 +1,79 @@ +/* LICENSE AND DISCLAIMER + * + * Copyright =C2=A9 2011 The Johns Hopkins University/Applied Physics=20 + * Laboratory + + * This software was developed at The Johns Hopkins University/Applied=20 + * Physics Laboratory (=E2=80=9CJHU/APL=E2=80=9D) that is the author the= reof under the=20 + * =E2=80=9Cwork made for hire=E2=80=9D provisions of the copyright law.= Permission is=20 + * hereby granted, free of charge, to any person obtaining a copy of thi= s=20 + * software and associated documentation (the =E2=80=9CSoftware=E2=80=9D= ), to use the=20 + * Software without restriction, including without limitation the rights= to=20 + * copy, modify, merge, publish, distribute, sublicense, and/or sell cop= ies=20 + * of the Software, and to permit others to do so, subject to the follow= ing=20 + * conditions: + + * 1. This LICENSE AND DISCLAIMER, including the copyright notice, shal= l=20 + * be included in all copies of the Software, including copies of=20 + * substantial portions of the Software;=20 + + * 2. JHU/APL assumes no obligation to provide support of any kind with= =20 + * regard to the Software. This includes no obligation to provide assis= tance=20 + * in using the Software nor to provide updated versions of the Software= ; and + + * 3. THE SOFTWARE AND ITS DOCUMENTATION ARE PROVIDED AS IS AND WITHOUT= ANY=20 + * EXPRESS OR IMPLIED WARRANTIES WHATSOEVER. ALL WARRANTIES INCLUDING, = BUT=20 + * NOT LIMITED TO, PERFORMANCE, MERCHANTABILITY, FITNESS FOR A PARTICULA= R=20 + * PURPOSE, AND NONINFRINGEMENT ARE HEREBY DISCLAIMED. USERS ASSUME THE= =20 + * ENTIRE RISK AND LIABILITY OF USING THE SOFTWARE. USERS ARE ADVISED T= O=20 + * TEST THE SOFTWARE THOROUGHLY BEFORE RELYING ON IT. IN NO EVENT SHALL= THE=20 + * JOHNS HOPKINS UNIVERSITY BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUD= ING,=20 + * WITHOUT LIMITATION, ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTA= L OR=20 + * CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY TO USE THE= =20 + * SOFTWARE. + */ +#ifndef VTPMMGR_H +#define VTPMMGR_H + +#include +#include + +struct Opts { + int seal; + TPM_AUTHDATA ownerauth; + TPM_AUTHDATA srkauth; + enum { + TPMDRV_TPM_TIS, + TPMDRV_TPMFRONT, + } tpmdriver; + unsigned long tpmiomem; + unsigned int tpmirq; + unsigned int tpmlocality; +}; + +extern struct Opts opts; + +#define VTPM_MANAGER_GEN 2 // This is incremented when the manager= 's table + // is changed. It's used for backwards = compatability + +typedef struct tdVTPM_GLOBALS { + TCS_CONTEXT_HANDLE manager_tcs_handle; // TCS Handle used by mana= ger + TPM_HANDLE storageKeyHandle; // Key used by persistent = store + CRYPTO_INFO storageKey; // For software encryption= + CRYPTO_INFO bootKey; // For saving table + TCS_AUTH keyAuth; // OIAP session for storag= eKey=20 + =20 + // Persistent Data + TPM_AUTHDATA owner_usage_auth; // OwnerAuth of real TPM + buffer_t storageKeyWrap; // Wrapped copy of storage= Key + TPM_AUTHDATA srk_usage_auth; + TPM_AUTHDATA storage_key_usage_auth;=20 + + buffer_t bootKeyWrap; // Wrapped copy of boot ke= y=20 + +}VTPM_GLOBALS; + +// --------------------------- Global Values -------------------------- +extern VTPM_GLOBALS *vtpm_globals; // Key info and DMI states + +#endif diff -Naur xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpm_storage.c x= en-unstable-trp-sdp/stubdom/vtpmmgr/vtpm_storage.c --- xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpm_storage.c 1969-12-= 31 19:00:00.000000000 -0500 +++ xen-unstable-trp-sdp/stubdom/vtpmmgr/vtpm_storage.c 2011-02-23 19:40:= 48.000000000 -0500 @@ -0,0 +1,1348 @@ +/* LICENSE AND DISCLAIMER + * + * Copyright =C2=A9 2011 The Johns Hopkins University/Applied Physics=20 + * Laboratory + + * This software was developed at The Johns Hopkins University/Applied=20 + * Physics Laboratory (=E2=80=9CJHU/APL=E2=80=9D) that is the author the= reof under the=20 + * =E2=80=9Cwork made for hire=E2=80=9D provisions of the copyright law.= Permission is=20 + * hereby granted, free of charge, to any person obtaining a copy of thi= s=20 + * software and associated documentation (the =E2=80=9CSoftware=E2=80=9D= ), to use the=20 + * Software without restriction, including without limitation the rights= to=20 + * copy, modify, merge, publish, distribute, sublicense, and/or sell cop= ies=20 + * of the Software, and to permit others to do so, subject to the follow= ing=20 + * conditions: + + * 1. This LICENSE AND DISCLAIMER, including the copyright notice, shal= l=20 + * be included in all copies of the Software, including copies of=20 + * substantial portions of the Software;=20 + + * 2. JHU/APL assumes no obligation to provide support of any kind with= =20 + * regard to the Software. This includes no obligation to provide assis= tance=20 + * in using the Software nor to provide updated versions of the Software= ; and + + * 3. THE SOFTWARE AND ITS DOCUMENTATION ARE PROVIDED AS IS AND WITHOUT= ANY=20 + * EXPRESS OR IMPLIED WARRANTIES WHATSOEVER. ALL WARRANTIES INCLUDING, = BUT=20 + * NOT LIMITED TO, PERFORMANCE, MERCHANTABILITY, FITNESS FOR A PARTICULA= R=20 + * PURPOSE, AND NONINFRINGEMENT ARE HEREBY DISCLAIMED. USERS ASSUME THE= =20 + * ENTIRE RISK AND LIABILITY OF USING THE SOFTWARE. USERS ARE ADVISED T= O=20 + * TEST THE SOFTWARE THOROUGHLY BEFORE RELYING ON IT. IN NO EVENT SHALL= THE=20 + * JOHNS HOPKINS UNIVERSITY BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUD= ING,=20 + * WITHOUT LIMITATION, ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTA= L OR=20 + * CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY TO USE THE= =20 + * SOFTWARE. + */ + +/*************************************************************** + * DISK IMAGE LAYOUT + * ************************************************************* + * All data is stored in BIG ENDIAN format + * ************************************************************* + * Section 1: Header (fixed size =3D 38 bytes) + * + * 10 bytes id ID String "VTPMMGRDOM" + * uint32_t version Disk Image version number (current =3D=3D 1) + * uint32_t off_mgrdata Manager Data Section Offset=09 + * uint32_t sz_mgrdata Manager Data Size + * uint32_t off_uuiddata Uuid Data Section Offset + * uint32_t sz_uuiddata Uuid Data Size (may be zero which indicates no = uuid data is present) + * uint32_t off_keydata Key Data Section Offset + * uint32_t sz_keys Key Data Section Size (may be zero which indicates= no key data is present) + * + * ************************************************************* + * Section 2: VTPM Manager Data + * + * see vtpm_manager.h + * + * ************************************************************* + * Section 3: Uuid Data + *=20 + * This section contains a bunch of null terminated (0 byte)=20 + * strings. One for each vtpm Uuid we are storing keys for. + * Each null terminated string is followed by a 32 bit integer + * index telling where its data is located in the key section. + * The end of the section is marked by an empty string. + * + * Example: ("foo\0bar\0baz\0\0") + * + * This entire section is encrypted with the vTPM storage key + *=20 + * ************************************************************* + * Section 4: Key Data Table + * + * This section has a header: + * uint32_t sz_key Encrypted size of each table entry + * uint32_t num_keys Number of keys in this section + * + * Each table entry in the key data section is as follows: + * 20 bytes hash Sha1 hash of vTPM's data, supplied by vTPM + * 256 bytes key AES-256 Encryption Key, supplied by vTPM + * + * This data is encrypted, which may increase the total size of + * the key data entry. The encrypted size of each key data entry is give= n by sz_key + * in the header. The total size of this section is sz_key * num_keys. + * + * ************************************************************* + */ +#define DISKVERS 1 +#define HEADERSZ 38 +#define KEYHDRSZ 8 +#define IDSTR "VTPMMGRDOM" +#define IDSTRLEN 10 +#define DEFALLOC 16 +#define SECTIONBDD 512=20 + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "vtpmmgr.h" +#include "vtpm_storage.h" + +#define MAX(a,b) ( ((a) > (b)) ? (a) : (b) ) +#define MIN(a,b) ( ((a) < (b)) ? (a) : (b) ) + +/* blkfront device objets */ +static struct blkfront_dev* blkdev =3D NULL; +static int blkfront_fd =3D -1; + +/* In memory copy of the disk image contents */ +struct VtpmHeader { + uint32_t version; + uint32_t off_mgrdata; + uint32_t sz_mgrdata; + uint32_t off_uuids; + uint32_t sz_uuids; + uint32_t off_keys; + uint32_t sz_keys; +}; +struct UuidPair { + char* uuid; + uint32_t index; +}; +struct UuidData { + int num_alloc; + int num_uuids; + struct UuidPair* uuids; +}; +struct KeyData { + uint32_t sz_key; + uint32_t num_keys; +}; +struct SecureStorage { + struct VtpmHeader hdr; + struct UuidData uuiddata; + struct KeyData keydata; +}; +static struct SecureStorage storage =3D=20 +{ + .hdr =3D=20 + { + .version =3D DISKVERS, + .off_mgrdata =3D 0, + .sz_mgrdata =3D 0, + .off_uuids =3D 0, + .sz_uuids =3D 0, + .off_keys =3D 0, + .sz_keys =3D 0, + }, + .uuiddata =3D=20 + { + .num_alloc =3D 0, + .num_uuids =3D 0, + .uuids =3D NULL, + }, + .keydata =3D=20 + { + .sz_key =3D 0, + .num_keys =3D 0, + }, +}; + + + +/******************************************* + * INTERNAL HELPERS + * *************************************/ + +/* move sz bytes at location st offset bytes */ +TPM_RESULT block_move(uint32_t st, uint32_t sz, int offset) { + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t buf =3D NULL_BUF; + int rc; + + buffer_init(&buf, sz, NULL); + + /* Read data */ + lseek(blkfront_fd, st, SEEK_SET); + if((rc =3D read(blkfront_fd, buf.bytes, sz)) !=3D sz) { + vtpmlogerror(VTPM_LOG_VTPM, "Block Move Read Failure : offset=3D%"= PRIu32 " size=3D%" PRIu32 " return %d, error=3D`%s'\n", st, sz, rc, stre= rror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + /* Write data */ + lseek(blkfront_fd, st + offset, SEEK_SET); + if((rc =3D write(blkfront_fd, buf.bytes, sz)) !=3D sz) { + vtpmlogerror(VTPM_LOG_VTPM, "Block Move Write Failure : offset=3D%= " PRIu32 " size=3D%" PRIu32 " return %d, error=3D`%s'\n", st + offset, sz= , rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + goto egress; +abort_egress: +egress: + buffer_free(&buf); + return status; +} + +enum SecType { + SEC_MGRDATA, + SEC_UUIDDATA, + SEC_KEYDATA +}; + +/* ENCRYPTION FUNCTIONS */ +TPM_RESULT envelope_encrypt(const buffer_t *inbuf, + CRYPTO_INFO *asymkey, + buffer_t *sealed_data) { + TPM_RESULT status =3D TPM_SUCCESS; + symkey_t *symkey; + buffer_t data_cipher =3D NULL_BUF, + symkey_cipher =3D NULL_BUF; + =20 + UINT32 i; + struct pack_constbuf_t symkey_cipher32, data_cipher32; + =20 + vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Enveloping Input[%d]: 0x", buffer_len= (inbuf)); + for (i=3D0; i< buffer_len(inbuf); i++) + vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", inbuf->bytes[i]); + vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n"); + =20 + // 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, &da= ta_cipher) ); + + // Encrypt symmetric key + TPMTRYRETURN( VTSP_Bind( asymkey,=20 + Crypto_symkey_getkey(symkey),=20 + &symkey_cipher) ); + =20 + // Create output blob: symkey_size + symkey_cipher + state_cipher_size= + state_cipher + =20 + symkey_cipher32.size =3D buffer_len(&symkey_cipher); + symkey_cipher32.data =3D symkey_cipher.bytes; + =20 + data_cipher32.size =3D buffer_len(&data_cipher); + data_cipher32.data =3D data_cipher.bytes; + + TPMTRYRETURN( buffer_init(sealed_data, 2 * sizeof(UINT32) + symkey_cip= her32.size + data_cipher32.size, NULL)); + memset(sealed_data->bytes, 0, sealed_data->size); + =20 + BSG_PackList(sealed_data->bytes, 2, + BSG_TPM_SIZE32_DATA, &symkey_cipher32, + BSG_TPM_SIZE32_DATA, &data_cipher32); + + vtpmloginfo(VTPM_LOG_VTPM, "Saved %d bytes of E(symkey) + %d bytes of = E(data)\n", buffer_len(&symkey_cipher), buffer_len(&data_cipher)); + + vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Enveloping Output[%d]: 0x", buffer_le= n(sealed_data)); + for (i=3D0; i< buffer_len(sealed_data); i++) + vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", sealed_data->bytes[i]); + vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n"); + + goto egress; + + abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "Failed to envelope encrypt\n."); + =20 + egress: + =20 + buffer_free ( &data_cipher); + buffer_free ( &symkey_cipher); + Crypto_symcrypto_freekey (symkey); + =20 + return status; +} + +TPM_RESULT symkey_encrypt(const buffer_t *inbuf, + CRYPTO_INFO *asymkey, + buffer_t *sealed_key) { + buffer_t symkey_cipher =3D NULL_BUF; + struct pack_constbuf_t symkey_cipher32; + TPM_RESULT status =3D TPM_SUCCESS; + =20 + // Encrypt symmetric key + TPMTRYRETURN( VTSP_Bind( asymkey,=20 + inbuf, + &symkey_cipher)); + + symkey_cipher32.size =3D buffer_len(&symkey_cipher); + symkey_cipher32.data =3D symkey_cipher.bytes; + + TPMTRYRETURN( buffer_init(sealed_key, sizeof(UINT32) + symkey_cipher32= =2Esize, NULL)); + BSG_PackList(sealed_key->bytes, 1, + BSG_TPM_SIZE32_DATA, &symkey_cipher32); + goto egress; +abort_egress: +egress: + buffer_free( &symkey_cipher); + return status; +} + +TPM_RESULT envelope_decrypt(const buffer_t *cipher, + TCS_CONTEXT_HANDLE TCSContext, + TPM_HANDLE keyHandle, + const TPM_AUTHDATA *key_usage_auth, + buffer_t *unsealed_data) { + + TPM_RESULT status =3D TPM_SUCCESS; + symkey_t *symkey; + buffer_t data_cipher =3D NULL_BUF,=20 + symkey_clear =3D NULL_BUF,=20 + symkey_cipher =3D NULL_BUF; + struct pack_buf_t symkey_cipher32 =3D NULL_PACK_BUF, data_cipher32 =3D= NULL_PACK_BUF; + int i; + + vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Envelope Decrypt Input[%d]: 0x", buff= er_len(cipher) ); + for (i=3D0; i< buffer_len(cipher); i++) + vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", cipher->bytes[i]); + vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n"); + =20 + BSG_UnpackList(cipher->bytes, 2, + BSG_TPM_SIZE32_DATA, &symkey_cipher32, + BSG_TPM_SIZE32_DATA, &data_cipher32); + =20 + TPMTRYRETURN( buffer_init_alias_convert (&symkey_cipher,=20 + symkey_cipher32.size,=20 + symkey_cipher32.data) ); + =20 + TPMTRYRETURN( buffer_init_alias_convert (&data_cipher,=20 + data_cipher32.size,=20 + data_cipher32.data) ); + + // Decrypt Symmetric Key + TPMTRYRETURN( VTSP_Unbind( TCSContext, + keyHandle, + &symkey_cipher, + key_usage_auth, + &symkey_clear, + &(vtpm_globals->keyAuth) ) ); + + // create symmetric key using saved bits + Crypto_symcrypto_initkey (&symkey, &symkey_clear); + =20 + // Decrypt State + TPMTRY(TPM_DECRYPT_ERROR, Crypto_symcrypto_decrypt (symkey, &data_ciph= er, unsealed_data) ); + + vtpmloginfo(VTPM_LOG_VTPM_DEEP, "Envelope Decrypte Output[%d]: 0x", bu= ffer_len(unsealed_data)); + for (i=3D0; i< buffer_len(unsealed_data); i++) + vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "%x ", unsealed_data->bytes[i]);= + vtpmloginfomore(VTPM_LOG_VTPM_DEEP, "\n"); + =20 + goto egress; + =20 + abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "Failed to envelope decrypt data\n."); + =20 + egress: + buffer_free ( &data_cipher); + buffer_free ( &symkey_clear); + buffer_free ( &symkey_cipher); + BSG_Destroy(BSG_TPM_SIZE32_DATA, &symkey_cipher32); + BSG_Destroy(BSG_TPM_SIZE32_DATA, &data_cipher32); + Crypto_symcrypto_freekey (symkey); + + =20 + return status; +} + +TPM_RESULT symkey_decrypt(const buffer_t *cipher, + TCS_CONTEXT_HANDLE TCSContext, + TPM_HANDLE keyHandle, + const TPM_AUTHDATA *key_usage_auth, + buffer_t *symkey_clear) { + + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t symkey_cipher =3D NULL_BUF; + struct pack_buf_t symkey_cipher32 =3D NULL_PACK_BUF; + + BSG_UnpackList(cipher->bytes, 1, + BSG_TPM_SIZE32_DATA, &symkey_cipher32); + =20 + TPMTRYRETURN( buffer_init_alias_convert (&symkey_cipher,=20 + symkey_cipher32.size,=20 + symkey_cipher32.data) ); + =20 + // Decrypt Symmetric Key + TPMTRYRETURN( VTSP_Unbind( TCSContext, + keyHandle, + &symkey_cipher, + key_usage_auth, + symkey_clear, + &(vtpm_globals->keyAuth) ) ); + =20 + goto egress; + abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "Failed to decrypt symmetric key status=3D= %d\n.", status); + =20 + egress: + BSG_Destroy(BSG_TPM_SIZE32_DATA, &symkey_cipher32); + return status; +} + +/* HEADER DATA FUNCTIONS */ + +#define HEADERSANE(COND, MSG) if(COND) { \ + vtpmlogerror(VTPM_LOG_VTPM, "Corrupted Header: " MSG "\n");\ + status =3D TPM_FAIL;\ + goto abort_egress;\ +} + +TPM_RESULT HeaderLoad(void)=20 +{ + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t headerbuf =3D NULL_BUF; + int rc; + + buffer_init(&headerbuf, HEADERSZ, NULL); + + /* Read the header from disk */ + lseek(blkfront_fd, 0, SEEK_SET); + if((rc =3D read(blkfront_fd, headerbuf.bytes, HEADERSZ)) !=3D HEADERS= Z) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to read vtpm header section : = return=3D%d error=3D`%s'\n", rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + /* Verify the ID string */ + if(strncmp((char*)headerbuf.bytes, IDSTR, IDSTRLEN)) { + vtpmlogerror(VTPM_LOG_VTPM, "Invalid ID string in disk image!\n");= + status =3D TPM_FAIL; + goto abort_egress; + } + + /* Unpack the header */ + BSG_UnpackList(headerbuf.bytes + IDSTRLEN, 7, + BSG_TYPE_UINT32, &storage.hdr.version, + BSG_TYPE_UINT32, &storage.hdr.off_mgrdata, + BSG_TYPE_UINT32, &storage.hdr.sz_mgrdata, + BSG_TYPE_UINT32, &storage.hdr.off_uuids, + BSG_TYPE_UINT32, &storage.hdr.sz_uuids, + BSG_TYPE_UINT32, &storage.hdr.off_keys, + BSG_TYPE_UINT32, &storage.hdr.sz_keys + ); + + /* Verify the version */ + if(storage.hdr.version !=3D DISKVERS) { + vtpmlogerror(VTPM_LOG_VTPM, "Unsupported disk image version number= %" PRIu32 "\n", storage.hdr.version); + status =3D TPM_FAIL; + goto abort_egress; + } + + /* Sanity Check Manager data offsets */ + HEADERSANE((storage.hdr.off_mgrdata =3D=3D 0 || storage.hdr.sz_mgrdat= a =3D=3D 0), "Manager Data Section is not present"); + HEADERSANE((storage.hdr.off_mgrdata < HEADERSZ), "Manager Data collid= es with Header"); + HEADERSANE(((storage.hdr.off_mgrdata < storage.hdr.off_uuids && stora= ge.hdr.off_mgrdata + storage.hdr.sz_mgrdata > storage.hdr.off_uuids) + || (storage.hdr.off_mgrdata >=3D storage.hdr.off_uuids && storage.h= dr.off_mgrdata < storage.hdr.off_uuids + storage.hdr.sz_uuids)), "Manager= Data collides with Uuid Data"); + HEADERSANE(((storage.hdr.off_mgrdata < storage.hdr.off_keys && storag= e.hdr.off_mgrdata + storage.hdr.sz_mgrdata > storage.hdr.off_keys) + || (storage.hdr.off_mgrdata >=3D storage.hdr.off_keys && storage.hd= r.off_mgrdata < storage.hdr.off_keys + storage.hdr.sz_keys)), "Manager Da= ta collides with Key Data"); + + /* Sanity Check Uuid data offsets - may not be present */ + if(storage.hdr.off_uuids !=3D 0 && storage.hdr.sz_uuids !=3D 0) { + HEADERSANE((storage.hdr.off_uuids < HEADERSZ), "Uuid Data collides= with Header"); + HEADERSANE(((storage.hdr.off_uuids < storage.hdr.off_keys && stora= ge.hdr.off_uuids + storage.hdr.sz_uuids > storage.hdr.off_keys) + || (storage.hdr.off_uuids >=3D storage.hdr.off_keys && storage.h= dr.off_uuids < storage.hdr.off_keys + storage.hdr.sz_keys)), "Uuid Data c= ollides with Key Data"); + } + /* Sanity Check Key data offsets - may not be present */ + if(storage.hdr.off_keys !=3D 0 && storage.hdr.sz_keys !=3D 0) { + HEADERSANE((storage.hdr.off_keys < HEADERSZ), "Key Data collides w= ith Header"); + } + goto egress; +abort_egress: +egress: + buffer_free(&headerbuf); + return status; + +} + +TPM_RESULT HeaderSave(void)=20 +{ + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t headerbuf =3D NULL_BUF; + int rc; + + buffer_init(&headerbuf, HEADERSZ, NULL); + + /*Copy the ID string */ + memcpy(headerbuf.bytes, IDSTR, IDSTRLEN); + + /*Pack the header into the buffer */ + BSG_PackList(headerbuf.bytes + IDSTRLEN, 7, + BSG_TYPE_UINT32, &storage.hdr.version, + BSG_TYPE_UINT32, &storage.hdr.off_mgrdata, + BSG_TYPE_UINT32, &storage.hdr.sz_mgrdata, + BSG_TYPE_UINT32, &storage.hdr.off_uuids, + BSG_TYPE_UINT32, &storage.hdr.sz_uuids, + BSG_TYPE_UINT32, &storage.hdr.off_keys, + BSG_TYPE_UINT32, &storage.hdr.sz_keys + ); + + /* Write the header to disk */ + lseek(blkfront_fd, 0, SEEK_SET); + if((rc =3D write(blkfront_fd, headerbuf.bytes, HEADERSZ)) !=3D HEADER= SZ) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to write vtpm header section := return=3D%d error=3D`%s'\n", rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + goto egress; +abort_egress: +egress: + buffer_free(&headerbuf); + return status; +} + +/* DISK RELOCATION FUNCTION */ + +/* Checks if we need to relocate any sections on the disk to make + * room for the current section=20 + * + * @sectype - Section type enum + * @size - size of the updated section + * @writehdr - if 1, update the header on disk with the new offsets + * + * */ +TPM_RESULT relocate_section(int sectype , uint32_t size, int writehdr)=20 +{ + TPM_RESULT status =3D TPM_SUCCESS; + int diff =3D 0; + uint32_t st, end; + uint32_t mvst =3D 0; + uint32_t mvsz =3D 0; + + /* Store the current state of all the offsets and sizes */ + struct VtpmHeader backuphdr; + memcpy(&backuphdr, &storage.hdr, sizeof(backuphdr)); + + switch(sectype) { + case SEC_MGRDATA: + st =3D storage.hdr.off_mgrdata =3D (HEADERSZ / SECTIONBDD + 1) * SECTI= ONBDD; + storage.hdr.sz_mgrdata =3D size; + end =3D st + storage.hdr.sz_mgrdata; + + /* Find out if our new section will overlap */ + if(storage.hdr.sz_uuids !=3D 0 && end >=3D storage.hdr.off_uuids) { + diff =3D end - storage.hdr.off_uuids + 1; + mvst =3D storage.hdr.off_uuids; + } + if(storage.hdr.sz_keys !=3D 0 && end >=3D storage.hdr.off_keys) { + diff =3D MAX(diff, end - storage.hdr.off_keys + 1); + mvst =3D mvst !=3D 0 ? MIN(mvst, storage.hdr.off_keys) : storage.hd= r.off_keys; + } + + if(diff !=3D 0) { + /* Move the rest of the data to make room */ + mvsz =3D MAX(storage.hdr.off_uuids + storage.hdr.sz_uuids, storage.= hdr.off_keys + storage.hdr.sz_keys) - mvst; + + TPMTRYRETURN(block_move(mvst, mvsz, diff)); + if(storage.hdr.sz_uuids !=3D 0) { + storage.hdr.off_uuids +=3D diff; + } + if(storage.hdr.sz_keys !=3D 0) { + storage.hdr.off_keys +=3D diff; + } + } + break; + case SEC_UUIDDATA: + /* Make sure the manager data is in the right place */ + relocate_section(SEC_MGRDATA, storage.hdr.sz_mgrdata, 0); + + /* Put the Uuid Data after the Manager data */ + st =3D storage.hdr.off_uuids =3D ((storage.hdr.off_mgrdata + storage.h= dr.sz_mgrdata) / SECTIONBDD + 1) * SECTIONBDD; + storage.hdr.sz_uuids =3D size; + end =3D st + storage.hdr.sz_uuids; + + /* Find out if our new section will overlap the key data */ + if(end >=3D storage.hdr.off_keys) { + diff =3D end - storage.hdr.off_keys + 1; + + mvst =3D storage.hdr.off_keys; + mvsz =3D storage.hdr.sz_keys; + + TPMTRYRETURN(block_move(mvst, mvsz, diff)); + storage.hdr.off_keys +=3D diff; + } + break; + case SEC_KEYDATA: + /* Make sure the uuid data (and manager data) are in the right place *= / + relocate_section(SEC_UUIDDATA, storage.hdr.sz_uuids, 0); + + /* Put the Key Data after the Uuid data */ + st =3D storage.hdr.off_keys =3D ((storage.hdr.off_uuids + storage.hdr.= sz_uuids) / SECTIONBDD + 1) * SECTIONBDD; + storage.hdr.sz_keys =3D size; + end =3D st + storage.hdr.sz_keys; + + break; + } + + /* Update the header on disk if anything changed */ + if(writehdr && memcmp(&storage.hdr, &backuphdr, sizeof(backuphdr))) {= + HeaderSave(); + } + + goto egress; +abort_egress: +egress: + return status; +} + +/* MANAGER DATA FUNCTIONS */ + +TPM_RESULT MgrDataLoad(void)=20 +{ + TPM_RESULT status=3DTPM_SUCCESS; + long step_size; + int rc; + buffer_t unsealed_data =3D NULL_BUF; + buffer_t enc_table_abuf; + struct pack_buf_t storage_key_pack =3D NULL_PACK_BUF, boot_key_pack =3D= NULL_PACK_BUF; + UINT32 enc_size; + BYTE* flat_table =3D NULL; + BYTE vtpm_manager_gen; + + TPM_HANDLE boot_key_handle; + TPM_AUTHDATA boot_usage_auth; + memset(&boot_usage_auth, 0, sizeof(TPM_AUTHDATA)); + + /* Check offsets */ + if(storage.hdr.off_mgrdata =3D=3D 0 || storage.hdr.sz_mgrdata =3D=3D = 0) { + vtpmlogerror(VTPM_LOG_VTPM, "Invalid Manager Data offset and/or si= ze\n"); + status =3D TPM_FAIL; + goto abort_egress; + } + + flat_table =3D malloc(storage.hdr.sz_mgrdata); + + /* Read Data from Disk */ + lseek(blkfront_fd, storage.hdr.off_mgrdata, SEEK_SET); + if((rc =3D read(blkfront_fd, flat_table, storage.hdr.sz_mgrdata)) !=3D= storage.hdr.sz_mgrdata) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to read vtpm manager data sect= ion : return=3D%d, error=3D`%s'\n", rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + // Read Boot Key + step_size =3D BSG_UnpackList( flat_table, 2, + BSG_TPM_SIZE32_DATA, &boot_key_pack, + BSG_TYPE_UINT32, &enc_size); + + TPMTRYRETURN(buffer_init_alias_convert(&enc_table_abuf, enc_size, fla= t_table + step_size) ); + TPMTRYRETURN(buffer_init(&vtpm_globals->bootKeyWrap, boot_key_pack.si= ze, boot_key_pack.data) ); + + + //Load Boot Key + TPMTRYRETURN( VTSP_LoadKey( vtpm_globals->manager_tcs_handle, + TPM_SRK_KEYHANDLE, + &vtpm_globals->bootKeyWrap, + (const TPM_AUTHDATA*)&opts.srkauth, + &boot_key_handle, + &vtpm_globals->keyAuth, + &vtpm_globals->bootKey, + FALSE) ); + + TPMTRYRETURN( envelope_decrypt(&enc_table_abuf, + vtpm_globals->manager_tcs_handle, + boot_key_handle, + (const TPM_AUTHDATA*) &boot_usage_auth, + &unsealed_data) ); + step_size +=3D enc_size; + + if (*unsealed_data.bytes !=3D VTPM_MANAGER_GEN) { + // Once there is more than one gen, this will include some compata= bility stuff + vtpmlogerror(VTPM_LOG_VTPM, "Warning: Manager Data file is gen %d,= while this manager is gen %d.\n", vtpm_manager_gen, VTPM_MANAGER_GEN); + } + + // Global Values needing to be saved + BSG_UnpackList( unsealed_data.bytes, 4, + BSG_TYPE_BYTE, &vtpm_manager_gen,=20 + BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth, + BSG_TPM_SECRET, &vtpm_globals->storage_key_usage_auth, + BSG_TPM_SIZE32_DATA, &storage_key_pack); + + TPMTRYRETURN(buffer_init(&vtpm_globals->storageKeyWrap, storage_key_p= ack.size, storage_key_pack.data) ); + + goto egress; + +abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "Failed to load service data with error =3D= %s\n", tpm_get_error_name(status)); +egress: + BSG_Destroy(BSG_TPM_SIZE32_DATA, &boot_key_pack); + BSG_Destroy(BSG_TPM_SIZE32_DATA, &storage_key_pack); + + buffer_free(&unsealed_data); + free(flat_table); + + // TODO: Could be nice and evict BootKey. (Need to add EvictKey to VT= SP. + + return status; + +} + +TPM_RESULT MgrDataSave(void)=20 +{ + TPM_RESULT status=3DTPM_SUCCESS; + + int datasz; + + BYTE *flat_boot_key=3DNULL; + BYTE flat_enc_len[sizeof(UINT32)]; + buffer_t clear_flat_global=3DNULL_BUF, enc_flat_global=3DNULL_BUF; + UINT32 storageKeySize =3D buffer_len(&vtpm_globals->storageKeyWrap); + UINT32 bootKeySize =3D buffer_len(&vtpm_globals->bootKeyWrap); + struct pack_buf_t storage_key_pack =3D {storageKeySize, vtpm_globals-= >storageKeyWrap.bytes}; + struct pack_buf_t boot_key_pack =3D {bootKeySize, vtpm_globals->bootK= eyWrap.bytes}; + BYTE vtpm_manager_gen =3D VTPM_MANAGER_GEN; + + UINT32 boot_key_size =3D 0; + + // Initially fill these with buffer sizes for each data type. Later f= ill + // in actual size, once flattened. + boot_key_size =3D sizeof(UINT32) + // bootkeysize + bootKeySize; // boot key + + TPMTRYRETURN(buffer_init(&clear_flat_global,sizeof(BYTE) + // manager= version + 3*sizeof(TPM_DIGEST) + // Auths + sizeof(UINT32) +// storagekeysize + storageKeySize, NULL) ); // storage key + + + flat_boot_key =3D (BYTE *) malloc( boot_key_size ); + + boot_key_size =3D BSG_PackList(flat_boot_key, 1, + BSG_TPM_SIZE32_DATA, &boot_key_pack); + + BSG_PackList(clear_flat_global.bytes, 4, + BSG_TYPE_BYTE, &vtpm_manager_gen, + BSG_TPM_AUTHDATA, &vtpm_globals->owner_usage_auth, + BSG_TPM_SECRET, &vtpm_globals->storage_key_usage_auth, + BSG_TPM_SIZE32_DATA, &storage_key_pack); + + TPMTRYRETURN(envelope_encrypt(&clear_flat_global, + &vtpm_globals->bootKey, + &enc_flat_global) ); + + BSG_PackConst(buffer_len(&enc_flat_global), 4, flat_enc_len); + + /* Prepare the disk image for the new manager data */ + datasz =3D boot_key_size + sizeof(UINT32) + buffer_len(&enc_flat_glob= al); + relocate_section(SEC_MGRDATA, datasz, 1); + + /* Write the manager data */ + lseek(blkfront_fd, storage.hdr.off_mgrdata, SEEK_SET); + if ( ( write(blkfront_fd, flat_boot_key, boot_key_size) !=3D boot_key= _size ) || + ( write(blkfront_fd, flat_enc_len, sizeof(UINT32)) !=3D sizeof(UINT32)= ) || + ( write(blkfront_fd, enc_flat_global.bytes, buffer_len(&enc_flat_globa= l)) !=3D buffer_len(&enc_flat_global) )) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to completely write service da= ta.\n"); + status =3D TPM_IOERROR; + goto abort_egress; + } + + goto egress; + +abort_egress: +egress: + + free(flat_boot_key); + buffer_free(&clear_flat_global); + buffer_free(&enc_flat_global); + + vtpmloginfo(VTPM_LOG_VTPM, "Saved VTPM Manager state (status =3D %d)\= n", (int) status); + return status; +} + +/* UUID DATA FUNCTIONS */ + +int intcmp(const void* a, const void* b) { + printf("KEY INT CMP %d - %d =3D %d\n", *(int*)a, *(int*)b, *(int*)a -= *(int*)b); + return *(int*)a - *(int*)b; +} +static uint32_t UuidGetFreeIndex(void)=20 +{ + int i; + uint32_t index =3D 0; + + //FIXME: Why doesn't qsort work? Instead just always find a larget nu= mber + for(i =3D 0; i < storage.uuiddata.num_uuids; ++i) { + if(index <=3D storage.uuiddata.uuids[i].index) { + index =3D storage.uuiddata.uuids[i].index +1; + } + } +#if 0 + int* list; + /* Create a sorted list of all the used indices */ + list =3D malloc(sizeof(int) * storage.uuiddata.num_uuids); + for(i =3D 0; i < storage.uuiddata.num_uuids; ++i) { + list[i] =3D storage.uuiddata.uuids[i].index; +printf("LIST KEY %d / %" PRIu32 "\n", list[i], storage.uuiddata.num_uuid= s); + } + + qsort(list, storage.uuiddata.num_uuids, sizeof(int), intcmp); + for(i =3D 0; i < storage.uuiddata.num_uuids; ++i) { + list[i] =3D storage.uuiddata.uuids[i].index; +printf("SORTED LIST KEY %d / %" PRIu32 "\n", list[i], storage.uuiddata.n= um_uuids); + } + + /* Find the lowest free index available */ + for(i =3D 0; i < storage.uuiddata.num_uuids; ++i) { + if(index < list[i]) { + break; + } + index =3D list[i] + 1; + } + free(list); +#endif + + return index; +} + +static int __get_index(char* uuid, int st, int sz) { + int i =3D st + (sz / 2); + int cmp; + + if(sz <=3D 0) { + return -1; + } + + cmp =3D strcmp(uuid, storage.uuiddata.uuids[i].uuid); + + if(cmp =3D=3D 0) { + return storage.uuiddata.uuids[i].index; + } else if (cmp < 0) { + return __get_index(uuid, st, sz/2); + } else { + return __get_index(uuid, i + 1, sz/2 - ((sz + 1) % 2)); + } + +} + +static int UuidGetIndex(char* uuid) { + return __get_index(uuid, 0, storage.uuiddata.num_uuids); +} + +static int UuidAdd(char* uuid, int len, int newindex) { + + int i, j; + + /* Get a new index */ + if(newindex < 0) { + newindex =3D UuidGetFreeIndex(); + } + + /* Allocate memory on first add */ + if(storage.uuiddata.num_alloc =3D=3D 0) { + storage.uuiddata.num_alloc =3D DEFALLOC; + storage.uuiddata.uuids =3D malloc(sizeof(struct UuidPair) * storag= e.uuiddata.num_alloc); + for(i =3D 0; i < storage.uuiddata.num_alloc; ++i) { + storage.uuiddata.uuids[i].uuid =3D NULL; + storage.uuiddata.uuids[i].index =3D 0; + } + } + /* Resize the array if we need to */ + if(storage.uuiddata.num_uuids >=3D storage.uuiddata.num_alloc) { + storage.uuiddata.num_alloc *=3D 2; + storage.uuiddata.uuids =3D realloc(storage.uuiddata.uuids, sizeof(= struct UuidPair) * storage.uuiddata.num_alloc); + for(i =3D storage.uuiddata.num_alloc / 2; i < storage.uuiddata.num= _alloc; ++i) { + storage.uuiddata.uuids[i].uuid =3D NULL; + storage.uuiddata.uuids[i].index =3D 0; + } + } + /* calculate the length of the string if it hasn't been given */ + if(len < 0) { + len =3D strlen(uuid); + } + /* Find the location to put the new string */ + for(i =3D 0; i < storage.uuiddata.num_uuids; ++i) { + if(strcmp(uuid, storage.uuiddata.uuids[i].uuid) < 0) { + break; + } + } + /* Shift everything after the new uuid by 1*/ + for(j =3D storage.uuiddata.num_uuids; j > i; --j) { + storage.uuiddata.uuids[j] =3D storage.uuiddata.uuids[j-1]; + } + + /* Allocate and copy the uuid string */ + storage.uuiddata.uuids[i].uuid =3D malloc(sizeof(char) * (len +1)); + strcpy(storage.uuiddata.uuids[i].uuid, uuid); + + /* Set the index */ + storage.uuiddata.uuids[i].index =3D newindex; + + ++storage.uuiddata.num_uuids; + + return storage.uuiddata.uuids[i].index; +} + +static void UuidClear(void)=20 +{ + int i; + if(storage.uuiddata.uuids !=3D NULL) { + for(i =3D 0; i < storage.uuiddata.num_uuids; ++i) { + free(storage.uuiddata.uuids[i].uuid); + } + free(storage.uuiddata.uuids); + } + storage.uuiddata.num_uuids =3D storage.uuiddata.num_alloc =3D 0; +} + + +TPM_RESULT UuidDataLoad(void) +{ + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t uuiddata =3D NULL_BUF; + buffer_t sealed =3D NULL_BUF; + int rc; + uint8_t* ptr; + int len; + uint32_t index; + + /* If there is no uuid section, silently set everything to zero and e= xit */ + if(storage.hdr.sz_uuids =3D=3D 0) { + storage.uuiddata.num_uuids =3D storage.uuiddata.num_alloc =3D 0; + goto egress; + } + + buffer_init(&sealed, storage.hdr.sz_uuids, NULL); + + /* Read uuid data from disk */ + lseek(blkfront_fd, storage.hdr.off_uuids, SEEK_SET); + if((rc =3D read(blkfront_fd, sealed.bytes, storage.hdr.sz_uuids)) !=3D= storage.hdr.sz_uuids) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to read uuid data section : re= turn %d, error=3D`%s'\n", rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + /* Decrypt uuid data */ + if(opts.seal) { + TPMTRYRETURN( envelope_decrypt(&sealed, + vtpm_globals->manager_tcs_handle, + vtpm_globals->storageKeyHandle, + (const TPM_AUTHDATA*)&vtpm_globals->storage_key_usage_auth, + &uuiddata) ); + } else { + buffer_init_alias(&uuiddata, &sealed, 0, 0); + } + + /* Wipe out any current uuid data */ + UuidClear(); + + /* Prevent possible buffer overruns later */ + uuiddata.bytes[buffer_len(&uuiddata) - 1] =3D '\0'; + + /*Load uuids into the memory array*/ + ptr =3D uuiddata.bytes; + len =3D strlen((char*)ptr); + while(len !=3D 0) { + /* Sanity check - before we read a 4 byte int make sure there is r= oom left for at least the final null terminator*/ + if(ptr + len + 1 + sizeof(uint32_t) >=3D uuiddata.bytes + buffer_l= en(&uuiddata)) { + vtpmlogerror(VTPM_LOG_VTPM, "Corrupted uuid data section!\n"); + status =3D TPM_FAIL; + goto abort_egress; + } + + /* Get the index value */ + BSG_UnpackList(ptr + len + 1, 1, + BSG_TYPE_UINT32, &index); + + /* Add the new Uuid to the list */ + UuidAdd((char*)ptr, len, index); + + /* Jump to the next string */ + ptr +=3D len + 1 + sizeof(uint32_t); + len =3D strlen((char*)ptr); + } + + + goto egress; +abort_egress: + UuidClear(); +egress: + buffer_free(&uuiddata); + buffer_free(&sealed); + return status; +} + +TPM_RESULT UuidDataSave(void) +{ + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t uuiddata =3D NULL_BUF; + buffer_t sealed =3D NULL_BUF; + int rc; + int bufsz; + int* lenuuids =3D NULL; + uint8_t* ptr; + int i; + + /* If there are no UUIDs then we are just a NOP */ + if(storage.uuiddata.num_uuids =3D=3D 0) { + goto egress; + } + + lenuuids =3D malloc(sizeof(int) * storage.uuiddata.num_uuids); + + /* Get the lengths of the uuids and the entire buffer */ + bufsz =3D 0; + for(i =3D 0; i < storage.uuiddata.num_uuids; ++i) { + lenuuids[i] =3D strlen(storage.uuiddata.uuids[i].uuid); + bufsz +=3D lenuuids[i] + 1 + sizeof(uint32_t); + } + ++bufsz; /* Final null terminator */ + + /*Create and write the buffer */ + buffer_init(&uuiddata, bufsz, NULL); + ptr =3D uuiddata.bytes; + for(i =3D 0; i < storage.uuiddata.num_uuids; ++i) { + /* Copy uuid string with its null terminator*/ + memcpy(ptr, storage.uuiddata.uuids[i].uuid, lenuuids[i] + 1); + ptr +=3D lenuuids[i] + 1; + + /* Copy 4 byte index */ + BSG_PackList(ptr, 1, BSG_TYPE_UINT32, &storage.uuiddata.uuids[i].i= ndex); + ptr +=3D sizeof(uint32_t); + } + /* Last entry has a second null terminator */ + *ptr =3D '\0'; + + /* Encrypt the data */ + if(opts.seal) { + TPMTRYRETURN( envelope_encrypt(&uuiddata, + &vtpm_globals->storageKey, + &sealed) ); + } else { + buffer_init_alias(&sealed, &uuiddata, 0, 0); + } + + /* Prepare the disk for our new uuid data */ + TPMTRYRETURN(relocate_section(SEC_UUIDDATA, buffer_len(&sealed), 1));= + + /* Write uuid data to disk */ + lseek(blkfront_fd, storage.hdr.off_uuids, SEEK_SET); + if((rc =3D write(blkfront_fd, sealed.bytes, storage.hdr.sz_uuids)) !=3D= storage.hdr.sz_uuids) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to write uuid data section : r= eturn %d, error=3D`%s'\n", rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + + goto egress; +abort_egress: +egress: + buffer_free(&uuiddata); + buffer_free(&sealed); + free(lenuuids); + return status; +} + +/* KEY DATA SECTION FUNCTIONS */ + +#define GetKeyOffset(index) (storage.hdr.off_keys + KEYHDRSZ + (storage.= keydata.sz_key * index)) + +TPM_RESULT KeyDataHeaderLoad(void)=20 +{ + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t keydata =3D NULL_BUF; + int rc; + + /* If there are no keys then we set everything to zero and exit silen= tly */ + if(storage.hdr.sz_keys =3D=3D 0) { + storage.keydata.sz_key =3D storage.keydata.num_keys =3D 0; + goto egress; + } + + buffer_init(&keydata, KEYHDRSZ, NULL); + + /* Read the key header from the disk */ + lseek(blkfront_fd, storage.hdr.off_keys, SEEK_SET); + if((rc =3D read(blkfront_fd, keydata.bytes, KEYHDRSZ)) !=3D KEYHDRSZ)= { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to read vtpm key header : retu= rn=3D%d error=3D`%s'\n", rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + /* Unpack the header values */ + BSG_UnpackList(keydata.bytes, 2, + BSG_TYPE_UINT32, &storage.keydata.sz_key, + BSG_TYPE_UINT32, &storage.keydata.num_keys); + + + /* Verify the sanity of header values */ + if(storage.keydata.sz_key * storage.keydata.num_keys > storage.hdr.sz= _keys=20 + || storage.keydata.sz_key < HASHKEYSZ) { + vtpmlogerror(VTPM_LOG_VTPM, "Corrupted key data section!\n"); + status =3D TPM_FAIL; + goto abort_egress; + } + + goto egress; +abort_egress: + storage.keydata.num_keys =3D storage.keydata.sz_key =3D 0; +egress: + buffer_free(&keydata); + + return status; +} + +TPM_RESULT KeyDataHeaderSave(void)=20 +{ + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t keydata =3D NULL_BUF; + int rc; + + /* if there was no key section before make one */ + if(storage.hdr.sz_keys =3D=3D 0) { + relocate_section(SEC_KEYDATA, KEYHDRSZ, 1); + } + + buffer_init(&keydata, KEYHDRSZ, NULL); + + /* Unpack the header values */ + BSG_PackList(keydata.bytes, 2, + BSG_TYPE_UINT32, &storage.keydata.sz_key, + BSG_TYPE_UINT32, &storage.keydata.num_keys); + + /* Write the key header to the disk */ + lseek(blkfront_fd, storage.hdr.off_keys, SEEK_SET); + if((rc =3D write(blkfront_fd, keydata.bytes, KEYHDRSZ)) !=3D KEYHDRSZ= ) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to write vtpm key data header = : return=3D%d error=3D`%s'\n", rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + goto egress; +abort_egress: +egress: + buffer_free(&keydata); + + return status; +} + +/* OTHER HELPERS */ + +static void reset_storage(void) { + UuidClear(); + memset(&storage, 0, sizeof(storage)); + storage.hdr.version =3D DISKVERS; +} + + + +/************************************** + * PUBLIC FUNCTIONS + * ***********************************/ + +int vtpm_storage_init(void) { + struct blkfront_info info; + if((blkdev =3D init_blkfront(NULL, &info)) =3D=3D NULL) { + return -1; + } + if((blkfront_fd =3D blkfront_open(blkdev)) < 0) { + return -1; + } + return 0; +} + +void vtpm_storage_shutdown(void) { + close(blkfront_fd); +} + +TPM_RESULT vtpm_storage_LoadHashKey(char* uuid, buffer_t* outbuf) +{ + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t sealed_key =3D NULL_BUF; + int rc; + int key_num; + + + /* Sanity check the key header */ + if(storage.keydata.sz_key =3D=3D 0 || storage.keydata.num_keys =3D=3D= 0) { + vtpmlogerror(VTPM_LOG_VTPM, "Loadkey failure: Invalid header!\n");= + status =3D TPM_FAIL; + goto abort_egress; + } + + /* Determine which key index we are using */ + if((key_num =3D UuidGetIndex(uuid)) < 0) { + vtpmlogerror(VTPM_LOG_VTPM, "LoadKey failure: Unrecognized uuid!\n= "); + status =3D TPM_FAIL; + goto abort_egress; + } + printk("KEY NUM =3D %d\n", key_num); + + /* Sanity check the key index */ + if(key_num >=3D storage.keydata.num_keys) { + vtpmlogerror(VTPM_LOG_VTPM, "LoadKey failure: Invalid index (%d) i= nto key table size of %" PRIu32 "\n", key_num, storage.keydata.num_keys);= + status =3D TPM_FAIL; + goto abort_egress; + } + + buffer_init(&sealed_key, storage.keydata.sz_key, NULL); + + /* Read key data from disk */ + lseek(blkfront_fd, GetKeyOffset(key_num), SEEK_SET); + if((rc =3D read(blkfront_fd, sealed_key.bytes, storage.keydata.sz_key= )) !=3D storage.keydata.sz_key) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to read key data : key number = %d, return %d, error=3D`%s'\n", key_num, rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + /*Decrypt key data */ + if(opts.seal) { + TPMTRYRETURN( symkey_decrypt(&sealed_key, + vtpm_globals->manager_tcs_handle, + vtpm_globals->storageKeyHandle, + (const TPM_AUTHDATA*)&vtpm_globals->storage_key_usage_auth, + outbuf) ); + } else { + buffer_init_alias(outbuf, &sealed_key, 0, 0); + outbuf->is_owner =3D TRUE; + sealed_key.is_owner =3D FALSE; + } + + /* Chop off any padding bits from encryption*/ + TPMTRYRETURN(buffer_truncate(outbuf, HASHKEYSZ)); + + goto egress; +abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "Failed to load key\n"); +egress: + buffer_free(&sealed_key); + return status; + +} +TPM_RESULT vtpm_storage_SaveHashKey(char* uuid, buffer_t* inbuf) +{ + TPM_RESULT status =3D TPM_SUCCESS; + buffer_t sealed_key =3D NULL_BUF; + int key_num; + int rc; + + /* Sanity check the input */ + if(buffer_len(inbuf) !=3D HASHKEYSZ) { + vtpmlogerror(VTPM_LOG_VTPM, "SaveKey failure : invalid input buffe= r size %lu\n", (unsigned long) buffer_len(inbuf)); + status =3D TPM_FAIL; + goto abort_egress; + } + + /* Encrypt the key */ + if(opts.seal) { + TPMTRYRETURN( symkey_encrypt(inbuf, + &vtpm_globals->storageKey, + &sealed_key) ); + } else { + buffer_init_alias(&sealed_key, inbuf, 0, 0); + } + + /* Get the uuid index, or create one if this is new */ + if((key_num =3D UuidGetIndex(uuid)) < 0) { + /* Create new uuid->key position mapping and save the updated data= */ + key_num =3D UuidAdd(uuid, -1, -1); + TPMTRYRETURN(UuidDataSave()); + } + + /* Create the key data section if it does not already exist */ + if(storage.hdr.sz_keys =3D=3D 0) { + storage.keydata.sz_key =3D buffer_len(&sealed_key); + storage.keydata.num_keys =3D 0; + /* No need to save the key header yet, it'll get triggered on the = next if statement */ + }=20 + /* If it does exist, sanity check the encrypted data size */ + else { + if(buffer_len(&sealed_key) !=3D storage.keydata.sz_key) { + /* this should never happen, unless the user doing something dumb like= disabling encryption on encrypted data */ + vtpmlogerror(VTPM_LOG_VTPM, "SaveKey failure : Invalid encrypted data = size??\n"); + status =3D TPM_FAIL; + goto abort_egress; + } + } + + /* Grow the key data section if needed */ + if(key_num >=3D storage.keydata.num_keys) { + storage.keydata.num_keys =3D key_num +1; + KeyDataHeaderSave(); + } + + /* Prepare the disk for the key section */ + relocate_section(SEC_KEYDATA, KEYHDRSZ + (storage.keydata.sz_key * st= orage.keydata.num_keys), 1); + + /* Write the key data to disk */ + lseek(blkfront_fd, GetKeyOffset(key_num), SEEK_SET); + if((rc =3D write(blkfront_fd, sealed_key.bytes, storage.keydata.sz_ke= y)) !=3D storage.keydata.sz_key) { + vtpmlogerror(VTPM_LOG_VTPM, "Failed to write key data : key number= %d, return %d, error=3D`%s'\n", key_num, rc, strerror(errno)); + status =3D TPM_IOERROR; + goto abort_egress; + } + + goto egress; +abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "Failed to save key\n"); +egress: + buffer_free(&sealed_key); + return status; +} + +TPM_RESULT vtpm_storage_LoadManagerData(void) +{ + TPM_RESULT status =3D TPM_SUCCESS; + + reset_storage(); + + TPMTRYRETURN(HeaderLoad()); + TPMTRYRETURN(MgrDataLoad()); + + goto egress; +abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "vTPM LoadManagerData failed\n") + reset_storage(); +egress: + return status; +} + +TPM_RESULT vtpm_storage_LoadKeyMeta(void) +{ + TPM_RESULT status =3D TPM_SUCCESS; + + TPMTRYRETURN(UuidDataLoad()); + TPMTRYRETURN(KeyDataHeaderLoad()); + + goto egress; +abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "vTPM LoadKeyMeta failed\n"); +egress: + return status; +} + +TPM_RESULT vtpm_storage_SaveManagerData(void) +{ + TPM_RESULT status =3D TPM_SUCCESS; + + TPMTRYRETURN(HeaderSave()); + TPMTRYRETURN(MgrDataSave()); + /* Uuid and Key data get saved automatically during use */ + + goto egress; +abort_egress: + vtpmlogerror(VTPM_LOG_VTPM, "vTPM SaveManagerData failed\n") +egress: + return status; + +} + diff -Naur xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpm_storage.h x= en-unstable-trp-sdp/stubdom/vtpmmgr/vtpm_storage.h --- xen-unstable-trp-sdp-pristine/stubdom/vtpmmgr/vtpm_storage.h 1969-12-= 31 19:00:00.000000000 -0500 +++ xen-unstable-trp-sdp/stubdom/vtpmmgr/vtpm_storage.h 2011-02-23 19:40:= 48.000000000 -0500 @@ -0,0 +1,69 @@ +/* LICENSE AND DISCLAIMER + * + * Copyright =C2=A9 2011 The Johns Hopkins University/Applied Physics=20 + * Laboratory + + * This software was developed at The Johns Hopkins University/Applied=20 + * Physics Laboratory (=E2=80=9CJHU/APL=E2=80=9D) that is the author the= reof under the=20 + * =E2=80=9Cwork made for hire=E2=80=9D provisions of the copyright law.= Permission is=20 + * hereby granted, free of charge, to any person obtaining a copy of thi= s=20 + * software and associated documentation (the =E2=80=9CSoftware=E2=80=9D= ), to use the=20 + * Software without restriction, including without limitation the rights= to=20 + * copy, modify, merge, publish, distribute, sublicense, and/or sell cop= ies=20 + * of the Software, and to permit others to do so, subject to the follow= ing=20 + * conditions: + + * 1. This LICENSE AND DISCLAIMER, including the copyright notice, shal= l=20 + * be included in all copies of the Software, including copies of=20 + * substantial portions of the Software;=20 + + * 2. JHU/APL assumes no obligation to provide support of any kind with= =20 + * regard to the Software. This includes no obligation to provide assis= tance=20 + * in using the Software nor to provide updated versions of the Software= ; and + + * 3. THE SOFTWARE AND ITS DOCUMENTATION ARE PROVIDED AS IS AND WITHOUT= ANY=20 + * EXPRESS OR IMPLIED WARRANTIES WHATSOEVER. ALL WARRANTIES INCLUDING, = BUT=20 + * NOT LIMITED TO, PERFORMANCE, MERCHANTABILITY, FITNESS FOR A PARTICULA= R=20 + * PURPOSE, AND NONINFRINGEMENT ARE HEREBY DISCLAIMED. USERS ASSUME THE= =20 + * ENTIRE RISK AND LIABILITY OF USING THE SOFTWARE. USERS ARE ADVISED T= O=20 + * TEST THE SOFTWARE THOROUGHLY BEFORE RELYING ON IT. IN NO EVENT SHALL= THE=20 + * JOHNS HOPKINS UNIVERSITY BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUD= ING,=20 + * WITHOUT LIMITATION, ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTA= L OR=20 + * CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY TO USE THE= =20 + * SOFTWARE. + */ +#ifndef VTPM_STORAGE_H +#define VTPM_STORAGE_h + +#include + +#define VTPM_NVMKEY_SIZE 32 +#define HASHKEYSZ (sizeof(TPM_DIGEST) + VTPM_NVMKEY_SIZE) + +/* Initialize the storage system and its virtual disk */ +int vtpm_storage_init(void); + +/* Shutdown the storage system and its virtual disk */ +void vtpm_storage_shutdown(void); + +/* Loads Sha1 hash and 256 bit AES key from disk and stores them + * packed together in outbuf. outbuf must be freed + * by the caller using buffer_free()=20 + */ +TPM_RESULT vtpm_storage_LoadHashKey(char* uuid, buffer_t* outbuf); + +/* inbuf must contain a sha1 hash followed by a 256 bit AES key. + * Encrypts and stores the hash and key to disk */ +TPM_RESULT vtpm_storage_SaveHashKey(char* uuid, buffer_t* inbuf); + +/* Load the vtpm manager data - call this on startup */ +TPM_RESULT vtpm_storage_LoadManagerData(void); + +/* Load the uuid and key header data - call after loading the storage ke= y */ +TPM_RESULT vtpm_storage_LoadKeyMeta(void); + +/* Saves the vtpm manager data - call this on shutdown */ +TPM_RESULT vtpm_storage_SaveManagerData(void); + + +#endif --------------000003060604080703030204-- --------------ms010204010507000308040803 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIHZzCC A5kwggMCoAMCAQICBD/xkcEwDQYJKoZIhvcNAQEFBQAwLzELMAkGA1UEBhMCVVMxDzANBgNV BAoTBkpIVUFQTDEPMA0GA1UECxMGQklTRENBMB4XDTA5MDcxNzE1MDgwOVoXDTEyMDcxNzE1 MzgwOVowZjELMAkGA1UEBhMCVVMxDzANBgNVBAoTBkpIVUFQTDEPMA0GA1UECxMGUGVvcGxl MTUwFgYDVQQLEw9WUE5Hcm91cC1CSVNEQ0EwGwYDVQQDExRNYXR0aGV3IEUgRmlvcmF2YW50 ZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAyzeGk9zPA33fsB3uvk/Izs9GGHCpHI8b zXdBIVg6++S+jK53PoaWgmtSLr/c732ea1zPR6ACymwAWON+U5rB+VJAUZ4l/p0T3LZjE1Kq nbQJ+pgb+WAmBtdrxrtky61E9HD8dO70x37+ejhunpF9OuSU5MnOPmMx6ranvahUsOsCAwEA AaOCAYkwggGFMAsGA1UdDwQEAwIFIDAbBg0rBgEEAbMlCwMBAQEBBAoWCGZpb3JhbWUxMBsG DSsGAQQBsyULAwEBAQIEChIIMDAxMDQyNjEwWAYJYIZIAYb6ax4BBEsMSVRoZSBwcml2YXRl IGtleSBjb3JyZXNwb25kaW5nIHRvIHRoaXMgY2VydGlmaWNhdGUgbWF5IGhhdmUgYmVlbiBl eHBvcnRlZC4wKAYDVR0RBCEwH4EdTWF0dGhldy5GaW9yYXZhbnRlQGpodWFwbC5lZHUwUgYD VR0fBEswSTBHoEWgQ6RBMD8xCzAJBgNVBAYTAlVTMQ8wDQYDVQQKEwZKSFVBUEwxDzANBgNV BAsTBkJJU0RDQTEOMAwGA1UEAxMFQ1JMNDkwHwYDVR0jBBgwFoAUCDUpmxH52EU2CyWmF2EJ MB1yqeswHQYDVR0OBBYEFO3ziReJlElP3ilaLQ5gwsg0RlgoMAkGA1UdEwQCMAAwGQYJKoZI hvZ9B0EABAwwChsEVjcuMQMCBLAwDQYJKoZIhvcNAQEFBQADgYEAMOY3Zf6gx3gv/fDd11cz h2Daj+8NExx/2Le3c88gfDVhPVgVX5S52EjeFbK5yVP0Xlm82vRADO47dTA2PKbpp50rJcAZ rl5bg5tQ/WbLAaRITCtOJWVVKXD9V7X2o3Z/IM2op3hb4mmDXSDS+Hzn0Jd2mAXl4iHPfI0p XlXqA9QwggPGMIIDL6ADAgECAgQ/8cn9MA0GCSqGSIb3DQEBBQUAMC8xCzAJBgNVBAYTAlVT MQ8wDQYDVQQKEwZKSFVBUEwxDzANBgNVBAsTBkJJU0RDQTAeFw0xMDA2MTExODIyMDZaFw0x MzA2MTExODUyMDZaMGYxCzAJBgNVBAYTAlVTMQ8wDQYDVQQKEwZKSFVBUEwxDzANBgNVBAsT BlBlb3BsZTE1MBYGA1UECxMPVlBOR3JvdXAtQklTRENBMBsGA1UEAxMUTWF0dGhldyBFIEZp b3JhdmFudGUwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJ6W8FUj+qNTW+ZXFu3Xd8k6 PYgSXYu6s+JwDTjBTyuyTsuZ6SjdYoqLrJdvFP7HFCREueYD8AFmCSt7lckALAOGnYAyouQ6 A9VBw0BMKW2O4hyyXqtDT6+AamDapwhT2xOhwvM0ia6+Kip/oFVEE9/UiBanYiDycGS/BWE0 UP87AgMBAAGjggG2MIIBsjALBgNVHQ8EBAMCB4AwKwYDVR0QBCQwIoAPMjAxMDA2MTExODIy MDZagQ8yMDEyMDcxNzIyNTIwNlowGwYNKwYBBAGzJQsDAQEBAQQKFghmaW9yYW1lMTAbBg0r BgEEAbMlCwMBAQECBAoSCDAwMTA0MjYxMFgGCWCGSAGG+mseAQRLDElUaGUgcHJpdmF0ZSBr ZXkgY29ycmVzcG9uZGluZyB0byB0aGlzIGNlcnRpZmljYXRlIG1heSBoYXZlIGJlZW4gZXhw b3J0ZWQuMCgGA1UdEQQhMB+BHU1hdHRoZXcuRmlvcmF2YW50ZUBqaHVhcGwuZWR1MFIGA1Ud HwRLMEkwR6BFoEOkQTA/MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGSkhVQVBMMQ8wDQYDVQQL EwZCSVNEQ0ExDjAMBgNVBAMTBUNSTDU2MB8GA1UdIwQYMBaAFAg1KZsR+dhFNgslphdhCTAd cqnrMB0GA1UdDgQWBBTui2MYOq/cB2fs3ULQR59XWf2EzTAJBgNVHRMEAjAAMBkGCSqGSIb2 fQdBAAQMMAobBFY3LjEDAgSwMA0GCSqGSIb3DQEBBQUAA4GBACTvR0IeGDQoVS87maiuQESQ EQ/CaLoxmX3aO+arr4No1xUHtrBI7y58SWYJv6b/H3WWpKuPAlsI2ByrryKe7A40xLEH6Psu 0qTfzdjbyVlOUqnytVYGLTS0UaBiVAeUNWmfg4PeYN5Kqcn6VDCEiS/CClS7SVXzeH4IBVVG GfcMMYICMTCCAi0CAQEwNzAvMQswCQYDVQQGEwJVUzEPMA0GA1UEChMGSkhVQVBMMQ8wDQYD VQQLEwZCSVNEQ0ECBD/xyf0wCQYFKw4DAhoFAKCCAVAwGAYJKoZIhvcNAQkDMQsGCSqGSIb3 DQEHATAcBgkqhkiG9w0BCQUxDxcNMTEwMzExMjMyNjEyWjAjBgkqhkiG9w0BCQQxFgQUlJAD FfaXoZ1k9R/I5iSLVCYENmcwRgYJKwYBBAGCNxAEMTkwNzAvMQswCQYDVQQGEwJVUzEPMA0G A1UEChMGSkhVQVBMMQ8wDQYDVQQLEwZCSVNEQ0ECBD/xkcEwSAYLKoZIhvcNAQkQAgsxOaA3 MC8xCzAJBgNVBAYTAlVTMQ8wDQYDVQQKEwZKSFVBUEwxDzANBgNVBAsTBkJJU0RDQQIEP/GR wTBfBgkqhkiG9w0BCQ8xUjBQMAsGCWCGSAFlAwQBAjAKBggqhkiG9w0DBzAOBggqhkiG9w0D AgICAIAwDQYIKoZIhvcNAwICAUAwBwYFKw4DAgcwDQYIKoZIhvcNAwICASgwDQYJKoZIhvcN AQEBBQAEgYCMo3iiWedwquQsdRWSJlShqZho/Jh9D3gARfXcFhsy1Vz4sLQDaW8PbSkAW9O4 KVVPpTwLD1AcYAf4dAavoAQOiNUxgYcZaieudAkfCi0EZ3sBYR5PaI37V1ERYd8i8g3KXYeP dGNNK2F9ePh1JveJNasDWIgqc1VpkvT/mc+SuwAAAAAAAA== --------------ms010204010507000308040803-- --===============1670206541== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --===============1670206541==--