]> git.saurik.com Git - apple/security.git/blame - OSX/libsecurity_cdsa_utilities/lib/cssmerrors.cpp
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utilities / lib / cssmerrors.cpp
CommitLineData
b1ab9ed8 1/*
d8f41ccd 2 * Copyright (c) 2000-2004,2006,2011,2013-2014 Apple Inc. All Rights Reserved.
866f8763 3 *
b1ab9ed8 4 * @APPLE_LICENSE_HEADER_START@
866f8763 5 *
b1ab9ed8
A
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.
866f8763 12 *
b1ab9ed8
A
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.
866f8763 20 *
b1ab9ed8
A
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>
427c49bc 31#include <Security/SecBase.h>
fa7225c8 32#include <Security/SecBasePriv.h>
b1ab9ed8
A
33
34namespace Security {
35
36
866f8763 37CssmError::CssmError(CSSM_RETURN err, bool suppresslogging) : error(err)
b1ab9ed8
A
38{
39 SECURITY_EXCEPTION_THROW_CSSM(this, err);
fa7225c8 40
866f8763
A
41 if(!suppresslogging || secinfoenabled("security_exception")) {
42 snprintf(whatBuffer, whatBufferSize, "CSSM Exception: %d %s", err, cssmErrorString(err));
43 switch(err) {
44 /* reduce log noise by filtering out some non-error exceptions */
45 case CSSMERR_CL_UNKNOWN_TAG:
46 break;
47 default:
48 secnotice("security_exception", "%s", what());
49 LogBacktrace();
50 break;
51 }
6b200bc3 52 }
b1ab9ed8
A
53}
54
55
d64be36e 56const char *CssmError::what() const _NOEXCEPT
b1ab9ed8 57{
fa7225c8 58 return whatBuffer;
b1ab9ed8
A
59}
60
61
62OSStatus CssmError::osStatus() const
63{
64 if (error == CSSM_ERRCODE_INVALID_POINTER)
65 {
427c49bc 66 return errSecParam;
b1ab9ed8 67 }
866f8763 68
b1ab9ed8
A
69 return error;
70}
71
72
73int CssmError::unixError() const
74{
75 OSStatus err = osStatus();
866f8763 76
b1ab9ed8
A
77 // embedded UNIX errno values are returned verbatim
78 if (err >= errSecErrnoBase && err <= errSecErrnoLimit)
79 return err - errSecErrnoBase;
866f8763 80
b1ab9ed8
A
81 // re-map certain CSSM errors
82 switch (err) {
83 case CSSM_ERRCODE_MEMORY_ERROR:
84 return ENOMEM;
85 case CSSMERR_APPLEDL_DISK_FULL:
86 return ENOSPC;
87 case CSSMERR_APPLEDL_QUOTA_EXCEEDED:
88 return EDQUOT;
89 case CSSMERR_APPLEDL_FILE_TOO_BIG:
90 return EFBIG;
91 default:
92 // cannot map this to errno space
93 return -1;
94 }
95}
96
97
98void CssmError::throwMe(CSSM_RETURN err)
99{
866f8763
A
100 throw CssmError(err, false);
101}
102
103void CssmError::throwMeNoLogging(CSSM_RETURN err)
104{
105 throw CssmError(err, true);
b1ab9ed8
A
106}
107
108
109CSSM_RETURN CssmError::merge(CSSM_RETURN error, CSSM_RETURN base)
110{
111 if (0 < error && error < CSSM_ERRORCODE_COMMON_EXTENT) {
112 return base + error;
113 } else {
114 return error;
115 }
116}
117
118//
119// Get a CSSM_RETURN from a CommonError
120//
121CSSM_RETURN CssmError::cssmError(const CommonError &error, CSSM_RETURN base)
122{
123 if (const CssmError *cssm = dynamic_cast<const CssmError *>(&error)) {
124 return cssmError(cssm->error, base);
125 } else if (const MachPlusPlus::Error *mach = dynamic_cast<const MachPlusPlus::Error *>(&error)) {
126 switch (mach->error) {
127 case BOOTSTRAP_UNKNOWN_SERVICE:
128 case MIG_SERVER_DIED:
129 return CSSM_ERRCODE_SERVICE_NOT_AVAILABLE;
866f8763 130 default:
b1ab9ed8
A
131 return CSSM_ERRCODE_INTERNAL_ERROR;
132 }
133 } else {
134 return error.osStatus();
135 }
136}
137
138CSSM_RETURN CssmError::cssmError(CSSM_RETURN error, CSSM_RETURN base)
139{
140 if (0 < error && error < CSSM_ERRORCODE_COMMON_EXTENT) {
141 return base + error;
142 } else {
143 return error;
144 }
145}
146
147
148} // namespace Security