]> git.saurik.com Git - apple/security.git/blob - libsecurity_codesigning/lib/SecStaticCode.cpp
f0a67ccccc0894367b6ef817107a8f25054bdf31
[apple/security.git] / libsecurity_codesigning / lib / SecStaticCode.cpp
1 /*
2 * Copyright (c) 2006-2007 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 // SecStaticCode - API frame for SecStaticCode objects
26 //
27 #include "cs.h"
28 #include "StaticCode.h"
29 #include <security_utilities/cfmunge.h>
30 #include <fcntl.h>
31 #include <dirent.h>
32
33 using namespace CodeSigning;
34
35
36 //
37 // CF-standard type code function
38 //
39 CFTypeID SecStaticCodeGetTypeID(void)
40 {
41 BEGIN_CSAPI
42 return gCFObjects().StaticCode.typeID;
43 END_CSAPI1(_kCFRuntimeNotATypeID)
44 }
45
46
47 //
48 // Create an StaticCode directly from disk path.
49 //
50 OSStatus SecStaticCodeCreateWithPath(CFURLRef path, SecCSFlags flags, SecStaticCodeRef *staticCodeRef)
51 {
52 BEGIN_CSAPI
53
54 checkFlags(flags);
55 CodeSigning::Required(staticCodeRef) = (new SecStaticCode(DiskRep::bestGuess(cfString(path).c_str())))->handle();
56
57 END_CSAPI
58 }
59
60 const CFStringRef kSecCodeAttributeArchitecture = CFSTR("architecture");
61 const CFStringRef kSecCodeAttributeSubarchitecture =CFSTR("subarchitecture");
62 const CFStringRef kSecCodeAttributeBundleVersion = CFSTR("bundleversion");
63 const CFStringRef kSecCodeAttributeUniversalFileOffset = CFSTR("UniversalFileOffset");
64
65 OSStatus SecStaticCodeCreateWithPathAndAttributes(CFURLRef path, SecCSFlags flags, CFDictionaryRef attributes,
66 SecStaticCodeRef *staticCodeRef)
67 {
68 BEGIN_CSAPI
69
70 checkFlags(flags);
71 DiskRep::Context ctx;
72 std::string version; // holds memory placed into ctx
73 if (attributes) {
74 std::string archName;
75 int archNumber, subarchNumber, offset;
76 if (cfscan(attributes, "{%O=%d}", kSecCodeAttributeUniversalFileOffset, &offset)) {
77 ctx.offset = offset;
78 } else if (cfscan(attributes, "{%O=%s}", kSecCodeAttributeArchitecture, &archName)) {
79 ctx.arch = Architecture(archName.c_str());
80 } else if (cfscan(attributes, "{%O=%d,%O=%d}",
81 kSecCodeAttributeArchitecture, &archNumber, kSecCodeAttributeSubarchitecture, &subarchNumber))
82 ctx.arch = Architecture(archNumber, subarchNumber);
83 else if (cfscan(attributes, "{%O=%d}", kSecCodeAttributeArchitecture, &archNumber))
84 ctx.arch = Architecture(archNumber);
85 if (cfscan(attributes, "{%O=%s}", kSecCodeAttributeBundleVersion, &version))
86 ctx.version = version.c_str();
87 }
88
89 CodeSigning::Required(staticCodeRef) = (new SecStaticCode(DiskRep::bestGuess(cfString(path).c_str(), &ctx)))->handle();
90
91 END_CSAPI
92 }
93
94
95 //
96 // Check static validity of a StaticCode
97 //
98 OSStatus SecStaticCodeCheckValidity(SecStaticCodeRef staticCodeRef, SecCSFlags flags,
99 SecRequirementRef requirementRef)
100 {
101 return SecStaticCodeCheckValidityWithErrors(staticCodeRef, flags, requirementRef, NULL);
102 }
103
104 OSStatus SecStaticCodeCheckValidityWithErrors(SecStaticCodeRef staticCodeRef, SecCSFlags flags,
105 SecRequirementRef requirementRef, CFErrorRef *errors)
106 {
107 BEGIN_CSAPI
108
109 checkFlags(flags,
110 kSecCSCheckAllArchitectures
111 | kSecCSDoNotValidateExecutable
112 | kSecCSDoNotValidateResources
113 | kSecCSConsiderExpiration
114 | kSecCSEnforceRevocationChecks
115 | kSecCSCheckNestedCode);
116
117 SecPointer<SecStaticCode> code = SecStaticCode::requiredStatic(staticCodeRef);
118 const SecRequirement *req = SecRequirement::optional(requirementRef);
119 DTRACK(CODESIGN_EVAL_STATIC, code, (char*)code->mainExecutablePath().c_str());
120 code->staticValidate(flags, req);
121
122 END_CSAPI_ERRORS
123 }
124
125
126 //
127 // ====================================================================================
128 //
129 // The following API functions are called SecCode* but accept both SecCodeRef and
130 // SecStaticCodeRef arguments, operating on the implied SecStaticCodeRef as appropriate.
131 // Hence they're here, rather than in SecCode.cpp.
132 //
133
134
135 //
136 // Retrieve location information for an StaticCode.
137 //
138 OSStatus SecCodeCopyPath(SecStaticCodeRef staticCodeRef, SecCSFlags flags, CFURLRef *path)
139 {
140 BEGIN_CSAPI
141
142 checkFlags(flags);
143 SecPointer<SecStaticCode> staticCode = SecStaticCode::requiredStatic(staticCodeRef);
144 CodeSigning::Required(path) = staticCode->canonicalPath();
145
146 END_CSAPI
147 }
148
149
150 //
151 // Fetch or make up a designated requirement
152 //
153 OSStatus SecCodeCopyDesignatedRequirement(SecStaticCodeRef staticCodeRef, SecCSFlags flags,
154 SecRequirementRef *requirementRef)
155 {
156 BEGIN_CSAPI
157
158 checkFlags(flags);
159 const Requirement *req =
160 SecStaticCode::requiredStatic(staticCodeRef)->designatedRequirement();
161 CodeSigning::Required(requirementRef) = (new SecRequirement(req))->handle();
162
163 END_CSAPI
164 }
165
166
167 //
168 // Fetch a particular internal requirement, if present
169 //
170 OSStatus SecCodeCopyInternalRequirement(SecStaticCodeRef staticCodeRef, SecRequirementType type,
171 SecCSFlags flags, SecRequirementRef *requirementRef)
172 {
173 BEGIN_CSAPI
174
175 checkFlags(flags);
176 const Requirement *req =
177 SecStaticCode::requiredStatic(staticCodeRef)->internalRequirement(type);
178 CodeSigning::Required(requirementRef) = req ? (new SecRequirement(req))->handle() : NULL;
179
180 END_CSAPI
181 }
182
183
184 //
185 // Record for future use a detached code signature.
186 //
187 OSStatus SecCodeSetDetachedSignature(SecStaticCodeRef codeRef, CFDataRef signature,
188 SecCSFlags flags)
189 {
190 BEGIN_CSAPI
191
192 checkFlags(flags);
193 SecPointer<SecStaticCode> code = SecStaticCode::requiredStatic(codeRef);
194
195 code->detachedSignature(signature); // ... and pass it to the code
196 code->resetValidity();
197
198 END_CSAPI
199 }
200
201
202 //
203 // Attach a code signature to a kernel memory mapping for page-in validation.
204 //
205 OSStatus SecCodeMapMemory(SecStaticCodeRef codeRef, SecCSFlags flags)
206 {
207 BEGIN_CSAPI
208
209 checkFlags(flags);
210 SecPointer<SecStaticCode> code = SecStaticCode::requiredStatic(codeRef);
211 if (const CodeDirectory *cd = code->codeDirectory(false)) {
212 fsignatures args = { code->diskRep()->signingBase(), (void *)cd, cd->length() };
213 UnixError::check(::fcntl(code->diskRep()->fd(), F_ADDSIGS, &args));
214 } else
215 MacOSError::throwMe(errSecCSUnsigned);
216
217 END_CSAPI
218 }
219
220
221 //
222 // Attach a callback block to a code object
223 //
224 OSStatus SecStaticCodeSetCallback(SecStaticCodeRef codeRef, SecCSFlags flags, SecCodeCallback *old, SecCodeCallback monitor)
225 {
226 BEGIN_CSAPI
227
228 checkFlags(flags);
229 SecStaticCode *code = SecStaticCode::requiredStatic(codeRef);
230 if (old)
231 *old = code->monitor();
232 code->setMonitor(monitor);
233
234 END_CSAPI
235 }