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
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
2022-03-22 11:03:15 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-08-04 11:48:14 -04:00
|
|
|
"io"
|
2022-06-28 11:03:28 -04:00
|
|
|
"net"
|
2023-08-04 11:48:14 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
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"
|
2022-10-11 06:24:33 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
2023-08-08 09:42:06 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cmd/pathprefix"
|
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-08-21 07:50:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
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"
|
|
|
|
"github.com/edgelesssys/constellation/v2/verify/verifyproto"
|
2023-08-04 11:48:14 -04:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2023-07-26 07:43:39 -04:00
|
|
|
"github.com/google/go-sev-guest/abi"
|
2023-07-25 11:36:06 -04:00
|
|
|
"github.com/google/go-sev-guest/kds"
|
2022-04-27 05:17:41 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/cobra"
|
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-08-04 07:53:51 -04:00
|
|
|
"If arguments aren't specified, values are read from `" + constants.ClusterIDsFilename + "`.",
|
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-04-03 09:06:27 -04:00
|
|
|
cmd.Flags().Bool("raw", false, "print raw attestation document")
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
type verifyCmd struct {
|
|
|
|
log debugLog
|
|
|
|
}
|
|
|
|
|
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-04-03 09:06:27 -04:00
|
|
|
formatter := &attestationDocFormatterImpl{
|
|
|
|
log: log,
|
|
|
|
}
|
2022-11-21 11:02:33 -05:00
|
|
|
|
|
|
|
v := &verifyCmd{log: log}
|
2023-06-07 10:16:32 -04:00
|
|
|
fetcher := attestationconfigapi.NewFetcher()
|
2023-06-01 07:55:46 -04:00
|
|
|
return v.verify(cmd, fileHandler, verifyClient, formatter, fetcher)
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
func (c *verifyCmd) verify(cmd *cobra.Command, fileHandler file.Handler, verifyClient verifyClient, formatter attestationDocFormatter, configFetcher attestationconfigapi.Fetcher) error {
|
2023-04-03 09:06:27 -04:00
|
|
|
flags, err := c.parseVerifyFlags(cmd, fileHandler)
|
2022-04-27 05:17:41 -04:00
|
|
|
if err != nil {
|
2023-04-03 09:06:27 -04:00
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
c.log.Debugf("Using flags: %+v", flags)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2023-08-16 09:38:40 -04:00
|
|
|
c.log.Debugf("Loading configuration file from %q", flags.pf.PrefixPrintablePath(constants.ConfigFilename))
|
2023-08-04 07:53:51 -04:00
|
|
|
conf, err := config.New(fileHandler, constants.ConfigFilename, configFetcher, 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-05-03 05:11:53 -04:00
|
|
|
conf.UpdateMAAURL(flags.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-03-22 08:40:02 -04:00
|
|
|
if err := cloudcmd.UpdateInitMeasurements(attConfig, flags.ownerID, flags.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(),
|
|
|
|
flags.endpoint,
|
|
|
|
&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-08-04 11:48:14 -04:00
|
|
|
attDocOutput, err := formatter.format(
|
|
|
|
cmd.Context(),
|
|
|
|
rawAttestationDoc,
|
|
|
|
conf.Provider.Azure == nil,
|
|
|
|
flags.rawOutput,
|
|
|
|
attConfig.GetMeasurements(),
|
|
|
|
flags.maaURL,
|
|
|
|
)
|
2023-04-03 09:06:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("printing attestation document: %w", err)
|
|
|
|
}
|
|
|
|
cmd.Println(attDocOutput)
|
|
|
|
cmd.Println("Verification OK")
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
func (c *verifyCmd) parseVerifyFlags(cmd *cobra.Command, fileHandler file.Handler) (verifyFlags, error) {
|
2023-08-08 09:42:06 -04:00
|
|
|
workDir, err := cmd.Flags().GetString("workspace")
|
2022-07-01 04:57:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return verifyFlags{}, fmt.Errorf("parsing config path argument: %w", err)
|
|
|
|
}
|
2023-08-08 09:42:06 -04:00
|
|
|
c.log.Debugf("Flag 'workspace' set to %q", workDir)
|
|
|
|
pf := pathprefix.New(workDir)
|
2022-11-21 11:02:33 -05:00
|
|
|
|
2022-09-11 09:58:31 -04:00
|
|
|
ownerID := ""
|
2022-07-26 04:58:39 -04:00
|
|
|
clusterID, err := cmd.Flags().GetString("cluster-id")
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-07-26 04:58:39 -04:00
|
|
|
return verifyFlags{}, fmt.Errorf("parsing cluster-id argument: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
c.log.Debugf("Flag 'cluster-id' set to %q", clusterID)
|
2022-11-21 11:02:33 -05:00
|
|
|
|
2022-05-04 02:50:50 -04:00
|
|
|
endpoint, err := cmd.Flags().GetString("node-endpoint")
|
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return verifyFlags{}, fmt.Errorf("parsing node-endpoint argument: %w", err)
|
2022-05-04 02:50:50 -04:00
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
c.log.Debugf("Flag 'node-endpoint' set to %q", endpoint)
|
2022-05-04 02:50:50 -04:00
|
|
|
|
2023-01-31 05:45:31 -05:00
|
|
|
force, err := cmd.Flags().GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return verifyFlags{}, fmt.Errorf("parsing force argument: %w", err)
|
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
c.log.Debugf("Flag 'force' set to %t", force)
|
|
|
|
|
|
|
|
raw, err := cmd.Flags().GetBool("raw")
|
|
|
|
if err != nil {
|
|
|
|
return verifyFlags{}, fmt.Errorf("parsing raw argument: %w", err)
|
|
|
|
}
|
|
|
|
c.log.Debugf("Flag 'raw' set to %t", force)
|
2023-01-31 05:45:31 -05:00
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
var idFile clusterid.File
|
2023-08-04 07:53:51 -04:00
|
|
|
if err := fileHandler.ReadJSON(constants.ClusterIDsFilename, &idFile); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
|
2023-03-21 07:46:49 -04:00
|
|
|
return verifyFlags{}, fmt.Errorf("reading cluster ID file: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-11 09:51:38 -04:00
|
|
|
// Get empty values from ID file
|
2022-07-01 04:57:29 -04:00
|
|
|
emptyEndpoint := endpoint == ""
|
|
|
|
emptyIDs := ownerID == "" && clusterID == ""
|
2022-09-11 09:51:38 -04:00
|
|
|
if emptyEndpoint || emptyIDs {
|
2023-08-16 09:38:40 -04:00
|
|
|
c.log.Debugf("Trying to supplement empty flag values from %q", pf.PrefixPrintablePath(constants.ClusterIDsFilename))
|
2023-03-21 07:46:49 -04:00
|
|
|
if emptyEndpoint {
|
2023-08-16 09:38:40 -04:00
|
|
|
cmd.Printf("Using endpoint from %q. Specify --node-endpoint to override this.\n", pf.PrefixPrintablePath(constants.ClusterIDsFilename))
|
2023-03-21 07:46:49 -04:00
|
|
|
endpoint = idFile.IP
|
|
|
|
}
|
|
|
|
if emptyIDs {
|
2023-08-16 09:38:40 -04:00
|
|
|
cmd.Printf("Using ID from %q. Specify --cluster-id to override this.\n", pf.PrefixPrintablePath(constants.ClusterIDsFilename))
|
2023-03-21 07:46:49 -04:00
|
|
|
ownerID = idFile.OwnerID
|
|
|
|
clusterID = idFile.ClusterID
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate
|
|
|
|
if ownerID == "" && clusterID == "" {
|
2022-09-11 09:58:31 -04:00
|
|
|
return verifyFlags{}, errors.New("cluster-id not provided to verify the cluster")
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
2022-07-29 02:24:13 -04:00
|
|
|
endpoint, err = addPortIfMissing(endpoint, constants.VerifyServiceNodePortGRPC)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-07-01 04:57:29 -04:00
|
|
|
return verifyFlags{}, fmt.Errorf("validating endpoint argument: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
return verifyFlags{
|
2023-08-04 07:53:51 -04:00
|
|
|
endpoint: endpoint,
|
2023-08-08 09:42:06 -04:00
|
|
|
pf: pf,
|
2023-08-04 07:53:51 -04:00
|
|
|
ownerID: ownerID,
|
|
|
|
clusterID: clusterID,
|
|
|
|
maaURL: idFile.AttestationURL,
|
|
|
|
rawOutput: raw,
|
|
|
|
force: force,
|
2022-04-27 05:17:41 -04:00
|
|
|
}, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
type verifyFlags struct {
|
2023-08-04 07:53:51 -04:00
|
|
|
endpoint string
|
|
|
|
ownerID string
|
|
|
|
clusterID string
|
|
|
|
maaURL string
|
|
|
|
rawOutput bool
|
|
|
|
force bool
|
2023-08-08 09:42:06 -04:00
|
|
|
pf pathprefix.PathPrefixer
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-29 02:24:13 -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
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
2022-07-29 02:24:13 -04:00
|
|
|
|
|
|
|
return "", err
|
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-08-04 11:48:14 -04:00
|
|
|
format(ctx context.Context, docString string, PCRsOnly bool, rawOutput bool, expectedPCRs measurements.M,
|
|
|
|
attestationServiceURL string) (string, error)
|
2023-04-03 09:06:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type attestationDocFormatterImpl struct {
|
|
|
|
log debugLog
|
|
|
|
}
|
|
|
|
|
|
|
|
// format returns the raw or formatted attestation doc depending on the rawOutput argument.
|
2023-08-04 11:48:14 -04:00
|
|
|
func (f *attestationDocFormatterImpl) format(ctx context.Context, docString string, PCRsOnly bool,
|
|
|
|
rawOutput bool, expectedPCRs measurements.M, attestationServiceURL string,
|
|
|
|
) (string, error) {
|
2023-04-03 09:06:27 -04:00
|
|
|
b := &strings.Builder{}
|
|
|
|
b.WriteString("Attestation Document:\n")
|
|
|
|
if rawOutput {
|
|
|
|
b.WriteString(fmt.Sprintf("%s\n", docString))
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var doc attestationDoc
|
|
|
|
if err := json.Unmarshal([]byte(docString), &doc); err != nil {
|
|
|
|
return "", fmt.Errorf("unmarshal attestation document: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.parseQuotes(b, doc.Attestation.Quotes, expectedPCRs); err != nil {
|
|
|
|
return "", fmt.Errorf("parse quote: %w", err)
|
|
|
|
}
|
|
|
|
if PCRsOnly {
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceInfoString, err := base64.StdEncoding.DecodeString(doc.InstanceInfo)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("decode instance info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var instanceInfo azureInstanceInfo
|
|
|
|
if err := json.Unmarshal(instanceInfoString, &instanceInfo); err != nil {
|
|
|
|
return "", fmt.Errorf("unmarshal instance info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.parseCerts(b, "VCEK certificate", instanceInfo.Vcek); err != nil {
|
|
|
|
return "", fmt.Errorf("print VCEK certificate: %w", err)
|
|
|
|
}
|
|
|
|
if err := f.parseCerts(b, "Certificate chain", instanceInfo.CertChain); err != nil {
|
|
|
|
return "", fmt.Errorf("print certificate chain: %w", err)
|
|
|
|
}
|
2023-07-26 07:43:39 -04:00
|
|
|
if err := f.parseSNPReport(b, instanceInfo.AttestationReport); err != nil {
|
|
|
|
return "", fmt.Errorf("print SNP report: %w", err)
|
|
|
|
}
|
2023-08-04 11:48:14 -04:00
|
|
|
if err := parseMAAToken(ctx, b, instanceInfo.MAAToken, attestationServiceURL); err != nil {
|
|
|
|
return "", fmt.Errorf("print MAA token: %w", err)
|
|
|
|
}
|
2023-04-03 09:06:27 -04:00
|
|
|
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
2023-07-26 07:43:39 -04:00
|
|
|
// parseCerts parses the PEM certificates and writes their details to the output builder.
|
|
|
|
func (f *attestationDocFormatterImpl) parseCerts(b *strings.Builder, certTypeName string, cert []byte) error {
|
2023-09-04 02:30:18 -04:00
|
|
|
newlinesTrimmed := strings.TrimSpace(string(cert))
|
|
|
|
formattedCert := strings.ReplaceAll(newlinesTrimmed, "\n", "\n\t\t") + "\n"
|
2023-04-03 09:06:27 -04:00
|
|
|
b.WriteString(fmt.Sprintf("\tRaw %s:\n\t\t%s", certTypeName, formattedCert))
|
|
|
|
|
|
|
|
f.log.Debugf("Decoding PEM certificate: %s", certTypeName)
|
|
|
|
i := 1
|
2023-07-26 07:43:39 -04:00
|
|
|
var rest []byte
|
|
|
|
var block *pem.Block
|
2023-09-04 02:30:18 -04:00
|
|
|
for block, rest = pem.Decode([]byte(newlinesTrimmed)); block != nil; block, rest = pem.Decode(rest) {
|
2023-04-03 09:06:27 -04:00
|
|
|
f.log.Debugf("Parsing PEM block: %d", i)
|
|
|
|
if block.Type != "CERTIFICATE" {
|
|
|
|
return fmt.Errorf("parse %s: expected PEM block type 'CERTIFICATE', got '%s'", certTypeName, block.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parse %s: %w", certTypeName, err)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:02:59 -04:00
|
|
|
writeIndentfln(b, 1, "%s (%d):", certTypeName, i)
|
|
|
|
writeIndentfln(b, 2, "Serial Number: %s", cert.SerialNumber)
|
|
|
|
writeIndentfln(b, 2, "Subject: %s", cert.Subject)
|
|
|
|
writeIndentfln(b, 2, "Issuer: %s", cert.Issuer)
|
|
|
|
writeIndentfln(b, 2, "Not Before: %s", cert.NotBefore)
|
|
|
|
writeIndentfln(b, 2, "Not After: %s", cert.NotAfter)
|
|
|
|
writeIndentfln(b, 2, "Signature Algorithm: %s", cert.SignatureAlgorithm)
|
|
|
|
writeIndentfln(b, 2, "Public Key Algorithm: %s", cert.PublicKeyAlgorithm)
|
2023-04-03 09:06:27 -04:00
|
|
|
|
2023-07-25 11:36:06 -04:00
|
|
|
if certTypeName == "VCEK certificate" {
|
|
|
|
// Extensions documented in Table 8 and Table 9 of
|
|
|
|
// https://www.amd.com/system/files/TechDocs/57230.pdf
|
|
|
|
vcekExts, err := kds.VcekCertificateExtensions(cert)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing VCEK certificate extensions: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:02:59 -04:00
|
|
|
writeIndentfln(b, 2, "Struct version: %d", vcekExts.StructVersion)
|
|
|
|
writeIndentfln(b, 2, "Product name: %s", vcekExts.ProductName)
|
2023-07-25 11:36:06 -04:00
|
|
|
tcb := kds.DecomposeTCBVersion(vcekExts.TCBVersion)
|
2023-07-25 09:02:59 -04:00
|
|
|
writeIndentfln(b, 2, "Secure Processor bootloader SVN: %d", tcb.BlSpl)
|
|
|
|
writeIndentfln(b, 2, "Secure Processor operating system SVN: %d", tcb.TeeSpl)
|
|
|
|
writeIndentfln(b, 2, "SVN 4 (reserved): %d", tcb.Spl4)
|
|
|
|
writeIndentfln(b, 2, "SVN 5 (reserved): %d", tcb.Spl5)
|
|
|
|
writeIndentfln(b, 2, "SVN 6 (reserved): %d", tcb.Spl6)
|
|
|
|
writeIndentfln(b, 2, "SVN 7 (reserved): %d", tcb.Spl7)
|
|
|
|
writeIndentfln(b, 2, "SEV-SNP firmware SVN: %d", tcb.SnpSpl)
|
|
|
|
writeIndentfln(b, 2, "Microcode SVN: %d", tcb.UcodeSpl)
|
2023-07-26 07:43:39 -04:00
|
|
|
writeIndentfln(b, 2, "Hardware ID: %x", vcekExts.HWID)
|
2023-07-25 11:36:06 -04:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2023-09-04 02:30:18 -04:00
|
|
|
if i == 1 {
|
|
|
|
return fmt.Errorf("parse %s: no PEM blocks found", certTypeName)
|
|
|
|
}
|
2023-07-26 07:43:39 -04:00
|
|
|
if len(rest) != 0 {
|
|
|
|
return fmt.Errorf("parse %s: remaining PEM block is not a valid certificate: %s", certTypeName, rest)
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseQuotes parses the base64-encoded quotes and writes their details to the output builder.
|
2023-08-21 07:50:00 -04:00
|
|
|
func (f *attestationDocFormatterImpl) 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
|
|
|
|
}
|
|
|
|
|
2023-07-26 07:43:39 -04:00
|
|
|
func (f *attestationDocFormatterImpl) parseSNPReport(b *strings.Builder, reportBytes []byte) error {
|
|
|
|
report, err := abi.ReportToProto(reportBytes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing report to proto: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := abi.ParseSnpPolicy(report.Policy)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing policy: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
platformInfo, err := abi.ParseSnpPlatformInfo(report.PlatformInfo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing platform info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signature, err := abi.ReportToSignatureDER(reportBytes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing signature: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-09-21 08:08:00 -04:00
|
|
|
signerInfo, err := abi.ParseSignerInfo(report.SignerInfo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing signer info: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-26 07:43:39 -04:00
|
|
|
writeTCB := func(tcbVersion uint64) {
|
|
|
|
tcb := kds.DecomposeTCBVersion(kds.TCBVersion(tcbVersion))
|
|
|
|
writeIndentfln(b, 3, "Secure Processor bootloader SVN: %d", tcb.BlSpl)
|
|
|
|
writeIndentfln(b, 3, "Secure Processor operating system SVN: %d", tcb.TeeSpl)
|
|
|
|
writeIndentfln(b, 3, "SVN 4 (reserved): %d", tcb.Spl4)
|
|
|
|
writeIndentfln(b, 3, "SVN 5 (reserved): %d", tcb.Spl5)
|
|
|
|
writeIndentfln(b, 3, "SVN 6 (reserved): %d", tcb.Spl6)
|
|
|
|
writeIndentfln(b, 3, "SVN 7 (reserved): %d", tcb.Spl7)
|
|
|
|
writeIndentfln(b, 3, "SEV-SNP firmware SVN: %d", tcb.SnpSpl)
|
|
|
|
writeIndentfln(b, 3, "Microcode SVN: %d", tcb.UcodeSpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeIndentfln(b, 1, "SNP Report:")
|
|
|
|
writeIndentfln(b, 2, "Version: %d", report.Version)
|
|
|
|
writeIndentfln(b, 2, "Guest SVN: %d", report.GuestSvn)
|
|
|
|
writeIndentfln(b, 2, "Policy:")
|
|
|
|
writeIndentfln(b, 3, "ABI Minor: %d", policy.ABIMinor)
|
|
|
|
writeIndentfln(b, 3, "ABI Major: %d", policy.ABIMajor)
|
|
|
|
writeIndentfln(b, 3, "Symmetric Multithreading enabled: %t", policy.SMT)
|
|
|
|
writeIndentfln(b, 3, "Migration agent enabled: %t", policy.MigrateMA)
|
|
|
|
writeIndentfln(b, 3, "Debugging enabled (host decryption of VM): %t", policy.Debug)
|
|
|
|
writeIndentfln(b, 3, "Single socket enabled: %t", policy.SingleSocket)
|
|
|
|
writeIndentfln(b, 2, "Family ID: %x", report.FamilyId)
|
|
|
|
writeIndentfln(b, 2, "Image ID: %x", report.ImageId)
|
|
|
|
writeIndentfln(b, 2, "VMPL: %d", report.Vmpl)
|
|
|
|
writeIndentfln(b, 2, "Signature Algorithm: %d", report.SignatureAlgo)
|
|
|
|
writeIndentfln(b, 2, "Current TCB:")
|
|
|
|
writeTCB(report.CurrentTcb)
|
|
|
|
writeIndentfln(b, 2, "Platform Info:")
|
|
|
|
writeIndentfln(b, 3, "Symmetric Multithreading enabled (SMT): %t", platformInfo.SMTEnabled)
|
|
|
|
writeIndentfln(b, 3, "Transparent secure memory encryption (TSME): %t", platformInfo.TSMEEnabled)
|
2023-09-21 08:08:00 -04:00
|
|
|
writeIndentfln(b, 2, "Signer Info:")
|
|
|
|
writeIndentfln(b, 3, "Author Key Enabled: %t", signerInfo.AuthorKeyEn)
|
|
|
|
writeIndentfln(b, 3, "Chip ID Masking: %t", signerInfo.MaskChipKey)
|
|
|
|
writeIndentfln(b, 3, "Signing Type: %s", signerInfo.SigningKey)
|
2023-07-26 07:43:39 -04:00
|
|
|
writeIndentfln(b, 2, "Report Data: %x", report.ReportData)
|
|
|
|
writeIndentfln(b, 2, "Measurement: %x", report.Measurement)
|
|
|
|
writeIndentfln(b, 2, "Host Data: %x", report.HostData)
|
|
|
|
writeIndentfln(b, 2, "ID Key Digest: %x", report.IdKeyDigest)
|
|
|
|
writeIndentfln(b, 2, "Author Key Digest: %x", report.AuthorKeyDigest)
|
|
|
|
writeIndentfln(b, 2, "Report ID: %x", report.ReportId)
|
|
|
|
writeIndentfln(b, 2, "Report ID MA: %x", report.ReportIdMa)
|
|
|
|
writeIndentfln(b, 2, "Reported TCB:")
|
|
|
|
writeTCB(report.ReportedTcb)
|
|
|
|
writeIndentfln(b, 2, "Chip ID: %x", report.ChipId)
|
|
|
|
writeIndentfln(b, 2, "Committed TCB:")
|
|
|
|
writeTCB(report.CommittedTcb)
|
|
|
|
writeIndentfln(b, 2, "Current Build: %d", report.CurrentBuild)
|
|
|
|
writeIndentfln(b, 2, "Current Minor: %d", report.CurrentMinor)
|
|
|
|
writeIndentfln(b, 2, "Current Major: %d", report.CurrentMajor)
|
|
|
|
writeIndentfln(b, 2, "Committed Build: %d", report.CommittedBuild)
|
|
|
|
writeIndentfln(b, 2, "Committed Minor: %d", report.CommittedMinor)
|
|
|
|
writeIndentfln(b, 2, "Committed Major: %d", report.CommittedMajor)
|
|
|
|
writeIndentfln(b, 2, "Launch TCB:")
|
|
|
|
writeTCB(report.LaunchTcb)
|
|
|
|
writeIndentfln(b, 2, "Signature (DER):")
|
|
|
|
writeIndentfln(b, 3, "%x", signature)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:48:14 -04:00
|
|
|
func parseMAAToken(ctx context.Context, b *strings.Builder, rawToken, attestationServiceURL string) error {
|
|
|
|
var claims maaTokenClaims
|
|
|
|
_, err := jwt.ParseWithClaims(rawToken, &claims, keyFromJKUFunc(ctx, attestationServiceURL), jwt.WithIssuedAt())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.MarshalIndent(claims, "\t\t", " ")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshaling claims: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.WriteString("\tMicrosoft Azure Attestation Token:\n\t")
|
|
|
|
b.WriteString(string(out))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// keyFromJKUFunc returns a function that gets the JSON Web Key URI from the token
|
|
|
|
// and fetches the key from that URI. The keys are then parsed, and the key with
|
|
|
|
// the kid that matches the token header is returned.
|
|
|
|
func keyFromJKUFunc(ctx context.Context, webKeysURLBase string) func(token *jwt.Token) (any, error) {
|
|
|
|
return func(token *jwt.Token) (any, error) {
|
|
|
|
webKeysURL, err := url.JoinPath(webKeysURLBase, "certs")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("joining web keys base URL with path: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if token.Header["alg"] != "RS256" {
|
|
|
|
return nil, fmt.Errorf("invalid signing algorithm: %s", token.Header["alg"])
|
|
|
|
}
|
|
|
|
kid, ok := token.Header["kid"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid kid: %v", token.Header["kid"])
|
|
|
|
}
|
|
|
|
jku, ok := token.Header["jku"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid jku: %v", token.Header["jku"])
|
|
|
|
}
|
|
|
|
if jku != webKeysURL {
|
|
|
|
return nil, fmt.Errorf("jku from token (%s) does not match configured attestation service (%s)", jku, webKeysURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
keySetBytes, err := httpGet(ctx, jku)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting signing keys from jku %s: %w", jku, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rawKeySet struct {
|
|
|
|
Keys []struct {
|
|
|
|
X5c [][]byte
|
|
|
|
Kid string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(keySetBytes, &rawKeySet); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range rawKeySet.Keys {
|
|
|
|
if key.Kid != kid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(key.X5c[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert.PublicKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("no key found for kid %s", kid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type maaTokenClaims struct {
|
|
|
|
jwt.RegisteredClaims
|
|
|
|
Secureboot bool `json:"secureboot,omitempty"`
|
|
|
|
XMsAttestationType string `json:"x-ms-attestation-type,omitempty"`
|
|
|
|
XMsAzurevmAttestationProtocolVer string `json:"x-ms-azurevm-attestation-protocol-ver,omitempty"`
|
|
|
|
XMsAzurevmAttestedPcrs []int `json:"x-ms-azurevm-attested-pcrs,omitempty"`
|
|
|
|
XMsAzurevmBootdebugEnabled bool `json:"x-ms-azurevm-bootdebug-enabled,omitempty"`
|
|
|
|
XMsAzurevmDbvalidated bool `json:"x-ms-azurevm-dbvalidated,omitempty"`
|
|
|
|
XMsAzurevmDbxvalidated bool `json:"x-ms-azurevm-dbxvalidated,omitempty"`
|
|
|
|
XMsAzurevmDebuggersdisabled bool `json:"x-ms-azurevm-debuggersdisabled,omitempty"`
|
|
|
|
XMsAzurevmDefaultSecurebootkeysvalidated bool `json:"x-ms-azurevm-default-securebootkeysvalidated,omitempty"`
|
|
|
|
XMsAzurevmElamEnabled bool `json:"x-ms-azurevm-elam-enabled,omitempty"`
|
|
|
|
XMsAzurevmFlightsigningEnabled bool `json:"x-ms-azurevm-flightsigning-enabled,omitempty"`
|
|
|
|
XMsAzurevmHvciPolicy int `json:"x-ms-azurevm-hvci-policy,omitempty"`
|
|
|
|
XMsAzurevmHypervisordebugEnabled bool `json:"x-ms-azurevm-hypervisordebug-enabled,omitempty"`
|
|
|
|
XMsAzurevmIsWindows bool `json:"x-ms-azurevm-is-windows,omitempty"`
|
|
|
|
XMsAzurevmKerneldebugEnabled bool `json:"x-ms-azurevm-kerneldebug-enabled,omitempty"`
|
|
|
|
XMsAzurevmOsbuild string `json:"x-ms-azurevm-osbuild,omitempty"`
|
|
|
|
XMsAzurevmOsdistro string `json:"x-ms-azurevm-osdistro,omitempty"`
|
|
|
|
XMsAzurevmOstype string `json:"x-ms-azurevm-ostype,omitempty"`
|
|
|
|
XMsAzurevmOsversionMajor int `json:"x-ms-azurevm-osversion-major,omitempty"`
|
|
|
|
XMsAzurevmOsversionMinor int `json:"x-ms-azurevm-osversion-minor,omitempty"`
|
|
|
|
XMsAzurevmSigningdisabled bool `json:"x-ms-azurevm-signingdisabled,omitempty"`
|
|
|
|
XMsAzurevmTestsigningEnabled bool `json:"x-ms-azurevm-testsigning-enabled,omitempty"`
|
|
|
|
XMsAzurevmVmid string `json:"x-ms-azurevm-vmid,omitempty"`
|
|
|
|
XMsIsolationTee struct {
|
|
|
|
XMsAttestationType string `json:"x-ms-attestation-type,omitempty"`
|
|
|
|
XMsComplianceStatus string `json:"x-ms-compliance-status,omitempty"`
|
|
|
|
XMsRuntime struct {
|
|
|
|
Keys []struct {
|
|
|
|
E string `json:"e,omitempty"`
|
|
|
|
KeyOps []string `json:"key_ops,omitempty"`
|
|
|
|
Kid string `json:"kid,omitempty"`
|
|
|
|
Kty string `json:"kty,omitempty"`
|
|
|
|
N string `json:"n,omitempty"`
|
|
|
|
} `json:"keys,omitempty"`
|
|
|
|
VMConfiguration struct {
|
|
|
|
ConsoleEnabled bool `json:"console-enabled,omitempty"`
|
|
|
|
CurrentTime int `json:"current-time,omitempty"`
|
|
|
|
SecureBoot bool `json:"secure-boot,omitempty"`
|
|
|
|
TpmEnabled bool `json:"tpm-enabled,omitempty"`
|
|
|
|
VMUniqueID string `json:"vmUniqueId,omitempty"`
|
|
|
|
} `json:"vm-configuration,omitempty"`
|
|
|
|
} `json:"x-ms-runtime,omitempty"`
|
|
|
|
XMsSevsnpvmAuthorkeydigest string `json:"x-ms-sevsnpvm-authorkeydigest,omitempty"`
|
|
|
|
XMsSevsnpvmBootloaderSvn int `json:"x-ms-sevsnpvm-bootloader-svn,omitempty"`
|
|
|
|
XMsSevsnpvmFamilyID string `json:"x-ms-sevsnpvm-familyId,omitempty"`
|
|
|
|
XMsSevsnpvmGuestsvn int `json:"x-ms-sevsnpvm-guestsvn,omitempty"`
|
|
|
|
XMsSevsnpvmHostdata string `json:"x-ms-sevsnpvm-hostdata,omitempty"`
|
|
|
|
XMsSevsnpvmIdkeydigest string `json:"x-ms-sevsnpvm-idkeydigest,omitempty"`
|
|
|
|
XMsSevsnpvmImageID string `json:"x-ms-sevsnpvm-imageId,omitempty"`
|
|
|
|
XMsSevsnpvmIsDebuggable bool `json:"x-ms-sevsnpvm-is-debuggable,omitempty"`
|
|
|
|
XMsSevsnpvmLaunchmeasurement string `json:"x-ms-sevsnpvm-launchmeasurement,omitempty"`
|
|
|
|
XMsSevsnpvmMicrocodeSvn int `json:"x-ms-sevsnpvm-microcode-svn,omitempty"`
|
|
|
|
XMsSevsnpvmMigrationAllowed bool `json:"x-ms-sevsnpvm-migration-allowed,omitempty"`
|
|
|
|
XMsSevsnpvmReportdata string `json:"x-ms-sevsnpvm-reportdata,omitempty"`
|
|
|
|
XMsSevsnpvmReportid string `json:"x-ms-sevsnpvm-reportid,omitempty"`
|
|
|
|
XMsSevsnpvmSmtAllowed bool `json:"x-ms-sevsnpvm-smt-allowed,omitempty"`
|
|
|
|
XMsSevsnpvmSnpfwSvn int `json:"x-ms-sevsnpvm-snpfw-svn,omitempty"`
|
|
|
|
XMsSevsnpvmTeeSvn int `json:"x-ms-sevsnpvm-tee-svn,omitempty"`
|
|
|
|
XMsSevsnpvmVmpl int `json:"x-ms-sevsnpvm-vmpl,omitempty"`
|
|
|
|
} `json:"x-ms-isolation-tee,omitempty"`
|
|
|
|
XMsPolicyHash string `json:"x-ms-policy-hash,omitempty"`
|
|
|
|
XMsRuntime struct {
|
|
|
|
ClientPayload struct {
|
|
|
|
Nonce string `json:"nonce,omitempty"`
|
|
|
|
} `json:"client-payload,omitempty"`
|
|
|
|
Keys []struct {
|
|
|
|
E string `json:"e,omitempty"`
|
|
|
|
KeyOps []string `json:"key_ops,omitempty"`
|
|
|
|
Kid string `json:"kid,omitempty"`
|
|
|
|
Kty string `json:"kty,omitempty"`
|
|
|
|
N string `json:"n,omitempty"`
|
|
|
|
} `json:"keys,omitempty"`
|
|
|
|
} `json:"x-ms-runtime,omitempty"`
|
|
|
|
XMsVer string `json:"x-ms-ver,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// azureInstanceInfo is the b64-decoded InstanceInfo field of the attestation document.
|
|
|
|
// as of now (2023-04-03), it only contains interesting data on Azure.
|
|
|
|
type azureInstanceInfo struct {
|
2023-07-26 07:43:39 -04:00
|
|
|
Vcek []byte
|
|
|
|
CertChain []byte
|
|
|
|
AttestationReport []byte
|
|
|
|
RuntimeData []byte
|
|
|
|
MAAToken string
|
2023-04-03 09:06:27 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
func httpGet(ctx context.Context, url string) ([]byte, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.New(resp.Status)
|
|
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|