]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/utilities.cpp
716591fc3a7e22cd39aa93b502f17fd929f27677
[apple/security.git] / cdsa / cdsa_utilities / utilities.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // Utilities
21 //
22 #ifdef __MWERKS__
23 #define _CPP_UTILITIES
24 #endif
25
26 #include <Security/utilities.h>
27 #include <Security/debugging.h>
28 #include <stdio.h>
29
30
31 //
32 // The base of the exception hierarchy
33 //
34 CssmCommonError::CssmCommonError()
35 {
36 debug("exception", "constructing exception at %p", this);
37 }
38
39 CssmCommonError::CssmCommonError(const CssmCommonError &source)
40 {
41 debug("exception", "constructing exception at %p from %p", this, &source);
42 }
43
44 CssmCommonError::~CssmCommonError()
45 {
46 debug("exception", "destroying exception at %p", this);
47 }
48
49 OSStatus CssmCommonError::osStatus() const
50 { return cssmError(); }
51
52 CSSM_RETURN CssmCommonError::cssmError(CSSM_RETURN base) const
53 { return CssmError::merge(cssmError(), base); }
54
55
56 //
57 // CssmError exceptions
58 //
59 CssmError::CssmError(CSSM_RETURN err) : error(err) { }
60
61 const char *CssmError::what() const { return "CSSM exception"; }
62
63 CSSM_RETURN CssmError::cssmError() const { return error; }
64
65 OSStatus CssmError::osStatus() const { return error; }
66
67 void CssmError::throwMe(CSSM_RETURN err) { throw CssmError(err); }
68
69
70 //
71 // UnixError exceptions
72 //
73 UnixError::UnixError() : error(errno) { }
74
75 UnixError::UnixError(int err) : error(err) { }
76
77 const char *UnixError::what() const
78 { return "UNIX error exception"; }
79
80 CSSM_RETURN UnixError::cssmError() const
81 {
82 // @@@ this is a sample - go through and map errnos better
83 switch (error) {
84 #if defined(ENOMEM)
85 case ENOMEM:
86 return CSSM_ERRCODE_MEMORY_ERROR;
87 #endif
88 #if defined(ENOSYS)
89 case ENOSYS:
90 return CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED;
91 #endif
92 #if defined(ENOSPC)
93 case ENOSPC:
94 return CSSMERR_APPLEDL_DISK_FULL;
95 #endif
96 #if defined(EDQUOT)
97 case EDQUOT:
98 return CSSMERR_APPLEDL_QUOTA_EXCEEDED;
99 #endif
100 #if defined(EFBIG)
101 case EFBIG:
102 return CSSMERR_APPLEDL_FILE_TOO_BIG;
103 #endif
104 default:
105 return CSSM_ERRCODE_INTERNAL_ERROR;
106 }
107 }
108
109 OSStatus UnixError::osStatus() const { return cssmError(); }
110
111 void UnixError::throwMe(int err) { throw UnixError(err); }
112
113 // @@@ This is a hack for the Network protocol state machine
114 UnixError UnixError::make(int err) { return UnixError(err); }
115
116
117 //
118 // MacOSError exceptions
119 //
120 MacOSError::MacOSError(int err) : error(err) { }
121
122 const char *MacOSError::what() const
123 { return "MacOS error"; }
124
125 CSSM_RETURN MacOSError::cssmError() const
126 { return error; } // @@@ eventually...
127
128 OSStatus MacOSError::osStatus() const
129 { return error; }
130
131 void MacOSError::throwMe(int error)
132 { throw MacOSError(error); }
133
134
135 //
136 // Manage CSSM errors
137 //
138 CSSM_RETURN CssmError::merge(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 //
149 // GUID <-> string conversions.
150 // Note that we DO check for {} on input and insist on rigid formatting.
151 // We don't require a terminating null byte on input, but generate it on output.
152 //
153 char *Guid::toString(char buffer[stringRepLength+1]) const
154 {
155 sprintf(buffer, "{%8.8lx-%4.4x-%4.4x-",
156 (unsigned long)Data1, unsigned(Data2), unsigned(Data3));
157 for (int n = 0; n < 2; n++)
158 sprintf(buffer + 20 + 2*n, "%2.2x", Data4[n]);
159 buffer[24] = '-';
160 for (int n = 2; n < 8; n++)
161 sprintf(buffer + 21 + 2*n, "%2.2x", Data4[n]);
162 buffer[37] = '}';
163 buffer[38] = '\0';
164 return buffer;
165 }
166
167 Guid::Guid(const char *string)
168 {
169 // Arguably, we should be more flexible on input. But exactly what
170 // padding rules should we follow, and how should we try to interprete
171 // "doubtful" variations? Given that GUIDs are essentially magic
172 // cookies, everybody's better off if we just cut-and-paste them
173 // around the universe...
174 unsigned long d1;
175 unsigned int d2, d3;
176 if (sscanf(string, "{%lx-%x-%x-", &d1, &d2, &d3) != 3)
177 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
178 Data1 = d1; Data2 = d2; Data3 = d3;
179 // once, we did not expect the - after byte 2 of Data4
180 bool newForm = string[24] == '-';
181 for (int n = 0; n < 8; n++) {
182 unsigned int dn;
183 if (sscanf(string + 20 + 2*n + (newForm && n >= 2), "%2x", &dn) != 1)
184 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
185 Data4[n] = dn;
186 }
187 if (string[37 - !newForm] != '}')
188 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
189 }
190
191
192 //
193 // Methods for the CssmKey class
194 //
195 CssmKey::CssmKey(CSSM_KEY &key)
196 {
197 KeyHeader = key.KeyHeader;
198 KeyData = key.KeyData;
199 key.KeyData.Length = 0;
200 key.KeyData.Data = NULL;
201 }
202
203 CssmKey::CssmKey(CSSM_DATA &keyData)
204 {
205 memset(this, 0, sizeof(*this));
206 KeyData = keyData;
207 keyData.Length = 0;
208 keyData.Data = NULL;
209 KeyHeader.HeaderVersion = CSSM_KEYHEADER_VERSION;
210 KeyHeader.BlobType = CSSM_KEYBLOB_RAW;
211 KeyHeader.Format = CSSM_KEYBLOB_RAW_FORMAT_NONE;
212 }
213
214 CssmKey::CssmKey(uint32 length, uint8 *data)
215 {
216 memset(this, 0, sizeof(*this));
217 KeyData.Length = length;
218 KeyData.Data = data;
219 KeyHeader.HeaderVersion = CSSM_KEYHEADER_VERSION;
220 KeyHeader.BlobType = CSSM_KEYBLOB_RAW;
221 KeyHeader.Format = CSSM_KEYBLOB_RAW_FORMAT_NONE;
222 }
223
224 CryptoDataClass::~CryptoDataClass()
225 {
226 }
227
228 //
229 // Debug support
230 //
231 #if !defined(NDEBUG)
232
233 #endif //NDEBUG