Debian Codegraph @

tpm2/transport/linuxtpm/linuxtpm.go

raw
1 //go:build !windows
2
3 // Package linuxtpm provides access to a physical TPM device via the device file.
4 package linuxtpm
5
6 import (
7 "errors"
8 "fmt"
9 "os"
10
11 "github.com/google/go-tpm/tpm2/transport"
12 )
13
14 var (
15 // ErrFileIsNotDevice indicates that the TPM file mode was not a device.
16 ErrFileIsNotDevice = errors.New("TPM file is not a device")
17 )
18
19 // Open opens the TPM device file at the given path.
20 func Open(path string) (transport.TPMCloser, error) {
21 fi, err := os.Stat(path)
22 if err != nil {
23 return nil, err
24 }
25
26 if fi.Mode()&os.ModeDevice == 0 {
27 return nil, fmt.Errorf("%w: %s (%s)", ErrFileIsNotDevice, fi.Mode().String(), path)
28 }
29 var f *os.File
30 f, err = os.OpenFile(path, os.O_RDWR, 0600)
31 if err != nil {
32 return nil, err
33 }
34
35 return transport.FromReadWriteCloser(f), nil
36 }