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>
32 #include <Security/SecBase.h>
37 // From cssmapple.h - layering break
38 // Where should this go?
40 #define errSecErrnoBase 100000
41 #define errSecErrnoLimit 100255
44 // The base of the exception hierarchy.
46 CommonError::CommonError() : whatBuffer("CommonError")
52 // We strongly encourage catching all exceptions by const reference, so the copy
53 // constructor of our exceptions should never be called.
55 CommonError::CommonError(const CommonError
&source
)
57 strlcpy(whatBuffer
, source
.whatBuffer
, whatBufferSize
);
60 CommonError::~CommonError() throw ()
64 void CommonError::LogBacktrace() {
65 // Only do this work if we're actually going to log things
66 if(secinfoenabled("security_exception")) {
67 const size_t maxsize
= 32;
68 void* callstack
[maxsize
];
70 int size
= backtrace(callstack
, maxsize
);
71 char** names
= backtrace_symbols(callstack
, size
);
73 // C++ symbolicate the callstack
75 const char* delim
= " ";
80 for(int i
= 0; i
< size
; i
++) {
85 while((token
= strsep(&line
, delim
))) {
90 char * demangled
= abi::__cxa_demangle(token
, NULL
, NULL
, &status
);
104 secinfo("security_exception", "%s", build
.c_str());
113 // UnixError exceptions
115 UnixError::UnixError() : error(errno
)
117 SECURITY_EXCEPTION_THROW_UNIX(this, errno
);
119 snprintf(whatBuffer
, whatBufferSize
, "UNIX errno exception: %d", this->error
);
120 secnotice("security_exception", "%s", what());
124 UnixError::UnixError(int err
) : error(err
)
126 SECURITY_EXCEPTION_THROW_UNIX(this, err
);
128 snprintf(whatBuffer
, whatBufferSize
, "UNIX error exception: %d", this->error
);
129 secnotice("security_exception", "%s", what());
133 const char *UnixError::what() const throw ()
139 OSStatus
UnixError::osStatus() const
141 return error
+ errSecErrnoBase
;
144 int UnixError::unixError() const
147 void UnixError::throwMe(int err
) { throw UnixError(err
); }
149 // @@@ This is a hack for the Network protocol state machine
150 UnixError
UnixError::make(int err
) { return UnixError(err
); }
154 // MacOSError exceptions
156 MacOSError::MacOSError(int err
) : error(err
)
158 SECURITY_EXCEPTION_THROW_OSSTATUS(this, err
);
160 snprintf(whatBuffer
, whatBufferSize
, "MacOS error: %d", this->error
);
161 secnotice("security_exception", "%s", what());
165 const char *MacOSError::what() const throw ()
170 OSStatus
MacOSError::osStatus() const
173 int MacOSError::unixError() const
175 // embedded UNIX errno values are returned verbatim
176 if (error
>= errSecErrnoBase
&& error
<= errSecErrnoLimit
)
177 return error
- errSecErrnoBase
;
181 // cannot map this to errno space
186 void MacOSError::throwMe(int error
)
187 { throw MacOSError(error
); }
189 MacOSError
MacOSError::make(int error
)
190 { return MacOSError(error
); }
194 // CFError exceptions
198 SECURITY_EXCEPTION_THROW_CF(this);
199 secnotice("security_exception", "CFError");
203 const char *CFError::what() const throw ()
204 { return "CoreFoundation error"; }
206 OSStatus
CFError::osStatus() const
207 { return errSecCoreFoundationUnknown
; }
209 int CFError::unixError() const
211 return EFAULT
; // nothing really matches
214 void CFError::throwMe()
220 void ModuleNexusError::throwMe()
222 throw ModuleNexusError();
227 OSStatus
ModuleNexusError::osStatus() const
234 int ModuleNexusError::unixError() const