Skip to content

Commit

Permalink
linux: make the TPM version an enumerated type
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisccoulson committed Dec 14, 2023
1 parent 13acfd1 commit 9f69e9b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
32 changes: 21 additions & 11 deletions linux/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ var devices tpmDevices
// Deprecated: Use Tcti
type TctiDevice = Tcti

// TPMMajorVersion describes the major version of a TPM device.
type TPMMajorVersion int

const (
TPMVersion1 TPMMajorVersion = 1
TPMVersion2 TPMMajorVersion = 2
)

// TPMDevice represents a Linux TPM character device.
type TPMDevice struct {
path string
sysfsPath string
version int
version TPMMajorVersion
}

func (d *TPMDevice) openInternal() (*Tcti, *os.File, error) {
Expand Down Expand Up @@ -89,8 +97,8 @@ func (d *TPMDevice) SysfsPath() string {
return d.sysfsPath
}

// MajorVersion indicates the TPM version, either 1 or 2.
func (d *TPMDevice) MajorVersion() int {
// MajorVersion indicates the TPM version.
func (d *TPMDevice) MajorVersion() TPMMajorVersion {
return d.version
}

Expand Down Expand Up @@ -148,7 +156,7 @@ func (d *TPMDeviceRaw) PhysicalPresenceInterface() (ppi.PPI, error) {
func (d *TPMDeviceRaw) ResourceManagedDevice() (*TPMDeviceRM, error) {
d.rmOnce.Do(func() {
d.rm, d.rmErr = func() (*TPMDeviceRM, error) {
if d.version != 2 {
if d.version != TPMVersion2 {
// the kernel resource manager is only available for TPM2 devices.
return nil, ErrNoResourceManagedDevice
}
Expand Down Expand Up @@ -211,7 +219,7 @@ func OpenDevice(path string) (*Tcti, error) {
return tcti, nil
}

func tpmDeviceVersion(path string) (int, error) {
func tpmDeviceVersion(path string) (TPMMajorVersion, error) {
versionPath := filepath.Join(path, "tpm_version_major")

versionBytes, err := ioutil.ReadFile(versionPath)
Expand All @@ -224,11 +232,11 @@ func tpmDeviceVersion(path string) (int, error) {
_, err := os.Stat(filepath.Join(path, "pcrs"))
switch {
case os.IsNotExist(err):
return 2, nil
return TPMVersion2, nil
case err != nil:
return 0, err
default:
return 1, nil
return TPMVersion1, nil
}
case err != nil:
return 0, err
Expand All @@ -237,10 +245,12 @@ func tpmDeviceVersion(path string) (int, error) {
if err != nil {
return 0, err
}
if version < 1 || version > 2 {
switch version {
case 1, 2:
return TPMMajorVersion(version), nil
default:
return 0, fmt.Errorf("unexpected version %d", version)
}
return version, nil
}
}

Expand Down Expand Up @@ -308,7 +318,7 @@ func ListTPM2Devices() (out []*TPMDeviceRaw, err error) {
return nil, err
}
for _, device := range candidates {
if device.MajorVersion() != 2 {
if device.MajorVersion() != TPMVersion2 {
continue
}
out = append(out, device)
Expand Down Expand Up @@ -338,7 +348,7 @@ func DefaultTPM2Device() (*TPMDeviceRaw, error) {
if err != nil {
return nil, err
}
if device.MajorVersion() != 2 {
if device.MajorVersion() != TPMVersion2 {
return nil, ErrDefaultNotTPM2Device
}
return device, nil
Expand Down
56 changes: 28 additions & 28 deletions linux/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *deviceSuite) TestListTPMDevicesTPM2(c *C) {
devices, err := ListTPMDevices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0),
})
}

Expand All @@ -60,7 +60,7 @@ func (s *deviceSuite) TestListTPMDevicesTPM2OldKernel(c *C) {
devices, err := ListTPMDevices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0),
})
}

Expand All @@ -80,7 +80,7 @@ func (s *deviceSuite) TestListTPMDevicesTPM1(c *C) {
devices, err := ListTPMDevices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"), 1, 0),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"), TPMVersion1, 0),
})
}

Expand All @@ -91,8 +91,8 @@ func (s *deviceSuite) TestListTPMDevicesMixedTPM2(c *C) {
devices, err := ListTPMDevices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm1"), 1, 1),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm1"), TPMVersion1, 1),
})
}

Expand All @@ -103,8 +103,8 @@ func (s *deviceSuite) TestListTPMDevicesMixedTPM1(c *C) {
devices, err := ListTPMDevices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"), 1, 0),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm1"), 2, 1),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"), TPMVersion1, 0),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm1"), TPMVersion2, 1),
})
}

Expand All @@ -115,8 +115,8 @@ func (s *deviceSuite) TestListTPMDevicesTPM2Multiple(c *C) {
devices, err := ListTPMDevices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/MSFT0101:00/tpm/tpm0"), 2, 0),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm1"), 2, 1),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/MSFT0101:00/tpm/tpm0"), TPMVersion2, 0),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm1"), TPMVersion2, 1),
})
}

Expand All @@ -127,7 +127,7 @@ func (s *deviceSuite) TestListTPM2DevicesTPM2(c *C) {
devices, err := ListTPM2Devices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0),
})
}

Expand All @@ -138,7 +138,7 @@ func (s *deviceSuite) TestListTPM2DevicesTPM2OldKernel(c *C) {
devices, err := ListTPM2Devices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0),
})
}

Expand Down Expand Up @@ -167,7 +167,7 @@ func (s *deviceSuite) TestListTPM2DevicesMixedTPM2(c *C) {
devices, err := ListTPM2Devices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0),
})
}

Expand All @@ -178,7 +178,7 @@ func (s *deviceSuite) TestListTPM2DevicesMixedTPM1(c *C) {
devices, err := ListTPM2Devices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm1"), 2, 1),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm1"), TPMVersion2, 1),
})
}

Expand All @@ -189,8 +189,8 @@ func (s *deviceSuite) TestListTPM2DevicesTPM2Multiple(c *C) {
devices, err := ListTPM2Devices()
c.Check(err, IsNil)
c.Check(devices, DeepEquals, []*TPMDeviceRaw{
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/MSFT0101:00/tpm/tpm0"), 2, 0),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm1"), 2, 1),
NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/MSFT0101:00/tpm/tpm0"), TPMVersion2, 0),
NewMockTPMDeviceRaw("/dev/tpm1", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm1"), TPMVersion2, 1),
})
}

Expand All @@ -200,7 +200,7 @@ func (s *deviceSuite) TestDefaultTPMDeviceTPM2(c *C) {

device, err := DefaultTPMDevice()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0))
}

func (s *deviceSuite) TestDefaultTPMDeviceTPM2OldKernel(c *C) {
Expand All @@ -209,7 +209,7 @@ func (s *deviceSuite) TestDefaultTPMDeviceTPM2OldKernel(c *C) {

device, err := DefaultTPMDevice()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0))
}

func (s *deviceSuite) TestDefaultTPMDeviceNoDevices(c *C) {
Expand All @@ -226,7 +226,7 @@ func (s *deviceSuite) TestDefaultTPMDeviceTPM1(c *C) {

device, err := DefaultTPMDevice()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"), 1, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"), TPMVersion1, 0))
}

func (s *deviceSuite) TestDefaultTPMDeviceMixedTPM2(c *C) {
Expand All @@ -235,7 +235,7 @@ func (s *deviceSuite) TestDefaultTPMDeviceMixedTPM2(c *C) {

device, err := DefaultTPMDevice()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0))
}

func (s *deviceSuite) TestDefaultTPMDeviceMixedTPM1(c *C) {
Expand All @@ -244,7 +244,7 @@ func (s *deviceSuite) TestDefaultTPMDeviceMixedTPM1(c *C) {

device, err := DefaultTPMDevice()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"), 1, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"), TPMVersion1, 0))
}

func (s *deviceSuite) TestDefaultTPMDeviceTPM2Multiple(c *C) {
Expand All @@ -253,7 +253,7 @@ func (s *deviceSuite) TestDefaultTPMDeviceTPM2Multiple(c *C) {

device, err := DefaultTPMDevice()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/MSFT0101:00/tpm/tpm0"), 2, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/MSFT0101:00/tpm/tpm0"), TPMVersion2, 0))
}

func (s *deviceSuite) TestDefaultTPM2DeviceTPM2(c *C) {
Expand All @@ -262,7 +262,7 @@ func (s *deviceSuite) TestDefaultTPM2DeviceTPM2(c *C) {

device, err := DefaultTPM2Device()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0))
}

func (s *deviceSuite) TestDefaultTPM2DeviceTPM2OldKernel(c *C) {
Expand All @@ -271,7 +271,7 @@ func (s *deviceSuite) TestDefaultTPM2DeviceTPM2OldKernel(c *C) {

device, err := DefaultTPM2Device()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0))
}

func (s *deviceSuite) TestDefaultTPM2DeviceNoDevices(c *C) {
Expand All @@ -296,7 +296,7 @@ func (s *deviceSuite) TestDefaultTPM2DeviceMixedTPM2(c *C) {

device, err := DefaultTPM2Device()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), 2, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"), TPMVersion2, 0))
}

func (s *deviceSuite) TestDefaultTPM2DeviceMixedTPM1(c *C) {
Expand All @@ -313,7 +313,7 @@ func (s *deviceSuite) TestDefaultTPM2DeviceTPM2Multiple(c *C) {

device, err := DefaultTPM2Device()
c.Check(err, IsNil)
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/MSFT0101:00/tpm/tpm0"), 2, 0))
c.Check(device, DeepEquals, NewMockTPMDeviceRaw("/dev/tpm0", filepath.Join(sysfsPath, "devices/platform/MSFT0101:00/tpm/tpm0"), TPMVersion2, 0))
}

func (s *deviceSuite) TestTPMDeviceMethodsTPM2(c *C) {
Expand All @@ -324,7 +324,7 @@ func (s *deviceSuite) TestTPMDeviceMethodsTPM2(c *C) {
c.Assert(err, IsNil)
c.Check(device.Path(), Equals, "/dev/tpm0")
c.Check(device.SysfsPath(), Equals, filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpm/tpm0"))
c.Check(device.MajorVersion(), Equals, 2)
c.Check(device.MajorVersion(), Equals, TPMVersion2)
}

func (s *deviceSuite) TestTPMDeviceMethodsTPM1(c *C) {
Expand All @@ -335,7 +335,7 @@ func (s *deviceSuite) TestTPMDeviceMethodsTPM1(c *C) {
c.Assert(err, IsNil)
c.Check(device.Path(), Equals, "/dev/tpm0")
c.Check(device.SysfsPath(), Equals, filepath.Join(sysfsPath, "devices/platform/SMO3324:00/tpm/tpm0"))
c.Check(device.MajorVersion(), Equals, 1)
c.Check(device.MajorVersion(), Equals, TPMVersion1)
}

func (s *deviceSuite) TestTPMDeviceRawResourceManagedDeviceTPM2(c *C) {
Expand All @@ -347,7 +347,7 @@ func (s *deviceSuite) TestTPMDeviceRawResourceManagedDeviceTPM2(c *C) {

rm, err := device.ResourceManagedDevice()
c.Check(err, IsNil)
c.Check(rm, DeepEquals, NewMockTPMDeviceRM("/dev/tpmrm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpmrm/tpmrm0"), 2, device))
c.Check(rm, DeepEquals, NewMockTPMDeviceRM("/dev/tpmrm0", filepath.Join(sysfsPath, "devices/platform/STM0125:00/tpmrm/tpmrm0"), TPMVersion2, device))
c.Check(rm.RawDevice(), Equals, device)
}

Expand Down
4 changes: 2 additions & 2 deletions linux/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func MockSysfsPath(path string) (restore func()) {
}
}

func NewMockTPMDeviceRaw(path, sysfsPath string, version, devno int) *TPMDeviceRaw {
func NewMockTPMDeviceRaw(path, sysfsPath string, version TPMMajorVersion, devno int) *TPMDeviceRaw {
return &TPMDeviceRaw{
TPMDevice: TPMDevice{
path: path,
Expand All @@ -24,7 +24,7 @@ func NewMockTPMDeviceRaw(path, sysfsPath string, version, devno int) *TPMDeviceR
}
}

func NewMockTPMDeviceRM(path, sysfsPath string, version int, raw *TPMDeviceRaw) *TPMDeviceRM {
func NewMockTPMDeviceRM(path, sysfsPath string, version TPMMajorVersion, raw *TPMDeviceRaw) *TPMDeviceRM {
return &TPMDeviceRM{
TPMDevice: TPMDevice{
path: path,
Expand Down

0 comments on commit 9f69e9b

Please sign in to comment.