-
Notifications
You must be signed in to change notification settings - Fork 23
/
unsealtotp.c
99 lines (84 loc) · 2.04 KB
/
unsealtotp.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
/*
* sealtotp - generate a TOTP secret and seal it to the local TPM
*
* Copyright 2015 Matthew Garrett <[email protected]>
*
* Portions derived from unsealfile.c by J. Kravitz and Copyright (C) 2004 IBM
* Corporation
*
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include "tpmfunc.h"
#include <liboath/oath.h>
#define keylen 20
char key[keylen];
const char efivarfs[] = "/sys/firmware/efi/efivars/";
int main(int argc, char *argv[])
{
int ret;
struct stat sbuf;
uint32_t parhandle; /* handle of parent key */
unsigned char blob[4096]; /* resulting sealed blob */
unsigned int bloblen = 322; /* blob length */
unsigned char passptr1[20] = {0};
int fd, outlen, i;
char totp[7];
parhandle = 0x40000000;
ret = TPM_NV_ReadValue(0x10004d47, 0, 322, blob, &bloblen, NULL);
if (ret) {
for (i=1; i<argc; i++) {
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
argv++;
}
}
if (fd < 0) {
perror("Unable to open file");
return -1;
}
ret = fstat(fd, &sbuf);
if (ret) {
perror("Unable to stat file");
return -1;
}
bloblen = sbuf.st_size;
ret = read(fd, blob, bloblen);
if (ret != bloblen) {
fprintf(stderr, "Unable to read data\n");
return -1;
}
if (strncmp(argv[1], efivarfs, strlen(efivarfs)) == 0) {
bloblen -= sizeof(int);
memmove (blob, blob + sizeof(int), bloblen);
}
}
ret = TPM_Unseal(parhandle, /* KEY Entity Value */
passptr1, /* Key Password */
NULL,
blob, bloblen,
key, &outlen);
if (ret == 24) {
fprintf(stderr, "TPM refused to decrypt key - boot process attests that it is modified\n");
return -1;
}
if (ret != 0) {
printf("Error %s from TPM_Unseal\n", TPM_GetErrMsg(ret));
exit(6);
}
if (outlen != keylen) {
fprintf(stderr, "Returned buffer is incorrect length\n");
return -1;
}
ret = oath_totp_generate(key, keylen, time(NULL), 30, 0, 6, totp);
if (ret != 0) {
fprintf(stderr, "Error generating totp value\n");
return -1;
}
printf("%s\n", totp);
}