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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
* 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. Generates TPM duplication data
// Author: Arun Kumar Sekar
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/pem.h>
#include <pthread.h>
#include <sapi/tpm20.h>
#include <tcti/tcti_socket.h>
#include "tpm_duplication_aux.h"
#include "util.h"
void PrintHelp();
char version[] = "0.1";
void PrintHelp()
{
printf(
"OSSL key to tpm import tool, Version %s\nUsage:"
"./ossl_tpm_duplicate [-pemfile InputPemFile] [-pempwd inputPemPwd (optional)] [-parentPub inputParentPubFile]"
"[-dupPub out_dupPubFile] [-dupPriv out_dupPrivFile] [-dupSymSeed out_dupSymSeedFile] [-dupEncKey out_dupEncKeyFile] \n"
"\n"
, version);
}
static TPM_RC convert_PEM_To_EVP(EVP_PKEY **evpPkey, /* freed by caller */
const char *pem_Filename,
const char *pem_pwd)
{
TPM_RC rc = 0;
FILE *fp_pemfile = NULL;
if (rc == 0)
{
fp_pemfile = fopen(pem_Filename, "rb"); /* closed @2 */
if(fp_pemfile == NULL) {
rc = EXIT_FAILURE;
}
}
if (rc == 0)
{
*evpPkey = PEM_read_PrivateKey(fp_pemfile, NULL, NULL, (void *)pem_pwd);
if (*evpPkey == NULL)
{
printf("convert_PEM_To_EVP: Error reading key file %s\n", pem_Filename);
rc = EXIT_FAILURE;
}
printf("PEM_read_PrivateKey success for file: %s \n", pem_Filename);
}
end:
if (fp_pemfile != NULL)
{
fclose(fp_pemfile);
}
return rc;
}
static TPM_RC convert_EVP_to_RSA(RSA **rsaKey, /* freed by caller */
EVP_PKEY *evpPkey)
{
TPM_RC rc = 0;
if (rc == 0)
{
*rsaKey = EVP_PKEY_get1_RSA(evpPkey);
if (*rsaKey == NULL)
{
printf("convert_EVP_to_RSA: EVP_PKEY_get1_RSA failed\n");
rc = EXIT_FAILURE;
}
printf("convert_EVP_to_RSA success! \n");
}
return rc;
}
int main(int argc, char* argv[])
{
TPM_RC rval = 0;
int count=0;
char pem_Filename[256];
int pemfile_flag = 0;
const char *pem_pwd = ""; /* default empty password */
int pempwd_flag = 0;
// SW Key Duplicate I/P variables
char parent_pub_Filename[256];
int parent_pub_flag = 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;
TPM2B_DATA encryptionKey;
TPM2B_PUBLIC swKeyPublic;
TPM2B_PRIVATE swKeyPrivate;
TPM2B_ENCRYPTED_SECRET encSymSeed;
unsigned short file_size = 0;
UINT8 policyDigest[32] = {0};
UINT32 digestSize = 0;
TPM2B_PUBLIC parentKeyPublicPortion;
int pubKeysize = 0;
// RSA key structures
EVP_PKEY *evpPkey = NULL;
RSA *rsaKey = NULL;
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], "-pemfile" ) ) {
count++;
if( (1 != sscanf( argv[count], "%s", pem_Filename )) )
{
PrintHelp();
return 1;
}
pemfile_flag = 1;
}
else if( 0 == strcmp( argv[count], "-pempwd" ) ) {
count++;
pem_pwd = argv[count];
pempwd_flag = 1;
}
else if( 0 == strcmp( argv[count], "-parentPub" ) ) {
count++;
if( (1 != sscanf( argv[count], "%s", parent_pub_Filename )) )
{
PrintHelp();
return 1;
}
parent_pub_flag = 1;
}
else 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], "--help" ) ) {
PrintHelp();
exit(1);
}
else {
PrintHelp();
exit(1);
}
}
}
// For Duplicate functionality, check all input params are present
if( (!pemfile_flag) ||
(!parent_pub_flag) ||
(!dupPub_flag) ||
(!dupPriv_flag) ||
(!dupSymSeed_flag) ||
(!dupEncKey_flag)
)
{
printf("Error: One or more Inputs for Duplicate are not passed as input \n");
return -1;
}
printf("Input PEM file name: %s \n", pem_Filename);
if (rval == 0) {
rval = convert_PEM_To_EVP(&evpPkey, pem_Filename, pem_pwd);
}
if (rval == 0) {
rval = convert_EVP_to_RSA(&rsaKey, evpPkey);
}
if ( rval == 0 ) {
file_size = sizeof(TPM2B_PUBLIC);
rval = loadDataFromFile(parent_pub_Filename, (UINT8 *) &parentKeyPublicPortion, &file_size);
}
/* SW key duplicate operation started */
if ( rval == 0 ) {
rval = swKeyDuplicate(rsaKey, &parentKeyPublicPortion, policyDigest, digestSize,
&encryptionKey, &swKeyPublic, &swKeyPrivate, &encSymSeed);
if(rval != 0) {
printf("\nswKeyDuplicate failed: 0x%x ! \n", rval);
goto end;
}
else {
printf("\nswKeyDuplicate success: 0x%x ! \n", rval);
rval = saveDataToFile(dupPub_Filename, (UINT8 *) &swKeyPublic, sizeof(TPM2B_PUBLIC));
rval = saveDataToFile(dupPriv_Filename, (UINT8 *) &swKeyPrivate, sizeof(TPM2B_PRIVATE));
rval = saveDataToFile(dupSymSeed_Filename, (UINT8 *) &encSymSeed, sizeof(TPM2B_ENCRYPTED_SECRET));
rval = saveDataToFile(dupEncKey_Filename, (UINT8 *) &encryptionKey, sizeof(TPM2B_DATA));
printf("\nOutput files are written successfully ! \n");
}
}
end:
if (rsaKey != NULL) {
RSA_free(rsaKey);
}
if (evpPkey != NULL) {
EVP_PKEY_free(evpPkey);
}
return rval;
}
|