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