]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/osxcode.h
Security-59306.101.1.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / osxcode.h
1 /*
2 * Copyright (c) 2000-2001,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // osxcode - MacOS X's standard code objects
21 //
22 #ifndef _H_OSXCODE
23 #define _H_OSXCODE
24 #include <TargetConditionals.h>
25
26
27 #include <security_utilities/refcount.h>
28 #include <security_utilities/cfutilities.h>
29 #include <Security/CodeSigning.h>
30 #include <limits.h>
31 #include <string>
32 #include <vector>
33 #include <CoreFoundation/CFBundle.h>
34
35
36 namespace Security {
37
38
39 //
40 // A Signable with OS X support calls added
41 //
42 class OSXCode : public RefCount {
43 public:
44 virtual ~OSXCode() { }
45
46 public:
47 // creating OSXCode objects
48 static RefPointer<OSXCode> main();
49 static RefPointer<OSXCode> at(const char *path);
50 static RefPointer<OSXCode> at(const std::string &path) { return at(path.c_str()); }
51
52 public:
53 virtual string canonicalPath() const = 0; // reverse of at()
54 virtual string executablePath() const = 0; // path to main executable file
55 virtual SecStaticCodeRef codeRef() const; // defaults to code at canonicalPath()
56
57 protected:
58 OSXCode() { } // nonpublic
59 };
60
61
62 //
63 // A simple executable tool.
64 //
65 class ExecutableTool : public OSXCode {
66 public:
67 ExecutableTool(const char *path) : mPath(path) { }
68
69 string path() const { return mPath; }
70 string canonicalPath() const;
71 string executablePath() const;
72
73 private:
74 string mPath; // UTF8 pathname to executable
75 };
76
77
78 //
79 // A generic bundle
80 //
81 class Bundle : public OSXCode {
82 public:
83 Bundle(const char *path, const char *execPath = NULL); // from root and optional exec path
84 Bundle(CFBundleRef bundle, const char *root = NULL); // from existing CFBundleRef
85 ~Bundle();
86
87 string canonicalPath() const;
88 string path() const { return mPath; }
89 string executablePath() const;
90 string identifier() const { return cfString(CFBundleGetIdentifier(cfBundle())); }
91 CFTypeRef infoPlistItem(const char *name) const; // not retained
92
93 string resourcePath() const { return cfStringRelease(CFBundleCopyResourcesDirectoryURL(cfBundle())); }
94 string resource(const char *name, const char *type, const char *subdir = NULL);
95 void resources(vector<string> &paths, const char *type, const char *subdir = NULL);
96
97 virtual void *lookupSymbol(const char *name);
98
99 protected:
100 CFBundleRef cfBundle() const;
101
102 protected:
103 string mPath; // UTF8 path to bundle directory
104 mutable string mExecutablePath; // cached or determined path to main executable
105 mutable CFBundleRef mBundle; // CF-style bundle object (lazily built)
106 };
107
108
109 class LoadableBundle : public Bundle {
110 public:
111 LoadableBundle(const char *pathname) : Bundle(pathname) { }
112 LoadableBundle(CFBundleRef bundle) : Bundle(bundle) { }
113
114 virtual bool isLoaded() const;
115 virtual void load();
116 virtual void unload();
117 };
118
119
120 class OSXCodeWrap : public OSXCode {
121 public:
122 OSXCodeWrap(SecStaticCodeRef code) : mCode(code) { }
123
124 string encode() const;
125
126 string canonicalPath() const;
127 string executablePath() const;
128 SecStaticCodeRef codeRef() const;
129
130 private:
131 CFCopyRef<SecStaticCodeRef> mCode;
132 };
133
134
135 } // end namespace Security
136
137
138 #endif //_H_OSXCODE