aboutsummaryrefslogtreecommitdiffstats
path: root/tpm-util/import/tpm_wrapper.c
blob: 5f4ab2a51a016f375a31b0be528753c9f955674d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright 2018 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */
//
// Author: Arun Kumar Sekar
//

#include "tpm_wrapper.h"
#include "util.h"

#include <errno.h>

#include <sapi/tpm20.h>
#include <sapi/tss2_sys.h>
#include <tcti/tcti_device.h>
#include "tcti_util.h"

TSS2_RC swKeyTpmImport(
      /* IN */
      TSS2_SYS_CONTEXT *sysContext,
      TPM_HANDLE parentKeyHandle,
      TPM2B_DATA* encryptionKey, TPM2B_PUBLIC* swKeyPublic, TPM2B_PRIVATE* swKeyPrivate,  TPM2B_ENCRYPTED_SECRET* encSymSeed,
      unsigned char* tpm_pwd, int tpm_pwd_len, 
      /* OUT */
      TPM2B_PRIVATE *importPrivate)
{
    TPM_RC rval = TPM_RC_SUCCESS;
    TPM2B_NAME name = { { sizeof( TPM2B_NAME ) - 2, } };

    TPM_HANDLE wrapperKeyHandle;

    TSS2_SYS_CMD_AUTHS npsessionsData;
    TSS2_SYS_RSP_AUTHS npsessionsDataOut;
    TPMS_AUTH_COMMAND npsessionData;
    TPMS_AUTH_RESPONSE npsessionDataOut;

    if(NULL == tpm_pwd) {
        printf("TPM password pinter is NULL \n");
        return -1;
    }

    *((UINT8 *)((void *)&npsessionData.sessionAttributes)) = 0;
    npsessionData.sessionHandle = TPM_RS_PW;
    npsessionData.nonce.t.size = 0;
    npsessionData.hmac.t.size = 0;

    npsessionData.hmac.t.size = tpm_pwd_len;
    if(tpm_pwd_len > 0)
    {
        memcpy(npsessionData.hmac.t.buffer, tpm_pwd, npsessionData.hmac.t.size);
    }

    TPMS_AUTH_COMMAND *npsessionDataArray[1];
    TPMS_AUTH_RESPONSE *npsessionDataOutArray[1];
    TPMT_SYM_DEF_OBJECT symmetricAlg;

    npsessionDataArray[0] = &npsessionData;
    npsessionDataOutArray[0] = &npsessionDataOut;
    npsessionsData.cmdAuthsCount = 1;
    npsessionsData.cmdAuths = &npsessionDataArray[0];
    npsessionsDataOut.rspAuthsCount = 1;
    npsessionsDataOut.rspAuths = &npsessionDataOutArray[0];

    symmetricAlg.algorithm = TPM_ALG_AES;
    symmetricAlg.keyBits.aes = 128;
    symmetricAlg.mode.aes = TPM_ALG_CFB;

    rval =  Tss2_Sys_Import( sysContext,
                             parentKeyHandle,
                             &npsessionsData,
                             encryptionKey,
                             swKeyPublic,
                             swKeyPrivate,
                             encSymSeed,
                             &symmetricAlg,
                             importPrivate,
                             &npsessionsDataOut
                           );
    printf("The return value of sys import: 0x%x \n", rval);

#if 0
    if(rval == TSS2_RC_SUCCESS)
    {
        rval = Tss2_Sys_Load (sysContext, parentKeyHandle, &npsessionsData, importPrivate, swKeyPublic,
                &wrapperKeyHandle, &name, &npsessionsDataOut);
        printf("\n Load Imported Handle: 0x%x | rval 0x%x \n", wrapperKeyHandle, rval);
    }
    if(rval == TSS2_RC_SUCCESS)
    {
        rval = Tss2_Sys_FlushContext( sysContext, wrapperKeyHandle);
        printf("\n Flush Loaded key Handle : 0x%8.8x| rval 0x%x \n", wrapperKeyHandle, rval);
    }
#endif

    // Nullify TPM password
    memset(tpm_pwd, 0, tpm_pwd_len);

    return rval;
}

TSS2_TCTI_CONTEXT* tpm_tcti_tabrmd_init (void)
{
    TSS2_TCTI_CONTEXT *tcti_ctx;
    TSS2_RC rc;
    size_t size;

    rc = tss2_tcti_tabrmd_init(NULL, &size);
    if (rc != TSS2_RC_SUCCESS) {
        printf ("Failed to get size for TABRMD TCTI context: 0x%x", rc);
        return NULL;
    }

    tcti_ctx = (TSS2_TCTI_CONTEXT*)calloc (1, size);
    if (tcti_ctx == NULL) {
        printf ("Allocation for TABRMD TCTI context failed: %s",
                 strerror (errno));
        return NULL;
    }
    rc = tss2_tcti_tabrmd_init (tcti_ctx, &size);
    if (rc != TSS2_RC_SUCCESS) {
        printf ("Failed to initialize TABRMD TCTI context: 0x%x", rc);
        free (tcti_ctx);
        return NULL;
    }

    return tcti_ctx;
}

TSS2_SYS_CONTEXT* sys_ctx_init (TSS2_TCTI_CONTEXT *tcti_ctx)
{
    TSS2_SYS_CONTEXT *sys_ctx;
    TSS2_RC rc;
    size_t size;
    TSS2_ABI_VERSION abi_version = {
        .tssCreator = TSSWG_INTEROP,
        .tssFamily  = TSS_SAPI_FIRST_FAMILY,
        .tssLevel   = TSS_SAPI_FIRST_LEVEL,
        .tssVersion = TSS_SAPI_FIRST_VERSION,
    };

    size = Tss2_Sys_GetContextSize (0);
    sys_ctx = (TSS2_SYS_CONTEXT*)calloc (1, size);
    if (sys_ctx == NULL) {
        fprintf (stderr,
                 "Failed to allocate 0x%zx bytes for the SAPI context\n",
                 size);
        return NULL;
    }
    rc = Tss2_Sys_Initialize (sys_ctx, size, tcti_ctx, &abi_version);
    if (rc != TSS2_RC_SUCCESS) {
        fprintf (stderr, "Failed to initialize SAPI context: 0x%x\n", rc);
        free (sys_ctx);
        return NULL;
    }

    return sys_ctx;
}

TSS2_RC TeardownTctiContext( TSS2_TCTI_CONTEXT *tctiContext )
{
    ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->finalize( tctiContext );
    free (tctiContext);
    tctiContext = NULL;
    return TSS2_RC_SUCCESS;
}

void TeardownSysContext( TSS2_SYS_CONTEXT **sysContext )
{
    if( *sysContext != 0 ) {
        Tss2_Sys_Finalize(*sysContext);
        free(*sysContext);
        *sysContext = 0;
    }
}

/* helper functions */
#if 0
TSS2_RC TPM2ReadPublicPortion(TSS2_SYS_CONTEXT *sysContext, TPM_HANDLE objectHandle, TPM2B_PUBLIC *publicPortion )
{
    TPM_RC rval = TPM_RC_SUCCESS;
    TPM2B_NAME qualifiedName;
    TPM2B_NAME name;
    INIT_SIMPLE_TPM2B_SIZE( name );
    INIT_SIMPLE_TPM2B_SIZE( qualifiedName );

    if(publicPortion == NULL) {
        printf("Input reference for TPM2B_PUBLIC structure is NULL");
        return TPM_RC_FAILURE;
    }

    publicPortion->t.size = 0;
    rval = Tss2_Sys_ReadPublic( sysContext, objectHandle , 0, publicPortion, &name, &qualifiedName, 0 );
    if(rval != TPM_RC_SUCCESS)
    {
        printf("\n Tss2_Sys_ReadPublic failed: 0x%x ! for TPM handle: 0x%x \n", rval, objectHandle);
    }

    return rval;
}
#endif