2 * Copyright (c) 2006-2007,2011,2013-2014 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@
25 // diskrep - disk representations of code
29 #include <CoreFoundation/CFBundlePriv.h>
31 // specific disk representations created by the bestGuess() function
32 #include "filediskrep.h"
33 #include "bundlediskrep.h"
35 #include "diskimagerep.h"
39 namespace CodeSigning
{
41 using namespace UnixPlusPlus
;
53 CODESIGN_DISKREP_DESTROY(this);
58 // Normal DiskReps are their own base.
60 DiskRep
*DiskRep::base()
67 // By default, DiskReps are read-only.
69 DiskRep::Writer
*DiskRep::writer()
71 MacOSError::throwMe(errSecCSUnimplemented
);
75 void DiskRep::Writer::addDiscretionary(CodeDirectory::Builder
&)
82 // Given a file system path, come up with the most likely correct
83 // disk representation for what's there.
84 // This is, strictly speaking, a heuristic that could be fooled - there's
85 // no fool-proof rule for figuring this out. But we'd expect this to work
86 // fine in ordinary use. If you happen to know what you're looking at
87 // (say, a bundle), then just create the suitable subclass of DiskRep directly.
88 // That's quite legal.
89 // The optional context argument can provide additional information that guides the guess.
91 DiskRep
*DiskRep::bestGuess(const char *path
, const Context
*ctx
)
94 if (!(ctx
&& ctx
->fileOnly
)) {
96 if (::stat(path
, &st
))
99 // if it's a directory, assume it's a bundle
100 if ((st
.st_mode
& S_IFMT
) == S_IFDIR
) // directory - assume bundle
101 return new BundleDiskRep(path
, ctx
);
103 // see if it's the main executable of a recognized bundle
104 if (CFRef
<CFURLRef
> pathURL
= makeCFURL(path
))
105 if (CFRef
<CFBundleRef
> bundle
= _CFBundleCreateWithExecutableURLIfMightBeBundle(NULL
, pathURL
))
106 return new BundleDiskRep(bundle
, ctx
);
109 // try the various single-file representations
110 AutoFileDesc
fd(path
, O_RDONLY
);
111 if (MachORep::candidate(fd
))
112 return new MachORep(path
, ctx
);
113 if (DiskImageRep::candidate(fd
))
114 return new DiskImageRep(path
);
115 if (DYLDCacheRep::candidate(fd
))
116 return new DYLDCacheRep(path
);
118 // ultimate fallback - the generic file representation
119 return new FileDiskRep(path
);
121 } catch (const CommonError
&error
) {
122 switch (error
.unixError()) {
124 MacOSError::throwMe(errSecCSStaticCodeNotFound
);
132 DiskRep
*DiskRep::bestFileGuess(const char *path
, const Context
*ctx
)
137 dctx
.fileOnly
= true;
138 return bestGuess(path
, &dctx
);
143 // Given a main executable known to be a Mach-O binary, and an offset into
144 // the file of the actual architecture desired (of a Universal file),
145 // produce a suitable MachORep.
146 // This function does not consider non-MachO binaries. It does however handle
147 // bundles with Mach-O main executables correctly.
149 DiskRep
*DiskRep::bestGuess(const char *path
, size_t archOffset
)
152 // is it the main executable of a bundle?
153 if (CFRef
<CFURLRef
> pathURL
= makeCFURL(path
))
154 if (CFRef
<CFBundleRef
> bundle
= _CFBundleCreateWithExecutableURLIfMightBeBundle(NULL
, pathURL
)) {
155 Context ctx
; ctx
.offset
= archOffset
;
156 return new BundleDiskRep(bundle
, &ctx
); // ask bundle to make bundle-with-MachO-at-offset
158 // else, must be a Mach-O binary
159 Context ctx
; ctx
.offset
= archOffset
;
160 return new MachORep(path
, &ctx
);
161 } catch (const CommonError
&error
) {
162 switch (error
.unixError()) {
164 MacOSError::throwMe(errSecCSStaticCodeNotFound
);
173 // Default behaviors of DiskRep
175 string
DiskRep::resourcesRootPath()
177 return ""; // has no resources directory
180 void DiskRep::adjustResources(ResourceBuilder
&builder
)
185 void DiskRep::prepareForSigning(SigningContext
&state
)
190 Universal
*DiskRep::mainExecutableImage()
192 return NULL
; // no Mach-O executable
195 size_t DiskRep::signingBase()
197 return 0; // whole file (start at beginning)
200 CFArrayRef
DiskRep::modifiedFiles()
202 // by default, claim (just) the main executable modified
203 CFRef
<CFURLRef
> mainURL
= makeCFURL(mainExecutablePath());
204 return makeCFArray(1, mainURL
.get());
207 void DiskRep::flush()
212 CFDictionaryRef
DiskRep::diskRepInformation()
217 CFDictionaryRef
DiskRep::defaultResourceRules(const SigningContext
&)
222 const Requirements
*DiskRep::defaultRequirements(const Architecture
*, const SigningContext
&)
227 size_t DiskRep::pageSize(const SigningContext
&)
229 return monolithicPageSize
; // unpaged (monolithic)
233 void DiskRep::strictValidate(const CodeDirectory
*, const ToleratedErrors
& tolerated
, SecCSFlags flags
)
235 if (flags
& kSecCSRestrictToAppLike
)
236 if (tolerated
.find(errSecCSNotAppLike
) == tolerated
.end())
237 MacOSError::throwMe(errSecCSNotAppLike
);
240 CFArrayRef
DiskRep::allowedResourceOmissions()
247 // Given some string (usually a pathname), derive a suggested signing identifier
248 // in a canonical way (so there's some consistency).
250 // This is a heuristic. First we lop off any leading directories and final (non-numeric)
251 // extension. Then we walk backwards, eliminating numeric extensions except the first one.
252 // Thus, libfrotz7.3.5.dylib becomes libfrotz7, mumble.77.plugin becomes mumble.77,
253 // and rumble.rb becomes rumble. This isn't perfect, but it ought to handle 98%+ of
254 // the common varieties out there. Specify an explicit identifier for the oddballs.
256 // This is called by the various recommendedIdentifier() methods, who are
257 // free to modify or override it.
259 // Note: We use strchr("...") instead of is*() here because we do not
260 // wish to be influenced by locale settings.
262 std::string
DiskRep::canonicalIdentifier(const std::string
&name
)
267 // lop off any directory prefixes
268 if ((p
= s
.rfind('/')) != string::npos
)
271 // remove any final extension (last dot) unless it's numeric
272 if ((p
= s
.rfind('.')) != string::npos
&& !strchr("0123456789", s
[p
+1]))
275 // eat numeric suffixes except the first one; roughly:
276 // foo.2.3.4 => foo.2, foo2.3 => foo2, foo.9 => foo.9, foo => foo
277 if (strchr("0123456789.", s
[0])) // starts with digit or .
278 return s
; // ... so don't mess with it
280 // foo3.5^, foo.3.5^, foo3^, foo.3^, foo^
281 while (strchr("0123456789.", s
[p
]))
283 // fo^o3.5, fo^o.3.5, fo^o3, fo^o.3, fo^o
285 // foo^3.5, foo^.3.5, foo^3, foo^.3, foo^
288 // foo^3.5, foo.^3.5, foo^3, foo.^3, foo^
289 while (p
< s
.size() && strchr("0123456789", s
[p
]))
291 // foo3^.5, foo.3^.5, foo3^, foo.3^, foo^
292 return s
.substr(0, p
);
299 DiskRep::Writer::Writer(uint32_t attrs
)
300 : mArch(CPU_TYPE_ANY
), mAttributes(attrs
)
304 DiskRep::Writer::~Writer()
307 uint32_t DiskRep::Writer::attributes() const
308 { return mAttributes
; }
310 void DiskRep::Writer::flush()
313 void DiskRep::Writer::remove()
315 MacOSError::throwMe(errSecCSNotSupported
);
319 } // end namespace CodeSigning
320 } // end namespace Security