Skip to content

Commit

Permalink
tpm2: Allow for tpm2.OpenTPM() to use default paths on Linux (#256)
Browse files Browse the repository at this point in the history
This is a _breaking_ change.

Signed-off-by: Joe Richey <[email protected]>
  • Loading branch information
josephlr authored Jul 27, 2021
1 parent 43d309c commit b3942ee
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions tpm2/open_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,41 @@
package tpm2

import (
"errors"
"fmt"
"io"
"os"

"github.com/google/go-tpm/tpmutil"
)

// OpenTPM opens a channel to the TPM at the given path. If the file is a
// device, then it treats it like a normal TPM device, and if the file is a
// Unix domain socket, then it opens a connection to the socket.
func OpenTPM(path string) (io.ReadWriteCloser, error) {
rwc, err := tpmutil.OpenTPM(path)
//
// This function may also be invoked with no paths, as tpm2.OpenTPM(). In this
// case, the default paths on Linux (/dev/tpmrm0 then /dev/tpm0), will be used.
func OpenTPM(path ...string) (tpm io.ReadWriteCloser, err error) {
switch len(path) {
case 0:
tpm, err = tpmutil.OpenTPM("/dev/tpmrm0")
if errors.Is(err, os.ErrNotExist) {
tpm, err = tpmutil.OpenTPM("/dev/tpm0")
}
case 1:
tpm, err = tpmutil.OpenTPM(path[0])
default:
return nil, errors.New("cannot specify multiple paths to tpm2.OpenTPM")
}
if err != nil {
return nil, err
}

// Make sure this is a TPM 2.0
_, err = GetManufacturer(rwc)
_, err = GetManufacturer(tpm)
if err != nil {
rwc.Close()
tpm.Close()
return nil, fmt.Errorf("open %s: device is not a TPM 2.0", path)
}
return rwc, nil
return tpm, nil
}

0 comments on commit b3942ee

Please sign in to comment.