2 * Copyright (c) 2000-2004,2006,2011,2013-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@
26 // Miscellaneous CSSM PODWrappers
31 #include <security_utilities/utilities.h>
32 #include <security_cdsa_utilities/cssmdata.h>
40 // User-friendly GUIDs
42 class Guid
: public PodWrapper
<Guid
, CSSM_GUID
> {
44 Guid() { /*IFDEBUG(*/ memset(this, 0, sizeof(*this)) /*)*/ ; }
45 Guid(const CSSM_GUID
&rGuid
) { memcpy(this, &rGuid
, sizeof(*this)); }
46 Guid(const Guid
&rGuid
) { memcpy(this, &rGuid
, sizeof(*this)); }
47 Guid(const char *string
);
48 Guid(const std::string
&s
);
50 Guid
&operator = (const Guid
&rGuid
)
51 { memcpy(this, &rGuid
, sizeof(CSSM_GUID
)); return *this; }
53 bool operator == (const Guid
&other
) const
54 { return (this == &other
) || !memcmp(this, &other
, sizeof(CSSM_GUID
)); }
55 bool operator != (const Guid
&other
) const
56 { return (this != &other
) && memcmp(this, &other
, sizeof(CSSM_GUID
)); }
57 bool operator < (const Guid
&other
) const
58 { return memcmp(this, &other
, sizeof(CSSM_GUID
)) < 0; }
59 size_t hash() const { //@@@ revisit this hash
60 return Data1
+ (Data2
<< 3) + (Data3
<< 11) + (Data4
[3]) + (Data4
[6] << 22);
63 static const unsigned stringRepLength
= 38; // "{x8-x4-x4-x4-x12}"
64 char *toString(char buffer
[stringRepLength
+1]) const; // will append \0
65 string
toString() const; // make std::string
68 void parseGuid(const char *string
);
71 class CssmGuidData
: public CssmData
{
73 CssmGuidData(const CSSM_GUID
&guid
);
76 char buffer
[Guid::stringRepLength
+ 1];
81 // User-friendly CSSM_SUBSERVICE_UIDs
83 class CssmSubserviceUid
: public PodWrapper
<CssmSubserviceUid
, CSSM_SUBSERVICE_UID
> {
85 CssmSubserviceUid() { clearPod(); }
86 CssmSubserviceUid(const CSSM_SUBSERVICE_UID
&rSSuid
) { memcpy(this, &rSSuid
, sizeof(*this)); }
88 CssmSubserviceUid
&operator = (const CssmSubserviceUid
&rSSuid
)
89 { memcpy(this, &rSSuid
, sizeof(CSSM_SUBSERVICE_UID
)); return *this; }
91 bool operator == (const CssmSubserviceUid
&other
) const;
92 bool operator != (const CssmSubserviceUid
&other
) const { return !(*this == other
); }
93 bool operator < (const CssmSubserviceUid
&other
) const;
95 CssmSubserviceUid(const CSSM_GUID
&guid
, const CSSM_VERSION
*version
= NULL
,
96 uint32 subserviceId
= 0,
97 CSSM_SERVICE_TYPE subserviceType
= CSSM_SERVICE_DL
);
99 const ::Guid
&guid() const { return ::Guid::overlay(Guid
); }
100 uint32
subserviceId() const { return SubserviceId
; }
101 CSSM_SERVICE_TYPE
subserviceType() const { return SubserviceType
; }
102 CSSM_VERSION
version() const { return Version
; }
107 // User-friendler CSSM_CRYPTO_DATA objects
109 class CryptoCallback
{
111 CryptoCallback(CSSM_CALLBACK func
, void *ctx
= NULL
) : mFunction(func
), mCtx(ctx
) { }
112 CSSM_CALLBACK
function() const { return mFunction
; }
113 void *context() const { return mCtx
; }
115 CssmData
operator () () const
118 if (CSSM_RETURN err
= mFunction(&output
, mCtx
))
119 CssmError::throwMe(err
);
124 CSSM_CALLBACK mFunction
;
128 class CssmCryptoData
: public PodWrapper
<CssmCryptoData
, CSSM_CRYPTO_DATA
> {
132 CssmCryptoData(const CssmData
¶m
, CSSM_CALLBACK callback
= NULL
, void *ctx
= NULL
)
133 { Param
= const_cast<CssmData
&>(param
); Callback
= callback
; CallerCtx
= ctx
; }
135 CssmCryptoData(const CssmData
¶m
, CryptoCallback
&cb
)
136 { Param
= const_cast<CssmData
&>(param
); Callback
= cb
.function(); CallerCtx
= cb
.context(); }
138 CssmCryptoData(CSSM_CALLBACK callback
, void *ctx
= NULL
)
139 { /* ignore Param */ Callback
= callback
; CallerCtx
= ctx
; }
141 explicit CssmCryptoData(CryptoCallback
&cb
)
142 { /* ignore Param */ Callback
= cb
.function(); CallerCtx
= cb
.context(); }
145 CssmData
¶m() { return CssmData::overlay(Param
); }
146 const CssmData
¶m() const { return CssmData::overlay(Param
); }
147 bool hasCallback() const { return Callback
!= NULL
; }
148 CryptoCallback
callback() const { return CryptoCallback(Callback
, CallerCtx
); }
150 // get the value, whichever way is appropriate
151 CssmData
operator () () const
152 { return hasCallback() ? callback() () : param(); }
155 // a CssmCryptoContext whose callback is a virtual class member
156 class CryptoDataClass
: public CssmCryptoData
{
158 CryptoDataClass() : CssmCryptoData(callbackShim
, this) { }
159 virtual ~CryptoDataClass();
162 virtual CssmData
yield() = 0; // must subclass and implement this
165 static CSSM_RETURN
callbackShim(CSSM_DATA
*output
, void *ctx
);
170 // Other PodWrappers for stuff that is barely useful...
172 class CssmKeySize
: public PodWrapper
<CssmKeySize
, CSSM_KEY_SIZE
> {
175 CssmKeySize(uint32 nom
, uint32 eff
) { LogicalKeySizeInBits
= nom
; EffectiveKeySizeInBits
= eff
; }
176 CssmKeySize(uint32 size
) { LogicalKeySizeInBits
= EffectiveKeySizeInBits
= size
; }
178 uint32
logical() const { return LogicalKeySizeInBits
; }
179 uint32
effective() const { return EffectiveKeySizeInBits
; }
180 operator uint32 () const { return effective(); }
183 inline bool operator == (const CSSM_KEY_SIZE
&s1
, const CSSM_KEY_SIZE
&s2
)
185 return s1
.LogicalKeySizeInBits
== s2
.LogicalKeySizeInBits
186 && s1
.EffectiveKeySizeInBits
== s2
.EffectiveKeySizeInBits
;
189 inline bool operator != (const CSSM_KEY_SIZE
&s1
, const CSSM_KEY_SIZE
&s2
)
190 { return !(s1
== s2
); }
193 class QuerySizeData
: public PodWrapper
<QuerySizeData
, CSSM_QUERY_SIZE_DATA
> {
196 QuerySizeData(uint32 in
) { SizeInputBlock
= in
; SizeOutputBlock
= 0; }
198 uint32
inputSize() const { return SizeInputBlock
; }
199 uint32
inputSize(uint32 size
) { return SizeInputBlock
= size
; }
200 uint32
outputSize() const { return SizeOutputBlock
; }
203 inline bool operator == (const CSSM_QUERY_SIZE_DATA
&s1
, const CSSM_QUERY_SIZE_DATA
&s2
)
205 return s1
.SizeInputBlock
== s2
.SizeInputBlock
206 && s1
.SizeOutputBlock
== s2
.SizeOutputBlock
;
209 inline bool operator != (const CSSM_QUERY_SIZE_DATA
&s1
, const CSSM_QUERY_SIZE_DATA
&s2
)
210 { return !(s1
== s2
); }
213 class CSPOperationalStatistics
:
214 public PodWrapper
<CSPOperationalStatistics
, CSSM_CSP_OPERATIONAL_STATISTICS
> {
219 } // end namespace Security