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 char *string
);
47 Guid(const std::string
&s
);
49 Guid
&operator = (const CSSM_GUID
&rGuid
)
50 { memcpy(this, &rGuid
, sizeof(CSSM_GUID
)); return *this; }
52 bool operator == (const CSSM_GUID
&other
) const
53 { return (this == &other
) || !memcmp(this, &other
, sizeof(CSSM_GUID
)); }
54 bool operator != (const CSSM_GUID
&other
) const
55 { return (this != &other
) && memcmp(this, &other
, sizeof(CSSM_GUID
)); }
56 bool operator < (const CSSM_GUID
&other
) const
57 { return memcmp(this, &other
, sizeof(CSSM_GUID
)) < 0; }
58 size_t hash() const { //@@@ revisit this hash
59 return Data1
+ (Data2
<< 3) + (Data3
<< 11) + (Data4
[3]) + (Data4
[6] << 22);
62 static const unsigned stringRepLength
= 38; // "{x8-x4-x4-x4-x12}"
63 char *toString(char buffer
[stringRepLength
+1]) const; // will append \0
64 string
toString() const; // make std::string
67 void parseGuid(const char *string
);
70 class CssmGuidData
: public CssmData
{
72 CssmGuidData(const CSSM_GUID
&guid
);
75 char buffer
[Guid::stringRepLength
+ 1];
80 // User-friendly CSSM_SUBSERVICE_UIDs
82 class CssmSubserviceUid
: public PodWrapper
<CssmSubserviceUid
, CSSM_SUBSERVICE_UID
> {
84 CssmSubserviceUid() { clearPod(); }
85 CssmSubserviceUid(const CSSM_SUBSERVICE_UID
&rSSuid
) { memcpy(this, &rSSuid
, sizeof(*this)); }
87 CssmSubserviceUid
&operator = (const CSSM_SUBSERVICE_UID
&rSSuid
)
88 { memcpy(this, &rSSuid
, sizeof(CSSM_SUBSERVICE_UID
)); return *this; }
90 bool operator == (const CSSM_SUBSERVICE_UID
&other
) const;
91 bool operator != (const CSSM_SUBSERVICE_UID
&other
) const { return !(*this == other
); }
92 bool operator < (const CSSM_SUBSERVICE_UID
&other
) const;
94 CssmSubserviceUid(const CSSM_GUID
&guid
, const CSSM_VERSION
*version
= NULL
,
95 uint32 subserviceId
= 0,
96 CSSM_SERVICE_TYPE subserviceType
= CSSM_SERVICE_DL
);
98 const ::Guid
&guid() const { return ::Guid::overlay(Guid
); }
99 uint32
subserviceId() const { return SubserviceId
; }
100 CSSM_SERVICE_TYPE
subserviceType() const { return SubserviceType
; }
101 CSSM_VERSION
version() const { return Version
; }
106 // User-friendler CSSM_CRYPTO_DATA objects
108 class CryptoCallback
{
110 CryptoCallback(CSSM_CALLBACK func
, void *ctx
= NULL
) : mFunction(func
), mCtx(ctx
) { }
111 CSSM_CALLBACK
function() const { return mFunction
; }
112 void *context() const { return mCtx
; }
114 CssmData
operator () () const
117 if (CSSM_RETURN err
= mFunction(&output
, mCtx
))
118 CssmError::throwMe(err
);
123 CSSM_CALLBACK mFunction
;
127 class CssmCryptoData
: public PodWrapper
<CssmCryptoData
, CSSM_CRYPTO_DATA
> {
131 CssmCryptoData(const CssmData
¶m
, CSSM_CALLBACK callback
= NULL
, void *ctx
= NULL
)
132 { Param
= const_cast<CssmData
&>(param
); Callback
= callback
; CallerCtx
= ctx
; }
134 CssmCryptoData(const CssmData
¶m
, CryptoCallback
&cb
)
135 { Param
= const_cast<CssmData
&>(param
); Callback
= cb
.function(); CallerCtx
= cb
.context(); }
137 CssmCryptoData(CSSM_CALLBACK callback
, void *ctx
= NULL
)
138 { /* ignore Param */ Callback
= callback
; CallerCtx
= ctx
; }
140 explicit CssmCryptoData(CryptoCallback
&cb
)
141 { /* ignore Param */ Callback
= cb
.function(); CallerCtx
= cb
.context(); }
144 CssmData
¶m() { return CssmData::overlay(Param
); }
145 const CssmData
¶m() const { return CssmData::overlay(Param
); }
146 bool hasCallback() const { return Callback
!= NULL
; }
147 CryptoCallback
callback() const { return CryptoCallback(Callback
, CallerCtx
); }
149 // get the value, whichever way is appropriate
150 CssmData
operator () () const
151 { return hasCallback() ? callback() () : param(); }
154 // a CssmCryptoContext whose callback is a virtual class member
155 class CryptoDataClass
: public CssmCryptoData
{
157 CryptoDataClass() : CssmCryptoData(callbackShim
, this) { }
158 virtual ~CryptoDataClass();
161 virtual CssmData
yield() = 0; // must subclass and implement this
164 static CSSM_RETURN
callbackShim(CSSM_DATA
*output
, void *ctx
);
169 // Other PodWrappers for stuff that is barely useful...
171 class CssmKeySize
: public PodWrapper
<CssmKeySize
, CSSM_KEY_SIZE
> {
174 CssmKeySize(uint32 nom
, uint32 eff
) { LogicalKeySizeInBits
= nom
; EffectiveKeySizeInBits
= eff
; }
175 CssmKeySize(uint32 size
) { LogicalKeySizeInBits
= EffectiveKeySizeInBits
= size
; }
177 uint32
logical() const { return LogicalKeySizeInBits
; }
178 uint32
effective() const { return EffectiveKeySizeInBits
; }
179 operator uint32 () const { return effective(); }
182 inline bool operator == (const CSSM_KEY_SIZE
&s1
, const CSSM_KEY_SIZE
&s2
)
184 return s1
.LogicalKeySizeInBits
== s2
.LogicalKeySizeInBits
185 && s1
.EffectiveKeySizeInBits
== s2
.EffectiveKeySizeInBits
;
188 inline bool operator != (const CSSM_KEY_SIZE
&s1
, const CSSM_KEY_SIZE
&s2
)
189 { return !(s1
== s2
); }
192 class QuerySizeData
: public PodWrapper
<QuerySizeData
, CSSM_QUERY_SIZE_DATA
> {
195 QuerySizeData(uint32 in
) { SizeInputBlock
= in
; SizeOutputBlock
= 0; }
197 uint32
inputSize() const { return SizeInputBlock
; }
198 uint32
inputSize(uint32 size
) { return SizeInputBlock
= size
; }
199 uint32
outputSize() const { return SizeOutputBlock
; }
202 inline bool operator == (const CSSM_QUERY_SIZE_DATA
&s1
, const CSSM_QUERY_SIZE_DATA
&s2
)
204 return s1
.SizeInputBlock
== s2
.SizeInputBlock
205 && s1
.SizeOutputBlock
== s2
.SizeOutputBlock
;
208 inline bool operator != (const CSSM_QUERY_SIZE_DATA
&s1
, const CSSM_QUERY_SIZE_DATA
&s2
)
209 { return !(s1
== s2
); }
212 class CSPOperationalStatistics
:
213 public PodWrapper
<CSPOperationalStatistics
, CSSM_CSP_OPERATIONAL_STATISTICS
> {
218 } // end namespace Security