/src/tpm2-tss/src/tss2-sys/api/Tss2_Sys_GetCapability.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: BSD-2-Clause */ |
2 | | /***********************************************************************; |
3 | | * Copyright (c) 2015 - 2017, Intel Corporation |
4 | | * All rights reserved. |
5 | | ***********************************************************************/ |
6 | | |
7 | | #ifdef HAVE_CONFIG_H |
8 | | #include "config.h" // IWYU pragma: keep |
9 | | #endif |
10 | | |
11 | | #include <string.h> // for memcpy, size_t |
12 | | |
13 | | #include "sysapi_util.h" // for _TSS2_SYS_CONTEXT_BLOB, syscontext_cast |
14 | | #include "tss2_common.h" // for TSS2_RC, UINT32, TSS2_SYS_RC_BAD_REFERENCE |
15 | | #include "tss2_mu.h" // for Tss2_MU_UINT32_Marshal, Tss2_MU_TPMS_CA... |
16 | | #include "tss2_sys.h" // for TSS2_SYS_CONTEXT, TSS2L_SYS_AUTH_COMMAND |
17 | | #include "tss2_tpm2_types.h" // for TPMS_CAPABILITY_DATA, TPM2B_MAX_CAP_BUFFER |
18 | | |
19 | | TSS2_RC |
20 | | Tss2_Sys_GetCapability_Prepare(TSS2_SYS_CONTEXT *sysContext, |
21 | | TPM2_CAP capability, |
22 | | UINT32 property, |
23 | 0 | UINT32 propertyCount) { |
24 | 0 | TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext); |
25 | 0 | TSS2_RC rval; |
26 | |
|
27 | 0 | if (!ctx) |
28 | 0 | return TSS2_SYS_RC_BAD_REFERENCE; |
29 | | |
30 | 0 | rval = CommonPreparePrologue(ctx, TPM2_CC_GetCapability); |
31 | 0 | if (rval) |
32 | 0 | return rval; |
33 | | |
34 | 0 | rval = Tss2_MU_UINT32_Marshal(capability, ctx->cmdBuffer, ctx->maxCmdSize, &ctx->nextData); |
35 | 0 | if (rval) |
36 | 0 | return rval; |
37 | | |
38 | 0 | rval = Tss2_MU_UINT32_Marshal(property, ctx->cmdBuffer, ctx->maxCmdSize, &ctx->nextData); |
39 | 0 | if (rval) |
40 | 0 | return rval; |
41 | | |
42 | 0 | rval = Tss2_MU_UINT32_Marshal(propertyCount, ctx->cmdBuffer, ctx->maxCmdSize, &ctx->nextData); |
43 | 0 | if (rval) |
44 | 0 | return rval; |
45 | | |
46 | 0 | ctx->decryptAllowed = 0; |
47 | 0 | ctx->encryptAllowed = 0; |
48 | 0 | ctx->authAllowed = 1; |
49 | |
|
50 | 0 | return CommonPrepareEpilogue(ctx); |
51 | 0 | } |
52 | | |
53 | | TSS2_RC |
54 | | Tss2_Sys_GetCapability_Complete(TSS2_SYS_CONTEXT *sysContext, |
55 | | TPMI_YES_NO *moreData, |
56 | 0 | TPMS_CAPABILITY_DATA *capabilityData) { |
57 | 0 | TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext); |
58 | 0 | TSS2_RC rval; |
59 | 0 | TPM2_CAP cap; |
60 | |
|
61 | 0 | if (!ctx) |
62 | 0 | return TSS2_SYS_RC_BAD_REFERENCE; |
63 | | |
64 | 0 | rval = CommonComplete(ctx); |
65 | 0 | if (rval) |
66 | 0 | return rval; |
67 | | |
68 | 0 | rval = Tss2_MU_UINT8_Unmarshal(ctx->cmdBuffer, ctx->maxCmdSize, &ctx->nextData, moreData); |
69 | 0 | if (rval) |
70 | 0 | return rval; |
71 | | |
72 | | /* Peak at the capabilityData->capability field to decide what to do |
73 | | if this is a standard cap, just unmarshal it. |
74 | | if this is a vendor cap, we fill a tpm2b inside the struct buffer.*/ |
75 | 0 | size_t next_data_vendor = ctx->nextData; |
76 | 0 | rval = Tss2_MU_UINT32_Unmarshal(ctx->cmdBuffer, ctx->maxCmdSize, &next_data_vendor, &cap); |
77 | 0 | if (rval) |
78 | 0 | return rval; |
79 | | |
80 | 0 | if (cap != TPM2_CAP_VENDOR_PROPERTY) |
81 | 0 | return Tss2_MU_TPMS_CAPABILITY_DATA_Unmarshal(ctx->cmdBuffer, ctx->maxCmdSize, |
82 | 0 | &ctx->nextData, capabilityData); |
83 | | |
84 | | /* |
85 | | * Size the capabilityData is the end of response we can copy the remainder |
86 | | * of the response in case of cap_vendor. |
87 | | * |
88 | | * Example for 23 byte response size: |
89 | | * |
90 | | * Header: 10 Bytes |
91 | | * MoreData: 1 byte |
92 | | * cap: 4 bytes |
93 | | * Vendor Data: 8 bytes |
94 | | */ |
95 | 0 | ctx->nextData = next_data_vendor; |
96 | 0 | size_t left = ctx->rsp_header.responseSize - ctx->nextData; |
97 | 0 | if (left > sizeof(capabilityData->data.vendor.buffer)) |
98 | 0 | return TSS2_MU_RC_BAD_SIZE; |
99 | | |
100 | | /* seems callers can use NULL */ |
101 | 0 | if (capabilityData) { |
102 | 0 | capabilityData->capability = cap; |
103 | |
|
104 | 0 | capabilityData->data.vendor.size = left; |
105 | 0 | memcpy(&capabilityData->data.vendor.buffer[0], &ctx->cmdBuffer[ctx->nextData], left); |
106 | 0 | } |
107 | 0 | ctx->nextData += left; |
108 | |
|
109 | 0 | return TSS2_RC_SUCCESS; |
110 | 0 | } |
111 | | |
112 | | TSS2_RC |
113 | | Tss2_Sys_GetCapability(TSS2_SYS_CONTEXT *sysContext, |
114 | | TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray, |
115 | | TPM2_CAP capability, |
116 | | UINT32 property, |
117 | | UINT32 propertyCount, |
118 | | TPMI_YES_NO *moreData, |
119 | | TPMS_CAPABILITY_DATA *capabilityData, |
120 | 0 | TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray) { |
121 | 0 | TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext); |
122 | 0 | TSS2_RC rval; |
123 | |
|
124 | 0 | rval = Tss2_Sys_GetCapability_Prepare(sysContext, capability, property, propertyCount); |
125 | 0 | if (rval) |
126 | 0 | return rval; |
127 | | |
128 | 0 | rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray); |
129 | 0 | if (rval) |
130 | 0 | return rval; |
131 | | |
132 | 0 | return Tss2_Sys_GetCapability_Complete(sysContext, moreData, capabilityData); |
133 | 0 | } |