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_Rewrap_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT oldParent,TPMI_DH_OBJECT newParent,const TPM2B_PRIVATE * inDuplicate,const TPM2B_NAME * name,const TPM2B_ENCRYPTED_SECRET * inSymSeed)15 TSS2_RC Tss2_Sys_Rewrap_Prepare(
16     TSS2_SYS_CONTEXT *sysContext,
17     TPMI_DH_OBJECT oldParent,
18     TPMI_DH_OBJECT newParent,
19     const TPM2B_PRIVATE *inDuplicate,
20     const TPM2B_NAME *name,
21     const TPM2B_ENCRYPTED_SECRET *inSymSeed)
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     rval = CommonPreparePrologue(ctx, TPM2_CC_Rewrap);
30     if (rval)
31         return rval;
32 
33     rval = Tss2_MU_UINT32_Marshal(oldParent, ctx->cmdBuffer,
34                                   ctx->maxCmdSize,
35                                   &ctx->nextData);
36     if (rval)
37         return rval;
38 
39     rval = Tss2_MU_UINT32_Marshal(newParent, ctx->cmdBuffer,
40                                   ctx->maxCmdSize,
41                                   &ctx->nextData);
42     if (rval)
43         return rval;
44 
45     if (!inDuplicate) {
46         ctx->decryptNull = 1;
47 
48         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
49                                       ctx->maxCmdSize,
50                                       &ctx->nextData);
51     } else {
52 
53         rval = Tss2_MU_TPM2B_PRIVATE_Marshal(inDuplicate, ctx->cmdBuffer,
54                                              ctx->maxCmdSize,
55                                              &ctx->nextData);
56     }
57 
58     if (rval)
59         return rval;
60 
61     if (!name) {
62         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
63                                       ctx->maxCmdSize,
64                                       &ctx->nextData);
65 
66     } else {
67 
68         rval = Tss2_MU_TPM2B_NAME_Marshal(name, ctx->cmdBuffer,
69                                           ctx->maxCmdSize,
70                                           &ctx->nextData);
71     }
72 
73     if (rval)
74         return rval;
75 
76     if (!inSymSeed) {
77         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
78                                       ctx->maxCmdSize,
79                                       &ctx->nextData);
80 
81     } else {
82 
83         rval = Tss2_MU_TPM2B_ENCRYPTED_SECRET_Marshal(inSymSeed,
84                                                       ctx->cmdBuffer,
85                                                       ctx->maxCmdSize,
86                                                       &ctx->nextData);
87     }
88 
89     if (rval)
90         return rval;
91 
92     ctx->decryptAllowed = 1;
93     ctx->encryptAllowed = 1;
94     ctx->authAllowed = 1;
95 
96     return CommonPrepareEpilogue(ctx);
97 }
98 
Tss2_Sys_Rewrap_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_PRIVATE * outDuplicate,TPM2B_ENCRYPTED_SECRET * outSymSeed)99 TSS2_RC Tss2_Sys_Rewrap_Complete(
100     TSS2_SYS_CONTEXT *sysContext,
101     TPM2B_PRIVATE *outDuplicate,
102     TPM2B_ENCRYPTED_SECRET *outSymSeed)
103 {
104     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
105     TSS2_RC rval;
106 
107     if (!ctx)
108         return TSS2_SYS_RC_BAD_REFERENCE;
109 
110     rval = CommonComplete(ctx);
111     if (rval)
112         return rval;
113 
114     rval = Tss2_MU_TPM2B_PRIVATE_Unmarshal(ctx->cmdBuffer,
115                                            ctx->maxCmdSize,
116                                            &ctx->nextData, outDuplicate);
117     if (rval)
118         return rval;
119 
120     return Tss2_MU_TPM2B_ENCRYPTED_SECRET_Unmarshal(ctx->cmdBuffer,
121                                                     ctx->maxCmdSize,
122                                                     &ctx->nextData,
123                                                     outSymSeed);
124 }
125 
Tss2_Sys_Rewrap(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT oldParent,TPMI_DH_OBJECT newParent,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_PRIVATE * inDuplicate,const TPM2B_NAME * name,const TPM2B_ENCRYPTED_SECRET * inSymSeed,TPM2B_PRIVATE * outDuplicate,TPM2B_ENCRYPTED_SECRET * outSymSeed,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)126 TSS2_RC Tss2_Sys_Rewrap(
127     TSS2_SYS_CONTEXT *sysContext,
128     TPMI_DH_OBJECT oldParent,
129     TPMI_DH_OBJECT newParent,
130     TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
131     const TPM2B_PRIVATE *inDuplicate,
132     const TPM2B_NAME *name,
133     const TPM2B_ENCRYPTED_SECRET *inSymSeed,
134     TPM2B_PRIVATE *outDuplicate,
135     TPM2B_ENCRYPTED_SECRET *outSymSeed,
136     TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
137 {
138     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
139     TSS2_RC rval;
140 
141     rval = Tss2_Sys_Rewrap_Prepare(sysContext, oldParent, newParent, inDuplicate, name, inSymSeed);
142     if (rval)
143         return rval;
144 
145     rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
146     if (rval)
147         return rval;
148 
149     return Tss2_Sys_Rewrap_Complete(sysContext, outDuplicate, outSymSeed);
150 }
151