2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
23 #define _CPP_UTILITIES
26 #include <Security/utilities.h>
27 #include <Security/debugging.h>
32 // The base of the exception hierarchy
34 CssmCommonError::CssmCommonError()
36 debug("exception", "constructing exception at %p", this);
39 CssmCommonError::CssmCommonError(const CssmCommonError
&source
)
41 debug("exception", "constructing exception at %p from %p", this, &source
);
44 CssmCommonError::~CssmCommonError()
46 debug("exception", "destroying exception at %p", this);
49 OSStatus
CssmCommonError::osStatus() const
50 { return cssmError(); }
52 CSSM_RETURN
CssmCommonError::cssmError(CSSM_RETURN base
) const
53 { return CssmError::merge(cssmError(), base
); }
57 // CssmError exceptions
59 CssmError::CssmError(CSSM_RETURN err
) : error(err
) { }
61 const char *CssmError::what() const { return "CSSM exception"; }
63 CSSM_RETURN
CssmError::cssmError() const { return error
; }
65 OSStatus
CssmError::osStatus() const { return error
; }
67 void CssmError::throwMe(CSSM_RETURN err
) { throw CssmError(err
); }
71 // UnixError exceptions
73 UnixError::UnixError() : error(errno
) { }
75 UnixError::UnixError(int err
) : error(err
) { }
77 const char *UnixError::what() const
78 { return "UNIX error exception"; }
80 CSSM_RETURN
UnixError::cssmError() const
82 // @@@ this is a sample - go through and map errnos better
86 return CSSM_ERRCODE_MEMORY_ERROR
;
90 return CSSM_ERRCODE_FUNCTION_NOT_IMPLEMENTED
;
94 return CSSMERR_APPLEDL_DISK_FULL
;
98 return CSSMERR_APPLEDL_QUOTA_EXCEEDED
;
102 return CSSMERR_APPLEDL_FILE_TOO_BIG
;
105 return CSSM_ERRCODE_INTERNAL_ERROR
;
109 OSStatus
UnixError::osStatus() const { return cssmError(); }
111 void UnixError::throwMe(int err
) { throw UnixError(err
); }
113 // @@@ This is a hack for the Network protocol state machine
114 UnixError
UnixError::make(int err
) { return UnixError(err
); }
118 // MacOSError exceptions
120 MacOSError::MacOSError(int err
) : error(err
) { }
122 const char *MacOSError::what() const
123 { return "MacOS error"; }
125 CSSM_RETURN
MacOSError::cssmError() const
126 { return error
; } // @@@ eventually...
128 OSStatus
MacOSError::osStatus() const
131 void MacOSError::throwMe(int error
)
132 { throw MacOSError(error
); }
136 // Manage CSSM errors
138 CSSM_RETURN
CssmError::merge(CSSM_RETURN error
, CSSM_RETURN base
)
140 if (0 < error
&& error
< CSSM_ERRORCODE_COMMON_EXTENT
) {
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.
153 char *Guid::toString(char buffer
[stringRepLength
+1]) const
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
]);
160 for (int n
= 2; n
< 8; n
++)
161 sprintf(buffer
+ 21 + 2*n
, "%2.2x", Data4
[n
]);
167 Guid::Guid(const char *string
)
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...
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
++) {
183 if (sscanf(string
+ 20 + 2*n
+ (newForm
&& n
>= 2), "%2x", &dn
) != 1)
184 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
);
187 if (string
[37 - !newForm
] != '}')
188 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
);
193 // Methods for the CssmKey class
195 CssmKey::CssmKey(CSSM_KEY
&key
)
197 KeyHeader
= key
.KeyHeader
;
198 KeyData
= key
.KeyData
;
199 key
.KeyData
.Length
= 0;
200 key
.KeyData
.Data
= NULL
;
203 CssmKey::CssmKey(CSSM_DATA
&keyData
)
205 memset(this, 0, sizeof(*this));
209 KeyHeader
.HeaderVersion
= CSSM_KEYHEADER_VERSION
;
210 KeyHeader
.BlobType
= CSSM_KEYBLOB_RAW
;
211 KeyHeader
.Format
= CSSM_KEYBLOB_RAW_FORMAT_NONE
;
214 CssmKey::CssmKey(uint32 length
, uint8
*data
)
216 memset(this, 0, sizeof(*this));
217 KeyData
.Length
= length
;
219 KeyHeader
.HeaderVersion
= CSSM_KEYHEADER_VERSION
;
220 KeyHeader
.BlobType
= CSSM_KEYBLOB_RAW
;
221 KeyHeader
.Format
= CSSM_KEYBLOB_RAW_FORMAT_NONE
;
224 CryptoDataClass::~CryptoDataClass()