-
Notifications
You must be signed in to change notification settings - Fork 0
/
xar_reader.go
313 lines (244 loc) · 6.5 KB
/
xar_reader.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
package xargon
import (
"bytes"
"compress/bzip2"
"compress/zlib"
"crypto/x509"
"encoding/base64"
"encoding/binary"
"encoding/xml"
"errors"
"io"
"os"
"strings"
)
const (
applicationOctetStreamMimeType = "application/octet-stream"
applicationGzipMimeType = "application/x-gzip"
applicationBzip2MimeType = "application/x-bzip2"
)
type XarReader struct {
file *os.File
header *xarHeader
toc *xarToc
heap *io.SectionReader
certs []*x509.Certificate
filesIndex map[string]*xarFile
filesOrder []string
}
func (xr *XarReader) readHeader() error {
if xr.file == nil {
return errors.New("cannot read header from nil file")
}
h := make([]byte, xarHeaderSize)
if n, err := xr.file.ReadAt(h, 0); err != nil {
return err
} else if n != xarHeaderSize {
return errors.New("xar header size mismatch")
}
xh := &xarHeader{
magic: binary.BigEndian.Uint32(h[0:4]),
size: binary.BigEndian.Uint16(h[4:6]),
version: binary.BigEndian.Uint16(h[6:8]),
tocLengthCompressed: binary.BigEndian.Uint64(h[8:16]),
tocLengthUncompressed: binary.BigEndian.Uint64(h[16:24]),
checksumAlgorithm: binary.BigEndian.Uint32(h[24:28]),
}
// validate expected values
if xh.magic != xarHeaderMagic {
return errors.New("unexpected xar format")
}
if xh.size != xarHeaderSize {
return errors.New("unexpected xar h size")
}
if xh.version != xarHeaderVersion {
return errors.New("unsupported xar version")
}
if xh.tocLengthCompressed == 0 {
return errors.New("unexpected xar toc compressed length")
}
if xh.tocLengthUncompressed == 0 {
return errors.New("unexpected xar toc uncompressed length")
}
if xh.checksumAlgorithm == 3 {
return errors.New("unsupported xar checksum algorithm")
}
xr.header = xh
return nil
}
func (xr *XarReader) readToc() error {
if xr.file == nil {
return errors.New("cannot read toc from nil file")
}
if xr.header == nil {
return errors.New("cannot read toc from nil header")
}
toc := make([]byte, xr.header.tocLengthCompressed)
if n, err := xr.file.ReadAt(toc, xarHeaderSize); err != nil {
return err
} else if uint64(n) != xr.header.tocLengthCompressed {
return errors.New("xar toc size mismatch")
}
br := bytes.NewReader(toc)
zr, err := zlib.NewReader(br)
if err != nil {
return err
}
return xml.NewDecoder(zr).Decode(&xr.toc)
}
func (xr *XarReader) heapOffset() (int64, error) {
if xr.header == nil {
return -1, errors.New("cannot read toc from nil header")
}
return xarHeaderSize + int64(xr.header.tocLengthCompressed), nil
}
func (xr *XarReader) readHeap() error {
if xr.file == nil {
return errors.New("cannot read heap from nil file")
}
if xr.header == nil {
return errors.New("cannot read heap from nil header")
}
heapOffset, err := xr.heapOffset()
if err != nil {
return nil
}
xr.heap = io.NewSectionReader(xr.file, heapOffset, int64(xr.header.size)-heapOffset)
return nil
}
func (xr *XarReader) readCertificates() error {
if xr.toc == nil {
return errors.New("cannot check signatures from nil toc")
}
for _, encCert := range xr.toc.Toc.Signature.KeyInfo.X509Data.X509Certificate {
cb64 := []byte(strings.Replace(encCert, "\n", "", -1))
cder := make([]byte, base64.StdEncoding.DecodedLen(len(cb64)))
ndec, err := base64.StdEncoding.Decode(cder, cb64)
if err != nil {
return err
}
if cert, err := x509.ParseCertificate(cder[0:ndec]); err != nil {
return err
} else {
xr.certs = append(xr.certs, cert)
}
}
return nil
}
func (xr *XarReader) CheckCertificatesSignatures() error {
if len(xr.certs) == 0 {
if err := xr.readCertificates(); err != nil {
return err
}
}
// in order to verify certificates we check signatures in the chain
// this assumes certificates are ordered
for ii := 0; ii < len(xr.certs); ii++ {
if ii == len(xr.certs)-1 {
continue
}
if err := xr.certs[ii].CheckSignatureFrom(xr.certs[ii+1]); err != nil {
return err
}
}
return nil
}
func (xr *XarReader) indexFiles() error {
if xr.toc == nil {
return errors.New("cannot index files from nil toc")
}
files := xr.toc.Toc.File
xr.filesOrder = make([]string, 0, len(files))
xr.filesIndex = make(map[string]*xarFile, len(files))
for _, file := range files {
fo, fi := file.indexFiles()
for _, fof := range fo {
xr.filesOrder = append(xr.filesOrder, fof)
xr.filesIndex[fof] = fi[fof]
}
}
return nil
}
func (xr *XarReader) ReadFiles() ([]string, error) {
if xr.filesOrder == nil {
if err := xr.indexFiles(); err != nil {
return nil, err
}
}
if xr.filesOrder == nil {
return nil, errors.New("xar files have not been ordered")
}
return xr.filesOrder, nil
}
func (xr *XarReader) Open(name string) (io.Reader, error) {
if xr.filesIndex == nil {
if err := xr.indexFiles(); err != nil {
return nil, err
}
}
if xr.filesIndex == nil {
return nil, errors.New("xar files have not been indexed")
}
if xf, ok := xr.filesIndex[name]; ok {
return xr.openFile(xf)
} else {
return nil, errors.New("xar file not found: " + name)
}
}
func (xr *XarReader) ChecksumMethod(name string) (string, string, error) {
if xr.filesIndex == nil {
if err := xr.indexFiles(); err != nil {
return "", "", err
}
}
if xr.filesIndex == nil {
return "", "", errors.New("xar files have not been indexed")
}
if xf, ok := xr.filesIndex[name]; ok {
extChecksum := xf.Data.ExtractedChecksum
return extChecksum.Text, extChecksum.Style, nil
} else {
return "", "", errors.New("xar file not found: " + name)
}
}
func (xr *XarReader) openFile(xf *xarFile) (io.Reader, error) {
if xf == nil {
return nil, errors.New("cannot open nil xar file")
}
sectionReader := io.NewSectionReader(xr.heap, xf.Data.Offset, xf.Data.Length)
enc := xf.Data.Encoding.Style
switch enc {
case applicationOctetStreamMimeType:
return sectionReader, nil
case applicationGzipMimeType:
return zlib.NewReader(sectionReader)
case applicationBzip2MimeType:
return bzip2.NewReader(sectionReader), nil
default:
return nil, errors.New("unknown xar file encoding: " + enc)
}
}
func (xr *XarReader) Close() error {
return xr.file.Close()
}
func NewReader(path string) (*XarReader, error) {
xf, err := os.Open(path)
if err != nil {
return nil, err
}
xr := &XarReader{
file: xf,
}
// xar file = Header + TOC + Heap
// we initialize all three below
if err := xr.readHeader(); err != nil {
return nil, err
}
if err := xr.readToc(); err != nil {
return nil, err
}
if err := xr.readHeap(); err != nil {
return nil, err
}
return xr, nil
}