forked from mjg59/tpmtotp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sealtotp.c
610 lines (516 loc) · 14.5 KB
/
sealtotp.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
* sealtotp - generate a TOTP secret and seal it to the local TPM
*
* Copyright 2015 Matthew Garrett <[email protected]>
*
* Copyright 2015 Andreas Fuchs, Fraunhofer SIT
*
* Portions derived from sealfile.c by J. Kravitz and Copyright (C) 2004 IBM
* Corporation
*
* Portions derived from qrenc.c Copyright (C) 2006-2012 Kentaro Fukuchi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#undef CONFIG_TSS // don't use the large tspi library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <qrencode.h>
#ifdef CONFIG_TSS
#include <tss/tspi.h>
#else
#include "tpmfunc.h"
#include "tpm_error.h"
#endif
#include "base32.h"
#define keylen 20
char key[keylen];
static int margin=1;
static char efivarfs[] = "/sys/firmware/efi/efivars/";
#define LEN 20
#define NUM_PCRS 24
static uint8_t **pcrvals;
#define CHECK_MALLOC(res) if (res == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); return -1; }
void print_help()
{
char *help = "Usage: sealtotop [OPTION]... OUTFILE\n"
" -h, --help: Print this help.\n"
" -n, --nvram: store the secret in nvram rather than a file.\n"
" -s, --no-qrcode: Don't print the secret as QR code.\n"
" -b, --base32: Print the secret as a base32 string.\n"
" -p, --pcrs: A comma-separated list of PCRs to use. Use n=X to manually set the value \n"
" of PCR n to X, where X is the hex representation of the desired value.\n"
" Default: 0,1,2,3,4,5,7,8,9.\n"
" The hex representation may contain spaces between the bytes like XX XX.\n";
printf("%s", help);
}
void convert_hex_to_bytes(char *hex_vals, uint8_t* res)
{
int i = 0;
unsigned int tmp = 0;
for (i = 0; i < LEN; i++) {
sscanf(hex_vals + 2*i, "%02x", &tmp);
res[i] = (uint8_t)tmp;
}
}
unsigned int parse_pcrs(char *optarg)
{
uint32_t pcrflag = 0;
char *token = strtok(optarg, ",");
while (token != NULL) {
unsigned int num = (unsigned int)strtol(token, NULL, 10);
if (num >= NUM_PCRS) {
fprintf(stderr, "Invalid PCR number: %u. Must be between 0 and %u.\n",
num, NUM_PCRS-1);
return -1;
}
pcrflag |= (1 << num);
char *val = strchr(token, '=');
uint8_t *res = NULL;
if (val != NULL) { // PCR value supplied
val+=1; // ignore delimiter
res = malloc(LEN * sizeof(uint8_t));
CHECK_MALLOC(res);
if (strlen(val) == LEN*2) // no spaces
convert_hex_to_bytes(val, res);
else if (strlen(val) == LEN*3-1) { // spaces between each byte (like FF FF), remove them.
char *tmp = malloc(2*LEN*sizeof(uint8_t));
CHECK_MALLOC(tmp);
size_t i = 0;
size_t j = 0;
for (i = 0; i < strlen(val); i++) {
if (val[i] != ' ')
tmp[j++] = val[i];
}
if (j != LEN*2) {
fprintf(stdout, "Invalid format for PCR %u\n", num);
free(res);
return -1;
}
else {
convert_hex_to_bytes(tmp, res);
}
free(tmp);
} else { // invalid string
fprintf(stdout, "Invalid format for PCR %u\n", num);
return -1;
}
}
pcrvals[num] = res;
token = strtok(NULL, ",");
}
return pcrflag;
}
static void writeANSI_margin(FILE* fp, int realwidth,
char* buffer, int buffer_s,
char* white, int white_s )
{
(void) buffer_s;
int y;
strncpy(buffer, white, white_s);
memset(buffer + white_s, ' ', realwidth * 2);
strcpy(buffer + white_s + realwidth * 2, "\033[0m\n"); // reset to default colors
for(y=0; y<margin; y++ ){
fputs(buffer, fp);
}
}
static int writeANSI(QRcode *qrcode)
{
FILE *fp;
unsigned char *row, *p;
int x, y;
int realwidth;
int last, size;
char *white, *black, *buffer;
int white_s, black_s, buffer_s;
white = "\033[47m";
white_s = 5;
black = "\033[40m";
black_s = 5;
size = 1;
fp = stdout;
realwidth = (qrcode->width + margin * 2) * size;
buffer_s = ( realwidth * white_s ) * 2;
buffer = (char *)malloc( buffer_s );
if(buffer == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(1);
}
/* top margin */
writeANSI_margin(fp, realwidth, buffer, buffer_s, white, white_s);
/* data */
p = qrcode->data;
for(y=0; y<qrcode->width; y++) {
row = (p+(y*qrcode->width));
memset( buffer, 0, buffer_s );
strncpy( buffer, white, white_s );
for(x=0; x<margin; x++ ){
strncat( buffer, " ", 2 );
}
last = 0;
for(x=0; x<qrcode->width; x++) {
if(*(row+x)&0x1) {
if( last != 1 ){
strncat( buffer, black, black_s );
last = 1;
}
} else {
if( last != 0 ){
strncat( buffer, white, white_s );
last = 0;
}
}
strncat( buffer, " ", 2 );
}
if( last != 0 ){
strncat( buffer, white, white_s );
}
for(x=0; x<margin; x++ ){
strncat( buffer, " ", 2 );
}
strncat( buffer, "\033[0m\n", 5 );
fputs( buffer, fp );
}
/* bottom margin */
writeANSI_margin(fp, realwidth, buffer, buffer_s, white, white_s);
fclose(fp);
free(buffer);
return 0;
}
int generate_key() {
int fd = open("/dev/urandom", O_RDONLY);
int ret;
if (fd < 0) {
perror("Unable to open urandom");
return -1;
}
ret = read(fd, key, sizeof(key));
if (ret != 20) {
fprintf(stderr, "Unable to read from urandom");
return -1;
}
return 0;
}
#ifdef CONFIG_TSS
int TSPI_SealCurrPCR(TSS_HCONTEXT c, uint32_t keyhandle, uint32_t pcrmap,
unsigned char *keyauth,
unsigned char *dataauth,
unsigned char *data, unsigned int datalen,
unsigned char *blob, unsigned int *bloblen)
{
#define CHECK_ERROR(r,m) if (r != TSS_SUCCESS) { fprintf(stderr, m ": 0x%08x\n", r); return -1;}
TSS_RESULT r = 0;
TSS_HTPM tpm;
TSS_HPCRS pcrComposite;
TSS_UUID uuid;
TSS_UUID srk_uuid = TSS_UUID_SRK;
TSS_HKEY key;
TSS_HENCDATA seal;
TSS_HPOLICY key_policy, seal_policy;
unsigned char *cipher;
unsigned int cipher_len;
/* Get the PCR values into composite object */
r = Tspi_Context_GetTpmObject(c, &tpm);
CHECK_ERROR(r, "Error Getting TPM");
r = Tspi_Context_CreateObject(c, TSS_OBJECT_TYPE_PCRS, TSS_PCRS_STRUCT_INFO_LONG, &pcrComposite);
CHECK_ERROR(r, "Error Creating PCR-Composite");
r = Tspi_PcrComposite_SetPcrLocality(pcrComposite, TPM_LOC_ZERO | TPM_LOC_ONE |
TPM_LOC_TWO | TPM_LOC_THREE | TPM_LOC_FOUR);
CHECK_ERROR(r, "Error Setting Localities");
for (uint32_t pcrmask = 1, pcr = 0; pcr < NUM_PCRS; pcr++, pcrmask <<= 1) {
if ((pcrmap & pcrmask) != 0) {
uint32_t pcrval_size;
uint8_t *pcrval;
if (pcrvals[pcr] == NULL) {
r = Tspi_TPM_PcrRead(tpm, pcr, &pcrval_size, &pcrval);
CHECK_ERROR(r, "Error Reading PCR");
r = Tspi_PcrComposite_SetPcrValue(pcrComposite, pcr, pcrval_size, pcrval);
CHECK_ERROR(r, "Error Setting Composite");
r = Tspi_Context_FreeMemory(c, pcrval);
CHECK_ERROR(r, "Error Freeing Memory");
}
else {
pcrval = pcrvals[pcr];
r = Tspi_PcrComposite_SetPcrValue(pcrComposite, pcr, LEN, pcrval);
CHECK_ERROR(r, "Error Setting Composite");
}
}
}
/* Get the SRK and Policy Ready */
if (keyhandle = 0x40000000) {
uuid = srk_uuid;
} else {
fprintf(stderr, "Error, only SRK currently supported\n");
r = 1;
return -1;
}
r = Tspi_Context_LoadKeyByUUID(c, TSS_PS_TYPE_SYSTEM, uuid, &key);
CHECK_ERROR(r, "Error Loading Key");
r = Tspi_Context_CreateObject(c, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, &key_policy);
CHECK_ERROR(r, "Error Creating Policy");
r = Tspi_Policy_SetSecret(key_policy, TSS_SECRET_MODE_SHA1, keylen, keyauth);
CHECK_ERROR(r, "Error Setting Secret");
r = Tspi_Policy_AssignToObject(key_policy, key);
CHECK_ERROR(r, "Error Assigning Policy");
/* Get the Encdata Ready */
r = Tspi_Context_CreateObject(c, TSS_OBJECT_TYPE_ENCDATA, TSS_ENCDATA_SEAL, &seal);
CHECK_ERROR(r, "Error Creating EncData");
r = Tspi_Context_CreateObject(c, TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, &seal_policy);
CHECK_ERROR(r, "Error Creating Policy");
r = Tspi_Policy_SetSecret(seal_policy, TSS_SECRET_MODE_SHA1, keylen, dataauth);
CHECK_ERROR(r, "Error Setting Secret");
r = Tspi_Policy_AssignToObject(seal_policy, seal);
CHECK_ERROR(r, "Error Assigning Policy");
/* Seal the Data */
r = Tspi_Data_Seal(seal, key, datalen, data, pcrComposite);
CHECK_ERROR(r, "Error Sealing Data");
r = Tspi_GetAttribData(seal, TSS_TSPATTRIB_ENCDATA_BLOB,
TSS_TSPATTRIB_ENCDATABLOB_BLOB,
&cipher_len, &cipher);
CHECK_ERROR(r, "Error Getting Sealed Data");
/* Return that stuff */
if (cipher_len > bloblen) {
sprintf(stderr, "Internal Error, cipher too long");
r = 1;
return -1;
}
memcpy(blob, cipher, cipher_len);
*bloblen = cipher_len;
/* Note: Do not even bother to return cipher directly. Would be freed during Context_Close anyways */
Tspi_Context_FreeMemory(c, cipher);
return (r == 0)? 0 : -1;
}
#endif
int main(int argc, char *argv[])
{
int ret;
char base32_key[BASE32_LEN(keylen)+1];
unsigned char uefiblob[4100];
unsigned char blob[4096]; /* resulting sealed blob */
unsigned int bloblen; /* blob length */
unsigned char wellknown[20] = {0};
unsigned char totpstring[64];
uint32_t pcrmask = 0x000003BF; // PCRs 0-5 and 7-9
const char * hostname = "TPMTOTP";
char *outfile_name;
FILE *infile;
FILE *outfile;
QRcode *qrcode;
#ifdef CONFIG_TSS
TSS_HCONTEXT context;
#endif
if (generate_key()) {
return -1;
}
pcrvals = malloc(NUM_PCRS * sizeof(uint8_t*));
CHECK_MALLOC(pcrvals);
static int base32_flag = 0;
static int qr_flag = 1;
static int nvram_flag = 0;
int option_index = 0;
static struct option long_options[] = {
{"pcrs", required_argument, 0, 'p'},
{"nvram", no_argument, &nvram_flag, 1},
{"no-qrcode", no_argument, &qr_flag, 0},
{"base32", no_argument, &base32_flag, 1},
{"help", no_argument, 0, 'h'},
{0,0,0,0}
};
if (argc == 1) {
print_help();
return -1;
}
int c;
while ((c = getopt_long(argc, argv, "p:nbh", long_options, &option_index)) != -1) {
switch (c) {
case 0: // Flag only
break;
case 'p':
pcrmask = parse_pcrs(optarg);
if (pcrmask == (uint32_t) -1)
return -1;
break;
case 'h':
print_help();
return 0;
case 'b':
base32_flag = 1;
break;
case 'n':
nvram_flag = 1;
break;
case 's':
qr_flag = 0;
break;
case '?': // Unrecognized Option
print_help();
return -1;
default:
print_help();
}
}
if (!nvram_flag) {
if (optind == argc) {
fprintf(stderr, "Output name required!\n");
print_help();
return -1;
} else {
outfile_name = argv[optind];
}
}
base32_encode(key, keylen, base32_key);
base32_key[BASE32_LEN(keylen)] = NULL;
#ifdef CONFIG_TSS
ret = Tspi_Context_Create(&context);
if (ret != TSS_SUCCESS) {
fprintf(stderr, "Unable to create TPM context\n");
return -1;
}
ret = Tspi_Context_Connect(context, NULL);
if (ret != TSS_SUCCESS) {
fprintf(stderr, "Unable to connect to TPM\n");
return -1;
}
ret = TSPI_SealCurrPCR(context, // context
0x40000000, // SRK
pcrmask,
wellknown, // Well-known SRK secret
wellknown, // Well-known SEAL secret
key, keylen, /* data to be sealed */
blob, &bloblen); /* buffer to receive result */
#else
ret = TPM_SealCurrPCR(
0x40000000, // SRK
pcrmask,
wellknown, // Well-known SRK secret
wellknown, // Well-known SEAL secret
key, keylen, /* data to be sealed */
blob, &bloblen); /* buffer to receive result */
#endif
if (ret != 0) {
fprintf(stderr, "Error %s from TPM_Seal\n",
TPM_GetErrMsg(ret));
//goto out;
}
sprintf(totpstring, "otpauth://totp/%s?secret=%s", hostname, base32_key);
//sprintf(totpstring, "%s", base32_key);
if (base32_flag) {
printf("%s\n", base32_key);
}
printf("%s\n", totpstring);
if (qr_flag) {
qrcode = QRcode_encodeString(totpstring, 0, QR_ECLEVEL_L,
QR_MODE_8, 1);
writeANSI(qrcode);
}
if (nvram_flag) {
uint32_t nvindex = 0x00004d47;
uint32_t permissions = TPM_NV_PER_OWNERREAD|TPM_NV_PER_OWNERWRITE;
#ifdef CONFIG_TSS
TSS_HNVSTORE nvObject;
TSS_FLAG nvattrs = 0;
ret = Tspi_Context_CreateObject(context, TSS_OBJECT_TYPE_NV,
nvattrs, &nvObject);
if (ret != TSS_SUCCESS) {
fprintf(stderr, "Unable to create nvram object: %x\n",
ret);
goto out;
}
ret = Tspi_SetAttribUint32(nvObject, TSS_TSPATTRIB_NV_INDEX, 0,
nvindex);
if (ret != TSS_SUCCESS) {
fprintf(stderr, "Unable to set index\n");
goto out;
}
ret = Tspi_SetAttribUint32(nvObject,
TSS_TSPATTRIB_NV_PERMISSIONS, 0,
permissions);
if (ret != TSS_SUCCESS) {
fprintf(stderr, "Unable to set permissions\n");
goto out;
}
ret = Tspi_SetAttribUint32(nvObject, TSS_TSPATTRIB_NV_DATASIZE,
0, bloblen);
if (ret != TSS_SUCCESS) {
fprintf(stderr, "Unable to set size\n");
goto out;
}
ret = Tspi_NV_DefineSpace(nvObject, NULL, NULL);
if (ret != TSS_SUCCESS && ret != (0x3000 | TSS_E_NV_AREA_EXIST)) {
fprintf(stderr, "Unable to define space: %x\n", ret);
goto out;
}
ret = Tspi_NV_WriteValue(nvObject, 0, bloblen, blob);
if (ret != TSS_SUCCESS) {
fprintf(stderr, "Unable to write to nvram\n");
goto out;
}
#else
unsigned char * ownauth = wellknown;
unsigned char * areaauth = wellknown;
TPM_PCR_INFO_SHORT *pcrInfoRead = NULL;
TPM_PCR_INFO_SHORT *pcrInfoWrite = NULL;
ret = TPM_NV_DefineSpace2(
ownauth,
nvindex,
bloblen,
permissions,
areaauth,
pcrInfoRead,
pcrInfoWrite);
//if (ret != TPM_SUCCESS && ret != (0x3000 | TSS_E_NV_AREA_EXIST)) {
if (ret != TPM_SUCCESS) {
fprintf(stderr, "Unable to define space: %x\n", ret);
goto out;
}
ret = TPM_NV_WriteValue(
nvindex,
0,
blob,
bloblen,
ownauth
);
if (ret != TPM_SUCCESS) {
fprintf(stderr, "Unable to write to nvram\n");
goto out;
}
#endif
} else {
outfile = fopen(outfile_name, "w");
if (outfile == NULL) {
fprintf(stderr, "Unable to open output file '%s'\n",
outfile_name);
goto out;
}
if (strncmp(outfile_name, efivarfs, strlen(efivarfs)) == 0) {
int attributes = 7; // NV, RT, BS
memcpy(uefiblob, &attributes, sizeof(int));
memcpy(uefiblob + sizeof(int), blob, bloblen);
bloblen += sizeof(int);
ret = fwrite(uefiblob, 1, bloblen, outfile);
} else {
ret = fwrite(blob, 1, bloblen, outfile);
}
if (ret != bloblen) {
fprintf(stderr,
"I/O Error while writing output file '%s'\n",
outfile_name);
goto out;
}
}
out:
#ifdef CONFIG_TSS
Tspi_Context_FreeMemory(context, NULL);
Tspi_Context_Close(context);
#endif
exit(ret);
}