forked from agl/crlset-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crlset.go
380 lines (321 loc) · 8.8 KB
/
crlset.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file of the Chromium repository.
// This is a small program to download and parse CRLSets.
package main
import (
"archive/zip"
"bytes"
"crypto"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
"encoding/binary"
"encoding/json"
"encoding/pem"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
)
// update and the related structures are used for parsing the XML response from Omaha. The response looks like:
// <?xml version="1.0" encoding="UTF-8"?>
// <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0" server="prod">
// <daystart elapsed_seconds="42913"/>
// <app appid="hfnkpimlhhgieaddgfemjhofmfblmnib" status="ok">
// <updatecheck codebase="http://www.gstatic.com/chrome/crlset/56/crl-set-14830555124393087472.crx.data" hash="" size="0" status="ok" version="56"/>
// </app>
// </gupdate>
type update struct {
XMLName xml.Name `xml:"gupdate"`
Apps []updateApp `xml:"app"`
}
type updateApp struct {
AppId string `xml:"appid,attr"`
UpdateCheck updateCheck
}
type updateCheck struct {
XMLName xml.Name `xml:"updatecheck"`
URL string `xml:"codebase,attr"`
Version string `xml:"version,attr"`
}
// crlSetAppId is the hex(ish) encoded public key hash of the key that signs
// the CRL sets.
const crlSetAppId = "hfnkpimlhhgieaddgfemjhofmfblmnib"
// buildVersionRequestURL returns a URL from which the current CRLSet version
// information can be fetched.
func buildVersionRequestURL() string {
args := url.Values(make(map[string][]string))
args.Add("x", "id="+crlSetAppId+"&v=&uc")
return (&url.URL{
Scheme: "http",
Host: "clients2.google.com",
Path: "/service/update2/crx",
RawQuery: args.Encode(),
}).String()
}
// crxHeader reflects the binary header of a CRX file.
type crxHeader struct {
Magic [4]byte
Version uint32
PubKeyBytes uint32
SigBytes uint32
}
// zipReader is a small wrapper around a []byte which implements ReaderAt.
type zipReader []byte
func (z zipReader) ReadAt(p []byte, pos int64) (int, error) {
if int(pos) < 0 {
return 0, nil
}
return copy(p, []byte(z)[int(pos):]), nil
}
func fetch() bool {
resp, err := http.Get(buildVersionRequestURL())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get current version: %s\n", err)
return false
}
var reply update
bodyBytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read version reply: %s\n", err)
return false
}
if err := xml.Unmarshal(bodyBytes, &reply); err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse version reply: %s\n", err)
return false
}
var crxURL, version string
for _, app := range reply.Apps {
if app.AppId == crlSetAppId {
crxURL = app.UpdateCheck.URL
version = app.UpdateCheck.Version
break
}
}
fmt.Fprintf(os.Stderr, "Downloading CRLSet version %s\n", version)
if len(crxURL) == 0 {
fmt.Fprintf(os.Stderr, "Failed to parse Omaha response\n")
return false
}
resp, err = http.Get(crxURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get CRX: %s\n", err)
return false
}
defer resp.Body.Close()
// zip needs to seek around, so we read the whole reply into memory.
crxBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to download CRX: %s\n", err)
return false
}
crx := bytes.NewBuffer(crxBytes)
var header crxHeader
if err := binary.Read(crx, binary.LittleEndian, &header); err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse CRX header: %s\n", err)
return false
}
if !bytes.Equal(header.Magic[:], []byte("Cr24")) ||
int(header.PubKeyBytes) < 0 ||
int(header.SigBytes) < 0 {
fmt.Fprintf(os.Stderr, "Downloaded file doesn't look like a CRX\n")
return false
}
pubKeyBytes := crx.Next(int(header.PubKeyBytes))
sigBytes := crx.Next(int(header.SigBytes))
if len(pubKeyBytes) != int(header.PubKeyBytes) ||
len(sigBytes) != int(header.SigBytes) {
fmt.Fprintf(os.Stderr, "Downloaded file doesn't look like a CRX\n")
return false
}
pubKey, err := x509.ParsePKIXPublicKey(pubKeyBytes)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse public key: %s\n", err)
return false
}
rsaPubKey, ok := pubKey.(*rsa.PublicKey)
if !ok {
fmt.Fprintf(os.Stderr, "Not signed with an RSA key\n")
return false
}
h := sha256.New()
h.Write(pubKeyBytes)
pubKeyHash := fmt.Sprintf("%x", h.Sum(nil)[:16])
tweakedPubKeyHash := make([]byte, len(pubKeyHash))
// AppIds use a different hex character set so we convert our hash into
// it.
for i := range pubKeyHash {
if pubKeyHash[i] < 97 {
tweakedPubKeyHash[i] = pubKeyHash[i] + 49
} else {
tweakedPubKeyHash[i] = pubKeyHash[i] + 10
}
}
if string(tweakedPubKeyHash) != crlSetAppId {
fmt.Fprintf(os.Stderr, "Public key mismatch (%s)\n", tweakedPubKeyHash)
return false
}
zipBytes := crx.Bytes()
sha1Hash := sha1.New()
sha1Hash.Write(zipBytes)
if err := rsa.VerifyPKCS1v15(rsaPubKey, crypto.SHA1, sha1Hash.Sum(nil), sigBytes); err != nil {
fmt.Fprintf(os.Stderr, "Signature verification failure: %s\n", err)
return false
}
zipReader := zipReader(zipBytes)
z, err := zip.NewReader(zipReader, int64(len(zipBytes)))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse ZIP file: %s\n", err)
return false
}
var crlFile *zip.File
for _, file := range z.File {
if file.Name == "crl-set" {
crlFile = file
break
}
}
if crlFile == nil {
fmt.Fprintf(os.Stderr, "Downloaded CRX didn't contain a CRLSet\n")
return false
}
crlSetReader, err := crlFile.Open()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open crl-set in ZIP: %s\n", err)
return false
}
defer crlSetReader.Close()
io.Copy(os.Stdout, crlSetReader)
return true
}
// crlSetHeader is used to parse the JSON header found in CRLSet files.
type crlSetHeader struct {
Sequence int
NumParents int
}
func dump(filename string, certificateFilename string) bool {
var spki []byte
if len(certificateFilename) > 0 {
certBytes, err := ioutil.ReadFile(certificateFilename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read certificate: %s\n", err)
return false
}
var derBytes []byte
if block, _ := pem.Decode(certBytes); block == nil {
derBytes = certBytes
} else {
derBytes = block.Bytes
}
cert, err := x509.ParseCertificate(derBytes)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse certificate: %s\n", err)
return false
}
h := sha256.New()
h.Write(cert.RawSubjectPublicKeyInfo)
spki = h.Sum(nil)
}
c, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read CRLSet: %s\n", err)
return false
}
if len(c) < 2 {
fmt.Fprintf(os.Stderr, "CRLSet truncated at header length\n")
return false
}
headerLen := int(c[0]) | int(c[1])<<8
c = c[2:]
if len(c) < headerLen {
fmt.Fprintf(os.Stderr, "CRLSet truncated at header\n")
return false
}
headerBytes := c[:headerLen]
c = c[headerLen:]
var header crlSetHeader
if err := json.Unmarshal(headerBytes, &header); err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse header: %s", err)
return false
}
if len(spki) == 0 {
fmt.Printf("Sequence: %d\n", header.Sequence)
fmt.Printf("Parents: %d\n", header.NumParents)
fmt.Printf("\n")
}
const spkiHashLen = 32
for len(c) > 0 {
if len(c) < spkiHashLen {
fmt.Fprintf(os.Stderr, "CRLSet truncated at SPKI hash\n")
return false
}
spkiMatches := bytes.Equal(spki, c[:spkiHashLen])
if len(spki) == 0 {
fmt.Printf("%x\n", c[:spkiHashLen])
}
c = c[spkiHashLen:]
if len(c) < 4 {
fmt.Fprintf(os.Stderr, "CRLSet truncated at serial count\n")
return false
}
numSerials := uint32(c[0]) | uint32(c[1])<<8 | uint32(c[2])<<16 | uint32(c[3])<<24
c = c[4:]
for i := uint32(0); i < numSerials; i++ {
if len(c) < 1 {
fmt.Fprintf(os.Stderr, "CRLSet truncated at serial length\n")
return false
}
serialLen := int(c[0])
c = c[1:]
if len(c) < serialLen {
fmt.Fprintf(os.Stderr, "CRLSet truncated at serial\n")
return false
}
if len(spki) == 0 {
fmt.Printf(" %x\n", c[:serialLen])
} else if spkiMatches {
fmt.Printf("%x\n", c[:serialLen])
}
c = c[serialLen:]
}
}
return true
}
func usage() {
fmt.Fprintf(os.Stderr, "%s: { fetch | dump <filename> [<cert filename>] }\n", os.Args[0])
}
func main() {
if len(os.Args) < 2 {
usage()
os.Exit(1)
}
result := false
needUsage := true
switch os.Args[1] {
case "fetch":
if len(os.Args) == 2 {
result = fetch()
needUsage = false
}
case "dump":
if len(os.Args) == 3 {
needUsage = false
result = dump(os.Args[2], "")
} else if len(os.Args) == 4 {
needUsage = false
result = dump(os.Args[2], os.Args[3])
}
}
if needUsage {
usage()
}
if !result {
os.Exit(1)
}
}