Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attest and debugVerify command to gotpm #293

Merged
merged 7 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions client/attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type AttestOpts struct {
// attestation protocols:
// https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.70.4562&rep=rep1&type=pdf
Nonce []byte
// TCG Event Log to add to the attestation.
// If not specified then it take Event Log by calling GetEventLog().
TCGEventLog []byte
// TCG Canonical Event Log to add to the attestation.
// Currently, we only support PCR replay for PCRs orthogonal to those in the
// firmware event log, where PCRs 0-9 and 14 are often measured. If the two
Expand Down Expand Up @@ -236,8 +239,12 @@ func (k *Key) Attest(opts AttestOpts) (*pb.Attestation, error) {
}
attestation.Quotes = append(attestation.Quotes, quote)
}
if attestation.EventLog, err = GetEventLog(k.rw); err != nil {
return nil, fmt.Errorf("failed to retrieve TCG Event Log: %w", err)
if opts.TCGEventLog == nil {
Pranjali-2501 marked this conversation as resolved.
Show resolved Hide resolved
if attestation.EventLog, err = GetEventLog(k.rw); err != nil {
return nil, fmt.Errorf("failed to retrieve TCG Event Log: %w", err)
}
} else {
attestation.EventLog = opts.TCGEventLog
}
if len(opts.CanonicalEventLog) != 0 {
attestation.CanonicalEventLog = opts.CanonicalEventLog
Expand Down
167 changes: 167 additions & 0 deletions cmd/attest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package cmd

import (
"fmt"
"io"
"strconv"

"cloud.google.com/go/compute/metadata"
"github.com/google/go-tpm-tools/client"
"github.com/google/go-tpm-tools/proto/attest"
"github.com/google/go-tpm/tpm2"
"github.com/spf13/cobra"
"google.golang.org/protobuf/proto"
)

var (
key string
)

var attestationKeys = map[string]map[tpm2.Algorithm]func(rw io.ReadWriter) (*client.Key, error){
"AK": {
tpm2.AlgRSA: client.AttestationKeyRSA,
tpm2.AlgECC: client.AttestationKeyECC,
},
"gceAK": {
tpm2.AlgRSA: client.GceAttestationKeyRSA,
tpm2.AlgECC: client.GceAttestationKeyECC,
},
}

// If hardware technology needs a variable length teenonce then please modify the flags description
var attestCmd = &cobra.Command{
Use: "attest",
Short: "Create a remote attestation report",
Long: `Gather information for remote attestation.
The Attestation report contains a quote on all available PCR banks, a way to validate
the quote, and a TCG Event Log (Linux only).
Use --key to specify the type of attestation key. It can be gceAK for GCE attestation
Pranjali-2501 marked this conversation as resolved.
Show resolved Hide resolved
alexmwu marked this conversation as resolved.
Show resolved Hide resolved
Pranjali-2501 marked this conversation as resolved.
Show resolved Hide resolved
key or AK for a custom attestation key. By default it uses AK.
--algo flag overrides the public key algorithm for attestation key. If not provided then
by default rsa is used.
--teenonce attaches a 64 bytes extra data to the attestation report of TDX and SEV-SNP
hardware and guarantees a fresh quote.
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {

rwc, err := openTpm()
if err != nil {
return err
}
defer rwc.Close()

if !(format == "binarypb" || format == "textproto") {
return fmt.Errorf("format should be either binarypb or textproto")
}

var attestationKey *client.Key
alexmwu marked this conversation as resolved.
Show resolved Hide resolved
algoToCreateAK, ok := attestationKeys[key]
if !ok {
return fmt.Errorf("key should be either AK or gceAK")
}
createFunc := algoToCreateAK[keyAlgo]
attestationKey, err = createFunc(rwc)
alexmwu marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("failed to create attestation key: %v", err)
}
defer attestationKey.Close()

attestOpts := client.AttestOpts{}
attestOpts.Nonce = nonce
if len(teeNonce) != 0 {
attestOpts.TEENonce = teeNonce
attestOpts.TEEDevice, err = client.CreateSevSnpDevice()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getTeeAttestationReport will choose the device if TEEDevice is not set, so I don't think you need to make that default decision here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In getTeeAttestationReport , there is a check

// TEEDevice can't be nil while TEENonce is non-nil
	if opts.TEENonce != nil {
		return fmt.Errorf("got non-nil TEENonce when TEEDevice is nil: %v", opts.TEENonce)
	}

And if the user provides a TEENonce flag , then it always fails that check because TEEDevice is nil.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should just default to using the nonce as the teenonce (and then remove the option). That's what client.Attest does if it successfully creates an SNP device but doesn't get a TEENonce.

Or, we can add a new flag requesting the hardware type (hwattestation?) as part of requesting hardware attestation. And teenonce would fail without that flag.

Either way, we need to be forwards compatible with new technologies like TDX.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my intention for teenonce to take the value of nonce if teenonce is not populated.

if err != nil {
return fmt.Errorf("failed to collect TEE attestation report: %v", err)
}
}

attestOpts.TCGEventLog, err = client.GetEventLog(rwc)
if err != nil {
return fmt.Errorf("failed to retrieve TCG Event Log: %w", err)
}

attestation, err := attestationKey.Attest(attestOpts)
if err != nil {
return fmt.Errorf("failed to collect attestation report : %v", err)
}

if key == "gceAK" {
instanceInfo, err := getInstanceInfoFromMetadata()
if err != nil {
return err
}
attestation.InstanceInfo = instanceInfo
}

var out []byte
if format == "binarypb" {
out, err = proto.Marshal(attestation)
if err != nil {
return fmt.Errorf("failed to marshal attestation proto: %v", attestation)
}
} else {
out = []byte(marshalOptions.Format(attestation))
}
if _, err := dataOutput().Write(out); err != nil {
return fmt.Errorf("failed to write attestation report: %v", err)
}
return nil
},
}

func getInstanceInfoFromMetadata() (*attest.GCEInstanceInfo, error) {

var err error
instanceInfo := &attest.GCEInstanceInfo{}

instanceInfo.ProjectId, err = metadata.ProjectID()
if err != nil {
return nil, err
}

projectNumber, err := metadata.NumericProjectID()
if err != nil {
return nil, err
}
instanceInfo.ProjectNumber, err = strconv.ParseUint(projectNumber, 10, 64)
if err != nil {
return nil, err
}

instanceInfo.Zone, err = metadata.Zone()
if err != nil {
return nil, err
}

instanceID, err := metadata.InstanceID()
if err != nil {
return nil, err
}
instanceInfo.InstanceId, err = strconv.ParseUint(instanceID, 10, 64)
if err != nil {
return nil, err
}

instanceInfo.InstanceName, err = metadata.InstanceName()
if err != nil {
return nil, err
}

return instanceInfo, err
}

func addKeyFlag(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&key, "key", "AK", "indicates type of attestation key to use <gceAK|AK>")
}

func init() {
RootCmd.AddCommand(attestCmd)
addKeyFlag(attestCmd)
addNonceFlag(attestCmd)
addTeeNonceflag(attestCmd)
addPublicKeyAlgoFlag(attestCmd)
addOutputFlag(attestCmd)
addFormatFlag(attestCmd)
}
Loading