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