2019-03-28 20:21:16 +00:00
|
|
|
// Copyright 2019 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
// use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations under
|
|
|
|
// the License.
|
2019-03-28 20:29:24 +00:00
|
|
|
|
2019-10-08 04:38:05 +00:00
|
|
|
// +build linux,!gofuzz
|
2019-03-28 20:21:16 +00:00
|
|
|
|
|
|
|
package attest
|
|
|
|
|
|
|
|
import (
|
2020-09-24 18:35:08 +00:00
|
|
|
"errors"
|
2019-03-28 20:21:16 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/go-tpm/tpm2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tpmRoot = "/sys/class/tpm"
|
|
|
|
)
|
|
|
|
|
2020-09-24 18:35:08 +00:00
|
|
|
// This will be initialized if we build with CGO (needed for TPM 1.2 support).
|
|
|
|
var getTPM12Impl func() (*TPM, error)
|
|
|
|
|
2020-11-19 06:43:19 +00:00
|
|
|
// InjectSimulatedTPMForTest returns a fake TPM that interfaces with
|
|
|
|
// the provided simulated TPM. This method should be used for testing
|
|
|
|
// only.
|
|
|
|
func InjectSimulatedTPMForTest(rwc io.ReadWriteCloser) *TPM {
|
|
|
|
return &TPM{tpm: &wrappedTPM20{
|
|
|
|
interf: TPMInterfaceCommandChannel,
|
|
|
|
rwc: &fakeCmdChannel{rwc},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:21:16 +00:00
|
|
|
func probeSystemTPMs() ([]probedTPM, error) {
|
|
|
|
var tpms []probedTPM
|
|
|
|
|
|
|
|
tpmDevs, err := ioutil.ReadDir(tpmRoot)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
for _, tpmDev := range tpmDevs {
|
|
|
|
if strings.HasPrefix(tpmDev.Name(), "tpm") {
|
|
|
|
tpm := probedTPM{
|
|
|
|
Path: path.Join(tpmRoot, tpmDev.Name()),
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(path.Join(tpm.Path, "caps")); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tpm.Version = TPMVersion20
|
|
|
|
} else {
|
|
|
|
tpm.Version = TPMVersion12
|
|
|
|
}
|
|
|
|
tpms = append(tpms, tpm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tpms, nil
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
type linuxCmdChannel struct {
|
|
|
|
io.ReadWriteCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeasurementLog implements CommandChannelTPM20.
|
|
|
|
func (cc *linuxCmdChannel) MeasurementLog() ([]byte, error) {
|
|
|
|
return ioutil.ReadFile("/sys/kernel/security/tpm0/binary_bios_measurements")
|
|
|
|
}
|
2019-03-28 20:21:16 +00:00
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
func openTPM(tpm probedTPM) (*TPM, error) {
|
2019-03-28 20:21:16 +00:00
|
|
|
switch tpm.Version {
|
|
|
|
case TPMVersion12:
|
2020-09-24 18:35:08 +00:00
|
|
|
if getTPM12Impl == nil {
|
|
|
|
return nil, errors.New("support for Linux TPM 1.2 disabled (build with CGO to enable)")
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
2020-09-24 18:35:08 +00:00
|
|
|
return getTPM12Impl()
|
2019-03-28 20:21:16 +00:00
|
|
|
|
|
|
|
case TPMVersion20:
|
2020-05-05 23:56:57 +00:00
|
|
|
interf := TPMInterfaceDirect
|
2019-03-28 20:21:16 +00:00
|
|
|
// If the TPM has a kernel-provided resource manager, we should
|
|
|
|
// use that instead of communicating directly.
|
|
|
|
devPath := path.Join("/dev", path.Base(tpm.Path))
|
|
|
|
f, err := ioutil.ReadDir(path.Join(tpm.Path, "device", "tpmrm"))
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if len(f) > 0 {
|
|
|
|
devPath = path.Join("/dev", f[0].Name())
|
|
|
|
interf = TPMInterfaceKernelManaged
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
rwc, err := tpm2.OpenTPM(devPath)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-05 23:56:57 +00:00
|
|
|
|
|
|
|
return &TPM{tpm: &wrappedTPM20{
|
|
|
|
interf: interf,
|
|
|
|
rwc: &linuxCmdChannel{rwc},
|
|
|
|
}}, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsuported TPM version: %v", tpm.Version)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
2020-05-05 23:56:57 +00:00
|
|
|
}
|