• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_Import_Prepare(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT parentHandle,const TPM2B_DATA * encryptionKey,const TPM2B_PUBLIC * objectPublic,const TPM2B_PRIVATE * duplicate,const TPM2B_ENCRYPTED_SECRET * inSymSeed,const TPMT_SYM_DEF_OBJECT * symmetricAlg)15 TSS2_RC Tss2_Sys_Import_Prepare(
16     TSS2_SYS_CONTEXT *sysContext,
17     TPMI_DH_OBJECT parentHandle,
18     const TPM2B_DATA *encryptionKey,
19     const TPM2B_PUBLIC *objectPublic,
20     const TPM2B_PRIVATE *duplicate,
21     const TPM2B_ENCRYPTED_SECRET *inSymSeed,
22     const TPMT_SYM_DEF_OBJECT *symmetricAlg)
23 {
24     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
25     TSS2_RC rval;
26 
27     if (!ctx || !symmetricAlg)
28         return TSS2_SYS_RC_BAD_REFERENCE;
29 
30     rval = CommonPreparePrologue(ctx, TPM2_CC_Import);
31     if (rval)
32         return rval;
33 
34     rval = Tss2_MU_UINT32_Marshal(parentHandle, ctx->cmdBuffer,
35                                   ctx->maxCmdSize,
36                                   &ctx->nextData);
37     if (rval)
38         return rval;
39 
40     if (!encryptionKey) {
41         ctx->decryptNull = 1;
42 
43         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
44                                       ctx->maxCmdSize,
45                                       &ctx->nextData);
46     } else {
47 
48         rval = Tss2_MU_TPM2B_DATA_Marshal(encryptionKey, ctx->cmdBuffer,
49                                           ctx->maxCmdSize,
50                                           &ctx->nextData);
51     }
52 
53     if (rval)
54         return rval;
55 
56     if (!objectPublic) {
57         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
58                                       ctx->maxCmdSize,
59                                       &ctx->nextData);
60 
61     } else {
62         rval = ValidatePublicTemplate(objectPublic);
63 
64         if (rval)
65             return rval;
66         rval = Tss2_MU_TPM2B_PUBLIC_Marshal(objectPublic, ctx->cmdBuffer,
67                                             ctx->maxCmdSize,
68                                             &ctx->nextData);
69     }
70 
71     if (rval)
72         return rval;
73 
74     if (!duplicate) {
75         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
76                                       ctx->maxCmdSize,
77                                       &ctx->nextData);
78 
79     } else {
80 
81         rval = Tss2_MU_TPM2B_PRIVATE_Marshal(duplicate, ctx->cmdBuffer,
82                                              ctx->maxCmdSize,
83                                              &ctx->nextData);
84     }
85 
86     if (rval)
87         return rval;
88 
89     if (!inSymSeed) {
90         rval = Tss2_MU_UINT16_Marshal(0, ctx->cmdBuffer,
91                                       ctx->maxCmdSize,
92                                       &ctx->nextData);
93 
94     } else {
95 
96         rval = Tss2_MU_TPM2B_ENCRYPTED_SECRET_Marshal(inSymSeed,
97                                                       ctx->cmdBuffer,
98                                                       ctx->maxCmdSize,
99                                                       &ctx->nextData);
100     }
101 
102     if (rval)
103         return rval;
104 
105     rval = Tss2_MU_TPMT_SYM_DEF_OBJECT_Marshal(symmetricAlg,
106                                                ctx->cmdBuffer,
107                                                ctx->maxCmdSize,
108                                                &ctx->nextData);
109     if (rval)
110         return rval;
111 
112     ctx->decryptAllowed = 1;
113     ctx->encryptAllowed = 1;
114     ctx->authAllowed = 1;
115 
116     return CommonPrepareEpilogue(ctx);
117 }
118 
Tss2_Sys_Import_Complete(TSS2_SYS_CONTEXT * sysContext,TPM2B_PRIVATE * outPrivate)119 TSS2_RC Tss2_Sys_Import_Complete(
120     TSS2_SYS_CONTEXT *sysContext,
121     TPM2B_PRIVATE *outPrivate)
122 {
123     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
124     TSS2_RC rval;
125 
126     if (!ctx)
127         return TSS2_SYS_RC_BAD_REFERENCE;
128 
129     rval = CommonComplete(ctx);
130     if (rval)
131         return rval;
132 
133     return Tss2_MU_TPM2B_PRIVATE_Unmarshal(ctx->cmdBuffer,
134                                            ctx->maxCmdSize,
135                                            &ctx->nextData,
136                                            outPrivate);
137 }
138 
Tss2_Sys_Import(TSS2_SYS_CONTEXT * sysContext,TPMI_DH_OBJECT parentHandle,TSS2L_SYS_AUTH_COMMAND const * cmdAuthsArray,const TPM2B_DATA * encryptionKey,const TPM2B_PUBLIC * objectPublic,const TPM2B_PRIVATE * duplicate,const TPM2B_ENCRYPTED_SECRET * inSymSeed,const TPMT_SYM_DEF_OBJECT * symmetricAlg,TPM2B_PRIVATE * outPrivate,TSS2L_SYS_AUTH_RESPONSE * rspAuthsArray)139 TSS2_RC Tss2_Sys_Import(
140     TSS2_SYS_CONTEXT *sysContext,
141     TPMI_DH_OBJECT parentHandle,
142     TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray,
143     const TPM2B_DATA *encryptionKey,
144     const TPM2B_PUBLIC *objectPublic,
145     const TPM2B_PRIVATE *duplicate,
146     const TPM2B_ENCRYPTED_SECRET *inSymSeed,
147     const TPMT_SYM_DEF_OBJECT *symmetricAlg,
148     TPM2B_PRIVATE *outPrivate,
149     TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray)
150 {
151     _TSS2_SYS_CONTEXT_BLOB *ctx = syscontext_cast(sysContext);
152     TSS2_RC rval;
153 
154     if (!symmetricAlg)
155         return TSS2_SYS_RC_BAD_REFERENCE;
156 
157     rval = Tss2_Sys_Import_Prepare(sysContext, parentHandle, encryptionKey,
158                                    objectPublic, duplicate, inSymSeed,
159                                    symmetricAlg);
160     if (rval)
161         return rval;
162 
163     rval = CommonOneCall(ctx, cmdAuthsArray, rspAuthsArray);
164     if (rval)
165         return rval;
166 
167     return Tss2_Sys_Import_Complete(sysContext, outPrivate);
168 }
169