X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=SecurityPkg%2FLibrary%2FTpm12CommandLib%2FTpm12NvStorage.c;h=7baafa8f55be2568cb3c4ab61e175be008b95393;hp=d644d33846f34394d30d37c84c1b67e8bf071ede;hb=83b9662fb0086841d4f6220147bbf78966c38c56;hpb=e7c83012fd664fdf5312099441e6c02f1bf28592 diff --git a/SecurityPkg/Library/Tpm12CommandLib/Tpm12NvStorage.c b/SecurityPkg/Library/Tpm12CommandLib/Tpm12NvStorage.c index d644d33846..7baafa8f55 100644 --- a/SecurityPkg/Library/Tpm12CommandLib/Tpm12NvStorage.c +++ b/SecurityPkg/Library/Tpm12CommandLib/Tpm12NvStorage.c @@ -1,7 +1,7 @@ /** @file Implement TPM1.2 NV storage related command. -Copyright (c) 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -12,18 +12,17 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ -#include -#include -#include -#include -#include +#include #include +#include #include +#include +#include // -// Max TPM command/reponse length +// Max TPM NV value length // -#define TPMCMDBUFLENGTH 1024 +#define TPMNVVALUELENGTH 1024 #pragma pack(1) @@ -33,10 +32,6 @@ typedef struct { TPM_ENCAUTH EncAuth; } TPM_CMD_NV_DEFINE_SPACE; -typedef struct { - TPM_RSP_COMMAND_HDR Hdr; -} TPM_RSP_NV_DEFINE_SPACE; - typedef struct { TPM_RQU_COMMAND_HDR Hdr; TPM_NV_INDEX NvIndex; @@ -47,7 +42,7 @@ typedef struct { typedef struct { TPM_RSP_COMMAND_HDR Hdr; UINT32 DataSize; - UINT8 Data[TPMCMDBUFLENGTH]; + UINT8 Data[TPMNVVALUELENGTH]; } TPM_RSP_NV_READ_VALUE; typedef struct { @@ -55,13 +50,9 @@ typedef struct { TPM_NV_INDEX NvIndex; UINT32 Offset; UINT32 DataSize; - UINT8 Data[TPMCMDBUFLENGTH]; + UINT8 Data[TPMNVVALUELENGTH]; } TPM_CMD_NV_WRITE_VALUE; -typedef struct { - TPM_RSP_COMMAND_HDR Hdr; -} TPM_RSP_NV_WRITE_VALUE; - #pragma pack() /** @@ -80,57 +71,50 @@ Tpm12NvDefineSpace ( IN TPM_ENCAUTH *EncAuth ) { - EFI_STATUS Status; - UINT32 TpmRecvSize; - UINT32 TpmSendSize; - TPM_CMD_NV_DEFINE_SPACE SendBuffer; - TPM_RSP_NV_DEFINE_SPACE RecvBuffer; - UINT32 ReturnCode; + EFI_STATUS Status; + TPM_CMD_NV_DEFINE_SPACE Command; + TPM_RSP_COMMAND_HDR Response; + UINT32 Length; // // send Tpm command TPM_ORD_NV_DefineSpace // - TpmRecvSize = sizeof (TPM_RSP_NV_DEFINE_SPACE); - TpmSendSize = sizeof (TPM_CMD_NV_DEFINE_SPACE); - SendBuffer.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND); - SendBuffer.Hdr.paramSize = SwapBytes32 (sizeof(TPM_CMD_NV_DEFINE_SPACE)); - SendBuffer.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_DefineSpace); - SendBuffer.PubInfo.tag = SwapBytes16 (PubInfo->tag); - SendBuffer.PubInfo.nvIndex = SwapBytes32 (PubInfo->nvIndex); - SendBuffer.PubInfo.pcrInfoRead.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoRead.pcrSelection.sizeOfSelect); - SendBuffer.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[0]; - SendBuffer.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[1]; - SendBuffer.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[2]; - SendBuffer.PubInfo.pcrInfoRead.localityAtRelease = PubInfo->pcrInfoRead.localityAtRelease; - CopyMem (&SendBuffer.PubInfo.pcrInfoRead.digestAtRelease, &PubInfo->pcrInfoRead.digestAtRelease, sizeof(PubInfo->pcrInfoRead.digestAtRelease)); - SendBuffer.PubInfo.pcrInfoWrite.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoWrite.pcrSelection.sizeOfSelect); - SendBuffer.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[0]; - SendBuffer.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[1]; - SendBuffer.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[2]; - SendBuffer.PubInfo.pcrInfoWrite.localityAtRelease = PubInfo->pcrInfoWrite.localityAtRelease; - CopyMem (&SendBuffer.PubInfo.pcrInfoWrite.digestAtRelease, &PubInfo->pcrInfoWrite.digestAtRelease, sizeof(PubInfo->pcrInfoWrite.digestAtRelease)); - SendBuffer.PubInfo.permission.tag = SwapBytes16 (PubInfo->permission.tag); - SendBuffer.PubInfo.permission.attributes = SwapBytes32 (PubInfo->permission.attributes); - SendBuffer.PubInfo.bReadSTClear = PubInfo->bReadSTClear; - SendBuffer.PubInfo.bWriteSTClear = PubInfo->bWriteSTClear; - SendBuffer.PubInfo.bWriteDefine = PubInfo->bWriteDefine; - SendBuffer.PubInfo.dataSize = SwapBytes32 (PubInfo->dataSize); - CopyMem (&SendBuffer.EncAuth, EncAuth, sizeof(*EncAuth)); - - Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer); + Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND); + Command.Hdr.paramSize = SwapBytes32 (sizeof (Command)); + Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_DefineSpace); + Command.PubInfo.tag = SwapBytes16 (PubInfo->tag); + Command.PubInfo.nvIndex = SwapBytes32 (PubInfo->nvIndex); + Command.PubInfo.pcrInfoRead.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoRead.pcrSelection.sizeOfSelect); + Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[0]; + Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[1]; + Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[2]; + Command.PubInfo.pcrInfoRead.localityAtRelease = PubInfo->pcrInfoRead.localityAtRelease; + CopyMem (&Command.PubInfo.pcrInfoRead.digestAtRelease, &PubInfo->pcrInfoRead.digestAtRelease, sizeof(PubInfo->pcrInfoRead.digestAtRelease)); + Command.PubInfo.pcrInfoWrite.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoWrite.pcrSelection.sizeOfSelect); + Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[0]; + Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[1]; + Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[2]; + Command.PubInfo.pcrInfoWrite.localityAtRelease = PubInfo->pcrInfoWrite.localityAtRelease; + CopyMem (&Command.PubInfo.pcrInfoWrite.digestAtRelease, &PubInfo->pcrInfoWrite.digestAtRelease, sizeof(PubInfo->pcrInfoWrite.digestAtRelease)); + Command.PubInfo.permission.tag = SwapBytes16 (PubInfo->permission.tag); + Command.PubInfo.permission.attributes = SwapBytes32 (PubInfo->permission.attributes); + Command.PubInfo.bReadSTClear = PubInfo->bReadSTClear; + Command.PubInfo.bWriteSTClear = PubInfo->bWriteSTClear; + Command.PubInfo.bWriteDefine = PubInfo->bWriteDefine; + Command.PubInfo.dataSize = SwapBytes32 (PubInfo->dataSize); + CopyMem (&Command.EncAuth, EncAuth, sizeof(*EncAuth)); + Length = sizeof (Response); + Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response); if (EFI_ERROR (Status)) { return Status; } - ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode); - DEBUG ((DEBUG_INFO, "Tpm12NvDefineSpace - ReturnCode = %x\n", ReturnCode)); - switch (ReturnCode) { + DEBUG ((DEBUG_INFO, "Tpm12NvDefineSpace - ReturnCode = %x\n", SwapBytes32 (Response.returnCode))); + switch (SwapBytes32 (Response.returnCode)) { case TPM_SUCCESS: - break; + return EFI_SUCCESS; default: return EFI_DEVICE_ERROR; } - - return EFI_SUCCESS; } /** @@ -147,38 +131,33 @@ Tpm12NvDefineSpace ( EFI_STATUS EFIAPI Tpm12NvReadValue ( - IN TPM_NV_INDEX NvIndex, - IN UINT32 Offset, - IN OUT UINT32 *DataSize, - OUT UINT8 *Data + IN TPM_NV_INDEX NvIndex, + IN UINT32 Offset, + IN OUT UINT32 *DataSize, + OUT UINT8 *Data ) { - EFI_STATUS Status; - UINT32 TpmRecvSize; - UINT32 TpmSendSize; - TPM_CMD_NV_READ_VALUE SendBuffer; - TPM_RSP_NV_READ_VALUE RecvBuffer; - UINT32 ReturnCode; + EFI_STATUS Status; + TPM_CMD_NV_READ_VALUE Command; + TPM_RSP_NV_READ_VALUE Response; + UINT32 Length; // // send Tpm command TPM_ORD_NV_ReadValue // - TpmRecvSize = sizeof (TPM_RSP_NV_READ_VALUE); - TpmSendSize = sizeof (TPM_CMD_NV_READ_VALUE); - SendBuffer.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND); - SendBuffer.Hdr.paramSize = SwapBytes32 (sizeof(TPM_CMD_NV_READ_VALUE)); - SendBuffer.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_ReadValue); - SendBuffer.NvIndex = SwapBytes32 (NvIndex); - SendBuffer.Offset = SwapBytes32 (Offset); - SendBuffer.DataSize = SwapBytes32 (*DataSize); - - Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer); + Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND); + Command.Hdr.paramSize = SwapBytes32 (sizeof (Command)); + Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_ReadValue); + Command.NvIndex = SwapBytes32 (NvIndex); + Command.Offset = SwapBytes32 (Offset); + Command.DataSize = SwapBytes32 (*DataSize); + Length = sizeof (Response); + Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response); if (EFI_ERROR (Status)) { return Status; } - ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode); - DEBUG ((DEBUG_INFO, "Tpm12NvReadValue - ReturnCode = %x\n", ReturnCode)); - switch (ReturnCode) { + DEBUG ((DEBUG_INFO, "Tpm12NvReadValue - ReturnCode = %x\n", SwapBytes32 (Response.Hdr.returnCode))); + switch (SwapBytes32 (Response.Hdr.returnCode)) { case TPM_SUCCESS: break; default: @@ -188,8 +167,12 @@ Tpm12NvReadValue ( // // Return the response // - *DataSize = SwapBytes32(RecvBuffer.DataSize); - CopyMem (Data, &RecvBuffer.Data, *DataSize); + if (SwapBytes32 (Response.DataSize) > *DataSize) { + return EFI_BUFFER_TOO_SMALL; + } + *DataSize = SwapBytes32 (Response.DataSize); + ZeroMem (Data, *DataSize); + CopyMem (Data, &Response.Data, *DataSize); return EFI_SUCCESS; } @@ -208,48 +191,41 @@ Tpm12NvReadValue ( EFI_STATUS EFIAPI Tpm12NvWriteValue ( - IN TPM_NV_INDEX NvIndex, - IN UINT32 Offset, - IN UINT32 DataSize, - IN UINT8 *Data + IN TPM_NV_INDEX NvIndex, + IN UINT32 Offset, + IN UINT32 DataSize, + IN UINT8 *Data ) { - EFI_STATUS Status; - UINT32 TpmRecvSize; - UINT32 TpmSendSize; - TPM_CMD_NV_WRITE_VALUE SendBuffer; - TPM_RSP_NV_WRITE_VALUE RecvBuffer; - UINT32 ReturnCode; - - if (DataSize > sizeof(SendBuffer.Data)) { + EFI_STATUS Status; + TPM_CMD_NV_WRITE_VALUE Command; + TPM_RSP_COMMAND_HDR Response; + UINT32 Length; + + if (DataSize > sizeof (Command.Data)) { return EFI_UNSUPPORTED; } // // send Tpm command TPM_ORD_NV_WriteValue // - TpmRecvSize = sizeof (TPM_RSP_NV_WRITE_VALUE); - TpmSendSize = sizeof (TPM_CMD_NV_WRITE_VALUE) - sizeof(SendBuffer.Data) + DataSize; - SendBuffer.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND); - SendBuffer.Hdr.paramSize = SwapBytes32 (sizeof(TPM_CMD_NV_WRITE_VALUE) - sizeof(SendBuffer.Data) + DataSize); - SendBuffer.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_WriteValue); - SendBuffer.NvIndex = SwapBytes32 (NvIndex); - SendBuffer.Offset = SwapBytes32 (Offset); - SendBuffer.DataSize = SwapBytes32 (DataSize); - CopyMem (SendBuffer.Data, Data, DataSize); - - Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer); + Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND); + Command.Hdr.paramSize = SwapBytes32 (sizeof (Command) - sizeof(Command.Data) + DataSize); + Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_WriteValue); + Command.NvIndex = SwapBytes32 (NvIndex); + Command.Offset = SwapBytes32 (Offset); + Command.DataSize = SwapBytes32 (DataSize); + CopyMem (Command.Data, Data, DataSize); + Length = sizeof (Response); + Status = Tpm12SubmitCommand (Command.Hdr.paramSize, (UINT8 *)&Command, &Length, (UINT8 *)&Response); if (EFI_ERROR (Status)) { return Status; } - ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode); - DEBUG ((DEBUG_INFO, "Tpm12NvWritedValue - ReturnCode = %x\n", ReturnCode)); - switch (ReturnCode) { + DEBUG ((DEBUG_INFO, "Tpm12NvWritedValue - ReturnCode = %x\n", SwapBytes32 (Response.returnCode))); + switch (SwapBytes32 (Response.returnCode)) { case TPM_SUCCESS: - break; + return EFI_SUCCESS; default: return EFI_DEVICE_ERROR; } - - return EFI_SUCCESS; }