1 #include <sys/socket.h>
9 #include <openssl/bn.h>
10 #include <openssl/sha.h>
11 #include <lib/base/etpm.h>
17 struct sockaddr_un addr;
19 unsigned int tag, len;
22 level2_cert_read = level3_cert_read = false;
24 addr.sun_family = AF_UNIX;
25 strcpy(addr.sun_path, TPMD_SOCKET);
27 fd = socket(PF_UNIX, SOCK_STREAM, 0);
29 eDebug("[eTPM] socket error");
33 if (connect(fd, (const struct sockaddr *)&addr, SUN_LEN(&addr)) < 0) {
34 eDebug("[eTPM] connect error");
38 buf[0] = TPMD_DT_LEVEL2_CERT;
39 buf[1] = TPMD_DT_LEVEL3_CERT;
40 if (!send_cmd(TPMD_CMD_GET_DATA, buf, 2))
45 val = (unsigned char*)recv_cmd(&tag, &len);
60 bool eTPM::send_cmd(enum tpmd_cmd cmd, const void *data, unsigned int len)
62 unsigned char buf[len + 4];
64 buf[0] = (cmd >> 8) & 0xff;
65 buf[1] = (cmd >> 0) & 0xff;
66 buf[2] = (len >> 8) & 0xff;
67 buf[3] = (len >> 0) & 0xff;
68 memcpy(&buf[4], data, len);
70 if (write(fd, buf, sizeof(buf)) != (ssize_t)sizeof(buf)) {
71 fprintf(stderr, "%s: incomplete write\n", __func__);
78 void* eTPM::recv_cmd(unsigned int *tag, unsigned int *len)
83 if (read(fd, buf, 4) != 4) {
84 fprintf(stderr, "%s: incomplete read\n", __func__);
88 *tag = (buf[0] << 8) | buf[1];
89 *len = (buf[2] << 8) | buf[3];
95 if (read(fd, val, *len) != (ssize_t)*len) {
96 fprintf(stderr, "%s: incomplete read\n", __func__);
104 void eTPM::parse_data(const unsigned char *data, unsigned int datalen)
109 const unsigned char *val;
111 for (i = 0; i < datalen; i += len) {
117 case TPMD_DT_LEVEL2_CERT:
120 memcpy(level2_cert, val, 210);
121 level2_cert_read = true;
123 case TPMD_DT_LEVEL3_CERT:
126 memcpy(level3_cert, val, 210);
127 level3_cert_read = true;
133 PyObject *eTPM::getCert(cert_type type)
135 if (type == TPMD_DT_LEVEL2_CERT && level2_cert_read)
136 return PyBuffer_FromMemory(level2_cert, 210);
137 else if (type == TPMD_DT_LEVEL3_CERT && level3_cert_read)
138 return PyBuffer_FromMemory(level3_cert, 210);
143 PyObject *eTPM::challenge(PyObject* rnd)
145 if (PyString_Check(rnd) && PyString_Size(rnd) == 8)
147 char* buf = PyString_AsString(rnd);
148 if (!send_cmd(TPMD_CMD_COMPUTE_SIGNATURE, buf, 8))
151 unsigned int tag, len;
152 unsigned char *val = (unsigned char*)recv_cmd(&tag, &len);
154 if (tag != TPMD_CMD_COMPUTE_SIGNATURE)
157 return PyBuffer_FromMemory(val, len);