-
Notifications
You must be signed in to change notification settings - Fork 15
/
attest.go
195 lines (170 loc) · 4.85 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
190
191
192
193
194
195
package attest
import (
"bytes"
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"os/exec"
)
const (
NUM_RTMRS = 4
rtmrSize = sha512.Size384
)
type TdReport struct {
ReportMac
TeeTcbInfo
Reserved [17]byte
TdInfo
}
type ReportType struct {
Type byte
Subtype byte
Version byte
Reserved byte
}
type ReportMac struct {
ReportType
Reserved0 [12]byte
CpuSvn [16]byte
TeeTcbInfoHash TeeMeasurement
TeeInfoHash TeeMeasurement
ReportData TeeReportData
Reserved1 [32]byte
Mac [32]byte
}
type TeeTcbInfo struct {
Data [239]byte
}
type TdInfo struct {
Attributes TeeAttributes // TD's attributes
Xfam TeeAttributes // TD's XFAM
Mrtd TeeMeasurement // Measurement of initial contents of the TD
MrConfigId TeeMeasurement // Software-defined ID for non-owner-defined configuration of the guest TD
MrOwner TeeMeasurement // Software-defined ID for the guest TD's owner
MrOwnerConfig TeeMeasurement // Software-defined ID for owner-defined configuration of the guest TD
Rtmr [NUM_RTMRS]TeeMeasurement // Array of NUM_RTMRS(4) run-time extendable measurement registers
ServTdHash TeeMeasurement
Reserved [64]byte
}
type TeeMeasurement struct {
M [48]byte
}
func (m TeeMeasurement) MarshalJSON() ([]byte, error) {
src := m.M[:]
return json.Marshal(hex.EncodeToString(src))
}
func (m TeeMeasurement) String() string {
return hex.EncodeToString(m.M[:])
}
type TeeAttributes struct {
A [2]uint32
}
func (a TeeAttributes) String() string {
return fmt.Sprintf("%#x %#x", a.A[0], a.A[1])
}
type TeeReportData struct {
D [64]byte
}
func (r TeeReportData) MarshalJSON() ([]byte, error) {
src := r.D[:]
//fmt.Println(hex.EncodeToString(src))
return json.Marshal(hex.EncodeToString(src))
}
type SGXQuote4Header struct {
Version uint16 `json:"version"`
AttKeyType uint16 `json:"-"`
TeeType uint32 `json:"teeType"`
Reserved uint32 `json:"-"`
VendorId [16]byte `json:"-"`
UserData [20]byte `json:"-"`
}
type SGXReport2Body struct {
TeeTcbSvn [16]byte `json:"-"`
MrSeam TeeMeasurement `json:"-"`
MrSignerSeam TeeMeasurement `json:"-"`
SeamAttributes TeeAttributes `json:"-"`
TdAttributes TeeAttributes `json:"-"`
Xfam TeeAttributes `json:"-"`
MrTd TeeMeasurement `json:"-"`
MrConfigId TeeMeasurement `json:"-"`
MrOwner TeeMeasurement `json:"-"`
MrOwnerConfig TeeMeasurement `json:"-"`
Rtmr [NUM_RTMRS]TeeMeasurement `json:"rtmr"`
ReportData TeeReportData `json:"reportData"`
}
type SGXQuote4 struct {
Header SGXQuote4Header `json:"header"`
ReportBody SGXReport2Body `json:"reportBody"`
SigDataLen uint32 `json:"-"`
//SigData []byte
}
type AttestData struct {
ApiVersion string `json:"api_version"`
RequestorNonce []byte `json:"requestor_nonce"`
AcondNonce []byte `json:"acond_nonce"`
AttestationData map[string]map[uint32]AttestDataValue `json:"attestation_data"`
}
type AttestDataValue struct {
Type int32 `json:"type"`
Data string `json:"data"`
}
func ParseQuote(quote []byte) (*SGXQuote4, error) {
quoteStruct := new(SGXQuote4)
err := binary.Read(bytes.NewReader(quote), binary.LittleEndian, quoteStruct)
if err != nil {
return nil, fmt.Errorf("parse quote: %v", err)
}
return quoteStruct, nil
}
func ParseAttestData(data []byte) (*AttestData, error) {
attestData := new(AttestData)
err := json.Unmarshal(data, attestData)
if err != nil {
return nil, fmt.Errorf("unmarshal attest data error: %v", err)
}
return attestData, nil
}
func WriteQuote(filename string, quote []byte) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(quote)
return err
}
func VerifyQuote(verifier, quote string) (bool, error) {
cmd := exec.Command(verifier, "-quote", quote)
if err := cmd.Run(); err != nil {
return false, err
} else {
return true, nil
}
}
func GetRtmrValue(logs []string) []byte {
result := make([]byte, rtmrSize)
for _, log := range logs {
logSum := sha512.Sum384([]byte(log))
result = append(result, logSum[:]...)
sum := sha512.Sum384(result)
result = sum[:]
}
return result
}
func VerifyRtmr(quote []byte, logs []string) (bool, error) {
quoteStruct, err := ParseQuote(quote)
if err != nil {
return false, fmt.Errorf("verify rtmr error: %v", err)
}
mr := hex.EncodeToString(GetRtmrValue(logs))
r := quoteStruct.ReportBody.Rtmr[3]
mrFromQuote := hex.EncodeToString(r.M[:])
if mr == mrFromQuote {
return true, nil
} else {
return false, fmt.Errorf("rtmr does not match")
}
}