Debian Codegraph @

lib/fruit/csblob/verify.go

raw symbols SCIP
1 package csblob
2
3 import (
4 "crypto"
5 "crypto/hmac"
6 "errors"
7 "fmt"
8 "hash"
9 "io"
10
11 "github.com/sassoftware/relic/v7/lib/pkcs9"
12 )
13
14 type VerifiedBlob struct {
15 Blob *SigBlob
16 Signature *pkcs9.TimestampedSignature
17 HashFunc crypto.Hash
18 }
19
20 type VerifyParams struct {
21 InfoPlist []byte
22 Resources []byte
23 RepSpecific []byte
24 }
25
26 func Verify(blob []byte, params VerifyParams) (*VerifiedBlob, error) {
27 sig, err := parseSignature(blob)
28 if err != nil {
29 return nil, err
30 }
31 // verify hashes in each code directory
32 computedHashes := make(map[crypto.Hash][]byte)
33 var hashFunc crypto.Hash
34 for _, dir := range sig.Directories {
35 hashFunc = dir.HashFunc
36 h := dir.HashFunc.New()
37 h.Write(dir.Raw)
38 computedHashes[dir.HashFunc] = h.Sum(nil)
39 if dir.EntitlementsDERHash != nil {
40 if err := hashCheck(h, sig.EntitlementDER, dir.EntitlementsDERHash); err != nil {
41 return nil, fmt.Errorf("entitlementsDER: %w", err)
42 }
43 }
44 if dir.EntitlementsHash != nil {
45 if err := hashCheck(h, sig.Entitlement, dir.EntitlementsHash); err != nil {
46 return nil, fmt.Errorf("entitlements: %w", err)
47 }
48 }
49 if dir.RequirementsHash != nil {
50 if err := hashCheck(h, sig.RawRequirements, dir.RequirementsHash); err != nil {
51 return nil, fmt.Errorf("requirements: %w", err)
52 }
53 }
54 if dir.RepSpecificHash != nil {
55 if err := hashCheck(h, params.RepSpecific, dir.RepSpecificHash); err != nil {
56 return nil, fmt.Errorf("rep_specific: %w", err)
57 }
58 }
59 if dir.ManifestHash != nil && params.InfoPlist != nil {
60 if err := hashCheck(h, params.InfoPlist, dir.ManifestHash); err != nil {
61 return nil, fmt.Errorf("info_plist: %w", err)
62 }
63 }
64 if dir.ResourcesHash != nil && params.Resources != nil {
65 if err := hashCheck(h, params.Resources, dir.ResourcesHash); err != nil {
66 return nil, fmt.Errorf("resources: %w", err)
67 }
68 }
69 }
70 // verify CMS signature against the first code dir
71 mdContent := sig.Directories[0].Raw
72 if sig.CMS == nil {
73 return nil, errors.New("signature wrapper not found, possibly an adhoc signature")
74 }
75 pksig, err := sig.CMS.Content.Verify(mdContent, false)
76 if err != nil {
77 return nil, err
78 }
79 // verify all code dirs using a propretiary attribute if present
80 if err := checkCDHashes(pksig.SignerInfo, computedHashes); err != nil {
81 return nil, fmt.Errorf("verifying cd hashes: %w", err)
82 }
83 // verify using plist attribute if present -- unnecessary but useful for documentation
84 if err := checkPlistHashes(sig.Directories, pksig.SignerInfo, computedHashes); err != nil {
85 return nil, fmt.Errorf("verifying cd hashes: plist: %w", err)
86 }
87 // mark proprietary certificate extensions as handled so it doesn't fail the chain
88 for _, cert := range pksig.Intermediates {
89 MarkHandledExtensions(cert)
90 }
91 // validate timestamp token
92 ts, err := pkcs9.VerifyOptionalTimestamp(pksig)
93 if err != nil {
94 return nil, err
95 }
96 return &VerifiedBlob{
97 Blob: sig,
98 Signature: &ts,
99 HashFunc: hashFunc,
100 }, nil
101 }
102
103 func hashCheck(h hash.Hash, blob, expected []byte) error {
104 h.Reset()
105 h.Write(blob)
106 actual := h.Sum(nil)
107 if hmac.Equal(actual, expected) {
108 return nil
109 }
110 return fmt.Errorf("digest mismatch: expected %x but got %x", expected, actual)
111 }
112
113 func (s *SigBlob) bestDir() *CodeDirectory {
114 var dir *CodeDirectory
115 for _, dir2 := range s.Directories {
116 if dir == nil || dir2.Header.HashType > dir.Header.HashType {
117 dir = dir2
118 }
119 }
120 return dir
121 }
122
123 func (s *SigBlob) CodeSize() int64 {
124 dir := s.bestDir()
125 if dir == nil {
126 return 0
127 }
128 if dir.Header.CodeLimit64 != 0 {
129 return dir.Header.CodeLimit64
130 }
131 return int64(dir.Header.CodeLimit)
132 }
133
134 func (s *SigBlob) VerifyPages(r io.Reader) error {
135 dir := s.bestDir()
136 if dir == nil {
137 return errors.New("no valid code dir found")
138 }
139 remaining := s.CodeSize()
140 if dir.Header.PageSizeLog2 == 0 {
141 // single page for DMG bundles
142 if len(dir.CodeHashes) != 1 {
143 return fmt.Errorf("expected 1 hash slot but found %d", len(dir.CodeHashes))
144 }
145 h := dir.HashFunc.New()
146 n, err := io.Copy(h, r)
147 if err != nil {
148 return err
149 } else if n != remaining {
150 return fmt.Errorf("expected code size of %d but got %d", remaining, n)
151 }
152 computed := h.Sum(nil)
153 if !hmac.Equal(computed, dir.CodeHashes[0]) {
154 return fmt.Errorf("digest mismatch: expected %x, got %x", dir.CodeHashes[0], computed)
155 }
156 return nil
157 }
158 pageSize := int64(1 << dir.Header.PageSizeLog2)
159 page := make([]byte, pageSize)
160 h := dir.HashFunc.New()
161 for i, expected := range dir.CodeHashes {
162 if remaining <= 0 {
163 return errors.New("not enough hash slots to cover indicated size")
164 } else if remaining < pageSize {
165 page = page[:remaining]
166 }
167 if _, err := io.ReadFull(r, page); err != nil {
168 return err
169 }
170 h.Reset()
171 h.Write(page)
172 computed := h.Sum(nil)
173 if !hmac.Equal(computed, expected) {
174 return fmt.Errorf("digest mismatch: page %d: expected %x, got %x", i, expected, computed)
175 }
176 remaining -= int64(len(page))
177 }
178 return nil
179 }