-
Notifications
You must be signed in to change notification settings - Fork 259
/
cert.go
217 lines (187 loc) · 5.47 KB
/
cert.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
package main
import (
"bytes"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"log"
"time"
)
func verify_mra_cert(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
printCert(rawCerts[0])
// get the pubkey and payload from raw data
pub_k, payload := unmarshalCert(rawCerts[0])
// Load Intel CA, Verify Cert and Signature
attn_report_raw, err := verifyCert(payload)
if err != nil {
log.Fatalln(err)
return err
}
// Verify attestation report
err = verifyAttReport(attn_report_raw, pub_k)
if err != nil {
log.Fatalln(err)
return err
}
return nil
}
func unmarshalCert(rawbyte []byte) ([]byte, []byte) {
// Search for Public Key prime256v1 OID
prime256v1_oid := []byte{0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07}
offset := uint(bytes.Index(rawbyte, prime256v1_oid))
offset += 11 // 10 + TAG (0x03)
// Obtain Public Key length
length := uint(rawbyte[offset])
if length > 0x80 {
length = uint(rawbyte[offset+1])*uint(0x100) + uint(rawbyte[offset+2])
offset += 2
}
// Obtain Public Key
offset += 1
pub_k := rawbyte[offset+2 : offset+length] // skip "00 04"
// Search for Netscape Comment OID
ns_cmt_oid := []byte{0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x42, 0x01, 0x0D}
offset = uint(bytes.Index(rawbyte, ns_cmt_oid))
offset += 12 // 11 + TAG (0x04)
// Obtain Netscape Comment length
length = uint(rawbyte[offset])
if length > 0x80 {
length = uint(rawbyte[offset+1])*uint(0x100) + uint(rawbyte[offset+2])
offset += 2
}
// Obtain Netscape Comment
offset += 1
payload := rawbyte[offset : offset+length]
return pub_k, payload
}
func verifyCert(payload []byte) ([]byte, error) {
// Extract each field
pl_split := bytes.Split(payload, []byte{0x7C})
attn_report_raw := pl_split[0]
sig_raw := pl_split[1]
var sig, sig_cert_dec []byte
sig, err := base64.StdEncoding.DecodeString(string(sig_raw))
if err != nil {
log.Fatalln(err)
return nil, err
}
sig_cert_raw := pl_split[2]
sig_cert_dec, err = base64.StdEncoding.DecodeString(string(sig_cert_raw))
if err != nil {
log.Fatalln(err)
return nil, err
}
certServer, err := x509.ParseCertificate(sig_cert_dec)
if err != nil {
log.Fatalln(err)
return nil, err
}
roots := x509.NewCertPool()
cacert, err := readFile("./../../cert/AttestationReportSigningCACert.pem")
if err != nil {
log.Fatalln(err)
return nil, err
}
ok := roots.AppendCertsFromPEM([]byte(cacert))
if !ok {
panic("failed to parse root certificate")
}
opts := x509.VerifyOptions{
Roots: roots,
}
if _, err := certServer.Verify(opts); err != nil {
log.Fatalln(err)
return nil, err
} else {
fmt.Println("Cert is good")
}
// Verify the signature against the signing cert
err = certServer.CheckSignature(certServer.SignatureAlgorithm, attn_report_raw, sig)
if err != nil {
log.Fatalln(err)
return nil, err
} else {
fmt.Println("Signature good")
}
return attn_report_raw, nil
}
func verifyAttReport(attn_report_raw []byte, pub_k []byte) error {
var qr QuoteReport
err := json.Unmarshal(attn_report_raw, &qr)
if err != nil {
return err
}
// 1. Check timestamp is within 24H
if qr.Timestamp != "" {
//timeFixed := qr.Timestamp + "+0000"
timeFixed := qr.Timestamp + "Z"
ts, _ := time.Parse(time.RFC3339, timeFixed)
now := time.Now().Unix()
fmt.Println("Time diff = ", now-ts.Unix())
} else {
return errors.New("Failed to fetch timestamp from attestation report")
}
// 2. Verify quote status (mandatory field)
if qr.IsvEnclaveQuoteStatus != "" {
fmt.Println("isvEnclaveQuoteStatus = ", qr.IsvEnclaveQuoteStatus)
switch qr.IsvEnclaveQuoteStatus {
case "OK":
break
case "GROUP_OUT_OF_DATE", "GROUP_REVOKED", "CONFIGURATION_NEEDED":
// Verify platformInfoBlob for further info if status not OK
if qr.PlatformInfoBlob != "" {
platInfo, err := hex.DecodeString(qr.PlatformInfoBlob)
if err != nil && len(platInfo) != 105 {
return errors.New("illegal PlatformInfoBlob")
}
platInfo = platInfo[4:]
piBlob := parsePlatform(platInfo)
piBlobJson ,err := json.Marshal(piBlob)
if err != nil{
return err
}
fmt.Println("Platform info is: "+string(piBlobJson))
} else {
return errors.New("Failed to fetch platformInfoBlob from attestation report")
}
default:
return errors.New("SGX_ERROR_UNEXPECTED")
}
} else {
err := errors.New("Failed to fetch isvEnclaveQuoteStatus from attestation report")
return err
}
// 3. Verify quote body
if qr.IsvEnclaveQuoteBody != "" {
qb, err := base64.StdEncoding.DecodeString(qr.IsvEnclaveQuoteBody)
if err != nil {
return err
}
var quoteBytes, quoteHex, pubHex string
for _, b := range qb {
quoteBytes += fmt.Sprint(int(b), ", ")
quoteHex += fmt.Sprintf("%02x", int(b))
}
for _, b := range pub_k {
pubHex += fmt.Sprintf("%02x", int(b))
}
qrData := parseReport(qb, quoteHex)
fmt.Println("Quote = [" + quoteBytes[:len(quoteBytes)-2] + "]")
fmt.Println("sgx quote version = ", qrData.version)
fmt.Println("sgx quote signature type = ", qrData.signType)
fmt.Println("sgx quote report_data = ", qrData.reportBody.reportData)
fmt.Println("sgx quote mr_enclave = ", qrData.reportBody.mrEnclave)
fmt.Println("sgx quote mr_signer = ", qrData.reportBody.mrSigner)
fmt.Println("Anticipated public key = ", pubHex)
if qrData.reportBody.reportData == pubHex {
fmt.Println("ue RA done!")
}
} else {
err := errors.New("Failed to fetch isvEnclaveQuoteBody from attestation report")
return err
}
return nil
}