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
|
/*
* Licensed Materials - Property of IBM
*
* trousers - An open source TCG Software Stack
*
* (C) Copyright International Business Machines Corp. 2004, 2005
*
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include "trousers/tss.h"
#include "trousers_types.h"
#include "linux/tpm.h"
#include "tcslog.h"
#include "tddl.h"
struct tpm_device_node tpm_device_nodes[] = {
{"/dev/tpm0", TDDL_UNDEF, TDDL_UNDEF},
{"/udev/tpm0", TDDL_UNDEF, TDDL_UNDEF},
{"/dev/tpm", TDDL_UNDEF, TDDL_UNDEF},
{NULL, 0, 0}
};
struct tpm_device_node *opened_device = NULL;
BYTE txBuffer[TDDL_TXBUF_SIZE];
TSS_BOOL use_in_socket = FALSE;
struct tcsd_config *_tcsd_options = NULL;
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#include <fcntl.h>
int
open_device()
{
int i = 0, fd = -1, tcp_device_port;
char *tcp_device_hostname = NULL;
char *un_socket_device_path = NULL;
char *tcp_device_port_string = NULL;
if (getenv("TCSD_USE_TCP_DEVICE")) {
if ((tcp_device_hostname = getenv("TCSD_TCP_DEVICE_HOSTNAME")) == NULL)
tcp_device_hostname = "localhost";
if ((un_socket_device_path = getenv("TCSD_UN_SOCKET_DEVICE_PATH")) == NULL)
un_socket_device_path = "/var/run/tpm/tpmd_socket:0";
if ((tcp_device_port_string = getenv("TCSD_TCP_DEVICE_PORT")) != NULL)
tcp_device_port = atoi(tcp_device_port_string);
else
tcp_device_port = 6545;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd > 0) {
struct hostent *host = gethostbyname(tcp_device_hostname);
if (host != NULL) {
struct sockaddr_in addr;
memset(&addr, 0x0, sizeof(addr));
addr.sin_family = host->h_addrtype;
addr.sin_port = htons(tcp_device_port);
memcpy(&addr.sin_addr,
host->h_addr,
host->h_length);
if (connect(fd, (struct sockaddr *)&addr,
sizeof(addr)) < 0) {
close(fd);
fd = -1;
} else
use_in_socket = TRUE;
} else {
close (fd);
fd = -1;
}
}
if (fd < 0) {
struct sockaddr_un addr;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd >= 0) {
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, un_socket_device_path,
sizeof(addr.sun_path));
if (connect(fd, (void *)&addr, sizeof(addr)) < 0) {
close(fd);
fd = -1;
}
}
}
}
if (fd < 0) {
/* tpm_device_paths is filled out in tddl.h */
for (i = 0; tpm_device_nodes[i].path != NULL; i++) {
errno = 0;
if ((fd = open(tpm_device_nodes[i].path, O_RDWR)) >= 0)
break;
}
}
if (fd > 0) {
opened_device = &(tpm_device_nodes[i]);
tpm_device_nodes[i].fd = fd;
}
return fd;
}
TSS_RESULT
Tddli_Open()
{
int rc;
if (opened_device != NULL) {
LogDebug("attempted to re-open the TPM driver!");
return TDDLERR(TDDL_E_ALREADY_OPENED);
}
rc = open_device();
if (rc < 0) {
LogError("Could not find a device to open!");
if (errno == ENOENT) {
/* File DNE */
return TDDLERR(TDDL_E_COMPONENT_NOT_FOUND);
}
return TDDLERR(TDDL_E_FAIL);
}
return TSS_SUCCESS;
}
TSS_RESULT
Tddli_Close()
{
if (opened_device == NULL) {
LogDebug("attempted to re-close the TPM driver!");
return TDDLERR(TDDL_E_ALREADY_CLOSED);
}
close(opened_device->fd);
opened_device->fd = TDDL_UNDEF;
opened_device = NULL;
return TSS_SUCCESS;
}
TSS_RESULT
Tddli_TransmitData(BYTE * pTransmitBuf, UINT32 TransmitBufLen, BYTE * pReceiveBuf,
UINT32 * pReceiveBufLen)
{
int sizeResult;
if (TransmitBufLen > TDDL_TXBUF_SIZE) {
LogError("buffer size handed to TDDL is too large! (%u bytes)", TransmitBufLen);
return TDDLERR(TDDL_E_FAIL);
}
memcpy(txBuffer, pTransmitBuf, TransmitBufLen);
LogDebug("Calling write to driver");
if (use_in_socket) {
Tddli_Close();
if (Tddli_Open())
return TDDLERR(TDDL_E_IOERROR);
}
switch (opened_device->transmit) {
case TDDL_UNDEF:
/* fall through */
case TDDL_TRANSMIT_IOCTL:
errno = 0;
if ((sizeResult = ioctl(opened_device->fd, TPMIOC_TRANSMIT, txBuffer)) != -1) {
opened_device->transmit = TDDL_TRANSMIT_IOCTL;
break;
}
LogWarn("ioctl: (%d) %s", errno, strerror(errno));
LogInfo("Falling back to Read/Write device support.");
/* fall through */
case TDDL_TRANSMIT_RW:
if ((sizeResult = write(opened_device->fd,
txBuffer,
TransmitBufLen)) == (int)TransmitBufLen) {
opened_device->transmit = TDDL_TRANSMIT_RW;
sizeResult = read(opened_device->fd, txBuffer,
TDDL_TXBUF_SIZE);
break;
} else {
if (sizeResult == -1) {
LogError("write to device %s failed: %s",
opened_device->path,
strerror(errno));
} else {
LogError("wrote %d bytes to %s (tried "
"to write %d)", sizeResult,
opened_device->path,
TransmitBufLen);
}
}
/* fall through */
default:
return TDDLERR(TDDL_E_IOERROR);
}
if (sizeResult < 0) {
LogError("read from device %s failed: %s", opened_device->path, strerror(errno));
return TDDLERR(TDDL_E_IOERROR);
} else if (sizeResult == 0) {
LogError("Zero bytes read from device %s", opened_device->path);
return TDDLERR(TDDL_E_IOERROR);
}
if ((unsigned)sizeResult > *pReceiveBufLen) {
LogError("read %d bytes from device %s, (only room for %d)", sizeResult,
opened_device->path, *pReceiveBufLen);
return TDDLERR(TDDL_E_INSUFFICIENT_BUFFER);
}
*pReceiveBufLen = sizeResult;
memcpy(pReceiveBuf, txBuffer, *pReceiveBufLen);
return TSS_SUCCESS;
}
TSS_RESULT
Tddli_GetStatus(UINT32 ReqStatusType, UINT32 *pStatus)
{
return TDDLERR(TSS_E_NOTIMPL);
}
TSS_RESULT
Tddli_SetCapability(UINT32 CapArea, UINT32 SubCap,
BYTE *pSetCapBuf, UINT32 SetCapBufLen)
{
return TDDLERR(TSS_E_NOTIMPL);
}
TSS_RESULT
Tddli_GetCapability(UINT32 CapArea, UINT32 SubCap,
BYTE *pCapBuf, UINT32 *pCapBufLen)
{
return TDDLERR(TSS_E_NOTIMPL);
}
TSS_RESULT Tddli_Cancel(void)
{
int rc;
if (opened_device->transmit == TDDL_TRANSMIT_IOCTL) {
if ((rc = ioctl(opened_device->fd, TPMIOC_CANCEL, NULL)) == -1) {
LogError("ioctl: (%d) %s", errno, strerror(errno));
return TDDLERR(TDDL_E_FAIL);
} else if (rc == -EIO) {
/* The driver timed out while trying to tell the chip to cancel */
return TDDLERR(TDDL_E_COMMAND_COMPLETED);
}
return TSS_SUCCESS;
} else {
return TDDLERR(TSS_E_NOTIMPL);
}
}
|