diff --git a/linux/device.go b/linux/device.go index 9838633..9a2ae37 100644 --- a/linux/device.go +++ b/linux/device.go @@ -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) { @@ -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 } @@ -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 } @@ -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) @@ -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 @@ -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 } } @@ -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) @@ -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 diff --git a/linux/device_test.go b/linux/device_test.go index fb5049d..2ad030b 100644 --- a/linux/device_test.go +++ b/linux/device_test.go @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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), }) } @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) } diff --git a/linux/export_test.go b/linux/export_test.go index 3331516..32664cc 100644 --- a/linux/export_test.go +++ b/linux/export_test.go @@ -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, @@ -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,