]> git.saurik.com Git - apple/libsecurity_codesigning.git/blob - lib/cs.h
d7957fc9e4994e0e4246a0c70619040d12773513
[apple/libsecurity_codesigning.git] / lib / cs.h
1 /*
2 * Copyright (c) 2006 Apple Computer, 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 <Security/CodeSigning.h>
32 #include <Security/SecCodeSigner.h>
33 #include <Security/SecBasePriv.h>
34 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
35 #include <security_utilities/globalizer.h>
36 #include <security_utilities/seccfobject.h>
37 #include <security_utilities/cfclass.h>
38 #include <security_utilities/errors.h>
39 #include <security_utilities/cfutilities.h>
40
41
42 namespace Security {
43 namespace CodeSigning {
44
45
46 //
47 // API per-thread globals
48 //
49 struct PerThread {
50 SecCSFlags flags; // flags of pending API call
51 };
52
53
54 //
55 // API globals
56 //
57 struct CFObjects {
58 CFObjects();
59 CFClass Code;
60 CFClass StaticCode;
61 CFClass Requirement;
62 CFClass CodeSigner;
63
64 ThreadNexus<PerThread> perThread;
65
66 SecCSFlags &flags() { return perThread().flags; }
67 };
68
69 extern ModuleNexus<CFObjects> gCFObjects;
70
71 static inline SecCSFlags apiFlags() { return gCFObjects().flags(); }
72
73
74 //
75 // Code Signing API brackets
76 //
77 #define BEGIN_CSAPI \
78 try {
79
80 #define END_CSAPI \
81 } \
82 catch (const UnixError &err) { \
83 switch (err.error) { \
84 case ENOEXEC: return errSecCSBadObjectFormat; \
85 default: return err.osStatus(); \
86 }} \
87 catch (const MacOSError &err) { return err.osStatus(); } \
88 catch (const CommonError &err) { return SecKeychainErrFromOSStatus(err.osStatus()); } \
89 catch (const std::bad_alloc &) { return memFullErr; } \
90 catch (...) { return internalComponentErr; } \
91 return noErr;
92
93 #define END_CSAPI_ERRORS \
94 } \
95 catch (const CSError &err) { return err.cfError(errors); } \
96 catch (const UnixError &err) { \
97 switch (err.error) { \
98 case ENOEXEC: return CSError::cfError(errors, errSecCSBadObjectFormat); \
99 default: return CSError::cfError(errors, err.osStatus()); \
100 }} \
101 catch (const MacOSError &err) { return CSError::cfError(errors, err.osStatus()); } \
102 catch (const CommonError &err) { return CSError::cfError(errors, SecKeychainErrFromOSStatus(err.osStatus())); } \
103 catch (const std::bad_alloc &) { return CSError::cfError(errors, memFullErr); } \
104 catch (...) { return CSError::cfError(errors, internalComponentErr); } \
105 return noErr;
106
107 #define END_CSAPI1(bad) } catch (...) { return bad; }
108
109
110 //
111 // A version of Required
112 //
113 template <class T>
114 static inline T &Required(T *ptr)
115 {
116 if (ptr == NULL)
117 MacOSError::throwMe(errSecCSObjectRequired);
118 return *ptr;
119 }
120
121 static inline void Required(const void *ptr)
122 {
123 if (ptr == NULL)
124 MacOSError::throwMe(errSecCSObjectRequired);
125 }
126
127
128 //
129 // Check flags against a validity mask
130 //
131 static inline void checkFlags(SecCSFlags flags, SecCSFlags acceptable = 0)
132 {
133 if (flags & ~acceptable)
134 MacOSError::throwMe(errSecCSInvalidFlags);
135 gCFObjects().flags() = flags;
136 }
137
138
139 } // CodeSigning
140 } // Security
141
142 #endif //_H_CS