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"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
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"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/oid"
|
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-02 04:48:16 -05:00
|
|
|
oid.GCPSEVES
|
2022-03-22 11:03:15 -04:00
|
|
|
*vtpm.Validator
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-03-22 11:03:15 -04:00
|
|
|
return &Validator{
|
|
|
|
Validator: vtpm.NewValidator(
|
|
|
|
pcrs,
|
|
|
|
trustedKeyFromGCEAPI(newInstanceClient),
|
2023-03-06 03:17:08 -05:00
|
|
|
validateCVM,
|
2022-08-29 10:41:09 -04:00
|
|
|
log,
|
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.
|
|
|
|
func trustedKeyFromGCEAPI(getClient func(ctx context.Context, opts ...option.ClientOption) (gcpRestClient, error)) func(akPub []byte, instanceInfoRaw []byte) (crypto.PublicKey, error) {
|
|
|
|
return func(akPub, instanceInfoRaw []byte) (crypto.PublicKey, error) {
|
|
|
|
var instanceInfo attest.GCEInstanceInfo
|
|
|
|
if err := json.Unmarshal(instanceInfoRaw, &instanceInfo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
client, err := getClient(ctx)
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return nil, fmt.Errorf("creating GCE client: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
instance, err := client.GetShieldedInstanceIdentity(ctx, &computepb.GetShieldedInstanceIdentityInstanceRequest{
|
|
|
|
Instance: instanceInfo.GetInstanceName(),
|
|
|
|
Project: instanceInfo.GetProjectId(),
|
|
|
|
Zone: instanceInfo.GetZone(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-06-09 10:04:30 -04:00
|
|
|
return nil, fmt.Errorf("retrieving VM identity: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|