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"
36 #include "diskimagerep.h"
40 namespace CodeSigning
{
42 using namespace UnixPlusPlus
;
54 CODESIGN_DISKREP_DESTROY(this);
59 // Normal DiskReps are their own base.
61 DiskRep
*DiskRep::base()
68 // By default, DiskReps are read-only.
70 DiskRep::Writer
*DiskRep::writer()
72 MacOSError::throwMe(errSecCSUnimplemented
);
76 void DiskRep::Writer::addDiscretionary(CodeDirectory::Builder
&)
83 // Given a file system path, come up with the most likely correct
84 // disk representation for what's there.
85 // This is, strictly speaking, a heuristic that could be fooled - there's
86 // no fool-proof rule for figuring this out. But we'd expect this to work
87 // fine in ordinary use. If you happen to know what you're looking at
88 // (say, a bundle), then just create the suitable subclass of DiskRep directly.
89 // That's quite legal.
90 // The optional context argument can provide additional information that guides the guess.
92 DiskRep
*DiskRep::bestGuess(const char *path
, const Context
*ctx
)
95 if (!(ctx
&& ctx
->fileOnly
)) {
97 if (::stat(path
, &st
))
100 // if it's a directory, assume it's a bundle
101 if ((st
.st_mode
& S_IFMT
) == S_IFDIR
) // directory - assume bundle
102 return new BundleDiskRep(path
, ctx
);
104 // see if it's the main executable of a recognized bundle
105 if (CFRef
<CFURLRef
> pathURL
= makeCFURL(path
))
106 if (CFRef
<CFBundleRef
> bundle
= _CFBundleCreateWithExecutableURLIfMightBeBundle(NULL
, pathURL
))
107 return new BundleDiskRep(bundle
, ctx
);
110 // try the various single-file representations
111 AutoFileDesc
fd(path
, O_RDONLY
);
112 if (MachORep::candidate(fd
))
113 return new MachORep(path
, ctx
);
115 if (DiskImageRep::candidate(fd
))
116 return new DiskImageRep(path
);
118 if (DYLDCacheRep::candidate(fd
))
119 return new DYLDCacheRep(path
);
121 // ultimate fallback - the generic file representation
122 return new FileDiskRep(path
);
124 } catch (const CommonError
&error
) {
125 switch (error
.unixError()) {
127 MacOSError::throwMe(errSecCSStaticCodeNotFound
);
135 DiskRep
*DiskRep::bestFileGuess(const char *path
, const Context
*ctx
)
140 dctx
.fileOnly
= true;
141 return bestGuess(path
, &dctx
);
146 // Given a main executable known to be a Mach-O binary, and an offset into
147 // the file of the actual architecture desired (of a Universal file),
148 // produce a suitable MachORep.
149 // This function does not consider non-MachO binaries. It does however handle
150 // bundles with Mach-O main executables correctly.
152 DiskRep
*DiskRep::bestGuess(const char *path
, size_t archOffset
)
155 // is it the main executable of a bundle?
156 if (CFRef
<CFURLRef
> pathURL
= makeCFURL(path
))
157 if (CFRef
<CFBundleRef
> bundle
= _CFBundleCreateWithExecutableURLIfMightBeBundle(NULL
, pathURL
)) {
158 Context ctx
; ctx
.offset
= archOffset
;
159 return new BundleDiskRep(bundle
, &ctx
); // ask bundle to make bundle-with-MachO-at-offset
161 // else, must be a Mach-O binary
162 Context ctx
; ctx
.offset
= archOffset
;
163 return new MachORep(path
, &ctx
);
164 } catch (const CommonError
&error
) {
165 switch (error
.unixError()) {
167 MacOSError::throwMe(errSecCSStaticCodeNotFound
);
176 // Default behaviors of DiskRep
178 string
DiskRep::resourcesRootPath()
180 return ""; // has no resources directory
183 void DiskRep::adjustResources(ResourceBuilder
&builder
)
188 void DiskRep::prepareForSigning(SigningContext
&state
)
193 Universal
*DiskRep::mainExecutableImage()
195 return NULL
; // no Mach-O executable
198 size_t DiskRep::signingBase()
200 return 0; // whole file (start at beginning)
203 size_t DiskRep::execSegBase(const Architecture
*)
205 return 0; // whole file (start at beginning)
208 CFArrayRef
DiskRep::modifiedFiles()
210 // by default, claim (just) the main executable modified
211 CFRef
<CFURLRef
> mainURL
= makeCFURL(mainExecutablePath());
212 return makeCFArray(1, mainURL
.get());
215 void DiskRep::flush()
220 CFDictionaryRef
DiskRep::diskRepInformation()
225 CFDictionaryRef
DiskRep::defaultResourceRules(const SigningContext
&)
230 const Requirements
*DiskRep::defaultRequirements(const Architecture
*, const SigningContext
&)
235 size_t DiskRep::pageSize(const SigningContext
&)
237 return monolithicPageSize
; // unpaged (monolithic)
241 void DiskRep::strictValidate(const CodeDirectory
*, const ToleratedErrors
& tolerated
, SecCSFlags flags
)
243 if (flags
& kSecCSRestrictToAppLike
)
244 if (tolerated
.find(errSecCSNotAppLike
) == tolerated
.end())
245 MacOSError::throwMe(errSecCSNotAppLike
);
248 CFArrayRef
DiskRep::allowedResourceOmissions()
255 // Given some string (usually a pathname), derive a suggested signing identifier
256 // in a canonical way (so there's some consistency).
258 // This is a heuristic. First we lop off any leading directories and final (non-numeric)
259 // extension. Then we walk backwards, eliminating numeric extensions except the first one.
260 // Thus, libfrotz7.3.5.dylib becomes libfrotz7, mumble.77.plugin becomes mumble.77,
261 // and rumble.rb becomes rumble. This isn't perfect, but it ought to handle 98%+ of
262 // the common varieties out there. Specify an explicit identifier for the oddballs.
264 // This is called by the various recommendedIdentifier() methods, who are
265 // free to modify or override it.
267 // Note: We use strchr("...") instead of is*() here because we do not
268 // wish to be influenced by locale settings.
270 std::string
DiskRep::canonicalIdentifier(const std::string
&name
)
275 // lop off any directory prefixes
276 if ((p
= s
.rfind('/')) != string::npos
)
279 // remove any final extension (last dot) unless it's numeric
280 if ((p
= s
.rfind('.')) != string::npos
&& !strchr("0123456789", s
[p
+1]))
283 // eat numeric suffixes except the first one; roughly:
284 // foo.2.3.4 => foo.2, foo2.3 => foo2, foo.9 => foo.9, foo => foo
285 if (strchr("0123456789.", s
[0])) // starts with digit or .
286 return s
; // ... so don't mess with it
288 // foo3.5^, foo.3.5^, foo3^, foo.3^, foo^
289 while (strchr("0123456789.", s
[p
]))
291 // fo^o3.5, fo^o.3.5, fo^o3, fo^o.3, fo^o
293 // foo^3.5, foo^.3.5, foo^3, foo^.3, foo^
296 // foo^3.5, foo.^3.5, foo^3, foo.^3, foo^
297 while (p
< s
.size() && strchr("0123456789", s
[p
]))
299 // foo3^.5, foo.3^.5, foo3^, foo.3^, foo^
300 return s
.substr(0, p
);
307 DiskRep::Writer::Writer(uint32_t attrs
)
308 : mArch(CPU_TYPE_ANY
), mAttributes(attrs
)
312 DiskRep::Writer::~Writer()
315 uint32_t DiskRep::Writer::attributes() const
316 { return mAttributes
; }
318 void DiskRep::Writer::flush()
321 void DiskRep::Writer::remove()
323 MacOSError::throwMe(errSecCSNotSupported
);
327 } // end namespace CodeSigning
328 } // end namespace Security