2 * Copyright (c) 2000-2006,2011-2012,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
28 #include <security_cdsa_utilities/cssmpods.h>
29 #include <security_cdsa_utilities/cssmbridge.h>
30 #include <security_utilities/endian.h>
33 // GUID <-> string conversions.
34 // Note that we DO check for {} on input and insist on rigid formatting.
35 // We don't require a terminating null byte on input, but generate it on output.
37 char *Guid::toString(char buffer
[stringRepLength
+1]) const
39 sprintf(buffer
, "{%8.8x-%4.4hx-%4.4hx-",
40 int(n2h(Data1
)), n2h(Data2
), n2h(Data3
));
41 for (int n
= 0; n
< 2; n
++) {
42 sprintf(buffer
+ 20 + 2*n
, "%2.2hhx", Data4
[n
]);
45 for (int n
= 2; n
< 8; n
++)
46 sprintf(buffer
+ 21 + 2*n
, "%2.2hhx", Data4
[n
]);
52 string
Guid::toString() const
54 char buffer
[stringRepLength
+1];
55 return toString(buffer
);
58 Guid::Guid(const char *s
)
63 Guid::Guid(const string
&s
)
68 void Guid::parseGuid(const char *string
)
70 // Arguably, we should be more flexible on input. But exactly what
71 // padding rules should we follow, and how should we try to interprete
72 // "doubtful" variations? Given that GUIDs are essentially magic
73 // cookies, everybody's better off if we just cut-and-paste them
74 // around the universe...
76 // do sanity checking, don't assume that what's passed in makes sense
79 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
);
82 // what follows had better be big enough
83 if (strlen(string
) < 37) // needed because the code hard codes the length
85 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
);
90 if (sscanf(string
, "{%8x-%4hx-%4hx-", &d1
, &d2
, &d3
) != 3) {
91 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
);
93 Data1
= h2n(uint32(d1
));
96 // once, we did not expect the - after byte 2 of Data4
97 bool newForm
= string
[24] == '-';
98 for (int n
= 0; n
< 8; n
++) {
100 if (sscanf(string
+ 20 + 2*n
+ (newForm
&& n
>= 2), "%2hhx", &dn
) != 1)
101 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
);
104 if (string
[37 - !newForm
] != '}')
105 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
);
109 CssmGuidData::CssmGuidData(const CSSM_GUID
&guid
) : CssmData(buffer
, sizeof(buffer
))
111 Guid::overlay(guid
).toString(buffer
);
116 // CssmSubserviceUids.
117 // Note that for comparison, we ignore the version field.
118 // This is not necessarily the Right Choice, but suits certain
119 // constraints in the Sec* layer. Perhaps we might reconsider
120 // this after a thorough code review to determine the intended
121 // (by the standard) semantics and proper use. Yeah, right.
123 CssmSubserviceUid::CssmSubserviceUid(const CSSM_GUID
&guid
,
124 const CSSM_VERSION
*version
, uint32 subserviceId
, CSSM_SERVICE_TYPE subserviceType
)
127 SubserviceId
= subserviceId
;
128 SubserviceType
= subserviceType
;
132 Version
.Major
= Version
.Minor
= 0;
136 bool CssmSubserviceUid::operator == (const CssmSubserviceUid
&otherUid
) const
138 // make sure we don't crash if we get bad data
139 #pragma clang diagnostic push
140 #pragma clang diagnostic ignored "-Wtautological-undefined-compare"
141 if (&otherUid
== 0x0) { return false; }
142 #pragma clang diagnostic pop
144 const CssmSubserviceUid
&other
= CssmSubserviceUid::overlay(otherUid
);
145 return subserviceId() == other
.subserviceId()
146 && subserviceType() == other
.subserviceType()
147 && guid() == other
.guid();
150 bool CssmSubserviceUid::operator < (const CssmSubserviceUid
&otherUid
) const
152 #pragma clang diagnostic push
153 #pragma clang diagnostic ignored "-Wtautological-undefined-compare"
154 if (&otherUid
== 0x0) { return false; }
155 #pragma clang diagnostic pop
157 const CssmSubserviceUid
&other
= CssmSubserviceUid::overlay(otherUid
);
158 if (subserviceId() < other
.subserviceId())
160 if (subserviceId() > other
.subserviceId())
162 if (subserviceType() < other
.subserviceType())
164 if (subserviceType() > other
.subserviceType())
166 return guid() < other
.guid();
171 // CryptoData & friends
173 CryptoDataClass::~CryptoDataClass()
176 CSSM_RETURN
CryptoDataClass::callbackShim(CSSM_DATA
*output
, void *ctx
)
179 *output
= reinterpret_cast<CryptoDataClass
*>(ctx
)->yield();