File: linuxtpm.go

package info (click to toggle)
golang-github-google-go-tpm 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: makefile: 13
file content (36 lines) | stat: -rw-r--r-- 788 bytes parent folder | download | duplicates (2)
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
//go:build !windows

// Package linuxtpm provides access to a physical TPM device via the device file.
package linuxtpm

import (
	"errors"
	"fmt"
	"os"

	"github.com/google/go-tpm/tpm2/transport"
)

var (
	// ErrFileIsNotDevice indicates that the TPM file mode was not a device.
	ErrFileIsNotDevice = errors.New("TPM file is not a device")
)

// Open opens the TPM device file at the given path.
func Open(path string) (transport.TPMCloser, error) {
	fi, err := os.Stat(path)
	if err != nil {
		return nil, err
	}

	if fi.Mode()&os.ModeDevice == 0 {
		return nil, fmt.Errorf("%w: %s (%s)", ErrFileIsNotDevice, fi.Mode().String(), path)
	}
	var f *os.File
	f, err = os.OpenFile(path, os.O_RDWR, 0600)
	if err != nil {
		return nil, err
	}

	return transport.FromReadWriteCloser(f), nil
}