]> git.proxmox.com Git - libtpms.git/commitdiff
Move common debug, memory & nvfile units to src/
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Thu, 5 Aug 2021 10:21:54 +0000 (14:21 +0400)
committerStefan Berger <stefanb@us.ibm.com>
Fri, 6 Aug 2021 12:04:37 +0000 (08:04 -0400)
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
18 files changed:
include/libtpms/tpm_memory.h
src/Makefile.am
src/tpm12/tpm_constants.h
src/tpm12/tpm_debug.c [deleted file]
src/tpm12/tpm_debug.h [deleted file]
src/tpm12/tpm_memory.c [deleted file]
src/tpm12/tpm_nvfile.c [deleted file]
src/tpm12/tpm_nvfile.h [deleted file]
src/tpm12/tpm_structures.h
src/tpm_debug.c [new file with mode: 0644]
src/tpm_debug.h [new file with mode: 0644]
src/tpm_library.c
src/tpm_memory.c [new file with mode: 0644]
src/tpm_nvfile.c [new file with mode: 0644]
src/tpm_nvfile.h [new file with mode: 0644]
src/tpm_tpm12_interface.c
src/tpm_tpm12_tis.c
src/tpm_tpm2_interface.c

index 69d60b4abb291fcfcb61833b7d293ccc7eac3f10..3ca3aa6692aba71de6a7ba23b5370180d30ac24a 100644 (file)
 extern "C" {
 #endif
 
+/* This value is used to limit memory allocation to prevent resource overload. */
+
+#ifndef TPM_ALLOC_MAX
+#define TPM_ALLOC_MAX  0x20000  /* 128k bytes */
+#endif
+
 TPM_RESULT TPM_Malloc(unsigned char **buffer, uint32_t size);
 TPM_RESULT TPM_Realloc(unsigned char **buffer, uint32_t size);
 void       TPM_Free(unsigned char *buffer);
index c7b1e49705696d1ac045123e46a69c0f0db8951f..fc1066369173b8244221f55ba8f9e04b52b7ddd7 100644 (file)
@@ -17,6 +17,10 @@ common_CFLAGS = -include tpm_library_conf.h \
                $(SANITIZERS) \
                $(FUZZER)
 
+# build with libtpms callback support
+common_CFLAGS += -DTPM_LIBTPMS_CALLBACKS
+# let the default NVRAM write to disk
+common_CFLAGS += -DTPM_NV_DISK
 
 #
 # TPM1.2
@@ -40,10 +44,6 @@ libtpms_tpm12_la_CFLAGS += -DTPM_VOLATILE_LOAD
 libtpms_tpm12_la_CFLAGS += -DTPM_ENABLE_ACTIVATE
 # build with AES support for symmetric crypto
 libtpms_tpm12_la_CFLAGS += -DTPM_AES
-# build with libtpms callback support
-libtpms_tpm12_la_CFLAGS += -DTPM_LIBTPMS_CALLBACKS
-# let the default NVRAM write to disk
-libtpms_tpm12_la_CFLAGS += -DTPM_NV_DISK
 # build a POSIX type of TPM
 libtpms_tpm12_la_CFLAGS += -DTPM_POSIX
 # build without maintenance commands
@@ -60,7 +60,6 @@ libtpms_tpm12_la_SOURCES = \
        tpm12/tpm_cryptoh.c \
        tpm12/tpm_counter.c \
        tpm12/tpm_daa.c \
-       tpm12/tpm_debug.c \
        tpm12/tpm_delegate.c \
        tpm12/tpm_digest.c \
        tpm12/tpm_error.c \
@@ -71,10 +70,8 @@ libtpms_tpm12_la_SOURCES = \
        tpm12/tpm_key.c \
        tpm12/tpm_load.c \
        tpm12/tpm_maint.c \
-       tpm12/tpm_memory.c \
        tpm12/tpm_migration.c \
        tpm12/tpm_nonce.c \
-       tpm12/tpm_nvfile.c \
        tpm12/tpm_nvram.c \
        tpm12/tpm_openssl_helpers.c \
        tpm12/tpm_owner.c \
@@ -106,7 +103,7 @@ noinst_HEADERS = \
        tpm12/tpm_crypto.h \
        tpm12/tpm_cryptoh.h \
        tpm12/tpm_daa.h \
-       tpm12/tpm_debug.h \
+       tpm_debug.h \
        tpm12/tpm_delegate.h \
        tpm12/tpm_digest.h \
        tpm12/tpm_global.h \
@@ -120,7 +117,7 @@ noinst_HEADERS = \
        tpm12/tpm_maint.h \
        tpm12/tpm_migration.h \
        tpm12/tpm_nonce.h \
-       tpm12/tpm_nvfile.h \
+       tpm_nvfile.h \
        tpm12/tpm_nvram_const.h \
        tpm12/tpm_nvram.h \
        tpm12/tpm_openssl_helpers.h \
@@ -182,8 +179,6 @@ libtpms_tpm2_la_CFLAGS = $(common_CFLAGS)
 
 libtpms_tpm2_la_CFLAGS += -D_POSIX_
 libtpms_tpm2_la_CFLAGS += -DTPM_POSIX
-# build with libtpms callback support
-libtpms_tpm2_la_CFLAGS += -DTPM_LIBTPMS_CALLBACKS
 
 libtpms_tpm2_la_SOURCES = \
        tpm2/ACT_spt.c \
@@ -573,7 +568,10 @@ endif # WITH_TPM2
 #
 
 libtpms_la_SOURCES = \
-       tpm_library.c
+       tpm_debug.c \
+       tpm_library.c \
+       tpm_memory.c \
+       tpm_nvfile.c
 
 libtpms_la_CFLAGS = $(common_CFLAGS)
 
index 75eed840ad78c30c9f1464de679365cab2b3b38f..9569e6d083448101eaecc36ec5cb2c2f15a9d8d6 100644 (file)
   TPM buffer limits
 */
 
-/* This value is used to limit memory allocation to prevent resource overload. */
-
-#ifndef TPM_ALLOC_MAX
-#define TPM_ALLOC_MAX  0x20000  /* 128k bytes */
-#endif
-
 /* This is the increment by which the TPM_STORE_BUFFER grows.  A larger number saves realloc's.  A
    smaller number saves memory.
 
diff --git a/src/tpm12/tpm_debug.c b/src/tpm12/tpm_debug.c
deleted file mode 100644 (file)
index da4347a..0000000
+++ /dev/null
@@ -1,137 +0,0 @@
-/********************************************************************************/
-/*                                                                              */
-/*                         TPM Debug Utilities                                  */
-/*                           Written by Ken Goldman                             */
-/*                     IBM Thomas J. Watson Research Center                     */
-/*            $Id: tpm_debug.c 4179 2010-11-10 20:10:24Z kgoldman $             */
-/*                                                                              */
-/* (c) Copyright IBM Corporation 2006, 2010.                                   */
-/*                                                                             */
-/* All rights reserved.                                                                */
-/*                                                                             */
-/* Redistribution and use in source and binary forms, with or without          */
-/* modification, are permitted provided that the following conditions are      */
-/* met:                                                                                */
-/*                                                                             */
-/* Redistributions of source code must retain the above copyright notice,      */
-/* this list of conditions and the following disclaimer.                       */
-/*                                                                             */
-/* Redistributions in binary form must reproduce the above copyright           */
-/* notice, this list of conditions and the following disclaimer in the         */
-/* documentation and/or other materials provided with the distribution.                */
-/*                                                                             */
-/* Neither the names of the IBM Corporation nor the names of its               */
-/* contributors may be used to endorse or promote products derived from                */
-/* this software without specific prior written permission.                    */
-/*                                                                             */
-/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
-/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
-/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
-/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
-/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
-/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
-/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
-/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
-/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
-/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
-/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
-/********************************************************************************/
-
-#include <stdio.h>
-
-#include "tpm_commands.h"
-#include "tpm_load.h"
-
-#include "tpm_debug.h"
-#undef printf
-
-#if 0
-
-int swallow_rc = 0;
-
-int tpm_swallow_printf_args(const char *format, ...)
-{
-    format = format;   /* to silence compiler */
-    return 0;
-}
-
-#else
-
-void TPM_PrintFourLimit(const char *string,
-                        const unsigned char *buff, size_t buflen)
-{
-    if (buff != NULL) {
-        switch (buflen) {
-        case 0:
-            TPMLIB_LogPrintf("%s (no data)\n", string);
-            break;
-        case 1:
-            TPMLIB_LogPrintf("%s %02x\n",
-                   string,
-                   buff[0]);
-            break;
-        case 2:
-            TPMLIB_LogPrintf("%s %02x %02x\n",
-                   string,
-                   buff[0],
-                   buff[1]);
-            break;
-        case 3:
-            TPMLIB_LogPrintf("%s %02x %02x %02x\n",
-                   string,
-                   buff[0],
-                   buff[1],
-                   buff[2]);
-            break;
-        default:
-            TPMLIB_LogPrintf("%s %02x %02x %02x %02x\n",
-                   string,
-                   buff[0],
-                   buff[1],
-                   buff[2],
-                   buff[3]);
-        }
-    }
-    else {
-        TPMLIB_LogPrintf("%s null\n", string);
-    }
-    return;
-}
-
-/* TPM_PrintFour() prints a prefix plus 4 bytes of a buffer */
-
-void TPM_PrintFour(const char *string, const unsigned char* buff)
-{
-    TPM_PrintFourLimit(string, buff, 4);
-}
-
-#endif
-
-/* TPM_PrintAll() prints 'string', the length, and then the entire byte array
- */
-
-void TPM_PrintAll(const char *string, const unsigned char* buff, uint32_t length)
-{
-    uint32_t i;
-    int indent;
-
-    if (buff != NULL) {
-        indent = TPMLIB_LogPrintf("%s length %u\n", string, length);
-        if (indent < 0)
-            return;
-
-        for (i = 0 ; i < length ; i++) {
-            if (i && !( i % 16 ))
-                TPMLIB_LogPrintfA(0, "\n");
-
-            if (!(i % 16))
-                TPMLIB_LogPrintf(" %.2X ", buff[i]);
-            else
-                TPMLIB_LogPrintfA(0, "%.2X ", buff[i]);
-        }
-        TPMLIB_LogPrintfA(0, "\n");
-    } else {
-        TPMLIB_LogPrintf("%s null\n", string);
-    }
-    return;
-}
diff --git a/src/tpm12/tpm_debug.h b/src/tpm12/tpm_debug.h
deleted file mode 100644 (file)
index a9671d6..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-/********************************************************************************/
-/*                                                                              */
-/*                         TPM Debug Utilities                                  */
-/*                           Written by Ken Goldman                             */
-/*                     IBM Thomas J. Watson Research Center                     */
-/*            $Id: tpm_debug.h 4179 2010-11-10 20:10:24Z kgoldman $             */
-/*                                                                              */
-/* (c) Copyright IBM Corporation 2006, 2010.                                   */
-/*                                                                             */
-/* All rights reserved.                                                                */
-/*                                                                             */
-/* Redistribution and use in source and binary forms, with or without          */
-/* modification, are permitted provided that the following conditions are      */
-/* met:                                                                                */
-/*                                                                             */
-/* Redistributions of source code must retain the above copyright notice,      */
-/* this list of conditions and the following disclaimer.                       */
-/*                                                                             */
-/* Redistributions in binary form must reproduce the above copyright           */
-/* notice, this list of conditions and the following disclaimer in the         */
-/* documentation and/or other materials provided with the distribution.                */
-/*                                                                             */
-/* Neither the names of the IBM Corporation nor the names of its               */
-/* contributors may be used to endorse or promote products derived from                */
-/* this software without specific prior written permission.                    */
-/*                                                                             */
-/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
-/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
-/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
-/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
-/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
-/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
-/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
-/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
-/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
-/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
-/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
-/********************************************************************************/
-
-#ifndef TPM_DEBUG_H
-#define TPM_DEBUG_H
-
-#include "tpm_types.h"
-#include "tpm_library_intern.h"
-
-/* prototypes */
-
-void TPM_PrintFour(const char *string, const unsigned char* buff);
-void TPM_PrintFourLimit(const char *string,
-                        const unsigned char* buff, size_t bufflen);
-void TPM_PrintAll(const char *string, const unsigned char* buff, uint32_t length);
-
-#if 0
-#ifndef TPM_DEBUG       /* if debug is turned off */
-
-/* dummy function to match the printf prototype */
-int tpm_swallow_printf_args(const char *format, ...);
-
-/* assign to this dummy value to eliminate "statement has no effect" warnings */
-extern int swallow_rc;
-
-/* redefine printf to null */
-#define printf swallow_rc = swallow_rc && tpm_swallow_printf_args
-#define TPM_PrintFour(arg1, arg2)
-
-#endif  /* TPM_DEBUG */
-#endif
-
-#define printf(...) TPMLIB_LogPrintf(__VA_ARGS__);
-
-#endif
diff --git a/src/tpm12/tpm_memory.c b/src/tpm12/tpm_memory.c
deleted file mode 100644 (file)
index 5bfa4f1..0000000
+++ /dev/null
@@ -1,131 +0,0 @@
-/********************************************************************************/
-/*                                                                              */
-/*                           TPM Memory Allocation                              */
-/*                           Written by Ken Goldman                             */
-/*                     IBM Thomas J. Watson Research Center                     */
-/*            $Id: tpm_memory.c 4609 2011-08-26 19:27:38Z kgoldman $            */
-/*                                                                              */
-/* (c) Copyright IBM Corporation 2006, 2010.                                   */
-/*                                                                             */
-/* All rights reserved.                                                                */
-/*                                                                             */
-/* Redistribution and use in source and binary forms, with or without          */
-/* modification, are permitted provided that the following conditions are      */
-/* met:                                                                                */
-/*                                                                             */
-/* Redistributions of source code must retain the above copyright notice,      */
-/* this list of conditions and the following disclaimer.                       */
-/*                                                                             */
-/* Redistributions in binary form must reproduce the above copyright           */
-/* notice, this list of conditions and the following disclaimer in the         */
-/* documentation and/or other materials provided with the distribution.                */
-/*                                                                             */
-/* Neither the names of the IBM Corporation nor the names of its               */
-/* contributors may be used to endorse or promote products derived from                */
-/* this software without specific prior written permission.                    */
-/*                                                                             */
-/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
-/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
-/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
-/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
-/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
-/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
-/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
-/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
-/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
-/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
-/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
-/********************************************************************************/
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "tpm_constants.h"
-#include "tpm_debug.h"
-#include "tpm_error.h"
-
-#include "tpm_memory.h"
-
-/* TPM_Malloc() is a general purpose wrapper around malloc()
- */
-
-TPM_RESULT TPM_Malloc(unsigned char **buffer, uint32_t size)
-{
-    TPM_RESULT          rc = 0;
-    
-    /* assertion test.  The coding style requires that all allocated pointers are initialized to
-       NULL.  A non-NULL value indicates either a missing initialization or a pointer reuse (a
-       memory leak). */
-    if (rc == 0) {
-        if (*buffer != NULL) {
-            printf("TPM_Malloc: Error (fatal), *buffer %p should be NULL before malloc\n", *buffer);
-            rc = TPM_FAIL;
-        }
-    }
-    /* verify that the size is not "too large" */
-    if (rc == 0) {
-        if (size > TPM_ALLOC_MAX) {
-            printf("TPM_Malloc: Error, size %u greater than maximum allowed\n", size);
-            rc = TPM_SIZE;
-        }       
-    }
-    /* verify that the size is not 0, this would be implementation defined and should never occur */
-    if (rc == 0) {
-        if (size == 0) {
-            printf("TPM_Malloc: Error (fatal), size is zero\n");
-            rc = TPM_FAIL;
-        }       
-    }
-    if (rc == 0) {
-        *buffer = malloc(size);
-        if (*buffer == NULL) {
-            printf("TPM_Malloc: Error allocating %u bytes\n", size);
-            rc = TPM_SIZE;
-        }
-    }
-    return rc;
-}
-
-/* TPM_Realloc() is a general purpose wrapper around realloc()
- */
-
-TPM_RESULT TPM_Realloc(unsigned char **buffer,
-                       uint32_t size)
-{
-    TPM_RESULT          rc = 0;
-    unsigned char       *tmpptr = NULL;
-    
-    /* verify that the size is not "too large" */
-    if (rc == 0) {
-        if (size > TPM_ALLOC_MAX) {
-            printf("TPM_Realloc: Error, size %u greater than maximum allowed\n", size);
-            rc = TPM_SIZE;
-        }       
-    }
-    if (rc == 0) {
-        tmpptr = realloc(*buffer, size);
-        if (tmpptr == NULL) {
-            printf("TPM_Realloc: Error reallocating %u bytes\n", size);
-            rc = TPM_SIZE;
-        }
-    }
-    if (rc == 0) {
-        *buffer = tmpptr;
-    }
-    return rc;
-}
-
-/* TPM_Free() is the companion to the TPM allocation functions.  It is not used internally.  The
-   intent is for use by an application that links directly to a TPM and wants to free memory
-   allocated by the TPM.
-
-   It avoids a potential problem if the application uses a different allocation library, perhaps one
-   that wraps the functions to detect overflows or memory leaks.
-*/
-
-void TPM_Free(unsigned char *buffer)
-{
-    free(buffer);
-    return;
-}
-
diff --git a/src/tpm12/tpm_nvfile.c b/src/tpm12/tpm_nvfile.c
deleted file mode 100644 (file)
index 0268bd0..0000000
+++ /dev/null
@@ -1,419 +0,0 @@
-/********************************************************************************/
-/*                                                                              */
-/*                      NVRAM File Abstraction Layer                            */
-/*                           Written by Ken Goldman                             */
-/*                     IBM Thomas J. Watson Research Center                     */
-/*            $Id: tpm_nvfile.c 4664 2012-01-03 22:15:08Z kgoldman $            */
-/*                                                                              */
-/* (c) Copyright IBM Corporation 2006, 2010.                                   */
-/*                                                                             */
-/* All rights reserved.                                                                */
-/*                                                                             */
-/* Redistribution and use in source and binary forms, with or without          */
-/* modification, are permitted provided that the following conditions are      */
-/* met:                                                                                */
-/*                                                                             */
-/* Redistributions of source code must retain the above copyright notice,      */
-/* this list of conditions and the following disclaimer.                       */
-/*                                                                             */
-/* Redistributions in binary form must reproduce the above copyright           */
-/* notice, this list of conditions and the following disclaimer in the         */
-/* documentation and/or other materials provided with the distribution.                */
-/*                                                                             */
-/* Neither the names of the IBM Corporation nor the names of its               */
-/* contributors may be used to endorse or promote products derived from                */
-/* this software without specific prior written permission.                    */
-/*                                                                             */
-/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
-/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
-/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
-/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
-/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
-/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
-/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
-/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
-/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
-/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
-/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
-/********************************************************************************/
-
-/* This module abstracts out all NVRAM read and write operations.
-
-   This implementation uses standard, portable C files.
-
-   The basic high level abstractions are:
-
-        TPM_NVRAM_LoadData();
-        TPM_NVRAM_StoreData();
-        TPM_NVRAM_DeleteName();
-
-   They take a 'name' that is mapped to a rooted file name.
-*/
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-
-#include "tpm_debug.h"
-#include "tpm_error.h"
-#include "tpm_memory.h"
-#include "tpm_nvram.h"
-
-#include "tpm_nvfile.h"
-
-#ifdef TPM_LIBTPMS_CALLBACKS
-#include "tpm_library_intern.h"
-#include "tpm_library.h"
-#endif
-
-
-/* local prototypes */
-
-static TPM_RESULT TPM_NVRAM_GetFilenameForName(char *filename,
-                                               size_t filename_len,
-                                              uint32_t tpm_number,
-                                               const char *name);
-
-
-/* A file name in NVRAM is composed of 3 parts:
-
-  1 - 'state_directory' is the rooted path to the TPM state home directory
-  2 = 'tpm_number' is the TPM instance, 00 for a single TPM
-  2 - the file name
-
-  For the IBM cryptographic coprocessor version, the root path is hard coded.
-  
-  For the Linux and Windows versions, the path comes from an environment variable.  This variable is
-  used once in TPM_NVRAM_Init().
-
-  One root path is used for all virtual TPM's, so it can be a static variable.
-*/
-
-char state_directory[FILENAME_MAX];
-
-/* TPM_NVRAM_Init() is called once at startup.  It does any NVRAM required initialization.
-
-   This function sets some static variables that are used by all TPM's.
-*/
-
-TPM_RESULT TPM_NVRAM_Init(void)
-{
-    TPM_RESULT  rc = 0;
-    char        *tpm_state_path;
-    size_t      length;
-
-#ifdef TPM_LIBTPMS_CALLBACKS
-    struct libtpms_callbacks *cbs = TPMLIB_GetCallbacks();
-
-    /* call user-provided function if available, otherwise execute
-       default behavior */
-    if (cbs->tpm_nvram_init) {
-        rc = cbs->tpm_nvram_init();
-        return rc;
-    }
-#endif
-
-    printf(" TPM_NVRAM_Init:\n");
-#ifdef TPM_NV_DISK
-    /* TPM_NV_DISK TPM emulation stores in local directory determined by environment variable. */
-    if (rc == 0) {
-        tpm_state_path = getenv("TPM_PATH");
-        if (tpm_state_path == NULL) {
-            printf("TPM_NVRAM_Init: Error (fatal), TPM_PATH environment variable not set\n");
-            rc = TPM_FAIL;
-        }
-    }
-#endif
-    /* check that the directory name plus a file name will not overflow FILENAME_MAX */
-    if (rc == 0) {
-        length = strlen(tpm_state_path);
-        if ((length + TPM_FILENAME_MAX) > FILENAME_MAX) {
-            printf("TPM_NVRAM_Init: Error (fatal), TPM state path name %s too large\n",
-                  tpm_state_path);
-            rc = TPM_FAIL;
-        }
-    }
-    if (rc == 0) {
-        strcpy(state_directory, tpm_state_path);
-        printf("TPM_NVRAM_Init: Rooted state path %s\n", state_directory);
-    }
-    return rc;
-}
-
-/* Load 'data' of 'length' from the 'name'.
-
-   'data' must be freed after use.
-   
-   Returns
-        0 on success.
-        TPM_RETRY and NULL,0 on non-existent file (non-fatal, first time start up)
-        TPM_FAIL on failure to load (fatal), since it should never occur
-*/
-
-TPM_RESULT TPM_NVRAM_LoadData(unsigned char **data,     /* freed by caller */
-                              uint32_t *length,
-                             uint32_t tpm_number,
-                              const char *name) 
-{
-    TPM_RESULT  rc = 0;
-    long        lrc;
-    size_t      src;
-    int         irc;
-    FILE        *file = NULL;
-    char        filename[FILENAME_MAX]; /* rooted file name from name */
-
-#ifdef TPM_LIBTPMS_CALLBACKS
-    struct libtpms_callbacks *cbs;
-    bool is_empty_buffer;
-
-    /* try to get state blob set with TPMLIB_SetState() */
-    GetCachedState(TPMLIB_NameToStateType(name), data, length, &is_empty_buffer);
-    if (is_empty_buffer)
-        return TPM_RETRY;
-    if (*data)
-        return TPM_SUCCESS;
-
-    cbs = TPMLIB_GetCallbacks();
-
-    /* call user-provided function if available, otherwise execute
-       default behavior */
-    if (cbs->tpm_nvram_loaddata) {
-        rc = cbs->tpm_nvram_loaddata(data, length, tpm_number, name);
-        return rc;
-    }
-#endif
-
-    printf(" TPM_NVRAM_LoadData: From file %s\n", name);
-    *data = NULL;
-    *length = 0;
-    /* open the file */
-    if (rc == 0) {
-        /* map name to the rooted filename */
-        rc = TPM_NVRAM_GetFilenameForName(filename, sizeof(filename),
-                                          tpm_number, name);
-    }
-    if (rc == 0) {
-        printf("  TPM_NVRAM_LoadData: Opening file %s\n", filename);
-        file = fopen(filename, "rb");                           /* closed @1 */
-        if (file == NULL) {     /* if failure, determine cause */
-            if (errno == ENOENT) {
-                printf("TPM_NVRAM_LoadData: No such file %s\n", filename);
-                rc = TPM_RETRY;         /* first time start up */
-            }
-            else {
-                printf("TPM_NVRAM_LoadData: Error (fatal) opening %s for read, %s\n",
-                       filename, strerror(errno));
-                rc = TPM_FAIL;
-            }
-        }
-    }
-    /* determine the file length */
-    if (rc == 0) {
-        irc = fseek(file, 0L, SEEK_END);        /* seek to end of file */
-        if (irc == -1L) {
-            printf("TPM_NVRAM_LoadData: Error (fatal) fseek'ing %s, %s\n",
-                   filename, strerror(errno));
-            rc = TPM_FAIL;
-        }
-    }
-    if (rc == 0) {
-        lrc = ftell(file);                      /* get position in the stream */
-        if (lrc == -1L) {
-            printf("TPM_NVRAM_LoadData: Error (fatal) ftell'ing %s, %s\n",
-                   filename, strerror(errno));
-            rc = TPM_FAIL;
-        }
-        else {
-            *length = (uint32_t)lrc;           /* save the length */
-        }
-    }
-    if (rc == 0) {
-        irc = fseek(file, 0L, SEEK_SET);        /* seek back to the beginning of the file */
-        if (irc == -1L) {
-            printf("TPM_NVRAM_LoadData: Error (fatal) fseek'ing %s, %s\n",
-                   filename, strerror(errno));
-            rc = TPM_FAIL;
-        }
-    }
-    /* allocate a buffer for the actual data */
-    if ((rc == 0) && *length != 0) {
-        printf(" TPM_NVRAM_LoadData: Reading %u bytes of data\n", *length);
-        rc = TPM_Malloc(data, *length);
-       if (rc != 0) {
-            printf("TPM_NVRAM_LoadData: Error (fatal) allocating %u bytes\n", *length);
-            rc = TPM_FAIL;
-       }
-    }
-    /* read the contents of the file into the data buffer */
-    if ((rc == 0) && *length != 0) {
-        src = fread(*data, 1, *length, file);
-        if (src != *length) {
-            printf("TPM_NVRAM_LoadData: Error (fatal), data read of %u only read %lu\n",
-                   *length, (unsigned long)src);
-            rc = TPM_FAIL;
-        }
-    }
-    /* close the file */
-    if (file != NULL) {
-        printf(" TPM_NVRAM_LoadData: Closing file %s\n", filename);
-        irc = fclose(file);             /* @1 */
-        if (irc != 0) {
-            printf("TPM_NVRAM_LoadData: Error (fatal) closing file %s\n", filename);
-            rc = TPM_FAIL;
-        }
-        else {
-            printf(" TPM_NVRAM_LoadData: Closed file %s\n", filename);
-        }
-    }
-    return rc;
-}
-
-/* TPM_NVRAM_StoreData stores 'data' of 'length' to the rooted 'filename'
-
-   Returns
-        0 on success
-        TPM_FAIL for other fatal errors
-*/
-
-TPM_RESULT TPM_NVRAM_StoreData(const unsigned char *data,
-                               uint32_t length,
-                              uint32_t tpm_number,
-                               const char *name)
-{
-    TPM_RESULT  rc = 0;
-    uint32_t      lrc;
-    int         irc;
-    FILE        *file = NULL;
-    char        filename[FILENAME_MAX]; /* rooted file name from name */
-
-#ifdef TPM_LIBTPMS_CALLBACKS
-    struct libtpms_callbacks *cbs = TPMLIB_GetCallbacks();
-
-    /* call user-provided function if available, otherwise execute
-       default behavior */
-    if (cbs->tpm_nvram_storedata) {
-        rc = cbs->tpm_nvram_storedata(data, length, tpm_number, name);
-        return rc;
-    }
-#endif
-
-    printf(" TPM_NVRAM_StoreData: To name %s\n", name);
-    if (rc == 0) {
-        /* map name to the rooted filename */
-        rc = TPM_NVRAM_GetFilenameForName(filename, sizeof(filename),
-                                          tpm_number, name);
-    }
-    if (rc == 0) {
-        /* open the file */
-        printf(" TPM_NVRAM_StoreData: Opening file %s\n", filename);
-        file = fopen(filename, "wb");                           /* closed @1 */
-        if (file == NULL) {
-            printf("TPM_NVRAM_StoreData: Error (fatal) opening %s for write failed, %s\n",
-                   filename, strerror(errno));
-            rc = TPM_FAIL;
-        }
-    }
-    /* write the data to the file */
-    if (rc == 0) {
-        printf("  TPM_NVRAM_StoreData: Writing %u bytes of data\n", length);
-        lrc = fwrite(data, 1, length, file);
-        if (lrc != length) {
-            printf("TPM_NVRAM_StoreData: Error (fatal), data write of %u only wrote %u\n",
-                   length, lrc);
-            rc = TPM_FAIL;
-        }
-    }
-    if (file != NULL) {
-        printf("  TPM_NVRAM_StoreData: Closing file %s\n", filename);
-        irc = fclose(file);             /* @1 */
-        if (irc != 0) {
-            printf("TPM_NVRAM_StoreData: Error (fatal) closing file\n");
-            rc = TPM_FAIL;
-        }
-        else {
-            printf("  TPM_NVRAM_StoreData: Closed file %s\n", filename);
-        }
-    }
-    return rc;
-}
-
-
-/* TPM_NVRAM_GetFilenameForName() constructs a rooted file name from the name.
-
-   The filename is of the form:
-
-   state_directory/tpm_number.name
-*/
-
-static TPM_RESULT TPM_NVRAM_GetFilenameForName(char *filename,        /* output: rooted filename */
-                                              size_t filename_len,
-                                              uint32_t tpm_number,
-                                               const char *name)      /* input: abstract name */
-{
-    int n;
-    TPM_RESULT rc = TPM_FAIL;
-
-    printf(" TPM_NVRAM_GetFilenameForName: For name %s\n", name);
-    n = snprintf(filename, filename_len,
-                 "%s/%02lx.%s", state_directory, (unsigned long)tpm_number,
-                 name);
-    if (n < 0) {
-        printf(" TPM_NVRAM_GetFilenameForName: Error (fatal), snprintf failed\n");
-    } else if ((size_t)n >= filename_len) {
-        printf(" TPM_NVRAM_GetFilenameForName: Error (fatal), buffer too small\n");
-    } else {
-        printf("  TPM_NVRAM_GetFilenameForName: File name %s\n", filename);
-        rc = TPM_SUCCESS;
-    }
-    return rc;
-}
-
-/* TPM_NVRAM_DeleteName() deletes the 'name' from NVRAM
-
-   Returns:
-        0 on success, or if the file does not exist and mustExist is FALSE
-        TPM_FAIL if the file could not be removed, since this should never occur and there is
-               no recovery
-
-   NOTE: Not portable code, but supported by Linux and Windows
-*/
-
-TPM_RESULT TPM_NVRAM_DeleteName(uint32_t tpm_number,
-                               const char *name,
-                                TPM_BOOL mustExist)
-{
-    TPM_RESULT  rc = 0;
-    int         irc;
-    char        filename[FILENAME_MAX]; /* rooted file name from name */
-
-#ifdef TPM_LIBTPMS_CALLBACKS
-    struct libtpms_callbacks *cbs = TPMLIB_GetCallbacks();
-
-    /* call user-provided function if available, otherwise execute
-       default behavior */
-    if (cbs->tpm_nvram_deletename) {
-        rc = cbs->tpm_nvram_deletename(tpm_number, name, mustExist);
-        return rc;
-    }
-#endif
-    
-    printf(" TPM_NVRAM_DeleteName: Name %s\n", name);
-    /* map name to the rooted filename */
-    if (rc == 0) {
-        rc = TPM_NVRAM_GetFilenameForName(filename, sizeof(filename),
-                                          tpm_number, name);
-    }
-    if (rc == 0) {
-        irc = remove(filename);
-        if ((irc != 0) &&               /* if the remove failed */
-            (mustExist ||               /* if any error is a failure, or */
-             (errno != ENOENT))) {      /* if error other than no such file */
-            printf("TPM_NVRAM_DeleteName: Error, (fatal) file remove failed, errno %d\n",
-                   errno);
-            rc = TPM_FAIL;
-        }
-    }
-    return rc;
-}
-
diff --git a/src/tpm12/tpm_nvfile.h b/src/tpm12/tpm_nvfile.h
deleted file mode 100644 (file)
index 69b5823..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-/********************************************************************************/
-/*                                                                              */
-/*                              NVRAM Utilities                                 */
-/*                           Written by Ken Goldman                             */
-/*                     IBM Thomas J. Watson Research Center                     */
-/*            $Id: tpm_nvfile.h 4623 2011-09-28 15:15:09Z kgoldman $            */
-/*                                                                              */
-/* (c) Copyright IBM Corporation 2006, 2010.                                   */
-/*                                                                             */
-/* All rights reserved.                                                                */
-/*                                                                             */
-/* Redistribution and use in source and binary forms, with or without          */
-/* modification, are permitted provided that the following conditions are      */
-/* met:                                                                                */
-/*                                                                             */
-/* Redistributions of source code must retain the above copyright notice,      */
-/* this list of conditions and the following disclaimer.                       */
-/*                                                                             */
-/* Redistributions in binary form must reproduce the above copyright           */
-/* notice, this list of conditions and the following disclaimer in the         */
-/* documentation and/or other materials provided with the distribution.                */
-/*                                                                             */
-/* Neither the names of the IBM Corporation nor the names of its               */
-/* contributors may be used to endorse or promote products derived from                */
-/* this software without specific prior written permission.                    */
-/*                                                                             */
-/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
-/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
-/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
-/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
-/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
-/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
-/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
-/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
-/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
-/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
-/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
-/********************************************************************************/
-
-#ifndef TPM_NVFILE_H
-#define TPM_NVFILE_H
-
-#include "tpm_types.h"
-
-/* characters in the TPM base file name, 14 for file name, slash, NUL terminator, etc.
-
-   This macro is used once during initialization to ensure that the TPM_PATH environment variable
-   length will not cause the rooted file name to overflow file name buffers.
-*/
-
-#define TPM_FILENAME_MAX 20
-
-TPM_RESULT TPM_NVRAM_Init(void);
-
-/*
-  Basic abstraction for read and write
-*/
-
-TPM_RESULT TPM_NVRAM_LoadData(unsigned char **data,
-                              uint32_t *length,
-                             uint32_t tpm_number,
-                              const char *name);
-TPM_RESULT TPM_NVRAM_StoreData(const unsigned char *data,
-                               uint32_t length,
-                              uint32_t tpm_number,
-                               const char *name);
-TPM_RESULT TPM_NVRAM_DeleteName(uint32_t tpm_number,
-                               const char *name,
-                                TPM_BOOL mustExist);
-
-#endif
index 4f44d8cda32db09a205f70477a600ff3244b9d86..155212642c4a6e2ffaa3bf1c2bed90b94e69ae83 100644 (file)
@@ -42,6 +42,7 @@
 
 #include <limits.h>
 #include "tpm_constants.h"
+#include "tpm_memory.h"
 #include "tpm_types.h"
 #include "tpm_nvram_const.h"
 
diff --git a/src/tpm_debug.c b/src/tpm_debug.c
new file mode 100644 (file)
index 0000000..7c1efdf
--- /dev/null
@@ -0,0 +1,134 @@
+/********************************************************************************/
+/*                                                                              */
+/*                         TPM Debug Utilities                                  */
+/*                           Written by Ken Goldman                             */
+/*                     IBM Thomas J. Watson Research Center                     */
+/*            $Id: tpm_debug.c 4179 2010-11-10 20:10:24Z kgoldman $             */
+/*                                                                              */
+/* (c) Copyright IBM Corporation 2006, 2010.                                   */
+/*                                                                             */
+/* All rights reserved.                                                                */
+/*                                                                             */
+/* Redistribution and use in source and binary forms, with or without          */
+/* modification, are permitted provided that the following conditions are      */
+/* met:                                                                                */
+/*                                                                             */
+/* Redistributions of source code must retain the above copyright notice,      */
+/* this list of conditions and the following disclaimer.                       */
+/*                                                                             */
+/* Redistributions in binary form must reproduce the above copyright           */
+/* notice, this list of conditions and the following disclaimer in the         */
+/* documentation and/or other materials provided with the distribution.                */
+/*                                                                             */
+/* Neither the names of the IBM Corporation nor the names of its               */
+/* contributors may be used to endorse or promote products derived from                */
+/* this software without specific prior written permission.                    */
+/*                                                                             */
+/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
+/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
+/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
+/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
+/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
+/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
+/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
+/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
+/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
+/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
+/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
+/********************************************************************************/
+
+#include <stdio.h>
+
+#include "tpm_debug.h"
+#undef printf
+
+#if 0
+
+int swallow_rc = 0;
+
+int tpm_swallow_printf_args(const char *format, ...)
+{
+    format = format;   /* to silence compiler */
+    return 0;
+}
+
+#else
+
+void TPM_PrintFourLimit(const char *string,
+                        const unsigned char *buff, size_t buflen)
+{
+    if (buff != NULL) {
+        switch (buflen) {
+        case 0:
+            TPMLIB_LogPrintf("%s (no data)\n", string);
+            break;
+        case 1:
+            TPMLIB_LogPrintf("%s %02x\n",
+                   string,
+                   buff[0]);
+            break;
+        case 2:
+            TPMLIB_LogPrintf("%s %02x %02x\n",
+                   string,
+                   buff[0],
+                   buff[1]);
+            break;
+        case 3:
+            TPMLIB_LogPrintf("%s %02x %02x %02x\n",
+                   string,
+                   buff[0],
+                   buff[1],
+                   buff[2]);
+            break;
+        default:
+            TPMLIB_LogPrintf("%s %02x %02x %02x %02x\n",
+                   string,
+                   buff[0],
+                   buff[1],
+                   buff[2],
+                   buff[3]);
+        }
+    }
+    else {
+        TPMLIB_LogPrintf("%s null\n", string);
+    }
+    return;
+}
+
+/* TPM_PrintFour() prints a prefix plus 4 bytes of a buffer */
+
+void TPM_PrintFour(const char *string, const unsigned char* buff)
+{
+    TPM_PrintFourLimit(string, buff, 4);
+}
+
+#endif
+
+/* TPM_PrintAll() prints 'string', the length, and then the entire byte array
+ */
+
+void TPM_PrintAll(const char *string, const unsigned char* buff, uint32_t length)
+{
+    uint32_t i;
+    int indent;
+
+    if (buff != NULL) {
+        indent = TPMLIB_LogPrintf("%s length %u\n", string, length);
+        if (indent < 0)
+            return;
+
+        for (i = 0 ; i < length ; i++) {
+            if (i && !( i % 16 ))
+                TPMLIB_LogPrintfA(0, "\n");
+
+            if (!(i % 16))
+                TPMLIB_LogPrintf(" %.2X ", buff[i]);
+            else
+                TPMLIB_LogPrintfA(0, "%.2X ", buff[i]);
+        }
+        TPMLIB_LogPrintfA(0, "\n");
+    } else {
+        TPMLIB_LogPrintf("%s null\n", string);
+    }
+    return;
+}
diff --git a/src/tpm_debug.h b/src/tpm_debug.h
new file mode 100644 (file)
index 0000000..a9671d6
--- /dev/null
@@ -0,0 +1,71 @@
+/********************************************************************************/
+/*                                                                              */
+/*                         TPM Debug Utilities                                  */
+/*                           Written by Ken Goldman                             */
+/*                     IBM Thomas J. Watson Research Center                     */
+/*            $Id: tpm_debug.h 4179 2010-11-10 20:10:24Z kgoldman $             */
+/*                                                                              */
+/* (c) Copyright IBM Corporation 2006, 2010.                                   */
+/*                                                                             */
+/* All rights reserved.                                                                */
+/*                                                                             */
+/* Redistribution and use in source and binary forms, with or without          */
+/* modification, are permitted provided that the following conditions are      */
+/* met:                                                                                */
+/*                                                                             */
+/* Redistributions of source code must retain the above copyright notice,      */
+/* this list of conditions and the following disclaimer.                       */
+/*                                                                             */
+/* Redistributions in binary form must reproduce the above copyright           */
+/* notice, this list of conditions and the following disclaimer in the         */
+/* documentation and/or other materials provided with the distribution.                */
+/*                                                                             */
+/* Neither the names of the IBM Corporation nor the names of its               */
+/* contributors may be used to endorse or promote products derived from                */
+/* this software without specific prior written permission.                    */
+/*                                                                             */
+/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
+/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
+/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
+/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
+/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
+/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
+/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
+/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
+/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
+/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
+/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
+/********************************************************************************/
+
+#ifndef TPM_DEBUG_H
+#define TPM_DEBUG_H
+
+#include "tpm_types.h"
+#include "tpm_library_intern.h"
+
+/* prototypes */
+
+void TPM_PrintFour(const char *string, const unsigned char* buff);
+void TPM_PrintFourLimit(const char *string,
+                        const unsigned char* buff, size_t bufflen);
+void TPM_PrintAll(const char *string, const unsigned char* buff, uint32_t length);
+
+#if 0
+#ifndef TPM_DEBUG       /* if debug is turned off */
+
+/* dummy function to match the printf prototype */
+int tpm_swallow_printf_args(const char *format, ...);
+
+/* assign to this dummy value to eliminate "statement has no effect" warnings */
+extern int swallow_rc;
+
+/* redefine printf to null */
+#define printf swallow_rc = swallow_rc && tpm_swallow_printf_args
+#define TPM_PrintFour(arg1, arg2)
+
+#endif  /* TPM_DEBUG */
+#endif
+
+#define printf(...) TPMLIB_LogPrintf(__VA_ARGS__);
+
+#endif
index 2cffd3aa91b0a389b02e6c04c5efce6d1d24a386..460cd59c0fec08e4c5eb124a6678e119702828ad 100644 (file)
@@ -60,7 +60,7 @@
 # include <openssl/evp.h>
 #endif
 
-#include "tpm12/tpm_debug.h"
+#include "tpm_debug.h"
 #include "tpm_error.h"
 #include "tpm_library.h"
 #include "tpm_library_intern.h"
diff --git a/src/tpm_memory.c b/src/tpm_memory.c
new file mode 100644 (file)
index 0000000..ce78a3e
--- /dev/null
@@ -0,0 +1,130 @@
+/********************************************************************************/
+/*                                                                              */
+/*                           TPM Memory Allocation                              */
+/*                           Written by Ken Goldman                             */
+/*                     IBM Thomas J. Watson Research Center                     */
+/*            $Id: tpm_memory.c 4609 2011-08-26 19:27:38Z kgoldman $            */
+/*                                                                              */
+/* (c) Copyright IBM Corporation 2006, 2010.                                   */
+/*                                                                             */
+/* All rights reserved.                                                                */
+/*                                                                             */
+/* Redistribution and use in source and binary forms, with or without          */
+/* modification, are permitted provided that the following conditions are      */
+/* met:                                                                                */
+/*                                                                             */
+/* Redistributions of source code must retain the above copyright notice,      */
+/* this list of conditions and the following disclaimer.                       */
+/*                                                                             */
+/* Redistributions in binary form must reproduce the above copyright           */
+/* notice, this list of conditions and the following disclaimer in the         */
+/* documentation and/or other materials provided with the distribution.                */
+/*                                                                             */
+/* Neither the names of the IBM Corporation nor the names of its               */
+/* contributors may be used to endorse or promote products derived from                */
+/* this software without specific prior written permission.                    */
+/*                                                                             */
+/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
+/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
+/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
+/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
+/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
+/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
+/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
+/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
+/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
+/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
+/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
+/********************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "tpm_debug.h"
+#include "tpm_error.h"
+
+#include "tpm_memory.h"
+
+/* TPM_Malloc() is a general purpose wrapper around malloc()
+ */
+
+TPM_RESULT TPM_Malloc(unsigned char **buffer, uint32_t size)
+{
+    TPM_RESULT          rc = 0;
+    
+    /* assertion test.  The coding style requires that all allocated pointers are initialized to
+       NULL.  A non-NULL value indicates either a missing initialization or a pointer reuse (a
+       memory leak). */
+    if (rc == 0) {
+        if (*buffer != NULL) {
+            printf("TPM_Malloc: Error (fatal), *buffer %p should be NULL before malloc\n", *buffer);
+            rc = TPM_FAIL;
+        }
+    }
+    /* verify that the size is not "too large" */
+    if (rc == 0) {
+        if (size > TPM_ALLOC_MAX) {
+            printf("TPM_Malloc: Error, size %u greater than maximum allowed\n", size);
+            rc = TPM_SIZE;
+        }       
+    }
+    /* verify that the size is not 0, this would be implementation defined and should never occur */
+    if (rc == 0) {
+        if (size == 0) {
+            printf("TPM_Malloc: Error (fatal), size is zero\n");
+            rc = TPM_FAIL;
+        }       
+    }
+    if (rc == 0) {
+        *buffer = malloc(size);
+        if (*buffer == NULL) {
+            printf("TPM_Malloc: Error allocating %u bytes\n", size);
+            rc = TPM_SIZE;
+        }
+    }
+    return rc;
+}
+
+/* TPM_Realloc() is a general purpose wrapper around realloc()
+ */
+
+TPM_RESULT TPM_Realloc(unsigned char **buffer,
+                       uint32_t size)
+{
+    TPM_RESULT          rc = 0;
+    unsigned char       *tmpptr = NULL;
+    
+    /* verify that the size is not "too large" */
+    if (rc == 0) {
+        if (size > TPM_ALLOC_MAX) {
+            printf("TPM_Realloc: Error, size %u greater than maximum allowed\n", size);
+            rc = TPM_SIZE;
+        }       
+    }
+    if (rc == 0) {
+        tmpptr = realloc(*buffer, size);
+        if (tmpptr == NULL) {
+            printf("TPM_Realloc: Error reallocating %u bytes\n", size);
+            rc = TPM_SIZE;
+        }
+    }
+    if (rc == 0) {
+        *buffer = tmpptr;
+    }
+    return rc;
+}
+
+/* TPM_Free() is the companion to the TPM allocation functions.  It is not used internally.  The
+   intent is for use by an application that links directly to a TPM and wants to free memory
+   allocated by the TPM.
+
+   It avoids a potential problem if the application uses a different allocation library, perhaps one
+   that wraps the functions to detect overflows or memory leaks.
+*/
+
+void TPM_Free(unsigned char *buffer)
+{
+    free(buffer);
+    return;
+}
+
diff --git a/src/tpm_nvfile.c b/src/tpm_nvfile.c
new file mode 100644 (file)
index 0000000..d9049e8
--- /dev/null
@@ -0,0 +1,418 @@
+/********************************************************************************/
+/*                                                                              */
+/*                      NVRAM File Abstraction Layer                            */
+/*                           Written by Ken Goldman                             */
+/*                     IBM Thomas J. Watson Research Center                     */
+/*            $Id: tpm_nvfile.c 4664 2012-01-03 22:15:08Z kgoldman $            */
+/*                                                                              */
+/* (c) Copyright IBM Corporation 2006, 2010.                                   */
+/*                                                                             */
+/* All rights reserved.                                                                */
+/*                                                                             */
+/* Redistribution and use in source and binary forms, with or without          */
+/* modification, are permitted provided that the following conditions are      */
+/* met:                                                                                */
+/*                                                                             */
+/* Redistributions of source code must retain the above copyright notice,      */
+/* this list of conditions and the following disclaimer.                       */
+/*                                                                             */
+/* Redistributions in binary form must reproduce the above copyright           */
+/* notice, this list of conditions and the following disclaimer in the         */
+/* documentation and/or other materials provided with the distribution.                */
+/*                                                                             */
+/* Neither the names of the IBM Corporation nor the names of its               */
+/* contributors may be used to endorse or promote products derived from                */
+/* this software without specific prior written permission.                    */
+/*                                                                             */
+/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
+/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
+/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
+/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
+/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
+/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
+/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
+/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
+/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
+/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
+/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
+/********************************************************************************/
+
+/* This module abstracts out all NVRAM read and write operations.
+
+   This implementation uses standard, portable C files.
+
+   The basic high level abstractions are:
+
+        TPM_NVRAM_LoadData();
+        TPM_NVRAM_StoreData();
+        TPM_NVRAM_DeleteName();
+
+   They take a 'name' that is mapped to a rooted file name.
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include "tpm_debug.h"
+#include "tpm_error.h"
+#include "tpm_memory.h"
+
+#include "tpm_nvfile.h"
+
+#ifdef TPM_LIBTPMS_CALLBACKS
+#include "tpm_library_intern.h"
+#include "tpm_library.h"
+#endif
+
+
+/* local prototypes */
+
+static TPM_RESULT TPM_NVRAM_GetFilenameForName(char *filename,
+                                               size_t filename_len,
+                                              uint32_t tpm_number,
+                                               const char *name);
+
+
+/* A file name in NVRAM is composed of 3 parts:
+
+  1 - 'state_directory' is the rooted path to the TPM state home directory
+  2 = 'tpm_number' is the TPM instance, 00 for a single TPM
+  2 - the file name
+
+  For the IBM cryptographic coprocessor version, the root path is hard coded.
+  
+  For the Linux and Windows versions, the path comes from an environment variable.  This variable is
+  used once in TPM_NVRAM_Init().
+
+  One root path is used for all virtual TPM's, so it can be a static variable.
+*/
+
+char state_directory[FILENAME_MAX];
+
+/* TPM_NVRAM_Init() is called once at startup.  It does any NVRAM required initialization.
+
+   This function sets some static variables that are used by all TPM's.
+*/
+
+TPM_RESULT TPM_NVRAM_Init(void)
+{
+    TPM_RESULT  rc = 0;
+    char        *tpm_state_path = NULL;
+    size_t      length;
+
+#ifdef TPM_LIBTPMS_CALLBACKS
+    struct libtpms_callbacks *cbs = TPMLIB_GetCallbacks();
+
+    /* call user-provided function if available, otherwise execute
+       default behavior */
+    if (cbs->tpm_nvram_init) {
+        rc = cbs->tpm_nvram_init();
+        return rc;
+    }
+#endif
+
+    printf(" TPM_NVRAM_Init:\n");
+#ifdef TPM_NV_DISK
+    /* TPM_NV_DISK TPM emulation stores in local directory determined by environment variable. */
+    if (rc == 0) {
+        tpm_state_path = getenv("TPM_PATH");
+        if (tpm_state_path == NULL) {
+            printf("TPM_NVRAM_Init: Error (fatal), TPM_PATH environment variable not set\n");
+            rc = TPM_FAIL;
+        }
+    }
+#endif
+    /* check that the directory name plus a file name will not overflow FILENAME_MAX */
+    if (rc == 0) {
+        length = strlen(tpm_state_path);
+        if ((length + TPM_FILENAME_MAX) > FILENAME_MAX) {
+            printf("TPM_NVRAM_Init: Error (fatal), TPM state path name %s too large\n",
+                  tpm_state_path);
+            rc = TPM_FAIL;
+        }
+    }
+    if (rc == 0) {
+        strcpy(state_directory, tpm_state_path);
+        printf("TPM_NVRAM_Init: Rooted state path %s\n", state_directory);
+    }
+    return rc;
+}
+
+/* Load 'data' of 'length' from the 'name'.
+
+   'data' must be freed after use.
+   
+   Returns
+        0 on success.
+        TPM_RETRY and NULL,0 on non-existent file (non-fatal, first time start up)
+        TPM_FAIL on failure to load (fatal), since it should never occur
+*/
+
+TPM_RESULT TPM_NVRAM_LoadData(unsigned char **data,     /* freed by caller */
+                              uint32_t *length,
+                             uint32_t tpm_number,
+                              const char *name) 
+{
+    TPM_RESULT  rc = 0;
+    long        lrc;
+    size_t      src;
+    int         irc;
+    FILE        *file = NULL;
+    char        filename[FILENAME_MAX]; /* rooted file name from name */
+
+#ifdef TPM_LIBTPMS_CALLBACKS
+    struct libtpms_callbacks *cbs;
+    bool is_empty_buffer;
+
+    /* try to get state blob set with TPMLIB_SetState() */
+    GetCachedState(TPMLIB_NameToStateType(name), data, length, &is_empty_buffer);
+    if (is_empty_buffer)
+        return TPM_RETRY;
+    if (*data)
+        return TPM_SUCCESS;
+
+    cbs = TPMLIB_GetCallbacks();
+
+    /* call user-provided function if available, otherwise execute
+       default behavior */
+    if (cbs->tpm_nvram_loaddata) {
+        rc = cbs->tpm_nvram_loaddata(data, length, tpm_number, name);
+        return rc;
+    }
+#endif
+
+    printf(" TPM_NVRAM_LoadData: From file %s\n", name);
+    *data = NULL;
+    *length = 0;
+    /* open the file */
+    if (rc == 0) {
+        /* map name to the rooted filename */
+        rc = TPM_NVRAM_GetFilenameForName(filename, sizeof(filename),
+                                          tpm_number, name);
+    }
+    if (rc == 0) {
+        printf("  TPM_NVRAM_LoadData: Opening file %s\n", filename);
+        file = fopen(filename, "rb");                           /* closed @1 */
+        if (file == NULL) {     /* if failure, determine cause */
+            if (errno == ENOENT) {
+                printf("TPM_NVRAM_LoadData: No such file %s\n", filename);
+                rc = TPM_RETRY;         /* first time start up */
+            }
+            else {
+                printf("TPM_NVRAM_LoadData: Error (fatal) opening %s for read, %s\n",
+                       filename, strerror(errno));
+                rc = TPM_FAIL;
+            }
+        }
+    }
+    /* determine the file length */
+    if (rc == 0) {
+        irc = fseek(file, 0L, SEEK_END);        /* seek to end of file */
+        if (irc == -1L) {
+            printf("TPM_NVRAM_LoadData: Error (fatal) fseek'ing %s, %s\n",
+                   filename, strerror(errno));
+            rc = TPM_FAIL;
+        }
+    }
+    if (rc == 0) {
+        lrc = ftell(file);                      /* get position in the stream */
+        if (lrc == -1L) {
+            printf("TPM_NVRAM_LoadData: Error (fatal) ftell'ing %s, %s\n",
+                   filename, strerror(errno));
+            rc = TPM_FAIL;
+        }
+        else {
+            *length = (uint32_t)lrc;           /* save the length */
+        }
+    }
+    if (rc == 0) {
+        irc = fseek(file, 0L, SEEK_SET);        /* seek back to the beginning of the file */
+        if (irc == -1L) {
+            printf("TPM_NVRAM_LoadData: Error (fatal) fseek'ing %s, %s\n",
+                   filename, strerror(errno));
+            rc = TPM_FAIL;
+        }
+    }
+    /* allocate a buffer for the actual data */
+    if ((rc == 0) && *length != 0) {
+        printf(" TPM_NVRAM_LoadData: Reading %u bytes of data\n", *length);
+        rc = TPM_Malloc(data, *length);
+       if (rc != 0) {
+            printf("TPM_NVRAM_LoadData: Error (fatal) allocating %u bytes\n", *length);
+            rc = TPM_FAIL;
+       }
+    }
+    /* read the contents of the file into the data buffer */
+    if ((rc == 0) && *length != 0) {
+        src = fread(*data, 1, *length, file);
+        if (src != *length) {
+            printf("TPM_NVRAM_LoadData: Error (fatal), data read of %u only read %lu\n",
+                   *length, (unsigned long)src);
+            rc = TPM_FAIL;
+        }
+    }
+    /* close the file */
+    if (file != NULL) {
+        printf(" TPM_NVRAM_LoadData: Closing file %s\n", filename);
+        irc = fclose(file);             /* @1 */
+        if (irc != 0) {
+            printf("TPM_NVRAM_LoadData: Error (fatal) closing file %s\n", filename);
+            rc = TPM_FAIL;
+        }
+        else {
+            printf(" TPM_NVRAM_LoadData: Closed file %s\n", filename);
+        }
+    }
+    return rc;
+}
+
+/* TPM_NVRAM_StoreData stores 'data' of 'length' to the rooted 'filename'
+
+   Returns
+        0 on success
+        TPM_FAIL for other fatal errors
+*/
+
+TPM_RESULT TPM_NVRAM_StoreData(const unsigned char *data,
+                               uint32_t length,
+                              uint32_t tpm_number,
+                               const char *name)
+{
+    TPM_RESULT  rc = 0;
+    uint32_t      lrc;
+    int         irc;
+    FILE        *file = NULL;
+    char        filename[FILENAME_MAX]; /* rooted file name from name */
+
+#ifdef TPM_LIBTPMS_CALLBACKS
+    struct libtpms_callbacks *cbs = TPMLIB_GetCallbacks();
+
+    /* call user-provided function if available, otherwise execute
+       default behavior */
+    if (cbs->tpm_nvram_storedata) {
+        rc = cbs->tpm_nvram_storedata(data, length, tpm_number, name);
+        return rc;
+    }
+#endif
+
+    printf(" TPM_NVRAM_StoreData: To name %s\n", name);
+    if (rc == 0) {
+        /* map name to the rooted filename */
+        rc = TPM_NVRAM_GetFilenameForName(filename, sizeof(filename),
+                                          tpm_number, name);
+    }
+    if (rc == 0) {
+        /* open the file */
+        printf(" TPM_NVRAM_StoreData: Opening file %s\n", filename);
+        file = fopen(filename, "wb");                           /* closed @1 */
+        if (file == NULL) {
+            printf("TPM_NVRAM_StoreData: Error (fatal) opening %s for write failed, %s\n",
+                   filename, strerror(errno));
+            rc = TPM_FAIL;
+        }
+    }
+    /* write the data to the file */
+    if (rc == 0) {
+        printf("  TPM_NVRAM_StoreData: Writing %u bytes of data\n", length);
+        lrc = fwrite(data, 1, length, file);
+        if (lrc != length) {
+            printf("TPM_NVRAM_StoreData: Error (fatal), data write of %u only wrote %u\n",
+                   length, lrc);
+            rc = TPM_FAIL;
+        }
+    }
+    if (file != NULL) {
+        printf("  TPM_NVRAM_StoreData: Closing file %s\n", filename);
+        irc = fclose(file);             /* @1 */
+        if (irc != 0) {
+            printf("TPM_NVRAM_StoreData: Error (fatal) closing file\n");
+            rc = TPM_FAIL;
+        }
+        else {
+            printf("  TPM_NVRAM_StoreData: Closed file %s\n", filename);
+        }
+    }
+    return rc;
+}
+
+
+/* TPM_NVRAM_GetFilenameForName() constructs a rooted file name from the name.
+
+   The filename is of the form:
+
+   state_directory/tpm_number.name
+*/
+
+static TPM_RESULT TPM_NVRAM_GetFilenameForName(char *filename,        /* output: rooted filename */
+                                              size_t filename_len,
+                                              uint32_t tpm_number,
+                                               const char *name)      /* input: abstract name */
+{
+    int n;
+    TPM_RESULT rc = TPM_FAIL;
+
+    printf(" TPM_NVRAM_GetFilenameForName: For name %s\n", name);
+    n = snprintf(filename, filename_len,
+                 "%s/%02lx.%s", state_directory, (unsigned long)tpm_number,
+                 name);
+    if (n < 0) {
+        printf(" TPM_NVRAM_GetFilenameForName: Error (fatal), snprintf failed\n");
+    } else if ((size_t)n >= filename_len) {
+        printf(" TPM_NVRAM_GetFilenameForName: Error (fatal), buffer too small\n");
+    } else {
+        printf("  TPM_NVRAM_GetFilenameForName: File name %s\n", filename);
+        rc = TPM_SUCCESS;
+    }
+    return rc;
+}
+
+/* TPM_NVRAM_DeleteName() deletes the 'name' from NVRAM
+
+   Returns:
+        0 on success, or if the file does not exist and mustExist is FALSE
+        TPM_FAIL if the file could not be removed, since this should never occur and there is
+               no recovery
+
+   NOTE: Not portable code, but supported by Linux and Windows
+*/
+
+TPM_RESULT TPM_NVRAM_DeleteName(uint32_t tpm_number,
+                               const char *name,
+                                TPM_BOOL mustExist)
+{
+    TPM_RESULT  rc = 0;
+    int         irc;
+    char        filename[FILENAME_MAX]; /* rooted file name from name */
+
+#ifdef TPM_LIBTPMS_CALLBACKS
+    struct libtpms_callbacks *cbs = TPMLIB_GetCallbacks();
+
+    /* call user-provided function if available, otherwise execute
+       default behavior */
+    if (cbs->tpm_nvram_deletename) {
+        rc = cbs->tpm_nvram_deletename(tpm_number, name, mustExist);
+        return rc;
+    }
+#endif
+    
+    printf(" TPM_NVRAM_DeleteName: Name %s\n", name);
+    /* map name to the rooted filename */
+    if (rc == 0) {
+        rc = TPM_NVRAM_GetFilenameForName(filename, sizeof(filename),
+                                          tpm_number, name);
+    }
+    if (rc == 0) {
+        irc = remove(filename);
+        if ((irc != 0) &&               /* if the remove failed */
+            (mustExist ||               /* if any error is a failure, or */
+             (errno != ENOENT))) {      /* if error other than no such file */
+            printf("TPM_NVRAM_DeleteName: Error, (fatal) file remove failed, errno %d\n",
+                   errno);
+            rc = TPM_FAIL;
+        }
+    }
+    return rc;
+}
+
diff --git a/src/tpm_nvfile.h b/src/tpm_nvfile.h
new file mode 100644 (file)
index 0000000..69b5823
--- /dev/null
@@ -0,0 +1,71 @@
+/********************************************************************************/
+/*                                                                              */
+/*                              NVRAM Utilities                                 */
+/*                           Written by Ken Goldman                             */
+/*                     IBM Thomas J. Watson Research Center                     */
+/*            $Id: tpm_nvfile.h 4623 2011-09-28 15:15:09Z kgoldman $            */
+/*                                                                              */
+/* (c) Copyright IBM Corporation 2006, 2010.                                   */
+/*                                                                             */
+/* All rights reserved.                                                                */
+/*                                                                             */
+/* Redistribution and use in source and binary forms, with or without          */
+/* modification, are permitted provided that the following conditions are      */
+/* met:                                                                                */
+/*                                                                             */
+/* Redistributions of source code must retain the above copyright notice,      */
+/* this list of conditions and the following disclaimer.                       */
+/*                                                                             */
+/* Redistributions in binary form must reproduce the above copyright           */
+/* notice, this list of conditions and the following disclaimer in the         */
+/* documentation and/or other materials provided with the distribution.                */
+/*                                                                             */
+/* Neither the names of the IBM Corporation nor the names of its               */
+/* contributors may be used to endorse or promote products derived from                */
+/* this software without specific prior written permission.                    */
+/*                                                                             */
+/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS         */
+/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT           */
+/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR       */
+/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
+/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,      */
+/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT            */
+/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,       */
+/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY       */
+/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         */
+/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       */
+/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
+/********************************************************************************/
+
+#ifndef TPM_NVFILE_H
+#define TPM_NVFILE_H
+
+#include "tpm_types.h"
+
+/* characters in the TPM base file name, 14 for file name, slash, NUL terminator, etc.
+
+   This macro is used once during initialization to ensure that the TPM_PATH environment variable
+   length will not cause the rooted file name to overflow file name buffers.
+*/
+
+#define TPM_FILENAME_MAX 20
+
+TPM_RESULT TPM_NVRAM_Init(void);
+
+/*
+  Basic abstraction for read and write
+*/
+
+TPM_RESULT TPM_NVRAM_LoadData(unsigned char **data,
+                              uint32_t *length,
+                             uint32_t tpm_number,
+                              const char *name);
+TPM_RESULT TPM_NVRAM_StoreData(const unsigned char *data,
+                               uint32_t length,
+                              uint32_t tpm_number,
+                               const char *name);
+TPM_RESULT TPM_NVRAM_DeleteName(uint32_t tpm_number,
+                               const char *name,
+                                TPM_BOOL mustExist);
+
+#endif
index 7252fb1434359b80bc9c78f91a192c7ffbd4c67c..6e3cde6e86e07761a6c54207401ea0f02f8dcba0 100644 (file)
@@ -45,7 +45,7 @@
 #include <string.h>
 #include <stdbool.h>
 
-#include "tpm12/tpm_debug.h"
+#include "tpm_debug.h"
 #include "tpm_error.h"
 #include "tpm12/tpm_init.h"
 #include "tpm_library_intern.h"
@@ -53,7 +53,7 @@
 #include "tpm12/tpm_startup.h"
 #include "tpm12/tpm_global.h"
 #include "tpm12/tpm_permanent.h"
-#include "tpm12/tpm_nvfile.h"
+#include "tpm_nvfile.h"
 
 static TPM_RESULT TPM12_MainInit(void)
 {
index d56d49d04a5f7b16549395a6abd0bd3c4abcf4c0..0253cbaf96a826c9b2ebe49291e266d4b07b87ca 100644 (file)
@@ -46,7 +46,7 @@
 
 #include "tpm12/tpm_crypto.h"
 #include "tpm12/tpm_cryptoh.h"
-#include "tpm12/tpm_debug.h"
+#include "tpm_debug.h"
 #include "tpm_error.h"
 #include "tpm12/tpm_digest.h"
 #include "tpm12/tpm_global.h"
index 9e36ac2dac4cb3eb964b2f688f988adee79ca911..3c86aaa295cc07ba239168f554eb0a4c08c20294 100644 (file)
@@ -62,7 +62,7 @@
 #include "tpm2/crypto/openssl/ExpDCache_fp.h"
 
 #define TPM_HAVE_TPM2_DECLARATIONS
-#include "tpm12/tpm_nvfile.h" // TPM_NVRAM_Loaddata()
+#include "tpm_nvfile.h" // TPM_NVRAM_Loaddata()
 #include "tpm_error.h"
 #include "tpm_library_intern.h"
 #include "tpm_nvfilename.h"