]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cdsa_utilities/lib/cssmpods.cpp
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utilities / lib / cssmpods.cpp
1 /*
2 * Copyright (c) 2000-2006,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // Miscellaneous CSSM PODWrappers
27 //
28 #include <security_cdsa_utilities/cssmpods.h>
29 #include <security_cdsa_utilities/cssmbridge.h>
30 #include <security_utilities/endian.h>
31
32 //
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.
36 //
37 char *Guid::toString(char buffer[stringRepLength+1]) const
38 {
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]);
43 }
44 buffer[24] = '-';
45 for (int n = 2; n < 8; n++)
46 sprintf(buffer + 21 + 2*n, "%2.2hhx", Data4[n]);
47 buffer[37] = '}';
48 buffer[38] = '\0';
49 return buffer;
50 }
51
52 string Guid::toString() const
53 {
54 char buffer[stringRepLength+1];
55 return toString(buffer);
56 }
57
58 Guid::Guid(const char *s)
59 {
60 parseGuid(s);
61 }
62
63 Guid::Guid(const string &s)
64 {
65 parseGuid(s.c_str());
66 }
67
68 void Guid::parseGuid(const char *string)
69 {
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...
75
76 // do sanity checking, don't assume that what's passed in makes sense
77 if (string == NULL)
78 {
79 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
80 }
81
82 // what follows had better be big enough
83 if (strlen(string) < 37) // needed because the code hard codes the length
84 {
85 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
86 }
87
88 int d1;
89 uint16 d2, d3;
90 if (sscanf(string, "{%8x-%4hx-%4hx-", &d1, &d2, &d3) != 3) {
91 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
92 }
93 Data1 = h2n(uint32(d1));
94 Data2 = h2n(d2);
95 Data3 = h2n(d3);
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++) {
99 unsigned char dn;
100 if (sscanf(string + 20 + 2*n + (newForm && n >= 2), "%2hhx", &dn) != 1)
101 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
102 Data4[n] = dn;
103 }
104 if (string[37 - !newForm] != '}')
105 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID);
106 }
107
108
109 CssmGuidData::CssmGuidData(const CSSM_GUID &guid) : CssmData(buffer, sizeof(buffer))
110 {
111 Guid::overlay(guid).toString(buffer);
112 }
113
114
115 //
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.
122 //
123 CssmSubserviceUid::CssmSubserviceUid(const CSSM_GUID &guid,
124 const CSSM_VERSION *version, uint32 subserviceId, CSSM_SERVICE_TYPE subserviceType)
125 {
126 Guid = guid;
127 SubserviceId = subserviceId;
128 SubserviceType = subserviceType;
129 if (version)
130 Version = *version;
131 else
132 Version.Major = Version.Minor = 0;
133 }
134
135
136 bool CssmSubserviceUid::operator == (const CssmSubserviceUid &otherUid) const
137 {
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
143
144 const CssmSubserviceUid &other = CssmSubserviceUid::overlay(otherUid);
145 return subserviceId() == other.subserviceId()
146 && subserviceType() == other.subserviceType()
147 && guid() == other.guid();
148 }
149
150 bool CssmSubserviceUid::operator < (const CssmSubserviceUid &otherUid) const
151 {
152 #pragma clang diagnostic push
153 #pragma clang diagnostic ignored "-Wtautological-undefined-compare"
154 if (&otherUid == 0x0) { return false; }
155 #pragma clang diagnostic pop
156
157 const CssmSubserviceUid &other = CssmSubserviceUid::overlay(otherUid);
158 if (subserviceId() < other.subserviceId())
159 return true;
160 if (subserviceId() > other.subserviceId())
161 return false;
162 if (subserviceType() < other.subserviceType())
163 return true;
164 if (subserviceType() > other.subserviceType())
165 return false;
166 return guid() < other.guid();
167 }
168
169
170 //
171 // CryptoData & friends
172 //
173 CryptoDataClass::~CryptoDataClass()
174 { }
175
176 CSSM_RETURN CryptoDataClass::callbackShim(CSSM_DATA *output, void *ctx)
177 {
178 BEGIN_API_NO_METRICS
179 *output = reinterpret_cast<CryptoDataClass *>(ctx)->yield();
180 END_API(CSSM)
181 }