]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_codesigning/lib/cs.h
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / libsecurity_codesigning / lib / cs.h
1 /*
2 * Copyright (c) 2006,2011,2013-2014 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 // cs.h - code signing core header
26 //
27 #ifndef _H_CS
28 #define _H_CS
29
30 #include "cserror.h"
31 #include "codesigning_dtrace.h"
32 #include <Security/CSCommonPriv.h>
33 #include <Security/SecCodePriv.h>
34 #include <Security/SecStaticCodePriv.h>
35 #include <Security/SecRequirementPriv.h>
36 #include <Security/SecCodeSigner.h>
37 #include <Security/SecBasePriv.h>
38 #include <security_utilities/globalizer.h>
39 #include <security_utilities/seccfobject.h>
40 #include <security_utilities/cfclass.h>
41 #include <security_utilities/errors.h>
42 #include <security_utilities/sqlite++.h>
43 #include <security_utilities/cfutilities.h>
44 #include <security_utilities/logging.h>
45
46
47 namespace Security {
48 namespace CodeSigning {
49
50
51 //
52 // API per-thread globals
53 //
54 struct PerThread {
55 SecCSFlags flags; // flags of pending API call
56 };
57
58
59 //
60 // API globals
61 //
62 struct CFObjects {
63 CFObjects();
64 CFClass Code;
65 CFClass StaticCode;
66 CFClass Requirement;
67 CFClass CodeSigner;
68
69 ThreadNexus<PerThread> perThread;
70
71 SecCSFlags &flags() { return perThread().flags; }
72 };
73
74 extern ModuleNexus<CFObjects> gCFObjects;
75
76 OSStatus dbError(const SQLite3::Error &err);
77
78
79 //
80 // Code Signing API brackets
81 //
82 #define BEGIN_CSAPI \
83 try {
84
85 #define END_CSAPI \
86 } \
87 catch (const UnixError &err) { \
88 switch (err.error) { \
89 case ENOEXEC: return errSecCSBadObjectFormat; \
90 default: return err.osStatus(); \
91 }} \
92 catch (const MacOSError &err) { return err.osStatus(); } \
93 catch (const SQLite3::Error &err) { return dbError(err); } \
94 catch (const CommonError &err) { return SecKeychainErrFromOSStatus(err.osStatus()); } \
95 catch (const std::bad_alloc &) { return errSecAllocate; } \
96 catch (...) { Syslog::notice("unknown exception in CSAPI"); return errSecCSInternalError; } \
97 return errSecSuccess;
98
99 #define END_CSAPI_ERRORS \
100 } \
101 catch (const CSError &err) { return err.cfError(errors); } \
102 catch (const UnixError &err) { \
103 switch (err.error) { \
104 case ENOEXEC: return CSError::cfError(errors, errSecCSBadObjectFormat); \
105 default: return CSError::cfError(errors, err.osStatus()); \
106 }} \
107 catch (const MacOSError &err) { return CSError::cfError(errors, err.osStatus()); } \
108 catch (const SQLite3::Error &err) { return CSError::cfError(errors, dbError(err)); } \
109 catch (const CommonError &err) { return CSError::cfError(errors, SecKeychainErrFromOSStatus(err.osStatus())); } \
110 catch (const std::bad_alloc &) { return CSError::cfError(errors, errSecAllocate); } \
111 catch (...) { Syslog::notice("unknown exception in CSAPI"); return CSError::cfError(errors, errSecCSInternalError); } \
112 return errSecSuccess;
113
114 #define END_CSAPI1(bad) } catch (...) { return bad; }
115
116
117 #define END_CSAPI_ERRORS1(bad) \
118 } \
119 catch (const CSError &err) { err.cfError(errors); } \
120 catch (const UnixError &err) { \
121 switch (err.error) { \
122 case ENOEXEC: CSError::cfError(errors, errSecCSBadObjectFormat); \
123 default: CSError::cfError(errors, err.osStatus()); \
124 }} \
125 catch (const MacOSError &err) { CSError::cfError(errors, err.osStatus()); } \
126 catch (const SQLite3::Error &err) { CSError::cfError(errors, dbError(err)); } \
127 catch (const CommonError &err) { CSError::cfError(errors, SecKeychainErrFromOSStatus(err.osStatus())); } \
128 catch (const std::bad_alloc &) { CSError::cfError(errors, errSecAllocate); } \
129 catch (...) { Syslog::notice("unknown exception in CSAPI"); CSError::cfError(errors, errSecCSInternalError); } \
130 return bad;
131
132
133 //
134 // A version of CodeSigning::Required
135 //
136 template <class T>
137 static inline T &Required(T *ptr)
138 {
139 if (ptr == NULL)
140 MacOSError::throwMe(errSecCSObjectRequired);
141 return *ptr;
142 }
143
144 static inline void Required(const void *ptr)
145 {
146 if (ptr == NULL)
147 MacOSError::throwMe(errSecCSObjectRequired);
148 }
149
150
151 //
152 // Check flags against a validity mask
153 //
154 static inline void checkFlags(SecCSFlags flags, SecCSFlags acceptable = 0)
155 {
156 if (flags & ~acceptable)
157 MacOSError::throwMe(errSecCSInvalidFlags);
158 }
159
160
161 //
162 // DTrace USDT function bracket.
163 // Use like this:
164 // DTRACK(PROVIDER_PROBE_PREFIX, arguments-after-this);
165 // which will call
166 // PROVIDER_PROBE_PREFIX_START(this, arguments-after-this)
167 // and
168 // PROVIDER_PROBE_PREFIX_END(this)
169 //
170 #define DTRACK(_prefix, _obj, _args...) \
171 if (_prefix ## _START_ENABLED()) _prefix ## _START((_obj), ## _args); \
172 struct _DTFrame ## _prefix { void *me; \
173 _DTFrame ## _prefix(void *m) : me(m) { } \
174 ~_DTFrame ## _prefix() { _prefix ## _END(me); } \
175 } _dtframe##_prefix((_obj));
176
177
178 } // CodeSigning
179 } // Security
180
181 #endif //_H_CS