Files
cocos/pkg/attestation/tdx/tdx.go
T
Sammy Kerata Oina 8eb1fac9ad NOISSUE - Refactor and update dependencies in the project (#491)
* Refactor and update dependencies in the project

- Updated go.sum to replace `github.com/absmach/magistrala` with `github.com/absmach/supermq` across various modules.
- Removed VSock configuration from environment variables and QEMU arguments.
- Updated QEMU configuration and related tests to remove references to guest CID and VSock.
- Added new HTTP transport layer for API endpoints in the manager.
- Introduced Prometheus monitoring configuration with alert rules and Alertmanager setup.
- Updated service and VM interfaces to remove unused methods and references.
- Refactored tests to align with the new structure and dependencies.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* Add MaxVMs configuration and enforce limit on VM creation

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* Add comprehensive tests for HTTP transport handlers and endpoints

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* Add test case for exceeding maximum number of VMs in TestRun

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* Improve error handling in TestHandlerWithCustomRouter to ensure response writing is checked

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* Update dependencies to latest versions

- Upgrade cel.dev/expr from v0.23.0 to v0.24.0
- Upgrade github.com/absmach/supermq from v0.16.0 to v0.17.0
- Upgrade github.com/cenkalti/backoff from v4.3.0 to v5.0.2
- Upgrade github.com/cncf/xds/go to v0.0.0-20250501225837-2ac532fd4443
- Upgrade github.com/go-chi/chi/v5 from v5.2.1 to v5.2.2
- Upgrade github.com/go-jose/go-jose/v3 from v3.0.3 to v3.0.4
- Upgrade github.com/gofrs/uuid/v5 from v5.3.0 to v5.3.2
- Upgrade github.com/prometheus/client_golang from v1.22.0 to v1.23.0
- Upgrade github.com/prometheus/client_model from v0.6.1 to v0.6.2
- Upgrade github.com/prometheus/common from v0.62.0 to v0.65.0
- Upgrade github.com/prometheus/procfs from v0.15.1 to v0.16.1
- Upgrade go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp from v0.60.0 to v0.62.0
- Upgrade go.opentelemetry.io/otel/exporters/otlp/otlptrace from v1.36.0 to v1.37.0
- Upgrade golang.org/x/crypto from v0.39.0 to v0.40.0
- Upgrade golang.org/x/sys from v0.33.0 to v0.34.0
- Upgrade golang.org/x/text from v0.26.0 to v0.27.0
- Upgrade golang.org/x/time from v0.11.0 to v0.12.0
- Upgrade google.golang.org/grpc from v1.73.0 to v1.74.2

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

---------

Signed-off-by: Sammy Oina <sammyoina@gmail.com>
2025-08-05 11:22:02 +02:00

157 lines
3.6 KiB
Go

// Copyright (c) Ultraviolet
// SPDX-License-Identifier: Apache-2.0
//go:build !embed
// +build !embed
package tdx
import (
"fmt"
"os"
"time"
"github.com/absmach/supermq/pkg/errors"
"github.com/google/go-tdx-guest/abi"
"github.com/google/go-tdx-guest/client"
"github.com/google/go-tdx-guest/proto/checkconfig"
valdatetdx "github.com/google/go-tdx-guest/validate"
verifytdx "github.com/google/go-tdx-guest/verify"
trusttdx "github.com/google/go-tdx-guest/verify/trust"
"github.com/ultravioletrs/cocos/pkg/attestation"
"google.golang.org/protobuf/encoding/protojson"
)
var errOpenTDXDevice = errors.New("failed to open TDX device")
var (
_ attestation.Provider = (*provider)(nil)
_ attestation.Verifier = (*verifier)(nil)
)
var (
timeout = time.Minute * 2
maxTryDelay = time.Second * 30
)
type provider struct{}
func NewProvider() attestation.Provider {
return provider{}
}
func (v provider) Attestation(teeNonce []byte, vTpmNonce []byte) ([]byte, error) {
return v.TeeAttestation(teeNonce)
}
func (v provider) TeeAttestation(teeNonce []byte) ([]byte, error) {
if teeNonce == nil {
return nil, errors.New("tee nonce is required for TDX attestation")
}
if len(teeNonce) != 64 {
return nil, fmt.Errorf("invalid tee nonce length: expected 64 bytes, got %d bytes", len(teeNonce))
}
quoteprovider, err := client.GetQuoteProvider()
if err != nil {
return nil, errors.Wrap(err, errOpenTDXDevice)
}
return quoteprovider.GetRawQuote([64]byte(teeNonce))
}
func (v provider) VTpmAttestation(vTpmNonce []byte) ([]byte, error) {
return nil, errors.New("vTPM attestation fetch is not supported")
}
func (v provider) AzureAttestationToken(tokenNonce []byte) ([]byte, error) {
return nil, errors.New("Azure attestation token is not supported")
}
type verifier struct {
Policy *checkconfig.Config
}
func NewVerifier() attestation.Verifier {
Policy := &checkconfig.Config{
RootOfTrust: &checkconfig.RootOfTrust{},
Policy: &checkconfig.Policy{HeaderPolicy: &checkconfig.HeaderPolicy{}, TdQuoteBodyPolicy: &checkconfig.TDQuoteBodyPolicy{}},
}
return verifier{
Policy: Policy,
}
}
func NewVerifierWithPolicy(policy *checkconfig.Config) attestation.Verifier {
if policy == nil {
return NewVerifier()
}
return verifier{
Policy: policy,
}
}
func (v verifier) VerifTeeAttestation(report []byte, teeNonce []byte) error {
if v.Policy == nil {
return fmt.Errorf("tdx policy is not provided")
}
quote, err := abi.QuoteToProto(report)
if err != nil {
return err
}
sopts, err := verifytdx.RootOfTrustToOptions(v.Policy.RootOfTrust)
if err != nil {
return err
}
sopts.Getter = &trusttdx.RetryHTTPSGetter{
Timeout: timeout,
MaxRetryDelay: maxTryDelay,
Getter: &trusttdx.SimpleHTTPSGetter{},
}
if err := verifytdx.TdxQuote(quote, sopts); err != nil {
return err
}
opts, err := valdatetdx.PolicyToOptions(v.Policy.Policy)
if err != nil {
return err
}
if err := valdatetdx.TdxQuote(quote, opts); err != nil {
return err
}
return nil
}
func (v verifier) VerifVTpmAttestation(report []byte, vTpmNonce []byte) error {
return errors.New("VTPM attestation verification is not supported")
}
func (v verifier) VerifyAttestation(report []byte, teeNonce []byte, vTpmNonce []byte) error {
return v.VerifTeeAttestation(report, teeNonce)
}
func (v verifier) JSONToPolicy(path string) error {
return ReadTDXAttestationPolicy(path, v.Policy)
}
func ReadTDXAttestationPolicy(policyPath string, policy *checkconfig.Config) error {
policyByte, err := os.ReadFile(policyPath)
if err != nil {
return err
}
if err := protojson.Unmarshal(policyByte, policy); err != nil {
return err
}
return nil
}