- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
194 lines
5.6 KiB
Go
194 lines
5.6 KiB
Go
package attest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// PCR assignment used by the connector's boot measurement chain. These mirror
|
|
// the TCG convention for PCR 0-7 (platform firmware and configuration) as
|
|
// closely as a software root of trust can.
|
|
const (
|
|
PCRFirmware = 0 // connector executable image digest
|
|
PCRPlatform = 1 // OS / arch / kernel identity of the host
|
|
PCRTPMProvider = 2 // TPM provider type and device path
|
|
PCRTrustAnchor = 7 // root CA fingerprint + device identity binding
|
|
)
|
|
|
|
// DefaultQuotePCRMask is the PCR set devices are asked to quote (PCR 0-7).
|
|
const DefaultQuotePCRMask uint32 = 0x000000FF
|
|
|
|
// DefaultRequiredPCRMask is the subset a quote must cover to be accepted:
|
|
// PCR 0, 1, 2 and 7.
|
|
const DefaultRequiredPCRMask uint32 = (1 << PCRFirmware) | (1 << PCRPlatform) |
|
|
(1 << PCRTPMProvider) | (1 << PCRTrustAnchor)
|
|
|
|
// GoldenEntry is the recorded reference measurement for one device.
|
|
type GoldenEntry struct {
|
|
PCRDigest string `json:"pcr_digest"`
|
|
PCRMask uint32 `json:"pcr_mask"`
|
|
FirmwareHash string `json:"firmware_hash,omitempty"`
|
|
PCRs []PCR `json:"pcrs,omitempty"`
|
|
RecordedAt time.Time `json:"recorded_at"`
|
|
TOFU bool `json:"tofu"`
|
|
Note string `json:"note,omitempty"`
|
|
}
|
|
|
|
// Policy is the enrollment measurement policy.
|
|
type Policy struct {
|
|
// QuotePCRMask is advertised to devices in the enrollment challenge.
|
|
QuotePCRMask uint32 `json:"quote_pcr_mask"`
|
|
|
|
// RequiredPCRMask is the subset that must be present in the quote.
|
|
RequiredPCRMask uint32 `json:"required_pcr_mask"`
|
|
|
|
// AllowTOFU records an unknown device's first measurement as its golden
|
|
// value instead of rejecting it. Appropriate for a lab / software root of
|
|
// trust; turn it off once golden values are provisioned out-of-band.
|
|
AllowTOFU bool `json:"allow_tofu"`
|
|
|
|
// AllowedFirmwareHashes, when non-empty, restricts which firmware digests
|
|
// may enroll.
|
|
AllowedFirmwareHashes []string `json:"allowed_firmware_hashes,omitempty"`
|
|
|
|
// Golden maps device ID to its reference measurement.
|
|
Golden map[string]*GoldenEntry `json:"golden"`
|
|
}
|
|
|
|
// DefaultPolicy returns a bring-up policy: quote PCR 0-7, require 0/1/2/7,
|
|
// trust on first use, no firmware allow list.
|
|
func DefaultPolicy() *Policy {
|
|
return &Policy{
|
|
QuotePCRMask: DefaultQuotePCRMask,
|
|
RequiredPCRMask: DefaultRequiredPCRMask,
|
|
AllowTOFU: true,
|
|
Golden: make(map[string]*GoldenEntry),
|
|
}
|
|
}
|
|
|
|
func (p *Policy) normalize() {
|
|
if p.QuotePCRMask == 0 {
|
|
p.QuotePCRMask = DefaultQuotePCRMask
|
|
}
|
|
if p.RequiredPCRMask == 0 {
|
|
p.RequiredPCRMask = DefaultRequiredPCRMask
|
|
}
|
|
// A device cannot satisfy a requirement it was never asked to quote.
|
|
p.QuotePCRMask |= p.RequiredPCRMask
|
|
if p.Golden == nil {
|
|
p.Golden = make(map[string]*GoldenEntry)
|
|
}
|
|
}
|
|
|
|
func (p *Policy) firmwareAllowed(h string) bool {
|
|
if len(p.AllowedFirmwareHashes) == 0 {
|
|
return true
|
|
}
|
|
for _, a := range p.AllowedFirmwareHashes {
|
|
if strings.EqualFold(strings.TrimSpace(a), strings.TrimSpace(h)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// clone returns a deep-enough copy for safe serialisation outside the lock.
|
|
func (p *Policy) clone() *Policy {
|
|
c := &Policy{
|
|
QuotePCRMask: p.QuotePCRMask,
|
|
RequiredPCRMask: p.RequiredPCRMask,
|
|
AllowTOFU: p.AllowTOFU,
|
|
AllowedFirmwareHashes: append([]string(nil), p.AllowedFirmwareHashes...),
|
|
Golden: make(map[string]*GoldenEntry, len(p.Golden)),
|
|
}
|
|
for k, v := range p.Golden {
|
|
e := *v
|
|
e.PCRs = append([]PCR(nil), v.PCRs...)
|
|
c.Golden[k] = &e
|
|
}
|
|
return c
|
|
}
|
|
|
|
// Store persists golden measurements across server restarts.
|
|
type Store interface {
|
|
Load() (*Policy, error)
|
|
Save(*Policy) error
|
|
}
|
|
|
|
// FileStore stores the policy as a JSON document on disk.
|
|
type FileStore struct {
|
|
mu sync.Mutex
|
|
Path string
|
|
}
|
|
|
|
// NewFileStore creates a policy store rooted at path.
|
|
func NewFileStore(path string) *FileStore { return &FileStore{Path: path} }
|
|
|
|
// Load reads the policy. A missing file yields DefaultPolicy so a fresh server
|
|
// starts in a usable state rather than refusing every enrollment.
|
|
func (f *FileStore) Load() (*Policy, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
|
|
data, err := os.ReadFile(f.Path)
|
|
if os.IsNotExist(err) {
|
|
return DefaultPolicy(), nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read attestation policy: %w", err)
|
|
}
|
|
|
|
var p Policy
|
|
if err := json.Unmarshal(data, &p); err != nil {
|
|
return nil, fmt.Errorf("parse attestation policy %s: %w", f.Path, err)
|
|
}
|
|
p.normalize()
|
|
return &p, nil
|
|
}
|
|
|
|
// Save writes the policy atomically (temp file + rename) so a crash mid-write
|
|
// cannot leave a truncated golden-value database behind.
|
|
func (f *FileStore) Save(p *Policy) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
|
|
if err := os.MkdirAll(filepath.Dir(f.Path), 0o755); err != nil {
|
|
return fmt.Errorf("create policy dir: %w", err)
|
|
}
|
|
|
|
data, err := json.MarshalIndent(p, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("encode attestation policy: %w", err)
|
|
}
|
|
data = append(data, '\n')
|
|
|
|
tmp := f.Path + ".tmp"
|
|
if err := os.WriteFile(tmp, data, 0o600); err != nil {
|
|
return fmt.Errorf("write attestation policy: %w", err)
|
|
}
|
|
if err := os.Rename(tmp, f.Path); err != nil {
|
|
os.Remove(tmp)
|
|
return fmt.Errorf("commit attestation policy: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// LoadPolicy loads a policy from path, returning the default when path is
|
|
// empty. It also returns the store so callers can persist TOFU records.
|
|
func LoadPolicy(path string) (*Policy, Store, error) {
|
|
if path == "" {
|
|
return DefaultPolicy(), nil, nil
|
|
}
|
|
store := NewFileStore(path)
|
|
p, err := store.Load()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return p, store, nil
|
|
}
|