2 * Copyright (c) 2006-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 #include "bundlediskrep.h"
24 #include <CoreFoundation/CFURLAccess.h>
25 #include <CoreFoundation/CFBundlePriv.h>
26 #include <security_utilities/cfmunge.h>
31 namespace CodeSigning
{
33 using namespace UnixPlusPlus
;
37 // We make a CFBundleRef immediately, but everything else is lazy
39 BundleDiskRep::BundleDiskRep(const char *path
, const Context
*ctx
)
40 : mBundle(_CFBundleCreateIfMightBeBundle(NULL
, CFTempURL(path
)))
43 MacOSError::throwMe(errSecCSBadObjectFormat
);
45 CODESIGN_DISKREP_CREATE_BUNDLE_PATH(this, (char*)path
, (void*)ctx
, mExecRep
);
48 BundleDiskRep::BundleDiskRep(CFBundleRef ref
, const Context
*ctx
)
50 mBundle
= ref
; // retains
52 CODESIGN_DISKREP_CREATE_BUNDLE_REF(this, ref
, (void*)ctx
, mExecRep
);
55 void BundleDiskRep::setup(const Context
*ctx
)
57 string version
= resourcesRootPath()
59 + ((ctx
&& ctx
->version
) ? ctx
->version
: "Current")
61 if (::access(version
.c_str(), F_OK
) == 0) { // versioned bundle
62 if (CFBundleRef versionBundle
= CFBundleCreate(NULL
, CFTempURL(version
)))
63 mBundle
.take(versionBundle
); // replace top bundle ref
65 MacOSError::throwMe(errSecCSStaticCodeNotFound
);
67 if (ctx
&& ctx
->version
) // explicitly specified
68 MacOSError::throwMe(errSecCSStaticCodeNotFound
);
70 mExecRep
= DiskRep::bestFileGuess(this->mainExecutablePath(), ctx
);
75 // Create a path to a bundle signing resource, by name.
76 // If the BUNDLEDISKREP_DIRECTORY directory exists in the bundle's support directory, files
77 // will be read and written there. Otherwise, they go directly into the support directory.
79 string
BundleDiskRep::metaPath(const char *name
)
81 if (mMetaPath
.empty()) {
82 string support
= cfString(CFBundleCopySupportFilesDirectoryURL(mBundle
), true);
83 mMetaPath
= support
+ "/" BUNDLEDISKREP_DIRECTORY
;
84 if (::access(mMetaPath
.c_str(), F_OK
) == 0) {
91 return mMetaPath
+ "/" + name
;
96 // Try to create the meta-file directory in our bundle.
97 // Does nothing if the directory already exists.
98 // Throws if an error occurs.
100 void BundleDiskRep::createMeta()
102 string meta
= metaPath(BUNDLEDISKREP_DIRECTORY
);
104 if (::mkdir(meta
.c_str(), 0755) == 0) {
105 copyfile(cfString(canonicalPath(), true).c_str(), meta
.c_str(), NULL
, COPYFILE_SECURITY
);
108 } else if (errno
!= EEXIST
)
109 UnixError::throwMe();
115 // Load and return a component, by slot number.
116 // Info.plist components come from the bundle, always (we don't look
117 // for Mach-O embedded versions).
118 // Everything else comes from the embedded blobs of a Mach-O image, or from
119 // files located in the Contents directory of the bundle.
121 CFDataRef
BundleDiskRep::component(CodeDirectory::SpecialSlot slot
)
124 // the Info.plist comes from the magic CFBundle-indicated place and ONLY from there
126 if (CFRef
<CFURLRef
> info
= _CFBundleCopyInfoPlistURL(mBundle
))
127 return cfLoadFile(info
);
130 // by default, we take components from the executable image or files
132 if (CFDataRef data
= mExecRep
->component(slot
))
135 // but the following always come from files
136 case cdResourceDirSlot
:
137 if (const char *name
= CodeDirectory::canonicalSlotName(slot
))
138 return metaData(name
);
146 // The binary identifier is taken directly from the main executable.
148 CFDataRef
BundleDiskRep::identification()
150 return mExecRep
->identification();
155 // Various aspects of our DiskRep personality.
157 CFURLRef
BundleDiskRep::canonicalPath()
159 return CFBundleCopyBundleURL(mBundle
);
162 string
BundleDiskRep::mainExecutablePath()
164 if (CFURLRef exec
= CFBundleCopyExecutableURL(mBundle
))
165 return cfString(exec
, true);
167 MacOSError::throwMe(errSecCSNoMainExecutable
);
170 string
BundleDiskRep::resourcesRootPath()
172 return cfString(CFBundleCopySupportFilesDirectoryURL(mBundle
), true);
175 void BundleDiskRep::adjustResources(ResourceBuilder
&builder
)
177 // exclude entire contents of meta directory
178 builder
.addExclusion("^" BUNDLEDISKREP_DIRECTORY
"/");
180 // exclude the main executable file
181 string resources
= resourcesRootPath();
182 string executable
= mainExecutablePath();
183 if (!executable
.compare(0, resources
.length(), resources
, 0, resources
.length())) // is prefix
184 builder
.addExclusion(string("^")
185 + ResourceBuilder::escapeRE(executable
.substr(resources
.length() + 1)) + "$");
190 Universal
*BundleDiskRep::mainExecutableImage()
192 return mExecRep
->mainExecutableImage();
195 size_t BundleDiskRep::signingBase()
197 return mExecRep
->signingBase();
200 size_t BundleDiskRep::signingLimit()
202 return mExecRep
->signingLimit();
205 string
BundleDiskRep::format()
207 return string("bundle with ") + mExecRep
->format();
210 CFArrayRef
BundleDiskRep::modifiedFiles()
212 CFMutableArrayRef files
= CFArrayCreateMutableCopy(NULL
, 0, mExecRep
->modifiedFiles());
213 checkModifiedFile(files
, cdCodeDirectorySlot
);
214 checkModifiedFile(files
, cdSignatureSlot
);
215 checkModifiedFile(files
, cdResourceDirSlot
);
216 checkModifiedFile(files
, cdEntitlementSlot
);
220 void BundleDiskRep::checkModifiedFile(CFMutableArrayRef files
, CodeDirectory::SpecialSlot slot
)
222 if (CFDataRef data
= mExecRep
->component(slot
)) // provided by executable file
224 else if (const char *resourceName
= CodeDirectory::canonicalSlotName(slot
)) {
225 string file
= metaPath(resourceName
);
226 if (::access(file
.c_str(), F_OK
) == 0)
227 CFArrayAppendValue(files
, CFTempURL(file
));
231 FileDesc
&BundleDiskRep::fd()
233 return mExecRep
->fd();
236 void BundleDiskRep::flush()
243 // Defaults for signing operations
245 string
BundleDiskRep::recommendedIdentifier(const SigningContext
&)
247 if (CFStringRef identifier
= CFBundleGetIdentifier(mBundle
))
248 return cfString(identifier
);
249 if (CFDictionaryRef infoDict
= CFBundleGetInfoDictionary(mBundle
))
250 if (CFStringRef identifier
= CFStringRef(CFDictionaryGetValue(infoDict
, kCFBundleNameKey
)))
251 return cfString(identifier
);
253 // fall back to using the canonical path
254 return canonicalIdentifier(cfString(this->canonicalPath()));
257 CFDictionaryRef
BundleDiskRep::defaultResourceRules(const SigningContext
&)
259 return cfmake
<CFDictionaryRef
>("{rules={"
260 "'^version.plist$' = #T"
262 "'^Resources/.*\\.lproj/' = {optional=#T, weight=1000}"
263 "'^Resources/.*\\.lproj/locversion.plist$' = {omit=#T, weight=1100}"
267 const Requirements
*BundleDiskRep::defaultRequirements(const Architecture
*arch
, const SigningContext
&ctx
)
269 return mExecRep
->defaultRequirements(arch
, ctx
);
272 size_t BundleDiskRep::pageSize(const SigningContext
&ctx
)
274 return mExecRep
->pageSize(ctx
);
281 DiskRep::Writer
*BundleDiskRep::writer()
283 return new Writer(this);
286 BundleDiskRep::Writer::Writer(BundleDiskRep
*r
)
287 : rep(r
), mMadeMetaDirectory(false)
289 execWriter
= rep
->mExecRep
->writer();
294 // Write a component.
295 // Note that this isn't concerned with Mach-O writing; this is handled at
296 // a much higher level. If we're called, we write to a file in the Bundle's meta directory.
298 void BundleDiskRep::Writer::component(CodeDirectory::SpecialSlot slot
, CFDataRef data
)
302 if (!execWriter
->attribute(writerLastResort
)) // willing to take the data...
303 return execWriter
->component(slot
, data
); // ... so hand it through
304 // execWriter doesn't want the data; store it as a resource file (below)
305 case cdResourceDirSlot
:
306 // the resource directory always goes into a bundle file
307 if (const char *name
= CodeDirectory::canonicalSlotName(slot
)) {
309 string path
= rep
->metaPath(name
);
310 AutoFileDesc
fd(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
311 fd
.writeAll(CFDataGetBytePtr(data
), CFDataGetLength(data
));
313 MacOSError::throwMe(errSecCSBadObjectFormat
);
319 // Remove all signature data
321 void BundleDiskRep::Writer::remove()
323 // remove signature from the executable
324 execWriter
->remove();
326 // remove signature files from bundle
327 for (CodeDirectory::SpecialSlot slot
= 0; slot
< cdSlotCount
; slot
++)
329 remove(cdSignatureSlot
);
332 void BundleDiskRep::Writer::remove(CodeDirectory::SpecialSlot slot
)
334 if (const char *name
= CodeDirectory::canonicalSlotName(slot
))
335 if (::unlink(rep
->metaPath(name
).c_str()))
337 case ENOENT
: // not found - that's okay
340 UnixError::throwMe();
345 void BundleDiskRep::Writer::flush()
351 } // end namespace CodeSigning
352 } // end namespace Security