/
attest.go
189 lines (163 loc) · 4.95 KB
/
attest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
teeTechnology string
)
// Add constants for other devices when required
const (
// SevSnp is a constant denotes device name for teeTechnology
SevSnp = "sev-snp"
)
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
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.
--tee-nonce 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
algoToCreateAK, ok := attestationKeys[key]
if !ok {
return fmt.Errorf("key should be either AK or gceAK")
}
createFunc := algoToCreateAK[keyAlgo]
attestationKey, err = createFunc(rwc)
if err != nil {
return fmt.Errorf("failed to create attestation key: %v", err)
}
defer attestationKey.Close()
attestOpts := client.AttestOpts{}
attestOpts.Nonce = nonce
// Add logic to open other hardware devices when required.
switch teeTechnology {
case SevSnp:
attestOpts.TEEDevice, err = client.CreateSevSnpDevice()
if err != nil {
return fmt.Errorf("failed to open %s device: %v", SevSnp, err)
}
attestOpts.TEENonce = teeNonce
case "":
if len(teeNonce) != 0 {
return fmt.Errorf("use of --tee-nonce requires specifying TEE hardware type with --tee-technology")
}
default:
// Change the return statement when more devices are added
return fmt.Errorf("tee-technology should be empty or %s", SevSnp)
}
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 addTeeTechnology(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&teeTechnology, "tee-technology", "", "indicates the type of TEE hardware. Should be empty or sev-snp")
}
func init() {
RootCmd.AddCommand(attestCmd)
addKeyFlag(attestCmd)
addNonceFlag(attestCmd)
addTeeNonceflag(attestCmd)
addPublicKeyAlgoFlag(attestCmd)
addOutputFlag(attestCmd)
addFormatFlag(attestCmd)
addTeeTechnology(attestCmd)
}