]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_codesigning/lib/resources.h
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / libsecurity_codesigning / lib / resources.h
1 /*
2 * Copyright (c) 2006-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 //
25 // resource directory construction and verification
26 //
27 #ifndef _H_RSIGN
28 #define _H_RSIGN
29
30 #include "codedirectory.h"
31 #include <security_utilities/utilities.h>
32 #include <security_utilities/cfutilities.h>
33 #include <security_utilities/hashing.h>
34 #include "regex.h"
35 #include <CoreFoundation/CoreFoundation.h>
36 #include <vector>
37 #include <fts.h>
38
39 namespace Security {
40 namespace CodeSigning {
41
42
43 //
44 // The builder of ResourceDirectories.
45 //
46 // Note that this *is* a ResourceEnumerator, which can enumerate
47 // its source directory once (only).
48 //
49 class ResourceBuilder {
50 NOCOPY(ResourceBuilder)
51 public:
52 ResourceBuilder(const std::string &root, const std::string &relBase,
53 CFDictionaryRef rulesDict, bool strict, const MacOSErrorSet& toleratedErrors);
54 ~ResourceBuilder();
55
56 std::string root() const { return mRoot; }
57
58 enum {
59 optional = 0x01, // may be absent at runtime
60 omitted = 0x02, // do not seal even if present
61 nested = 0x04, // nested code (recursively signed)
62 exclusion = 0x10, // overriding exclusion (stop looking)
63 softTarget = 0x20, // valid symlink target even though omitted/excluded
64 };
65
66 typedef unsigned int Weight;
67
68 public:
69 class Rule : private regex_t {
70 public:
71 Rule(const std::string &pattern, Weight weight, uint32_t flags);
72 ~Rule();
73
74 bool match(const char *s) const;
75
76 const Weight weight;
77 const uint32_t flags;
78 std::string source;
79 };
80 void addRule(Rule *rule) { mRules.push_back(rule); }
81 void addExclusion(const std::string &pattern, uint32_t flags = 0) { mRules.insert(mRules.begin(), new Rule(pattern, 0, exclusion | flags)); }
82
83 static std::string escapeRE(const std::string &s);
84
85 typedef void (^Scanner)(FTSENT *ent, uint32_t flags, const std::string relpath, Rule *rule);
86 void scan(Scanner next);
87 bool includes(string path) const;
88 Rule *findRule(string path) const;
89
90 static CFDataRef hashFile(const char *path, CodeDirectory::HashAlgorithm type);
91 static CFMutableDictionaryRef hashFile(const char *path, CodeDirectory::HashAlgorithms types, bool strictCheck);
92
93 static std::string hashName(CodeDirectory::HashAlgorithm type);
94
95 CFDictionaryRef rules() const { return mRawRules; }
96
97 protected:
98 void addRule(CFTypeRef key, CFTypeRef value);
99
100 private:
101 std::string mRoot, mRelBase;
102 FTS *mFTS;
103 CFCopyRef<CFDictionaryRef> mRawRules;
104 typedef std::vector<Rule *> Rules;
105 Rules mRules;
106 bool mCheckUnreadable;
107 bool mCheckUnknownType;
108 };
109
110
111 //
112 // The "seal" on a single resource.
113 //
114 class ResourceSeal {
115 NOCOPY(ResourceSeal)
116 public:
117 ResourceSeal(CFTypeRef ref);
118
119 public:
120 const Hashing::Byte *hash(CodeDirectory::HashAlgorithm type) const;
121 bool nested() const { return mFlags & ResourceBuilder::nested; }
122 bool optional() const { return mFlags & ResourceBuilder::optional; }
123 CFDictionaryRef dict() const { return mDict; }
124 CFStringRef requirement() const { return mRequirement; }
125 CFStringRef link() const { return mLink; }
126
127 private:
128 CFRef<CFDictionaryRef> mDict;
129 CFStringRef mRequirement;
130 CFStringRef mLink;
131 uint32_t mFlags;
132 };
133
134
135 } // end namespace CodeSigning
136 } // end namespace Security
137
138 #endif // !_H_RSIGN