-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtpm.go
42 lines (33 loc) · 948 Bytes
/
tpm.go
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
37
38
39
40
41
42
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/google/go-tpm/tpm2"
"github.com/paulgriffiths/pgtpm"
)
// getTPM opens the TPM associated with the named device. If the named device
// cannot be found, and the name is in the form hostname:port, an attempt will
// be made to open a connection with a Microsoft TPM 2.0 Simulator listening on
// that port.
func getTPM(name string) (io.ReadWriteCloser, error) {
if name == "" {
name = defaultTPMDevice
}
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) && len(strings.Split(name, ":")) == 2 {
s, err := pgtpm.NewMSSimulator(name)
if err != nil {
return nil, fmt.Errorf("failed to initialize MS TPM simulator: %v", err)
}
return s, nil
}
return nil, fmt.Errorf("failed to locate TPM device: %v", err)
}
t, err := tpm2.OpenTPM(name)
if err != nil {
return nil, fmt.Errorf("failed to open TPM device: %v", err)
}
return t, nil
}