/
dhatool.go
308 lines (279 loc) · 7.38 KB
/
dhatool.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
package main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/TylerBrock/colorjson"
xj "github.com/basgys/goxml2json"
"github.com/google/go-tpm/tpm2"
)
var (
logFile = flag.String("log", "", "the TCG log file to use")
certFile = flag.String("cert", "", "the health certificate file to use")
bank = flag.String("bank", "", "the PCR bank to use (SHA1 or SHA256)")
aikFile = flag.String("aik", "", "the .aik file to use")
nonce = flag.String("nonce", "", "the nonce to use (hex)")
)
func printUsage() {
fmt.Printf(`dhatool usage:
extend a log:
dhatool --log=<log file> --bank=<sha1 or sha256> replay
fetch a health certificate:
dhatool --log=<log file> --bank=<sha1 or sha256> --aik=<aik file> getcert
validate a health certificate:
dhatool --cert=<cert file> --bank=<sha1 or sha256> --aik=<aik file> --nonce=<hex nonce> validate
`)
}
func main() {
flag.Parse()
os.Exit(mainWithReturn())
}
func mainWithReturn() int {
if len(flag.Args()) != 1 {
fmt.Fprintf(os.Stderr, "please specify a command\n")
printUsage()
return -1
}
switch strings.ToLower(flag.Args()[0]) {
case "replay":
if err := replayLog(*logFile, *bank); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
printUsage()
return -1
}
case "getcert":
if err := getHealthCert(*logFile, *bank, *aikFile); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
printUsage()
return -1
}
case "validate":
if err := validate(*certFile, *bank, *aikFile, *nonce); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
printUsage()
return -1
}
default:
printUsage()
return -1
}
return -1
}
func hashAlgFromString(name string) (tpm2.Algorithm, error) {
switch strings.ToLower(name) {
case "sha1":
return tpm2.AlgSHA1, nil
case "sha256":
return tpm2.AlgSHA256, nil
default:
return tpm2.AlgNull, fmt.Errorf("'%v' is not a valid PCR bank")
}
}
func replayLog(logFile, bank string) error {
log, err := ioutil.ReadFile(logFile)
if err != nil {
return err
}
alg, err := hashAlgFromString(bank)
if err != nil {
return err
}
fmt.Printf("extending log events...")
err = ReplayLog(log, alg)
fmt.Printf("done!\n")
return err
}
type certifiedAK struct {
Template []byte
Certificate []byte
}
func getHealthCert(logFile, bank, aikFile string) error {
alg, err := hashAlgFromString(bank)
if err != nil {
return err
}
if aikFile == "" {
return fmt.Errorf("please specify an AIK file")
}
if logFile == "" {
return fmt.Errorf("please specify a TCG log file")
}
aikContents, err := ioutil.ReadFile(aikFile)
if err != nil {
return err
}
log, err := ioutil.ReadFile(logFile)
if err != nil {
return err
}
var aik certifiedAK
if err := json.Unmarshal(aikContents, &aik); err != nil {
return err
}
fmt.Printf("generating claims (may take up to 60 seconds)...")
claims, err := MakeClaims(log, aik.Template, nil, alg)
fmt.Printf("done!\n")
if err != nil {
return err
}
claimsBytes, err := claims.Marshal()
if err != nil {
return err
}
req := Request{
Claims: claimsBytes,
AIK: aik.Certificate,
}
reqBytes, err := req.Marshal()
if err != nil {
return err
}
reqRdr := bytes.NewReader(reqBytes)
fmt.Printf("requesting cert...")
client := &http.Client{}
httpReq, err := http.NewRequest("POST", "https://has.spserv.microsoft.com/devicehealthattestation/gethealthcertificate/v1", reqRdr)
if err != nil {
return fmt.Errorf("could not initialize DHA client: %w\n", err)
}
httpReq.Header.Add("content-type", "application/xml")
httpReq.Header.Add("accept", "* /*")
httpReq.Header.Add("user-agent", "Windows Health Cert Retrieval 1.0")
httpReq.Header.Add("dhascorrelationid", "cab697aa-3975-4972-a4b4-6e01f047d224")
resp, err := client.Do(httpReq)
if err != nil {
return fmt.Errorf("error POSTing to DHA: %w\n", err)
}
var respBuf bytes.Buffer
resp.Write(&respBuf)
fmt.Printf("done!\n")
certFile := "./healthcert"
cert, err := extractCertFromResponse(respBuf.Bytes())
if err != nil {
return fmt.Errorf("error from DHA: %w\n", err)
}
if err = ioutil.WriteFile(certFile, cert, 0644); err != nil {
return err
}
fmt.Printf("Wrote %d bytes of health certificate to %s.\n", len(cert), certFile)
return nil
}
func validate(certFile, bank, aikFile, nonce string) error {
alg, err := hashAlgFromString(bank)
if err != nil {
return err
}
if aikFile == "" {
return fmt.Errorf("please specify an AIK file")
}
aikContents, err := ioutil.ReadFile(aikFile)
if err != nil {
return err
}
cert, err := ioutil.ReadFile(certFile)
if err != nil {
return err
}
nonceDecoded, err := hex.DecodeString(nonce)
if err != nil {
return err
}
var aik certifiedAK
if err := json.Unmarshal(aikContents, &aik); err != nil {
return err
}
fmt.Printf("generating claims (may take up to 60 seconds)...")
claims, err := MakeValClaims(aik.Template, nonceDecoded, alg)
fmt.Printf("done!\n")
if err != nil {
return err
}
claimsBytes, err := claims.Marshal()
if err != nil {
return err
}
req := Validation{
Cert: cert,
Nonce: nonceDecoded,
Claims: claimsBytes,
}
reqBytes, err := req.Marshal()
if err != nil {
return err
}
reqRdr := bytes.NewReader(reqBytes)
fmt.Printf("requesting validation...")
client := &http.Client{}
httpReq, err := http.NewRequest("POST", "https://has.spserv.microsoft.com/devicehealthattestation/validatehealthcertificate/v1", reqRdr)
if err != nil {
return fmt.Errorf("could not initialize DHA client: %w\n", err)
}
httpReq.Header.Add("content-type", "application/xml")
httpReq.Header.Add("accept", "*/*")
httpReq.Header.Add("user-agent", "Windows Health Cert Retrieval 1.0")
httpReq.Header.Add("dhascorrelationid", "c8b697aa-3975-4972-a4b4-6e01f047d224")
resp, err := client.Do(httpReq)
if err != nil {
return fmt.Errorf("error POSTing to DHA: %w\n", err)
}
var respBuf bytes.Buffer
resp.Write(&respBuf)
fmt.Printf("done!\n")
prettyPrintStatus(respBuf.Bytes())
return nil
}
func extractCertFromResponse(rsp []byte) ([]byte, error) {
// HACK: TODO: proper xml unmarshalling here...
rspString := string(rsp)
fmt.Printf("Response\n%v\n", rspString)
start := "<HealthCertificateBlob>"
startIdx := strings.Index(rspString, start)
startIdx += len(start)
end := "</HealthCertificateBlob>"
endIdx := strings.Index(rspString, end)
// If we can't find the health blob, just assume there was an error.
if startIdx == -1 || endIdx == -1 {
return nil, fmt.Errorf("error from service: %v", rspString)
}
decoded, err := base64.StdEncoding.DecodeString(rspString[startIdx:endIdx])
if err != nil {
return nil, err
}
if len(decoded) < 3 {
return nil, fmt.Errorf("error from service: %v", rspString)
}
return decoded, nil
}
func prettyPrintStatus(rsp []byte) error {
rspString := string(rsp)
start := "<?xml"
startIdx := strings.Index(rspString, start)
// If we can't find the health data, just assume there was an error.
if startIdx == -1 {
return fmt.Errorf("error from service: %v", rspString)
}
fmt.Printf("Response (XML):\n%s\n", rspString[startIdx:])
xml := rsp[startIdx:]
jsonified, err := xj.Convert(bytes.NewReader(xml))
if err != nil {
return err
}
var parsed map[string]interface{}
if err := json.Unmarshal(jsonified.Bytes(), &parsed); err != nil {
return err
}
f := colorjson.NewFormatter()
f.Indent = 2
s, err := f.Marshal(parsed)
if err != nil {
return err
}
fmt.Printf("Response (JSON):\n%s\n", string(s))
return nil
}