aboutsummaryrefslogtreecommitdiffstats
path: root/tpm2-simulator-vtpm
blob: c5c0c14c87ab91b905a9b9ba9dac237414282720 (plain)
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
#!/usr/bin/env python

import io
import socket
import struct
import tpm2
from argparse import ArgumentParser
from argparse import FileType
from fcntl import ioctl
from ctypes import Structure
from ctypes import c_uint32
from ctypes import addressof

VTPM_FLAG_TPM2 = 1
VTPM_IOC_NEW_DEV = 0x4014a100


class c_vtpm_new_dev(Structure):
    _fields_ = \
        [('flags', c_uint32),
         ('tpm_num', c_uint32),
         ('fd', c_uint32),
         ('major', c_uint32),
         ('minor', c_uint32)]


def main():
    parser = ArgumentParser(description='Run a TPM 2.0 simulator proxy')
    parser.add_argument('--host', dest='host', metavar='NAMEALG',
                        help='Address of the simulator',
                        type=str, default='localhost')
    args = parser.parse_args()

    sim = tpm2.Simulator(args.host)

    client = tpm2.Client(flags = tpm2.Client.FLAG_DEBUG, simulator = sim)
    startup_cmd = struct.pack('B' * 12,
        0x80, 0x01,
        0x00, 0x00, 0x00, 0x0C,
        0x00, 0x00, 0x01, 0x44,
        0x00, 0x00)
    client.send_cmd(startup_cmd)

    new_dev = c_vtpm_new_dev(flags = VTPM_FLAG_TPM2)

    with open("/dev/vtpmx", "rb+") as vtpmx:
        ioctl(vtpmx, VTPM_IOC_NEW_DEV, addressof(new_dev))

        fp = io.open(new_dev.fd, 'rb+', buffering = 0)

        while True:
            stream = fp.read(4096)
            try:
                resp = client.send_cmd(stream)
            except tpm2.ProtocolError, e:
                print str(e)
            fp.write(resp)


if __name__ == '__main__':
    main()