2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package gcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
compute "cloud.google.com/go/compute/apiv1"
|
2022-12-08 05:26:51 -05:00
|
|
|
"cloud.google.com/go/compute/apiv1/computepb"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
2023-03-29 03:30:13 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/variant"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/google/go-tpm-tools/proto/attest"
|
|
|
|
"github.com/googleapis/gax-go/v2"
|
|
|
|
"google.golang.org/api/option"
|
|
|
|
)
|
|
|
|
|
2023-03-06 03:17:08 -05:00
|
|
|
const minimumGceVersion = 1
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// Validator for GCP confidential VM attestation.
|
|
|
|
type Validator struct {
|
2023-03-29 03:30:13 -04:00
|
|
|
variant.GCPSEVES
|
2022-03-22 11:03:15 -04:00
|
|
|
*vtpm.Validator
|
2023-03-21 07:46:49 -04:00
|
|
|
|
|
|
|
restClient func(context.Context, ...option.ClientOption) (gcpRestClient, error)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewValidator initializes a new GCP validator with the provided PCR values.
|
2022-11-24 04:57:58 -05:00
|
|
|
func NewValidator(pcrs measurements.M, log vtpm.AttestationLogger) *Validator {
|
2023-03-21 07:46:49 -04:00
|
|
|
v := &Validator{
|
|
|
|
restClient: newInstanceClient,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-03-21 07:46:49 -04:00
|
|
|
v.Validator = vtpm.NewValidator(
|
|
|
|
pcrs,
|
|
|
|
v.trustedKeyFromGCEAPI,
|
|
|
|
validateCVM,
|
|
|
|
log,
|
|
|
|
)
|
|
|
|
|
|
|
|
return v
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type gcpRestClient interface {
|
|
|
|
GetShieldedInstanceIdentity(ctx context.Context, req *computepb.GetShieldedInstanceIdentityInstanceRequest, opts ...gax.CallOption) (*computepb.ShieldedInstanceIdentity, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type instanceClient struct {
|
|
|
|
*compute.InstancesClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInstanceClient(ctx context.Context, opts ...option.ClientOption) (gcpRestClient, error) {
|
|
|
|
c, err := compute.NewInstancesRESTClient(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &instanceClient{c}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// trustedKeyFromGCEAPI queries the GCE API for a shieldedVM's public signing key.
|
|
|
|
// This key can be used to verify attestation statements issued by the VM.
|
2023-03-21 07:46:49 -04:00
|
|
|
func (v *Validator) trustedKeyFromGCEAPI(ctx context.Context, attDoc vtpm.AttestationDocument, _ []byte) (crypto.PublicKey, error) {
|
|
|
|
client, err := v.restClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating GCE client: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-03-21 07:46:49 -04:00
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
var instanceInfo attest.GCEInstanceInfo
|
|
|
|
if err := json.Unmarshal(attDoc.InstanceInfo, &instanceInfo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
instance, err := client.GetShieldedInstanceIdentity(ctx, &computepb.GetShieldedInstanceIdentityInstanceRequest{
|
|
|
|
Instance: instanceInfo.GetInstanceName(),
|
|
|
|
Project: instanceInfo.GetProjectId(),
|
|
|
|
Zone: instanceInfo.GetZone(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("retrieving VM identity: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if instance.SigningKey == nil || instance.SigningKey.EkPub == nil {
|
|
|
|
return nil, fmt.Errorf("received no signing key from GCP API")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the signing key return by GetShieldedInstanceIdentity
|
|
|
|
block, _ := pem.Decode([]byte(*instance.SigningKey.EkPub))
|
|
|
|
if block == nil || block.Type != "PUBLIC KEY" {
|
|
|
|
return nil, fmt.Errorf("failed to decode PEM block containing public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return x509.ParsePKIXPublicKey(block.Bytes)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-06 03:17:08 -05:00
|
|
|
// validateCVM checks that the machine state represents a GCE AMD-SEV VM.
|
|
|
|
func validateCVM(_ vtpm.AttestationDocument, state *attest.MachineState) error {
|
|
|
|
gceVersion := state.Platform.GetGceVersion()
|
|
|
|
if gceVersion < minimumGceVersion {
|
|
|
|
return fmt.Errorf("outdated GCE version: %v (require >= %v)", gceVersion, minimumGceVersion)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-03-06 03:17:08 -05:00
|
|
|
|
|
|
|
tech := state.Platform.Technology
|
|
|
|
wantTech := attest.GCEConfidentialTechnology_AMD_SEV
|
|
|
|
if tech != wantTech {
|
|
|
|
return fmt.Errorf("unexpected confidential technology: %v (expected: %v)", tech, wantTech)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-03-06 03:17:08 -05:00
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil
|
|
|
|
}
|