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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/*
* 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.
*/
//
// main.c : Tool to import Openssl RSA key into TPM
// Author: Arun Kumar Sekar
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sapi/tpm20.h>
#include "tpm_wrapper.h"
#include "util.h"
void PrintHelp();
char version[] = "0.1";
void PrintHelp()
{
printf(
"OSSL key to tpm import tool, Version %s\nUsage:"
"./ossl_tpm_import "
"[-dupPub out_dupPubFile] [-dupPriv out_dupPrivFile] [-dupSymSeed out_dupSymSeedFile] "
"[-dupEncKey out_dupEncKeyFile] [-password keyPassword] "
"[-pub out_keyPub] [-priv out_KeyPriv] [-H primaryKeyHandle]\n"
"\n"
, version);
}
int main(int argc, char* argv[])
{
TPM_RC rval = 0;
int count=0;
TSS2_TCTI_CONTEXT *tcti_ctx = 0;
TSS2_SYS_CONTEXT *sysContext = 0;
// SW Key Duplicate O/P variables
char dupPub_Filename[256];
int dupPub_flag = 0;
char dupPriv_Filename[256];
int dupPriv_flag = 0;
char dupSymSeed_Filename[256];
int dupSymSeed_flag = 0;
char dupEncKey_Filename[256];
int dupEncKey_flag = 0;
char keyPassword[256] = {0};
TPM2B_DATA encryptionKey;
TPM2B_PUBLIC swKeyPublic;
TPM2B_PRIVATE swKeyPrivate;
TPM2B_ENCRYPTED_SECRET encSymSeed;
// SW Key Import O/P variables
char pub_Filename[256];
int pub_flag = 0;
char priv_Filename[256];
int priv_flag = 0;
unsigned short file_size = 0;
TPM_HANDLE primaryKeyHandle = 0;
int H_flag = 0;
TPM2B_PUBLIC parentKeyPublicPortion;
int pubKeysize = 0;
setbuf(stdout, NULL);
setvbuf (stdout, NULL, _IONBF, BUFSIZ);
if( (argc < 2) )
{
printf("Arguments count does not match \n");
PrintHelp();
return 1;
}
else
{
/* Get the argument values and evaluate it */
for( count = 1; count < argc; count++ )
{
if( 0 == strcmp( argv[count], "-dupPub" ) ) {
count++;
if( (1 != sscanf( argv[count], "%s", dupPub_Filename )) )
{
PrintHelp();
return 1;
}
dupPub_flag = 1;
}
else if( 0 == strcmp( argv[count], "-dupPriv" ) ) {
count++;
if( (1 != sscanf( argv[count], "%s", dupPriv_Filename )) )
{
PrintHelp();
return 1;
}
dupPriv_flag = 1;
}
else if( 0 == strcmp( argv[count], "-dupSymSeed" ) ) {
count++;
if( (1 != sscanf( argv[count], "%s", dupSymSeed_Filename )) )
{
PrintHelp();
return 1;
}
dupSymSeed_flag = 1;
}
else if( 0 == strcmp( argv[count], "-dupEncKey" ) ) {
count++;
if( (1 != sscanf( argv[count], "%s", dupEncKey_Filename )) )
{
PrintHelp();
return 1;
}
dupEncKey_flag = 1;
}
else if( 0 == strcmp( argv[count], "-password" ) ) {
count++;
// Read no more than a fixed length of characters
if ( (1 != sscanf(argv[count], "%255s", keyPassword )) )
{
PrintHelp();
return 1;
}
}
else if( 0 == strcmp( argv[count], "-pub" ) ) {
count++;
if( (1 != sscanf( argv[count], "%s", pub_Filename )) )
{
PrintHelp();
return 1;
}
pub_flag = 1;
}
else if( 0 == strcmp( argv[count], "-priv" ) ) {
count++;
if( (1 != sscanf( argv[count], "%s", priv_Filename )) )
{
PrintHelp();
return 1;
}
priv_flag = 1;
}
else if( 0 == strcmp( argv[count], "-H" ) ) {
count++;
primaryKeyHandle = strtoul(argv[count], NULL, 16);
printf("Primary Key handle Given: 0x%x \n", primaryKeyHandle);
H_flag = 1;
}
else if( 0 == strcmp( argv[count], "--help" ) ) {
PrintHelp();
exit(1);
}
else {
PrintHelp();
exit(1);
}
}
}
if((!H_flag)) {
printf("Parent handle should be passed for TPM import operation \n");
return -1;
}
// For TPM Import functionality, check all input params are present
if( (!dupPub_flag) ||
(!dupPriv_flag) ||
(!dupSymSeed_flag) ||
(!dupEncKey_flag) ||
(!pub_flag) ||
(!priv_flag)
) {
printf("Error: One or more Inputs for TPM import functionality is missing ! \n");
return -1;
}
/* SW Key TPM Import operation started */
if(rval == 0) {
file_size = sizeof(TPM2B_PUBLIC);
rval = loadDataFromFile(dupPub_Filename, (UINT8 *) &swKeyPublic, &file_size);
if ( rval == 0 ) {
file_size = sizeof(TPM2B_PRIVATE);
rval = loadDataFromFile(dupPriv_Filename, (UINT8 *) &swKeyPrivate, &file_size);
}
if ( rval == 0 ) {
file_size = sizeof(TPM2B_ENCRYPTED_SECRET);
rval = loadDataFromFile(dupSymSeed_Filename, (UINT8 *) &encSymSeed, &file_size);
}
if ( rval == 0 ) {
file_size = sizeof(TPM2B_DATA);
rval = loadDataFromFile(dupEncKey_Filename, (UINT8 *) &encryptionKey, &file_size);
}
if ( rval == 0 ) {
/* Initialize TCTI and sapi context */
tcti_ctx = tpm_tcti_tabrmd_init();
if(tcti_ctx == NULL) {
printf("Creation of TCTI context with TABRMD failed ! \n");
goto end;
}
sysContext = sys_ctx_init(tcti_ctx);
if(sysContext == NULL) {
printf("Creation of SAPI context with TABRMD failed ! \n");
goto end;
}
printf("\nInitializing TPM context success: 0x%x ! \n", rval);
}
TPM2B_PRIVATE importPrivate;
INIT_SIMPLE_TPM2B_SIZE(importPrivate);
rval = swKeyTpmImport(sysContext, primaryKeyHandle,
&encryptionKey, &swKeyPublic, &swKeyPrivate, &encSymSeed,
keyPassword, strlen(keyPassword),
&importPrivate);
if(rval != 0) {
printf("\nswKeyTpmImport failed: 0x%x ! \n", rval);
goto end;
}
else {
printf("\nswKeyImport success: 0x%x ! \n", rval);
saveDataToFile(pub_Filename, (UINT8 *) &swKeyPublic, sizeof(TPM2B_PUBLIC));
saveDataToFile(priv_Filename, (UINT8 *) &importPrivate, sizeof(TPM2B_PRIVATE));
printf("\nOutput files are written successfully ! \n");
}
}
end:
if(sysContext) {
TeardownSysContext(&sysContext);
}
if(tcti_ctx) {
TeardownTctiContext(tcti_ctx);
}
return rval;
}
|