@@ -9,7 +9,6 @@ package snp
9
9
import (
10
10
"context"
11
11
"crypto"
12
- "crypto/sha512"
13
12
"crypto/x509"
14
13
"encoding/json"
15
14
"fmt"
@@ -27,7 +26,6 @@ import (
27
26
"github.com/google/go-sev-guest/verify"
28
27
"github.com/google/go-sev-guest/verify/trust"
29
28
"github.com/google/go-tpm-tools/proto/attest"
30
- "github.com/google/go-tpm/legacy/tpm2"
31
29
)
32
30
33
31
// Validator for GCP SEV-SNP / TPM attestation.
@@ -62,53 +60,30 @@ func NewValidator(cfg *config.GCPSEVSNP, log attestation.Logger) (*Validator, er
62
60
v .Validator = vtpm .NewValidator (
63
61
cfg .Measurements ,
64
62
v .getTrustedKey ,
65
- v . validateCVM ,
63
+ func ( _ vtpm. AttestationDocument , _ * attest. MachineState ) error { return nil } ,
66
64
log ,
67
65
)
68
66
return v , nil
69
67
}
70
68
71
69
// getTrustedKey returns TPM endorsement key provided through the GCE metadata API.
72
- func (v * Validator ) getTrustedKey (ctx context.Context , attDoc vtpm.AttestationDocument , _ []byte ) (crypto.PublicKey , error ) {
70
+ func (v * Validator ) getTrustedKey (ctx context.Context , attDoc vtpm.AttestationDocument , extraData []byte ) (crypto.PublicKey , error ) {
73
71
ekPub , err := v .gceKeyGetter (ctx , attDoc , nil )
74
72
if err != nil {
75
73
return nil , fmt .Errorf ("getting TPM endorsement key: %w" , err )
76
74
}
77
75
78
- return ekPub , nil
79
- }
80
-
81
- // validateCVM validates the SEV-SNP attestation document.
82
- func (v * Validator ) validateCVM (attDoc vtpm.AttestationDocument , _ * attest.MachineState ) error {
83
- pubArea , err := tpm2 .DecodePublic (attDoc .Attestation .AkPub )
84
- if err != nil {
85
- return fmt .Errorf ("decoding public area: %w" , err )
86
- }
87
-
88
- pubKey , err := pubArea .Key ()
89
- if err != nil {
90
- return fmt .Errorf ("getting public key: %w" , err )
91
- }
92
-
93
- akDigest , err := sha512sum (pubKey )
94
- if err != nil {
95
- return fmt .Errorf ("calculating hash of attestation key: %w" , err )
96
- }
97
-
98
- if err := v .reportValidator .validate (attDoc , (* x509 .Certificate )(& v .cfg .AMDSigningKey ), (* x509 .Certificate )(& v .cfg .AMDRootKey ), akDigest , v .cfg , v .log ); err != nil {
99
- return fmt .Errorf ("validating SNP report: %w" , err )
76
+ if len (extraData ) > 64 {
77
+ return nil , fmt .Errorf ("extra data too long: %d, should be 64 bytes at most" , len (extraData ))
100
78
}
101
- return nil
102
- }
79
+ extraData64 := make ([] byte , 64 )
80
+ copy ( extraData64 , extraData )
103
81
104
- // sha512sum PEM-encodes a public key and calculates the SHA512 hash of the encoded key.
105
- func sha512sum (key crypto.PublicKey ) ([64 ]byte , error ) {
106
- pub , err := x509 .MarshalPKIXPublicKey (key )
107
- if err != nil {
108
- return [64 ]byte {}, fmt .Errorf ("marshalling public key: %w" , err )
82
+ if err := v .reportValidator .validate (attDoc , (* x509 .Certificate )(& v .cfg .AMDSigningKey ), (* x509 .Certificate )(& v .cfg .AMDRootKey ), [64 ]byte (extraData64 ), v .cfg , v .log ); err != nil {
83
+ return nil , fmt .Errorf ("validating SNP report: %w" , err )
109
84
}
110
85
111
- return sha512 . Sum512 ( pub ) , nil
86
+ return ekPub , nil
112
87
}
113
88
114
89
// snpReportValidator validates a given SNP report.
@@ -146,7 +121,7 @@ func (r *reportVerifierImpl) SnpAttestation(att *sevsnp.Attestation, opts *verif
146
121
// validate the report by checking if it has a valid VCEK signature.
147
122
// The certificate chain ARK -> ASK -> VCEK is also validated.
148
123
// Checks that the report's userData matches the connection's userData.
149
- func (a * gcpValidator ) validate (attestation vtpm.AttestationDocument , ask * x509.Certificate , ark * x509.Certificate , akDigest [64 ]byte , config * config.GCPSEVSNP , log attestation.Logger ) error {
124
+ func (a * gcpValidator ) validate (attestation vtpm.AttestationDocument , ask * x509.Certificate , ark * x509.Certificate , reportData [64 ]byte , config * config.GCPSEVSNP , log attestation.Logger ) error {
150
125
var info snp.InstanceInfo
151
126
if err := json .Unmarshal (attestation .InstanceInfo , & info ); err != nil {
152
127
return fmt .Errorf ("unmarshalling instance info: %w" , err )
@@ -170,7 +145,7 @@ func (a *gcpValidator) validate(attestation vtpm.AttestationDocument, ask *x509.
170
145
171
146
validateOpts := & validate.Options {
172
147
// Check that the attestation key's digest is included in the report.
173
- ReportData : akDigest [:],
148
+ ReportData : reportData [:],
174
149
GuestPolicy : abi.SnpPolicy {
175
150
Debug : false , // Debug means the VM can be decrypted by the host for debugging purposes and thus is not allowed.
176
151
SMT : true , // Allow Simultaneous Multi-Threading (SMT). Normally, we would want to disable SMT
@@ -205,11 +180,11 @@ func (a *gcpValidator) validate(attestation vtpm.AttestationDocument, ask *x509.
205
180
func getVerifyOpts (att * sevsnp.Attestation ) (* verify.Options , error ) {
206
181
ask , err := x509 .ParseCertificate (att .CertificateChain .AskCert )
207
182
if err != nil {
208
- return & verify. Options {} , fmt .Errorf ("parsing ASK certificate: %w" , err )
183
+ return nil , fmt .Errorf ("parsing ASK certificate: %w" , err )
209
184
}
210
185
ark , err := x509 .ParseCertificate (att .CertificateChain .ArkCert )
211
186
if err != nil {
212
- return & verify. Options {} , fmt .Errorf ("parsing ARK certificate: %w" , err )
187
+ return nil , fmt .Errorf ("parsing ARK certificate: %w" , err )
213
188
}
214
189
215
190
verifyOpts := & verify.Options {
0 commit comments