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 cmd
|
|
|
|
|
|
|
|
import (
|
2022-06-28 11:03:28 -04:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-04-03 09:06:27 -04:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2022-03-22 11:03:15 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-06-28 11:03:28 -04:00
|
|
|
"net"
|
2023-09-08 02:08:09 -04:00
|
|
|
"sort"
|
2022-07-29 02:24:13 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2023-08-21 07:50:00 -04:00
|
|
|
tpmProto "github.com/google/go-tpm-tools/proto/tpm"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
2023-06-07 10:16:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2023-04-03 09:06:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2023-10-30 07:39:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/snp"
|
2023-08-21 07:50:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
2023-10-07 10:24:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/dialer"
|
2023-11-22 08:52:56 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/state"
|
2023-10-07 10:24:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/verify"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/verify/verifyproto"
|
2022-04-27 05:17:41 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/cobra"
|
2023-10-16 09:05:29 -04:00
|
|
|
"github.com/spf13/pflag"
|
2022-06-28 11:03:28 -04:00
|
|
|
"google.golang.org/grpc"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
// NewVerifyCmd returns a new cobra.Command for the verify command.
|
|
|
|
func NewVerifyCmd() *cobra.Command {
|
2022-03-22 11:03:15 -04:00
|
|
|
cmd := &cobra.Command{
|
2022-09-07 09:38:29 -04:00
|
|
|
Use: "verify",
|
2022-05-09 11:02:47 -04:00
|
|
|
Short: "Verify the confidential properties of a Constellation cluster",
|
2023-07-05 10:44:57 -04:00
|
|
|
Long: "Verify the confidential properties of a Constellation cluster.\n" +
|
2023-10-09 07:04:29 -04:00
|
|
|
"If arguments aren't specified, values are read from `" + constants.StateFilename + "`.",
|
2022-12-07 05:48:54 -05:00
|
|
|
Args: cobra.ExactArgs(0),
|
2022-04-27 05:17:41 -04:00
|
|
|
RunE: runVerify,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-09-11 09:58:31 -04:00
|
|
|
cmd.Flags().String("cluster-id", "", "expected cluster identifier")
|
2023-10-07 10:24:29 -04:00
|
|
|
cmd.Flags().StringP("output", "o", "", "print the attestation document in the output format {json|raw}")
|
2022-07-01 04:57:29 -04:00
|
|
|
cmd.Flags().StringP("node-endpoint", "e", "", "endpoint of the node to verify, passed as HOST[:PORT]")
|
2022-03-22 11:03:15 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
type verifyFlags struct {
|
|
|
|
rootFlags
|
|
|
|
endpoint string
|
|
|
|
ownerID string
|
|
|
|
clusterID string
|
|
|
|
output string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *verifyFlags) parse(flags *pflag.FlagSet) error {
|
|
|
|
if err := f.rootFlags.parse(flags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
f.output, err = flags.GetString("output")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting 'output' flag: %w", err)
|
|
|
|
}
|
|
|
|
f.endpoint, err = flags.GetString("node-endpoint")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting 'node-endpoint' flag: %w", err)
|
|
|
|
}
|
|
|
|
f.clusterID, err = flags.GetString("cluster-id")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting 'cluster-id' flag: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
type verifyCmd struct {
|
2023-10-16 09:05:29 -04:00
|
|
|
fileHandler file.Handler
|
|
|
|
flags verifyFlags
|
|
|
|
log debugLog
|
2022-11-21 11:02:33 -05:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func runVerify(cmd *cobra.Command, _ []string) error {
|
2022-11-21 11:02:33 -05:00
|
|
|
log, err := newCLILogger(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating logger: %w", err)
|
|
|
|
}
|
|
|
|
defer log.Sync()
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
2022-11-21 11:02:33 -05:00
|
|
|
verifyClient := &constellationVerifier{
|
|
|
|
dialer: dialer.New(nil, nil, &net.Dialer{}),
|
|
|
|
log: log,
|
|
|
|
}
|
2023-10-07 10:24:29 -04:00
|
|
|
formatterFactory := func(output string, provider cloudprovider.Provider, log debugLog) (attestationDocFormatter, error) {
|
2023-11-07 06:17:08 -05:00
|
|
|
if output == "json" && (provider != cloudprovider.Azure && provider != cloudprovider.AWS) {
|
|
|
|
return nil, errors.New("json output is only supported for Azure and AWS")
|
2023-10-07 10:24:29 -04:00
|
|
|
}
|
|
|
|
switch output {
|
|
|
|
case "json":
|
|
|
|
return &jsonAttestationDocFormatter{log}, nil
|
|
|
|
case "raw":
|
|
|
|
return &rawAttestationDocFormatter{log}, nil
|
|
|
|
case "":
|
|
|
|
return &defaultAttestationDocFormatter{log}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid output value for formatter: %s", output)
|
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
v := &verifyCmd{
|
|
|
|
fileHandler: fileHandler,
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
if err := v.flags.parse(cmd.Flags()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.log.Debugf("Using flags: %+v", v.flags)
|
2023-06-07 10:16:32 -04:00
|
|
|
fetcher := attestationconfigapi.NewFetcher()
|
2023-10-16 09:05:29 -04:00
|
|
|
return v.verify(cmd, verifyClient, formatterFactory, fetcher)
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
|
|
|
|
2023-10-07 10:24:29 -04:00
|
|
|
type formatterFactory func(output string, provider cloudprovider.Provider, log debugLog) (attestationDocFormatter, error)
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
func (c *verifyCmd) verify(cmd *cobra.Command, verifyClient verifyClient, factory formatterFactory, configFetcher attestationconfigapi.Fetcher) error {
|
|
|
|
c.log.Debugf("Loading configuration file from %q", c.flags.pathPrefixer.PrefixPrintablePath(constants.ConfigFilename))
|
|
|
|
conf, err := config.New(c.fileHandler, constants.ConfigFilename, configFetcher, c.flags.force)
|
2023-02-07 06:56:25 -05:00
|
|
|
var configValidationErr *config.ValidationError
|
|
|
|
if errors.As(err, &configValidationErr) {
|
|
|
|
cmd.PrintErrln(configValidationErr.LongMessage())
|
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
if err != nil {
|
2023-04-03 09:06:27 -04:00
|
|
|
return fmt.Errorf("loading config file: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
stateFile, err := state.ReadFromFile(c.fileHandler, constants.StateFilename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading state file: %w", err)
|
|
|
|
}
|
2023-11-03 10:47:03 -04:00
|
|
|
if err := stateFile.Validate(state.PostInit, conf.GetProvider()); err != nil {
|
|
|
|
return fmt.Errorf("validating state file: %w", err)
|
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
|
|
|
|
ownerID, clusterID, err := c.validateIDFlags(cmd, stateFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
endpoint, err := c.validateEndpointFlag(cmd, stateFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var maaURL string
|
|
|
|
if stateFile.Infrastructure.Azure != nil {
|
|
|
|
maaURL = stateFile.Infrastructure.Azure.AttestationURL
|
|
|
|
}
|
|
|
|
conf.UpdateMAAURL(maaURL)
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
c.log.Debugf("Updating expected PCRs")
|
2023-05-03 05:11:53 -04:00
|
|
|
attConfig := conf.GetAttestationConfig()
|
2023-10-16 09:05:29 -04:00
|
|
|
if err := cloudcmd.UpdateInitMeasurements(attConfig, ownerID, clusterID); err != nil {
|
2023-04-03 09:06:27 -04:00
|
|
|
return fmt.Errorf("updating expected PCRs: %w", err)
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
|
|
|
|
2023-05-03 05:11:53 -04:00
|
|
|
c.log.Debugf("Creating aTLS Validator for %s", conf.GetAttestationConfig().GetVariant())
|
|
|
|
validator, err := cloudcmd.NewValidator(cmd, attConfig, c.log)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating aTLS validator: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
nonce, err := crypto.GenerateRandomBytes(32)
|
2022-06-28 11:03:28 -04:00
|
|
|
if err != nil {
|
2023-04-03 09:06:27 -04:00
|
|
|
return fmt.Errorf("generating random nonce: %w", err)
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
c.log.Debugf("Generated random nonce: %x", nonce)
|
2022-06-28 11:03:28 -04:00
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
rawAttestationDoc, err := verifyClient.Verify(
|
2022-06-28 11:03:28 -04:00
|
|
|
cmd.Context(),
|
2023-10-16 09:05:29 -04:00
|
|
|
endpoint,
|
2022-06-28 11:03:28 -04:00
|
|
|
&verifyproto.GetAttestationRequest{
|
2023-01-17 09:28:07 -05:00
|
|
|
Nonce: nonce,
|
2022-06-28 11:03:28 -04:00
|
|
|
},
|
2023-05-03 05:11:53 -04:00
|
|
|
validator,
|
2023-04-03 09:06:27 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("verifying: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
// certificates are only available for Azure
|
2023-10-16 09:05:29 -04:00
|
|
|
formatter, err := factory(c.flags.output, conf.GetProvider(), c.log)
|
2023-10-07 10:24:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating formatter: %w", err)
|
|
|
|
}
|
2023-08-04 11:48:14 -04:00
|
|
|
attDocOutput, err := formatter.format(
|
|
|
|
cmd.Context(),
|
|
|
|
rawAttestationDoc,
|
2023-11-07 09:19:31 -05:00
|
|
|
(conf.Provider.Azure == nil && conf.Provider.AWS == nil),
|
2023-11-07 06:17:08 -05:00
|
|
|
attConfig,
|
2023-08-04 11:48:14 -04:00
|
|
|
)
|
2023-04-03 09:06:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("printing attestation document: %w", err)
|
|
|
|
}
|
|
|
|
cmd.Println(attDocOutput)
|
2023-10-07 10:24:29 -04:00
|
|
|
cmd.PrintErrln("Verification OK")
|
2023-04-03 09:06:27 -04:00
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
func (c *verifyCmd) validateIDFlags(cmd *cobra.Command, stateFile *state.State) (ownerID, clusterID string, err error) {
|
|
|
|
ownerID, clusterID = c.flags.ownerID, c.flags.clusterID
|
|
|
|
if c.flags.clusterID == "" {
|
|
|
|
cmd.PrintErrf("Using ID from %q. Specify --cluster-id to override this.\n", c.flags.pathPrefixer.PrefixPrintablePath(constants.StateFilename))
|
|
|
|
clusterID = stateFile.ClusterValues.ClusterID
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
if ownerID == "" {
|
|
|
|
// We don't want to print warnings until this is implemented again
|
|
|
|
// cmd.PrintErrf("Using ID from %q. Specify --owner-id to override this.\n", c.flags.pathPrefixer.PrefixPrintablePath(constants.StateFilename))
|
|
|
|
ownerID = stateFile.ClusterValues.OwnerID
|
2023-10-09 07:04:29 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 04:57:29 -04:00
|
|
|
// Validate
|
|
|
|
if ownerID == "" && clusterID == "" {
|
2023-10-16 09:05:29 -04:00
|
|
|
return "", "", errors.New("cluster-id not provided to verify the cluster")
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
return ownerID, clusterID, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:05:29 -04:00
|
|
|
func (c *verifyCmd) validateEndpointFlag(cmd *cobra.Command, stateFile *state.State) (string, error) {
|
|
|
|
endpoint := c.flags.endpoint
|
2022-07-29 02:24:13 -04:00
|
|
|
if endpoint == "" {
|
2023-10-16 09:05:29 -04:00
|
|
|
cmd.PrintErrf("Using endpoint from %q. Specify --node-endpoint to override this.\n", c.flags.pathPrefixer.PrefixPrintablePath(constants.StateFilename))
|
|
|
|
endpoint = stateFile.Infrastructure.ClusterEndpoint
|
2022-07-29 02:24:13 -04:00
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
endpoint, err := addPortIfMissing(endpoint, constants.VerifyServiceNodePortGRPC)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("validating endpoint argument: %w", err)
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
return endpoint, nil
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
// an attestationDocFormatter formats the attestation document.
|
|
|
|
type attestationDocFormatter interface {
|
|
|
|
// format returns the raw or formatted attestation doc depending on the rawOutput argument.
|
2023-11-07 06:17:08 -05:00
|
|
|
format(ctx context.Context, docString string, PCRsOnly bool, attestationCfg config.AttestationCfg) (string, error)
|
2023-04-03 09:06:27 -04:00
|
|
|
}
|
|
|
|
|
2023-10-07 10:24:29 -04:00
|
|
|
type jsonAttestationDocFormatter struct {
|
2023-04-03 09:06:27 -04:00
|
|
|
log debugLog
|
|
|
|
}
|
|
|
|
|
2023-10-07 10:24:29 -04:00
|
|
|
// format returns the json formatted attestation doc.
|
|
|
|
func (f *jsonAttestationDocFormatter) format(ctx context.Context, docString string, _ bool,
|
2023-11-07 06:17:08 -05:00
|
|
|
attestationCfg config.AttestationCfg,
|
2023-10-07 10:24:29 -04:00
|
|
|
) (string, error) {
|
2023-11-07 09:19:31 -05:00
|
|
|
var doc attestationDoc
|
|
|
|
if err := json.Unmarshal([]byte(docString), &doc); err != nil {
|
|
|
|
return "", fmt.Errorf("unmarshal attestation document: %w", err)
|
2023-10-07 10:24:29 -04:00
|
|
|
}
|
|
|
|
|
2023-11-07 09:19:31 -05:00
|
|
|
instanceInfo, err := extractInstanceInfo(doc)
|
2023-10-07 10:24:29 -04:00
|
|
|
if err != nil {
|
2023-11-07 09:19:31 -05:00
|
|
|
return "", fmt.Errorf("unmarshalling instance info: %w", err)
|
2023-10-07 10:24:29 -04:00
|
|
|
}
|
2023-11-07 06:17:08 -05:00
|
|
|
report, err := verify.NewReport(ctx, instanceInfo, attestationCfg, f.log)
|
2023-10-07 10:24:29 -04:00
|
|
|
if err != nil {
|
2023-11-07 09:19:31 -05:00
|
|
|
return "", fmt.Errorf("parsing SNP report: %w", err)
|
2023-10-07 10:24:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.Marshal(report)
|
|
|
|
|
|
|
|
return string(jsonBytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawAttestationDocFormatter struct {
|
|
|
|
log debugLog
|
|
|
|
}
|
|
|
|
|
|
|
|
// format returns the raw attestation doc.
|
|
|
|
func (f *rawAttestationDocFormatter) format(_ context.Context, docString string, _ bool,
|
2023-11-07 06:17:08 -05:00
|
|
|
_ config.AttestationCfg,
|
2023-10-07 10:24:29 -04:00
|
|
|
) (string, error) {
|
|
|
|
b := &strings.Builder{}
|
|
|
|
b.WriteString("Attestation Document:\n")
|
|
|
|
b.WriteString(fmt.Sprintf("%s\n", docString))
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type defaultAttestationDocFormatter struct {
|
|
|
|
log debugLog
|
|
|
|
}
|
|
|
|
|
|
|
|
// format returns the formatted attestation doc.
|
|
|
|
func (f *defaultAttestationDocFormatter) format(ctx context.Context, docString string, PCRsOnly bool,
|
2023-11-07 06:17:08 -05:00
|
|
|
attestationCfg config.AttestationCfg,
|
2023-08-04 11:48:14 -04:00
|
|
|
) (string, error) {
|
2023-04-03 09:06:27 -04:00
|
|
|
b := &strings.Builder{}
|
|
|
|
b.WriteString("Attestation Document:\n")
|
|
|
|
|
|
|
|
var doc attestationDoc
|
|
|
|
if err := json.Unmarshal([]byte(docString), &doc); err != nil {
|
|
|
|
return "", fmt.Errorf("unmarshal attestation document: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-11-07 06:17:08 -05:00
|
|
|
if err := f.parseQuotes(b, doc.Attestation.Quotes, attestationCfg.GetMeasurements()); err != nil {
|
2023-04-03 09:06:27 -04:00
|
|
|
return "", fmt.Errorf("parse quote: %w", err)
|
|
|
|
}
|
|
|
|
if PCRsOnly {
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 09:19:31 -05:00
|
|
|
instanceInfo, err := extractInstanceInfo(doc)
|
2023-04-03 09:06:27 -04:00
|
|
|
if err != nil {
|
2023-11-07 09:19:31 -05:00
|
|
|
return "", fmt.Errorf("unmarshalling instance info: %w", err)
|
2023-04-03 09:06:27 -04:00
|
|
|
}
|
|
|
|
|
2023-11-07 06:17:08 -05:00
|
|
|
report, err := verify.NewReport(ctx, instanceInfo, attestationCfg, f.log)
|
2023-10-07 10:24:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("parsing SNP report: %w", err)
|
2023-07-26 07:43:39 -04:00
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
|
2023-11-07 09:19:31 -05:00
|
|
|
return report.FormatString(b)
|
2023-04-03 09:06:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseQuotes parses the base64-encoded quotes and writes their details to the output builder.
|
2023-10-07 10:24:29 -04:00
|
|
|
func (f *defaultAttestationDocFormatter) parseQuotes(b *strings.Builder, quotes []*tpmProto.Quote, expectedPCRs measurements.M) error {
|
2023-07-25 09:02:59 -04:00
|
|
|
writeIndentfln(b, 1, "Quote:")
|
2023-09-08 02:08:09 -04:00
|
|
|
|
|
|
|
var pcrNumbers []uint32
|
|
|
|
for pcrNum := range expectedPCRs {
|
|
|
|
pcrNumbers = append(pcrNumbers, pcrNum)
|
|
|
|
}
|
|
|
|
sort.Slice(pcrNumbers, func(i, j int) bool { return pcrNumbers[i] < pcrNumbers[j] })
|
|
|
|
|
|
|
|
for _, pcrNum := range pcrNumbers {
|
|
|
|
expectedPCR := expectedPCRs[pcrNum]
|
2023-08-21 07:50:00 -04:00
|
|
|
pcrIdx, err := vtpm.GetSHA256QuoteIndex(quotes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get SHA256 quote index: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-09-08 02:08:09 -04:00
|
|
|
actualPCR, ok := quotes[pcrIdx].Pcrs.Pcrs[pcrNum]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("PCR %d not found in quote", pcrNum)
|
2023-04-03 09:06:27 -04:00
|
|
|
}
|
2023-07-25 09:02:59 -04:00
|
|
|
writeIndentfln(b, 2, "PCR %d (Strict: %t):", pcrNum, !expectedPCR.ValidationOpt)
|
|
|
|
writeIndentfln(b, 3, "Expected:\t%x", expectedPCR.Expected)
|
|
|
|
writeIndentfln(b, 3, "Actual:\t\t%x", actualPCR)
|
2023-04-03 09:06:27 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// attestationDoc is the attestation document returned by the verifier.
|
|
|
|
type attestationDoc struct {
|
|
|
|
Attestation struct {
|
2023-08-21 07:50:00 -04:00
|
|
|
AkPub string `json:"ak_pub"`
|
|
|
|
Quotes []*tpmProto.Quote `json:"quotes"`
|
|
|
|
EventLog string `json:"event_log"`
|
|
|
|
TeeAttestation interface{} `json:"TeeAttestation"`
|
2023-04-03 09:06:27 -04:00
|
|
|
} `json:"Attestation"`
|
|
|
|
InstanceInfo string `json:"InstanceInfo"`
|
|
|
|
UserData string `json:"UserData"`
|
|
|
|
}
|
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
type constellationVerifier struct {
|
2022-06-28 12:33:27 -04:00
|
|
|
dialer grpcInsecureDialer
|
2022-11-21 11:02:33 -05:00
|
|
|
log debugLog
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify retrieves an attestation statement from the Constellation and verifies it using the validator.
|
|
|
|
func (v *constellationVerifier) Verify(
|
|
|
|
ctx context.Context, endpoint string, req *verifyproto.GetAttestationRequest, validator atls.Validator,
|
2023-04-03 09:06:27 -04:00
|
|
|
) (string, error) {
|
2023-01-18 07:10:24 -05:00
|
|
|
v.log.Debugf("Dialing endpoint: %q", endpoint)
|
2022-06-28 11:03:28 -04:00
|
|
|
conn, err := v.dialer.DialInsecure(ctx, endpoint)
|
|
|
|
if err != nil {
|
2023-04-03 09:06:27 -04:00
|
|
|
return "", fmt.Errorf("dialing init server: %w", err)
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
client := verifyproto.NewAPIClient(conn)
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
v.log.Debugf("Sending attestation request")
|
2022-06-28 11:03:28 -04:00
|
|
|
resp, err := client.GetAttestation(ctx, req)
|
|
|
|
if err != nil {
|
2023-04-03 09:06:27 -04:00
|
|
|
return "", fmt.Errorf("getting attestation: %w", err)
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
v.log.Debugf("Verifying attestation")
|
2023-03-29 03:06:10 -04:00
|
|
|
signedData, err := validator.Validate(ctx, resp.Attestation, req.Nonce)
|
2022-06-28 11:03:28 -04:00
|
|
|
if err != nil {
|
2023-04-03 09:06:27 -04:00
|
|
|
return "", fmt.Errorf("validating attestation: %w", err)
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 09:28:07 -05:00
|
|
|
if !bytes.Equal(signedData, []byte(constants.ConstellationVerifyServiceUserData)) {
|
2023-04-03 09:06:27 -04:00
|
|
|
return "", errors.New("signed data in attestation does not match expected user data")
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
|
|
|
|
return string(resp.Attestation), nil
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type verifyClient interface {
|
2023-04-03 09:06:27 -04:00
|
|
|
Verify(ctx context.Context, endpoint string, req *verifyproto.GetAttestationRequest, validator atls.Validator) (string, error)
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
type grpcInsecureDialer interface {
|
2022-06-28 11:03:28 -04:00
|
|
|
DialInsecure(ctx context.Context, endpoint string) (conn *grpc.ClientConn, err error)
|
|
|
|
}
|
2023-07-25 09:02:59 -04:00
|
|
|
|
|
|
|
// writeIndentfln writes a formatted string to the builder with the given indentation level
|
|
|
|
// and a newline at the end.
|
|
|
|
func writeIndentfln(b *strings.Builder, indentLvl int, format string, args ...any) {
|
|
|
|
for i := 0; i < indentLvl; i++ {
|
|
|
|
b.WriteByte('\t')
|
|
|
|
}
|
|
|
|
b.WriteString(fmt.Sprintf(format+"\n", args...))
|
|
|
|
}
|
2023-08-04 11:48:14 -04:00
|
|
|
|
2023-11-07 09:19:31 -05:00
|
|
|
func extractInstanceInfo(doc attestationDoc) (snp.InstanceInfo, error) {
|
2023-10-07 10:24:29 -04:00
|
|
|
instanceInfoString, err := base64.StdEncoding.DecodeString(doc.InstanceInfo)
|
|
|
|
if err != nil {
|
2023-10-30 07:39:15 -04:00
|
|
|
return snp.InstanceInfo{}, fmt.Errorf("decode instance info: %w", err)
|
2023-10-07 10:24:29 -04:00
|
|
|
}
|
|
|
|
|
2023-10-30 07:39:15 -04:00
|
|
|
var instanceInfo snp.InstanceInfo
|
2023-10-07 10:24:29 -04:00
|
|
|
if err := json.Unmarshal(instanceInfoString, &instanceInfo); err != nil {
|
2023-10-30 07:39:15 -04:00
|
|
|
return snp.InstanceInfo{}, fmt.Errorf("unmarshal instance info: %w", err)
|
2023-10-07 10:24:29 -04:00
|
|
|
}
|
|
|
|
return instanceInfo, nil
|
|
|
|
}
|
2023-10-16 09:05:29 -04:00
|
|
|
|
|
|
|
func addPortIfMissing(endpoint string, defaultPort int) (string, error) {
|
|
|
|
if endpoint == "" {
|
|
|
|
return "", errors.New("endpoint is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := net.SplitHostPort(endpoint)
|
|
|
|
if err == nil {
|
|
|
|
return endpoint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), "missing port in address") {
|
|
|
|
return net.JoinHostPort(endpoint, strconv.Itoa(defaultPort)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|