1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************;
3  * Copyright (c) 2015 - 2018, Intel Corporation
4  * All rights reserved.
5  ***********************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include "tss2_tpm2_types.h"
12 #include "tss2_mu.h"
13 #include "sysapi_util.h"
14 
Tss2_Sys_GetRpBuffer(TSS2_SYS_CONTEXT * sysContext,size_t * rpBufferUsedSize,const uint8_t ** rpBuffer)15 TSS2_RC Tss2_Sys_GetRpBuffer(
16     TSS2_SYS_CONTEXT *sysContext,
17     size_t *rpBufferUsedSize,
18     const uint8_t **rpBuffer)
19 {
20     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
21     TSS2_RC rval;
22 
23     if (!ctx || !rpBufferUsedSize || !rpBuffer)
24         return TSS2_SYS_RC_BAD_REFERENCE;
25 
26     if (ctx->previousStage != CMD_STAGE_RECEIVE_RESPONSE)
27         return TSS2_SYS_RC_BAD_SEQUENCE;
28 
29     /* Calculate the position of the response parameter section within the TPM
30      * response as well as its size. Structure is:
31      * Header (tag, responseSize, responseCode)
32      * handle(if Command has handles)
33      * parameterSize (if TPM_ST_SESSIONS), size of rpArea
34      * rpArea
35      * Sessions (if TPM_ST_SESSIONS) */
36     size_t offset = sizeof(TPM20_Header_Out); /* Skip over the header */
37     offset += ctx->numResponseHandles * sizeof(TPM2_HANDLE); /* Skip handle */
38 
39     if (ctx->rsp_header.tag == TPM2_ST_SESSIONS) {
40         /* If sessions are used a parameterSize values exists for convenience */
41         TPM2_PARAMETER_SIZE parameterSize;
42         rval = Tss2_MU_UINT32_Unmarshal(ctx->cmdBuffer,
43                 ctx->rsp_header.responseSize, &offset, &parameterSize);
44         if (rval != TSS2_RC_SUCCESS) {
45             return rval;
46         }
47         *rpBuffer = ctx->cmdBuffer + offset;
48         *rpBufferUsedSize = parameterSize;
49     } else {
50         /* If no session is used the remainder is the rpArea */
51         *rpBuffer = ctx->cmdBuffer + offset;
52         *rpBufferUsedSize = ctx->rsp_header.responseSize - offset;
53     }
54 
55     return TSS2_RC_SUCCESS;
56 }
57