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
|
#include <sys/socket.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/un.h>
#include <unistd.h>
#include <openssl/bn.h>
#include <openssl/sha.h>
#include <lib/base/eerror.h>
#include "etpm.h"
eTPM::eTPM()
{
struct sockaddr_un addr;
unsigned char buf[8];
unsigned int tag;
size_t len;
unsigned char *val;
level2_cert_read = level3_cert_read = false;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, TPMD_SOCKET);
fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
{
eDebug("[eTPM] socket error");
return;
}
if (connect(fd, (const struct sockaddr *)&addr, SUN_LEN(&addr)) < 0)
{
eDebug("[eTPM] connect error");
return;
}
buf[0] = TPMD_DT_LEVEL2_CERT;
buf[1] = TPMD_DT_LEVEL3_CERT;
if (!send_cmd(TPMD_CMD_GET_DATA, buf, 2))
{
return;
}
val = (unsigned char*)recv_cmd(&tag, &len);
if (val == NULL)
{
return;
}
parse_data(val, len);
free(val);
}
eTPM::~eTPM()
{
}
bool eTPM::send_cmd(enum tpmd_cmd cmd, const void *data, size_t len)
{
unsigned char buf[len + 4];
buf[0] = (cmd >> 8) & 0xff;
buf[1] = (cmd >> 0) & 0xff;
buf[2] = (len >> 8) & 0xff;
buf[3] = (len >> 0) & 0xff;
memcpy(&buf[4], data, len);
if (write(fd, buf, sizeof(buf)) != (ssize_t)sizeof(buf))
{
fprintf(stderr, "%s: incomplete write\n", __func__);
return false;
}
return true;
}
void* eTPM::recv_cmd(unsigned int *tag, size_t *len)
{
unsigned char buf[4];
void *val;
if (read(fd, buf, 4) != 4)
{
fprintf(stderr, "%s: incomplete read\n", __func__);
return NULL;
}
*tag = (buf[0] << 8) | buf[1];
*len = (buf[2] << 8) | buf[3];
val = malloc(*len);
if (val == NULL)
return NULL;
ssize_t rd = read(fd, val, *len);
if (rd < 0)
{
perror("eTPM::recv_cmd read");
free(val);
}
else if ((size_t)rd != *len) {
fprintf(stderr, "%s: incomplete read\n", __func__);
free(val);
return NULL;
}
return val;
}
void eTPM::parse_data(const unsigned char *data, size_t datalen)
{
unsigned int i;
unsigned int tag;
unsigned int len;
const unsigned char *val;
for (i = 0; i < datalen; i += len) {
tag = data[i++];
len = data[i++];
val = &data[i];
switch (tag) {
case TPMD_DT_LEVEL2_CERT:
if (len != 210)
break;
memcpy(level2_cert, val, 210);
level2_cert_read = true;
break;
case TPMD_DT_LEVEL3_CERT:
if (len != 210)
break;
memcpy(level3_cert, val, 210);
level3_cert_read = true;
break;
}
}
}
std::string eTPM::getCert(cert_type type)
{
if (type == TPMD_DT_LEVEL2_CERT && level2_cert_read)
return std::string((char*)level2_cert, 210);
else if (type == TPMD_DT_LEVEL3_CERT && level3_cert_read)
return std::string((char*)level3_cert, 210);
return "";
}
std::string eTPM::challenge(std::string rnd)
{
if (rnd.length() == 8)
{
if (!send_cmd(TPMD_CMD_COMPUTE_SIGNATURE, rnd.c_str(), 8))
return "";
unsigned int tag;
size_t len;
unsigned char *val = (unsigned char*)recv_cmd(&tag, &len);
if (tag != TPMD_CMD_COMPUTE_SIGNATURE)
return "";
std::string ret((char*)val, len);
free(val);
return ret;
}
return "";
}
|