swtpm

repository·master·Indexed 18 days ago

https://github.com/stefanberger/swtpm

A suite of TPM (Trusted Platform Module) emulators that interface with libtpms. It supports multiple front-end interfaces including TCP/IP, Unix sockets, and the Linux CUSE interface. The package includes specialized tools such as swtpm_setup for manufacturing simulation (EK and certificate provisioning), swtpm_bios for simulating system firmware behavior, swtpm_cert for certificate creation, and swtpm_ioctl for out-of-band control of CUSE TPMs.

Tokens
3.2K
Snippets
16
Records
22
Agent score
74%

What's inside swtpm

  1. Overview of swtpm_bios

    master
    swtpm_bios is a tool designed to simulate the behavior of system firmware (BIOS/UEFI) by sending the TPM commands that a real BIOS would typically send to a Trusted Platform Module (TPM). It is used to facilitate the interaction between firmware-level operations and a TPM instance.
  2. Overview of swtpm_setup

    master

    swtpm_setup is a tool designed to simulate the manufacturing process of a Trusted Platform Module (TPM). It allows users to prepare the initial state of a TPM by performing the following sequence of operations:

    1. EK Creation: Instructing the TPM to create an Endorsement Key (EK).
    2. Certificate Generation: Creating the EK and platform certificates.
    3. NVRAM Provisioning: Saving these certificates into the TPM's Non-Volatile RAM (NVRAM) before the TPM is used for the first time.

    This ensures the TPM starts in a pre-provisioned state suitable for subsequent use in simulated environments.

  3. Overview of SWTPM TPM Emulators

    master

    SWTPM is a package of TPM (Trusted Platform Module) emulators that interface with libtpms. It allows for the creation of emulated TPMs accessible through various front-end interfaces.

    Supported interfaces include:

    • Socket interfaces: TCP/IP and Unix sockets.
    • Linux CUSE interface: Enables the creation of multiple native /dev/vtpm* devices.
  4. Use swtpm_ioctl to send control commands to CUSE TPM

    master

    The swtpm_ioctl tool is used to send out-of-band control commands to the CUSE TPM (swtpm_cuse). This allows for management and control operations that are handled outside the standard TPM command interface.

    For detailed command usage and available ioctls, refer to the manual page: man swtpm_ioctl.

  5. Available SWTPM tools and functionality

    master

    Beyond the core emulators, the SWTPM package includes several specialized tools for TPM lifecycle management and simulation:

    • TPM Usage: Tools to interact with and use the emulated TPM.
    • Certificate Management: Tools for creating certificates for a TPM.
    • Manufacturing Simulation: Tools to simulate the manufacturing process, such as creating an Endorsement Key (EK) and platform certificates.

    For detailed instructions on specific tools, refer to the individual README files located in the src/ directory of the repository.

  6. Run swtpm using Docker Compose

    master

    You can use the provided docker-compose.yml to orchestrate a swtpm instance and a test client. The setup uses a shared volume tpm-data to persist TPM state and a dedicated network swtpm for communication.

    Service Configuration

    swtpm service

    This service runs the TPM simulator in socket mode. Key configurations include:

    • Command arguments:
      • --tpm2: Enables TPM 2.0 support.
      • --server type=unixio,path=/swtpm/swtpm.sock: Configures the server to use a Unix domain socket at /swtpm/swtpm.sock.
      • --ctrl type=unixio,path=/swtpm/swtpm.sock.ctrl: Configures the control socket at /swtpm/swtpm.sock.ctrl.
      • --tpmstate dir=/swtpm: Sets the directory for TPM state persistence.
      • --log file=swtpm.log and --log level=20: Configures logging.
      • --flags not-need-init,startup-clear: Sets specific TPM startup flags.
    • Volumes: Mounts tpm-data to /swtpm to ensure state persistence.

    swtpm-test service

    This service acts as a client to verify the TPM functionality. It depends on the swtpm service being ready.

    • Environment Variables:
      • TPM2TOOLS_TCTI: Set to swtpm:path=/swtpm/swtpm.sock to instruct TPM2 tools to communicate with the simulator via the Unix socket.
    • Command: Runs tpm2 clear to test the TPM interface.
    services:
      swtpm:
        build:
          context: .
        volumes:
          - tpm-data:/swtpm
        networks:
          - swtpm
        command: ['socket', '--tpm2',
                  '--server', 'type=unixio,path=/swtpm/swtpm.sock',
                  '--ctrl', 'type=unixio,path=/swtpm/swtpm.sock.ctrl',
                  '--tpmstate', 'dir=/swtpm',
                  '--log', 'file=swtpm.log',
                  '--log', 'level=20',
                  '--flags', 'not-need-init,startup-clear']
    
      swtpm-test:
        image: docker.io/strongx509/tpm:5.9.13
        depends_on:
          - swtpm
        volumes:
          - tpm-data:/swtpm
        networks:
          - swtpm
        environment:
          TPM2TOOLS_TCTI: swtpm:path=/swtpm/swtpm.sock
        command: ['tpm2', 'clear']
    
    volumes:
      tpm-data:
    
    networks:
      swtpm:
  7. Use the swtpm CLI to start a TPM emulator

    master

    The swtpm command-line tool simulates a TPM (Trusted Platform Module). To start the emulator, you must specify a TPM interface type as the first argument. The available interfaces depend on how the binary was compiled.

    Interface Types

    • socket: The standard interface type.
    • chardev: Character device interface (available if WITH_CHARDEV is enabled).
    • cuse: CUSE (Character device in Userspace) interface (available if WITH_CUSE is enabled).

    Basic Usage

    # Start using the socket interface
    swtpm socket [options]
    
    # Start using the chardev interface (if supported)
    swtpm chardev [options]
    
    # Start using the cuse interface (if supported)
    swtpm cuse [options]

    To see the specific options available for a chosen interface, use the --help flag after the interface type (e.g., swtpm socket --help).

    swtpm socket [options]
  8. Use swtpm_cuse_main for CUSE-based TPM simulation

    master

    If the project is compiled with HAVE_SWTPM_CUSE_MAIN defined, you can use swtpm_cuse_main. This entry point allows running swtpm using the CUSE (Character device in USERspace) framework, enabling the TPM to appear as a character device in the filesystem.

    #ifdef HAVE_SWTPM_CUSE_MAIN
    int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *iface);
    #endif
  9. Manage the TPM server lifecycle with the `server` struct

    master

    The server abstraction manages the underlying file descriptor and connection parameters (such as Unix socket paths) for the TPM server interface. It supports different transport modes like TCP, Unix sockets, or CUSE via the file descriptor and flags provided during initialization.

    To use the server interface, you typically initialize it with server_new, interact with its file descriptor using server_get_fd, and ensure resources are released with server_free.

    // Initialize a new server instance
    struct server *s = server_new(fd, flags, "/tmp/swtpm.sock");
    
    if (s != NULL) {
        int current_fd = server_get_fd(s);
        // ... use the server ...
        
        // Clean up resources and close the connection
        server_free(s);
    }
  10. Use swtpm_main to run the standard swtpm CLI

    master

    The swtpm_main function serves as the primary entry point for the standard swtpm command-line tool. It initializes and executes the TPM simulation logic based on the provided command-line arguments.

    int swtpm_main(int argc, char **argv, const char *prgname, const char *iface);