2025-07-18 14:31:52 +00:00
|
|
|
//go:build linux
|
|
|
|
|
|
|
|
|
|
package securehw
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2025-07-30 14:46:36 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/constant"
|
2025-07-18 14:31:52 +00:00
|
|
|
"github.com/google/go-tpm/tpm2/transport/linuxtpm"
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const tpm20DevicePath = "/dev/tpmrm0"
|
|
|
|
|
|
2025-07-29 18:21:09 +00:00
|
|
|
// Creates a new SecureHW instance using TPM 2.0 for Linux.
|
2025-07-18 14:31:52 +00:00
|
|
|
// It attempts to open the TPM device using the provided configuration.
|
2025-07-23 17:30:44 +00:00
|
|
|
func newSecureHW(metadataDir string, logger zerolog.Logger) (SecureHW, error) {
|
2025-07-18 14:31:52 +00:00
|
|
|
if metadataDir == "" {
|
|
|
|
|
return nil, errors.New("required metadata directory not set")
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-30 14:46:36 +00:00
|
|
|
logger.Info().Msg("opening TPM 2.0 resource manager")
|
2025-07-18 14:31:52 +00:00
|
|
|
|
|
|
|
|
// Open the TPM 2.0 resource manager, which
|
|
|
|
|
// - Provides managed access to TPM resources, allowing multiple applications to share the TPM safely.
|
|
|
|
|
// - Used by the TPM2 Access Broker and Resource Manager (tpm2-abrmd or the kernel resource manager).
|
|
|
|
|
device, err := linuxtpm.Open(tpm20DevicePath)
|
|
|
|
|
if err != nil {
|
2025-07-23 17:30:44 +00:00
|
|
|
return nil, ErrSecureHWUnavailable{
|
2025-07-18 14:31:52 +00:00
|
|
|
Message: fmt.Sprintf("failed to open TPM 2.0 device %q: %s", tpm20DevicePath, err.Error()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-30 14:46:36 +00:00
|
|
|
logger.Info().Str("device_path", tpm20DevicePath).Msg("successfully opened TPM 2.0 resource manager")
|
2025-07-18 14:31:52 +00:00
|
|
|
|
2025-07-23 17:30:44 +00:00
|
|
|
return &tpm2SecureHW{
|
2025-07-29 18:21:09 +00:00
|
|
|
device: device,
|
|
|
|
|
logger: logger.With().Str("component", "securehw-tpm").Logger(),
|
2025-07-30 14:46:36 +00:00
|
|
|
keyFilePath: filepath.Join(metadataDir, constant.FleetHTTPSignatureTPMKeyFileName),
|
2025-07-18 14:31:52 +00:00
|
|
|
}, nil
|
|
|
|
|
}
|