Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[c85d95]: src / tpm_mgmt / tpm_createek.c Maximize Restore History

Download this file

tpm_createek.c    282 lines (246 with data), 7.3 kB

  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
/*
* The Initial Developer of the Original Code is International
* Business Machines Corporation. Portions created by IBM
* Corporation are Copyright (C) 2005 International Business
* Machines Corporation. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the Common Public License as published by
* IBM Corporation; either version 1 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Common Public License for more details.
*
* You should have received a copy of the Common Public License
* along with this program; if not, a copy can be viewed at
* http://www.opensource.org/licenses/cpl1.0.php.
*/
#include "tpm_tspi.h"
#include "tpm_utils.h"
TSS_HCONTEXT hContext = 0;
#ifdef TSS_LIB_IS_12
#include <limits.h>
//Controled by input options
static char in_filename[PATH_MAX] = "", out_filename[PATH_MAX] = "";
static BOOL isRevocable = FALSE;
static BOOL needGenerateSecret = FALSE;
static BOOL inFileSet = FALSE;
static BOOL outFileSet = FALSE;
static void help(const char *aCmd)
{
logCmdHelp(aCmd);
logCmdOption("-r, --revocable",
_("Creates a revocable EK instead of the default non-revocable one. Requires [-g -o] or [-i]"));
logCmdOption("-i, --infile FILE",
_("Filename containing the secret data used to revoke the EK."));
logCmdOption("-g, --generate-secret",
_("Generates a 20 Bytes secret that is used to revoke the EK. Requires [-o]"));
logCmdOption("-o, --outfile FILE",
_("Filename to write the secret data generated to revoke the EK."));
}
static int parse(const int aOpt, const char *aArg)
{
switch (aOpt){
case 'r':
isRevocable = TRUE;
break;
case 'g':
needGenerateSecret = TRUE;
break;
case 'i':
inFileSet = TRUE;
if (aArg){
strncpy(in_filename, aArg, PATH_MAX);
}
break;
case 'o':
outFileSet = TRUE;
if (aArg){
strncpy(out_filename, aArg, PATH_MAX);
}
break;
default:
return -1;
}
return 0;
}
static TSS_RESULT
tpmCreateRevEk(TSS_HTPM a_hTpm, TSS_HKEY a_hKey,
TSS_VALIDATION * a_pValData, UINT32 *revDataSz, BYTE **revData)
{
TSS_RESULT result = Tspi_TPM_CreateRevocableEndorsementKey(a_hTpm, a_hKey,
a_pValData, revDataSz, revData);
tspiResult("Tspi_TPM_CreateRevocableEndorsementKey", result);
return result;
}
static int readData(UINT32 bytesToRead, BYTE **buffer)
{
FILE *infile = NULL;
size_t iBytes;
int rc = 0;
BYTE eofile;
__memset(*buffer, 0x00, bytesToRead);
infile = fopen(in_filename, "r");
if ( !infile ){
logError(_("Unable to open input file: %s\n"),
in_filename);
return -1;
}
//Read the data
iBytes = fread( *buffer, 1, bytesToRead, infile );
if ( iBytes < bytesToRead ) {
logError(_("Error: the secret data file %s contains less than %d bytes. Aborting ...\n"),
in_filename, bytesToRead);
rc = -1;
} else if ( (iBytes = fread( &eofile, 1, 1, infile )) ) {
//Test if there's more than 20 bytes
if ( !feof( infile))
logMsg(_("WARNING: Using only the first %d bytes of file %s for secret data\n"),
bytesToRead, in_filename);
} else {
logDebug(_("Read %d bytes of secret data from file %s.\n"),
bytesToRead, in_filename);
}
fclose( infile);
return rc;
}
static int writeData(UINT32 bytesToWrite, BYTE *buffer)
{
FILE *outfile = NULL;
size_t iBytes;
int rc = 0;
logDebug(_("bytesToWrite: %d\n"), bytesToWrite);
outfile = fopen(out_filename, "w");
if ( !outfile ) {
logError(_("Unable to open output file: %s\n"), out_filename);
return -1;
}
//Write data in buffer
iBytes = fwrite( buffer, 1, bytesToWrite, outfile);
if ( iBytes != bytesToWrite ) {
logError(_("Error: Unable to write %d bytes on the file %s.\n"),
bytesToWrite, out_filename);
rc = -1;
}
logDebug(_("%zd bytes written on file %s.\n"), iBytes, out_filename);
fclose( outfile );
return rc;
}
#endif
static TSS_RESULT
tpmCreateEk(TSS_HTPM a_hTpm, TSS_HKEY a_hKey,
TSS_VALIDATION * a_pValData)
{
TSS_RESULT result = Tspi_TPM_CreateEndorsementKey(a_hTpm, a_hKey,
a_pValData);
tspiResult("Tspi_TPM_CreateEndorsementKey", result);
return result;
}
int main(int argc, char **argv)
{
TSS_RESULT tResult;
TSS_HTPM hTpm;
TSS_HKEY hEk;
TSS_FLAG fEkAttrs;
int iRc = -1;
#ifdef TSS_LIB_IS_12
struct option opts[] = {{"revocable", no_argument, NULL, 'r'},
{"generate-secret", no_argument, NULL, 'g'},
{"infile", required_argument, NULL, 'i'},
{"outfile", required_argument, NULL, 'o'},
};
UINT32 revDataSz;
BYTE revokeData[TPM_SHA1BASED_NONCE_LEN];
BYTE *pRevData;
#endif
initIntlSys();
#ifdef TSS_LIB_IS_12
if (genericOptHandler(argc, argv, "rgi:o:", opts, sizeof(opts) / sizeof(struct option),
parse, help) != 0)
goto out;
//Check commands for command hierarchy
if (isRevocable) {
if (needGenerateSecret) {
if (!outFileSet) {
logError(_("Please specify an output file\n"));
goto out;
}
if (inFileSet) {
logError(_("The option -i, --infile is not valid with -g\n"));
goto out;
}
} else if (!inFileSet) {
logError(_("Please specify -i, --infile or -g, --generate-secret\n"));
goto out;
} else if (outFileSet) {
logError(_("The option -o, --outfile is not valid with -i, --infile"));
goto out;
}
}
logDebug("Input file name: %s\n", in_filename);
logDebug("Output file name: %s\n", out_filename);
if (inFileSet) {
pRevData = revokeData;
revDataSz = sizeof(revokeData);
if (readData(revDataSz, &pRevData))
goto out;
} else if (outFileSet) {
FILE *outfile = fopen(out_filename, "w");
if (!outfile) {
iRc = -1;
logError(_("Unable to open output file: %s\n"), out_filename);
goto out;
}
fclose(outfile);
//TPM should generate the revoke data
revDataSz = 0;
pRevData = NULL;
}
#else
if (genericOptHandler(argc, argv, NULL, NULL, 0, NULL, NULL) != 0){
logError(_("See man pages for details.\n"));
goto out;
}
#endif
if (contextCreate(&hContext) != TSS_SUCCESS)
goto out;
if (contextConnect(hContext) != TSS_SUCCESS)
goto out_close;
if (contextGetTpm(hContext, &hTpm) != TSS_SUCCESS)
goto out_close;
//Initialize EK attributes here
fEkAttrs = TSS_KEY_SIZE_2048 | TSS_KEY_TYPE_LEGACY;
if (contextCreateObject(hContext, TSS_OBJECT_TYPE_RSAKEY, fEkAttrs, &hEk) != TSS_SUCCESS)
goto out_close;
#ifdef TSS_LIB_IS_12
if (isRevocable){
tResult = tpmCreateRevEk(hTpm, hEk, NULL, &revDataSz, &pRevData);
if (tResult != TSS_SUCCESS)
goto out_close;
//Writes the generated secret into the output file
if (outFileSet) {
if (writeData(revDataSz, pRevData)) {
logError(_("Creating revocable EK succeeded, but writing the EK "
"revoke authorization to disk failed.\nPrinting the "
"revoke authorization instead:\n"));
logHex(revDataSz, pRevData);
logError(_("You should record this data, as its the authorization "
"you'll need to revoke your EK!\n"));
goto out_close;
}
}
} else
#endif
tResult = tpmCreateEk(hTpm, hEk, NULL);
if (tResult != TSS_SUCCESS)
goto out_close;
iRc = 0;
logSuccess(argv[0]);
out_close:
contextClose(hContext);
out:
return iRc;
}