Debian Codegraph @

lib/fruit/csblob/codedir.go

raw symbols SCIP
1 package csblob
2
3 import (
4 "bytes"
5 "crypto"
6 "encoding/binary"
7 "errors"
8 )
9
10 type CodeDirectoryHeader struct {
11 Magic csMagic
12 Length uint32
13 Version uint32
14 Flags SignatureFlags
15
16 HashOffset uint32
17 IdentOffset uint32
18 SpecialSlotCount uint32
19 CodeSlotCount uint32
20 CodeLimit uint32
21
22 HashSize uint8
23 HashType HashType
24 _ uint8
25 PageSizeLog2 uint8
26 _ uint32
27 // Version >= 0x20100
28 ScatterOffset uint32
29 // Version >= 0x20200
30 TeamOffset uint32
31 _ uint32
32 // Version >= 0x20300
33 CodeLimit64 int64
34 // Version >= 0x20400
35 ExecSegmentBase int64
36 ExecSegmentLimit int64
37 ExecSegmentFlags int64
38 }
39
40 type CodeDirectory struct {
41 Header CodeDirectoryHeader
42 SigningIdentity string
43 TeamIdentifier string
44 HashFunc crypto.Hash
45
46 CodeHashes [][]byte
47 ManifestHash []byte
48 RequirementsHash []byte
49 ResourcesHash []byte
50 EntitlementsHash []byte
51 EntitlementsDERHash []byte
52 RepSpecificHash []byte
53
54 Raw []byte
55 CDHash []byte
56 IType uint32
57 }
58
59 type SignatureFlags uint32
60
61 // CSCommon.h
62 const (
63 FlagHost SignatureFlags = 0x000001
64 FlagAdhoc SignatureFlags = 0x000002
65 FlagForceHard SignatureFlags = 0x000100
66 FlagForceKill SignatureFlags = 0x000200
67 FlagForceExpiration SignatureFlags = 0x000400
68 FlagRestrict SignatureFlags = 0x000800
69 FlagEnforcement SignatureFlags = 0x001000
70 FlagLibraryValidation SignatureFlags = 0x002000
71 FlagRuntime SignatureFlags = 0x010000
72 FlagLinkerSigned SignatureFlags = 0x020000
73 )
74
75 // don't propagate these to a new signature
76 const clearFlags = FlagAdhoc | FlagLinkerSigned
77
78 func parseCodeDirectory(blob []byte, itype uint32) (*CodeDirectory, error) {
79 var hdr CodeDirectoryHeader
80 if err := binary.Read(bytes.NewReader(blob), binary.BigEndian, &hdr); err != nil {
81 return nil, err
82 }
83 // zero out fields that aren't present in the current version
84 switch {
85 case hdr.Version < 0x20100:
86 hdr.ScatterOffset = 0
87 fallthrough
88 case hdr.Version < 0x20200:
89 hdr.TeamOffset = 0
90 fallthrough
91 case hdr.Version < 0x20300:
92 hdr.CodeLimit64 = 0
93 fallthrough
94 case hdr.Version < 0x20400:
95 hdr.ExecSegmentBase = 0
96 hdr.ExecSegmentFlags = 0
97 hdr.ExecSegmentLimit = 0
98 }
99 dir := &CodeDirectory{
100 Header: hdr,
101 Raw: blob,
102 IType: itype,
103 }
104 // read indirect fields
105 var err error
106 if hdr.IdentOffset != 0 {
107 dir.SigningIdentity, err = cstring(blob, int(hdr.IdentOffset))
108 if err != nil {
109 return nil, err
110 }
111 }
112 if hdr.TeamOffset != 0 {
113 dir.TeamIdentifier, err = cstring(blob, int(hdr.TeamOffset))
114 if err != nil {
115 return nil, err
116 }
117 }
118 if hdr.ScatterOffset != 0 {
119 return nil, errors.New("scatterOffset is not supported")
120 }
121 dir.HashFunc, err = hashFunc(hdr.HashType, hdr.HashSize)
122 if err != nil {
123 return nil, err
124 }
125 // hash over whole directory for signature to use
126 h := dir.HashFunc.New()
127 h.Write(blob)
128 dir.CDHash = h.Sum(nil)
129 // read hash slots
130 hashBase := int(hdr.HashOffset)
131 hashLen := int(hdr.HashSize)
132 slot := func(i int) []byte {
133 hash := blob[hashBase+i*hashLen : hashBase+(i+1)*hashLen]
134 for _, c := range hash {
135 if c != 0 {
136 return hash
137 }
138 }
139 // all zero
140 return nil
141 }
142 dir.CodeHashes = make([][]byte, hdr.CodeSlotCount)
143 for i := 0; i < int(hdr.CodeSlotCount); i++ {
144 dir.CodeHashes[i] = slot(i)
145 }
146 for i := 1; i <= int(hdr.SpecialSlotCount); i++ {
147 // special slots are placed before the code slots with an index equal to
148 // the negative of their superblob itype
149 v := slot(-i)
150 switch i {
151 case cdInfoSlot:
152 dir.ManifestHash = v
153 case cdRequirementsSlot:
154 dir.RequirementsHash = v
155 case cdResourceDirSlot:
156 dir.ResourcesHash = v
157 case cdEntitlementSlot:
158 dir.EntitlementsHash = v
159 case cdEntitlementDERSlot:
160 dir.EntitlementsDERHash = v
161 case cdRepSpecificSlot:
162 dir.RepSpecificHash = v
163 }
164 }
165 return dir, nil
166 }
167
168 func cstring(blob []byte, i int) (string, error) {
169 if i >= len(blob) {
170 return "", errShort
171 }
172 blob = blob[i:]
173 j := bytes.IndexByte(blob, 0)
174 if j < 0 {
175 return "", errShort
176 }
177 return string(blob[:j]), nil
178 }
179
180 type codeDirParams struct {
181 *SignatureParams
182 Specials [][]byte
183 CodeSlots []byte
184 CodeSlotCount uint32
185 HashFunc crypto.Hash
186 CodeLimit int64
187 SinglePage bool
188 }
189
190 type codeDirResult struct {
191 Raw []byte
192 Digest []byte
193 HashFunc crypto.Hash
194 }
195
196 func newCodeDirectory(params codeDirParams) (codeDirResult, error) {
197 hdr := CodeDirectoryHeader{
198 Magic: csCodeDirectory,
199 Version: 0x20300,
200 Flags: params.Flags,
201 SpecialSlotCount: uint32(len(params.Specials)),
202 CodeSlotCount: params.CodeSlotCount,
203 HashSize: uint8(params.HashFunc.Size()),
204 PageSizeLog2: defaultPageSizeLog2,
205 ExecSegmentBase: params.ExecSegmentBase,
206 ExecSegmentLimit: params.ExecSegmentLimit,
207 ExecSegmentFlags: params.ExecSegmentFlags,
208 }
209 var err error
210 hdr.HashType, err = hashType(params.HashFunc)
211 if err != nil {
212 return codeDirResult{}, err
213 }
214 if hdr.ExecSegmentBase != 0 || hdr.ExecSegmentLimit != 0 || hdr.ExecSegmentFlags != 0 {
215 hdr.Version = 0x20400
216 }
217 if params.SinglePage {
218 // single slot DMG
219 hdr.PageSizeLog2 = 0
220 }
221 // compute special slots
222 h := params.HashFunc.New()
223 specialSlots := make([]byte, 0, h.Size()*len(params.Specials))
224 for _, special := range params.Specials {
225 if special != nil {
226 h.Reset()
227 h.Write(special)
228 specialSlots = h.Sum(specialSlots)
229 } else {
230 specialSlots = specialSlots[:len(specialSlots)+h.Size()]
231 }
232 }
233 // fill out header
234 if params.CodeLimit > (1<<31)-2 {
235 hdr.CodeLimit64 = params.CodeLimit
236 } else {
237 hdr.CodeLimit = uint32(params.CodeLimit)
238 }
239 offset := binary.Size(hdr)
240 hdr.IdentOffset = uint32(offset)
241 offset += len(params.SigningIdentity) + 1
242 if params.TeamIdentifier != "" {
243 hdr.TeamOffset = uint32(offset)
244 offset += len(params.TeamIdentifier) + 1
245 }
246 hdr.HashOffset = uint32(offset) + hdr.SpecialSlotCount*uint32(hdr.HashSize)
247 offset += len(specialSlots) + len(params.CodeSlots)
248 hdr.Length = uint32(offset)
249 // marshal
250 b := bytes.NewBuffer(make([]byte, 0, offset))
251 if err := binary.Write(b, binary.BigEndian, hdr); err != nil {
252 return codeDirResult{}, err
253 }
254 b.WriteString(params.SigningIdentity)
255 b.WriteByte(0)
256 if params.TeamIdentifier != "" {
257 b.WriteString(params.TeamIdentifier)
258 b.WriteByte(0)
259 }
260 b.Write(specialSlots)
261 b.Write(params.CodeSlots)
262 blob := b.Bytes()
263 // compute cd hash
264 h.Reset()
265 h.Write(blob)
266 return codeDirResult{
267 Raw: blob,
268 Digest: h.Sum(nil),
269 HashFunc: params.HashFunc,
270 }, nil
271 }