]>
Commit | Line | Data |
---|---|---|
7d31e928 A |
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 | // diskrep - disk representations of code | |
26 | // | |
27 | #ifndef _H_DISKREP | |
28 | #define _H_DISKREP | |
29 | ||
30 | #include "cs.h" | |
31 | #include "codedirectory.h" | |
32 | #include "requirement.h" | |
33 | #include "macho++.h" // for class Architecture | |
34 | #include <security_utilities/refcount.h> | |
35 | #include <security_utilities/superblob.h> | |
36 | #include <CoreFoundation/CFData.h> | |
37 | ||
38 | namespace Security { | |
39 | namespace CodeSigning { | |
40 | ||
41 | ||
42 | // | |
43 | // DiskRep is an abstract interface to code somewhere located by | |
44 | // a file system path. It presents the ability to read and write | |
45 | // Code Signing-related information about such code without exposing | |
46 | // the details of the storage locations or formats. | |
47 | // | |
48 | class DiskRep : public RefCount { | |
49 | public: | |
50 | DiskRep(); | |
51 | virtual ~DiskRep(); | |
52 | virtual DiskRep *base(); | |
53 | virtual CFDataRef component(CodeDirectory::SpecialSlot slot) = 0; // fetch component | |
54 | virtual std::string mainExecutablePath() = 0; // path to main executable | |
55 | virtual CFURLRef canonicalPath() = 0; // path to whole code | |
56 | virtual std::string recommendedIdentifier() = 0; // default identifier | |
57 | virtual std::string resourcesRootPath(); // resource directory if any | |
58 | virtual CFDictionaryRef defaultResourceRules(); // default resource rules | |
59 | virtual const Requirements *defaultRequirements(const Architecture *arch); // default internal requirements | |
60 | virtual Universal *mainExecutableImage(); // binary if Mach-O/Universal | |
61 | virtual size_t pageSize(); // default main executable page size | |
62 | virtual size_t signingBase(); // start offset of signed area in main executable | |
63 | virtual size_t signingLimit() = 0; // size of signed area in main executable | |
64 | virtual std::string format() = 0; // human-readable type string | |
65 | virtual CFArrayRef modifiedFiles(); // list of files modified by signing | |
66 | virtual UnixPlusPlus::FileDesc &fd() = 0; // a cached fd for main executable file | |
67 | virtual void flush(); // flush caches (refetch as needed) | |
68 | ||
69 | bool mainExecutableIsMachO() { return mainExecutableImage() != NULL; } | |
70 | ||
71 | // shorthands | |
72 | CFDataRef codeDirectory() { return component(cdCodeDirectorySlot); } | |
73 | CFDataRef signature() { return component(cdSignatureSlot); } | |
74 | ||
75 | public: | |
76 | class Writer; | |
77 | virtual Writer *writer(); | |
78 | ||
79 | public: | |
80 | static DiskRep *bestGuess(const char *path); // canonical heuristic, any path | |
81 | static DiskRep *bestFileGuess(const char *path); // canonical heuristic, single file only | |
82 | ||
83 | static DiskRep *bestGuess(const std::string &path) { return bestGuess(path.c_str()); } | |
84 | static DiskRep *bestFileGuess(const std::string &path) { return bestFileGuess(path.c_str()); } | |
85 | ||
86 | ||
87 | public: | |
88 | static const size_t segmentedPageSize = 4096; // default page size for system-paged signatures | |
89 | static const size_t monolithicPageSize = 0; // default page size for non-Mach-O executables | |
90 | }; | |
91 | ||
92 | ||
93 | // | |
94 | // Write-access objects. | |
95 | // At this layer they are quite abstract, carrying just the functionality needed | |
96 | // for the signing machinery to place data wherever it should go. Each DiskRep subclass | |
97 | // that supports writing signing data to a place inside the code needs to implement | |
98 | // a subclass of Writer and return an instance in the DiskRep::writer() method when asked. | |
99 | // | |
100 | class DiskRep::Writer : public RefCount { | |
101 | public: | |
102 | Writer(uint32_t attrs = 0); | |
103 | virtual ~Writer(); | |
104 | virtual void component(CodeDirectory::SpecialSlot slot, CFDataRef data) = 0; | |
105 | virtual uint32_t attributes() const; | |
106 | virtual void flush(); | |
107 | ||
108 | bool attribute(uint32_t attr) const { return mAttributes & attr; } | |
109 | ||
110 | void signature(CFDataRef data) { component(cdSignatureSlot, data); } | |
111 | void codeDirectory(const CodeDirectory *cd) | |
112 | { component(cdCodeDirectorySlot, CFTempData(cd->data(), cd->length())); } | |
113 | ||
114 | private: | |
115 | Architecture mArch; | |
116 | uint32_t mAttributes; | |
117 | }; | |
118 | ||
119 | // | |
120 | // Writer attributes. Defaults should be off-bits. | |
121 | // | |
122 | enum { | |
123 | writerLastResort = 0x0001, // prefers not to store attributes itself | |
124 | writerNoGlobal = 0x0002, // has only per-architecture storage | |
125 | }; | |
126 | ||
127 | ||
128 | } // end namespace CodeSigning | |
129 | } // end namespace Security | |
130 | ||
131 | #endif // !_H_DISKREP |