]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/errors.cpp
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / errors.cpp
1 /*
2 * Copyright (c) 2000-2004,2011-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 //
26 // Error hierarchy
27 //
28 #include <security_utilities/errors.h>
29 #include <security_utilities/debugging.h>
30 #include <security_utilities/utility_config.h>
31 #include <security_utilities/debugging_internal.h>
32 #include <typeinfo>
33 #include <stdio.h>
34 #include <Security/SecBase.h>
35 #include <Security/CSCommon.h>
36 #include <execinfo.h>
37 #include <cxxabi.h>
38
39 //@@@
40 // From cssmapple.h - layering break
41 // Where should this go?
42 //@@@
43 #define errSecErrnoBase 100000
44 #define errSecErrnoLimit 100255
45
46 //
47 // The base of the exception hierarchy.
48 //
49 CommonError::CommonError() : whatBuffer("CommonError")
50 {
51 }
52
53
54 //
55 // We strongly encourage catching all exceptions by const reference, so the copy
56 // constructor of our exceptions should never be called.
57 //
58 CommonError::CommonError(const CommonError &source)
59 {
60 strlcpy(whatBuffer, source.whatBuffer, whatBufferSize);
61 }
62
63 CommonError::~CommonError() _NOEXCEPT
64 {
65 }
66
67 void CommonError::LogBacktrace() {
68 // Only do this work if we're actually going to log things
69 if(secinfoenabled("security_exception")) {
70 const size_t maxsize = 32;
71 void* callstack[maxsize];
72
73 int size = backtrace(callstack, maxsize);
74 char** names = backtrace_symbols(callstack, size);
75
76 // C++ symbolicate the callstack
77
78 const char* delim = " ";
79 string build;
80 char * token = NULL;
81 char * line = NULL;
82
83 for(int i = 0; i < size; i++) {
84 build = "";
85
86 line = names[i];
87
88 while((token = strsep(&line, delim))) {
89 if(*token == '\0') {
90 build += " ";
91 } else {
92 int status = 0;
93 char * demangled = abi::__cxa_demangle(token, NULL, NULL, &status);
94 if(status == 0) {
95 build += demangled;
96 } else {
97 build += token;
98 }
99 build += " ";
100
101 if(demangled) {
102 free(demangled);
103 }
104 }
105 }
106
107 secinfo("security_exception", "%s", build.c_str());
108 }
109 free(names);
110 }
111 }
112
113
114
115 //
116 // UnixError exceptions
117 //
118 UnixError::UnixError() : error(errno)
119 {
120 SECURITY_EXCEPTION_THROW_UNIX(this, errno);
121
122 snprintf(whatBuffer, whatBufferSize, "UNIX errno exception: %d", this->error);
123 secnotice("security_exception", "%s", what());
124 LogBacktrace();
125 }
126
127 UnixError::UnixError(int err, bool suppresslogging) : error(err)
128 {
129 SECURITY_EXCEPTION_THROW_UNIX(this, err);
130
131 if(!suppresslogging || secinfoenabled("security_exception")) {
132 snprintf(whatBuffer, whatBufferSize, "UNIX error exception: %d", this->error);
133 secnotice("security_exception", "%s", what());
134 LogBacktrace();
135 }
136 }
137
138 const char *UnixError::what() const _NOEXCEPT
139 {
140 return whatBuffer;
141 }
142
143
144 OSStatus UnixError::osStatus() const
145 {
146 return error + errSecErrnoBase;
147 }
148
149 int UnixError::unixError() const
150 { return error; }
151
152 void UnixError::throwMe(int err) { throw UnixError(err, false); }
153 void UnixError::throwMeNoLogging(int err) { throw UnixError(err, true); }
154
155
156 // @@@ This is a hack for the Network protocol state machine
157 UnixError UnixError::make(int err) { return UnixError(err, false); }
158
159
160 //
161 // MacOSError exceptions
162 //
163 MacOSError::MacOSError(int err) : error(err)
164 {
165 SECURITY_EXCEPTION_THROW_OSSTATUS(this, err);
166
167 snprintf(whatBuffer, whatBufferSize, "MacOS error: %d", this->error);
168 switch (err) {
169 case errSecCSReqFailed:
170 // This 'error' isn't an actual error and doesn't warrant being logged.
171 break;
172 default:
173 secnotice("security_exception", "%s", what());
174 LogBacktrace();
175 }
176 }
177
178 const char *MacOSError::what() const _NOEXCEPT
179 {
180 return whatBuffer;
181 }
182
183 OSStatus MacOSError::osStatus() const
184 { return error; }
185
186 int MacOSError::unixError() const
187 {
188 // embedded UNIX errno values are returned verbatim
189 if (error >= errSecErrnoBase && error <= errSecErrnoLimit)
190 return error - errSecErrnoBase;
191
192 switch (error) {
193 default:
194 // cannot map this to errno space
195 return -1;
196 }
197 }
198
199 void MacOSError::throwMe(int error)
200 { throw MacOSError(error); }
201
202 void MacOSError::throwMe(int error, char const *message, ...)
203 {
204 // Ignoring the message for now, will do something with it later.
205 throw MacOSError(error);
206 }
207
208 MacOSError MacOSError::make(int error)
209 { return MacOSError(error); }
210
211
212 //
213 // CFError exceptions
214 //
215 CFError::CFError()
216 {
217 SECURITY_EXCEPTION_THROW_CF(this);
218 secnotice("security_exception", "CFError");
219 LogBacktrace();
220 }
221
222 const char *CFError::what() const _NOEXCEPT
223 { return "CoreFoundation error"; }
224
225 OSStatus CFError::osStatus() const
226 { return errSecCoreFoundationUnknown; }
227
228 int CFError::unixError() const
229 {
230 return EFAULT; // nothing really matches
231 }
232
233 void CFError::throwMe()
234 { throw CFError(); }
235
236
237
238
239 void ModuleNexusError::throwMe()
240 {
241 throw ModuleNexusError();
242 }
243
244
245
246 OSStatus ModuleNexusError::osStatus() const
247 {
248 return errSecParam;
249 }
250
251
252
253 int ModuleNexusError::unixError() const
254 {
255 return EINVAL;
256 }
257