-
Notifications
You must be signed in to change notification settings - Fork 16
/
snp.go
330 lines (284 loc) · 11.4 KB
/
snp.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package attest
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"io/ioutil"
"os/exec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
snpReportSize = 1184
)
type SNPAttestationReport struct {
// version no. of this attestation report. Set to 1 for this specification.
Version uint32 `json:"version"`
// The guest SVN
GuestSvn uint32 `json:"guest_svn"`
// see table 8 - various settings
Policy uint64 `json:"policy"`
// as provided at launch hex string of a 16-byte integer
FamilyID string `json:"family_id"`
// as provided at launch hex string of a 16-byte integer
ImageID string `json:"image_id"`
// the request VMPL for the attestation report
VMPL uint32 `json:"vmpl"`
SignatureAlgo uint32 `json:"signature_algo"`
// The install version of the firmware
PlatformVersion uint64 `json:"platform_version"`
// information about the platform see table 22
PlatformInfo uint64 `json:"platform_info"`
// 31 bits of reserved, must be zero, bottom bit indicates that the digest of the author key is present in AUTHOR_KEY_DIGEST. Set to the value of GCTX.AuthorKeyEn.
AuthorKeyEn uint32 `json:"author_key_en"`
// must be zero
Reserved1 uint32 `json:"reserved1"`
// Guest provided data. 64-byte
ReportData string `json:"report_data"`
// measurement calculated at launch 48-byte
Measurement string `json:"measurement"`
// data provided by the hypervisor at launch 32-byte
HostData string `json:"host_data"`
// SHA-384 digest of the ID public key that signed the ID block provided in SNP_LAUNCH_FINISH 48-byte
IDKeyDigest string `json:"id_key_digest"`
// SHA-384 digest of the Author public key that certified the ID key, if provided in SNP_LAUNCH_FINISH. Zeros if author_key_en is 1 (sounds backwards to me). 48-byte
AuthorKeyDigest string `json:"author_key_digest"`
// Report ID of this guest. 32-byte
ReportID string `json:"report_id"`
// Report ID of this guest's mmigration agent. 32-byte
ReportIDMA string `json:"report_id_ma"`
// Reported TCB version used to derive the VCEK that signed this report
ReportedTCB uint64 `json:"reported_tcb"`
// reserved 24-byte
Reserved2 string `json:"reserved2"`
// Identifier unique to the chip 64-byte
ChipID string `json:"chip_id"`
// The current commited SVN of the firware (version 2 report feature)
CommittedSvn uint64 `json:"committed_svn"`
// The current commited version of the firware
CommittedVersion uint64 `json:"committed_version"`
// The SVN that this guest was launched or migrated at
LaunchSvn uint64 `json:"launch_svn"`
// reserved 168-byte
Reserved3 string `json:"reserved3"`
// Signature of this attestation report. See table 23. 512-byte
Signature string `json:"signature"`
}
func (r *SNPAttestationReport) DeserializeReport(report []uint8) error {
if len(report) != snpReportSize {
return errors.Errorf("invalid snp report size")
}
r.Version = binary.LittleEndian.Uint32(report[0:4])
r.GuestSvn = binary.LittleEndian.Uint32(report[4:8])
r.Policy = binary.LittleEndian.Uint64(report[8:16])
r.FamilyID = hex.EncodeToString(report[16:32])
r.ImageID = hex.EncodeToString(report[32:48])
r.VMPL = binary.LittleEndian.Uint32(report[48:52])
r.SignatureAlgo = binary.LittleEndian.Uint32(report[52:56])
r.PlatformVersion = binary.LittleEndian.Uint64(report[56:64])
r.PlatformInfo = binary.LittleEndian.Uint64(report[64:72])
r.AuthorKeyEn = binary.LittleEndian.Uint32(report[72:76])
r.Reserved1 = binary.LittleEndian.Uint32(report[76:80])
r.ReportData = hex.EncodeToString(report[80:144])
r.Measurement = hex.EncodeToString(report[144:192])
r.HostData = hex.EncodeToString(report[192:224])
r.IDKeyDigest = hex.EncodeToString(report[224:272])
r.AuthorKeyDigest = hex.EncodeToString(report[272:320])
r.ReportID = hex.EncodeToString(report[320:352])
r.ReportIDMA = hex.EncodeToString(report[352:384])
r.ReportedTCB = binary.LittleEndian.Uint64(report[384:392])
r.Reserved2 = hex.EncodeToString(report[392:416])
r.ChipID = hex.EncodeToString(report[416:480])
r.CommittedSvn = binary.LittleEndian.Uint64(report[480:488])
r.CommittedVersion = binary.LittleEndian.Uint64(report[488:496])
r.LaunchSvn = binary.LittleEndian.Uint64(report[496:504])
r.Reserved3 = hex.EncodeToString(report[504:672])
r.Signature = hex.EncodeToString(report[672:1184])
return nil
}
func (r *SNPAttestationReport) SerializeReport() (report []uint8, err error) {
report = make([]uint8, snpReportSize) // size of struct is 1184 bytes
binary.LittleEndian.PutUint32(report[0:4], r.Version)
binary.LittleEndian.PutUint32(report[4:8], r.GuestSvn)
binary.LittleEndian.PutUint64(report[8:16], r.Policy)
familyIDByteArray, err := hex.DecodeString(r.FamilyID)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.FamilyID failed")
}
for index, b := range familyIDByteArray {
report[index+16] = b
}
imageIDByteArray, err := hex.DecodeString(r.ImageID)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.ImageID failed")
}
for index, b := range imageIDByteArray {
report[index+32] = b
}
binary.LittleEndian.PutUint32(report[48:52], r.VMPL)
binary.LittleEndian.PutUint32(report[52:56], r.SignatureAlgo)
binary.LittleEndian.PutUint64(report[56:64], r.PlatformVersion)
binary.LittleEndian.PutUint64(report[64:72], r.PlatformInfo)
binary.LittleEndian.PutUint32(report[72:76], r.AuthorKeyEn)
binary.LittleEndian.PutUint32(report[76:80], r.Reserved1)
reportDataByteArray, err := hex.DecodeString(r.ReportData)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.ReportData failed")
}
for index, b := range reportDataByteArray {
report[index+80] = b
}
measurementByteArray, err := hex.DecodeString(r.Measurement)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.Measurement failed")
}
for index, b := range measurementByteArray {
report[index+144] = b
}
hostDataByteArray, err := hex.DecodeString(r.HostData)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.HostData failed")
}
for index, b := range hostDataByteArray {
report[index+192] = b
}
IDKeyDigestByteArray, err := hex.DecodeString(r.IDKeyDigest)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.IDKeyDigest failed")
}
for index, b := range IDKeyDigestByteArray {
report[index+224] = b
}
authorKeyDigestByteArray, err := hex.DecodeString(r.AuthorKeyDigest)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.AuthorKeyDigest failed")
}
for index, b := range authorKeyDigestByteArray {
report[index+272] = b
}
reportIDByteArray, err := hex.DecodeString(r.ReportID)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.ReportID failed")
}
for index, b := range reportIDByteArray {
report[index+320] = b
}
reportIDMAByteArray, err := hex.DecodeString(r.ReportIDMA)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.ReportIDMA failed")
}
for index, b := range reportIDMAByteArray {
report[index+352] = b
}
binary.LittleEndian.PutUint64(report[384:392], r.ReportedTCB)
reserved2ByteArray, err := hex.DecodeString(r.Reserved2)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.Reserved2 failed")
}
for index, b := range reserved2ByteArray {
report[index+392] = b
}
chipIDByteArray, err := hex.DecodeString(r.ChipID)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.ChipID failed")
}
for index, b := range chipIDByteArray {
report[index+416] = b
}
reserved3ByteArray, err := hex.DecodeString(r.Reserved3)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.Reserved3 failed")
}
for index, b := range reserved3ByteArray {
report[index+480] = b
}
signatureByteArray, err := hex.DecodeString(r.Signature)
if err != nil {
return nil, errors.Wrapf(err, "decoding r.Signature failed")
}
for index, b := range signatureByteArray {
report[index+672] = b
}
return report, nil
}
// fetchSNPReport abstracts whether we fetch a real or a hardcoded snp
// report (for testing purposes)
func FetchSNPReport(real bool, keyBlob []byte, policyBlob []byte) ([]byte, error) {
if real {
return fetchRealSNPReport(keyBlob)
}
return fetchFakeSNPReport(keyBlob, policyBlob)
}
// fetchFakeSNPReport returns an attestation report containing the hash of the policy in the report's host_data
// this is used for end-to-end testing on hardware without SNP.
func fetchFakeSNPReport(keyBlobBytes []byte, policyBlobBytes []byte) ([]byte, error) {
// hardcoded attestation report that includes default key and policy blobs
runtimeData := sha256.New()
if keyBlobBytes != nil {
runtimeData.Write(keyBlobBytes)
}
hostData := sha256.New()
if policyBlobBytes != nil {
hostData.Write(policyBlobBytes)
}
// For specialist standalone debugging on a non snp machine it is possible use a canned
// report (from a real machine) along with certs from that machine, matching UVM_REFERENCE_INFO
// and policy. This is done by setting the environment variables. Capturing all that from a
// real ACI instance requires a container running with a policy that lets this happen, typically
// the open_door.rego from the hcsshim repo.
var num int
reportBytes, err := ioutil.ReadFile("snp_report.bin")
if err != nil {
// the get-fake-snp-report binary expects ReportData and HostData (hash of policy)
// the rest of such a report will not pass verification of course.
logrus.Debugf("/bin/get-fake-snp-report %s %s", hex.EncodeToString(runtimeData.Sum(nil)), hex.EncodeToString(hostData.Sum(nil)))
cmd := exec.Command("/bin/get-fake-snp-report", hex.EncodeToString(runtimeData.Sum(nil)), hex.EncodeToString(hostData.Sum(nil)))
reportBytesString, err := cmd.Output()
if err != nil {
return nil, errors.Wrapf(err, "cmd.Run() for fetching snp report failed")
}
// the get-snp-report binary outputs the raw hexadecimal representation of the report
reportBytes := make([]byte, hex.DecodedLen(len(reportBytesString)))
num, err = hex.Decode(reportBytes, reportBytesString)
if err != nil {
return nil, errors.Wrapf(err, "decoding output to hexstring failed")
}
} else {
num = len(reportBytes)
}
if num != len(reportBytes) {
return nil, errors.Wrapf(err, "decoding output not expected number of bytes")
}
return reportBytes, nil
}
// fetchRealSNPReport uses the get-snp-report tool to retrieve an attestation report from the PSP
// The attestation report retrieval request passes an arbiratry 64-byte block that will be
// reported as REPORT_DATA (reportBytes)
//
// The get-snp-report tool is compiled during preperation of the container rootfs. The binary is expected to
// be fount under /bin
func fetchRealSNPReport(keyBytes []byte) (reportBytes []byte, err error) {
runtimeData := sha256.New()
if keyBytes != nil {
runtimeData.Write(keyBytes)
}
// the get-snp-report binary expects ReportData as the only command line attribute
logrus.Debugf("/bin/get-snp-report %s", hex.EncodeToString(runtimeData.Sum(nil)))
cmd := exec.Command("/bin/get-snp-report", hex.EncodeToString(runtimeData.Sum(nil)))
reportBytesString, err := cmd.Output()
if err != nil {
return nil, errors.Wrapf(err, "cmd.Run() for fetching snp report failed")
}
// the get-snp-report binary outputs the raw hexadecimal representation of the report
reportBytes = make([]byte, hex.DecodedLen(len(reportBytesString)))
num, err := hex.Decode(reportBytes, reportBytesString)
if err != nil {
return nil, errors.Wrapf(err, "decoding output to hexstring failed")
}
if num != len(reportBytes) {
return nil, errors.Wrapf(err, "decoding output not expected number of bytes")
}
return reportBytes, nil
}