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> 
  35 #include <Security/CSCommon.h> 
  40 // From cssmapple.h - layering break 
  41 // Where should this go? 
  43 #define errSecErrnoBase 100000 
  44 #define errSecErrnoLimit 100255 
  47 // The base of the exception hierarchy. 
  49 CommonError::CommonError() : whatBuffer("CommonError") 
  55 // We strongly encourage catching all exceptions by const reference, so the copy 
  56 // constructor of our exceptions should never be called. 
  58 CommonError::CommonError(const CommonError 
&source
) 
  60     strlcpy(whatBuffer
, source
.whatBuffer
, whatBufferSize
); 
  63 CommonError::~CommonError() throw () 
  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
]; 
  73         int size 
= backtrace(callstack
, maxsize
); 
  74         char** names 
= backtrace_symbols(callstack
, size
); 
  76         // C++ symbolicate the callstack 
  78         const char* delim 
= " "; 
  83         for(int i 
= 0; i 
< size
; i
++) { 
  88             while((token 
= strsep(&line
, delim
))) { 
  93                     char * demangled 
= abi::__cxa_demangle(token
, NULL
, NULL
, &status
); 
 107             secinfo("security_exception", "%s", build
.c_str()); 
 116 // UnixError exceptions 
 118 UnixError::UnixError() : error(errno
) 
 120     SECURITY_EXCEPTION_THROW_UNIX(this, errno
); 
 122     snprintf(whatBuffer
, whatBufferSize
, "UNIX errno exception: %d", this->error
); 
 123     secnotice("security_exception", "%s", what()); 
 127 UnixError::UnixError(int err
, bool suppresslogging
) : error(err
) 
 129     SECURITY_EXCEPTION_THROW_UNIX(this, err
); 
 131     if(!suppresslogging 
|| secinfoenabled("security_exception")) { 
 132         snprintf(whatBuffer
, whatBufferSize
, "UNIX error exception: %d", this->error
); 
 133         secnotice("security_exception", "%s", what()); 
 138 const char *UnixError::what() const throw () 
 144 OSStatus 
UnixError::osStatus() const 
 146         return error 
+ errSecErrnoBase
; 
 149 int UnixError::unixError() const 
 152 void UnixError::throwMe(int err
) { throw UnixError(err
, false); } 
 153 void UnixError::throwMeNoLogging(int err
) { throw UnixError(err
, true); } 
 156 // @@@ This is a hack for the Network protocol state machine 
 157 UnixError 
UnixError::make(int err
) { return UnixError(err
, false); } 
 161 // MacOSError exceptions 
 163 MacOSError::MacOSError(int err
) : error(err
) 
 165     SECURITY_EXCEPTION_THROW_OSSTATUS(this, err
); 
 167     snprintf(whatBuffer
, whatBufferSize
, "MacOS error: %d", this->error
); 
 169         case errSecCSReqFailed
: 
 170             // This 'error' isn't an actual error and doesn't warrant being logged. 
 173             secnotice("security_exception", "%s", what()); 
 178 const char *MacOSError::what() const throw () 
 183 OSStatus 
MacOSError::osStatus() const 
 186 int MacOSError::unixError() const 
 188         // embedded UNIX errno values are returned verbatim 
 189         if (error 
>= errSecErrnoBase 
&& error 
<= errSecErrnoLimit
) 
 190                 return error 
- errSecErrnoBase
; 
 194                 // cannot map this to errno space 
 199 void MacOSError::throwMe(int error
) 
 200 { throw MacOSError(error
); } 
 202 void MacOSError::throwMe(int error
, char const *message
, ...) 
 204     // Ignoring the message for now, will do something with it later. 
 205     throw MacOSError(error
); 
 208 MacOSError 
MacOSError::make(int error
) 
 209 { return MacOSError(error
); } 
 213 // CFError exceptions 
 217     SECURITY_EXCEPTION_THROW_CF(this); 
 218     secnotice("security_exception", "CFError"); 
 222 const char *CFError::what() const throw () 
 223 { return "CoreFoundation error"; } 
 225 OSStatus 
CFError::osStatus() const 
 226 { return errSecCoreFoundationUnknown
; } 
 228 int CFError::unixError() const 
 230         return EFAULT
;          // nothing really matches 
 233 void CFError::throwMe() 
 239 void ModuleNexusError::throwMe() 
 241     throw ModuleNexusError(); 
 246 OSStatus 
ModuleNexusError::osStatus() const 
 253 int ModuleNexusError::unixError() const