]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cdsa_utilities/lib/cssmerrors.cpp
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utilities / lib / cssmerrors.cpp
1 /*
2 * Copyright (c) 2000-2004,2006,2011,2013-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 // cssmerrors
27 //
28 #include <security_cdsa_utilities/cssmerrors.h>
29 #include <security_utilities/mach++.h>
30 #include <Security/cssmapple.h>
31 #include <Security/SecBase.h>
32 #include <Security/SecBasePriv.h>
33
34 namespace Security {
35
36
37 CssmError::CssmError(CSSM_RETURN err) : error(err)
38 {
39 SECURITY_EXCEPTION_THROW_CSSM(this, err);
40
41 snprintf(whatBuffer, whatBufferSize, "CSSM Exception: %d %s", err, cssmErrorString(err));
42 secnotice("security_exception", "%s", what());
43 LogBacktrace();
44 }
45
46
47 const char *CssmError::what() const throw ()
48 {
49 return whatBuffer;
50 }
51
52
53 OSStatus CssmError::osStatus() const
54 {
55 if (error == CSSM_ERRCODE_INVALID_POINTER)
56 {
57 return errSecParam;
58 }
59
60 return error;
61 }
62
63
64 int CssmError::unixError() const
65 {
66 OSStatus err = osStatus();
67
68 // embedded UNIX errno values are returned verbatim
69 if (err >= errSecErrnoBase && err <= errSecErrnoLimit)
70 return err - errSecErrnoBase;
71
72 // re-map certain CSSM errors
73 switch (err) {
74 case CSSM_ERRCODE_MEMORY_ERROR:
75 return ENOMEM;
76 case CSSMERR_APPLEDL_DISK_FULL:
77 return ENOSPC;
78 case CSSMERR_APPLEDL_QUOTA_EXCEEDED:
79 return EDQUOT;
80 case CSSMERR_APPLEDL_FILE_TOO_BIG:
81 return EFBIG;
82 default:
83 // cannot map this to errno space
84 return -1;
85 }
86 }
87
88
89 void CssmError::throwMe(CSSM_RETURN err)
90 {
91 throw CssmError(err);
92 }
93
94
95 CSSM_RETURN CssmError::merge(CSSM_RETURN error, CSSM_RETURN base)
96 {
97 if (0 < error && error < CSSM_ERRORCODE_COMMON_EXTENT) {
98 return base + error;
99 } else {
100 return error;
101 }
102 }
103
104 //
105 // Get a CSSM_RETURN from a CommonError
106 //
107 CSSM_RETURN CssmError::cssmError(const CommonError &error, CSSM_RETURN base)
108 {
109 if (const CssmError *cssm = dynamic_cast<const CssmError *>(&error)) {
110 return cssmError(cssm->error, base);
111 } else if (const MachPlusPlus::Error *mach = dynamic_cast<const MachPlusPlus::Error *>(&error)) {
112 switch (mach->error) {
113 case BOOTSTRAP_UNKNOWN_SERVICE:
114 case MIG_SERVER_DIED:
115 return CSSM_ERRCODE_SERVICE_NOT_AVAILABLE;
116 default:
117 return CSSM_ERRCODE_INTERNAL_ERROR;
118 }
119 } else {
120 return error.osStatus();
121 }
122 }
123
124 CSSM_RETURN CssmError::cssmError(CSSM_RETURN error, CSSM_RETURN base)
125 {
126 if (0 < error && error < CSSM_ERRORCODE_COMMON_EXTENT) {
127 return base + error;
128 } else {
129 return error;
130 }
131 }
132
133
134 } // namespace Security