2 * Copyright (c) 2000-2004,2011-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
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>
34 #include <Security/SecBase.h>
39 // From cssmapple.h - layering break
40 // Where should this go?
42 #define errSecErrnoBase 100000
43 #define errSecErrnoLimit 100255
46 // The base of the exception hierarchy.
48 CommonError::CommonError() : whatBuffer("CommonError")
54 // We strongly encourage catching all exceptions by const reference, so the copy
55 // constructor of our exceptions should never be called.
57 CommonError::CommonError(const CommonError
&source
)
59 strlcpy(whatBuffer
, source
.whatBuffer
, whatBufferSize
);
62 CommonError::~CommonError() throw ()
66 void CommonError::LogBacktrace() {
67 // Only do this work if we're actually going to log things
68 if(secinfoenabled("security_exception")) {
69 const size_t maxsize
= 32;
70 void* callstack
[maxsize
];
72 int size
= backtrace(callstack
, maxsize
);
73 char** names
= backtrace_symbols(callstack
, size
);
75 // C++ symbolicate the callstack
77 const char* delim
= " ";
82 for(int i
= 0; i
< size
; i
++) {
87 while((token
= strsep(&line
, delim
))) {
92 char * demangled
= abi::__cxa_demangle(token
, NULL
, NULL
, &status
);
106 secinfo("security_exception", "%s", build
.c_str());
115 // UnixError exceptions
117 UnixError::UnixError() : error(errno
)
119 SECURITY_EXCEPTION_THROW_UNIX(this, errno
);
121 snprintf(whatBuffer
, whatBufferSize
, "UNIX errno exception: %d", this->error
);
122 secnotice("security_exception", "%s", what());
126 UnixError::UnixError(int err
, bool suppresslogging
) : error(err
)
128 SECURITY_EXCEPTION_THROW_UNIX(this, err
);
130 if(!suppresslogging
|| secinfoenabled("security_exception")) {
131 snprintf(whatBuffer
, whatBufferSize
, "UNIX error exception: %d", this->error
);
132 secnotice("security_exception", "%s", what());
137 const char *UnixError::what() const throw ()
143 OSStatus
UnixError::osStatus() const
145 return error
+ errSecErrnoBase
;
148 int UnixError::unixError() const
151 void UnixError::throwMe(int err
) { throw UnixError(err
, false); }
152 void UnixError::throwMeNoLogging(int err
) { throw UnixError(err
, true); }
155 // @@@ This is a hack for the Network protocol state machine
156 UnixError
UnixError::make(int err
) { return UnixError(err
, false); }
160 // MacOSError exceptions
162 MacOSError::MacOSError(int err
) : error(err
)
164 SECURITY_EXCEPTION_THROW_OSSTATUS(this, err
);
166 snprintf(whatBuffer
, whatBufferSize
, "MacOS error: %d", this->error
);
167 secnotice("security_exception", "%s", what());
171 const char *MacOSError::what() const throw ()
176 OSStatus
MacOSError::osStatus() const
179 int MacOSError::unixError() const
181 // embedded UNIX errno values are returned verbatim
182 if (error
>= errSecErrnoBase
&& error
<= errSecErrnoLimit
)
183 return error
- errSecErrnoBase
;
187 // cannot map this to errno space
192 void MacOSError::throwMe(int error
)
193 { throw MacOSError(error
); }
195 MacOSError
MacOSError::make(int error
)
196 { return MacOSError(error
); }
200 // CFError exceptions
204 SECURITY_EXCEPTION_THROW_CF(this);
205 secnotice("security_exception", "CFError");
209 const char *CFError::what() const throw ()
210 { return "CoreFoundation error"; }
212 OSStatus
CFError::osStatus() const
213 { return errSecCoreFoundationUnknown
; }
215 int CFError::unixError() const
217 return EFAULT
; // nothing really matches
220 void CFError::throwMe()
226 void ModuleNexusError::throwMe()
228 throw ModuleNexusError();
233 OSStatus
ModuleNexusError::osStatus() const
240 int ModuleNexusError::unixError() const