forked from avast/apkverifier
/
signingblock.go
596 lines (494 loc) · 16.3 KB
/
signingblock.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package signingblock
import (
"bytes"
"crypto"
"crypto/sha256"
"crypto/x509"
"encoding/binary"
"errors"
"fmt"
"github.com/appflight/apkverifier/apilevel"
"hash"
"io"
"math"
"os"
)
// https://source.android.com/security/apksigning/v2.html
// frameworks/base/core/java/android/util/apk/ApkSignatureSchemeV2Verifier.java
const (
eocdRecMinSize = 22
eocdRecMagic = 0x06054b50
eocdCommentSizeOffset = 20
eocdCentralDirSizeOffset = 12
eocdCentralDirOffsetOffset = 16
zip64LocatorSize = 20
zip64LocatorMagic = 0x07064b50
apkSigBlockMinSize = 32
apkSigBlockMagicHi = 0x3234206b636f6c42
apkSigBlockMagicLo = 0x20676953204b5041
blockIdVerityPadding = 0x42726577
blockIdSchemeV2 = 0x7109871a
blockIdSchemeV3 = 0xf05368c0
blockIdFrosting = 0x2146444e
schemeIdV1 = 1
schemeIdV2 = 2
schemeIdV3 = 3
maxChunkSize = 1024 * 1024
)
var (
errNoSigningBlockSignature = errors.New("This apk does not have signing block signature")
errEocdNotFound = errors.New("EOCD record not found.")
)
type signatureBlockScheme interface {
parseSigners(block *bytes.Buffer, contentDigests map[crypto.Hash][]byte, result *VerificationResult)
finalizeResult(minSdkVersion, maxSdkVersion int32, result *VerificationResult)
}
type signingBlock struct {
file io.ReadSeeker
fileSize int64
eocdOffset int64
centralDirOffset int64
centralDirSize int64
sigBlockOffset int64
eocd []byte
}
type signingBlockNotFoundError struct {
err error
}
func (e *signingBlockNotFoundError) Error() string {
return "Signature Block signature not found: " + e.err.Error()
}
func IsSigningBlockNotFoundError(err error) bool {
_, ok := err.(*signingBlockNotFoundError)
return ok
}
func VerifySigningBlock(path string, minSdkVersion, maxSdkVersion int32) (res *VerificationResult, magic uint32, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
return VerifySigningBlockReader(f, minSdkVersion, maxSdkVersion)
}
func VerifySigningBlockReader(r io.ReadSeeker, minSdkVersion, maxSdkVersion int32) (res *VerificationResult, magic uint32, err error) {
var s *signingBlock
s, magic, err = newSigningBlock(r, maxSdkVersion)
if err != nil {
return
}
res = &VerificationResult{}
blocks, frosting, err := s.findSignatureBlocks(maxSdkVersion, res)
if frosting != nil && res.Frosting.Error == nil {
res.Frosting.Error = frosting.verifyApk(r, s.sigBlockOffset, blocks[schemeIdV2], s.centralDirOffset, s.centralDirSize, s.eocd)
}
if err != nil {
err = &signingBlockNotFoundError{err}
return
}
var block []byte
var scheme signatureBlockScheme
res.SchemeId, scheme, block = s.pickScheme(blocks, minSdkVersion, maxSdkVersion)
s.verify(scheme, block, minSdkVersion, maxSdkVersion, res)
err = res.GetLastError()
return
}
func ExtractCerts(path string, minSdkVersion, maxSdkVersion int32) (certs [][]*x509.Certificate, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
return ExtractCertsReader(f, minSdkVersion, maxSdkVersion)
}
func ExtractCertsReader(r io.ReadSeeker, minSdkVersion, maxSdkVersion int32) (certs [][]*x509.Certificate, err error) {
var s *signingBlock
s, _, err = newSigningBlock(r, maxSdkVersion)
if err != nil {
return
}
res := &VerificationResult{}
blocks, _, err := s.findSignatureBlocks(maxSdkVersion, res)
if err != nil {
err = &signingBlockNotFoundError{err}
return
}
var block []byte
var scheme signatureBlockScheme
res.SchemeId, scheme, block = s.pickScheme(blocks, minSdkVersion, maxSdkVersion)
s.extractCerts(scheme, block, res)
return res.Certs, res.GetLastError()
}
func newSigningBlock(r io.ReadSeeker, maxSdkVersion int32) (sblock *signingBlock, magic uint32, err error) {
if !apilevel.SupportsSigV2(maxSdkVersion) {
err = &signingBlockNotFoundError{errors.New("unsupported SDK version, requires at least N")}
return
}
size, err := r.Seek(0, io.SeekEnd)
if err != nil {
err = fmt.Errorf("failed to seek to the end of the APK file: %s", err.Error())
}
if _, err := r.Seek(0, io.SeekStart); err != nil {
err = fmt.Errorf("failed to seek to the start of the APK file: %s", err.Error())
}
if size < 4 {
err = fmt.Errorf("APK file is too short (%d bytes).", size)
return
}
if err = binary.Read(r, binary.LittleEndian, &magic); err != nil {
err = fmt.Errorf("Failed to read APK magic: %s", err.Error())
return
}
sblock = &signingBlock{
file: r,
fileSize: size,
}
return
}
func (s *signingBlock) findEocd() error {
if s.fileSize < eocdRecMinSize {
return fmt.Errorf("APK file is too short (%d bytes).", s.fileSize)
}
if err := s.findEocdMaxCommentSize(0); err == nil {
return nil
}
return s.findEocdMaxCommentSize(math.MaxUint16)
}
func (s *signingBlock) findEocdMaxCommentSize(maxCommentSize int) error {
if maxCommentSize > int(s.fileSize-eocdRecMinSize) {
maxCommentSize = int(s.fileSize - eocdRecMinSize)
}
buf := make([]byte, eocdRecMinSize+maxCommentSize)
bufOffsetInFile := s.fileSize - int64(len(buf))
if _, err := s.file.Seek(bufOffsetInFile, io.SeekStart); err != nil {
return err
}
if _, err := io.ReadFull(s.file, buf); err != nil {
return err
}
maxCommentSize = len(buf) - eocdRecMinSize
if maxCommentSize > math.MaxUint16 {
maxCommentSize = math.MaxUint16
}
emptyCommentStart := len(buf) - eocdRecMinSize
for commentSize := 0; commentSize <= maxCommentSize; commentSize++ {
pos := emptyCommentStart - commentSize
if binary.LittleEndian.Uint32(buf[pos:pos+4]) == eocdRecMagic {
recordCommentSize := binary.LittleEndian.Uint16(buf[pos+eocdCommentSizeOffset:])
if int(recordCommentSize) == commentSize {
s.eocdOffset = bufOffsetInFile + int64(pos)
s.centralDirOffset = int64(binary.LittleEndian.Uint32(buf[pos+eocdCentralDirOffsetOffset:]))
s.eocd = buf[pos:]
if s.centralDirOffset >= s.eocdOffset {
return fmt.Errorf("ZIP Central Directory offset ouf of range: %d. Zip End of Central Directory offset: %d",
s.centralDirOffset, s.eocdOffset)
}
s.centralDirSize = int64(binary.LittleEndian.Uint32(buf[pos+eocdCentralDirSizeOffset:]))
if s.centralDirOffset+int64(s.centralDirSize) != s.eocdOffset {
return errors.New("ZIP Central Directory is not immediately followed by End of Central Directory")
}
return nil
}
}
}
return errEocdNotFound
}
func (s *signingBlock) isZip64() bool {
locatorPos := s.eocdOffset - zip64LocatorSize
if locatorPos < 0 {
return false
}
if _, err := s.file.Seek(locatorPos, io.SeekStart); err != nil {
return false
}
var magic uint32
if err := binary.Read(s.file, binary.LittleEndian, &magic); err != nil {
return false
}
return magic == zip64LocatorMagic
}
func (s *signingBlock) pickScheme(blocks map[int][]byte, minSdkVersion, maxSdkVersion int32) (schemeId int, scheme signatureBlockScheme, block []byte) {
if block = blocks[schemeIdV3]; block != nil {
schemeId = schemeIdV3
scheme = &schemeV3{}
} else if block = blocks[schemeIdV2]; block != nil {
schemeId = schemeIdV2
scheme = &schemeV2{minSdkVersion, maxSdkVersion}
} else {
panic("unhandled")
}
return
}
func (s *signingBlock) findApkSigningBlock() (block []byte, offset int64, err error) {
if s.centralDirOffset < apkSigBlockMinSize {
err = errNoSigningBlockSignature
return
}
footer := make([]byte, 24)
if _, err = s.file.Seek(s.centralDirOffset-int64(len(footer)), io.SeekStart); err != nil {
return
}
if _, err = io.ReadFull(s.file, footer); err != nil {
return
}
if binary.LittleEndian.Uint64(footer[8:]) != apkSigBlockMagicLo ||
binary.LittleEndian.Uint64(footer[16:]) != apkSigBlockMagicHi {
err = errNoSigningBlockSignature
return
}
blockSizeFooter := binary.LittleEndian.Uint64(footer)
if blockSizeFooter < uint64(len(footer)) || blockSizeFooter > math.MaxInt32-8 {
err = fmt.Errorf("APK Signing Block size out of range: %d", blockSizeFooter)
return
}
totalSize := int64(blockSizeFooter + 8)
if totalSize < apkSigBlockMinSize {
err = fmt.Errorf("Apk Signing Block is too small: %d vs %d", totalSize, apkSigBlockMinSize)
return
}
offset = int64(s.centralDirOffset) - totalSize
if offset < 0 {
err = fmt.Errorf("APK Signing Block offset out of range: %d", offset)
return
}
block = make([]byte, totalSize)
if _, err = s.file.Seek(offset, io.SeekStart); err != nil {
return
}
if _, err = io.ReadFull(s.file, block); err != nil {
return
}
if blockSizeHeader := binary.LittleEndian.Uint64(block); blockSizeHeader != blockSizeFooter {
err = fmt.Errorf("APK Signing Block sizes in header and footer do not match: %d vs %d",
blockSizeHeader, blockSizeFooter)
return
}
return
}
func (s *signingBlock) findSignatureBlocks(maxSdkVersion int32, res *VerificationResult) (blocks map[int][]byte, frostingRes *frostingInfo, err error) {
if err = s.findEocd(); err != nil {
err = &signingBlockNotFoundError{err}
return
}
if s.isZip64() {
err = &signingBlockNotFoundError{errors.New("ZIP64 APK not supported")}
return
}
var sigBlock []byte
sigBlock, s.sigBlockOffset, err = s.findApkSigningBlock()
if err != nil {
err = &signingBlockNotFoundError{err}
return
}
pairs := bytes.NewReader(sigBlock[8 : len(sigBlock)-24])
entryCount := 0
blocks = make(map[int][]byte)
for pairs.Len() > 0 {
entryCount++
if pairs.Len() < 8 {
err = fmt.Errorf("Insufficient data to read size of APK Signing Block entry #%d", entryCount)
return
}
var entryLen int64
binary.Read(pairs, binary.LittleEndian, &entryLen)
if entryLen < 4 || entryLen > math.MaxInt32 {
err = fmt.Errorf("APK Signing Block entry #%d size out of range: %d", entryCount, entryLen)
return
}
nextEntryPos := pairs.Size() - int64(pairs.Len()) + entryLen
if entryLen > int64(pairs.Len()) {
err = fmt.Errorf("APK Signing Block entry #%d size out of range: %d, available: %d",
entryCount, entryLen, pairs.Len())
return
}
var id uint32
if err = binary.Read(pairs, binary.LittleEndian, &id); err != nil {
err = fmt.Errorf("failed to read signing block id: %s", err.Error())
return
}
switch id {
case blockIdSchemeV3:
if apilevel.SupportsSigV3(maxSdkVersion) {
block := make([]byte, entryLen-4)
if _, err = pairs.Read(block); err != nil {
return
}
blocks[schemeIdV3] = block
}
case blockIdSchemeV2:
block := make([]byte, entryLen-4)
if _, err = pairs.Read(block); err != nil {
return
}
blocks[schemeIdV2] = block
case blockIdVerityPadding:
// Block full of zeros to ensure the signing block is padded to 4K,
// for verity signature verification.
case blockIdFrosting:
frostingBlock := make([]byte, entryLen-4)
if _, err = pairs.Read(frostingBlock); err != nil {
return
}
var frosting frostingInfo
var frResult FrostingResult
res.Frosting = &frResult
frResult.KeySha256, frResult.ProtobufInfo, frResult.Error = frosting.parse(frostingBlock)
if frResult.Error != nil {
res.addWarning("frosting: %s", frResult.Error.Error())
} else {
frostingRes = &frosting
}
}
if _, err = pairs.Seek(nextEntryPos, io.SeekStart); err != nil {
return
}
}
if len(blocks) == 0 {
err = errors.New("No APK Signature block in APK Signing Block")
}
return
}
func (s *signingBlock) extractCerts(scheme signatureBlockScheme, block []byte, res *VerificationResult) map[crypto.Hash][]byte {
contentDigests := make(map[crypto.Hash][]byte)
signatureBlock := bytes.NewBuffer(block)
scheme.parseSigners(signatureBlock, contentDigests, res)
return contentDigests
}
func (s *signingBlock) verify(scheme signatureBlockScheme, block []byte, minSdkVersion, maxSdkVersion int32, res *VerificationResult) {
contentDigests := s.extractCerts(scheme, block, res)
if len(res.Certs) == 0 {
res.addError("no signers found")
return
}
if len(contentDigests) == 0 {
res.addError("no content digests found")
return
}
if !s.verifyIntegrity(contentDigests, res) {
return
}
scheme.finalizeResult(minSdkVersion, maxSdkVersion, res)
}
func (s *signingBlock) verifyIntegrity(expectedDigests map[crypto.Hash][]byte, result *VerificationResult) bool {
beforeApkSigningBlock := &dataSourceApk{file: s.file, start: 0, end: s.sigBlockOffset}
centralDir := &dataSourceApk{file: s.file, start: s.centralDirOffset, end: s.eocdOffset}
eocd := &dataSourceBytes{data: append([]byte(nil), s.eocd...)}
// For the purposes of integrity verification, ZIP End of Central Directory's field Start of
// Central Directory must be considered to point to the offset of the APK Signing Block.
binary.LittleEndian.PutUint32(eocd.data[eocdCentralDirOffsetOffset:], uint32(s.sigBlockOffset))
digestAlgorithms := make([]crypto.Hash, 0, len(expectedDigests))
for algo := range expectedDigests {
digestAlgorithms = append(digestAlgorithms, algo)
}
actualDigests, err := s.computeContentDigests(digestAlgorithms, beforeApkSigningBlock, centralDir, eocd)
if err != nil {
result.addError("Failed to compute digest(s) of contents: %s", err.Error())
return false
}
for _, algo := range digestAlgorithms {
if !bytes.Equal(expectedDigests[algo], actualDigests[algo]) {
result.addError("%d digest of contents did not verify.", algo)
continue
}
}
return true
}
func (s *signingBlock) computeContentDigests(digestAlgorithms []crypto.Hash, contents ...dataSource) (map[crypto.Hash][]byte, error) {
var oneMbAlgos []crypto.Hash
hasVerity := false
for _, algo := range digestAlgorithms {
switch algo {
case crypto.SHA256, crypto.SHA512:
oneMbAlgos = append(oneMbAlgos, algo)
case veritySHA256:
hasVerity = true
default:
panic(fmt.Sprintf("unhandled crypto algo %d", int(algo)))
}
}
var result map[crypto.Hash][]byte
var err error
if len(oneMbAlgos) != 0 {
result, err = s.computeOneMbChunkContentDigests(oneMbAlgos, contents)
if err != nil {
return result, err
}
}
if hasVerity {
if result == nil {
result = make(map[crypto.Hash][]byte)
}
result[veritySHA256], err = s.computeVerityContentDigest(contents)
if err != nil {
return result, err
}
}
if len(result) == 0 {
return nil, fmt.Errorf("no valid digest algorithms")
}
return result, nil
}
func (s *signingBlock) computeOneMbChunkContentDigests(digestAlgorithms []crypto.Hash, contents []dataSource) (map[crypto.Hash][]byte, error) {
var totalChunkCount int64
for _, input := range contents {
totalChunkCount += input.chunkCount()
}
if totalChunkCount >= math.MaxInt32/1024 {
return nil, fmt.Errorf("Too many chunks: %d", totalChunkCount)
}
digestsOfChunks := make([][]byte, len(digestAlgorithms))
hashers := make([]hash.Hash, len(digestAlgorithms))
for i, algo := range digestAlgorithms {
buf := make([]byte, 5+totalChunkCount*int64(algo.Size()))
buf[0] = 0x5a
binary.LittleEndian.PutUint32(buf[1:], uint32(totalChunkCount))
digestsOfChunks[i] = buf
hashers[i] = algo.New()
}
chunkContentPrefix := make([]byte, 5)
chunkContentPrefix[0] = 0xa5
chunkIndex := 0
for inputIdx, input := range contents {
var offset int64
remaining := input.length()
for remaining > 0 {
chunkSize := remaining
if chunkSize > maxChunkSize {
chunkSize = maxChunkSize
}
binary.LittleEndian.PutUint32(chunkContentPrefix[1:], uint32(chunkSize))
for i := range hashers {
hashers[i].Write(chunkContentPrefix)
if err := input.writeTo(hashers[i], offset, chunkSize); err != nil {
return nil, fmt.Errorf("Failed to digest chunk #%d of section #%d", chunkIndex, inputIdx)
}
sum := hashers[i].Sum(nil)
hashers[i].Reset()
copy(digestsOfChunks[i][5+chunkIndex*len(sum):], sum)
}
offset += chunkSize
remaining -= chunkSize
chunkIndex++
}
}
result := make(map[crypto.Hash][]byte, len(digestAlgorithms))
for i := range digestsOfChunks {
hashers[i].Write(digestsOfChunks[i])
result[digestAlgorithms[i]] = hashers[i].Sum(nil)
}
return result, nil
}
func (s *signingBlock) computeVerityContentDigest(contents []dataSource) ([]byte, error) {
if contents[0].length()%verityChunkSize != 0 {
return nil, fmt.Errorf("APK Signing block size not a multiple of %d", verityChunkSize)
}
chained := newChainedDataSource(contents...)
builder := newVerityTreeBuilder(make([]byte, 8))
rootHash, err := builder.generateTreeRootHash(chained)
if err != nil {
return nil, fmt.Errorf("failed to generate verity tree root hash: %s", err.Error())
}
result := make([]byte, sha256.Size+8)
copy(result, rootHash)
binary.LittleEndian.PutUint64(result[sha256.Size:], uint64(chained.length()))
return result, nil
}