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 25#include <security_utilities/refcount.h> 26#include <security_utilities/cfutilities.h> 27#include <Security/CodeSigning.h> 28#include <limits.h> 29#include <string> 30#include <vector> 31#include <CoreFoundation/CFBundle.h> 32 33 34namespace Security { 35 36 37// 38// A Signable with OS X support calls added 39// 40class OSXCode : public RefCount { 41public: 42 virtual ~OSXCode() { } 43 44public: 45 // creating OSXCode objects 46 static RefPointer<OSXCode> main(); 47 static RefPointer<OSXCode> at(const char *path); 48 static RefPointer<OSXCode> at(const std::string &path) { return at(path.c_str()); } 49 50public: 51 virtual string canonicalPath() const = 0; // reverse of at() 52 virtual string executablePath() const = 0; // path to main executable file 53 virtual SecStaticCodeRef codeRef() const; // defaults to code at canonicalPath() 54 55protected: 56 OSXCode() { } // nonpublic 57}; 58 59 60// 61// A simple executable tool. 62// 63class ExecutableTool : public OSXCode { 64public: 65 ExecutableTool(const char *path) : mPath(path) { } 66 67 string path() const { return mPath; } 68 string canonicalPath() const; 69 string executablePath() const; 70 71private: 72 string mPath; // UTF8 pathname to executable 73}; 74 75 76// 77// A generic bundle 78// 79class Bundle : public OSXCode { 80public: 81 Bundle(const char *path, const char *execPath = NULL); // from root and optional exec path 82 Bundle(CFBundleRef bundle, const char *root = NULL); // from existing CFBundleRef 83 ~Bundle(); 84 85 string canonicalPath() const; 86 string path() const { return mPath; } 87 string executablePath() const; 88 string identifier() const { return cfString(CFBundleGetIdentifier(cfBundle())); } 89 CFTypeRef infoPlistItem(const char *name) const; // not retained 90 91 string resourcePath() const { return cfStringRelease(CFBundleCopyResourcesDirectoryURL(cfBundle())); } 92 string resource(const char *name, const char *type, const char *subdir = NULL); 93 void resources(vector<string> &paths, const char *type, const char *subdir = NULL); 94 95 virtual void *lookupSymbol(const char *name); 96 97protected: 98 CFBundleRef cfBundle() const; 99 100protected: 101 string mPath; // UTF8 path to bundle directory 102 mutable string mExecutablePath; // cached or determined path to main executable 103 mutable CFBundleRef mBundle; // CF-style bundle object (lazily built) 104}; 105 106 107class LoadableBundle : public Bundle { 108public: 109 LoadableBundle(const char *pathname) : Bundle(pathname) { } 110 LoadableBundle(CFBundleRef bundle) : Bundle(bundle) { } 111 112 virtual bool isLoaded() const; 113 virtual void load(); 114 virtual void unload(); 115}; 116 117 118class OSXCodeWrap : public OSXCode { 119public: 120 OSXCodeWrap(SecStaticCodeRef code) : mCode(code) { } 121 122 string encode() const; 123 124 string canonicalPath() const; 125 string executablePath() const; 126 SecStaticCodeRef codeRef() const; 127 128private: 129 CFCopyRef<SecStaticCodeRef> mCode; 130}; 131 132 133} // end namespace Security 134 135 136#endif //_H_OSXCODE 137