| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/google/go-sev-guest/proto/sevsnp" |
| 9 |
sv "github.com/google/go-sev-guest/verify" |
| 10 |
"github.com/google/go-tdx-guest/proto/tdx" |
| 11 |
tv "github.com/google/go-tdx-guest/verify" |
| 12 |
pb "github.com/google/go-tpm-tools/proto/attest" |
| 13 |
"github.com/google/go-tpm-tools/server" |
| 14 |
"github.com/google/go-tpm/legacy/tpm2" |
| 15 |
"github.com/spf13/cobra" |
| 16 |
"google.golang.org/protobuf/proto" |
| 17 |
) |
| 18 |
|
| 19 |
var verifyCmd = &cobra.Command{ |
| 20 |
Use: "verify", |
| 21 |
Short: "Verify a remote attestation report.", |
| 22 |
Args: cobra.NoArgs, |
| 23 |
} |
| 24 |
var debugCmd = &cobra.Command{ |
| 25 |
Use: "debug", |
| 26 |
Short: "Debug the contents of an attestation report without verifying its root-of-trust (e.g., attestation key certificate). For debugging purposes only", |
| 27 |
RunE: func(*cobra.Command, []string) error { |
| 28 |
attestationBytes, err := io.ReadAll(dataInput()) |
| 29 |
if err != nil { |
| 30 |
return err |
| 31 |
} |
| 32 |
attestation := &pb.Attestation{} |
| 33 |
|
| 34 |
if format == "binarypb" { |
| 35 |
err = proto.Unmarshal(attestationBytes, attestation) |
| 36 |
} else if format == "textproto" { |
| 37 |
err = unmarshalOptions.Unmarshal(attestationBytes, attestation) |
| 38 |
} else { |
| 39 |
return fmt.Errorf("format should be either binarypb or textproto") |
| 40 |
} |
| 41 |
if err != nil { |
| 42 |
return fmt.Errorf("fail to unmarshal attestation report: %v", err) |
| 43 |
} |
| 44 |
|
| 45 |
pub, err := tpm2.DecodePublic(attestation.GetAkPub()) |
| 46 |
if err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
cryptoPub, err := pub.Key() |
| 50 |
if err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
|
| 54 |
// TODO(#524): create separate, discrete subcommands that verifies SNP and TDX attestation. |
| 55 |
ms, err := server.VerifyAttestation(attestation, server.VerifyOpts{Nonce: nonce, TrustedAKs: []crypto.PublicKey{cryptoPub}}) |
| 56 |
if err != nil { |
| 57 |
return fmt.Errorf("verifying TPM attestation: %w", err) |
| 58 |
} |
| 59 |
err = verifyGceTechnology(attestation) |
| 60 |
if err != nil { |
| 61 |
return fmt.Errorf("verifying TEE attestation: %w", err) |
| 62 |
} |
| 63 |
teeMS, err := parseTEEAttestation(attestation, ms.GetPlatform().Technology) |
| 64 |
if err != nil { |
| 65 |
return fmt.Errorf("failed to parse machineState from TEE attestation: %w", err) |
| 66 |
} |
| 67 |
ms.TeeAttestation = teeMS.TeeAttestation |
| 68 |
out, err := marshalOptions.Marshal(ms) |
| 69 |
if err != nil { |
| 70 |
return nil |
| 71 |
} |
| 72 |
if _, err := dataOutput().Write(out); err != nil { |
| 73 |
return fmt.Errorf("failed to write verified attestation report: %v", err) |
| 74 |
} |
| 75 |
return nil |
| 76 |
}, |
| 77 |
} |
| 78 |
|
| 79 |
// parseTEEAttestation parses a machineState from TeeAttestation. |
| 80 |
// For now it simply populates the machineState TeeAttestation field with the verified TDX/SNP data. |
| 81 |
// In long term, it should parse a full machineState from TeeAttestation. |
| 82 |
func parseTEEAttestation(attestation *pb.Attestation, tech pb.GCEConfidentialTechnology) (*pb.MachineState, error) { |
| 83 |
switch tech { |
| 84 |
case pb.GCEConfidentialTechnology_AMD_SEV_SNP: |
| 85 |
tee, ok := attestation.TeeAttestation.(*pb.Attestation_SevSnpAttestation) |
| 86 |
if !ok { |
| 87 |
return nil, fmt.Errorf("TEE attestation is %T, expected a SevSnpAttestation", attestation.GetTeeAttestation()) |
| 88 |
} |
| 89 |
return &pb.MachineState{ |
| 90 |
TeeAttestation: &pb.MachineState_SevSnpAttestation{ |
| 91 |
SevSnpAttestation: proto.Clone(tee.SevSnpAttestation).(*sevsnp.Attestation), |
| 92 |
}}, nil |
| 93 |
case pb.GCEConfidentialTechnology_INTEL_TDX: |
| 94 |
tee, ok := attestation.TeeAttestation.(*pb.Attestation_TdxAttestation) |
| 95 |
if !ok { |
| 96 |
return nil, fmt.Errorf("TEE attestation is %T, expected a TdxAttestation", attestation.GetTeeAttestation()) |
| 97 |
} |
| 98 |
return &pb.MachineState{ |
| 99 |
TeeAttestation: &pb.MachineState_TdxAttestation{ |
| 100 |
TdxAttestation: proto.Clone(tee.TdxAttestation).(*tdx.QuoteV4), |
| 101 |
}}, nil |
| 102 |
default: |
| 103 |
return &pb.MachineState{}, nil |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func verifyGceTechnology(attestation *pb.Attestation) error { |
| 108 |
if attestation.GetTeeAttestation() == nil { |
| 109 |
return nil |
| 110 |
} |
| 111 |
switch attestation.GetTeeAttestation().(type) { |
| 112 |
case *pb.Attestation_TdxAttestation: |
| 113 |
var tdxOpts *verifyTdxOpts |
| 114 |
if len(teeNonce) != 0 { |
| 115 |
tdxOpts = &verifyTdxOpts{ |
| 116 |
Validation: tdxDefaultValidateOpts(teeNonce), |
| 117 |
Verification: tv.DefaultOptions(), |
| 118 |
} |
| 119 |
} else { |
| 120 |
tdxOpts = &verifyTdxOpts{ |
| 121 |
Validation: tdxDefaultValidateOpts(nonce), |
| 122 |
Verification: tv.DefaultOptions(), |
| 123 |
} |
| 124 |
} |
| 125 |
tee, ok := attestation.TeeAttestation.(*pb.Attestation_TdxAttestation) |
| 126 |
if !ok { |
| 127 |
return fmt.Errorf("TEE attestation is %T, expected a TdxAttestation", attestation.GetTeeAttestation()) |
| 128 |
} |
| 129 |
return verifyTdxAttestation(tee.TdxAttestation, tdxOpts) |
| 130 |
case *pb.Attestation_SevSnpAttestation: |
| 131 |
var snpOpts *verifySnpOpts |
| 132 |
if len(teeNonce) != 0 { |
| 133 |
snpOpts = &verifySnpOpts{ |
| 134 |
Validation: sevSnpDefaultValidateOpts(teeNonce), |
| 135 |
Verification: &sv.Options{}, |
| 136 |
} |
| 137 |
} else { |
| 138 |
snpOpts = &verifySnpOpts{ |
| 139 |
Validation: sevSnpDefaultValidateOpts(nonce), |
| 140 |
Verification: &sv.Options{}, |
| 141 |
} |
| 142 |
} |
| 143 |
tee, ok := attestation.TeeAttestation.(*pb.Attestation_SevSnpAttestation) |
| 144 |
if !ok { |
| 145 |
return fmt.Errorf("TEE attestation is %T, expected a SevSnpAttestation", attestation.GetTeeAttestation()) |
| 146 |
} |
| 147 |
return verifySevSnpAttestation(tee.SevSnpAttestation, snpOpts) |
| 148 |
default: |
| 149 |
return fmt.Errorf("unknown attestation type: %T", attestation.GetTeeAttestation()) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func init() { |
| 154 |
RootCmd.AddCommand(verifyCmd) |
| 155 |
verifyCmd.AddCommand(debugCmd) |
| 156 |
addNonceFlag(debugCmd) |
| 157 |
addOutputFlag(debugCmd) |
| 158 |
addInputFlag(debugCmd) |
| 159 |
addFormatFlag(debugCmd) |
| 160 |
addTeeNonceflag(debugCmd) |
| 161 |
addCertifiedAKBlobFlag(debugCmd) |
| 162 |
debugCmd.AddCommand(verifySVSMCmd) |
| 163 |
addEKPubFlag(verifySVSMCmd) |
| 164 |
addTeeTechnology(verifySVSMCmd) |
| 165 |
} |