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>
9 #endif
10 
11 #include "tss2_tpm2_types.h"
12 #include "tss2_mu.h"
13 #include "sysapi_util.h"
14 
Tss2_Sys_CreatePrimary_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_RH_HIERARCHY primaryHandle,const TPM2B_SENSITIVE_CREATE * inSensitive,const TPM2B_PUBLIC * inPublic,const TPM2B_DATA * outsideInfo,const TPML_PCR_SELECTION * creationPCR)15 TSS2_RC Tss2_Sys_CreatePrimary_Prepare(
16     TSS2_SYS_CONTEXT *sysContext,
17     TPMI_RH_HIERARCHY primaryHandle,
18     const TPM2B_SENSITIVE_CREATE *inSensitive,
19     const TPM2B_PUBLIC *inPublic,
20     const TPM2B_DATA *outsideInfo,
21     const TPML_PCR_SELECTION *creationPCR)
22 {
23     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
24     TSS2_RC rval;
25 
26     if (!ctx)
27         return TSS2_SYS_RC_BAD_REFERENCE;
28 
29     if (!creationPCR)
30         return TSS2_SYS_RC_BAD_REFERENCE;
31 
32     rval = CommonPreparePrologue(ctx, TPM2_CC_CreatePrimary);
33     if (rval)
34         return rval;
35 
36     rval = Tss2_MU_UINT32_Marshal(primaryHandle, ctx->cmdBuffer,
37                           ctx->maxCmdSize,
38                           &ctx->nextData);
39     if (rval)
40         return rval;
41 
42     if (!inSensitive) {
43         ctx->decryptNull = 1;
44 
45         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
46                                       ctx->maxCmdSize,
47                                       &ctx->nextData);
48     } else {
49         rval = Tss2_MU_TPM2B_SENSITIVE_CREATE_Marshal(inSensitive,
50                                                       ctx->cmdBuffer,
51                                                       ctx->maxCmdSize,
52                                                       &ctx->nextData);
53     }
54 
55     if (rval)
56         return rval;
57 
58     if (!inPublic) {
59         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
60                                       ctx->maxCmdSize,
61                                       &ctx->nextData);
62 
63     } else {
64         rval = ValidatePublicTemplate(inPublic);
65 
66         if (rval)
67             return rval;
68 
69         rval = Tss2_MU_TPM2B_PUBLIC_Marshal(inPublic, ctx->cmdBuffer,
70                                             ctx->maxCmdSize,
71                                             &ctx->nextData);
72     }
73 
74     if (rval)
75         return rval;
76 
77     if (!outsideInfo) {
78         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
79                                       ctx->maxCmdSize,
80                                       &ctx->nextData);
81 
82     } else {
83 
84         rval = Tss2_MU_TPM2B_DATA_Marshal(outsideInfo, ctx->cmdBuffer,
85                                           ctx->maxCmdSize,
86                                           &ctx->nextData);
87     }
88 
89     if (rval)
90         return rval;
91 
92     rval = Tss2_MU_TPML_PCR_SELECTION_Marshal(creationPCR,
93                                               ctx->cmdBuffer,
94                                               ctx->maxCmdSize,
95                                               &ctx->nextData);
96     if (rval)
97         return rval;
98 
99     ctx->decryptAllowed = 1;
100     ctx->encryptAllowed = 1;
101     ctx->authAllowed = 1;
102 
103     rval = CommonPrepareEpilogue(ctx);
104     return rval;
105 }
106 
Tss2_Sys_CreatePrimary_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2_HANDLE * objectHandle,TPM2B_PUBLIC * outPublic,TPM2B_CREATION_DATA * creationData,TPM2B_DIGEST * creationHash,TPMT_TK_CREATION * creationTicket,TPM2B_NAME * name)107 TSS2_RC Tss2_Sys_CreatePrimary_Complete(
108     TSS2_SYS_CONTEXT *sysContext,
109     TPM2_HANDLE *objectHandle,
110     TPM2B_PUBLIC *outPublic,
111     TPM2B_CREATION_DATA *creationData,
112     TPM2B_DIGEST *creationHash,
113     TPMT_TK_CREATION *creationTicket,
114     TPM2B_NAME *name)
115 {
116     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
117     TSS2_RC rval;
118 
119     if (!ctx)
120         return TSS2_SYS_RC_BAD_REFERENCE;
121 
122     rval = Tss2_MU_UINT32_Unmarshal(ctx->cmdBuffer,
123                                     ctx->maxCmdSize,
124                                     &ctx->nextData, objectHandle);
125     if (rval)
126         return rval;
127 
128     rval = CommonComplete(ctx);
129     if (rval)
130         return rval;
131 
132     rval = Tss2_MU_TPM2B_PUBLIC_Unmarshal(ctx->cmdBuffer,
133                                           ctx->maxCmdSize,
134                                           &ctx->nextData, outPublic);
135     if (rval)
136         return rval;
137 
138     rval = Tss2_MU_TPM2B_CREATION_DATA_Unmarshal(ctx->cmdBuffer,
139                                                  ctx->maxCmdSize,
140                                                  &ctx->nextData,
141                                                  creationData);
142     if (rval)
143         return rval;
144 
145     rval = Tss2_MU_TPM2B_DIGEST_Unmarshal(ctx->cmdBuffer,
146                                           ctx->maxCmdSize,
147                                           &ctx->nextData,
148                                           creationHash);
149     if (rval)
150         return rval;
151 
152     rval = Tss2_MU_TPMT_TK_CREATION_Unmarshal(ctx->cmdBuffer,
153                                               ctx->maxCmdSize,
154                                               &ctx->nextData,
155                                               creationTicket);
156     if (rval)
157         return rval;
158 
159     rval = Tss2_MU_TPM2B_NAME_Unmarshal(ctx->cmdBuffer,
160                                         ctx->maxCmdSize,
161                                         &ctx->nextData, name);
162     return rval;
163 }
164 
Tss2_Sys_CreatePrimary(TSS2_SYS_CONTEXT * sysContext,TPMI_RH_HIERARCHY primaryHandle,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_SENSITIVE_CREATE * inSensitive,const TPM2B_PUBLIC * inPublic,const TPM2B_DATA * outsideInfo,const TPML_PCR_SELECTION * creationPCR,TPM2_HANDLE * objectHandle,TPM2B_PUBLIC * outPublic,TPM2B_CREATION_DATA * creationData,TPM2B_DIGEST * creationHash,TPMT_TK_CREATION * creationTicket,TPM2B_NAME * name,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)165 TSS2_RC Tss2_Sys_CreatePrimary(
166     TSS2_SYS_CONTEXT *sysContext,
167     TPMI_RH_HIERARCHY primaryHandle,
168     TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
169     const TPM2B_SENSITIVE_CREATE *inSensitive,
170     const TPM2B_PUBLIC *inPublic,
171     const TPM2B_DATA *outsideInfo,
172     const TPML_PCR_SELECTION *creationPCR,
173     TPM2_HANDLE *objectHandle,
174     TPM2B_PUBLIC *outPublic,
175     TPM2B_CREATION_DATA *creationData,
176     TPM2B_DIGEST *creationHash,
177     TPMT_TK_CREATION *creationTicket,
178     TPM2B_NAME *name,
179     TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
180 {
181     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
182     TSS2_RC rval;
183 
184     if (!ctx || !creationPCR)
185         return TSS2_SYS_RC_BAD_REFERENCE;
186 
187     rval = Tss2_Sys_CreatePrimary_Prepare(sysContext, primaryHandle, inSensitive,
188                                           inPublic, outsideInfo, creationPCR);
189     if (rval)
190         return rval;
191 
192     rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
193 
194     if (rval)
195         return rval;
196 
197     rval = Tss2_Sys_CreatePrimary_Complete(sysContext, objectHandle, outPublic,
198                                            creationData, creationHash,
199                                            creationTicket, name);
200     return rval;
201 
202 }
203