Debian Codegraph @

signingblock/schemev2.go

raw symbols SCIP
1 package signingblock
2
3 import (
4 "bytes"
5 "crypto"
6 "encoding/binary"
7 )
8
9 const (
10 attrV2StrippingProtection = 0xbeeff00d
11 )
12
13 type schemeV2 struct {
14 minSdkVersion, maxSdkVersion int32
15 }
16
17 func (s *schemeV2) parseSigners(block *bytes.Buffer, contentDigests map[crypto.Hash][]byte, result *VerificationResult) {
18 signers, err := getLenghtPrefixedSlice(block)
19 if err != nil {
20 result.addError("failed to read list of signers: %s", err.Error())
21 return
22 }
23
24 signerCount := 0
25 for signers.Len() > 0 {
26 signerCount++
27
28 signer, err := getLenghtPrefixedSlice(signers)
29 if err != nil {
30 result.addError("failed to parse/verify signer #%d block: %s", signerCount, err.Error())
31 return
32 }
33
34 s.verifySigner(signer, contentDigests, result)
35 }
36 }
37
38 func (s *schemeV2) finalizeResult(minSdkVersion, maxSdkVersion int32, result *VerificationResult) {
39
40 }
41
42 func (s *schemeV2) verifySigner(signerBlock *bytes.Buffer, contentDigests map[crypto.Hash][]byte, result *VerificationResult) {
43 signedData, err := getLenghtPrefixedSlice(signerBlock)
44 if err != nil {
45 result.addError("failed to read signed data: %s", err.Error())
46 return
47 }
48 signedDataBytes := signedData.Bytes()
49
50 ctx := signerContext{result: result}
51
52 // Parse signatures
53 signaturesSlice, err := getLenghtPrefixedSlice(signerBlock)
54 if err != nil {
55 result.addError("failed to read signatures: %s", err.Error())
56 return
57 }
58
59 if !ctx.parseSignatures(signaturesSlice) {
60 return
61 }
62
63 // Parse & verify public key
64 publicKeySlice, err := getLenghtPrefixedSlice(signerBlock)
65 if err != nil {
66 result.addError("failed to read publicKeySlice: %s", err.Error())
67 return
68 }
69
70 if !ctx.parsePublicKey(publicKeySlice, signedDataBytes) {
71 return
72 }
73
74 // Get digests
75 digestsSlice, err := getLenghtPrefixedSlice(signedData)
76 if err != nil {
77 result.addError("failed to read digests from signedData: %s", err.Error())
78 return
79 }
80
81 // Get certs
82 certificatesSlice, err := getLenghtPrefixedSlice(signedData)
83 if err != nil {
84 result.addError("failed to read certificates from signedData: %s", err.Error())
85 return
86 }
87
88 // Parse certificates
89 if _, success := ctx.parseCertificates(certificatesSlice); !success {
90 return
91 }
92
93 // Parse digests
94 if !ctx.parseDigests(digestsSlice, contentDigests) {
95 return
96 }
97
98 // Parse additional attributes
99 additionalAttributes, err := getLenghtPrefixedSlice(signedData)
100 if err != nil {
101 result.addError("failed to read additionalAttributes from signedData: %s", err.Error())
102 return
103 }
104
105 additionalAttributeCount := 0
106 for additionalAttributes.Len() > 0 {
107 additionalAttributeCount++
108
109 attribute, err := getLenghtPrefixedSlice(additionalAttributes)
110 if err != nil {
111 result.addError("failed to read additional attribute %d: %s", additionalAttributeCount, err.Error())
112 return
113 }
114
115 var id uint32
116 if err := binary.Read(attribute, binary.LittleEndian, &id); err != nil {
117 result.addError("failed to read additional attribute %d's id: %s", additionalAttributeCount, err.Error())
118 return
119 }
120
121 switch id {
122 case attrV2StrippingProtection:
123 if s.maxSdkVersion < 28 {
124 break
125 }
126
127 var strippedSchemeId int32
128 if err := binary.Read(attribute, binary.LittleEndian, &strippedSchemeId); err != nil {
129 result.addError("failed to read additional attribute %d's strippedSchemeId: %s", additionalAttributeCount, err.Error())
130 return
131 }
132
133 switch strippedSchemeId {
134 case schemeIdV3:
135 result.addError("this apk was signed with v3 signing scheme, but it was stripped, downgrade attack?")
136 return
137 default:
138 result.addError("unknown stripped scheme id: %d", strippedSchemeId)
139 }
140 default:
141 result.addWarning("unknown additional attribute id 0x%x", uint32(id))
142 }
143 }
144 }